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