From cb610edad37c5ab4d2ba729e2f661dfc41bd091d Mon Sep 17 00:00:00 2001 From: cuiruibin Date: Sat, 28 Oct 2023 20:00:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?*=20File=5FAPI=E6=96=B0=E5=A2=9E=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=85=AC=E5=85=B1=E8=B7=AF=E5=BE=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cuiruibin --- bundle.json | 3 +- interfaces/kits/js/BUILD.gn | 2 + .../environment_n_exporter.cpp | 191 +++++++++++++++++- .../mod_environment/environment_n_exporter.h | 5 + .../src/mod_environment/environment_napi.cpp | 5 + 5 files changed, 204 insertions(+), 2 deletions(-) diff --git a/bundle.json b/bundle.json index dbf370104..46dd10022 100644 --- a/bundle.json +++ b/bundle.json @@ -37,7 +37,8 @@ "napi", "samgr", "app_file_service", - "build_framework" + "build_framework", + "os_account" ], "third_party": [ "bounds_checking_function", diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index d279adaeb..a882ce1a1 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -325,10 +325,12 @@ ohos_shared_library("environment") { ] external_deps = [ + "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "hilog:libhilog", "ipc:ipc_core", "napi:ace_napi", + "os_account:os_account_innerkits", ] } diff --git a/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp index 2c6099151..f1bad62fd 100644 --- a/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp @@ -20,13 +20,24 @@ #include "filemgmt_libhilog.h" #include "ipc_skeleton.h" #include "tokenid_kit.h" - +#include "accesstoken_kit.h" +#include "os_account_manager.h" namespace OHOS { namespace FileManagement { namespace ModuleEnvironment { using namespace OHOS::FileManagement::LibN; namespace { const std::string STORAGE_DATA_PATH = "/data"; + const std::string PC_STORAGE_PATH = "/storage/Users/"; + const std::string EXTERNAL_Storage_PATH = "/storage/External"; + const std::string USER_APP_DATA_PATH = "/appdata"; + const std::string USER_DATA_HOME_PATH = "storage/Users/"; + const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; + const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; + const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; + const std::string DOWNLOAD_PATH = "/Download"; + const std::string DESKTOP_PATH = "/Desktop"; + const std::string DOCUMENTS_PATH = "/Documents"; bool IsSystemApp() { uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); @@ -106,6 +117,184 @@ napi_value GetUserDataDir(napi_env env, napi_callback_info info) NVal cb(env, funcArg[NARG_POS::FIRST]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; } +static bool CheckCallingPermission(const std::string &permission) +{ + Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission); + if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + return false; + } + return true; +} +static std::string GetUserName(){ + std::string userName = ""; + int32_t errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if(!errCode){ + HILOGE("get userName Failed"); + return nullptr; + } + return userName; +}; +napi_value GetUserDownloadDir(napi_env env, napi_callback_info info) +{ + if(!CheckCallingPermission(READ_WRITE_DOWNLOAD_PERMISSION)){ + return nullptr; + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto publicDirPath = std::make_shared(); + auto cbExec = [publicDirPath]() -> NError { + (*publicDirPath).append(PC_STORAGE_PATH).append(GetUserName()).append(DOWNLOAD_PATH); + return NError(ERRNO_NOERR); + }; + auto cbComplete = [publicDirPath](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateUTF8String(env, *publicDirPath); + }; + + static const std::string PROCEDURE_NAME = "GetUserDownloadDir"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; +} +napi_value GetUserDesktopDir(napi_env env, napi_callback_info info) +{ + if(!CheckCallingPermission(READ_WRITE_DESKTOP_PERMISSION)){ + return nullptr; + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto publicDirPath = std::make_shared(); + auto cbExec = [publicDirPath]() -> NError { + (*publicDirPath).append(PC_STORAGE_PATH).append(GetUserName()).append(DESKTOP_PATH); + return NError(ERRNO_NOERR); + }; + auto cbComplete = [publicDirPath](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateUTF8String(env, *publicDirPath); + }; + + static const std::string PROCEDURE_NAME = "GetUserDesktopDir"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; +} +napi_value GetUserDocumentsDir(napi_env env, napi_callback_info info) +{ + if(!CheckCallingPermission(READ_WRITE_DOCUMENTS_PERMISSION)){ + return nullptr; + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto publicDirPath = std::make_shared(); + auto cbExec = [publicDirPath]() -> NError { + (*publicDirPath).append(PC_STORAGE_PATH).append(GetUserName()).append(DOCUMENTS_PATH); + return NError(ERRNO_NOERR); + }; + auto cbComplete = [publicDirPath](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateUTF8String(env, *publicDirPath); + }; + + static const std::string PROCEDURE_NAME = "GetUserDocumentsDir"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; +} +napi_value GetExternalStorageDir(napi_env env, napi_callback_info info) +{ + if (!IsSystemApp()) { + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto cbExec = []() -> NError { + return NError(ERRNO_NOERR); + }; + auto cbComplete = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateUTF8String(env, EXTERNAL_Storage_PATH); + }; + + static const std::string PROCEDURE_NAME = "GetExternalUsb"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; +} +napi_value GetUserHomeDir(napi_env env, napi_callback_info info) +{ + if (!IsSystemApp()) { + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto userHomePath = std::make_shared(); + auto cbExec = [userHomePath]() -> NError { + (*userHomePath).append(PC_STORAGE_PATH).append(GetUserName()); + return NError(ERRNO_NOERR); + }; + auto cbComplete = [userHomePath](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateUTF8String(env, *userHomePath); + }; + + static const std::string PROCEDURE_NAME = "GetUserHomeDir"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; +} } // namespace ModuleEnvironment } // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_n_exporter.h b/interfaces/kits/js/src/mod_environment/environment_n_exporter.h index 01a1fea4a..4dd70e424 100644 --- a/interfaces/kits/js/src/mod_environment/environment_n_exporter.h +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.h @@ -23,6 +23,11 @@ namespace FileManagement { namespace ModuleEnvironment { napi_value GetStorageDataDir(napi_env env, napi_callback_info info); napi_value GetUserDataDir(napi_env env, napi_callback_info info); +napi_value GetUserDownloadDir(napi_env env, napi_callback_info info); +napi_value GetUserDesktopDir(napi_env env, napi_callback_info info); +napi_value GetUserDocumentsDir(napi_env env, napi_callback_info info); +napi_value GetUserHomeDir(napi_env env, napi_callback_info info); +napi_value GetExternalStorageDir(napi_env env, napi_callback_info info); } // namespace ModuleEnvironment } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_environment/environment_napi.cpp b/interfaces/kits/js/src/mod_environment/environment_napi.cpp index f8ef720e2..4aea02c20 100644 --- a/interfaces/kits/js/src/mod_environment/environment_napi.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_napi.cpp @@ -26,6 +26,11 @@ napi_value EnvironmentExport(napi_env env, napi_value exports) static napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("getStorageDataDir", GetStorageDataDir), DECLARE_NAPI_FUNCTION("getUserDataDir", GetUserDataDir), + DECLARE_NAPI_FUNCTION("getUserDownloadDir", GetUserDownloadDir), + DECLARE_NAPI_FUNCTION("getUserDesktopDir", GetUserDesktopDir), + DECLARE_NAPI_FUNCTION("getUserDocumentsDir", GetUserDocumentsDir), + DECLARE_NAPI_FUNCTION("getExternalStorageDir", GetExternalStorageDir), + DECLARE_NAPI_FUNCTION("getUserHomeDir", GetUserHomeDir), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; -- Gitee From 523a0b32399bd8e16134a03299b122f06eb331c2 Mon Sep 17 00:00:00 2001 From: cuiruibin Date: Wed, 8 Nov 2023 16:30:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?*=20File=5FAPI=E6=96=B0=E5=A2=9E=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=85=AC=E5=85=B1=E8=B7=AF=E5=BE=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cuiruibin --- interfaces/kits/js/BUILD.gn | 26 ++- .../environment_n_exporter.cpp | 188 ------------------ .../mod_environment/environment_n_exporter.h | 5 - .../src/mod_environment/environment_napi.cpp | 5 - .../userdirectory_n_exporter.cpp | 137 +++++++++++++ .../userdirectory_n_exporter.h | 30 +++ .../mod_userdirectory/userdirectory_napi.cpp | 54 +++++ 7 files changed, 246 insertions(+), 199 deletions(-) create mode 100644 interfaces/kits/js/src/mod_userdirectory/userdirectory_n_exporter.cpp create mode 100644 interfaces/kits/js/src/mod_userdirectory/userdirectory_n_exporter.h create mode 100644 interfaces/kits/js/src/mod_userdirectory/userdirectory_napi.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index a882ce1a1..2a4bc332b 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -330,10 +330,33 @@ ohos_shared_library("environment") { "hilog:libhilog", "ipc:ipc_core", "napi:ace_napi", - "os_account:os_account_innerkits", ] } +ohos_shared_library("userdirectory") { + subsystem_name = "filemanagement" + part_name = "file_api" + + relative_install_dir = "module/file" + + sources = [ + "src/mod_userdirectory/userdirectory_n_exporter.cpp", + "src/mod_userdirectory/userdirectory_napi.cpp", + ] + + deps = [ + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${utils_path}/filemgmt_libn:filemgmt_libn", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "hilog:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + "os_account:os_account_innerkits", + ] +} ohos_shared_library("securitylabel") { subsystem_name = "filemanagement" part_name = "file_api" @@ -400,5 +423,6 @@ group("build_kits_js") { ":securitylabel", ":statfs", ":statvfs", + ":userdirectory", ] } diff --git a/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp index f1bad62fd..716abd02a 100644 --- a/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp @@ -21,23 +21,12 @@ #include "ipc_skeleton.h" #include "tokenid_kit.h" #include "accesstoken_kit.h" -#include "os_account_manager.h" namespace OHOS { namespace FileManagement { namespace ModuleEnvironment { using namespace OHOS::FileManagement::LibN; namespace { const std::string STORAGE_DATA_PATH = "/data"; - const std::string PC_STORAGE_PATH = "/storage/Users/"; - const std::string EXTERNAL_Storage_PATH = "/storage/External"; - const std::string USER_APP_DATA_PATH = "/appdata"; - const std::string USER_DATA_HOME_PATH = "storage/Users/"; - const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; - const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; - const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; - const std::string DOWNLOAD_PATH = "/Download"; - const std::string DESKTOP_PATH = "/Desktop"; - const std::string DOCUMENTS_PATH = "/Documents"; bool IsSystemApp() { uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); @@ -117,184 +106,7 @@ napi_value GetUserDataDir(napi_env env, napi_callback_info info) NVal cb(env, funcArg[NARG_POS::FIRST]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; } -static bool CheckCallingPermission(const std::string &permission) -{ - Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); - int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission); - if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { - return false; - } - return true; -} -static std::string GetUserName(){ - std::string userName = ""; - int32_t errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); - if(!errCode){ - HILOGE("get userName Failed"); - return nullptr; - } - return userName; -}; -napi_value GetUserDownloadDir(napi_env env, napi_callback_info info) -{ - if(!CheckCallingPermission(READ_WRITE_DOWNLOAD_PERMISSION)){ - return nullptr; - } - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto publicDirPath = std::make_shared(); - auto cbExec = [publicDirPath]() -> NError { - (*publicDirPath).append(PC_STORAGE_PATH).append(GetUserName()).append(DOWNLOAD_PATH); - return NError(ERRNO_NOERR); - }; - auto cbComplete = [publicDirPath](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; - } - return NVal::CreateUTF8String(env, *publicDirPath); - }; - - static const std::string PROCEDURE_NAME = "GetUserDownloadDir"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; - } - - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; -} -napi_value GetUserDesktopDir(napi_env env, napi_callback_info info) -{ - if(!CheckCallingPermission(READ_WRITE_DESKTOP_PERMISSION)){ - return nullptr; - } - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto publicDirPath = std::make_shared(); - auto cbExec = [publicDirPath]() -> NError { - (*publicDirPath).append(PC_STORAGE_PATH).append(GetUserName()).append(DESKTOP_PATH); - return NError(ERRNO_NOERR); - }; - auto cbComplete = [publicDirPath](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; - } - return NVal::CreateUTF8String(env, *publicDirPath); - }; - - static const std::string PROCEDURE_NAME = "GetUserDesktopDir"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; - } - - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; -} -napi_value GetUserDocumentsDir(napi_env env, napi_callback_info info) -{ - if(!CheckCallingPermission(READ_WRITE_DOCUMENTS_PERMISSION)){ - return nullptr; - } - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto publicDirPath = std::make_shared(); - auto cbExec = [publicDirPath]() -> NError { - (*publicDirPath).append(PC_STORAGE_PATH).append(GetUserName()).append(DOCUMENTS_PATH); - return NError(ERRNO_NOERR); - }; - auto cbComplete = [publicDirPath](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; - } - return NVal::CreateUTF8String(env, *publicDirPath); - }; - static const std::string PROCEDURE_NAME = "GetUserDocumentsDir"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; - } - - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; -} -napi_value GetExternalStorageDir(napi_env env, napi_callback_info info) -{ - if (!IsSystemApp()) { - NError(E_PERMISSION_SYS).ThrowErr(env); - return nullptr; - } - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto cbExec = []() -> NError { - return NError(ERRNO_NOERR); - }; - auto cbComplete = [](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; - } - return NVal::CreateUTF8String(env, EXTERNAL_Storage_PATH); - }; - - static const std::string PROCEDURE_NAME = "GetExternalUsb"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; - } - - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; -} -napi_value GetUserHomeDir(napi_env env, napi_callback_info info) -{ - if (!IsSystemApp()) { - NError(E_PERMISSION_SYS).ThrowErr(env); - return nullptr; - } - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto userHomePath = std::make_shared(); - auto cbExec = [userHomePath]() -> NError { - (*userHomePath).append(PC_STORAGE_PATH).append(GetUserName()); - return NError(ERRNO_NOERR); - }; - auto cbComplete = [userHomePath](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; - } - return NVal::CreateUTF8String(env, *userHomePath); - }; - - static const std::string PROCEDURE_NAME = "GetUserHomeDir"; - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; - } - - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; -} } // namespace ModuleEnvironment } // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_n_exporter.h b/interfaces/kits/js/src/mod_environment/environment_n_exporter.h index 4dd70e424..01a1fea4a 100644 --- a/interfaces/kits/js/src/mod_environment/environment_n_exporter.h +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.h @@ -23,11 +23,6 @@ namespace FileManagement { namespace ModuleEnvironment { napi_value GetStorageDataDir(napi_env env, napi_callback_info info); napi_value GetUserDataDir(napi_env env, napi_callback_info info); -napi_value GetUserDownloadDir(napi_env env, napi_callback_info info); -napi_value GetUserDesktopDir(napi_env env, napi_callback_info info); -napi_value GetUserDocumentsDir(napi_env env, napi_callback_info info); -napi_value GetUserHomeDir(napi_env env, napi_callback_info info); -napi_value GetExternalStorageDir(napi_env env, napi_callback_info info); } // namespace ModuleEnvironment } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_environment/environment_napi.cpp b/interfaces/kits/js/src/mod_environment/environment_napi.cpp index 4aea02c20..f8ef720e2 100644 --- a/interfaces/kits/js/src/mod_environment/environment_napi.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_napi.cpp @@ -26,11 +26,6 @@ napi_value EnvironmentExport(napi_env env, napi_value exports) static napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("getStorageDataDir", GetStorageDataDir), DECLARE_NAPI_FUNCTION("getUserDataDir", GetUserDataDir), - DECLARE_NAPI_FUNCTION("getUserDownloadDir", GetUserDownloadDir), - DECLARE_NAPI_FUNCTION("getUserDesktopDir", GetUserDesktopDir), - DECLARE_NAPI_FUNCTION("getUserDocumentsDir", GetUserDocumentsDir), - DECLARE_NAPI_FUNCTION("getExternalStorageDir", GetExternalStorageDir), - DECLARE_NAPI_FUNCTION("getUserHomeDir", GetUserHomeDir), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; diff --git a/interfaces/kits/js/src/mod_userdirectory/userdirectory_n_exporter.cpp b/interfaces/kits/js/src/mod_userdirectory/userdirectory_n_exporter.cpp new file mode 100644 index 000000000..8fdefdf32 --- /dev/null +++ b/interfaces/kits/js/src/mod_userdirectory/userdirectory_n_exporter.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "userdirectory_n_exporter.h" +#include + +#include "filemgmt_libhilog.h" +#include "ipc_skeleton.h" +#include "tokenid_kit.h" +#include "accesstoken_kit.h" +#include "os_account_manager.h" +#include "account_error_no.h" +namespace OHOS { +namespace FileManagement { +namespace ModuleUserDirectory { +using namespace OHOS::FileManagement::LibN; +namespace { + const std::string PC_STORAGE_PATH = "/storage/Users/"; + const std::string EXTERNAL_STORAGE_PATH = "/storage/External"; + const std::string USER_APP_DATA_PATH = "/appdata"; + const std::string USER_DATA_HOME_PATH = "storage/Users/"; + const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; + const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; + const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; + const std::string DOWNLOAD_PATH = "/Download"; + const std::string DESKTOP_PATH = "/Desktop"; + const std::string DOCUMENTS_PATH = "/Documents"; + bool IsSystemApp() + { + uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); + } +} +static bool CheckCallingPermission(const std::string &permission) +{ + Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission); + if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + return false; + } + return true; +} +static std::string GetUserName(){ + std::string userName; + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if(errCode != ERR_OK){ + HILOGE("get userName Failed"); + return nullptr; + } + return userName; +}; +static std::string GetPublicPath(const std::string &directoryName){ + return PC_STORAGE_PATH + GetUserName() + directoryName; +} +napi_value GetUserDownloadDir(napi_env env, napi_callback_info info) +{ + if(CheckCallingPermission(READ_WRITE_DOWNLOAD_PERMISSION)){ + HILOGE("no Permission"); + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, GetPublicPath(DOWNLOAD_PATH)).val_; +} +napi_value GetUserDesktopDir(napi_env env, napi_callback_info info) +{ + if(CheckCallingPermission(READ_WRITE_DESKTOP_PERMISSION)){ + HILOGE("no Permission"); + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, GetPublicPath(DESKTOP_PATH)).val_; +} +napi_value GetUserDocumentsDir(napi_env env, napi_callback_info info) +{ + if(CheckCallingPermission(READ_WRITE_DOCUMENTS_PERMISSION)){ + HILOGE("no Permission"); + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, GetPublicPath(DOCUMENTS_PATH)).val_; +} +napi_value GetExternalStorageDir(napi_env env, napi_callback_info info) +{ + if (!IsSystemApp()) { + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, EXTERNAL_STORAGE_PATH).val_; + +} +napi_value GetUserHomeDir(napi_env env, napi_callback_info info) +{ + if (!IsSystemApp()) { + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, PC_STORAGE_PATH + GetUserName()).val_; +} +} +} +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_userdirectory/userdirectory_n_exporter.h b/interfaces/kits/js/src/mod_userdirectory/userdirectory_n_exporter.h new file mode 100644 index 000000000..5872ff678 --- /dev/null +++ b/interfaces/kits/js/src/mod_userdirectory/userdirectory_n_exporter.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef USERDIRECTORY_N_EXPORTER_H +#define USERDIRECTORY_N_EXPORTER_H +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleUserDirectory { + napi_value GetUserDownloadDir(napi_env env, napi_callback_info info); + napi_value GetUserDesktopDir(napi_env env, napi_callback_info info); + napi_value GetUserDocumentsDir(napi_env env, napi_callback_info info); + napi_value GetUserHomeDir(napi_env env, napi_callback_info info); + napi_value GetExternalStorageDir(napi_env env, napi_callback_info info); +} +} +} +#endif //USERDIRECTORY_N_EXPORTER_H diff --git a/interfaces/kits/js/src/mod_userdirectory/userdirectory_napi.cpp b/interfaces/kits/js/src/mod_userdirectory/userdirectory_napi.cpp new file mode 100644 index 000000000..5ef78259b --- /dev/null +++ b/interfaces/kits/js/src/mod_userdirectory/userdirectory_napi.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "userdirectory_n_exporter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleUserDirectory { +/*********************************************** + * Module export and register + ***********************************************/ +napi_value UserDirectoryExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getUserDownloadDir", GetUserDownloadDir), + DECLARE_NAPI_FUNCTION("getUserDesktopDir", GetUserDesktopDir), + DECLARE_NAPI_FUNCTION("getUserDocumentsDir", GetUserDocumentsDir), + DECLARE_NAPI_FUNCTION("getUserHomeDir", GetUserHomeDir), + DECLARE_NAPI_FUNCTION("getExternalStorageDir", GetExternalStorageDir), + + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +static napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = UserDirectoryExport, + .nm_modname = "file.userDirectory", + .nm_priv = ((void *)0), + .reserved = {0} +}; + +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} +} // namespace ModuleEnvironment +} // namespace ModuleGetDirectoryPath +} // namespace OHOS -- Gitee