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_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(NativeEngine * engine,const std::shared_ptr<NativeReference> & jsContext,const std::shared_ptr<AppExecFwk::Configuration> & config)26 void JsExtensionContext::ConfigurationUpdated(NativeEngine* engine, const std::shared_ptr<NativeReference>& jsContext,
27 const std::shared_ptr<AppExecFwk::Configuration>& config)
28 {
29 if (engine == nullptr || jsContext == nullptr || config == nullptr) {
30 HILOG_ERROR("engine or jsContext or config is nullptr.");
31 return;
32 }
33
34 NativeValue* value = jsContext->Get();
35 NativeObject* object = ConvertNativeValueTo<NativeObject>(value);
36 if (object == nullptr) {
37 HILOG_ERROR("object is nullptr.");
38 return;
39 }
40
41 NativeValue* method = object->GetProperty("onUpdateConfiguration");
42 if (method == nullptr) {
43 HILOG_ERROR("Failed to get onUpdateConfiguration from object");
44 return;
45 }
46
47 HILOG_INFO("JsExtensionContext call onUpdateConfiguration.");
48 NativeValue* argv[] = { CreateJsConfiguration(*engine, *config) };
49 engine->CallFunction(value, method, argv, 1);
50 }
51
CreateJsExtensionContext(NativeEngine & engine,const std::shared_ptr<ExtensionContext> & context,std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo)52 NativeValue* CreateJsExtensionContext(NativeEngine& engine, const std::shared_ptr<ExtensionContext>& context,
53 std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo)
54 {
55 NativeValue* objValue = CreateJsBaseContext(engine, context);
56 if (context == nullptr) {
57 HILOG_ERROR("Failed to CreateJsExtensionContext, context is nullptr.");
58 return objValue;
59 }
60 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
61 if (object == nullptr) {
62 HILOG_ERROR("Failed to CreateJsExtensionContext, object is nullptr.");
63 return objValue;
64 }
65 auto configuration = context->GetConfiguration();
66 if (configuration != nullptr) {
67 object->SetProperty("config", CreateJsConfiguration(engine, *configuration));
68 }
69
70 auto hapModuleInfo = context->GetHapModuleInfo();
71 if (abilityInfo && hapModuleInfo) {
72 auto isExist = [&abilityInfo](const AppExecFwk::ExtensionAbilityInfo& info) {
73 HILOG_DEBUG("%{public}s, %{public}s", info.bundleName.c_str(), info.name.c_str());
74 return info.bundleName == abilityInfo->bundleName && info.name == abilityInfo->name;
75 };
76 auto infoIter = std::find_if(
77 hapModuleInfo->extensionInfos.begin(), hapModuleInfo->extensionInfos.end(), isExist);
78 if (infoIter == hapModuleInfo->extensionInfos.end()) {
79 HILOG_ERROR("Set extensionAbilityInfo fail.");
80 } else {
81 object->SetProperty("extensionAbilityInfo", CreateJsExtensionAbilityInfo(engine, *infoIter));
82 }
83 }
84
85 return objValue;
86 }
87 } // namespace AbilityRuntime
88 } // namespace OHOS
89