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