diff --git a/bundle.json b/bundle.json index dbf37010436d05a9fb0fa3a8048b1a292aa0d541..46dd10022b16da17c61fa6a05fc1594477b90641 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 d279adaebba72cb542177e4c24ad1522762d7559..2a4bc332b3607b724120e26c7e7d7962c105da0c 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -325,13 +325,38 @@ ohos_shared_library("environment") { ] external_deps = [ + "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "hilog:libhilog", "ipc:ipc_core", "napi:ace_napi", ] } +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" @@ -398,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 2c60991511c3e292ae3d8e694e5df9c2afc94770..716abd02add8dd5354c8df09875916c65536c3ab 100644 --- a/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp @@ -20,7 +20,7 @@ #include "filemgmt_libhilog.h" #include "ipc_skeleton.h" #include "tokenid_kit.h" - +#include "accesstoken_kit.h" namespace OHOS { namespace FileManagement { namespace ModuleEnvironment { @@ -106,6 +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_; } + } // namespace ModuleEnvironment } // namespace FileManagement } // namespace OHOS \ No newline at end of file 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 0000000000000000000000000000000000000000..8fdefdf322010b529aaf8ab09f7b1aab27e6b42c --- /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 0000000000000000000000000000000000000000..5872ff678e637163949fcce4ba40472f9d6ed4d0 --- /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 0000000000000000000000000000000000000000..5ef78259bdfe326e32fd3d14754dcbcf83fad885 --- /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