1 /*
2  * Copyright (c) 2021-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_context.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "js_context_utils.h"
20 #include "js_data_struct_converter.h"
21 #include "js_runtime.h"
22 #include "js_runtime_utils.h"
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
ConfigurationUpdated(napi_env env,const std::shared_ptr<NativeReference> & jsContext,const std::shared_ptr<AppExecFwk::Configuration> & config)26 void JsExtensionContext::ConfigurationUpdated(napi_env env, const std::shared_ptr<NativeReference>& jsContext,
27     const std::shared_ptr<AppExecFwk::Configuration> &config)
28 {
29     if (env == nullptr || jsContext == nullptr || config == nullptr) {
30         TAG_LOGE(AAFwkTag::CONTEXT, "null engine or jsContext or config");
31         return;
32     }
33 
34     napi_value object = jsContext->GetNapiValue();
35     if (!CheckTypeForNapiValue(env, object, napi_object)) {
36         TAG_LOGE(AAFwkTag::CONTEXT, "object is not object");
37         return;
38     }
39 
40     napi_value method = nullptr;
41     napi_get_named_property(env, object, "onUpdateConfiguration", &method);
42     if (method == nullptr) {
43         TAG_LOGE(AAFwkTag::CONTEXT, "failed to get onUpdateConfiguration from object");
44         return;
45     }
46 
47     TAG_LOGD(AAFwkTag::CONTEXT, "call onUpdateConfiguration");
48     napi_value argv[] = { CreateJsConfiguration(env, *config) };
49     napi_call_function(env, object, method, 1, argv, nullptr);
50 }
51 
CreateJsExtensionContext(napi_env env,const std::shared_ptr<ExtensionContext> & context,std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo)52 napi_value CreateJsExtensionContext(napi_env env, const std::shared_ptr<ExtensionContext>& context,
53     std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo)
54 {
55     if (context == nullptr) {
56         TAG_LOGE(AAFwkTag::CONTEXT, "null context");
57         return nullptr;
58     }
59     napi_value object = CreateJsBaseContext(env, context);
60     if (object == nullptr) {
61         TAG_LOGE(AAFwkTag::CONTEXT, "null object");
62         return nullptr;
63     }
64     auto configuration = context->GetConfiguration();
65     if (configuration != nullptr) {
66         napi_set_named_property(env, object, "config", CreateJsConfiguration(env, *configuration));
67     }
68 
69     auto hapModuleInfo = context->GetHapModuleInfo();
70     if (abilityInfo && hapModuleInfo) {
71         auto isExist = [&abilityInfo](const AppExecFwk::ExtensionAbilityInfo& info) {
72             TAG_LOGD(AAFwkTag::CONTEXT, "%{public}s, %{public}s", info.bundleName.c_str(), info.name.c_str());
73             return info.bundleName == abilityInfo->bundleName && info.name == abilityInfo->name;
74         };
75         auto infoIter = std::find_if(
76             hapModuleInfo->extensionInfos.begin(), hapModuleInfo->extensionInfos.end(), isExist);
77         if (infoIter == hapModuleInfo->extensionInfos.end()) {
78             TAG_LOGE(AAFwkTag::CONTEXT, "set extensionAbilityInfo fail");
79         } else {
80             napi_set_named_property(env, object, "extensionAbilityInfo", CreateJsExtensionAbilityInfo(env, *infoIter));
81         }
82     }
83 
84     return object;
85 }
86 } // namespace AbilityRuntime
87 } // namespace OHOS
88