From 86273e4e026ceacafe21df733e952876a6a8f79d Mon Sep 17 00:00:00 2001 From: zhaoyuan Date: Mon, 27 Sep 2021 13:24:39 +0000 Subject: [PATCH] add getPreferencesDirSync interface Signed-off-by: zhaoyuan --- .../aafwk/featureAbility/napi_context.cpp | 257 +++++++++++++++++- .../napi_common/feature_ability_common.h | 11 +- tools/zip/include/file_path.h | 2 +- tools/zip/src/file_path.cpp | 22 +- 4 files changed, 275 insertions(+), 17 deletions(-) diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp index b971dacc1bf..fa595d3fd55 100755 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp @@ -28,7 +28,9 @@ using namespace OHOS::AppExecFwk; namespace OHOS { namespace AppExecFwk { -const std::string CONTEXT_DEAL_FILE_SEPARATOR = std::string("/"); +const std::string NAPI_CONTEXT_FILE_SEPARATOR = std::string("/"); +const std::string NAPI_CONTEXT_DATABASE = std::string("database"); +const std::string NAPI_CONTEXT_PREFERENCES = std::string("preferences"); CallbackInfo aceCallbackInfoPermission; napi_value ContextConstructor(napi_env env, napi_callback_info info) @@ -430,7 +432,7 @@ void CallOnRequestPermissionsFromUserResult(int requestCode, const std::vectorpermissions.size(); i++) { napi_create_string_utf8(onRequestPermissionCB->cb.env, - onRequestPermissionCB->permissions[i].c_str(), + onRequestPermissionCB->permissions[i].c_str(), NAPI_AUTO_LENGTH, &perValue); napi_set_element(onRequestPermissionCB->cb.env, perArray, i, perValue); @@ -2254,7 +2256,7 @@ void GetOrCreateLocalDirExecuteCB(napi_env env, void *data) std::string dataDir = getOrCreateLocalDirCB->cbBase.ability->GetAbilityInfo()->applicationInfo.dataDir; std::shared_ptr hap = getOrCreateLocalDirCB->cbBase.ability->GetHapModuleInfo(); std::string moduleName = (hap != nullptr) ? hap->name : std::string(); - std::string dataDirWithModuleName = dataDir + CONTEXT_DEAL_FILE_SEPARATOR + moduleName; + std::string dataDirWithModuleName = dataDir + NAPI_CONTEXT_FILE_SEPARATOR + moduleName; HILOG_INFO("NAPI_GetOrCreateLocalDir, dataDir:%{public}s moduleName:%{public}s abilityName:%{public}s", dataDir.c_str(), moduleName.c_str(), @@ -2268,7 +2270,7 @@ void GetOrCreateLocalDirExecuteCB(napi_env env, void *data) return; } - getOrCreateLocalDirCB->rootDir = dataDirWithModuleName + CONTEXT_DEAL_FILE_SEPARATOR + abilityName; + getOrCreateLocalDirCB->rootDir = dataDirWithModuleName + NAPI_CONTEXT_FILE_SEPARATOR + abilityName; HILOG_INFO("NAPI_GetOrCreateLocalDir, GetDir rootDir:%{public}s", getOrCreateLocalDirCB->rootDir.c_str()); if (!OHOS::FileExists(getOrCreateLocalDirCB->rootDir)) { HILOG_INFO("NAPI_GetOrCreateLocalDir dir is not exits, create dir."); @@ -2668,6 +2670,251 @@ napi_value NAPI_GetOrCreateLocalDir(napi_env env, napi_callback_info info) HILOG_INFO("%{public}s end.", __func__); return ret; } + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to CallingBundleCB on success, nullptr on failure. + */ +DatabaseDirCB *CreateGetDatabaseDirCBInfo(napi_env env) +{ + HILOG_INFO("%{public}s called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + DatabaseDirCB *getDatabaseDirCB = new (std::nothrow) DatabaseDirCB; + if (getDatabaseDirCB == nullptr) { + HILOG_ERROR("%{public}s, getDatabaseDirCB == nullptr.", __func__); + return nullptr; + } + getDatabaseDirCB->cbBase.cbInfo.env = env; + getDatabaseDirCB->cbBase.asyncWork = nullptr; + getDatabaseDirCB->cbBase.deferred = nullptr; + getDatabaseDirCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s end.", __func__); + return getDatabaseDirCB; +} + +/** + * @brief GetOrCreateLocalDir processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetDatabaseDirWrap(napi_env env, napi_callback_info info, DatabaseDirCB *getDatabaseDirCB) +{ + HILOG_INFO("%{public}s, called.", __func__); + if (getDatabaseDirCB == nullptr) { + HILOG_ERROR("%{public}s, getDatabaseDirCB == nullptr.", __func__); + return nullptr; + } + + getDatabaseDirCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (getDatabaseDirCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetDatabaseDir, ability == nullptr"); + getDatabaseDirCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return nullptr; + } + + std::string abilityName = getDatabaseDirCB->cbBase.ability->GetAbilityInfo()->name; + std::string dataDir = getDatabaseDirCB->cbBase.ability->GetAbilityInfo()->applicationInfo.dataDir; + std::shared_ptr hap = getDatabaseDirCB->cbBase.ability->GetHapModuleInfo(); + std::string moduleName = (hap != nullptr) ? hap->name : std::string(); + std::string dataDirWithModuleName = dataDir + NAPI_CONTEXT_FILE_SEPARATOR + moduleName; + HILOG_INFO("%{public}s, dataDir:%{public}s moduleName:%{public}s abilityName:%{public}s", + __func__, + dataDir.c_str(), + moduleName.c_str(), + abilityName.c_str()); + + // if dataDirWithModuleName is not exits, do nothing and return. + if (!OHOS::FileExists(dataDirWithModuleName)) { + getDatabaseDirCB->dataBaseDir = ""; + HILOG_INFO("%{public}s, dirWithModuleName is not exits:%{public}s, do nothing and return null.", + __func__, + dataDirWithModuleName.c_str()); + } else { + getDatabaseDirCB->dataBaseDir = dataDirWithModuleName + NAPI_CONTEXT_FILE_SEPARATOR + abilityName + + NAPI_CONTEXT_FILE_SEPARATOR + NAPI_CONTEXT_DATABASE; + HILOG_INFO("%{public}s, GetDir dataBaseDir:%{public}s", __func__, getDatabaseDirCB->dataBaseDir.c_str()); + if (!OHOS::FileExists(getDatabaseDirCB->dataBaseDir)) { + HILOG_INFO("NAPI_GetDatabaseDir dir is not exits, create dir."); + OHOS::ForceCreateDirectory(getDatabaseDirCB->dataBaseDir); + OHOS::ChangeModeDirectory(getDatabaseDirCB->dataBaseDir, MODE); + } + } + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, getDatabaseDirCB->dataBaseDir.c_str(), NAPI_AUTO_LENGTH, &result)); + + HILOG_INFO("%{public}s, end.", __func__); + return result; +} + +/** + * @brief Obtains the local database path. If it is the first call, the dir will be created. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetDatabaseDirSync(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + DatabaseDirCB *getDatabaseDirCB = CreateGetDatabaseDirCBInfo(env); + if (getDatabaseDirCB == nullptr) { + return WrapVoidToJS(env); + } + + getDatabaseDirCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + napi_value ret = GetDatabaseDirWrap(env, info, getDatabaseDirCB); + + delete getDatabaseDirCB; + getDatabaseDirCB = nullptr; + + if (ret == nullptr) { + ret = WrapVoidToJS(env); + HILOG_ERROR("%{public}s ret == nullptr", __func__); + } else { + HILOG_INFO("%{public}s, end.", __func__); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to CallingBundleCB on success, nullptr on failure. + */ +PreferencesDirCB *CreateGetPreferencesDirCBInfo(napi_env env) +{ + HILOG_INFO("%{public}s called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + PreferencesDirCB *getPreferencesDirCB = new (std::nothrow) PreferencesDirCB; + if (getPreferencesDirCB == nullptr) { + HILOG_ERROR("%{public}s, getPreferencesDirCB == nullptr.", __func__); + return nullptr; + } + getPreferencesDirCB->cbBase.cbInfo.env = env; + getPreferencesDirCB->cbBase.asyncWork = nullptr; + getPreferencesDirCB->cbBase.deferred = nullptr; + getPreferencesDirCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s end.", __func__); + return getPreferencesDirCB; +} + +/** + * @brief GetOrCreateLocalDir processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetPreferencesDirWrap(napi_env env, napi_callback_info info, PreferencesDirCB *getPreferencesDirCB) +{ + HILOG_INFO("%{public}s, called.", __func__); + if (getPreferencesDirCB == nullptr) { + HILOG_ERROR("%{public}s, getPreferencesDirCB == nullptr.", __func__); + return nullptr; + } + + getPreferencesDirCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (getPreferencesDirCB->cbBase.ability == nullptr) { + HILOG_ERROR("%{public}s, ability == nullptr", __func__); + getPreferencesDirCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return nullptr; + } + + std::string abilityName = getPreferencesDirCB->cbBase.ability->GetAbilityInfo()->name; + std::string dataDir = getPreferencesDirCB->cbBase.ability->GetAbilityInfo()->applicationInfo.dataDir; + std::shared_ptr hap = getPreferencesDirCB->cbBase.ability->GetHapModuleInfo(); + std::string moduleName = (hap != nullptr) ? hap->name : std::string(); + std::string dataDirWithModuleName = dataDir + NAPI_CONTEXT_FILE_SEPARATOR + moduleName; + HILOG_INFO("%{public}s, dataDir:%{public}s moduleName:%{public}s abilityName:%{public}s", + __func__, + dataDir.c_str(), + moduleName.c_str(), + abilityName.c_str()); + + // if dataDirWithModuleName is not exits, do nothing and return. + if (!OHOS::FileExists(dataDirWithModuleName)) { + getPreferencesDirCB->preferencesDir = ""; + HILOG_INFO("%{public}s, dirWithModuleName is not exits:%{public}s, do nothing and return null.", + __func__, + dataDirWithModuleName.c_str()); + } else { + getPreferencesDirCB->preferencesDir = dataDirWithModuleName + NAPI_CONTEXT_FILE_SEPARATOR + abilityName + + NAPI_CONTEXT_FILE_SEPARATOR + NAPI_CONTEXT_PREFERENCES; + HILOG_INFO( + "%{public}s, GetDir preferencesDir:%{public}s", __func__, getPreferencesDirCB->preferencesDir.c_str()); + if (!OHOS::FileExists(getPreferencesDirCB->preferencesDir)) { + HILOG_INFO("NAPI_GetPreferencesDir dir is not exits, create dir."); + OHOS::ForceCreateDirectory(getPreferencesDirCB->preferencesDir); + OHOS::ChangeModeDirectory(getPreferencesDirCB->preferencesDir, MODE); + } + } + napi_value result = nullptr; + NAPI_CALL( + env, napi_create_string_utf8(env, getPreferencesDirCB->preferencesDir.c_str(), NAPI_AUTO_LENGTH, &result)); + + HILOG_INFO("%{public}s, end.", __func__); + return result; +} + +/** + * @brief Obtains the local database path. If it is the first call, the dir will be created. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetPreferencesDirSync(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + PreferencesDirCB *preferencesDirCB = CreateGetPreferencesDirCBInfo(env); + if (preferencesDirCB == nullptr) { + return WrapVoidToJS(env); + } + + preferencesDirCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + napi_value ret = GetPreferencesDirWrap(env, info, preferencesDirCB); + + delete preferencesDirCB; + preferencesDirCB = nullptr; + + if (ret == nullptr) { + ret = WrapVoidToJS(env); + HILOG_ERROR("%{public}s ret == nullptr", __func__); + } else { + HILOG_INFO("%{public}s, end.", __func__); + } + return ret; +} + /** * @brief Context NAPI module registration. * @@ -2695,6 +2942,8 @@ napi_value ContextPermissionInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getCallingBundle", NAPI_GetCallingBundle), DECLARE_NAPI_FUNCTION("getOrCreateLocalDir", NAPI_GetOrCreateLocalDir), DECLARE_NAPI_FUNCTION("getFilesDir", NAPI_GetFilesDir), + DECLARE_NAPI_FUNCTION("getDatabaseDirSync", NAPI_GetDatabaseDirSync), + DECLARE_NAPI_FUNCTION("getPreferencesDirSync", NAPI_GetPreferencesDirSync), }; NAPI_CALL(env, diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h b/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h index 42761eabe79..a17344bbca0 100644 --- a/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h +++ b/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h @@ -165,6 +165,16 @@ struct GetOrCreateLocalDirCB { std::string rootDir; }; +struct DatabaseDirCB { + CBBase cbBase; + std::string dataBaseDir; +}; + +struct PreferencesDirCB { + CBBase cbBase; + std::string preferencesDir; +}; + struct ElementNameCB { CBBase cbBase; std::string deviceId; @@ -329,7 +339,6 @@ struct DAHelperReleaseCB { bool result = false; }; - struct DAHelperExecuteBatchCB { CBBase cbBase; std::string uri; diff --git a/tools/zip/include/file_path.h b/tools/zip/include/file_path.h index 918a598288c..72a4614f73e 100755 --- a/tools/zip/include/file_path.h +++ b/tools/zip/include/file_path.h @@ -81,7 +81,7 @@ private: std::string path_; void StripTrailingSeparatorsInternal(); - std::string::size_type FindDriveLetter(const std::string &path); + int FindDriveLetter(const std::string &path); bool AreAllSeparators(const std::string &input); }; } // namespace LIBZIP diff --git a/tools/zip/src/file_path.cpp b/tools/zip/src/file_path.cpp index bf85601fae8..b9ba70e0dcc 100755 --- a/tools/zip/src/file_path.cpp +++ b/tools/zip/src/file_path.cpp @@ -111,8 +111,8 @@ void FilePath::GetComponents(std::vector &components) // Capture drive letter, if any. FilePath dir = current.DirName(); - std::string::size_type letter = FindDriveLetter(dir.path_); - if (letter != std::string::npos) { + int letter = FindDriveLetter(dir.path_); + if (letter != static_cast(std::string::npos)) { components.push_back(std::string(dir.path_, 0, letter + 1)); } } @@ -122,12 +122,12 @@ FilePath FilePath::DirName() FilePath newPath(path_); newPath.StripTrailingSeparatorsInternal(); - std::string::size_type letter = FindDriveLetter(newPath.path_); + int letter = FindDriveLetter(newPath.path_); std::string::size_type lastSeparator = newPath.path_.find_last_of(kSeparators, std::string::npos, kSeparatorsLength - 1); - std::string::size_type one = 1; - std::string::size_type two = 2; - std::string::size_type three = 3; + int one = 1; + int two = 2; + int three = 3; if (lastSeparator == std::string::npos) { // path_ is in the current directory. newPath.path_.resize(letter + one); @@ -155,8 +155,8 @@ FilePath FilePath::BaseName() FilePath newPath(path_); newPath.StripTrailingSeparatorsInternal(); // The drive letter, if any, is always stripped. - std::string::size_type letter = FindDriveLetter(newPath.path_); - if (letter != std::string::npos) { + int letter = FindDriveLetter(newPath.path_); + if (letter != static_cast(std::string::npos)) { newPath.path_.erase(0, letter + 1); } @@ -178,7 +178,7 @@ void FilePath::StripTrailingSeparatorsInternal() } std::string::size_type one = 1; std::string::size_type two = 2; - std::string::size_type start = FindDriveLetter(path_) + two; + int start = FindDriveLetter(path_) + two; std::string::size_type lastStripped = std::string::npos; for (std::string::size_type pos = path_.length(); pos > start && FilePath::IsSeparator(path_[pos - one]); --pos) { if (pos != start + one || lastStripped == start + two || !FilePath::IsSeparator(path_[start - one])) { @@ -188,9 +188,9 @@ void FilePath::StripTrailingSeparatorsInternal() } } -std::string::size_type FilePath::FindDriveLetter(const std::string &path) +int FilePath::FindDriveLetter(const std::string &path) { - return std::string::npos; + return (int)std::string::npos; } bool FilePath::AreAllSeparators(const std::string &input) -- Gitee