1 /*
2  * Copyright (c) 2022-2024 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_tag_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     TAG_LOGI(AAFwkTag::EXT, "called");
53     if (!fullConfig) {
54         TAG_LOGE(AAFwkTag::EXT, "invalid config");
55         return;
56     }
57 
58     HandleScope handleScope(jsRuntime_);
59     auto env = jsRuntime_.GetNapiEnv();
60     JsExtensionContext::ConfigurationUpdated(env, shellContextRef_, fullConfig);
61 
62     napi_value napiConfiguration = OHOS::AppExecFwk::WrapConfiguration(env, *fullConfig);
63     CallObjectMethod("onConfigurationUpdate", &napiConfiguration, ARGC_ONE);
64 }
65 
OnMemoryLevel(int level)66 void JsExtensionCommon::OnMemoryLevel(int level)
67 {
68     TAG_LOGD(AAFwkTag::EXT, "called");
69 
70     HandleScope handleScope(jsRuntime_);
71     auto env = jsRuntime_.GetNapiEnv();
72 
73     napi_value obj = jsObj_.GetNapiValue();
74     if (!CheckTypeForNapiValue(env, obj, napi_object)) {
75         TAG_LOGE(AAFwkTag::EXT, "get instance obj failed");
76         return;
77     }
78 
79     napi_value jslevel = CreateJsValue(env, level);
80     napi_value argv[] = {
81         jslevel,
82     };
83     CallObjectMethod("onMemoryLevel", argv, ArraySize(argv));
84 }
85 
CallObjectMethod(const char * name,napi_value const * argv,size_t argc)86 napi_value JsExtensionCommon::CallObjectMethod(const char* name, napi_value const* argv, size_t argc)
87 {
88     TAG_LOGD(AAFwkTag::EXT, "name: %{public}s", name);
89 
90     HandleScope handleScope(jsRuntime_);
91     auto env = jsRuntime_.GetNapiEnv();
92     napi_value obj = jsObj_.GetNapiValue();
93     if (!CheckTypeForNapiValue(env, obj, napi_object)) {
94         TAG_LOGE(AAFwkTag::EXT, "get instance obj failed");
95         return nullptr;
96     }
97 
98     napi_value method = nullptr;
99     napi_get_named_property(env, obj, name, &method);
100     if (!CheckTypeForNapiValue(env, obj, napi_function)) {
101         TAG_LOGE(AAFwkTag::EXT, "get '%{public}s' failed", name);
102         return nullptr;
103     }
104     TAG_LOGD(AAFwkTag::EXT, "(%{public}s), success", name);
105     napi_value result = nullptr;
106     napi_call_function(env, obj, method, argc, argv, &result);
107     return result;
108 }
109 }
110 }
111