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_data_struct_converter.h"
17
18 #include "configuration_convertor.h"
19 #include "js_runtime.h"
20 #include "js_runtime_utils.h"
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 using namespace OHOS::AppExecFwk;
CreateJsWantObject(NativeEngine & engine,const AAFwk::Want & want)25 NativeValue* CreateJsWantObject(NativeEngine& engine, const AAFwk::Want& want)
26 {
27 NativeValue* objValue = engine.CreateObject();
28 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
29
30 object->SetProperty("deviceId", CreateJsValue(engine, want.GetOperation().GetDeviceId()));
31 object->SetProperty("bundleName", CreateJsValue(engine, want.GetBundle()));
32 object->SetProperty("abilityName", CreateJsValue(engine, want.GetOperation().GetAbilityName()));
33 object->SetProperty("uri", CreateJsValue(engine, want.GetUriString()));
34 object->SetProperty("type", CreateJsValue(engine, want.GetType()));
35 object->SetProperty("flags", CreateJsValue(engine, want.GetFlags()));
36 object->SetProperty("action", CreateJsValue(engine, want.GetAction()));
37 object->SetProperty("entities", CreateNativeArray(engine, want.GetEntities()));
38 return objValue;
39 }
40
CreateJsAbilityInfo(NativeEngine & engine,const AppExecFwk::AbilityInfo & abilityInfo)41 NativeValue* CreateJsAbilityInfo(NativeEngine& engine, const AppExecFwk::AbilityInfo& abilityInfo)
42 {
43 NativeValue* objValue = engine.CreateObject();
44 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
45
46 object->SetProperty("bundleName", CreateJsValue(engine, abilityInfo.bundleName));
47 object->SetProperty("name", CreateJsValue(engine, abilityInfo.name));
48 object->SetProperty("label", CreateJsValue(engine, abilityInfo.label));
49 object->SetProperty("description", CreateJsValue(engine, abilityInfo.description));
50 object->SetProperty("icon", CreateJsValue(engine, abilityInfo.iconPath));
51 object->SetProperty("labelId", CreateJsValue(engine, abilityInfo.labelId));
52 object->SetProperty("descriptionId", CreateJsValue(engine, abilityInfo.descriptionId));
53 object->SetProperty("iconId", CreateJsValue(engine, abilityInfo.iconId));
54 object->SetProperty("moduleName", CreateJsValue(engine, abilityInfo.moduleName));
55 object->SetProperty("process", CreateJsValue(engine, abilityInfo.process));
56 object->SetProperty("targetAbility", CreateJsValue(engine, abilityInfo.targetAbility));
57 object->SetProperty("backgroundModes", CreateJsValue(engine, abilityInfo.backgroundModes));
58 object->SetProperty("isVisible", CreateJsValue(engine, abilityInfo.visible));
59 object->SetProperty("formEnabled", CreateJsValue(engine, abilityInfo.formEnabled));
60
61 object->SetProperty("type", CreateJsValue(engine, abilityInfo.type));
62 object->SetProperty("subType", CreateJsValue(engine, abilityInfo.subType));
63 object->SetProperty("orientation", CreateJsValue(engine, abilityInfo.orientation));
64 object->SetProperty("launchMode", CreateJsValue(engine, abilityInfo.launchMode));
65
66 object->SetProperty("permissions", CreateNativeArray(engine, abilityInfo.permissions));
67 object->SetProperty("deviceTypes", CreateNativeArray(engine, abilityInfo.deviceTypes));
68 object->SetProperty("deviceCapabilities", CreateNativeArray(engine, abilityInfo.deviceCapabilities));
69
70 object->SetProperty("readPermission", CreateJsValue(engine, abilityInfo.readPermission));
71 object->SetProperty("writePermission", CreateJsValue(engine, abilityInfo.writePermission));
72
73 object->SetProperty("applicationInfo", CreateJsApplicationInfo(engine, abilityInfo.applicationInfo));
74
75 object->SetProperty("formEntity", CreateJsValue(engine, abilityInfo.formEntity));
76 object->SetProperty("minFormHeight", CreateJsValue(engine, abilityInfo.minFormHeight));
77 object->SetProperty("defaultFormHeight", CreateJsValue(engine, abilityInfo.defaultFormHeight));
78 object->SetProperty("minFormWidth", CreateJsValue(engine, abilityInfo.minFormWidth));
79 object->SetProperty("defaultFormWidth", CreateJsValue(engine, abilityInfo.defaultFormWidth));
80 object->SetProperty("uri", CreateJsValue(engine, abilityInfo.uri));
81
82 object->SetProperty("metaData", CreateJsCustomizeDataArray(engine, abilityInfo.metaData.customizeData));
83 object->SetProperty("metadata", CreateJsMetadataArray(engine, abilityInfo.metadata));
84 return objValue;
85 }
86
CreateJsMetadataArray(NativeEngine & engine,const std::vector<AppExecFwk::Metadata> & info)87 NativeValue* CreateJsMetadataArray(NativeEngine& engine, const std::vector<AppExecFwk::Metadata> &info)
88 {
89 NativeValue* arrayValue = engine.CreateArray(info.size());
90 NativeArray* array = ConvertNativeValueTo<NativeArray>(arrayValue);
91 uint32_t index = 0;
92 for (const auto& item : info) {
93 array->SetElement(index++, CreateJsMetadata(engine, item));
94 }
95 return arrayValue;
96 }
97
CreateJsMetadata(NativeEngine & engine,const AppExecFwk::Metadata & Info)98 NativeValue* CreateJsMetadata(NativeEngine& engine, const AppExecFwk::Metadata &Info)
99 {
100 NativeValue *objValue = engine.CreateObject();
101 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
102
103 object->SetProperty("name", CreateJsValue(engine, Info.name));
104 object->SetProperty("value", CreateJsValue(engine, Info.value));
105 object->SetProperty("resource", CreateJsValue(engine, Info.resource));
106 return objValue;
107 }
108
CreateJsCustomizeData(NativeEngine & engine,const AppExecFwk::CustomizeData & Info)109 NativeValue* CreateJsCustomizeData(NativeEngine& engine, const AppExecFwk::CustomizeData &Info)
110 {
111 NativeValue *objValue = engine.CreateObject();
112 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
113
114 object->SetProperty("name", CreateJsValue(engine, Info.name));
115 object->SetProperty("value", CreateJsValue(engine, Info.value));
116 object->SetProperty("extra", CreateJsValue(engine, Info.extra));
117 return objValue;
118 }
119
CreateJsCustomizeDataArray(NativeEngine & engine,const std::vector<AppExecFwk::CustomizeData> & info)120 NativeValue* CreateJsCustomizeDataArray(NativeEngine& engine, const std::vector<AppExecFwk::CustomizeData> &info)
121 {
122 NativeValue* arrayValue = engine.CreateArray(info.size());
123 NativeArray* array = ConvertNativeValueTo<NativeArray>(arrayValue);
124 uint32_t index = 0;
125 for (const auto& item : info) {
126 array->SetElement(index++, CreateJsCustomizeData(engine, item));
127 }
128 return arrayValue;
129 }
130
CreateJsModuleInfo(NativeEngine & engine,const AppExecFwk::ModuleInfo & moduleInfo)131 NativeValue* CreateJsModuleInfo(NativeEngine& engine, const AppExecFwk::ModuleInfo &moduleInfo)
132 {
133 NativeValue *objValue = engine.CreateObject();
134 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
135
136 object->SetProperty("moduleName", CreateJsValue(engine, moduleInfo.moduleName));
137 object->SetProperty("moduleSourceDir", CreateJsValue(engine, moduleInfo.moduleSourceDir));
138
139 return objValue;
140 }
141
CreateJsApplicationInfo(NativeEngine & engine,const AppExecFwk::ApplicationInfo & applicationInfo)142 NativeValue* CreateJsApplicationInfo(NativeEngine& engine, const AppExecFwk::ApplicationInfo &applicationInfo)
143 {
144 NativeValue *objValue = engine.CreateObject();
145 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
146
147 object->SetProperty("name", CreateJsValue(engine, applicationInfo.name));
148 object->SetProperty("description", CreateJsValue(engine, applicationInfo.description));
149 object->SetProperty("descriptionId", CreateJsValue(engine, applicationInfo.descriptionId));
150 object->SetProperty("systemApp", CreateJsValue(engine, applicationInfo.isSystemApp));
151 object->SetProperty("enabled", CreateJsValue(engine, applicationInfo.enabled));
152 object->SetProperty("label", CreateJsValue(engine, applicationInfo.label));
153 object->SetProperty("labelId", CreateJsValue(engine, std::to_string(applicationInfo.labelId)));
154 object->SetProperty("icon", CreateJsValue(engine, applicationInfo.iconPath));
155 object->SetProperty("iconId", CreateJsValue(engine, std::to_string(applicationInfo.iconId)));
156 object->SetProperty("process", CreateJsValue(engine, applicationInfo.process));
157 object->SetProperty("supportedModes", CreateJsValue(engine, applicationInfo.supportedModes));
158 object->SetProperty("entryDir", CreateJsValue(engine, applicationInfo.entryDir));
159 object->SetProperty("codePath", CreateJsValue(engine, applicationInfo.codePath));
160 object->SetProperty("removable", CreateJsValue(engine, applicationInfo.removable));
161
162 object->SetProperty("moduleSourceDirs", CreateNativeArray(engine, applicationInfo.moduleSourceDirs));
163 object->SetProperty("permissions", CreateNativeArray(engine, applicationInfo.permissions));
164
165 NativeValue *arrayValue = engine.CreateArray(applicationInfo.moduleInfos.size());
166 NativeArray *array = ConvertNativeValueTo<NativeArray>(arrayValue);
167 if (array != nullptr) {
168 uint32_t index = 0;
169 for (auto entity : applicationInfo.moduleInfos) {
170 array->SetElement(index++, CreateJsModuleInfo(engine, entity));
171 }
172 }
173 object->SetProperty("moduleInfos", arrayValue);
174
175 for (auto &item : applicationInfo.metaData) {
176 object->SetProperty(item.first.c_str(), CreateJsCustomizeDataArray(engine, item.second));
177 }
178
179 for (auto &item : applicationInfo.metadata) {
180 object->SetProperty(item.first.c_str(), CreateJsMetadataArray(engine, item.second));
181 }
182
183 return objValue;
184 }
185
CreateJsLaunchParam(NativeEngine & engine,const AAFwk::LaunchParam & launchParam)186 NativeValue* CreateJsLaunchParam(NativeEngine& engine, const AAFwk::LaunchParam& launchParam)
187 {
188 NativeValue *objValue = engine.CreateObject();
189 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
190
191 object->SetProperty("launchReason", CreateJsValue(engine, launchParam.launchReason));
192 object->SetProperty("lastExitReason", CreateJsValue(engine, launchParam.lastExitReason));
193
194 return objValue;
195 }
196
CreateJsConfiguration(NativeEngine & engine,const AppExecFwk::Configuration & configuration)197 NativeValue* CreateJsConfiguration(NativeEngine& engine, const AppExecFwk::Configuration& configuration)
198 {
199 NativeValue* objValue = engine.CreateObject();
200 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
201
202 object->SetProperty("language", CreateJsValue(engine,
203 configuration.GetItem(GlobalConfigurationKey::SYSTEM_LANGUAGE)));
204 object->SetProperty("colorMode", CreateJsValue(engine,
205 ConvertColorMode(configuration.GetItem(GlobalConfigurationKey::SYSTEM_COLORMODE))));
206
207 int32_t displayId = ConvertDisplayId(configuration.GetItem(ConfigurationInner::APPLICATION_DISPLAYID));
208
209 std::string direction = configuration.GetItem(displayId, ConfigurationInner::APPLICATION_DIRECTION);
210 object->SetProperty("direction", CreateJsValue(engine, ConvertDirection(direction)));
211
212 std::string density = configuration.GetItem(displayId, ConfigurationInner::APPLICATION_DENSITYDPI);
213 object->SetProperty("screenDensity", CreateJsValue(engine, ConvertDensity(density)));
214
215 object->SetProperty("displayId", CreateJsValue(engine, displayId));
216
217 return objValue;
218 }
219 } // namespace AbilityRuntime
220 } // namespace OHOS
221