• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_data_struct_converter.h"
17 
18 #include "common_func.h"
19 #include "configuration_convertor.h"
20 #include "hilog_tag_wrapper.h"
21 #include "js_app_process_state.h"
22 #include "js_runtime.h"
23 #include "js_runtime_utils.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 using namespace OHOS::AppExecFwk;
CreateJsWantObject(napi_env env,const AAFwk::Want & want)28 napi_value CreateJsWantObject(napi_env env, const AAFwk::Want& want)
29 {
30     napi_value object = nullptr;
31     napi_create_object(env, &object);
32     if (object == nullptr) {
33         TAG_LOGE(AAFwkTag::JSRUNTIME, "null object");
34         return nullptr;
35     }
36     napi_set_named_property(env, object, "deviceId", CreateJsValue(env, want.GetOperation().GetDeviceId()));
37     napi_set_named_property(env, object, "bundleName", CreateJsValue(env, want.GetBundle()));
38     napi_set_named_property(env, object, "abilityName", CreateJsValue(env, want.GetOperation().GetAbilityName()));
39     napi_set_named_property(env, object, "uri", CreateJsValue(env, want.GetUriString()));
40     napi_set_named_property(env, object, "type", CreateJsValue(env, want.GetType()));
41     napi_set_named_property(env, object, "flags", CreateJsValue(env, want.GetFlags()));
42     napi_set_named_property(env, object, "action", CreateJsValue(env, want.GetAction()));
43     napi_set_named_property(env, object, "entities", CreateNativeArray(env, want.GetEntities()));
44     return object;
45 }
46 
CreateJsAbilityInfo(napi_env env,const AppExecFwk::AbilityInfo & abilityInfo)47 napi_value CreateJsAbilityInfo(napi_env env, const AppExecFwk::AbilityInfo& abilityInfo)
48 {
49     napi_value object = nullptr;
50     napi_create_object(env, &object);
51     if (object == nullptr) {
52         TAG_LOGE(AAFwkTag::JSRUNTIME, "null object");
53         return nullptr;
54     }
55     AppExecFwk::CommonFunc::ConvertAbilityInfo(env, abilityInfo, object);
56     return object;
57 }
58 
CreateJsApplicationInfo(napi_env env,const AppExecFwk::ApplicationInfo & applicationInfo)59 napi_value CreateJsApplicationInfo(napi_env env, const AppExecFwk::ApplicationInfo &applicationInfo)
60 {
61     napi_value object = nullptr;
62     napi_create_object(env, &object);
63     if (object == nullptr) {
64         TAG_LOGE(AAFwkTag::JSRUNTIME, "null object");
65         return nullptr;
66     }
67     AppExecFwk::CommonFunc::ConvertApplicationInfo(env, object, applicationInfo);
68     return object;
69 }
70 
CreateLastExitDetailInfo(napi_env env,const AAFwk::LastExitDetailInfo & lastExitDetailInfo)71 napi_value CreateLastExitDetailInfo(napi_env env, const AAFwk::LastExitDetailInfo &lastExitDetailInfo)
72 {
73     napi_value object = nullptr;
74     napi_create_object(env, &object);
75     if (object == nullptr) {
76         TAG_LOGE(AAFwkTag::JSRUNTIME, "null object");
77         return nullptr;
78     }
79     napi_set_named_property(env, object, "pid", CreateJsValue(env, lastExitDetailInfo.pid));
80     napi_set_named_property(env, object, "processName", CreateJsValue(env, lastExitDetailInfo.processName));
81     napi_set_named_property(env, object, "uid", CreateJsValue(env, lastExitDetailInfo.uid));
82     napi_set_named_property(env, object, "exitSubReason", CreateJsValue(env, lastExitDetailInfo.exitSubReason));
83     napi_set_named_property(env, object, "exitMsg", CreateJsValue(env, lastExitDetailInfo.exitMsg));
84     napi_set_named_property(env, object, "rss", CreateJsValue(env, lastExitDetailInfo.rss));
85     napi_set_named_property(env, object, "pss", CreateJsValue(env, lastExitDetailInfo.pss));
86     napi_set_named_property(env, object, "processState", CreateJsValue(env,
87         ConvertToJsAppProcessState(static_cast<AppExecFwk::AppProcessState>(lastExitDetailInfo.processState), false)));
88     napi_set_named_property(env, object, "timestamp", CreateJsValue(env, lastExitDetailInfo.timestamp));
89 
90     return object;
91 }
CreateJsLaunchParam(napi_env env,const AAFwk::LaunchParam & launchParam)92 napi_value CreateJsLaunchParam(napi_env env, const AAFwk::LaunchParam& launchParam)
93 {
94     napi_value object = nullptr;
95     napi_create_object(env, &object);
96     if (object == nullptr) {
97         TAG_LOGE(AAFwkTag::JSRUNTIME, "null object");
98         return nullptr;
99     }
100     napi_set_named_property(env, object, "launchReason", CreateJsValue(env, launchParam.launchReason));
101     napi_set_named_property(env, object, "launchReasonMessage", CreateJsValue(env, launchParam.launchReasonMessage));
102     napi_set_named_property(env, object, "lastExitReason", CreateJsValue(env, launchParam.lastExitReason));
103     napi_set_named_property(env, object, "lastExitMessage", CreateJsValue(env, launchParam.lastExitMessage));
104     napi_set_named_property(env, object, "lastExitDetailInfo", CreateLastExitDetailInfo(
105         env, launchParam.lastExitDetailInfo));
106     return object;
107 }
108 
CreateJsConfiguration(napi_env env,const AppExecFwk::Configuration & configuration)109 napi_value CreateJsConfiguration(napi_env env, const AppExecFwk::Configuration& configuration)
110 {
111     napi_value object = nullptr;
112     napi_create_object(env, &object);
113     if (object == nullptr) {
114         TAG_LOGE(AAFwkTag::JSRUNTIME, "null object");
115         return nullptr;
116     }
117 
118     napi_set_named_property(env, object, "language", CreateJsValue(env,
119         configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE)));
120 
121     napi_set_named_property(env, object, "locale", CreateJsLocale(env,
122         configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE)));
123 
124     napi_set_named_property(env, object, "colorMode", CreateJsValue(env,
125         ConvertColorMode(configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE))));
126 
127     napi_set_named_property(env, object, "time24", CreateJsValue(env,
128         ConvertTimeFormat(configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_HOUR))));
129 
130     int32_t displayId = ConvertDisplayId(configuration.GetItem(ConfigurationInner::APPLICATION_DISPLAYID));
131     std::string direction = configuration.GetItem(displayId, ConfigurationInner::APPLICATION_DIRECTION);
132     napi_set_named_property(env, object, "direction", CreateJsValue(env, ConvertDirection(direction)));
133 
134     std::string density = configuration.GetItem(displayId, ConfigurationInner::APPLICATION_DENSITYDPI);
135     napi_set_named_property(env, object, "screenDensity", CreateJsValue(env, ConvertDensity(density)));
136     napi_set_named_property(env, object, "displayId", CreateJsValue(env, displayId));
137 
138     std::string hasPointerDevice = configuration.GetItem(AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
139     napi_set_named_property(env, object, "hasPointerDevice",
140         CreateJsValue(env, hasPointerDevice == "true" ? true : false));
141 
142     std::string fontSizeScale = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE);
143     napi_set_named_property(env, object, "fontSizeScale",
144         CreateJsValue(env, fontSizeScale == "" ? 1.0 : std::stod(fontSizeScale)));
145 
146     napi_set_named_property(env, object, "fontId", CreateJsValue(env,
147         configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_ID)));
148 
149     std::string fontWeightScale = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_WEIGHT_SCALE);
150     napi_set_named_property(env, object, "fontWeightScale",
151         CreateJsValue(env, fontWeightScale == "" ? 1.0 : std::stod(fontWeightScale)));
152 
153     napi_set_named_property(env, object, "mcc", CreateJsValue(env,
154         configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MCC)));
155 
156     napi_set_named_property(env, object, "mnc", CreateJsValue(env,
157         configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MNC)));
158 
159     return object;
160 }
161 
CreateJsExtensionAbilityInfo(napi_env env,const AppExecFwk::ExtensionAbilityInfo & info)162 napi_value CreateJsExtensionAbilityInfo(napi_env env, const AppExecFwk::ExtensionAbilityInfo& info)
163 {
164     TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
165     napi_value object = nullptr;
166     napi_create_object(env, &object);
167     if (object == nullptr) {
168         TAG_LOGE(AAFwkTag::JSRUNTIME, "null object");
169         return nullptr;
170     }
171     AppExecFwk::CommonFunc::ConvertExtensionInfo(env, info, object);
172     return object;
173 }
174 
CreateJsHapModuleInfo(napi_env env,const AppExecFwk::HapModuleInfo & hapModuleInfo)175 napi_value CreateJsHapModuleInfo(napi_env env, const AppExecFwk::HapModuleInfo& hapModuleInfo)
176 {
177     napi_value object = nullptr;
178     napi_create_object(env, &object);
179     if (object == nullptr) {
180         TAG_LOGE(AAFwkTag::JSRUNTIME, "null object");
181         return nullptr;
182     }
183     AppExecFwk::CommonFunc::ConvertHapModuleInfo(env, hapModuleInfo, object);
184     return object;
185 }
186 
CreateJsLocale(napi_env env,const std::string & locale)187 napi_value CreateJsLocale(napi_env env, const std::string &locale)
188 {
189     napi_value global = nullptr;
190     napi_status status = napi_get_global(env, &global);
191     if (status != napi_ok || global == nullptr) {
192         TAG_LOGE(AAFwkTag::JSRUNTIME, "Load global failed");
193         return nullptr;
194     }
195 
196     napi_value intl = nullptr;
197     status = napi_get_named_property(env, global, "Intl", &intl);
198     if (status != napi_ok || intl == nullptr) {
199         TAG_LOGE(AAFwkTag::JSRUNTIME, "Load Intl failed");
200         return nullptr;
201     }
202 
203     napi_value localeConstructor = nullptr;
204     status = napi_get_named_property(env, intl, "Locale", &localeConstructor);
205     if (status != napi_ok || localeConstructor == nullptr) {
206         TAG_LOGE(AAFwkTag::JSRUNTIME, "Load Intl.Locale constructor failed");
207         return nullptr;
208     }
209 
210     napi_value localeJS = nullptr;
211     status = napi_create_string_utf8(env, locale.c_str(), NAPI_AUTO_LENGTH, &localeJS);
212     if (status != napi_ok || localeJS == nullptr) {
213         TAG_LOGE(AAFwkTag::JSRUNTIME, "Create string failed");
214         return nullptr;
215     }
216 
217     size_t argc = 1;
218     napi_value argv[1] = { localeJS };
219     napi_value intlLocale = nullptr;
220     status = napi_new_instance(env, localeConstructor, argc, argv, &intlLocale);
221     if (status != napi_ok || intlLocale == nullptr) {
222         TAG_LOGE(AAFwkTag::JSRUNTIME, "Create Intl.Locale instance failed");
223         return nullptr;
224     }
225     return intlLocale;
226 }
227 } // namespace AbilityRuntime
228 } // namespace OHOS
229