• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "js_extension_common.h"
17 
18 #include "hilog_wrapper.h"
19 #include "js_extension_context.h"
20 #include "js_runtime.h"
21 #include "js_runtime_utils.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "napi_common_configuration.h"
25 #include "napi_remote_object.h"
26 
27 namespace OHOS {
28 namespace AbilityRuntime {
29 namespace {
30 constexpr size_t ARGC_ONE = 1;
31 }
32 
33 using namespace OHOS::AppExecFwk;
34 
Create(JsRuntime & jsRuntime,NativeReference & jsObj,const std::shared_ptr<NativeReference> & shellContextRef)35 std::shared_ptr<JsExtensionCommon> JsExtensionCommon::Create(JsRuntime &jsRuntime, NativeReference &jsObj,
36     const std::shared_ptr<NativeReference> &shellContextRef)
37 {
38     return std::make_shared<JsExtensionCommon>(jsRuntime, jsObj, shellContextRef);
39 }
40 
JsExtensionCommon(JsRuntime & jsRuntime,NativeReference & jsObj,const std::shared_ptr<NativeReference> & shellContextRef)41 JsExtensionCommon::JsExtensionCommon(JsRuntime &jsRuntime, NativeReference &jsObj,
42     const std::shared_ptr<NativeReference> &shellContextRef)
43     : jsRuntime_(jsRuntime), jsObj_(jsObj), shellContextRef_(shellContextRef) {}
44 
~JsExtensionCommon()45 JsExtensionCommon::~JsExtensionCommon()
46 {
47     jsRuntime_.FreeNativeReference(std::move(shellContextRef_));
48 }
49 
OnConfigurationUpdated(const std::shared_ptr<AppExecFwk::Configuration> & fullConfig)50 void JsExtensionCommon::OnConfigurationUpdated(const std::shared_ptr<AppExecFwk::Configuration> &fullConfig)
51 {
52     HILOG_INFO("%{public}s called.", __func__);
53     if (!fullConfig) {
54         HILOG_ERROR("invalid configuration.");
55         return;
56     }
57 
58     HandleScope handleScope(jsRuntime_);
59     auto& nativeEngine = jsRuntime_.GetNativeEngine();
60     JsExtensionContext::ConfigurationUpdated(&nativeEngine, shellContextRef_, fullConfig);
61 
62     napi_value napiConfiguration = OHOS::AppExecFwk::WrapConfiguration(
63         reinterpret_cast<napi_env>(&nativeEngine), *fullConfig);
64     NativeValue* jsConfiguration = reinterpret_cast<NativeValue*>(napiConfiguration);
65     CallObjectMethod("onConfigurationUpdate", &jsConfiguration, ARGC_ONE);
66 }
67 
OnMemoryLevel(int level)68 void JsExtensionCommon::OnMemoryLevel(int level)
69 {
70     HILOG_DEBUG("%{public}s called.", __func__);
71 
72     HandleScope handleScope(jsRuntime_);
73     auto &nativeEngine = jsRuntime_.GetNativeEngine();
74 
75     NativeValue *value = jsObj_.Get();
76     NativeObject *obj = ConvertNativeValueTo<NativeObject>(value);
77     if (obj == nullptr) {
78         HILOG_ERROR("Failed to get js instance object");
79         return;
80     }
81 
82     NativeValue *jslevel = CreateJsValue(nativeEngine, level);
83     NativeValue *argv[] = {
84         jslevel,
85     };
86     CallObjectMethod("onMemoryLevel", argv, ArraySize(argv));
87 }
88 
CallObjectMethod(const char * name,NativeValue * const * argv,size_t argc)89 NativeValue* JsExtensionCommon::CallObjectMethod(const char* name, NativeValue* const* argv, size_t argc)
90 {
91     HILOG_INFO("JsExtensionCommon::CallObjectMethod(%{public}s), begin", name);
92 
93     HandleScope handleScope(jsRuntime_);
94     auto& nativeEngine = jsRuntime_.GetNativeEngine();
95     NativeValue* value = jsObj_.Get();
96     NativeObject* obj = ConvertNativeValueTo<NativeObject>(value);
97     if (obj == nullptr) {
98         HILOG_ERROR("Failed to get js instance object");
99         return nullptr;
100     }
101 
102     NativeValue* method = obj->GetProperty(name);
103     if (method == nullptr || method->TypeOf() != NATIVE_FUNCTION) {
104         HILOG_ERROR("Failed to get '%{public}s' from js object", name);
105         return nullptr;
106     }
107     HILOG_INFO("JsExtensionCommon::CallFunction(%{public}s), success", name);
108     return nativeEngine.CallFunction(value, method, argv, argc);
109 }
110 }
111 }
112