diff --git a/frameworks/kits/ability/ability_runtime/include/ability_context_impl.h b/frameworks/kits/ability/ability_runtime/include/ability_context_impl.h index 1a12d57bbf0e7ec92af48d9965f7cc0280bba05c..0a6e5a945402db7a67ea488063b8a14b90f4e772 100755 --- a/frameworks/kits/ability/ability_runtime/include/ability_context_impl.h +++ b/frameworks/kits/ability/ability_runtime/include/ability_context_impl.h @@ -34,6 +34,7 @@ public: std::string GetDatabaseDir() override; std::string GetStorageDir() override; std::string GetDistributedFilesDir() override; + void SwitchArea(int mode) override; std::string GetBundleName() const override; std::shared_ptr GetApplicationInfo() const override; std::shared_ptr GetResourceManager() const override; diff --git a/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp b/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp index e31aa1de2fe5974928534a9fae12488a3790cb31..8ac0aef46b804acc966d705e4f173a2374e9a744 100755 --- a/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp +++ b/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp @@ -59,6 +59,14 @@ std::string AbilityContextImpl::GetDistributedFilesDir() return stageContext_ ? stageContext_->GetDistributedFilesDir() : ""; } +void AbilityContextImpl::SwitchArea(int mode) +{ + HILOG_DEBUG("AbilityContextImpl::SwitchArea."); + if (stageContext_ != nullptr) { + stageContext_->SwitchArea(mode); + } +} + ErrCode AbilityContextImpl::StartAbility(const AAFwk::Want &want, int requestCode) { BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); diff --git a/frameworks/kits/ability/native/src/js_service_extension_context.cpp b/frameworks/kits/ability/native/src/js_service_extension_context.cpp index 8786829e243edb06e6d93ca9c73a4288e7f86e6c..940ef1336139ea6c48ce6fb0a0106b29593e9a02 100755 --- a/frameworks/kits/ability/native/src/js_service_extension_context.cpp +++ b/frameworks/kits/ability/native/src/js_service_extension_context.cpp @@ -264,13 +264,9 @@ private: return; } HILOG_INFO("context->DisconnectAbility"); - context->DisconnectAbility(want, connection); - /* auto errcode = context->DisconnectAbility(want, connection); - if (errcode == 0) { - task.Resolve(engine, engine.CreateUndefined()); - } else { + auto errcode = context->DisconnectAbility(want, connection); + errcode == 0 ? task.Resolve(engine, engine.CreateUndefined()) : task.Reject(engine, CreateJsError(engine, errcode, "Disconnect Ability failed.")); - } */ }; NativeValue* lastParam = (info.argc == ARGC_ONE) ? nullptr : info.argv[INDEX_ONE]; diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context.h b/frameworks/kits/appkit/native/ability_runtime/context/context.h old mode 100644 new mode 100755 index c7ea728746246203eb10980a450d2c38dccb35c0..c2ae9a9b18a3f28bd11808a288f15de618f3d119 --- a/frameworks/kits/appkit/native/ability_runtime/context/context.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context.h @@ -137,6 +137,13 @@ public: */ virtual std::string GetDistributedFilesDir() = 0; + /** + * @brief Switch file area + * + * @param mode file area. + */ + virtual void SwitchArea(int mode) = 0; + /** * @brief Getting derived class * diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp old mode 100644 new mode 100755 index 92a2057121c8b0f448ee8fcf1742b142cbdd8c5f..9654f279e157c315f2fac8ce295636949faca9ff --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp @@ -26,22 +26,23 @@ namespace OHOS { namespace AbilityRuntime { const size_t Context::CONTEXT_TYPE_ID(std::hash {} ("Context")); const int64_t ContextImpl::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001); -const std::string ContextImpl::CONTEXT_BUNDLECODE_BASE("/data/app/base/"); -const std::string ContextImpl::CONTEXT_BUNDLECODE("/data/storage/app/base"); +const std::string ContextImpl::CONTEXT_DATA_APP("/data/app/"); const std::string ContextImpl::CONTEXT_BUNDLE("/bundle/"); const std::string ContextImpl::CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE("/mnt/hmdfs/"); const std::string ContextImpl::CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE("/device_view/local/data/"); const std::string ContextImpl::CONTEXT_DISTRIBUTEDFILES("distributedfiles"); const std::string ContextImpl::CONTEXT_FILE_SEPARATOR("/"); const std::string ContextImpl::CONTEXT_DATA("/data/"); -const std::string ContextImpl::CONTEXT_BASE("/data/storage/"); +const std::string ContextImpl::CONTEXT_DATA_STORAGE("/data/storage/"); +const std::string ContextImpl::CONTEXT_BASE("base"); const std::string ContextImpl::CONTEXT_PRIVATE("/private/"); const std::string ContextImpl::CONTEXT_CACHES("caches"); const std::string ContextImpl::CONTEXT_STORAGE("storage"); const std::string ContextImpl::CONTEXT_DATABASE("database"); const std::string ContextImpl::CONTEXT_TEMP("/temp"); const std::string ContextImpl::CONTEXT_FILES("/files"); -const std::string ContextImpl::CONTEXT_CE("ce"); +const std::string ContextImpl::CONTEXT_HAPS("/haps"); +const std::string ContextImpl::CONTEXT_ELS[] = {"el1", "el2", "el3", "el4"}; std::string ContextImpl::GetBundleName() const { @@ -55,9 +56,9 @@ std::string ContextImpl::GetBundleCodeDir() { std::string dir; if (IsCreateBySystemApp()) { - dir = CONTEXT_BUNDLECODE_BASE + GetBundleName(); + dir = CONTEXT_DATA_APP + CONTEXT_ELS[0] + CONTEXT_BUNDLE + GetBundleName(); } else { - dir = CONTEXT_BUNDLECODE; + dir = CONTEXT_DATA_STORAGE + CONTEXT_ELS[0] + CONTEXT_BUNDLE; } HILOG_DEBUG("ContextImpl::GetBundleCodeDir:%{public}s", dir.c_str()); return dir; @@ -72,7 +73,14 @@ std::string ContextImpl::GetCacheDir() std::string ContextImpl::GetDatabaseDir() { - std::string dir = GetBaseDir() + CONTEXT_PRIVATE + CONTEXT_DATABASE; + std::string dir; + if (IsCreateBySystemApp()) { + dir = CONTEXT_DATA_APP + currArea_ + CONTEXT_FILE_SEPARATOR + GetCurrentAccountId() + CONTEXT_FILE_SEPARATOR + + CONTEXT_DATABASE + GetBundleName(); + } else { + dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_DATABASE; + } + dir = dir + CONTEXT_FILE_SEPARATOR + ((GetHapModuleInfo() == nullptr) ? "" : GetHapModuleInfo()->moduleName); HILOG_DEBUG("ContextImpl::GetDatabaseDir:%{public}s", dir.c_str()); return dir; } @@ -106,23 +114,36 @@ std::string ContextImpl::GetDistributedFilesDir() dir = CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE + GetCurrentAccountId() + CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE + GetBundleName(); } else { - dir = CONTEXT_BASE + CONTEXT_DISTRIBUTEDFILES; + dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_DISTRIBUTEDFILES + + CONTEXT_FILE_SEPARATOR + ((GetHapModuleInfo() == nullptr) ? "" : GetHapModuleInfo()->moduleName); } HILOG_DEBUG("ContextImpl::GetDistributedFilesDir:%{public}s", dir.c_str()); return dir; } +void ContextImpl::SwitchArea(int mode) +{ + HILOG_DEBUG("ContextImpl::SwitchArea, mode:%{public}d.", mode); + if (mode < 0 || mode >= (sizeof(CONTEXT_ELS) / sizeof(CONTEXT_ELS[0]))) { + HILOG_ERROR("ContextImpl::SwitchArea, mode is invalid."); + return; + } + currArea_ = CONTEXT_ELS[mode]; + HILOG_DEBUG("ContextImpl::SwitchArea end, currArea:%{public}s.", currArea_.c_str()); +} + std::string ContextImpl::GetBaseDir() const { std::string baseDir; if (IsCreateBySystemApp()) { - baseDir = CONTEXT_DATA + currArea_ + CONTEXT_FILE_SEPARATOR + GetCurrentAccountId() + - CONTEXT_BUNDLE + GetBundleName(); + baseDir = CONTEXT_DATA_APP + currArea_ + CONTEXT_FILE_SEPARATOR + GetCurrentAccountId() + + CONTEXT_FILE_SEPARATOR + CONTEXT_BASE + CONTEXT_FILE_SEPARATOR + GetBundleName(); } else { - baseDir = CONTEXT_BASE + currArea_; + baseDir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_BASE; } if (parentContext_ != nullptr) { - baseDir = baseDir + CONTEXT_FILE_SEPARATOR + GetHapModuleInfo()->moduleName; + baseDir = baseDir + CONTEXT_HAPS + CONTEXT_FILE_SEPARATOR + + ((GetHapModuleInfo() == nullptr) ? "" : GetHapModuleInfo()->moduleName); } HILOG_DEBUG("ContextImpl::GetBaseDir:%{public}s", baseDir.c_str()); @@ -300,6 +321,9 @@ void ContextImpl::InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleIn std::shared_ptr ContextImpl::GetHapModuleInfo() const { + if (hapModuleInfo_ == nullptr) { + HILOG_ERROR("ContextImpl::GetHapModuleInfo, hapModuleInfo is empty"); + } return hapModuleInfo_; } @@ -328,4 +352,4 @@ void ContextImpl::InitAppContext() Context::appContext_ = shared_from_this(); } } // namespace AbilityRuntime -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h old mode 100644 new mode 100755 index db19e2c8348737c5d9d0c9e24cce630b7fb42197..4658e7fb5a2c5eb181b09623f6771e9ca3eb9217 --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h @@ -87,6 +87,13 @@ public: */ std::string GetDistributedFilesDir() override; + /** + * @brief Switch file area + * + * @param mode file area. + */ + void SwitchArea(int mode) override; + /** * @brief set the ResourceManager. * @@ -175,14 +182,15 @@ public: */ void InitAppContext(); private: - static const std::string CONTEXT_BUNDLECODE_BASE; - static const std::string CONTEXT_BUNDLECODE; + static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP; + static const std::string CONTEXT_DATA_APP; static const std::string CONTEXT_BUNDLE; static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE; static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE; static const std::string CONTEXT_DISTRIBUTEDFILES; static const std::string CONTEXT_FILE_SEPARATOR; static const std::string CONTEXT_DATA; + static const std::string CONTEXT_DATA_STORAGE; static const std::string CONTEXT_BASE; static const std::string CONTEXT_PRIVATE; static const std::string CONTEXT_CACHES; @@ -190,8 +198,9 @@ private: static const std::string CONTEXT_DATABASE; static const std::string CONTEXT_TEMP; static const std::string CONTEXT_FILES; - static const std::string CONTEXT_CE; - static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP; + static const std::string CONTEXT_HAPS; + static const std::string CONTEXT_ELS[]; + static const int EL_DEFAULT = 1; int flags_ = 0x00000000; void InitResourceManager( @@ -205,8 +214,8 @@ private: std::shared_ptr parentContext_ = nullptr; std::shared_ptr resourceManager_ = nullptr; std::shared_ptr hapModuleInfo_ = nullptr; - std::string currArea_ = CONTEXT_CE; + std::string currArea_ = CONTEXT_ELS[EL_DEFAULT]; }; } // namespace AbilityRuntime } // namespace OHOS -#endif // ABILITY_RUNTIME_CONTEXT_IMPL_H +#endif // ABILITY_RUNTIME_CONTEXT_IMPL_H \ No newline at end of file diff --git a/frameworks/kits/appkit/native/ability_runtime/context/js_context_utils.cpp b/frameworks/kits/appkit/native/ability_runtime/context/js_context_utils.cpp old mode 100644 new mode 100755 index 6ee66fa1059f5f32af4681edc4d9c4bc70d34e3d..08c265d7c6faae0b5110e5bd47955592c5a85e80 --- a/frameworks/kits/appkit/native/ability_runtime/context/js_context_utils.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/js_context_utils.cpp @@ -34,6 +34,7 @@ public: static void Finalizer(NativeEngine* engine, void* data, void* hint); static NativeValue* CreateBundleContext(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* GetApplicationContext(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* SwitchArea(NativeEngine* engine, NativeCallbackInfo* info); void KeepContext(std::shared_ptr context) { @@ -43,6 +44,7 @@ public: private: NativeValue* OnCreateBundleContext(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnGetApplicationContext(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnSwitchArea(NativeEngine& engine, NativeCallbackInfo& info); std::shared_ptr keepContext_; @@ -68,6 +70,47 @@ NativeValue* JsBaseContext::GetApplicationContext(NativeEngine* engine, NativeCa return me != nullptr ? me->OnGetApplicationContext(*engine, *info) : nullptr; } +NativeValue* JsBaseContext::SwitchArea(NativeEngine* engine, NativeCallbackInfo* info) +{ + HILOG_INFO("JsBaseContext::SwitchArea is called"); + JsBaseContext* me = CheckParamsAndGetThis(engine, info, BASE_CONTEXT_NAME); + return me != nullptr ? me->OnSwitchArea(*engine, *info) : nullptr; +} + +NativeValue* JsBaseContext::OnSwitchArea(NativeEngine& engine, NativeCallbackInfo& info) +{ + if (info.argc == 0) { + HILOG_ERROR("Not enough params"); + return engine.CreateUndefined(); + } + + auto context = context_.lock(); + if (!context) { + HILOG_WARN("context is already released"); + return engine.CreateUndefined(); + } + + int mode; + if (!ConvertFromJsValue(engine, info.argv[0], mode)) { + HILOG_ERROR("Parse mode failed"); + return engine.CreateUndefined(); + } + + context->SwitchArea(mode); + + NativeValue* thisVar = info.thisVar; + NativeObject* object = ConvertNativeValueTo(thisVar); + object->SetProperty("cacheDir", CreateJsValue(engine, context->GetCacheDir())); + object->SetProperty("tempDir", CreateJsValue(engine, context->GetTempDir())); + object->SetProperty("filesDir", CreateJsValue(engine, context->GetFilesDir())); + object->SetProperty("distributedFilesDir", CreateJsValue(engine, context->GetDistributedFilesDir())); + object->SetProperty("databaseDir", CreateJsValue(engine, context->GetDatabaseDir())); + object->SetProperty("storageDir", CreateJsValue(engine, context->GetStorageDir())); + object->SetProperty("bundleCodeDir", CreateJsValue(engine, context->GetBundleCodeDir())); + + return engine.CreateUndefined(); +} + NativeValue* JsBaseContext::OnCreateBundleContext(NativeEngine& engine, NativeCallbackInfo& info) { if (info.argc == 0) { @@ -151,6 +194,7 @@ NativeValue* CreateJsBaseContext(NativeEngine& engine, std::shared_ptr BindNativeFunction(engine, *object, "createBundleContext", JsBaseContext::CreateBundleContext); BindNativeFunction(engine, *object, "getApplicationContext", JsBaseContext::GetApplicationContext); + BindNativeFunction(engine, *object, "switchArea", JsBaseContext::SwitchArea); return objValue; } diff --git a/interfaces/kits/napi/aafwk/app/context/context.js b/interfaces/kits/napi/aafwk/app/context/context.js old mode 100644 new mode 100755 index d28e3283fd0514ca1996b710928d453c79d2e71f..88f512579d901f96affaabd106a9335624daeb51 --- a/interfaces/kits/napi/aafwk/app/context/context.js +++ b/interfaces/kits/napi/aafwk/app/context/context.js @@ -16,24 +16,55 @@ class Context { constructor(obj) { this.__context_impl__ = obj - - this.resourceManager = obj.resourceManager - this.applicationInfo = obj.applicationInfo - this.cacheDir = obj.cacheDir - this.tempDir = obj.tempDir - this.filesDir = obj.filesDir - this.distributedFilesDir = obj.distributedFilesDir - this.databaseDir = obj.databaseDir - this.storageDir = obj.storageDir - this.bundleCodeDir = obj.bundleCodeDir } createBundleContext(bundleName) { return this.__context_impl__.createBundleContext(bundleName) } + getApplicationContext() { return this.__context_impl__.getApplicationContext() } + + switchArea(mode) { + return this.__context_impl__.switchArea(mode) + } + + get resourceManager() { + return this.__context_impl__.resourceManager + } + + get applicationInfo() { + return this.__context_impl__.applicationInfo + } + + get cacheDir() { + return this.__context_impl__.cacheDir + } + + get tempDir() { + return this.__context_impl__.tempDir + } + + get filesDir() { + return this.__context_impl__.filesDir + } + + get distributedFilesDir() { + return this.__context_impl__.distributedFilesDir + } + + get databaseDir() { + return this.__context_impl__.databaseDir + } + + get storageDir() { + return this.__context_impl__.storageDir + } + + get bundleCodeDir() { + return this.__context_impl__.bundleCodeDir + } } export default Context