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 #include "shortcut_info.h"
16
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include "bundle_constants.h"
23 #include "json_util.h"
24 #include "nlohmann/json.hpp"
25 #include "parcel_macro.h"
26 #include "string_ex.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string JSON_KEY_BUNDLE_ID = "id";
32 const std::string JSON_KEY_BUNDLE_HOST_ABILITY = "hostAbility";
33 const std::string JSON_KEY_BUNDLE_ICON = "icon";
34 const std::string JSON_KEY_BUNDLE_LABEL = "label";
35 const std::string JSON_KEY_BUNDLE_DISABLE_MESSAGE = "disableMessage";
36 const std::string JSON_KEY_BUNDLE_IS_STATIC = "isStatic";
37 const std::string JSON_KEY_BUNDLE_IS_HOME_SHORTCUT = "isHomeShortcut";
38 const std::string JSON_KEY_BUNDLE_IS_ENABLES = "isEnables";
39 const std::string JSON_KEY_BUNDLE_INTENTS = "intents";
40 const std::string JSON_KEY_BUNDLE_TARGET_BUNDLE = "targetBundle";
41 const std::string JSON_KEY_BUNDLE_TARGET_MODULE = "targetModule";
42 const std::string JSON_KEY_BUNDLE_TARGET_CLASS = "targetClass";
43 const std::string JSON_KEY_ICON_ID = "iconId";
44 const std::string JSON_KEY_LABEL_ID = "labelId";
45 const std::string SHORTCUTS = "shortcuts";
46 const std::string SHORTCUT_ID = "shortcutId";
47 const std::string SHORTCUT_WANTS = "wants";
48 const std::string ICON = "icon";
49 const std::string ICON_ID = "iconId";
50 const std::string LABEL = "label";
51 const std::string LABEL_ID = "labelId";
52 } // namespace
53
ReadFromParcel(Parcel & parcel)54 bool ShortcutInfo::ReadFromParcel(Parcel &parcel)
55 {
56 id = Str16ToStr8(parcel.ReadString16());
57 bundleName = Str16ToStr8(parcel.ReadString16());
58 moduleName = Str16ToStr8(parcel.ReadString16());
59 hostAbility = Str16ToStr8(parcel.ReadString16());
60 icon = Str16ToStr8(parcel.ReadString16());
61 label = Str16ToStr8(parcel.ReadString16());
62 iconId = parcel.ReadInt32();
63 labelId = parcel.ReadInt32();
64 disableMessage = Str16ToStr8(parcel.ReadString16());
65 isStatic = parcel.ReadBool();
66 isHomeShortcut = parcel.ReadBool();
67 isEnables = parcel.ReadBool();
68 int32_t intentsSize;
69 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, intentsSize);
70 for (auto i = 0; i < intentsSize; i++) {
71 ShortcutIntent shortcutIntent;
72 shortcutIntent.targetBundle = Str16ToStr8(parcel.ReadString16()); // target bundle name
73 shortcutIntent.targetModule = Str16ToStr8(parcel.ReadString16()); // target module name
74 shortcutIntent.targetClass = Str16ToStr8(parcel.ReadString16()); // target class name
75 intents.emplace_back(shortcutIntent);
76 }
77 return true;
78 }
79
Unmarshalling(Parcel & parcel)80 ShortcutInfo *ShortcutInfo::Unmarshalling(Parcel &parcel)
81 {
82 ShortcutInfo *info = new ShortcutInfo();
83 if (!info->ReadFromParcel(parcel)) {
84 APP_LOGW("read from parcel failed");
85 delete info;
86 info = nullptr;
87 }
88 return info;
89 }
90
Marshalling(Parcel & parcel) const91 bool ShortcutInfo::Marshalling(Parcel &parcel) const
92 {
93 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(id));
94 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
95 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
96 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hostAbility));
97 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(icon));
98 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(label));
99 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, iconId);
100 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, labelId);
101 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(disableMessage));
102 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isStatic);
103 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isHomeShortcut);
104 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isEnables);
105
106 const auto intentsSize = static_cast<int32_t>(intents.size());
107 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, intentsSize);
108 for (auto i = 0; i < intentsSize; i++) {
109 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(intents[i].targetBundle));
110 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(intents[i].targetModule));
111 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(intents[i].targetClass));
112 }
113 return true;
114 }
115
to_json(nlohmann::json & jsonObject,const ShortcutIntent & shortcutIntent)116 void to_json(nlohmann::json &jsonObject, const ShortcutIntent &shortcutIntent)
117 {
118 jsonObject = nlohmann::json {
119 {JSON_KEY_BUNDLE_TARGET_BUNDLE, shortcutIntent.targetBundle},
120 {JSON_KEY_BUNDLE_TARGET_MODULE, shortcutIntent.targetModule},
121 {JSON_KEY_BUNDLE_TARGET_CLASS, shortcutIntent.targetClass},
122 };
123 }
124
to_json(nlohmann::json & jsonObject,const ShortcutInfo & shortcutInfo)125 void to_json(nlohmann::json &jsonObject, const ShortcutInfo &shortcutInfo)
126 {
127 jsonObject = nlohmann::json {
128 {JSON_KEY_BUNDLE_ID, shortcutInfo.id},
129 {Constants::BUNDLE_NAME, shortcutInfo.bundleName},
130 {Constants::MODULE_NAME, shortcutInfo.moduleName},
131 {JSON_KEY_BUNDLE_HOST_ABILITY, shortcutInfo.hostAbility},
132 {JSON_KEY_BUNDLE_ICON, shortcutInfo.icon},
133 {JSON_KEY_BUNDLE_LABEL, shortcutInfo.label},
134 {JSON_KEY_BUNDLE_DISABLE_MESSAGE, shortcutInfo.disableMessage},
135 {JSON_KEY_BUNDLE_IS_STATIC, shortcutInfo.isStatic},
136 {JSON_KEY_BUNDLE_IS_HOME_SHORTCUT, shortcutInfo.isHomeShortcut},
137 {JSON_KEY_BUNDLE_IS_ENABLES, shortcutInfo.isEnables},
138 {JSON_KEY_BUNDLE_INTENTS, shortcutInfo.intents},
139 {JSON_KEY_ICON_ID, shortcutInfo.iconId},
140 {JSON_KEY_LABEL_ID, shortcutInfo.labelId},
141 };
142 }
143
from_json(const nlohmann::json & jsonObject,ShortcutIntent & shortcutIntent)144 void from_json(const nlohmann::json &jsonObject, ShortcutIntent &shortcutIntent)
145 {
146 const auto &jsonObjectEnd = jsonObject.end();
147 int32_t parseResult = ERR_OK;
148 GetValueIfFindKey<std::string>(jsonObject,
149 jsonObjectEnd,
150 JSON_KEY_BUNDLE_TARGET_BUNDLE,
151 shortcutIntent.targetBundle,
152 JsonType::STRING,
153 false,
154 parseResult,
155 ArrayType::NOT_ARRAY);
156 GetValueIfFindKey<std::string>(jsonObject,
157 jsonObjectEnd,
158 JSON_KEY_BUNDLE_TARGET_MODULE,
159 shortcutIntent.targetModule,
160 JsonType::STRING,
161 false,
162 parseResult,
163 ArrayType::NOT_ARRAY);
164 GetValueIfFindKey<std::string>(jsonObject,
165 jsonObjectEnd,
166 JSON_KEY_BUNDLE_TARGET_CLASS,
167 shortcutIntent.targetClass,
168 JsonType::STRING,
169 false,
170 parseResult,
171 ArrayType::NOT_ARRAY);
172 }
173
from_json(const nlohmann::json & jsonObject,ShortcutInfo & shortcutInfo)174 void from_json(const nlohmann::json &jsonObject, ShortcutInfo &shortcutInfo)
175 {
176 const auto &jsonObjectEnd = jsonObject.end();
177 int32_t parseResult = ERR_OK;
178 GetValueIfFindKey<std::string>(jsonObject,
179 jsonObjectEnd,
180 JSON_KEY_BUNDLE_ID,
181 shortcutInfo.id,
182 JsonType::STRING,
183 false,
184 parseResult,
185 ArrayType::NOT_ARRAY);
186 GetValueIfFindKey<std::string>(jsonObject,
187 jsonObjectEnd,
188 Constants::BUNDLE_NAME,
189 shortcutInfo.bundleName,
190 JsonType::STRING,
191 false,
192 parseResult,
193 ArrayType::NOT_ARRAY);
194 GetValueIfFindKey<std::string>(jsonObject,
195 jsonObjectEnd,
196 Constants::MODULE_NAME,
197 shortcutInfo.moduleName,
198 JsonType::STRING,
199 false,
200 parseResult,
201 ArrayType::NOT_ARRAY);
202 GetValueIfFindKey<std::string>(jsonObject,
203 jsonObjectEnd,
204 JSON_KEY_BUNDLE_HOST_ABILITY,
205 shortcutInfo.hostAbility,
206 JsonType::STRING,
207 false,
208 parseResult,
209 ArrayType::NOT_ARRAY);
210 GetValueIfFindKey<std::string>(jsonObject,
211 jsonObjectEnd,
212 JSON_KEY_BUNDLE_ICON,
213 shortcutInfo.icon,
214 JsonType::STRING,
215 false,
216 parseResult,
217 ArrayType::NOT_ARRAY);
218 GetValueIfFindKey<std::string>(jsonObject,
219 jsonObjectEnd,
220 JSON_KEY_BUNDLE_LABEL,
221 shortcutInfo.label,
222 JsonType::STRING,
223 false,
224 parseResult,
225 ArrayType::NOT_ARRAY);
226 GetValueIfFindKey<std::string>(jsonObject,
227 jsonObjectEnd,
228 JSON_KEY_BUNDLE_DISABLE_MESSAGE,
229 shortcutInfo.disableMessage,
230 JsonType::STRING,
231 false,
232 parseResult,
233 ArrayType::NOT_ARRAY);
234 GetValueIfFindKey<bool>(jsonObject,
235 jsonObjectEnd,
236 JSON_KEY_BUNDLE_IS_STATIC,
237 shortcutInfo.isStatic,
238 JsonType::BOOLEAN,
239 false,
240 parseResult,
241 ArrayType::NOT_ARRAY);
242 GetValueIfFindKey<bool>(jsonObject,
243 jsonObjectEnd,
244 JSON_KEY_BUNDLE_IS_HOME_SHORTCUT,
245 shortcutInfo.isHomeShortcut,
246 JsonType::BOOLEAN,
247 false,
248 parseResult,
249 ArrayType::NOT_ARRAY);
250 GetValueIfFindKey<bool>(jsonObject,
251 jsonObjectEnd,
252 JSON_KEY_BUNDLE_IS_ENABLES,
253 shortcutInfo.isEnables,
254 JsonType::BOOLEAN,
255 false,
256 parseResult,
257 ArrayType::NOT_ARRAY);
258 GetValueIfFindKey<std::vector<ShortcutIntent>>(jsonObject,
259 jsonObjectEnd,
260 JSON_KEY_BUNDLE_INTENTS,
261 shortcutInfo.intents,
262 JsonType::ARRAY,
263 false,
264 parseResult,
265 ArrayType::OBJECT);
266 GetValueIfFindKey<int32_t>(jsonObject,
267 jsonObjectEnd,
268 JSON_KEY_ICON_ID,
269 shortcutInfo.iconId,
270 JsonType::NUMBER,
271 false,
272 parseResult,
273 ArrayType::NOT_ARRAY);
274 GetValueIfFindKey<int32_t>(jsonObject,
275 jsonObjectEnd,
276 JSON_KEY_LABEL_ID,
277 shortcutInfo.labelId,
278 JsonType::NUMBER,
279 false,
280 parseResult,
281 ArrayType::NOT_ARRAY);
282 }
283
from_json(const nlohmann::json & jsonObject,ShortcutWant & shortcutWant)284 void from_json(const nlohmann::json &jsonObject, ShortcutWant &shortcutWant)
285 {
286 const auto &jsonObjectEnd = jsonObject.end();
287 int32_t parseResult = ERR_OK;
288 GetValueIfFindKey<std::string>(jsonObject,
289 jsonObjectEnd,
290 Constants::BUNDLE_NAME,
291 shortcutWant.bundleName,
292 JsonType::STRING,
293 false,
294 parseResult,
295 ArrayType::NOT_ARRAY);
296 GetValueIfFindKey<std::string>(jsonObject,
297 jsonObjectEnd,
298 Constants::MODULE_NAME,
299 shortcutWant.moduleName,
300 JsonType::STRING,
301 false,
302 parseResult,
303 ArrayType::NOT_ARRAY);
304 GetValueIfFindKey<std::string>(jsonObject,
305 jsonObjectEnd,
306 Constants::ABILITY_NAME,
307 shortcutWant.abilityName,
308 JsonType::STRING,
309 false,
310 parseResult,
311 ArrayType::NOT_ARRAY);
312 if (parseResult != ERR_OK) {
313 APP_LOGE("read shortcutWant from module.json error, error code : %{public}d", parseResult);
314 }
315 }
316
from_json(const nlohmann::json & jsonObject,Shortcut & shortcut)317 void from_json(const nlohmann::json &jsonObject, Shortcut &shortcut)
318 {
319 const auto &jsonObjectEnd = jsonObject.end();
320 int32_t parseResult = ERR_OK;
321 GetValueIfFindKey<std::string>(jsonObject,
322 jsonObjectEnd,
323 SHORTCUT_ID,
324 shortcut.shortcutId,
325 JsonType::STRING,
326 false,
327 parseResult,
328 ArrayType::NOT_ARRAY);
329 GetValueIfFindKey<std::string>(jsonObject,
330 jsonObjectEnd,
331 ICON,
332 shortcut.icon,
333 JsonType::STRING,
334 false,
335 parseResult,
336 ArrayType::NOT_ARRAY);
337 GetValueIfFindKey<int32_t>(jsonObject,
338 jsonObjectEnd,
339 ICON_ID,
340 shortcut.iconId,
341 JsonType::NUMBER,
342 false,
343 parseResult,
344 ArrayType::NOT_ARRAY);
345 GetValueIfFindKey<std::string>(jsonObject,
346 jsonObjectEnd,
347 LABEL,
348 shortcut.label,
349 JsonType::STRING,
350 false,
351 parseResult,
352 ArrayType::NOT_ARRAY);
353 GetValueIfFindKey<int32_t>(jsonObject,
354 jsonObjectEnd,
355 LABEL_ID,
356 shortcut.labelId,
357 JsonType::NUMBER,
358 false,
359 parseResult,
360 ArrayType::NOT_ARRAY);
361 GetValueIfFindKey<std::vector<ShortcutWant>>(jsonObject,
362 jsonObjectEnd,
363 SHORTCUT_WANTS,
364 shortcut.wants,
365 JsonType::ARRAY,
366 false,
367 parseResult,
368 ArrayType::OBJECT);
369 if (parseResult != ERR_OK) {
370 APP_LOGE("read Shortcut from module.json error, error code : %{public}d", parseResult);
371 }
372 }
373
from_json(const nlohmann::json & jsonObject,ShortcutJson & shortcutJson)374 void from_json(const nlohmann::json &jsonObject, ShortcutJson &shortcutJson)
375 {
376 APP_LOGD("read shortcuts tag from module.json");
377 const auto &jsonObjectEnd = jsonObject.end();
378 int32_t parseResult = ERR_OK;
379 GetValueIfFindKey<std::vector<Shortcut>>(jsonObject,
380 jsonObjectEnd,
381 SHORTCUTS,
382 shortcutJson.shortcuts,
383 JsonType::ARRAY,
384 false,
385 parseResult,
386 ArrayType::OBJECT);
387 if (parseResult != ERR_OK) {
388 APP_LOGE("read ShortcutJson from module.json error, error code : %{public}d", parseResult);
389 }
390 }
391 } // namespace AppExecFwk
392 } // namespace OHOS
393