1 /*
2 * Copyright (c) 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
16 #include "target_ability_info.h"
17
18 #include "app_log_tag_wrapper.h"
19 #include "app_log_wrapper.h"
20 #include "bundle_constants.h"
21 #include "json_util.h"
22 #include "nlohmann/json.hpp"
23 #include "parcel_macro.h"
24 #include "string_ex.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const char* JSON_KEY_VERSION = "version";
30 const char* JSON_KEY_TARGETINFO = "targetInfo";
31 const char* JSON_KEY_TARGETEXTSETTING = "targetExtSetting";
32 const char* JSON_KEY_EXTINFO = "extInfo";
33 const char* JSON_KEY_TRANSACTID = "transactId";
34 const char* JSON_KEY_FLAGS = "flags";
35 const char* JSON_KEY_REASONFLAG = "reasonFlag";
36 const char* JSON_KEY_CALLINGUID = "callingUid";
37 const char* JSON_KEY_CALLINGAPPTYPE = "callingAppType";
38 const char* JSON_KEY_CALLINGBUNDLENAMES = "callingBundleNames";
39 const char* JSON_KEY_CALLINGAPPIDS = "callingAppIds";
40 const char* JSON_KEY_PRELOAD_MODULE_NAMES = "preloadModuleNames";
41 const char* JSON_KEY_ACTION = "action";
42 const char* JSON_KEY_URI = "uri";
43 const char* JSON_KEY_TYPE = "type";
44 const char* JSON_KEY_EMBEDDED = "embedded";
45 } // namespace
46
to_json(nlohmann::json & jsonObject,const TargetExtSetting & targetExtSetting)47 void to_json(nlohmann::json &jsonObject, const TargetExtSetting &targetExtSetting)
48 {
49 jsonObject = nlohmann::json {
50 {JSON_KEY_EXTINFO, targetExtSetting.extValues},
51 };
52 }
53
from_json(const nlohmann::json & jsonObject,TargetExtSetting & targetExtSetting)54 void from_json(const nlohmann::json &jsonObject, TargetExtSetting &targetExtSetting)
55 {
56 const auto &jsonObjectEnd = jsonObject.end();
57 int32_t parseResult = ERR_OK;
58 GetValueIfFindKey<std::map<std::string, std::string>>(jsonObject,
59 jsonObjectEnd,
60 JSON_KEY_EXTINFO,
61 targetExtSetting.extValues,
62 JsonType::OBJECT,
63 false,
64 parseResult,
65 ArrayType::NOT_ARRAY);
66 if (parseResult != ERR_OK) {
67 LOG_E(BMS_TAG_DEFAULT, "read module targetExtSetting from jsonObject error: %{public}d", parseResult);
68 }
69 }
70
ReadFromParcel(Parcel & parcel)71 bool TargetExtSetting::ReadFromParcel(Parcel &parcel)
72 {
73 int32_t extValueSize;
74 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, extValueSize);
75 CONTAINER_SECURITY_VERIFY(parcel, extValueSize, &extValues);
76 for (int32_t i = 0; i < extValueSize; ++i) {
77 std::string key = Str16ToStr8(parcel.ReadString16());
78 std::string value = Str16ToStr8(parcel.ReadString16());
79 extValues.emplace(key, value);
80 }
81 return true;
82 }
83
Marshalling(Parcel & parcel) const84 bool TargetExtSetting::Marshalling(Parcel &parcel) const
85 {
86 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(extValues.size()));
87 for (auto& extValue : extValues) {
88 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(extValue.first));
89 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(extValue.second));
90 }
91 return true;
92 }
93
Unmarshalling(Parcel & parcel)94 TargetExtSetting *TargetExtSetting::Unmarshalling(Parcel &parcel)
95 {
96 TargetExtSetting *targetExtSettingInfo = new (std::nothrow) TargetExtSetting();
97 if (targetExtSettingInfo && !targetExtSettingInfo->ReadFromParcel(parcel)) {
98 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
99 delete targetExtSettingInfo;
100 targetExtSettingInfo = nullptr;
101 }
102 return targetExtSettingInfo;
103 }
104
to_json(nlohmann::json & jsonObject,const TargetInfo & targetInfo)105 void to_json(nlohmann::json &jsonObject, const TargetInfo &targetInfo)
106 {
107 jsonObject = nlohmann::json {
108 {JSON_KEY_TRANSACTID, targetInfo.transactId},
109 {Constants::BUNDLE_NAME, targetInfo.bundleName},
110 {Constants::MODULE_NAME, targetInfo.moduleName},
111 {Constants::ABILITY_NAME, targetInfo.abilityName},
112 {JSON_KEY_ACTION, targetInfo.action},
113 {JSON_KEY_URI, targetInfo.uri},
114 {JSON_KEY_TYPE, targetInfo.type},
115 {JSON_KEY_FLAGS, targetInfo.flags},
116 {JSON_KEY_REASONFLAG, targetInfo.reasonFlag},
117 {JSON_KEY_CALLINGUID, targetInfo.callingUid},
118 {JSON_KEY_CALLINGAPPTYPE, targetInfo.callingAppType},
119 {JSON_KEY_CALLINGBUNDLENAMES, targetInfo.callingBundleNames},
120 {JSON_KEY_CALLINGAPPIDS, targetInfo.callingAppIds},
121 {JSON_KEY_PRELOAD_MODULE_NAMES, targetInfo.preloadModuleNames},
122 {JSON_KEY_EMBEDDED, targetInfo.embedded}
123 };
124 }
125
from_json(const nlohmann::json & jsonObject,TargetInfo & targetInfo)126 void from_json(const nlohmann::json &jsonObject, TargetInfo &targetInfo)
127 {
128 const auto &jsonObjectEnd = jsonObject.end();
129 int32_t parseResult = ERR_OK;
130 GetValueIfFindKey<std::string>(jsonObject,
131 jsonObjectEnd,
132 JSON_KEY_TRANSACTID,
133 targetInfo.transactId,
134 JsonType::STRING,
135 false,
136 parseResult,
137 ArrayType::NOT_ARRAY);
138 GetValueIfFindKey<std::string>(jsonObject,
139 jsonObjectEnd,
140 Constants::BUNDLE_NAME,
141 targetInfo.bundleName,
142 JsonType::STRING,
143 false,
144 parseResult,
145 ArrayType::NOT_ARRAY);
146 GetValueIfFindKey<std::string>(jsonObject,
147 jsonObjectEnd,
148 Constants::MODULE_NAME,
149 targetInfo.moduleName,
150 JsonType::STRING,
151 false,
152 parseResult,
153 ArrayType::NOT_ARRAY);
154 GetValueIfFindKey<std::string>(jsonObject,
155 jsonObjectEnd,
156 Constants::ABILITY_NAME,
157 targetInfo.abilityName,
158 JsonType::STRING,
159 false,
160 parseResult,
161 ArrayType::NOT_ARRAY);
162 GetValueIfFindKey<std::string>(jsonObject,
163 jsonObjectEnd,
164 JSON_KEY_ACTION,
165 targetInfo.action,
166 JsonType::STRING,
167 false,
168 parseResult,
169 ArrayType::NOT_ARRAY);
170 GetValueIfFindKey<std::string>(jsonObject,
171 jsonObjectEnd,
172 JSON_KEY_URI,
173 targetInfo.uri,
174 JsonType::STRING,
175 false,
176 parseResult,
177 ArrayType::NOT_ARRAY);
178 GetValueIfFindKey<std::string>(jsonObject,
179 jsonObjectEnd,
180 JSON_KEY_TYPE,
181 targetInfo.type,
182 JsonType::STRING,
183 false,
184 parseResult,
185 ArrayType::NOT_ARRAY);
186 GetValueIfFindKey<std::uint32_t>(jsonObject,
187 jsonObjectEnd,
188 JSON_KEY_FLAGS,
189 targetInfo.flags,
190 JsonType::NUMBER,
191 false,
192 parseResult,
193 ArrayType::NOT_ARRAY);
194 GetValueIfFindKey<std::uint32_t>(jsonObject,
195 jsonObjectEnd,
196 JSON_KEY_REASONFLAG,
197 targetInfo.reasonFlag,
198 JsonType::NUMBER,
199 false,
200 parseResult,
201 ArrayType::NOT_ARRAY);
202 GetValueIfFindKey<std::uint32_t>(jsonObject,
203 jsonObjectEnd,
204 JSON_KEY_CALLINGUID,
205 targetInfo.callingUid,
206 JsonType::NUMBER,
207 false,
208 parseResult,
209 ArrayType::NOT_ARRAY);
210 GetValueIfFindKey<std::uint32_t>(jsonObject,
211 jsonObjectEnd,
212 JSON_KEY_CALLINGAPPTYPE,
213 targetInfo.callingAppType,
214 JsonType::NUMBER,
215 false,
216 parseResult,
217 ArrayType::NOT_ARRAY);
218 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
219 jsonObjectEnd,
220 JSON_KEY_CALLINGBUNDLENAMES,
221 targetInfo.callingBundleNames,
222 JsonType::ARRAY,
223 false,
224 parseResult,
225 ArrayType::STRING);
226 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
227 jsonObjectEnd,
228 JSON_KEY_CALLINGAPPIDS,
229 targetInfo.callingAppIds,
230 JsonType::ARRAY,
231 false,
232 parseResult,
233 ArrayType::STRING);
234 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
235 jsonObjectEnd,
236 JSON_KEY_PRELOAD_MODULE_NAMES,
237 targetInfo.preloadModuleNames,
238 JsonType::ARRAY,
239 false,
240 parseResult,
241 ArrayType::STRING);
242 GetValueIfFindKey<std::int32_t>(jsonObject,
243 jsonObjectEnd,
244 JSON_KEY_EMBEDDED,
245 targetInfo.embedded,
246 JsonType::NUMBER,
247 false,
248 parseResult,
249 ArrayType::NOT_ARRAY);
250 if (parseResult != ERR_OK) {
251 LOG_E(BMS_TAG_DEFAULT, "read module targetInfo from jsonObject error: %{public}d", parseResult);
252 }
253 }
254
ReadFromParcel(Parcel & parcel)255 bool TargetInfo::ReadFromParcel(Parcel &parcel)
256 {
257 transactId = Str16ToStr8(parcel.ReadString16());
258 bundleName = Str16ToStr8(parcel.ReadString16());
259 moduleName = Str16ToStr8(parcel.ReadString16());
260 abilityName = Str16ToStr8(parcel.ReadString16());
261 action = Str16ToStr8(parcel.ReadString16());
262 uri = Str16ToStr8(parcel.ReadString16());
263 type = Str16ToStr8(parcel.ReadString16());
264 flags = parcel.ReadInt32();
265 reasonFlag = parcel.ReadInt32();
266 callingUid = parcel.ReadInt32();
267 callingAppType = parcel.ReadInt32();
268 int32_t callingBundleNamesSize = 0;
269 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingBundleNamesSize);
270 CONTAINER_SECURITY_VERIFY(parcel, callingBundleNamesSize, &callingBundleNames);
271 for (int32_t i = 0; i < callingBundleNamesSize; i++) {
272 callingBundleNames.emplace_back(Str16ToStr8(parcel.ReadString16()));
273 }
274 int32_t callingAppIdsSize = 0;
275 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingAppIdsSize);
276 CONTAINER_SECURITY_VERIFY(parcel, callingAppIdsSize, &callingAppIds);
277 for (int32_t i = 0; i < callingAppIdsSize; i++) {
278 callingAppIds.emplace_back(Str16ToStr8(parcel.ReadString16()));
279 }
280 int32_t preloadModuleNamesSize = 0;
281 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, preloadModuleNamesSize);
282 CONTAINER_SECURITY_VERIFY(parcel, preloadModuleNamesSize, &preloadModuleNames);
283 for (int32_t i = 0; i < preloadModuleNamesSize; i++) {
284 preloadModuleNames.emplace_back(Str16ToStr8(parcel.ReadString16()));
285 }
286 embedded = parcel.ReadInt32();
287 return true;
288 }
289
Marshalling(Parcel & parcel) const290 bool TargetInfo::Marshalling(Parcel &parcel) const
291 {
292 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(transactId));
293 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
294 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
295 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName));
296 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(action));
297 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(uri));
298 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(type));
299 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, flags);
300 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, reasonFlag);
301 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingUid);
302 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingAppType);
303 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingBundleNames.size());
304 for (auto &callingBundleName : callingBundleNames) {
305 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(callingBundleName));
306 }
307 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingAppIds.size());
308 for (auto &callingAppId : callingAppIds) {
309 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(callingAppId));
310 }
311 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, preloadModuleNames.size());
312 for (auto &preloadItem : preloadModuleNames) {
313 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(preloadItem));
314 }
315 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, embedded);
316 return true;
317 }
318
Unmarshalling(Parcel & parcel)319 TargetInfo *TargetInfo::Unmarshalling(Parcel &parcel)
320 {
321 TargetInfo *targetInfo = new (std::nothrow) TargetInfo();
322 if (targetInfo && !targetInfo->ReadFromParcel(parcel)) {
323 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
324 delete targetInfo;
325 targetInfo = nullptr;
326 }
327 return targetInfo;
328 }
329
to_json(nlohmann::json & jsonObject,const TargetAbilityInfo & targetAbilityInfo)330 void to_json(nlohmann::json &jsonObject, const TargetAbilityInfo &targetAbilityInfo)
331 {
332 jsonObject = nlohmann::json {
333 {JSON_KEY_VERSION, targetAbilityInfo.version},
334 {JSON_KEY_TARGETINFO, targetAbilityInfo.targetInfo},
335 {JSON_KEY_TARGETEXTSETTING, targetAbilityInfo.targetExtSetting},
336 };
337 }
338
from_json(const nlohmann::json & jsonObject,TargetAbilityInfo & targetAbilityInfo)339 void from_json(const nlohmann::json &jsonObject, TargetAbilityInfo &targetAbilityInfo)
340 {
341 const auto &jsonObjectEnd = jsonObject.end();
342 int32_t parseResult = ERR_OK;
343 GetValueIfFindKey<std::string>(jsonObject,
344 jsonObjectEnd,
345 JSON_KEY_VERSION,
346 targetAbilityInfo.version,
347 JsonType::STRING,
348 false,
349 parseResult,
350 ArrayType::NOT_ARRAY);
351 GetValueIfFindKey<TargetInfo>(jsonObject,
352 jsonObjectEnd,
353 JSON_KEY_TARGETINFO,
354 targetAbilityInfo.targetInfo,
355 JsonType::OBJECT,
356 false,
357 parseResult,
358 ArrayType::NOT_ARRAY);
359 GetValueIfFindKey<TargetExtSetting>(jsonObject,
360 jsonObjectEnd,
361 JSON_KEY_TARGETEXTSETTING,
362 targetAbilityInfo.targetExtSetting,
363 JsonType::OBJECT,
364 false,
365 parseResult,
366 ArrayType::NOT_ARRAY);
367 if (parseResult != ERR_OK) {
368 LOG_E(BMS_TAG_DEFAULT, "read module targetAbilityInfo from jsonObject error: %{public}d", parseResult);
369 }
370 }
371
ReadFromParcel(Parcel & parcel)372 bool TargetAbilityInfo::ReadFromParcel(Parcel &parcel)
373 {
374 version = Str16ToStr8(parcel.ReadString16());
375 auto params = parcel.ReadParcelable<TargetInfo>();
376 if (params != nullptr) {
377 targetInfo = *params;
378 delete params;
379 params = nullptr;
380 } else {
381 return false;
382 }
383 auto extSetting = parcel.ReadParcelable<TargetExtSetting>();
384 if (extSetting != nullptr) {
385 targetExtSetting = *extSetting;
386 delete extSetting;
387 extSetting = nullptr;
388 } else {
389 return false;
390 }
391 return true;
392 }
393
Marshalling(Parcel & parcel) const394 bool TargetAbilityInfo::Marshalling(Parcel &parcel) const
395 {
396 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(version));
397 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &targetInfo);
398 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &targetExtSetting);
399 return true;
400 }
401
Unmarshalling(Parcel & parcel)402 TargetAbilityInfo *TargetAbilityInfo::Unmarshalling(Parcel &parcel)
403 {
404 TargetAbilityInfo *targetAbilityInfo = new (std::nothrow) TargetAbilityInfo();
405 if (targetAbilityInfo && !targetAbilityInfo->ReadFromParcel(parcel)) {
406 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
407 delete targetAbilityInfo;
408 targetAbilityInfo = nullptr;
409 }
410 return targetAbilityInfo;
411 }
412 } // namespace AppExecFwk
413 } // namespace OHOS