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