diff --git a/interfaces/common/include/common_func.h b/interfaces/common/include/common_func.h index 9603016e65d103075af0ac2ffaa468adf876b341..bb5e95abbffd9df7d613db527c97375b3a9dfaca 100644 --- a/interfaces/common/include/common_func.h +++ b/interfaces/common/include/common_func.h @@ -21,10 +21,19 @@ namespace OHOS { namespace AppFileService { +enum class ModeSetting : int32_t { + O_RDWR_FILE_URI = 2, +}; + class CommonFunc { public: static std::string GetSelfBundleName(); static std::string GetUriFromPath(const std::string &path); + static int32_t GrantPolicy(std::string uri, int32_t tokenId, ModeSetting modeSetting); + static int32_t PersistPermission(std::string uri, ModeSetting modeSetting); + static int32_t DesistPersistPermission(std::string uri, ModeSetting modeSetting); + static int32_t ActivateAccessingUri(std::string uri); + static int32_t DeactivateAccessingUri(std::string uri); }; } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index 74f715a78e0ae31e13794b07911d3043bd74d213..a8bbec230b82ab542b3e3a4f37adc107526eeb96 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -24,6 +24,7 @@ #include "log.h" #include "sandbox_helper.h" +#include "uri.h" using namespace std; @@ -95,6 +96,41 @@ string CommonFunc::GetUriFromPath(const string &path) realPath = FILE_SCHEME_PREFIX + packageName + SandboxHelper::Encode(realPath); return realPath; } + +int32_t CommonFunc::GrantPolicy(std::string uri, int32_t tokenId, ModeSetting modeSetting) +{ + Uri uriObject(uri); + string path = uriObject.GetPath(); + return 0; +} + +int32_t CommonFunc::PersistPermission(std::string uri, ModeSetting modeSetting) +{ + Uri uriObject(uri); + string path = uriObject.GetPath(); + return 0; +} + +int32_t CommonFunc::DesistPersistPermission(std::string uri, ModeSetting modeSetting) +{ + Uri uriObject(uri); + string path = uriObject.GetPath(); + return 0; +} + +int32_t CommonFunc::ActivateAccessingUri(std::string uri) +{ + Uri uriObject(uri); + string path = uriObject.GetPath(); + return 0; +} + +int32_t CommonFunc::DeactivateAccessingUri(std::string uri) +{ + Uri uriObject(uri); + string path = uriObject.GetPath(); + return 0; +} } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h index 2f146ade8e7930cc5ee037b0f624b7cae9ee4d93..c0f5b7593d66b0e336b4ad1c0efceadd80fd81ea 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -31,10 +31,11 @@ public: explicit FileUri(const std::string &uriOrPath); ~FileUri() = default; + private: Uri uri_; }; -} // ModuleFileUri -} // namespace AppFileService -} // namespace OHOS -#endif // APP_FILE_SERVICE_FILE_URI_FILE_URI_H \ No newline at end of file +} // namespace ModuleFileUri +} // namespace AppFileService +} // namespace OHOS +#endif // APP_FILE_SERVICE_FILE_URI_FILE_URI_H \ No newline at end of file diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 4838bf9b166d0f63e9d6c9d1f9e84b6530226f87..7b791f73d6ec88943938990180087b0f98fede23 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -67,8 +67,7 @@ string FileUri::GetRealPath() string realPath = sandboxPath; string bundleName = uri_.GetAuthority(); LOGD("GetRealPath decode path is %{private}s", sandboxPath.c_str()); - if (bundleName == FILE_MANAGER_AUTHORITY && - uri_.ToString().find(NETWORK_PARA) == string::npos && + if (bundleName == FILE_MANAGER_AUTHORITY && uri_.ToString().find(NETWORK_PARA) == string::npos && access(realPath.c_str(), F_OK) == 0) { return realPath; } @@ -88,10 +87,10 @@ string FileUri::ToString() return uri_.ToString(); } -FileUri::FileUri(const string &uriOrPath): uri_( - (uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath) -) -{} +FileUri::FileUri(const string &uriOrPath) + : uri_((uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath)) +{ } -} // namespace AppFileService -} // namespace OHOS \ No newline at end of file +} // namespace ModuleFileUri +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 47ecdfc165742d9a6667dd99b2246e59e72931d1..73263ea40b7835e4d430a918b38bfb9f6e502288 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -98,6 +98,8 @@ ohos_shared_library("fileuri") { external_deps = [ "ability_base:zuri", "ability_runtime:abilitykit_native", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp index d9dec0e39463cc58097a224faf90669e42615f3c..003f59287d64e2bf1d933e8faa69886ed6ec4d2f 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp @@ -53,7 +53,6 @@ napi_value FileUriNExporter::Constructor(napi_env env, napi_callback_info info) return funcArg.GetThisVar(); } - napi_value FileUriNExporter::UriToString(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.cpp b/interfaces/kits/js/file_uri/get_uri_from_path.cpp index da98d8611ae92487b4aca1a7985b2b6b738ed2ec..57b5f2a778a2212305c20f3f89d1c4f0f09d1633 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -19,11 +19,19 @@ #include "common_func.h" #include "log.h" +#include "access_token.h" +#include "accesstoken_kit.h" +#include "ipc_skeleton.h" +#include "tokenid_kit.h" + + namespace OHOS { namespace AppFileService { namespace ModuleFileUri { using namespace OHOS::FileManagement::LibN; +const std::string FILE_ACCESS_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; + napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -49,6 +57,261 @@ napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) return NVal::CreateUTF8String(env, uri).val_; } +napi_value GetUriFromPath::GrantPolicy(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE, NARG_CNT::FOUR)) { + LOGE("GrantPolicy Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + if (!IsSystemApp()) { + LOGE("GrantPolicy is not System App!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + auto [succUri, fileUri] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + auto [succTokenId, id] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + auto [succModeSetting, mode] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + ModeSetting modeSetting = static_cast(mode); + int32_t tokenId = id; + std::string uri = fileUri; + if (succUri || !succTokenId || !succModeSetting) { + LOGE("The first/second argument error"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + auto cbExec = [uri, tokenId, modeSetting]() -> NError { + int32_t ret = CommonFunc::GrantPolicy(uri, tokenId, modeSetting); + return NError(ret); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + const string PROCEDURENAME = "grant_policy"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::THREE) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FOURTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } +} + +napi_value GetUriFromPath::PersistPermission(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + LOGE("PersistPermission Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + if (!GetUriFromPath::CheckPermission(FILE_ACCESS_PERMISSION)) { + LOGE("PersistPermission has not ohos permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + + auto [succUri, fileUri] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + auto [succModeSetting, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + ModeSetting modeSetting = static_cast(mode); + std::string uri = fileUri; + + if (!succModeSetting || !succUri) { + LOGE("The argument error"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = [uri, modeSetting]() -> NError { + int32_t ret = CommonFunc::PersistPermission(uri, modeSetting); + return NError(ret); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + const string PROCEDURENAME = "persist_permission"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::TWO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } +} + +napi_value GetUriFromPath::DesistPersistPermission(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + LOGE("DesistPersistPermission Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + if (!GetUriFromPath::CheckPermission(FILE_ACCESS_PERMISSION)) { + LOGE("DesistPersistPermission has not ohos permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + auto [succUri, fileUri] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + auto [succModeSetting, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + ModeSetting modeSetting = static_cast(mode); + std::string uri = fileUri; + + if (!succModeSetting || !succUri) { + LOGE("The argument error"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = [uri, modeSetting]() -> NError { + int32_t ret = CommonFunc::DesistPersistPermission(uri, modeSetting); + return NError(ret); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + const string PROCEDURENAME = "desist_persist_permission"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::TWO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } +} + +napi_value GetUriFromPath::ActivateAccessingUri(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + LOGE("ActivateAccessingUri Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto [succUri, fileUri] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + std::string uri = fileUri; + + if (!succUri) { + LOGE("The argument error"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + auto cbExec = [uri]() -> NError { + int32_t ret = CommonFunc::ActivateAccessingUri(uri); + return NError(ret); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + const string PROCEDURENAME = "activate_accessing_uri"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } +} + +napi_value GetUriFromPath::DeactivateAccessingUri(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + LOGE("DeactivateAccessingUri Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + auto [succUri, fileUri] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + std::string uri = fileUri; + + if (!succUri) { + LOGE("The argument error"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = [uri]() -> NError { + int32_t ret = CommonFunc::DeactivateAccessingUri(uri); + return NError(ret); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + const string PROCEDURENAME = "deactivate_accessing_uri"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } +} + +static bool IsSystemApp() +{ + uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); +} + +static bool CheckPermission(const string &permission) +{ + Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + return Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission) == + Security::AccessToken::PermissionState::PERMISSION_GRANTED; +} + +void InitModeSetting(napi_env env, napi_value exports) +{ + char propertyName[] = "ModeSetting"; + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("O_RDWR", + NVal::CreateInt32(env, static_cast(ModeSetting::O_RDWR_FILE_URI)).val_), + }; + napi_value obj = nullptr; + napi_status status = napi_create_object(env, &obj); + if (status != napi_ok) { + HILOGE("Failed to create object at initializing ModeSetting"); + return; + } + status = napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc); + if (status != napi_ok) { + HILOGE("Failed to set properties of character at initializing ModeSetting"); + return; + } + status = napi_set_named_property(env, exports, propertyName, obj); + if (status != napi_ok) { + HILOGE("Failed to set direction property at initializing ModeSetting"); + return; + } +} + } // namespace ModuleFileUri } // namespace AppFileService } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.h b/interfaces/kits/js/file_uri/get_uri_from_path.h index 8adb88fb962b84965f874f356b11e41c509a4a79..ec53844fa552a0832eaaaffe69d5c5c078a529bf 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.h +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -16,15 +16,27 @@ #ifndef GET_URI_FROM_PATH_H #define GET_URI_FROM_PATH_H -#include #include "filemgmt_libn.h" +#include namespace OHOS { namespace AppFileService { namespace ModuleFileUri { +void InitModeSetting(napi_env env, napi_value exports); + class GetUriFromPath final { public: static napi_value Sync(napi_env env, napi_callback_info info); + + static napi_value GrantPolicy(napi_env env, napi_callback_info info); + static napi_value PersistPermission(napi_env env, napi_callback_info info); + static napi_value DesistPersistPermission(napi_env env, napi_callback_info info); + static napi_value ActivateAccessingUri(napi_env env, napi_callback_info info); + static napi_value DeactivateAccessingUri(napi_env env, napi_callback_info info); + +private: + static bool IsSystemApp(); + static bool CheckPermission(const std::string &permission); }; } // namespace ModuleFileUri } // namespace AppFileService diff --git a/interfaces/kits/js/file_uri/module.cpp b/interfaces/kits/js/file_uri/module.cpp index 378ed9f653762b30607bc73d4fcb32effb2ca6f6..3852a8986ecf44cebaa533da747dacee16d47784 100644 --- a/interfaces/kits/js/file_uri/module.cpp +++ b/interfaces/kits/js/file_uri/module.cpp @@ -27,6 +27,7 @@ using namespace FileManagement::LibN; static napi_value Export(napi_env env, napi_value exports) { + InitModeSetting(env, exports); std::vector> products; products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); diff --git a/interfaces/kits/js/file_uri/prop_n_exporter.cpp b/interfaces/kits/js/file_uri/prop_n_exporter.cpp index ef5864e2c764f5fa78a673fe04084c3cf4319362..f1d9134a33c6a1e3a35813be7ae6e9f414aad3f8 100644 --- a/interfaces/kits/js/file_uri/prop_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/prop_n_exporter.cpp @@ -24,6 +24,11 @@ bool PropNExporter::Export() { return exports_.AddProp({ NVal::DeclareNapiFunction("getUriFromPath", GetUriFromPath::Sync), + NVal::DeclareNapiFunction("grantPolicy", GetUriFromPath::GrantPolicy), + NVal::DeclareNapiFunction("persistPermission", GetUriFromPath::PersistPermission), + NVal::DeclareNapiFunction("desistPersistPermission", GetUriFromPath::DesistPersistPermission), + NVal::DeclareNapiFunction("activateAccessingUri", GetUriFromPath::ActivateAccessingUri), + NVal::DeclareNapiFunction("deactivateAccessingUri", GetUriFromPath::DeactivateAccessingUri), }); }