From 0fe82a8715fc3e5422779b6fffbc2e23339a00ae Mon Sep 17 00:00:00 2001 From: LVB8189 Date: Sat, 28 Oct 2023 20:08:45 +0800 Subject: [PATCH 1/3] add Permission requirements Signed-off-by: LVB8189 --- interfaces/innerkits/native/BUILD.gn | 2 + .../native/file_uri/include/file_uri.h | 18 +- .../native/file_uri/src/file_uri.cpp | 81 +++++- interfaces/kits/js/BUILD.gn | 2 + .../kits/js/file_uri/file_uri_n_exporter.cpp | 258 +++++++++++++++++- .../kits/js/file_uri/file_uri_n_exporter.h | 13 + interfaces/kits/js/file_uri/module.cpp | 1 + 7 files changed, 362 insertions(+), 13 deletions(-) diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 67c3c077e..49c2ae656 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -69,7 +69,9 @@ ohos_shared_library("fileuri_native") { "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", + "file_api:filemgmt_libn", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", ] diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h index 2f146ade8..6ebcc0245 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -18,10 +18,12 @@ #include +#include "filemgmt_libn.h" #include "uri.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { +using namespace FileManagement::LibN; class FileUri { public: std::string GetName(); @@ -29,12 +31,20 @@ public: std::string GetRealPath(); std::string ToString(); + NError SetPolicy(int32_t tokenId, int32_t modeSetting); + NError PersistPermission(int32_t modeSetting); + NError UnPersistPermission(int32_t modeSetting); + NError StartAccessingURI(); + NError StopAccessingURI(); + bool isPcDeviceType(); + 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 4838bf9b1..58b668b10 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -21,6 +21,7 @@ #include "common_func.h" #include "log.h" +#include "parameter.h" #include "sandbox_helper.h" using namespace std; @@ -34,6 +35,11 @@ const std::string FILE_SCHEME_PREFIX = "file://"; const std::string FILE_MANAGER_AUTHORITY = "docs"; const std::string MEDIA_AUTHORITY = "media"; const std::string NETWORK_PARA = "?networkid="; +constexpr const char *DEFAULT = "default"; +constexpr const char *TWOINONE = "2in1"; +constexpr const char *TABLE = "table"; +constexpr const char *DEVICETYPEKEY = "const.product.devicetype"; + string FileUri::GetName() { string sandboxPath = SandboxHelper::Decode(uri_.GetPath()); @@ -67,8 +73,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 +93,70 @@ string FileUri::ToString() return uri_.ToString(); } -FileUri::FileUri(const string &uriOrPath): uri_( - (uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath) -) -{} +NError FileUri::SetPolicy(int32_t tokenId, int32_t modeSetting) +{ + if (isPcDeviceType()) { + LOGE("device is not pc!"); + return E_DEVICENOTSUPPORT; + } + string path = uri_.GetPath(); + return EERRNO_NOERR; +} + +NError FileUri::PersistPermission(int32_t modeSetting) +{ + if (isPcDeviceType()) { + LOGE("device is not pc!"); + return E_DEVICENOTSUPPORT; + } + string path = uri_.GetPath(); + return EERRNO_NOERR; +} + +NError FileUri::UnPersistPermission(int32_t modeSetting) +{ + if (isPcDeviceType()) { + LOGE("device is not pc!"); + return E_DEVICENOTSUPPORT; + } + string path = uri_.GetPath(); + return EERRNO_NOERR; +} + +NError FileUri::StartAccessingURI() +{ + if (isPcDeviceType()) { + LOGE("device is not pc!"); + return E_DEVICENOTSUPPORT; + } + string path = uri_.GetPath(); + return EERRNO_NOERR; +} + +NError FileUri::StopAccessingURI() +{ + if (isPcDeviceType()) { + LOGE("device is not pc!"); + return E_DEVICENOTSUPPORT; + } + string path = uri_.GetPath(); + return EERRNO_NOERR; +} + +bool FileUri::isPcDeviceType() +{ + string deviceType = OHOS::system::GetParameter(string(DEVICETYPEKEY), string(DEFAULT)); + LOGD("GetRealPath real path is %{private}s", deviceType.c_str()); + if (deviceType.c_str() == TWOINONE || deviceType.c_str() == TABLE) { + return true; + } + return false; +} + +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 47ecdfc16..73263ea40 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 d9dec0e39..8c7ee75e3 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp @@ -14,8 +14,11 @@ */ #include "file_uri_n_exporter.h" +#include "access_token.h" +#include "accesstoken_kit.h" #include "file_uri_entity.h" #include "file_utils.h" +#include "ipc_skeleton.h" #include "log.h" namespace OHOS { @@ -25,6 +28,8 @@ using namespace std; using namespace FileManagement; using namespace FileManagement::LibN; +const std::string FILE_ACCESS_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; + napi_value FileUriNExporter::Constructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -53,7 +58,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); @@ -105,12 +109,264 @@ napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info inf return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetPath()).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; +} + +napi_value FileUriNExporter::SetPolicy(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + LOGE("SetPolicy Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + if (!IsSystemApp()) { + LOGE("SetPolicy is not System App!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + auto [succTokenId, tokenId] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + auto [succModeSetting, modeSetting] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + + if (!succTokenId || !succModeSetting) { + LOGE("The first/second argument error"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!fileuriEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + ModeSetting mode = static_cast(modeSetting); + auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.SetPolicy(tokenId, modeSetting); }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + const string PROCEDURENAME = "set_policy"; + 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 FileUriNExporter::PersistPermission(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + LOGE("PersistPermission Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + if (!CheckPermission(FILE_ACCESS_PERMISSION)) { + LOGE("PersistPermission has not ohos permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + + auto [succModeSetting, modeSetting] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + + if (!succModeSetting) { + LOGE("The first argument error"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!fileuriEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.PersistPermission(modeSetting); }; + + 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::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 FileUriNExporter::UnPersistPermission(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + LOGE("UnPersistPermission Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + if (!CheckPermission(FILE_ACCESS_PERMISSION)) { + LOGE("UnPersistPermission has not ohos permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + auto [succModeSetting, modeSetting] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + + if (!succModeSetting) { + LOGE("The first argument error"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!fileuriEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.UnPersistPermission(modeSetting); }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + const string PROCEDURENAME = "un_persist_permission"; + 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 FileUriNExporter::StartAccessingURI(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + LOGE("StartAccessingURI Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!fileuriEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.StartAccessingURI(); }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + const string PROCEDURENAME = "start_accessing_uri"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } +} + +napi_value FileUriNExporter::StopAccessingURI(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + LOGE("StopAccessingURI Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!fileuriEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.StopAccessingURI(); }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + const string PROCEDURENAME = "stop_accessing_uri"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; + } +} + +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)).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; + } +} + bool FileUriNExporter::Export() { vector props = { NVal::DeclareNapiFunction("toString", UriToString), NVal::DeclareNapiGetter("name", GetFileUriName), NVal::DeclareNapiGetter("path", GetFileUriPath), + NVal::DeclareNapiFunction("setPolicy", SetPolicy), + NVal::DeclareNapiFunction("persistPermission", PersistPermission), + NVal::DeclareNapiFunction("unPersistPermission", UnPersistPermission), + NVal::DeclareNapiFunction("startAccessingURI", StartAccessingURI), + NVal::DeclareNapiFunction("stopAccessingURI", StopAccessingURI), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.h b/interfaces/kits/js/file_uri/file_uri_n_exporter.h index 4172c08c5..8bb27757d 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.h +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.h @@ -20,6 +20,13 @@ namespace OHOS { namespace AppFileService { namespace ModuleFileUri { + +enum class ModeSetting : int32_t { + O_RDWR = 2, +}; + +void InitModeSetting(napi_env env, napi_value exports); + class FileUriNExporter final : public FileManagement::LibN::NExporter { public: inline static const std::string className = "FileUri"; @@ -32,6 +39,12 @@ public: static napi_value GetFileUriName(napi_env env, napi_callback_info info); static napi_value GetFileUriPath(napi_env env, napi_callback_info info); + static napi_value SetPolicy(napi_env env, napi_callback_info info); + static napi_value PersistPermission(napi_env env, napi_callback_info info); + static napi_value UnPersistPermission(napi_env env, napi_callback_info info); + static napi_value StartAccessingURI(napi_env env, napi_callback_info info); + static napi_value StopAccessingURI(napi_env env, napi_callback_info info); + FileUriNExporter(napi_env env, napi_value exports); ~FileUriNExporter() override; }; diff --git a/interfaces/kits/js/file_uri/module.cpp b/interfaces/kits/js/file_uri/module.cpp index 378ed9f65..3852a8986 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)); -- Gitee From 0cbf89f1e403e2c91e0bf48b7a13a50ffb08e9f5 Mon Sep 17 00:00:00 2001 From: LVB8189 Date: Tue, 7 Nov 2023 11:03:17 +0800 Subject: [PATCH 2/3] modify Signed-off-by: LVB8189 --- interfaces/common/include/common_func.h | 9 + interfaces/common/src/common_func.cpp | 36 +++ interfaces/innerkits/native/BUILD.gn | 1 - .../native/file_uri/include/file_uri.h | 9 - .../native/file_uri/src/file_uri.cpp | 63 +---- .../kits/js/file_uri/file_uri_n_exporter.cpp | 255 +---------------- .../kits/js/file_uri/file_uri_n_exporter.h | 12 - .../kits/js/file_uri/get_uri_from_path.cpp | 257 ++++++++++++++++++ .../kits/js/file_uri/get_uri_from_path.h | 14 +- .../kits/js/file_uri/prop_n_exporter.cpp | 5 + 10 files changed, 323 insertions(+), 338 deletions(-) diff --git a/interfaces/common/include/common_func.h b/interfaces/common/include/common_func.h index 9603016e6..bb5e95abb 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 74f715a78..a8bbec230 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/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 49c2ae656..e2336f0df 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -69,7 +69,6 @@ ohos_shared_library("fileuri_native") { "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", - "file_api:filemgmt_libn", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_core", diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h index 6ebcc0245..c0f5b7593 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -18,12 +18,10 @@ #include -#include "filemgmt_libn.h" #include "uri.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { -using namespace FileManagement::LibN; class FileUri { public: std::string GetName(); @@ -31,13 +29,6 @@ public: std::string GetRealPath(); std::string ToString(); - NError SetPolicy(int32_t tokenId, int32_t modeSetting); - NError PersistPermission(int32_t modeSetting); - NError UnPersistPermission(int32_t modeSetting); - NError StartAccessingURI(); - NError StopAccessingURI(); - bool isPcDeviceType(); - explicit FileUri(const std::string &uriOrPath); ~FileUri() = default; diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 58b668b10..abb7bc226 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -16,12 +16,13 @@ #include "file_uri.h" #include +#include #include "uri.h" #include "common_func.h" #include "log.h" -#include "parameter.h" +#include "parameters.h" #include "sandbox_helper.h" using namespace std; @@ -93,66 +94,6 @@ string FileUri::ToString() return uri_.ToString(); } -NError FileUri::SetPolicy(int32_t tokenId, int32_t modeSetting) -{ - if (isPcDeviceType()) { - LOGE("device is not pc!"); - return E_DEVICENOTSUPPORT; - } - string path = uri_.GetPath(); - return EERRNO_NOERR; -} - -NError FileUri::PersistPermission(int32_t modeSetting) -{ - if (isPcDeviceType()) { - LOGE("device is not pc!"); - return E_DEVICENOTSUPPORT; - } - string path = uri_.GetPath(); - return EERRNO_NOERR; -} - -NError FileUri::UnPersistPermission(int32_t modeSetting) -{ - if (isPcDeviceType()) { - LOGE("device is not pc!"); - return E_DEVICENOTSUPPORT; - } - string path = uri_.GetPath(); - return EERRNO_NOERR; -} - -NError FileUri::StartAccessingURI() -{ - if (isPcDeviceType()) { - LOGE("device is not pc!"); - return E_DEVICENOTSUPPORT; - } - string path = uri_.GetPath(); - return EERRNO_NOERR; -} - -NError FileUri::StopAccessingURI() -{ - if (isPcDeviceType()) { - LOGE("device is not pc!"); - return E_DEVICENOTSUPPORT; - } - string path = uri_.GetPath(); - return EERRNO_NOERR; -} - -bool FileUri::isPcDeviceType() -{ - string deviceType = OHOS::system::GetParameter(string(DEVICETYPEKEY), string(DEFAULT)); - LOGD("GetRealPath real path is %{private}s", deviceType.c_str()); - if (deviceType.c_str() == TWOINONE || deviceType.c_str() == TABLE) { - return true; - } - return false; -} - FileUri::FileUri(const string &uriOrPath) : uri_((uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath)) { 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 8c7ee75e3..7ef1cae2f 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp @@ -20,6 +20,7 @@ #include "file_utils.h" #include "ipc_skeleton.h" #include "log.h" +#include "tokenid_kit.h" namespace OHOS { namespace AppFileService { @@ -28,8 +29,6 @@ using namespace std; using namespace FileManagement; using namespace FileManagement::LibN; -const std::string FILE_ACCESS_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; - napi_value FileUriNExporter::Constructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -109,264 +108,12 @@ napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info inf return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetPath()).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; -} - -napi_value FileUriNExporter::SetPolicy(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - LOGE("SetPolicy Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - if (!IsSystemApp()) { - LOGE("SetPolicy is not System App!"); - NError(E_PERMISSION_SYS).ThrowErr(env); - return nullptr; - } - auto [succTokenId, tokenId] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - auto [succModeSetting, modeSetting] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - - if (!succTokenId || !succModeSetting) { - LOGE("The first/second argument error"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - if (!fileuriEntity) { - LOGE("Failed to get file entity"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - ModeSetting mode = static_cast(modeSetting); - auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.SetPolicy(tokenId, modeSetting); }; - - auto cbCompl = [](napi_env env, NError err) -> NVal { - if (err) { - return {env, err.GetNapiErr(env)}; - } - return NVal::CreateUndefined(env); - }; - - const string PROCEDURENAME = "set_policy"; - 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 FileUriNExporter::PersistPermission(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - LOGE("PersistPermission Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - if (!CheckPermission(FILE_ACCESS_PERMISSION)) { - LOGE("PersistPermission has not ohos permission!"); - NError(E_PERMISSION).ThrowErr(env); - return nullptr; - } - - auto [succModeSetting, modeSetting] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - - if (!succModeSetting) { - LOGE("The first argument error"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - if (!fileuriEntity) { - LOGE("Failed to get file entity"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.PersistPermission(modeSetting); }; - - 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::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 FileUriNExporter::UnPersistPermission(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - LOGE("UnPersistPermission Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - if (!CheckPermission(FILE_ACCESS_PERMISSION)) { - LOGE("UnPersistPermission has not ohos permission!"); - NError(E_PERMISSION).ThrowErr(env); - return nullptr; - } - auto [succModeSetting, modeSetting] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - - if (!succModeSetting) { - LOGE("The first argument error"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - if (!fileuriEntity) { - LOGE("Failed to get file entity"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.UnPersistPermission(modeSetting); }; - - auto cbCompl = [](napi_env env, NError err) -> NVal { - if (err) { - return {env, err.GetNapiErr(env)}; - } - return NVal::CreateUndefined(env); - }; - - const string PROCEDURENAME = "un_persist_permission"; - 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 FileUriNExporter::StartAccessingURI(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - LOGE("StartAccessingURI Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - if (!fileuriEntity) { - LOGE("Failed to get file entity"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.StartAccessingURI(); }; - - auto cbCompl = [](napi_env env, NError err) -> NVal { - if (err) { - return {env, err.GetNapiErr(env)}; - } - return NVal::CreateUndefined(env); - }; - - const string PROCEDURENAME = "start_accessing_uri"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; - } else { - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; - } -} - -napi_value FileUriNExporter::StopAccessingURI(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - LOGE("StopAccessingURI Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - if (!fileuriEntity) { - LOGE("Failed to get file entity"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto cbExec = [env]() -> NError { return fileuriEntity->fileUri_.StopAccessingURI(); }; - - auto cbCompl = [](napi_env env, NError err) -> NVal { - if (err) { - return {env, err.GetNapiErr(env)}; - } - return NVal::CreateUndefined(env); - }; - - const string PROCEDURENAME = "stop_accessing_uri"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; - } else { - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; - } -} - -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)).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; - } -} - bool FileUriNExporter::Export() { vector props = { NVal::DeclareNapiFunction("toString", UriToString), NVal::DeclareNapiGetter("name", GetFileUriName), NVal::DeclareNapiGetter("path", GetFileUriPath), - NVal::DeclareNapiFunction("setPolicy", SetPolicy), - NVal::DeclareNapiFunction("persistPermission", PersistPermission), - NVal::DeclareNapiFunction("unPersistPermission", UnPersistPermission), - NVal::DeclareNapiFunction("startAccessingURI", StartAccessingURI), - NVal::DeclareNapiFunction("stopAccessingURI", StopAccessingURI), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.h b/interfaces/kits/js/file_uri/file_uri_n_exporter.h index 8bb27757d..3907d9207 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.h +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.h @@ -21,12 +21,6 @@ namespace OHOS { namespace AppFileService { namespace ModuleFileUri { -enum class ModeSetting : int32_t { - O_RDWR = 2, -}; - -void InitModeSetting(napi_env env, napi_value exports); - class FileUriNExporter final : public FileManagement::LibN::NExporter { public: inline static const std::string className = "FileUri"; @@ -39,12 +33,6 @@ public: static napi_value GetFileUriName(napi_env env, napi_callback_info info); static napi_value GetFileUriPath(napi_env env, napi_callback_info info); - static napi_value SetPolicy(napi_env env, napi_callback_info info); - static napi_value PersistPermission(napi_env env, napi_callback_info info); - static napi_value UnPersistPermission(napi_env env, napi_callback_info info); - static napi_value StartAccessingURI(napi_env env, napi_callback_info info); - static napi_value StopAccessingURI(napi_env env, napi_callback_info info); - FileUriNExporter(napi_env env, napi_value exports); ~FileUriNExporter() override; }; 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 da98d8611..d9cf0dceb 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -24,6 +24,8 @@ 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 +51,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 8adb88fb9..ec53844fa 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/prop_n_exporter.cpp b/interfaces/kits/js/file_uri/prop_n_exporter.cpp index ef5864e2c..f1d9134a3 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), }); } -- Gitee From 23b2d6d339999bb0451ed8b3e5e32d59a8262f1e Mon Sep 17 00:00:00 2001 From: LVB8189 Date: Tue, 7 Nov 2023 11:08:14 +0800 Subject: [PATCH 3/3] modify Signed-off-by: LVB8189 --- interfaces/innerkits/native/BUILD.gn | 1 - interfaces/innerkits/native/file_uri/src/file_uri.cpp | 7 ------- interfaces/kits/js/file_uri/file_uri_n_exporter.cpp | 4 ---- interfaces/kits/js/file_uri/file_uri_n_exporter.h | 1 - interfaces/kits/js/file_uri/get_uri_from_path.cpp | 6 ++++++ 5 files changed, 6 insertions(+), 13 deletions(-) diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index e2336f0df..67c3c077e 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -70,7 +70,6 @@ ohos_shared_library("fileuri_native") { "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", - "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", ] diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index abb7bc226..7b791f73d 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -16,13 +16,11 @@ #include "file_uri.h" #include -#include #include "uri.h" #include "common_func.h" #include "log.h" -#include "parameters.h" #include "sandbox_helper.h" using namespace std; @@ -36,11 +34,6 @@ const std::string FILE_SCHEME_PREFIX = "file://"; const std::string FILE_MANAGER_AUTHORITY = "docs"; const std::string MEDIA_AUTHORITY = "media"; const std::string NETWORK_PARA = "?networkid="; -constexpr const char *DEFAULT = "default"; -constexpr const char *TWOINONE = "2in1"; -constexpr const char *TABLE = "table"; -constexpr const char *DEVICETYPEKEY = "const.product.devicetype"; - string FileUri::GetName() { string sandboxPath = SandboxHelper::Decode(uri_.GetPath()); 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 7ef1cae2f..003f59287 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp @@ -14,13 +14,9 @@ */ #include "file_uri_n_exporter.h" -#include "access_token.h" -#include "accesstoken_kit.h" #include "file_uri_entity.h" #include "file_utils.h" -#include "ipc_skeleton.h" #include "log.h" -#include "tokenid_kit.h" namespace OHOS { namespace AppFileService { diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.h b/interfaces/kits/js/file_uri/file_uri_n_exporter.h index 3907d9207..4172c08c5 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.h +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.h @@ -20,7 +20,6 @@ namespace OHOS { namespace AppFileService { namespace ModuleFileUri { - class FileUriNExporter final : public FileManagement::LibN::NExporter { public: inline static const std::string className = "FileUri"; 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 d9cf0dceb..57b5f2a77 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -19,6 +19,12 @@ #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 { -- Gitee