diff --git a/frameworks/kits/ability/native/BUILD.gn b/frameworks/kits/ability/native/BUILD.gn index 0ef838248f480d91710d3bb7d24b120033fee056..f9b88f955959fd907bc952d5a722dc8bbdaf933f 100644 --- a/frameworks/kits/ability/native/BUILD.gn +++ b/frameworks/kits/ability/native/BUILD.gn @@ -131,7 +131,7 @@ ohos_shared_library("abilitykit_native") { "${kits_path}/appkit/native/ability_runtime/extension_context.cpp", "${kits_path}/appkit/native/ability_runtime/service_extension_context.cpp", "${kits_path}/appkit/native/ability_runtime/static_subscriber_extension_context.cpp", - "${kits_path}/appkit/native/app/src/application_context.cpp", + "${kits_path}/appkit/native/app/src/app_context.cpp", "${kits_path}/appkit/native/app/src/context_container.cpp", "${kits_path}/appkit/native/app/src/context_deal.cpp", "${kits_path}/appkit/native/app/src/sys_mgr_client.cpp", diff --git a/frameworks/kits/ability/native/include/extension_base.inl b/frameworks/kits/ability/native/include/extension_base.inl index 6102eaae157834f4bd7c32b4655ab2a9087f0595..4413afad769371d06cc96fe35bddfd59fa42989d 100644 --- a/frameworks/kits/ability/native/include/extension_base.inl +++ b/frameworks/kits/ability/native/include/extension_base.inl @@ -14,6 +14,7 @@ */ #include "foundation/aafwk/standard/frameworks/kits/appkit/native/ability_runtime/context/context.h" +#include "foundation/aafwk/standard/frameworks/kits/appkit/native/ability_runtime/context/application_context.h" #include "hilog_wrapper.h" namespace OHOS { diff --git a/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp b/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp index 4f8e74336be9b455c1c6a0ab87bc4b2d20ab12f5..f3a95b99bd564ea150a210a86e6468039c3e14a1 100644 --- a/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp +++ b/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp @@ -129,7 +129,7 @@ void JsAbility::OnStart(const Want &want) HILOG_WARN("Not found Ability.js"); return; } - auto applicationContext = AbilityRuntime::Context::GetJsApplicationContext(); + auto applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { applicationContext->DispatchOnAbilityCreate(jsAbilityObj_); } @@ -182,7 +182,7 @@ void JsAbility::OnStop() HILOG_INFO("The service connection is not disconnected."); } - auto applicationContext = AbilityRuntime::Context::GetJsApplicationContext(); + auto applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { applicationContext->DispatchOnAbilityDestroy(jsAbilityObj_); } @@ -207,7 +207,7 @@ void JsAbility::OnSceneCreated() delegator->PostPerformScenceCreated(CreateADelegatorAbilityProperty()); } - auto applicationContext = AbilityRuntime::Context::GetJsApplicationContext(); + auto applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { applicationContext->DispatchOnAbilityWindowStageCreate(jsAbilityObj_); } @@ -247,7 +247,7 @@ void JsAbility::onSceneDestroyed() delegator->PostPerformScenceDestroyed(CreateADelegatorAbilityProperty()); } - auto applicationContext = AbilityRuntime::Context::GetJsApplicationContext(); + auto applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { applicationContext->DispatchOnAbilityWindowStageDestroy(jsAbilityObj_); } @@ -282,7 +282,7 @@ void JsAbility::OnForeground(const Want &want) delegator->PostPerformForeground(CreateADelegatorAbilityProperty()); } - auto applicationContext = AbilityRuntime::Context::GetJsApplicationContext(); + auto applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { applicationContext->DispatchOnAbilityForeground(jsAbilityObj_); } @@ -301,7 +301,7 @@ void JsAbility::OnBackground() delegator->PostPerformBackground(CreateADelegatorAbilityProperty()); } - auto applicationContext = AbilityRuntime::Context::GetJsApplicationContext(); + auto applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { applicationContext->DispatchOnAbilityBackground(jsAbilityObj_); } @@ -339,7 +339,7 @@ int32_t JsAbility::OnContinue(WantParams &wantParams) return false; } - auto applicationContext = AbilityRuntime::Context::GetJsApplicationContext(); + auto applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { applicationContext->DispatchOnAbilityContinue(jsAbilityObj_); } diff --git a/frameworks/kits/ability/native/test/BUILD.gn b/frameworks/kits/ability/native/test/BUILD.gn index 262821293b81985cc510f5eab9f650e84fbc0d31..1b66646317f6ea7a9738972d7c50958020d8668c 100644 --- a/frameworks/kits/ability/native/test/BUILD.gn +++ b/frameworks/kits/ability/native/test/BUILD.gn @@ -86,8 +86,8 @@ ohos_unittest("ability_test") { module_out_path = module_output_path sources = [ "${aafwk_path}/frameworks/kits/appkit/native/app/src/ability_record_mgr.cpp", + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", "unittest/ability_test.cpp", ] @@ -442,8 +442,8 @@ ohos_unittest("data_ability_operation_test") { ohos_unittest("ability_impl_active_test") { module_out_path = module_output_path sources = [ + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", ] @@ -480,8 +480,8 @@ ohos_unittest("ability_impl_active_test") { ohos_unittest("ability_impl_test") { module_out_path = module_output_path sources = [ + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", ] @@ -529,8 +529,8 @@ ohos_unittest("ability_thread_test") { module_out_path = module_output_path sources = [ "${aafwk_path}/frameworks/kits/ability/native/test/mock/include/mock_data_ability_impl.cpp", + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_container.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_deal.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", @@ -654,8 +654,8 @@ ohos_unittest("data_ability_impl_test") { module_out_path = module_output_path sources = [ "${aafwk_path}/frameworks/kits/ability/native/test/mock/include/mock_replace_ability_impl.cpp", + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_container.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_deal.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", @@ -706,8 +706,8 @@ ohos_unittest("data_ability_impl_file_secondpart_test") { module_out_path = module_output_path sources = [ "${aafwk_path}/frameworks/kits/ability/native/test/mock/include/mock_replace_ability_impl.cpp", + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_container.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_deal.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", @@ -758,8 +758,8 @@ ohos_unittest("data_ability_impl_file_test") { module_out_path = module_output_path sources = [ "${aafwk_path}/frameworks/kits/ability/native/test/mock/include/mock_replace_ability_impl.cpp", + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_container.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_deal.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", @@ -810,8 +810,8 @@ ohos_unittest("ability_thread_dataability_test") { module_out_path = module_output_path sources = [ "${aafwk_path}/frameworks/kits/ability/native/test/mock/include/mock_data_ability_impl.cpp", + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_container.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/context_deal.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", @@ -1042,8 +1042,8 @@ ohos_unittest("form_host_client_test") { if (ability_runtime_graphics) { sources = [ "${aafwk_path}/frameworks/kits/appkit/native/app/src/ability_record_mgr.cpp", + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", "${aafwk_path}/services/abilitymgr/src/ability_start_setting.cpp", "mock/include/mock_bundle_manager_form.cpp", @@ -1085,8 +1085,8 @@ ohos_unittest("form_host_client_test") { ohos_unittest("continuation_test") { module_out_path = module_output_path sources = [ + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/ohos_application.cpp", "unittest/continuation_test.cpp", ] diff --git a/frameworks/kits/appkit/BUILD.gn b/frameworks/kits/appkit/BUILD.gn index 58a109f911c56b382153a45fa33678a8fb3912f3..e47eda53779c42418a47749683989f6ce7110db8 100644 --- a/frameworks/kits/appkit/BUILD.gn +++ b/frameworks/kits/appkit/BUILD.gn @@ -94,8 +94,8 @@ ohos_shared_library("appkit_native") { "native/ability_runtime/app/js_ability_stage_context.cpp", "native/app/src/ability_manager.cpp", "native/app/src/ability_record_mgr.cpp", + "native/app/src/app_context.cpp", "native/app/src/app_loader.cpp", - "native/app/src/application_context.cpp", "native/app/src/application_env.cpp", "native/app/src/application_env_impl.cpp", "native/app/src/application_impl.cpp", diff --git a/frameworks/kits/appkit/native/ability_runtime/context/ability_lifecycle_callback.cpp b/frameworks/kits/appkit/native/ability_runtime/context/ability_lifecycle_callback.cpp index 85956d34cf64b2803c127e9f3527214e131dc7c3..600f8c3efc4b02e288d371584913488e6da39991 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/ability_lifecycle_callback.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/ability_lifecycle_callback.cpp @@ -20,88 +20,124 @@ namespace OHOS { namespace AbilityRuntime { -AbilityLifecycleCallback::AbilityLifecycleCallback(NativeEngine &engine) +JsAbilityLifecycleCallback::JsAbilityLifecycleCallback(NativeEngine* engine) : engine_(engine) { } -AbilityLifecycleCallback::~AbilityLifecycleCallback() = default; +int32_t JsAbilityLifecycleCallback::serialNumber_ = 0; -void AbilityLifecycleCallback::OnAbilityCreate(const std::weak_ptr &abilityObj) +void JsAbilityLifecycleCallback::CallJsMethodInner( + const std::string &methodName, const std::shared_ptr &ability) { - HILOG_INFO("AbilityLifecycleCallback onAbilityCreate"); - CallLifecycleCBFunction("onAbilityCreate", abilityObj); + for (auto &callback : callbacks_) { + if (!callback.second) { + HILOG_ERROR("Invalid jsCallback"); + return; + } + + auto value = callback.second->Get(); + auto obj = ConvertNativeValueTo(value); + if (obj == nullptr) { + HILOG_ERROR("Failed to get object"); + return; + } + + auto method = obj->GetProperty(methodName.data()); + if (method == nullptr) { + HILOG_ERROR("Failed to get %{public}s from object", methodName.data()); + return; + } + + auto nativeAbilityObj = engine_->CreateNull(); + if (ability != nullptr) { + nativeAbilityObj = ability->Get(); + } + + NativeValue *argv[] = { nativeAbilityObj }; + engine_->CallFunction(value, method, argv, ArraySize(argv)); + } } -void AbilityLifecycleCallback::OnAbilityWindowStageCreate(const std::weak_ptr &abilityObj) +void JsAbilityLifecycleCallback::CallJsMethod( + const std::string &methodName, const std::weak_ptr &abilityObj) { - CallLifecycleCBFunction("onAbilityWindowStageCreate", abilityObj); + HILOG_INFO("methodName = %{public}s", methodName.c_str()); + // js callback should run in js thread + auto ability = abilityObj.lock(); + if (!ability) { + return; + } + std::unique_ptr complete = std::make_unique( + [JsAbilityLifecycleCallback = this, methodName, ability]( + NativeEngine &engine, AsyncTask &task, int32_t status) { + if (JsAbilityLifecycleCallback) { + JsAbilityLifecycleCallback->CallJsMethodInner(methodName, ability); + } + }); + NativeReference *callback = nullptr; + std::unique_ptr execute = nullptr; + AsyncTask::Schedule(*engine_, std::make_unique(callback, std::move(execute), std::move(complete))); } -void AbilityLifecycleCallback::OnAbilityWindowStageDestroy(const std::weak_ptr &abilityObj) +void JsAbilityLifecycleCallback::OnAbilityCreate(const std::weak_ptr &abilityObj) { - CallLifecycleCBFunction("onAbilityWindowStageDestroy", abilityObj); + CallJsMethod("onAbilityCreate", abilityObj); } -void AbilityLifecycleCallback::OnAbilityDestroy(const std::weak_ptr &abilityObj) +void JsAbilityLifecycleCallback::OnAbilityWindowStageCreate(const std::weak_ptr &abilityObj) { - CallLifecycleCBFunction("onAbilityDestroy", abilityObj); + CallJsMethod("onAbilityWindowStageCreate", abilityObj); } -void AbilityLifecycleCallback::OnAbilityForeground(const std::weak_ptr &abilityObj) +void JsAbilityLifecycleCallback::OnAbilityWindowStageDestroy(const std::weak_ptr &abilityObj) { - CallLifecycleCBFunction("onAbilityForeground", abilityObj); + CallJsMethod("onAbilityWindowStageDestroy", abilityObj); } -void AbilityLifecycleCallback::OnAbilityBackground(const std::weak_ptr &abilityObj) +void JsAbilityLifecycleCallback::OnAbilityDestroy(const std::weak_ptr &abilityObj) { - CallLifecycleCBFunction("onAbilityBackground", abilityObj); + CallJsMethod("onAbilityDestroy", abilityObj); } -void AbilityLifecycleCallback::OnAbilityContinue(const std::weak_ptr &abilityObj) + +void JsAbilityLifecycleCallback::OnAbilityForeground(const std::weak_ptr &abilityObj) { - CallLifecycleCBFunction("onAbilityContinue", abilityObj); + CallJsMethod("onAbilityForeground", abilityObj); } -void AbilityLifecycleCallback::SetJsCallback(NativeValue *jsCallback) +void JsAbilityLifecycleCallback::OnAbilityBackground(const std::weak_ptr &abilityObj) { - jsCallback_ = std::shared_ptr(engine_.CreateReference(jsCallback, 1)); + CallJsMethod("onAbilityBackground", abilityObj); } -NativeValue *AbilityLifecycleCallback::CallLifecycleCBFunction(const std::string &functionName, - const std::weak_ptr &abilityObj) +void JsAbilityLifecycleCallback::OnAbilityContinue(const std::weak_ptr &abilityObj) { - if (functionName.empty()) { - HILOG_ERROR("Invalid function name"); - return nullptr; - } - - if (!jsCallback_) { - HILOG_ERROR("Invalid jsCallback"); - return nullptr; - } + CallJsMethod("onAbilityContinue", abilityObj); +} - auto value = jsCallback_->Get(); - auto obj = ConvertNativeValueTo(value); - if (obj == nullptr) { - HILOG_ERROR("Failed to get object"); - return nullptr; +int32_t JsAbilityLifecycleCallback::Register(NativeValue *jsCallback) +{ + if (engine_ == nullptr) { + return -1; } - - auto method = obj->GetProperty(functionName.data()); - if (method == nullptr) { - HILOG_ERROR("Failed to get %{public}s from object", functionName.data()); - return nullptr; + int32_t callbackId = serialNumber_; + if (callbackId < INT32_MAX) { + callbackId++; + } else { + callbackId = -1; } + callbacks_.emplace(callbackId, std::shared_ptr(engine_->CreateReference(jsCallback, 1))); + return callbackId; +} - auto nativeAbilityObj = engine_.CreateNull(); - if (!abilityObj.expired()) { - if (abilityObj.lock() != nullptr) { - nativeAbilityObj = abilityObj.lock()->Get(); - } - } +void JsAbilityLifecycleCallback::UnRegister(int32_t callbackId) +{ + callbacks_.erase(callbackId); +} - NativeValue* argv[] = { nativeAbilityObj }; - return engine_.CallFunction(value, method, argv, ArraySize(argv)); +bool JsAbilityLifecycleCallback::IsEmpty() +{ + return callbacks_.empty(); } } // namespace AbilityRuntime } // namespace OHOS \ No newline at end of file diff --git a/frameworks/kits/appkit/native/ability_runtime/context/ability_lifecycle_callback.h b/frameworks/kits/appkit/native/ability_runtime/context/ability_lifecycle_callback.h index 560a567c810d5373bda9ec62e08cdf81f39b808a..6d601a4822f4c5cea3cb58437aca8c12532dcc6d 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/ability_lifecycle_callback.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/ability_lifecycle_callback.h @@ -16,6 +16,7 @@ #ifndef ABILITY_RUNTIME_JS_APPLICATION_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H #define ABILITY_RUNTIME_JS_APPLICATION_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H +#include #include class NativeEngine; @@ -27,8 +28,7 @@ namespace OHOS { namespace AbilityRuntime { class AbilityLifecycleCallback { public: - explicit AbilityLifecycleCallback(NativeEngine &engine); - ~AbilityLifecycleCallback(); + virtual ~AbilityLifecycleCallback() {} /** * Called back when the ability is started for initialization. * @@ -37,7 +37,7 @@ public: * @param ability: Indicates the ability to register for listening. * @StageModelOnly */ - void OnAbilityCreate(const std::weak_ptr &abilityObj); + virtual void OnAbilityCreate(const std::weak_ptr &abilityObj) = 0; /** * Called back when the ability window stage is created. @@ -47,7 +47,7 @@ public: * @param ability: Indicates the ability to register for listening. * @StageModelOnly */ - void OnAbilityWindowStageCreate(const std::weak_ptr &abilityObj); + virtual void OnAbilityWindowStageCreate(const std::weak_ptr &abilityObj) = 0; /** * Called back when the ability window stage is created. @@ -57,7 +57,7 @@ public: * @param ability: Indicates the ability to register for listening. * @StageModelOnly */ - void OnAbilityWindowStageDestroy(const std::weak_ptr &abilityObj); + virtual void OnAbilityWindowStageDestroy(const std::weak_ptr &abilityObj) = 0; /** * Called back when the ability window stage is created. @@ -67,7 +67,7 @@ public: * @param ability: Indicates the ability to register for listening. * @StageModelOnly */ - void OnAbilityDestroy(const std::weak_ptr &abilityObj); + virtual void OnAbilityDestroy(const std::weak_ptr &abilityObj) = 0; /** * Called back when the ability window stage is created. @@ -77,7 +77,7 @@ public: * @param ability: Indicates the ability to register for listening. * @StageModelOnly */ - void OnAbilityForeground(const std::weak_ptr &abilityObj); + virtual void OnAbilityForeground(const std::weak_ptr &abilityObj) = 0; /** * Called back when the ability window stage is created. @@ -87,7 +87,7 @@ public: * @param ability: Indicates the ability to register for listening. * @StageModelOnly */ - void OnAbilityBackground(const std::weak_ptr &abilityObj); + virtual void OnAbilityBackground(const std::weak_ptr &abilityObj) = 0; /** * Called back when the ability window stage is created. @@ -97,16 +97,32 @@ public: * @param ability: Indicates the ability to register for listening. * @StageModelOnly */ - void OnAbilityContinue(const std::weak_ptr &abilityObj); + virtual void OnAbilityContinue(const std::weak_ptr &abilityObj) = 0; +}; - void SetJsCallback(NativeValue *jsCallback); +class JsAbilityLifecycleCallback : public AbilityLifecycleCallback, + public std::enable_shared_from_this { +public: + explicit JsAbilityLifecycleCallback(NativeEngine* engine); + void OnAbilityCreate(const std::weak_ptr &abilityObj) override; + void OnAbilityWindowStageCreate(const std::weak_ptr &abilityObj) override; + void OnAbilityWindowStageDestroy(const std::weak_ptr &abilityObj) override; + void OnAbilityDestroy(const std::weak_ptr &abilityObj) override; + void OnAbilityForeground(const std::weak_ptr &abilityObj) override; + void OnAbilityBackground(const std::weak_ptr &abilityObj) override; + void OnAbilityContinue(const std::weak_ptr &abilityObj) override; + int32_t Register(NativeValue *jsCallback); + void UnRegister(int32_t callbackId); + bool IsEmpty(); + static int32_t serialNumber_; private: - NativeValue *CallLifecycleCBFunction(const std::string &functionName, - const std::weak_ptr &abilityObj); - NativeEngine &engine_; + NativeEngine* engine_; std::shared_ptr jsCallback_; + std::map> callbacks_; + void CallJsMethod(const std::string &methodName, const std::weak_ptr &abilityObj); + void CallJsMethodInner(const std::string &methodName, const std::shared_ptr &ability); }; } // namespace AbilityRuntime } // namespace OHOS -#endif // ABILITY_RUNTIME_JS_APPLICATION_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H \ No newline at end of file +#endif // ABILITY_RUNTIME_JS_APPLICATION_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H diff --git a/frameworks/kits/appkit/native/ability_runtime/context/application_context.cpp b/frameworks/kits/appkit/native/ability_runtime/context/application_context.cpp index 0e888b7aae9ffd7584e21a54152f3d7f0f5e9884..68a4e770aea0ab9ec12c34f51c6d1bd92463fda1 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/application_context.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/application_context.cpp @@ -17,19 +17,11 @@ #include -#include "configuration.h" #include "hilog_wrapper.h" namespace OHOS { namespace AbilityRuntime { -std::map> ApplicationContext::callbacks_; -std::shared_ptr Context::applicationContext_ = nullptr; - -std::shared_ptr Context::GetJsApplicationContext() -{ - std::lock_guard lock(Context::contextMutex_); - return applicationContext_; -} +std::vector> ApplicationContext::callbacks_; void ApplicationContext::InitApplicationContext() { @@ -43,19 +35,19 @@ void ApplicationContext::AttachContextImpl(const std::shared_ptr &c } void ApplicationContext::RegisterAbilityLifecycleCallback( - const int64_t callbackId, const std::shared_ptr &abilityLifecycleCallback) + const std::shared_ptr &abilityLifecycleCallback) { HILOG_INFO("ApplicationContext RegisterAbilityLifecycleCallback"); - callbacks_.emplace(callbackId, abilityLifecycleCallback); + callbacks_.push_back(abilityLifecycleCallback); } -void ApplicationContext::UnregisterAbilityLifecycleCallback(const int64_t callbackId) +void ApplicationContext::UnregisterAbilityLifecycleCallback( + const std::shared_ptr &abilityLifecycleCallback) { HILOG_INFO("ApplicationContext UnregisterAbilityLifecycleCallback"); - auto iter = callbacks_.find(callbackId); - if (iter != callbacks_.end()) { - HILOG_INFO("callbackId exist"); - callbacks_.erase(iter); + auto it = std::find(callbacks_.begin(), callbacks_.end(), abilityLifecycleCallback); + if (it != callbacks_.end()) { + callbacks_.erase(it); } } @@ -66,7 +58,7 @@ void ApplicationContext::DispatchOnAbilityCreate(const std::weak_ptrOnAbilityCreate(abilityObj); + callback->OnAbilityCreate(abilityObj); } } @@ -77,7 +69,7 @@ void ApplicationContext::DispatchOnAbilityWindowStageCreate(const std::weak_ptr< return; } for (auto callback : callbacks_) { - callback.second->OnAbilityWindowStageCreate(abilityObj); + callback->OnAbilityWindowStageCreate(abilityObj); } } @@ -88,7 +80,7 @@ void ApplicationContext::DispatchOnAbilityWindowStageDestroy(const std::weak_ptr return; } for (auto callback : callbacks_) { - callback.second->OnAbilityWindowStageDestroy(abilityObj); + callback->OnAbilityWindowStageDestroy(abilityObj); } } @@ -99,7 +91,7 @@ void ApplicationContext::DispatchOnAbilityDestroy(const std::weak_ptrOnAbilityDestroy(abilityObj); + callback->OnAbilityDestroy(abilityObj); } } @@ -110,7 +102,7 @@ void ApplicationContext::DispatchOnAbilityForeground(const std::weak_ptrOnAbilityForeground(abilityObj); + callback->OnAbilityForeground(abilityObj); } } @@ -121,7 +113,7 @@ void ApplicationContext::DispatchOnAbilityBackground(const std::weak_ptrOnAbilityBackground(abilityObj); + callback->OnAbilityBackground(abilityObj); } } void ApplicationContext::DispatchOnAbilityContinue(const std::weak_ptr &abilityObj) @@ -131,7 +123,7 @@ void ApplicationContext::DispatchOnAbilityContinue(const std::weak_ptrOnAbilityContinue(abilityObj); + callback->OnAbilityContinue(abilityObj); } } @@ -238,7 +230,6 @@ int ApplicationContext::GetArea() return contextImpl_->GetArea(); } - std::shared_ptr ApplicationContext::GetConfiguration() const { return (contextImpl_ != nullptr) ? contextImpl_->GetConfiguration() : nullptr; @@ -249,4 +240,4 @@ std::string ApplicationContext::GetBaseDir() const return (contextImpl_ != nullptr) ? contextImpl_->GetBaseDir() : nullptr; } } // namespace AbilityRuntime -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/kits/appkit/native/ability_runtime/context/application_context.h b/frameworks/kits/appkit/native/ability_runtime/context/application_context.h index 15efdc1f4ea64b06c13dc89f0c4f6040be84429c..fb2a7bbc9645f5c8dcfa29da02818d2f5ecbdc3b 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/application_context.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/application_context.h @@ -16,7 +16,7 @@ #ifndef ABILITY_RUNTIME_APPLICATION_CONTEXT_H #define ABILITY_RUNTIME_APPLICATION_CONTEXT_H -#include +#include #include #include "ability_lifecycle_callback.h" @@ -25,14 +25,12 @@ namespace OHOS { namespace AbilityRuntime { -class ContextImpl; class ApplicationContext : public Context, public std::enable_shared_from_this { public: ApplicationContext() = default; ~ApplicationContext() = default; - void RegisterAbilityLifecycleCallback( - const int64_t callbackId, const std::shared_ptr &abilityLifecycleCallback); - void UnregisterAbilityLifecycleCallback(const int64_t callbackId); + void RegisterAbilityLifecycleCallback(const std::shared_ptr &abilityLifecycleCallback); + void UnregisterAbilityLifecycleCallback(const std::shared_ptr &abilityLifecycleCallback); void DispatchOnAbilityCreate(const std::weak_ptr &abilityObj); void DispatchOnAbilityWindowStageCreate(const std::weak_ptr &abilityObj); void DispatchOnAbilityWindowStageDestroy(const std::weak_ptr &abilityObj); @@ -41,9 +39,6 @@ public: void DispatchOnAbilityBackground(const std::weak_ptr &abilityObj); void DispatchOnAbilityContinue(const std::weak_ptr &abilityObj); - void SetConfiguration(const std::shared_ptr &config); - void SetStageContext(const std::shared_ptr &stageContext); - std::string GetBundleName() const override; std::shared_ptr CreateBundleContext(const std::string &bundleName) override; std::shared_ptr GetApplicationInfo() const override; @@ -71,8 +66,8 @@ public: private: std::shared_ptr contextImpl_; - static std::map> callbacks_; + static std::vector> callbacks_; }; } // namespace AbilityRuntime } // namespace OHOS -#endif // ABILITY_RUNTIME_APPLICATION_CONTEXT_H \ No newline at end of file +#endif // ABILITY_RUNTIME_APPLICATION_CONTEXT_H diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context.h b/frameworks/kits/appkit/native/ability_runtime/context/context.h index ce4b2a8e4e914b7bfcf85e6d097c997579bc462c..42db0764c80145d6a76e6e5c5675a10a6c97ab05 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/context.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context.h @@ -42,14 +42,7 @@ public: * * @return Returns the Context object of the application. */ - static std::shared_ptr GetApplicationContext(); - - /** - * @brief Obtains the Js Context object of the application. - * - * @return Returns the Js Context object of the application. - */ - static std::shared_ptr GetJsApplicationContext(); + static std::shared_ptr GetApplicationContext(); /** * @brief Obtains the bundle name of the current ability. @@ -236,7 +229,6 @@ protected: return contextTypeId == CONTEXT_TYPE_ID; } - static std::shared_ptr appContext_; static std::shared_ptr applicationContext_; static std::mutex contextMutex_; }; diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp index 02e88f707c269bb1d980f5c88a40588fad0a4942..ec4a79fc18c217f2ad8d7a4d5e6cd546d2efb4cd 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp @@ -434,19 +434,13 @@ bool ContextImpl::IsCreateBySystemApp() const return (static_cast(flags_) & static_cast(CONTEXT_CREATE_BY_SYSTEM_APP)) == 1; } -std::shared_ptr Context::appContext_ = nullptr; +std::shared_ptr Context::applicationContext_ = nullptr; std::mutex Context::contextMutex_; -std::shared_ptr Context::GetApplicationContext() +std::shared_ptr Context::GetApplicationContext() { std::lock_guard lock(contextMutex_); - return appContext_; -} - -void ContextImpl::InitAppContext() -{ - std::lock_guard lock(Context::contextMutex_); - Context::appContext_ = shared_from_this(); + return applicationContext_; } void ContextImpl::SetToken(const sptr &token) diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h index 45af4fa97682a3288f8eeecef93d375845c01b17..46efd972db4e581dd82d1d07b4b01fa78cb0bb72 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h @@ -199,11 +199,6 @@ public: */ void InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo); - /** - * @brief Set application context - */ - void InitAppContext(); - /** * @brief Set the token witch the app launched. * diff --git a/frameworks/kits/appkit/native/ability_runtime/context/js_application_context_utils.cpp b/frameworks/kits/appkit/native/ability_runtime/context/js_application_context_utils.cpp index 1c3c1e92f18abc26f50afb5e6955f514deba7a66..60014e30e1d9a9ae84553bb0610d9691bb50a096 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/js_application_context_utils.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/js_application_context_utils.cpp @@ -19,6 +19,10 @@ #include "application_context.h" #include "hilog_wrapper.h" +#include "js_context_utils.h" +#include "js_data_struct_converter.h" +#include "js_hap_module_info_utils.h" +#include "js_resource_manager_utils.h" #include "js_runtime_utils.h" namespace OHOS { @@ -30,7 +34,6 @@ constexpr size_t ARGC_TWO = 2; constexpr size_t INDEX_ZERO = 0; constexpr size_t INDEX_ONE = 1; constexpr int32_t ERROR_CODE_ONE = 1; -constexpr int32_t MAX_ERROR_CODE = -1; class JsApplicationContextUtils { public: @@ -42,21 +45,275 @@ public: static void Finalizer(NativeEngine *engine, void *data, void *hint); static NativeValue *RegisterAbilityLifecycleCallback(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue *UnregisterAbilityLifecycleCallback(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue *CreateBundleContext(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue *SwitchArea(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* GetArea(NativeEngine* engine, NativeCallbackInfo* info); NativeValue *OnRegisterAbilityLifecycleCallback(NativeEngine &engine, NativeCallbackInfo &info); NativeValue *OnUnregisterAbilityLifecycleCallback(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue *OnGetCacheDir(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue *OnGetTempDir(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue *OnGetFilesDir(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue *OnGetDistributedFilesDir(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue *OnGetDatabaseDir(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue *OnGetPreferencesDir(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue *OnGetBundleCodeDir(NativeEngine &engine, NativeCallbackInfo &info); + + static NativeValue *GetCacheDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue *GetTempDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue *GetFilesDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue *GetDistributedFilesDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue *GetDatabaseDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue *GetPreferencesDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue *GetBundleCodeDir(NativeEngine *engine, NativeCallbackInfo *info); + void KeepApplicationContext(std::shared_ptr applicationContext) { keepApplicationContext_ = applicationContext; } + protected: std::weak_ptr applicationContext_; private: + NativeValue *OnCreateBundleContext(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue *OnSwitchArea(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetArea(NativeEngine& engine, NativeCallbackInfo& info); std::shared_ptr keepApplicationContext_; + std::shared_ptr callback_; }; +NativeValue *JsApplicationContextUtils::CreateBundleContext(NativeEngine *engine, NativeCallbackInfo *info) +{ + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnCreateBundleContext(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnCreateBundleContext(NativeEngine &engine, NativeCallbackInfo &info) +{ + if (info.argc == 0) { + HILOG_ERROR("Not enough params"); + return engine.CreateUndefined(); + } + + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + + std::string bundleName; + if (!ConvertFromJsValue(engine, info.argv[0], bundleName)) { + HILOG_ERROR("Parse bundleName failed"); + return engine.CreateUndefined(); + } + + auto bundleContext = applicationContext->CreateBundleContext(bundleName); + if (!bundleContext) { + HILOG_ERROR("bundleContext is nullptr"); + return engine.CreateUndefined(); + } + + JsRuntime &jsRuntime = *static_cast(engine.GetJsEngine()); + NativeValue *value = CreateJsBaseContext(engine, bundleContext, true); + return jsRuntime.LoadSystemModule("application.Context", &value, 1)->Get(); +} + +NativeValue *JsApplicationContextUtils::SwitchArea(NativeEngine *engine, NativeCallbackInfo *info) +{ + HILOG_INFO("JsApplicationContextUtils::SwitchArea is called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnSwitchArea(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnSwitchArea(NativeEngine &engine, NativeCallbackInfo &info) +{ + if (info.argc == 0) { + HILOG_ERROR("Not enough params"); + return engine.CreateUndefined(); + } + + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + + int mode; + if (!ConvertFromJsValue(engine, info.argv[0], mode)) { + HILOG_ERROR("Parse mode failed"); + return engine.CreateUndefined(); + } + + applicationContext->SwitchArea(mode); + + NativeValue *thisVar = info.thisVar; + NativeObject *object = ConvertNativeValueTo(thisVar); + BindNativeProperty(*object, "cacheDir", GetCacheDir); + BindNativeProperty(*object, "tempDir", GetTempDir); + BindNativeProperty(*object, "filesDir", GetFilesDir); + BindNativeProperty(*object, "distributedFilesDir", GetDistributedFilesDir); + BindNativeProperty(*object, "databaseDir", GetDatabaseDir); + BindNativeProperty(*object, "preferencesDir", GetPreferencesDir); + BindNativeProperty(*object, "bundleCodeDir", GetBundleCodeDir); + return engine.CreateUndefined(); +} + +NativeValue* JsApplicationContextUtils::GetArea(NativeEngine* engine, NativeCallbackInfo* info) +{ + HILOG_INFO("JsApplicationContextUtils::GetArea is called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetArea(*engine, *info) : nullptr; +} + +NativeValue* JsApplicationContextUtils::OnGetArea(NativeEngine& engine, NativeCallbackInfo& info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + int area = applicationContext->GetArea(); + return engine.CreateNumber(area); +} + +NativeValue *JsApplicationContextUtils::GetCacheDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + HILOG_INFO("JsApplicationContextUtils::GetCacheDir is called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetCacheDir(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnGetCacheDir(NativeEngine &engine, NativeCallbackInfo &info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + std::string path = applicationContext->GetCacheDir(); + return engine.CreateString(path.c_str(), path.length()); +} + +NativeValue *JsApplicationContextUtils::GetTempDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + HILOG_INFO("JsApplicationContextUtils::GetTempDir is called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetTempDir(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnGetTempDir(NativeEngine &engine, NativeCallbackInfo &info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + std::string path = applicationContext->GetTempDir(); + return engine.CreateString(path.c_str(), path.length()); +} + +NativeValue *JsApplicationContextUtils::GetFilesDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + HILOG_INFO("JsApplicationContextUtils::GetFilesDir is called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetFilesDir(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnGetFilesDir(NativeEngine &engine, NativeCallbackInfo &info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + std::string path = applicationContext->GetFilesDir(); + return engine.CreateString(path.c_str(), path.length()); +} + +NativeValue *JsApplicationContextUtils::GetDistributedFilesDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + HILOG_INFO("JsApplicationContextUtils::GetDistributedFilesDir is called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetDistributedFilesDir(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnGetDistributedFilesDir(NativeEngine &engine, NativeCallbackInfo &info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + std::string path = applicationContext->GetDistributedFilesDir(); + return engine.CreateString(path.c_str(), path.length()); +} + +NativeValue *JsApplicationContextUtils::GetDatabaseDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + HILOG_INFO("JsApplicationContextUtils::GetDatabaseDir is called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetDatabaseDir(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnGetDatabaseDir(NativeEngine &engine, NativeCallbackInfo &info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + std::string path = applicationContext->GetDatabaseDir(); + return engine.CreateString(path.c_str(), path.length()); +} + +NativeValue *JsApplicationContextUtils::GetPreferencesDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + HILOG_INFO("JsApplicationContextUtils::GetPreferencesDir is called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetPreferencesDir(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnGetPreferencesDir(NativeEngine &engine, NativeCallbackInfo &info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + std::string path = applicationContext->GetPreferencesDir(); + return engine.CreateString(path.c_str(), path.length()); +} + +NativeValue *JsApplicationContextUtils::GetBundleCodeDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + HILOG_INFO("JsApplicationContextUtils::GetBundleCodeDir is called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetBundleCodeDir(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnGetBundleCodeDir(NativeEngine &engine, NativeCallbackInfo &info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext is already released"); + return engine.CreateUndefined(); + } + std::string path = applicationContext->GetBundleCodeDir(); + return engine.CreateString(path.c_str(), path.length()); +} + void JsApplicationContextUtils::Finalizer(NativeEngine *engine, void *data, void *hint) { HILOG_INFO("JsApplicationContextUtils::Finalizer is called"); @@ -87,20 +344,17 @@ NativeValue *JsApplicationContextUtils::OnRegisterAbilityLifecycleCallback( HILOG_ERROR("Not enough params."); return engine.CreateUndefined(); } - std::shared_ptr callback = std::make_shared(engine); - int64_t callbackId = serialNumber_; - if (serialNumber_ < INT64_MAX) { - serialNumber_++; - } else { - HILOG_ERROR("callback id exceed maximum"); - return engine.CreateNumber(MAX_ERROR_CODE); - } - callback->SetJsCallback(info.argv[INDEX_ZERO]); + if (keepApplicationContext_ == nullptr) { HILOG_ERROR("ApplicationContext is nullptr."); return engine.CreateUndefined(); } - keepApplicationContext_->RegisterAbilityLifecycleCallback(callbackId, callback); + if (callback_ != nullptr) { + return engine.CreateNumber(callback_->Register(info.argv[0])); + } + callback_ = std::make_shared(&engine); + int callbackId = callback_->Register(info.argv[INDEX_ZERO]); + keepApplicationContext_->RegisterAbilityLifecycleCallback(callback_); HILOG_INFO("OnRegisterAbilityLifecycleCallback is end"); return engine.CreateNumber(callbackId); } @@ -109,24 +363,40 @@ NativeValue *JsApplicationContextUtils::OnUnregisterAbilityLifecycleCallback( NativeEngine &engine, NativeCallbackInfo &info) { HILOG_INFO("OnUnregisterAbilityLifecycleCallback is called"); + int32_t errCode = 0; + if (keepApplicationContext_ == nullptr) { + HILOG_ERROR("ApplicationContext is nullptr."); + errCode = ERROR_CODE_ONE; + } if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) { HILOG_ERROR("Not enough params"); - return engine.CreateUndefined(); + errCode = ERROR_CODE_ONE; } int64_t callbackId = -1; - napi_get_value_int64( - reinterpret_cast(&engine), reinterpret_cast(info.argv[INDEX_ZERO]), &callbackId); - AsyncTask::CompleteCallback complete = [applicationContext = keepApplicationContext_, callbackId]( - NativeEngine &engine, AsyncTask &task, int32_t status) { - HILOG_INFO("OnUnregisterAbilityLifecycleCallback begin"); - if (applicationContext == nullptr) { - HILOG_ERROR("applicationContext is nullptr"); - task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "applicationContext is nullptr")); - return; - } - applicationContext->UnregisterAbilityLifecycleCallback(callbackId); - task.Resolve(engine, engine.CreateUndefined()); - }; + if (!errCode && !ConvertFromJsValue(engine, info.argv[0], callbackId)) { + HILOG_ERROR("Parse callbackId failed"); + errCode = ERROR_CODE_ONE; + } + HILOG_INFO("callbackId is %{public}d.", (int32_t)callbackId); + AsyncTask::CompleteCallback complete = + [&applicationContext = keepApplicationContext_, &callback = callback_, callbackId, errCode]( + NativeEngine &engine, AsyncTask &task, int32_t status) { + HILOG_INFO("OnUnregisterAbilityLifecycleCallback begin"); + if (errCode != 0) { + task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params.")); + return; + } + if (applicationContext == nullptr) { + HILOG_ERROR("applicationContext is nullptr"); + task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "applicationContext is nullptr")); + return; + } + callback->UnRegister(callbackId); + if (callback->IsEmpty()) { + applicationContext->UnregisterAbilityLifecycleCallback(callback); + } + task.Resolve(engine, engine.CreateUndefined()); + }; NativeValue *lastParam = (info.argc == ARGC_ONE) ? nullptr : info.argv[INDEX_ONE]; NativeValue *result = nullptr; AsyncTask::Schedule(engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); @@ -148,12 +418,35 @@ NativeValue *CreateJsApplicationContext( SetNamedNativePointer(engine, *object, APPLICATION_CONTEXT_NAME, jsApplicationContextUtils.release(), JsApplicationContextUtils::Finalizer); + auto appInfo = applicationContext->GetApplicationInfo(); + if (appInfo != nullptr) { + object->SetProperty("applicationInfo", CreateJsApplicationInfo(engine, *appInfo)); + } + auto hapModuleInfo = applicationContext->GetHapModuleInfo(); + if (hapModuleInfo != nullptr) { + object->SetProperty("currentHapModuleInfo", CreateJsHapModuleInfo(engine, *hapModuleInfo)); + } + auto resourceManager = applicationContext->GetResourceManager(); + if (resourceManager != nullptr) { + object->SetProperty("resourceManager", CreateJsResourceManager(engine, resourceManager)); + } + + BindNativeProperty(*object, "cacheDir", JsApplicationContextUtils::GetCacheDir); + BindNativeProperty(*object, "tempDir", JsApplicationContextUtils::GetTempDir); + BindNativeProperty(*object, "filesDir", JsApplicationContextUtils::GetFilesDir); + BindNativeProperty(*object, "distributedFilesDir", JsApplicationContextUtils::GetDistributedFilesDir); + BindNativeProperty(*object, "databaseDir", JsApplicationContextUtils::GetDatabaseDir); + BindNativeProperty(*object, "preferencesDir", JsApplicationContextUtils::GetPreferencesDir); + BindNativeProperty(*object, "bundleCodeDir", JsApplicationContextUtils::GetBundleCodeDir); BindNativeFunction(engine, *object, "registerAbilityLifecycleCallback", JsApplicationContextUtils::RegisterAbilityLifecycleCallback); BindNativeFunction(engine, *object, "unregisterAbilityLifecycleCallback", JsApplicationContextUtils::UnregisterAbilityLifecycleCallback); + BindNativeFunction(engine, *object, "createBundleContext", JsApplicationContextUtils::CreateBundleContext); + BindNativeFunction(engine, *object, "switchArea", JsApplicationContextUtils::SwitchArea); + BindNativeFunction(engine, *object, "getArea", JsApplicationContextUtils::GetArea); HILOG_INFO("CreateJsApplicationContext end"); return objValue; } } // namespace AbilityRuntime -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/kits/appkit/native/ability_runtime/context/js_application_context_utils.h b/frameworks/kits/appkit/native/ability_runtime/context/js_application_context_utils.h index 2fd6d7400a0b0d339afc6cb49d8be13b9688a453..1864731960c1085d67c27d1a3a53b4bc732fb69b 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/js_application_context_utils.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/js_application_context_utils.h @@ -31,7 +31,6 @@ namespace OHOS { namespace AbilityRuntime { NativeValue *CreateJsApplicationContext( NativeEngine &engine, std::shared_ptr applicationContext, bool keepApplicationContext = false); -static int64_t serialNumber_ = 0; } // namespace AbilityRuntime } // namespace OHOS #endif // ABILITY_RUNTIME_JS_APPLICATION_CONTEXT_UTILS_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 index 2021241e36d1ae08fc27a078486bfa44411059f3..19d66dbbef37df241ee7a33cc69eac8295d4eeda 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/js_context_utils.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/js_context_utils.cpp @@ -313,7 +313,7 @@ NativeValue* JsBaseContext::OnGetApplicationContext(NativeEngine& engine, Native return engine.CreateUndefined(); } - auto applicatioContext = Context::GetJsApplicationContext(); + auto applicatioContext = Context::GetApplicationContext(); if (applicatioContext == nullptr) { HILOG_WARN("applicatioContext is nullptr"); return engine.CreateUndefined(); diff --git a/frameworks/kits/appkit/native/app/include/application_context.h b/frameworks/kits/appkit/native/app/include/app_context.h similarity index 95% rename from frameworks/kits/appkit/native/app/include/application_context.h rename to frameworks/kits/appkit/native/app/include/app_context.h index ad2281b6ce0d79f30f325ee7222e82d7483da56d..d317e2fcebb48bae56d83dd8724b5356861b7566 100644 --- a/frameworks/kits/appkit/native/app/include/application_context.h +++ b/frameworks/kits/appkit/native/app/include/app_context.h @@ -13,17 +13,17 @@ * limitations under the License. */ -#ifndef FOUNDATION_APPEXECFWK_OHOS_APPLICATION_CONTEXT_H -#define FOUNDATION_APPEXECFWK_OHOS_APPLICATION_CONTEXT_H +#ifndef FOUNDATION_APPEXECFWK_OHOS_APP_CONTEXT_H +#define FOUNDATION_APPEXECFWK_OHOS_APP_CONTEXT_H #include "context_container.h" namespace OHOS { namespace AppExecFwk { -class ApplicationContext : public ContextContainer, public std::enable_shared_from_this { +class AppContext : public ContextContainer, public std::enable_shared_from_this { public: - ApplicationContext(); - virtual ~ApplicationContext(); + AppContext(); + virtual ~AppContext(); /** * @brief Obtains information about the current ability. @@ -160,4 +160,4 @@ protected: }; } // namespace AppExecFwk } // namespace OHOS -#endif // FOUNDATION_APPEXECFWK_OHOS_APPLICATION_CONTEXT_H +#endif // FOUNDATION_APPEXECFWK_OHOS_APP_CONTEXT_H diff --git a/frameworks/kits/appkit/native/app/include/ohos_application.h b/frameworks/kits/appkit/native/app/include/ohos_application.h index 32de38241eecd834f1488c8f2bfb494873d60f22..3367a34632169c95db4cd867e03dfd3b23507b47 100644 --- a/frameworks/kits/appkit/native/app/include/ohos_application.h +++ b/frameworks/kits/appkit/native/app/include/ohos_application.h @@ -23,7 +23,7 @@ #include "ability_lifecycle_callbacks.h" #include "ability_runtime/context/context.h" #include "ability_stage.h" -#include "app/include/application_context.h" +#include "app_context.h" #include "element_callback.h" namespace OHOS { @@ -35,7 +35,7 @@ class ElementsCallback; class ApplicationImpl; class Configuration; class AbilityRecordMgr; -class OHOSApplication : public ApplicationContext, +class OHOSApplication : public AppContext, public AbilityLifecycleCallbacks, public std::enable_shared_from_this { public: diff --git a/frameworks/kits/appkit/native/app/src/application_context.cpp b/frameworks/kits/appkit/native/app/src/app_context.cpp similarity index 86% rename from frameworks/kits/appkit/native/app/src/application_context.cpp rename to frameworks/kits/appkit/native/app/src/app_context.cpp index 74ac5dcc50144a442d985b516bb01c6f416547fe..18327984324eb84517732faf3cc9e3a73860ab0f 100644 --- a/frameworks/kits/appkit/native/app/src/application_context.cpp +++ b/frameworks/kits/appkit/native/app/src/app_context.cpp @@ -13,14 +13,14 @@ * limitations under the License. */ -#include "app/include/application_context.h" +#include "app_context.h" #include "hilog_wrapper.h" namespace OHOS { namespace AppExecFwk { -ApplicationContext::ApplicationContext() +AppContext::AppContext() {} -ApplicationContext::~ApplicationContext() +AppContext::~AppContext() {} /** @@ -29,7 +29,7 @@ ApplicationContext::~ApplicationContext() * * @return Returns the AbilityInfo object for the current ability. */ -const std::shared_ptr ApplicationContext::GetAbilityInfo() +const std::shared_ptr AppContext::GetAbilityInfo() { return nullptr; } @@ -49,7 +49,7 @@ const std::shared_ptr ApplicationContext::GetAbilityInfo() * * @return errCode ERR_OK on success, others on failure. */ -ErrCode ApplicationContext::StartAbility(const AAFwk::Want &want, int requestCode) +ErrCode AppContext::StartAbility(const AAFwk::Want &want, int requestCode) { return ERR_INVALID_VALUE; } @@ -64,7 +64,7 @@ ErrCode ApplicationContext::StartAbility(const AAFwk::Want &want, int requestCod * * @return errCode ERR_OK on success, others on failure. */ -ErrCode ApplicationContext::StartAbility(const Want &want, int requestCode, +ErrCode AppContext::StartAbility(const Want &want, int requestCode, const AbilityStartSetting &abilityStartSetting) { return ERR_INVALID_VALUE; @@ -78,7 +78,7 @@ ErrCode ApplicationContext::StartAbility(const Want &want, int requestCode, * * @return errCode ERR_OK on success, others on failure. */ -ErrCode ApplicationContext::TerminateAbility(int requestCode) +ErrCode AppContext::TerminateAbility(int requestCode) { return ERR_INVALID_VALUE; } @@ -88,7 +88,7 @@ ErrCode ApplicationContext::TerminateAbility(int requestCode) * * @return errCode ERR_OK on success, others on failure. */ -ErrCode ApplicationContext::TerminateAbility() +ErrCode AppContext::TerminateAbility() { return ERR_INVALID_VALUE; } @@ -108,7 +108,7 @@ ErrCode ApplicationContext::TerminateAbility() * @return Returns {@code true} if the {@code startId} matches the number of startup times * and this Service ability will be destroyed; returns {@code false} otherwise. */ -bool ApplicationContext::TerminateAbilityResult(int startId) +bool AppContext::TerminateAbilityResult(int startId) { return false; } @@ -121,7 +121,7 @@ bool ApplicationContext::TerminateAbilityResult(int startId) * * @return Returns the bundle name of the calling ability; returns null if no calling ability is available. */ -std::string ApplicationContext::GetCallingBundle() +std::string AppContext::GetCallingBundle() { return ""; } @@ -135,7 +135,7 @@ std::string ApplicationContext::GetCallingBundle() * * @return True means success and false means failure */ -bool ApplicationContext::ConnectAbility(const Want &want, const sptr &conn) +bool AppContext::ConnectAbility(const Want &want, const sptr &conn) { return false; } @@ -148,7 +148,7 @@ bool ApplicationContext::ConnectAbility(const Want &want, const sptr &conn) +ErrCode AppContext::DisconnectAbility(const sptr &conn) { return ERR_INVALID_VALUE; } @@ -163,12 +163,12 @@ ErrCode ApplicationContext::DisconnectAbility(const sptr ApplicationContext::GetToken() +sptr AppContext::GetToken() { return nullptr; } @@ -178,7 +178,7 @@ sptr ApplicationContext::GetToken() * * @param wants Indicates the Want containing information array about the target ability to start. */ -void ApplicationContext::StartAbilities(const std::vector &wants) +void AppContext::StartAbilities(const std::vector &wants) {} /** @@ -186,7 +186,7 @@ void ApplicationContext::StartAbilities(const std::vector &wants) * * @return Returns the unique mission ID. */ -int ApplicationContext::GetMissionId() +int AppContext::GetMissionId() { return -1; } diff --git a/frameworks/kits/appkit/native/app/src/context_container.cpp b/frameworks/kits/appkit/native/app/src/context_container.cpp index eeecaae7e51dec3fc4bf90615295f437ee8da658..f3ce256f47800ba8fc42d98ed6b66370738efcde 100644 --- a/frameworks/kits/appkit/native/app/src/context_container.cpp +++ b/frameworks/kits/appkit/native/app/src/context_container.cpp @@ -19,7 +19,7 @@ #include "ability_constants.h" #include "ability_manager_client.h" #include "ability_manager_errors.h" -#include "app/include/application_context.h" +#include "app_context.h" #include "bundle_constants.h" #include "hilog_wrapper.h" @@ -584,7 +584,7 @@ std::shared_ptr ContextContainer::CreateBundleContext(std::string bundl return nullptr; } - std::shared_ptr appContext = std::make_shared(); + std::shared_ptr appContext = std::make_shared(); if (appContext == nullptr) { HILOG_ERROR("ContextContainer::CreateBundleContext appContext is nullptr"); return nullptr; diff --git a/frameworks/kits/appkit/native/app/src/context_deal.cpp b/frameworks/kits/appkit/native/app/src/context_deal.cpp index 967062878914ec3c70dfc5376aaaed304a6a5660..23519c6e3308e51d878d1407d58e09c6e98ddeb1 100644 --- a/frameworks/kits/appkit/native/app/src/context_deal.cpp +++ b/frameworks/kits/appkit/native/app/src/context_deal.cpp @@ -20,7 +20,7 @@ #include "ability_constants.h" #include "ability_manager_client.h" #include "ability_manager_interface.h" -#include "app/include/application_context.h" +#include "app_context.h" #include "directory_ex.h" #include "file_ex.h" #include "hilog_wrapper.h" diff --git a/frameworks/kits/appkit/native/app/src/main_thread.cpp b/frameworks/kits/appkit/native/app/src/main_thread.cpp index ba8aed583836efd537bb643f6ef5e8bcc2411fa1..edb16ffa0dfa6a289c8be04e64f3d6c775cfc591 100644 --- a/frameworks/kits/appkit/native/app/src/main_thread.cpp +++ b/frameworks/kits/appkit/native/app/src/main_thread.cpp @@ -852,7 +852,6 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con std::shared_ptr contextImpl = std::make_shared(); contextImpl->SetResourceManager(resourceManager); contextImpl->SetApplicationInfo(std::make_shared(appInfo)); - contextImpl->InitAppContext(); std::shared_ptr applicationContext = std::make_shared(); applicationContext->AttachContextImpl(contextImpl); diff --git a/frameworks/kits/appkit/native/test/BUILD.gn b/frameworks/kits/appkit/native/test/BUILD.gn index 4eb26d24c21459dc9eb582f9b0049dd1ac67a897..437733e7f4f6a2a7cee4b49a470c703b484d517b 100644 --- a/frameworks/kits/appkit/native/test/BUILD.gn +++ b/frameworks/kits/appkit/native/test/BUILD.gn @@ -102,8 +102,8 @@ ohos_unittest("context_deal_interface_test") { ohos_unittest("application_test") { module_out_path = module_output_path sources = [ + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "../app/src/ohos_application.cpp", "unittest/ability_stage_test.cpp", "unittest/application_test.cpp", @@ -208,8 +208,8 @@ ohos_unittest("context_deal_test") { ohos_unittest("application_impl_test") { module_out_path = module_output_path sources = [ + "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_context.cpp", "${aafwk_path}/frameworks/kits/appkit/native/app/src/app_loader.cpp", - "${aafwk_path}/frameworks/kits/appkit/native/app/src/application_context.cpp", "../app/src/application_impl.cpp", "../app/src/ohos_application.cpp", "unittest/application_impl_test.cpp", diff --git a/frameworks/kits/wantagent/src/pending_want.cpp b/frameworks/kits/wantagent/src/pending_want.cpp index 789a2a5aae9575c3c047f3d24f6c28896b3ae41b..cb18af490e1698de613e4b0d776534da7d50adfe 100644 --- a/frameworks/kits/wantagent/src/pending_want.cpp +++ b/frameworks/kits/wantagent/src/pending_want.cpp @@ -40,14 +40,14 @@ WantAgentConstant::OperationType PendingWant::GetType(const sptr PendingWant::GetAbility( - const std::shared_ptr &context, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags) { return GetAbility(context, requestCode, want, flags, nullptr); } std::shared_ptr PendingWant::GetAbility( - const std::shared_ptr &context, int requestCode, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags, const std::shared_ptr &options) { WANT_AGENT_LOGI("PendingWant::GetAbility begin."); @@ -76,15 +76,16 @@ std::shared_ptr PendingWant::GetAbility( return std::make_shared(target); } -std::shared_ptr PendingWant::GetAbilities(const std::shared_ptr &context, - int requestCode, std::vector> &wants, unsigned int flags) +std::shared_ptr PendingWant::GetAbilities( + const std::shared_ptr &context, int requestCode, + std::vector> &wants, unsigned int flags) { return GetAbilities(context, requestCode, wants, flags, nullptr); } -std::shared_ptr PendingWant::GetAbilities(const std::shared_ptr &context, - int requestCode, std::vector> &wants, - unsigned int flags, const std::shared_ptr &options) +std::shared_ptr PendingWant::GetAbilities( + const std::shared_ptr &context, int requestCode, + std::vector> &wants, unsigned int flags, const std::shared_ptr &options) { if (context == nullptr) { WANT_AGENT_LOGE("PendingWant::GetAbilities invalid input param."); @@ -114,14 +115,14 @@ std::shared_ptr PendingWant::GetAbilities(const std::shared_ptr PendingWant::GetCommonEvent( - const std::shared_ptr &context, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags) { return GetCommonEventAsUser(context, requestCode, want, flags, 0); } std::shared_ptr PendingWant::GetCommonEventAsUser( - const std::shared_ptr &context, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags, int uid) { if (context == nullptr) { @@ -148,7 +149,7 @@ std::shared_ptr PendingWant::GetCommonEventAsUser( } std::shared_ptr PendingWant::GetService( - const std::shared_ptr &context, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags) { return BuildServicePendingWant(context, requestCode, want, flags, @@ -156,7 +157,7 @@ std::shared_ptr PendingWant::GetService( } std::shared_ptr PendingWant::GetForegroundService( - const std::shared_ptr &context, int requestCode, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags) { return BuildServicePendingWant( @@ -164,7 +165,7 @@ std::shared_ptr PendingWant::GetForegroundService( } std::shared_ptr PendingWant::BuildServicePendingWant( - const std::shared_ptr &context, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags, WantAgentConstant::OperationType serviceKind) { diff --git a/frameworks/kits/wantagent/src/want_agent_helper.cpp b/frameworks/kits/wantagent/src/want_agent_helper.cpp index 36f3e4f4a6763afdd04803e96fa8f670c276b4d9..c77c693b28f1af02f3c9befeec57a407ae8ce9a3 100644 --- a/frameworks/kits/wantagent/src/want_agent_helper.cpp +++ b/frameworks/kits/wantagent/src/want_agent_helper.cpp @@ -64,7 +64,7 @@ unsigned int WantAgentHelper::FlagsTransformer(const std::vector WantAgentHelper::GetWantAgent( - const std::shared_ptr &context, const WantAgentInfo ¶msInfo) + const std::shared_ptr &context, const WantAgentInfo ¶msInfo) { WANT_AGENT_LOGI("WantAgentHelper::GetWantAgent begin."); if (context == nullptr) { diff --git a/frameworks/kits/wantagent/test/unittest/pending_want_test/pending_want_test.cpp b/frameworks/kits/wantagent/test/unittest/pending_want_test/pending_want_test.cpp index edcbf611b09217f1226ed3eb2bbc64cf39d8c58d..dc6729ff774dfaeebe694d8cc7898c06466ccaa5 100644 --- a/frameworks/kits/wantagent/test/unittest/pending_want_test/pending_want_test.cpp +++ b/frameworks/kits/wantagent/test/unittest/pending_want_test/pending_want_test.cpp @@ -18,7 +18,7 @@ #include "cancel_listener.h" #include "completed_callback.h" #include "completed_dispatcher.h" -#include "context/context.h" +#include "context/application_context.h" #include "context_container.h" #include "context_impl.h" #include "element_name.h" @@ -94,9 +94,10 @@ Want PendingWantTest::MakeWant(std::string deviceId, std::string abilityName, st return want; } -std::shared_ptr GetAppContext() +std::shared_ptr GetAppContext() { - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); return context; } @@ -434,7 +435,8 @@ HWTEST_F(PendingWantTest, PendingWant_1900, Function | MediumTest | Level1) unsigned int flags = 1; flags |= FLAG_ONE_SHOT; WantAgentConstant::OperationType type = WantAgentConstant::OperationType::START_FOREGROUND_SERVICE; - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr pendingWant = PendingWant::BuildServicePendingWant(context, requestCode, want, flags, type); diff --git a/frameworks/kits/wantagent/test/unittest/want_agent_helper_test/want_agent_helper_test.cpp b/frameworks/kits/wantagent/test/unittest/want_agent_helper_test/want_agent_helper_test.cpp index c0c49f53224165da774181d02a1c63c35daab721..c828ea7aecd2c68cd0a9b2bf2835c9c89666aa0b 100644 --- a/frameworks/kits/wantagent/test/unittest/want_agent_helper_test/want_agent_helper_test.cpp +++ b/frameworks/kits/wantagent/test/unittest/want_agent_helper_test/want_agent_helper_test.cpp @@ -18,7 +18,7 @@ #include "completed_callback.h" #include "context_container.h" #include "context_impl.h" -#include "context/context.h" +#include "context/application_context.h" #include "element_name.h" #include "event_handler.h" #include "base_types.h" @@ -204,7 +204,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_0900, Function | MediumTest | Leve HWTEST_F(WantAgentHelperTest, WantAgentHelper_1000, Function | MediumTest | Level1) { std::shared_ptr wantAgentHelper = std::make_shared(); - std::shared_ptr context = OHOS::AbilityRuntime::Context::GetApplicationContext(); + std::shared_ptr context = + OHOS::AbilityRuntime::Context::GetApplicationContext(); WantAgentInfo wantAgentInfo; std::shared_ptr want = std::make_shared(); ElementName element("device", "bundleName", "abilityName"); @@ -225,7 +226,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_1000, Function | MediumTest | Leve HWTEST_F(WantAgentHelperTest, WantAgentHelper_1100, Function | MediumTest | Level1) { std::shared_ptr wantAgentHelper = std::make_shared(); - std::shared_ptr context = OHOS::AbilityRuntime::Context::GetApplicationContext(); + std::shared_ptr context = + OHOS::AbilityRuntime::Context::GetApplicationContext(); WantAgentInfo wantAgentInfo; std::shared_ptr want = std::make_shared(); ElementName element("device", "bundleName", "abilityName"); @@ -247,7 +249,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_1100, Function | MediumTest | Leve HWTEST_F(WantAgentHelperTest, WantAgentHelper_1200, Function | MediumTest | Level1) { std::shared_ptr wantAgentHelper = std::make_shared(); - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr want = std::make_shared(); ElementName element("device", "bundleName", "abilityName"); want->SetElement(element); @@ -271,7 +274,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_1200, Function | MediumTest | Leve HWTEST_F(WantAgentHelperTest, WantAgentHelper_1300, Function | MediumTest | Level1) { std::shared_ptr wantAgentHelper = std::make_shared(); - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr want = std::make_shared(); ElementName element("device", "bundleName", "abilityName"); want->SetElement(element); @@ -295,7 +299,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_1300, Function | MediumTest | Leve HWTEST_F(WantAgentHelperTest, WantAgentHelper_1400, Function | MediumTest | Level1) { std::shared_ptr wantAgentHelper = std::make_shared(); - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr want = std::make_shared(); ElementName element("device", "bundleName", "abilityName"); want->SetElement(element); @@ -319,7 +324,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_1400, Function | MediumTest | Leve HWTEST_F(WantAgentHelperTest, WantAgentHelper_1500, Function | MediumTest | Level1) { std::shared_ptr wantAgentHelper = std::make_shared(); - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr want = std::make_shared(); ElementName element("device", "bundleName", "abilityName"); want->SetElement(element); @@ -343,7 +349,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_1500, Function | MediumTest | Leve HWTEST_F(WantAgentHelperTest, WantAgentHelper_1600, Function | MediumTest | Level1) { std::shared_ptr wantAgentHelper = std::make_shared(); - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr want = std::make_shared(); ElementName element("device", "bundleName", "abilityName"); want->SetElement(element); @@ -367,7 +374,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_1600, Function | MediumTest | Leve HWTEST_F(WantAgentHelperTest, WantAgentHelper_1700, Function | MediumTest | Level1) { std::shared_ptr wantAgentHelper = std::make_shared(); - std::shared_ptr context = OHOS::AbilityRuntime::Context::GetApplicationContext(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr want = std::make_shared(); ElementName element("device", "bundleName", "abilityName"); want->SetElement(element); @@ -566,7 +574,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_2700, Function | MediumTest | Leve unsigned int flags = 1; flags |= FLAG_ONE_SHOT; WantAgentConstant::OperationType type = WantAgentConstant::OperationType::START_FOREGROUND_SERVICE; - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr pendingWant = PendingWant::BuildServicePendingWant(context, requestCode, want, flags, type); @@ -600,7 +609,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_2800, Function | MediumTest | Leve unsigned int flags = 1; flags |= FLAG_ONE_SHOT; WantAgentConstant::OperationType type = WantAgentConstant::OperationType::START_FOREGROUND_SERVICE; - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr pendingWant = PendingWant::BuildServicePendingWant(context, requestCode, want, flags, type); @@ -626,7 +636,8 @@ HWTEST_F(WantAgentHelperTest, WantAgentHelper_2900, Function | MediumTest | Leve unsigned int flags = 1; flags |= FLAG_ONE_SHOT; WantAgentConstant::OperationType type = WantAgentConstant::OperationType::START_FOREGROUND_SERVICE; - std::shared_ptr context = std::make_shared(); + std::shared_ptr context = + std::make_shared(); std::shared_ptr pendingWant = PendingWant::BuildServicePendingWant(context, requestCode, want, flags, type); diff --git a/interfaces/innerkits/wantagent/include/pending_want.h b/interfaces/innerkits/wantagent/include/pending_want.h index 9c9d3005841878cb0844955704d19de9220e1602..74b0e0564c5e9baa7bc5bd3bba195370502a4e21 100644 --- a/interfaces/innerkits/wantagent/include/pending_want.h +++ b/interfaces/innerkits/wantagent/include/pending_want.h @@ -21,7 +21,7 @@ #include #include #include "cancel_listener.h" -#include "context/context.h" +#include "context/application_context.h" #include "completed_dispatcher.h" #include "event_handler.h" #include "want.h" @@ -55,8 +55,9 @@ public: * parameters. May return null only if FLAG_NO_CREATE has been * supplied. */ - static std::shared_ptr GetAbility(const std::shared_ptr &context, - int requestCode, const std::shared_ptr &want, unsigned int flags); + static std::shared_ptr GetAbility( + const std::shared_ptr &context, int requestCode, + const std::shared_ptr &want, unsigned int flags); /** * Retrieve a PendingWant that will start a new ability @@ -74,9 +75,10 @@ public: * parameters. May return null only if FLAG_NO_CREATE has been * supplied. */ - static std::shared_ptr GetAbility(const std::shared_ptr &context, - int requestCode, const std::shared_ptr &want, - unsigned int flags, const std::shared_ptr &options); + static std::shared_ptr GetAbility( + const std::shared_ptr &context, int requestCode, + const std::shared_ptr &want, unsigned int flags, + const std::shared_ptr &options); /** * Like GetAbility(Context, int, Want, int)}, but allows an @@ -95,8 +97,9 @@ public: * parameters. May return null only if FLAG_NO_CREATE has been * supplied. */ - static std::shared_ptr GetAbilities(const std::shared_ptr &context, - int requestCode, std::vector> &wants, unsigned int flags); + static std::shared_ptr GetAbilities( + const std::shared_ptr &context, int requestCode, + std::vector> &wants, unsigned int flags); /** * Like GetAbility(Context, int, Want, int)}, but allows an @@ -116,8 +119,9 @@ public: * parameters. May return null only if FLAG_NO_CREATE has been * supplied. */ - static std::shared_ptr GetAbilities(const std::shared_ptr &context, - int requestCode, std::vector> &wants, unsigned int flags, + static std::shared_ptr GetAbilities( + const std::shared_ptr &context, int requestCode, + std::vector> &wants, unsigned int flags, const std::shared_ptr &options); /** @@ -135,15 +139,16 @@ public: * parameters. May return null only if FLAG_NO_CREATE has been * supplied. */ - static std::shared_ptr GetCommonEvent(const std::shared_ptr &context, - int requestCode, const std::shared_ptr &want, unsigned int flags); + static std::shared_ptr GetCommonEvent( + const std::shared_ptr &context, int requestCode, + const std::shared_ptr &want, unsigned int flags); /** * Note that current user will be interpreted at the time the * common event is sent, not when the pending want is created. */ static std::shared_ptr GetCommonEventAsUser( - const std::shared_ptr &context, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags, int uid); /** @@ -162,7 +167,7 @@ public: * supplied. */ static std::shared_ptr GetService( - const std::shared_ptr &context, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags); /** @@ -181,7 +186,7 @@ public: * supplied. */ static std::shared_ptr GetForegroundService( - const std::shared_ptr &context, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags); /** @@ -273,7 +278,7 @@ private: }; static std::shared_ptr BuildServicePendingWant( - const std::shared_ptr &context, + const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags, WantAgentConstant::OperationType serviceKind); }; diff --git a/interfaces/innerkits/wantagent/include/want_agent_helper.h b/interfaces/innerkits/wantagent/include/want_agent_helper.h index 90d93c22f63a8b09e110a9b4e7a5f7526a9f5f32..e40d5a8b6110e3161c10cb387127e33b3807a4e4 100644 --- a/interfaces/innerkits/wantagent/include/want_agent_helper.h +++ b/interfaces/innerkits/wantagent/include/want_agent_helper.h @@ -18,7 +18,7 @@ #include #include -#include "context/context.h" +#include "context/application_context.h" #include "completed_callback.h" #include "completed_dispatcher.h" #include "event_handler.h" @@ -56,7 +56,7 @@ public: * @return Returns the created WantAgent object. */ static std::shared_ptr GetWantAgent( - const std::shared_ptr &context, const WantAgentInfo ¶msInfo); + const std::shared_ptr &context, const WantAgentInfo ¶msInfo); /** * Obtains an WantAgent object. diff --git a/interfaces/kits/napi/aafwk/app/application_context/application_context.js b/interfaces/kits/napi/aafwk/app/application_context/application_context.js index 93a8596bb263bfdecbfeba4b45b1944977f735cb..624fb0885c8320559cac0f0b39260e2c826d3ae1 100644 --- a/interfaces/kits/napi/aafwk/app/application_context/application_context.js +++ b/interfaces/kits/napi/aafwk/app/application_context/application_context.js @@ -12,17 +12,120 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +class EventHub { + constructor() { + this.eventMap = {}; + } + + on(event, callback) { + if ((typeof(event) != 'string') || (typeof(callback) != 'function')) { + return; + } + if (!this.eventMap[event]) { + this.eventMap[event] = []; + } + if (this.eventMap[event].indexOf(callback) == -1) { + this.eventMap[event].push(callback); + } + } + + off(event, callback) { + if (typeof(event) != 'string') { + return; + } + if (this.eventMap[event]) { + if (callback) { + let index = this.eventMap[event].indexOf(callback); + if (index > -1) { + this.eventMap[event].splice(index, 1); + } + } else { + this.eventMap[event].length = 0; + } + } + } + + emit(event, ...args) { + if (typeof(event) != 'string') { + return; + } + if (this.eventMap[event]) { + this.eventMap[event].map((callback) => { + callback(...args); + }); + } + } +} class ApplicationContext { constructor(obj) { this.__context_impl__ = obj + this.__context_impl__.eventHub = new EventHub() } + registerAbilityLifecycleCallback(abilityLifecycleCallback) { return this.__context_impl__.registerAbilityLifecycleCallback(abilityLifecycleCallback) } + unregisterAbilityLifecycleCallback(callbackId, callback) { return this.__context_impl__.unregisterAbilityLifecycleCallback(callbackId, callback) } + + createBundleContext(bundleName) { + return this.__context_impl__.createBundleContext(bundleName) + } + + set area(mode) { + return this.__context_impl__.switchArea(mode) + } + + get area() { + return this.__context_impl__.getArea() + } + + 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 preferencesDir() { + return this.__context_impl__.preferencesDir + } + + get bundleCodeDir() { + return this.__context_impl__.bundleCodeDir + } + + get eventHub() { + return this.__context_impl__.eventHub + } + + get stageMode() { + return true; + } } export default ApplicationContext diff --git a/interfaces/kits/napi/aafwk/wantagent/napi_want_agent.h b/interfaces/kits/napi/aafwk/wantagent/napi_want_agent.h index 8e269c9921aeb50256c5803ce19a1a32e409dbea..81f9a82db2b68fa1f3f091869644b23b63b6f6b1 100644 --- a/interfaces/kits/napi/aafwk/wantagent/napi_want_agent.h +++ b/interfaces/kits/napi/aafwk/wantagent/napi_want_agent.h @@ -23,7 +23,7 @@ #include "ability.h" #include "completed_callback.h" -#include "context/context.h" +#include "context/application_context.h" #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" @@ -60,7 +60,7 @@ struct AsyncGetWantAgentCallbackInfo { int32_t requestCode = -1; std::vector wantAgentFlags; std::shared_ptr extraInfo; - std::shared_ptr context; + std::shared_ptr context; std::shared_ptr wantAgent; };