diff --git a/bundle.json b/bundle.json index 7d62dbdfe6e06af39af13c650e2997a481ec0fdd..7a34cdad7f9cd7db40b8a70cd1cde3ed97d0e6f7 100644 --- a/bundle.json +++ b/bundle.json @@ -146,6 +146,15 @@ }, "name": "//foundation/aafwk/standard/interfaces/innerkits/runtime:runtime" }, + { + "header": { + "header_base": "//foundation/aafwk/standard/interfaces/innerkits/napi_base_context/include", + "header_files": [ + "napi_base_context.h" + ] + }, + "name": "//foundation/aafwk/standard/interfaces/innerkits/napi_base_context:napi_base_context" + }, { "header": { "header_base": "//foundation/aafwk/standard/interfaces/innerkits/app_manager/include", diff --git a/frameworks/kits/ability/native/BUILD.gn b/frameworks/kits/ability/native/BUILD.gn index c2cb1ac238aeaa7e733497b7c2c5f16a9ade839a..c87cdb31b8b457a8126684ab54a991fe0ef083b9 100644 --- a/frameworks/kits/ability/native/BUILD.gn +++ b/frameworks/kits/ability/native/BUILD.gn @@ -247,6 +247,7 @@ ohos_shared_library("abilitykit_native") { external_deps = [ "ability_runtime:ability_context_native", + "ability_runtime:napi_base_context", "ability_runtime:runtime", "bytrace_standard:bytrace_core", "ces_standard:cesfwk_innerkits", 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 b5d78c05fc1f2e6259b79ed643615378b69646ae..08c265d7c6faae0b5110e5bd47955592c5a85e80 100755 --- a/frameworks/kits/appkit/native/ability_runtime/context/js_context_utils.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/js_context_utils.cpp @@ -196,9 +196,6 @@ NativeValue* CreateJsBaseContext(NativeEngine& engine, std::shared_ptr BindNativeFunction(engine, *object, "getApplicationContext", JsBaseContext::GetApplicationContext); BindNativeFunction(engine, *object, "switchArea", JsBaseContext::SwitchArea); - // Set field 'stageMode' for 'BaseContext', that we are in Stage Mode. - object->SetProperty("stageMode", CreateJsValue(engine, true)); - return objValue; } } // namespace AbilityRuntime diff --git a/interfaces/innerkits/napi_base_context/BUILD.gn b/interfaces/innerkits/napi_base_context/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..05a3d810a6713cf7bcb1df3ce80ea8dcd64e3bde --- /dev/null +++ b/interfaces/innerkits/napi_base_context/BUILD.gn @@ -0,0 +1,32 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +config("napi_context_base_public_config") { + visibility = [ ":*" ] + include_dirs = [ "include" ] +} + +ohos_shared_library("napi_base_context") { + sources = [ "src/napi_base_context.cpp" ] + + configs = [] + + public_configs = [ ":napi_context_base_public_config" ] + + external_deps = [ "napi:ace_napi" ] + + subsystem_name = "aafwk" + part_name = "ability_runtime" +} diff --git a/interfaces/innerkits/napi_base_context/include/napi_base_context.h b/interfaces/innerkits/napi_base_context/include/napi_base_context.h new file mode 100644 index 0000000000000000000000000000000000000000..2207ae2564961cc747d33a9be9492e0042ddf56c --- /dev/null +++ b/interfaces/innerkits/napi_base_context/include/napi_base_context.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ABILITY_RUNTIME_NAPI_BASE_CONTEXT_H +#define ABILITY_RUNTIME_NAPI_BASE_CONTEXT_H + +#include + +#include "napi/native_api.h" + +namespace OHOS { +namespace AppExecFwk { +class Ability; +} +namespace AbilityRuntime { +napi_value* GetFAModeContextClassObject(); + +napi_status IsStageContext(napi_env env, napi_value object, bool& stageMode); + +class Context; +std::shared_ptr GetStageModeContext(napi_env env, napi_value object); + +AppExecFwk::Ability* GetCurrentAbility(napi_env env); +} // namespace AbilityRuntime +} // namespace OHOS +#endif // ABILITY_RUNTIME_NAPI_BASE_CONTEXT_H \ No newline at end of file diff --git a/interfaces/innerkits/napi_base_context/src/napi_base_context.cpp b/interfaces/innerkits/napi_base_context/src/napi_base_context.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e386b3c6eefaf5594ca99c3ed189189d7766d0fe --- /dev/null +++ b/interfaces/innerkits/napi_base_context/src/napi_base_context.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi_base_context.h" + +#define OHOS_CALL_NAPI_RETURN(call) \ + do { \ + napi_status ret = (call); \ + if (ret != napi_ok) { \ + return ret; \ + } \ + } while (0) + +namespace OHOS { +namespace AbilityRuntime { +namespace { +napi_status GetStageModeContextClassObject(napi_env env, napi_value& classObject) +{ + static thread_local napi_ref contextClassObjectRef = {0}; + + napi_status ret = napi_get_reference_value(env, contextClassObjectRef, &classObject); + if (ret == napi_ok) { + return napi_ok; + } + + napi_value global; + OHOS_CALL_NAPI_RETURN(napi_get_global(env, &global)); + + napi_value requireNapi; + OHOS_CALL_NAPI_RETURN(napi_get_named_property(env, global, "requireNapi", &requireNapi)); + + napi_value className; + OHOS_CALL_NAPI_RETURN(napi_create_string_utf8(env, "application.Context", NAPI_AUTO_LENGTH, &className)); + + OHOS_CALL_NAPI_RETURN(napi_call_function(env, global, requireNapi, 1, &className, &classObject)); + + // Ignore return value + napi_create_reference(env, classObject, 1, &contextClassObjectRef); + return napi_ok; +} +} // namespace + +napi_value* GetFAModeContextClassObject() +{ + static thread_local napi_value contextClassObject = {0}; + return &contextClassObject; +} + +napi_status IsStageContext(napi_env env, napi_value object, bool& stageMode) +{ + napi_value boolValue; + OHOS_CALL_NAPI_RETURN(napi_get_named_property(env, object, "stageMode", &boolValue)); + + bool value = false; + OHOS_CALL_NAPI_RETURN(napi_get_value_bool(env, boolValue, &value)); + + napi_value classObject; + if (value) { + OHOS_CALL_NAPI_RETURN(GetStageModeContextClassObject(env, classObject)); + } else { + napi_value* clsObjPtr = GetFAModeContextClassObject(); + if (clsObjPtr == nullptr) { + return napi_generic_failure; + } + classObject = *clsObjPtr; + } + + bool result = false; + OHOS_CALL_NAPI_RETURN(napi_instanceof(env, object, classObject, &result)); + if (!result) { + return napi_generic_failure; + } + + stageMode = value; + return napi_ok; +} + +std::shared_ptr GetStageModeContext(napi_env env, napi_value object) +{ + void* wrapped = nullptr; + napi_status ret = napi_unwrap(env, object, &wrapped); + if (ret != napi_ok) { + return nullptr; + } + + auto weakContext = static_cast*>(wrapped); + return weakContext != nullptr ? weakContext->lock() : nullptr; +} + +AppExecFwk::Ability* GetCurrentAbility(napi_env env) +{ + napi_value global; + napi_status status = napi_get_global(env, &global); + if (status != napi_ok) { + return nullptr; + } + + napi_value abilityObj; + status = napi_get_named_property(env, global, "ability", &abilityObj); + if (status != napi_ok || abilityObj == nullptr) { + return nullptr; + } + + void* pointer = nullptr; + status = napi_get_value_external(env, abilityObj, &pointer); + if (status != napi_ok) { + return nullptr; + } + + return static_cast(pointer); +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/aafwk/app/context/context.js b/interfaces/kits/napi/aafwk/app/context/context.js index bfc7c078b8804088e8328a63dafa0836a600a4e8..a345d2dd4aa750554eb888ea47b1a8c2aeef0820 100755 --- a/interfaces/kits/napi/aafwk/app/context/context.js +++ b/interfaces/kits/napi/aafwk/app/context/context.js @@ -115,6 +115,10 @@ class Context { get eventHub() { return this.__context_impl__.eventHub } + + get stageMode() { + return true; + } } export default Context diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/BUILD.gn b/interfaces/kits/napi/aafwk/inner/napi_common/BUILD.gn index f8272aaf2f4387f342df76b224f3993d0d091796..e99dfe7b7582c202b7f30cbb31134fb4b2bef1e7 100644 --- a/interfaces/kits/napi/aafwk/inner/napi_common/BUILD.gn +++ b/interfaces/kits/napi/aafwk/inner/napi_common/BUILD.gn @@ -41,6 +41,7 @@ ohos_shared_library("napi_common") { external_deps = [ "ability_runtime:ability_manager", "ability_runtime:app_manager", + "ability_runtime:napi_base_context", "ability_runtime:want", "appexecfwk_standard:libeventhandler", "bundle_framework:appexecfwk_base", diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp index 41fa1bc652a6669230d3299471b4fbab827b5c4b..1c4be1a9f06205b55c7ec8cb021153335cb5fa84 100644 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp @@ -20,19 +20,19 @@ #include "hilog_wrapper.h" #include "napi_common_util.h" +#include "napi_base_context.h" #include "napi_remote_object.h" #include "securec.h" namespace OHOS { namespace AppExecFwk { -napi_value thread_local g_classContext; napi_value thread_local g_dataAbilityHelper; using NAPICreateJsRemoteObject = napi_value (*)(napi_env env, const sptr target); napi_value *GetGlobalClassContext(void) { - return &g_classContext; + return AbilityRuntime::GetFAModeContextClassObject(); } napi_value *GetGlobalDataAbilityHelper(void) @@ -1575,7 +1575,7 @@ napi_value GetContextAsync( napi_get_undefined(env, &undefined); result[PARAM0] = GetCallbackErrorValue(env, asyncCallbackInfo->errCode); if (asyncCallbackInfo->errCode == NAPI_ERR_NO_ERROR) { - napi_new_instance(env, g_classContext, 0, nullptr, &result[PARAM1]); + napi_new_instance(env, *GetGlobalClassContext(), 0, nullptr, &result[PARAM1]); } else { result[PARAM1] = WrapUndefinedToJS(env); } @@ -1623,7 +1623,7 @@ napi_value GetContextPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) AsyncCallbackInfo *asyncCallbackInfo = static_cast(data); napi_value result = nullptr; if (asyncCallbackInfo->errCode == NAPI_ERR_NO_ERROR) { - napi_new_instance(env, g_classContext, 0, nullptr, &result); + napi_new_instance(env, *GetGlobalClassContext(), 0, nullptr, &result); napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); } else { result = GetCallbackErrorValue(env, asyncCallbackInfo->errCode); @@ -1666,7 +1666,7 @@ napi_value GetContextWrap(napi_env env, napi_callback_info info, AsyncCallbackIn } napi_value result = nullptr; - napi_new_instance(env, g_classContext, 0, nullptr, &result); + napi_new_instance(env, *GetGlobalClassContext(), 0, nullptr, &result); HILOG_INFO("%{public}s, end.", __func__); return result; } diff --git a/ohos.build b/ohos.build index 738076ff300e89e6da75b884fb461c567c7c96a4..4f0c964970b7222ee9fc28028f442cce12dfc226 100755 --- a/ohos.build +++ b/ohos.build @@ -97,6 +97,15 @@ }, "name": "//foundation/aafwk/standard/interfaces/innerkits/runtime:runtime" }, + { + "header": { + "header_base": "//foundation/aafwk/standard/interfaces/innerkits/napi_base_context/include", + "header_files": [ + "napi_base_context.h" + ] + }, + "name": "//foundation/aafwk/standard/interfaces/innerkits/napi_base_context:napi_base_context" + }, { "header": { "header_base": "//foundation/aafwk/standard/interfaces/innerkits/app_manager/include",