• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
174 
from_json(const nlohmann::json & jsonObject,ShortcutInfo & shortcutInfo)175 void from_json(const nlohmann::json &jsonObject, ShortcutInfo &shortcutInfo)
176 {
177     const auto &jsonObjectEnd = jsonObject.end();
178     int32_t parseResult = ERR_OK;
179     GetValueIfFindKey<std::string>(jsonObject,
180         jsonObjectEnd,
181         JSON_KEY_BUNDLE_ID,
182         shortcutInfo.id,
183         JsonType::STRING,
184         false,
185         parseResult,
186         ArrayType::NOT_ARRAY);
187     GetValueIfFindKey<std::string>(jsonObject,
188         jsonObjectEnd,
189         Constants::BUNDLE_NAME,
190         shortcutInfo.bundleName,
191         JsonType::STRING,
192         false,
193         parseResult,
194         ArrayType::NOT_ARRAY);
195     GetValueIfFindKey<std::string>(jsonObject,
196         jsonObjectEnd,
197         Constants::MODULE_NAME,
198         shortcutInfo.moduleName,
199         JsonType::STRING,
200         false,
201         parseResult,
202         ArrayType::NOT_ARRAY);
203     GetValueIfFindKey<std::string>(jsonObject,
204         jsonObjectEnd,
205         JSON_KEY_BUNDLE_HOST_ABILITY,
206         shortcutInfo.hostAbility,
207         JsonType::STRING,
208         false,
209         parseResult,
210         ArrayType::NOT_ARRAY);
211     GetValueIfFindKey<std::string>(jsonObject,
212         jsonObjectEnd,
213         JSON_KEY_BUNDLE_ICON,
214         shortcutInfo.icon,
215         JsonType::STRING,
216         false,
217         parseResult,
218         ArrayType::NOT_ARRAY);
219     GetValueIfFindKey<std::string>(jsonObject,
220         jsonObjectEnd,
221         JSON_KEY_BUNDLE_LABEL,
222         shortcutInfo.label,
223         JsonType::STRING,
224         false,
225         parseResult,
226         ArrayType::NOT_ARRAY);
227     GetValueIfFindKey<std::string>(jsonObject,
228         jsonObjectEnd,
229         JSON_KEY_BUNDLE_DISABLE_MESSAGE,
230         shortcutInfo.disableMessage,
231         JsonType::STRING,
232         false,
233         parseResult,
234         ArrayType::NOT_ARRAY);
235     GetValueIfFindKey<bool>(jsonObject,
236         jsonObjectEnd,
237         JSON_KEY_BUNDLE_IS_STATIC,
238         shortcutInfo.isStatic,
239         JsonType::BOOLEAN,
240         false,
241         parseResult,
242         ArrayType::NOT_ARRAY);
243     GetValueIfFindKey<bool>(jsonObject,
244         jsonObjectEnd,
245         JSON_KEY_BUNDLE_IS_HOME_SHORTCUT,
246         shortcutInfo.isHomeShortcut,
247         JsonType::BOOLEAN,
248         false,
249         parseResult,
250         ArrayType::NOT_ARRAY);
251     GetValueIfFindKey<bool>(jsonObject,
252         jsonObjectEnd,
253         JSON_KEY_BUNDLE_IS_ENABLES,
254         shortcutInfo.isEnables,
255         JsonType::BOOLEAN,
256         false,
257         parseResult,
258         ArrayType::NOT_ARRAY);
259     GetValueIfFindKey<std::vector<ShortcutIntent>>(jsonObject,
260         jsonObjectEnd,
261         JSON_KEY_BUNDLE_INTENTS,
262         shortcutInfo.intents,
263         JsonType::ARRAY,
264         false,
265         parseResult,
266         ArrayType::OBJECT);
267     GetValueIfFindKey<int32_t>(jsonObject,
268         jsonObjectEnd,
269         JSON_KEY_ICON_ID,
270         shortcutInfo.iconId,
271         JsonType::NUMBER,
272         false,
273         parseResult,
274         ArrayType::NOT_ARRAY);
275     GetValueIfFindKey<int32_t>(jsonObject,
276         jsonObjectEnd,
277         JSON_KEY_LABEL_ID,
278         shortcutInfo.labelId,
279         JsonType::NUMBER,
280         false,
281         parseResult,
282         ArrayType::NOT_ARRAY);
283 }
284 
from_json(const nlohmann::json & jsonObject,ShortcutWant & shortcutWant)285 void from_json(const nlohmann::json &jsonObject, ShortcutWant &shortcutWant)
286 {
287     const auto &jsonObjectEnd = jsonObject.end();
288     int32_t parseResult = ERR_OK;
289     GetValueIfFindKey<std::string>(jsonObject,
290         jsonObjectEnd,
291         Constants::BUNDLE_NAME,
292         shortcutWant.bundleName,
293         JsonType::STRING,
294         false,
295         parseResult,
296         ArrayType::NOT_ARRAY);
297     GetValueIfFindKey<std::string>(jsonObject,
298         jsonObjectEnd,
299         Constants::MODULE_NAME,
300         shortcutWant.moduleName,
301         JsonType::STRING,
302         false,
303         parseResult,
304         ArrayType::NOT_ARRAY);
305     GetValueIfFindKey<std::string>(jsonObject,
306         jsonObjectEnd,
307         Constants::ABILITY_NAME,
308         shortcutWant.abilityName,
309         JsonType::STRING,
310         false,
311         parseResult,
312         ArrayType::NOT_ARRAY);
313     if (parseResult != ERR_OK) {
314         APP_LOGE("read shortcutWant from module.json error, error code : %{public}d", parseResult);
315     }
316 }
317 
from_json(const nlohmann::json & jsonObject,Shortcut & shortcut)318 void from_json(const nlohmann::json &jsonObject, Shortcut &shortcut)
319 {
320     const auto &jsonObjectEnd = jsonObject.end();
321     int32_t parseResult = ERR_OK;
322     GetValueIfFindKey<std::string>(jsonObject,
323         jsonObjectEnd,
324         SHORTCUT_ID,
325         shortcut.shortcutId,
326         JsonType::STRING,
327         false,
328         parseResult,
329         ArrayType::NOT_ARRAY);
330     GetValueIfFindKey<std::string>(jsonObject,
331         jsonObjectEnd,
332         ICON,
333         shortcut.icon,
334         JsonType::STRING,
335         false,
336         parseResult,
337         ArrayType::NOT_ARRAY);
338     GetValueIfFindKey<int32_t>(jsonObject,
339         jsonObjectEnd,
340         ICON_ID,
341         shortcut.iconId,
342         JsonType::NUMBER,
343         false,
344         parseResult,
345         ArrayType::NOT_ARRAY);
346     GetValueIfFindKey<std::string>(jsonObject,
347         jsonObjectEnd,
348         LABEL,
349         shortcut.label,
350         JsonType::STRING,
351         false,
352         parseResult,
353         ArrayType::NOT_ARRAY);
354     GetValueIfFindKey<int32_t>(jsonObject,
355         jsonObjectEnd,
356         LABEL_ID,
357         shortcut.labelId,
358         JsonType::NUMBER,
359         false,
360         parseResult,
361         ArrayType::NOT_ARRAY);
362     GetValueIfFindKey<std::vector<ShortcutWant>>(jsonObject,
363         jsonObjectEnd,
364         SHORTCUT_WANTS,
365         shortcut.wants,
366         JsonType::ARRAY,
367         false,
368         parseResult,
369         ArrayType::OBJECT);
370     if (parseResult != ERR_OK) {
371         APP_LOGE("read Shortcut from module.json error, error code : %{public}d", parseResult);
372     }
373 }
374 
from_json(const nlohmann::json & jsonObject,ShortcutJson & shortcutJson)375 void from_json(const nlohmann::json &jsonObject, ShortcutJson &shortcutJson)
376 {
377     APP_LOGD("read shortcuts tag from module.json");
378     const auto &jsonObjectEnd = jsonObject.end();
379     int32_t parseResult = ERR_OK;
380     GetValueIfFindKey<std::vector<Shortcut>>(jsonObject,
381         jsonObjectEnd,
382         SHORTCUTS,
383         shortcutJson.shortcuts,
384         JsonType::ARRAY,
385         false,
386         parseResult,
387         ArrayType::OBJECT);
388     if (parseResult != ERR_OK) {
389         APP_LOGE("read ShortcutJson from module.json error, error code : %{public}d", parseResult);
390     }
391 }
392 }  // namespace AppExecFwk
393 }  // namespace OHOS
394