Add new part for base context

Signed-off-by: shiyu_huang <huangshiyu1@huawei.com>
Change-Id: Ica02881d9f242ed6391075e54a16136c9d23b7b7
This commit is contained in:
shiyu_huang
2022-01-25 16:14:17 +08:00
parent 9058a02dca
commit f326b0c2bd
10 changed files with 224 additions and 8 deletions
+9
View File
@@ -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",
+1
View File
@@ -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",
@@ -196,9 +196,6 @@ NativeValue* CreateJsBaseContext(NativeEngine& engine, std::shared_ptr<Context>
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
@@ -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"
}
@@ -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 <memory>
#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<Context> GetStageModeContext(napi_env env, napi_value object);
AppExecFwk::Ability* GetCurrentAbility(napi_env env);
} // namespace AbilityRuntime
} // namespace OHOS
#endif // ABILITY_RUNTIME_NAPI_BASE_CONTEXT_H
@@ -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<Context> 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<std::weak_ptr<Context>*>(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<AppExecFwk::Ability*>(pointer);
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -115,6 +115,10 @@ class Context {
get eventHub() {
return this.__context_impl__.eventHub
}
get stageMode() {
return true;
}
}
export default Context
@@ -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",
@@ -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<IRemoteObject> 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<AsyncCallbackInfo *>(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;
}
+9
View File
@@ -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",