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 <cstdint>
19
20 #include "hilog_wrapper.h"
21 #include "js_context_utils.h"
22 #include "js_data_struct_converter.h"
23 #include "js_runtime.h"
24 #include "js_runtime_utils.h"
25
26 namespace OHOS {
27 namespace AbilityRuntime {
ConfigurationUpdated(NativeEngine * engine,std::shared_ptr<NativeReference> & jsContext,const std::shared_ptr<AppExecFwk::Configuration> & config)28 void JsExtensionContext::ConfigurationUpdated(NativeEngine* engine, std::shared_ptr<NativeReference> &jsContext,
29 const std::shared_ptr<AppExecFwk::Configuration> &config)
30 {
31 HILOG_INFO("%{public}s called.", __func__);
32 if ((!jsContext) || (!config)) {
33 HILOG_INFO("jsContext or config is nullptr.");
34 return;
35 }
36
37 NativeValue* value = jsContext->Get();
38 NativeObject* object = ConvertNativeValueTo<NativeObject>(value);
39 if (!object) {
40 HILOG_INFO("object is nullptr.");
41 return;
42 }
43
44 NativeValue* method = object->GetProperty("onUpdateConfiguration");
45 if (!method) {
46 HILOG_ERROR("Failed to get onUpdateConfiguration from object");
47 return;
48 }
49
50 HILOG_INFO("JsExtensionContext call onUpdateConfiguration.");
51 NativeValue* argv[] = {CreateJsConfiguration(*engine, *config)};
52 engine->CallFunction(value, method, argv, 1);
53 }
54
CreateJsExtensionContext(NativeEngine & engine,std::shared_ptr<ExtensionContext> context)55 NativeValue* CreateJsExtensionContext(NativeEngine& engine, std::shared_ptr<ExtensionContext> context)
56 {
57 HILOG_INFO("CreateJsExtensionContext begin");
58 NativeValue* objValue = CreateJsBaseContext(engine, context);
59 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
60
61 auto configuration = context->GetConfiguration();
62 if (configuration) {
63 object->SetProperty("config", CreateJsConfiguration(engine, *configuration));
64 }
65
66 return objValue;
67 }
68 } // namespace AbilityRuntime
69 } // namespace OHOS
70