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