1 /*
2  * Copyright (c) 2023-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_ability_stage_context.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "js_context_utils.h"
20 #include "js_data_converter.h"
21 #include "js_runtime_utils.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
CreateJsAbilityStageContext(napi_env env,const std::shared_ptr<AbilityRuntime::Context> & context)25 napi_value CreateJsAbilityStageContext(napi_env env, const std::shared_ptr<AbilityRuntime::Context> &context)
26 {
27     TAG_LOGD(AAFwkTag::ABILITY_SIM, "called");
28     napi_value objValue = CreateJsBaseContext(env, context);
29     if (context == nullptr) {
30         return objValue;
31     }
32     auto configuration = context->GetConfiguration();
33     if (configuration != nullptr && objValue != nullptr) {
34         napi_set_named_property(env, objValue, "config",
35             CreateJsConfiguration(env, *configuration));
36     }
37     return objValue;
38 }
39 
ConfigurationUpdated(napi_env env,std::shared_ptr<NativeReference> & jsContext,const std::shared_ptr<AppExecFwk::Configuration> & config)40 void JsAbilityStageContext::ConfigurationUpdated(napi_env env, std::shared_ptr<NativeReference> &jsContext,
41     const std::shared_ptr<AppExecFwk::Configuration> &config)
42 {
43     TAG_LOGD(AAFwkTag::ABILITY_SIM, "called");
44     if (!jsContext || !config) {
45         TAG_LOGE(AAFwkTag::ABILITY_SIM, "null jsContext/config");
46         return;
47     }
48 
49     napi_value value = jsContext->GetNapiValue();
50     if (value == nullptr) {
51         TAG_LOGE(AAFwkTag::ABILITY_SIM, "null value");
52         return;
53     }
54 
55     napi_value method = nullptr;
56     napi_get_named_property(env, value, "onUpdateConfiguration", &method);
57     if (!method) {
58         TAG_LOGE(AAFwkTag::ABILITY_SIM, "Failed");
59         return;
60     }
61 
62     napi_value argv[] = { CreateJsConfiguration(env, *config) };
63     napi_value callResult = nullptr;
64     napi_call_function(env, value, method, 1, argv, &callResult);
65 }
66 }  // namespace AbilityRuntime
67 }  // namespace OHOS
68