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 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
131 jsonObjectEnd,
132 JSON_KEY_TRANSACTID,
133 targetInfo.transactId,
134 false,
135 parseResult);
136 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
137 jsonObjectEnd,
138 Constants::BUNDLE_NAME,
139 targetInfo.bundleName,
140 false,
141 parseResult);
142 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
143 jsonObjectEnd,
144 Constants::MODULE_NAME,
145 targetInfo.moduleName,
146 false,
147 parseResult);
148 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
149 jsonObjectEnd,
150 Constants::ABILITY_NAME,
151 targetInfo.abilityName,
152 false,
153 parseResult);
154 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
155 jsonObjectEnd,
156 JSON_KEY_ACTION,
157 targetInfo.action,
158 false,
159 parseResult);
160 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
161 jsonObjectEnd,
162 JSON_KEY_URI,
163 targetInfo.uri,
164 false,
165 parseResult);
166 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
167 jsonObjectEnd,
168 JSON_KEY_TYPE,
169 targetInfo.type,
170 false,
171 parseResult);
172 GetValueIfFindKey<std::uint32_t>(jsonObject,
173 jsonObjectEnd,
174 JSON_KEY_FLAGS,
175 targetInfo.flags,
176 JsonType::NUMBER,
177 false,
178 parseResult,
179 ArrayType::NOT_ARRAY);
180 GetValueIfFindKey<std::uint32_t>(jsonObject,
181 jsonObjectEnd,
182 JSON_KEY_REASONFLAG,
183 targetInfo.reasonFlag,
184 JsonType::NUMBER,
185 false,
186 parseResult,
187 ArrayType::NOT_ARRAY);
188 GetValueIfFindKey<std::uint32_t>(jsonObject,
189 jsonObjectEnd,
190 JSON_KEY_CALLINGUID,
191 targetInfo.callingUid,
192 JsonType::NUMBER,
193 false,
194 parseResult,
195 ArrayType::NOT_ARRAY);
196 GetValueIfFindKey<std::uint32_t>(jsonObject,
197 jsonObjectEnd,
198 JSON_KEY_CALLINGAPPTYPE,
199 targetInfo.callingAppType,
200 JsonType::NUMBER,
201 false,
202 parseResult,
203 ArrayType::NOT_ARRAY);
204 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
205 jsonObjectEnd,
206 JSON_KEY_CALLINGBUNDLENAMES,
207 targetInfo.callingBundleNames,
208 JsonType::ARRAY,
209 false,
210 parseResult,
211 ArrayType::STRING);
212 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
213 jsonObjectEnd,
214 JSON_KEY_CALLINGAPPIDS,
215 targetInfo.callingAppIds,
216 JsonType::ARRAY,
217 false,
218 parseResult,
219 ArrayType::STRING);
220 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
221 jsonObjectEnd,
222 JSON_KEY_PRELOAD_MODULE_NAMES,
223 targetInfo.preloadModuleNames,
224 JsonType::ARRAY,
225 false,
226 parseResult,
227 ArrayType::STRING);
228 GetValueIfFindKey<std::int32_t>(jsonObject,
229 jsonObjectEnd,
230 JSON_KEY_EMBEDDED,
231 targetInfo.embedded,
232 JsonType::NUMBER,
233 false,
234 parseResult,
235 ArrayType::NOT_ARRAY);
236 if (parseResult != ERR_OK) {
237 LOG_E(BMS_TAG_DEFAULT, "read module targetInfo from jsonObject error: %{public}d", parseResult);
238 }
239 }
240
ReadFromParcel(Parcel & parcel)241 bool TargetInfo::ReadFromParcel(Parcel &parcel)
242 {
243 transactId = Str16ToStr8(parcel.ReadString16());
244 bundleName = Str16ToStr8(parcel.ReadString16());
245 moduleName = Str16ToStr8(parcel.ReadString16());
246 abilityName = Str16ToStr8(parcel.ReadString16());
247 action = Str16ToStr8(parcel.ReadString16());
248 uri = Str16ToStr8(parcel.ReadString16());
249 type = Str16ToStr8(parcel.ReadString16());
250 flags = parcel.ReadInt32();
251 reasonFlag = parcel.ReadInt32();
252 callingUid = parcel.ReadInt32();
253 callingAppType = parcel.ReadInt32();
254 int32_t callingBundleNamesSize = 0;
255 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingBundleNamesSize);
256 CONTAINER_SECURITY_VERIFY(parcel, callingBundleNamesSize, &callingBundleNames);
257 for (int32_t i = 0; i < callingBundleNamesSize; i++) {
258 callingBundleNames.emplace_back(Str16ToStr8(parcel.ReadString16()));
259 }
260 int32_t callingAppIdsSize = 0;
261 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingAppIdsSize);
262 CONTAINER_SECURITY_VERIFY(parcel, callingAppIdsSize, &callingAppIds);
263 for (int32_t i = 0; i < callingAppIdsSize; i++) {
264 callingAppIds.emplace_back(Str16ToStr8(parcel.ReadString16()));
265 }
266 int32_t preloadModuleNamesSize = 0;
267 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, preloadModuleNamesSize);
268 CONTAINER_SECURITY_VERIFY(parcel, preloadModuleNamesSize, &preloadModuleNames);
269 for (int32_t i = 0; i < preloadModuleNamesSize; i++) {
270 preloadModuleNames.emplace_back(Str16ToStr8(parcel.ReadString16()));
271 }
272 embedded = parcel.ReadInt32();
273 return true;
274 }
275
Marshalling(Parcel & parcel) const276 bool TargetInfo::Marshalling(Parcel &parcel) const
277 {
278 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(transactId));
279 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
280 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
281 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName));
282 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(action));
283 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(uri));
284 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(type));
285 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, flags);
286 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, reasonFlag);
287 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingUid);
288 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingAppType);
289 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingBundleNames.size());
290 for (auto &callingBundleName : callingBundleNames) {
291 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(callingBundleName));
292 }
293 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, callingAppIds.size());
294 for (auto &callingAppId : callingAppIds) {
295 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(callingAppId));
296 }
297 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, preloadModuleNames.size());
298 for (auto &preloadItem : preloadModuleNames) {
299 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(preloadItem));
300 }
301 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, embedded);
302 return true;
303 }
304
Unmarshalling(Parcel & parcel)305 TargetInfo *TargetInfo::Unmarshalling(Parcel &parcel)
306 {
307 TargetInfo *targetInfo = new (std::nothrow) TargetInfo();
308 if (targetInfo && !targetInfo->ReadFromParcel(parcel)) {
309 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
310 delete targetInfo;
311 targetInfo = nullptr;
312 }
313 return targetInfo;
314 }
315
to_json(nlohmann::json & jsonObject,const TargetAbilityInfo & targetAbilityInfo)316 void to_json(nlohmann::json &jsonObject, const TargetAbilityInfo &targetAbilityInfo)
317 {
318 jsonObject = nlohmann::json {
319 {JSON_KEY_VERSION, targetAbilityInfo.version},
320 {JSON_KEY_TARGETINFO, targetAbilityInfo.targetInfo},
321 {JSON_KEY_TARGETEXTSETTING, targetAbilityInfo.targetExtSetting},
322 };
323 }
324
from_json(const nlohmann::json & jsonObject,TargetAbilityInfo & targetAbilityInfo)325 void from_json(const nlohmann::json &jsonObject, TargetAbilityInfo &targetAbilityInfo)
326 {
327 const auto &jsonObjectEnd = jsonObject.end();
328 int32_t parseResult = ERR_OK;
329 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
330 jsonObjectEnd,
331 JSON_KEY_VERSION,
332 targetAbilityInfo.version,
333 false,
334 parseResult);
335 GetValueIfFindKey<TargetInfo>(jsonObject,
336 jsonObjectEnd,
337 JSON_KEY_TARGETINFO,
338 targetAbilityInfo.targetInfo,
339 JsonType::OBJECT,
340 false,
341 parseResult,
342 ArrayType::NOT_ARRAY);
343 GetValueIfFindKey<TargetExtSetting>(jsonObject,
344 jsonObjectEnd,
345 JSON_KEY_TARGETEXTSETTING,
346 targetAbilityInfo.targetExtSetting,
347 JsonType::OBJECT,
348 false,
349 parseResult,
350 ArrayType::NOT_ARRAY);
351 if (parseResult != ERR_OK) {
352 LOG_E(BMS_TAG_DEFAULT, "read module targetAbilityInfo from jsonObject error: %{public}d", parseResult);
353 }
354 }
355
ReadFromParcel(Parcel & parcel)356 bool TargetAbilityInfo::ReadFromParcel(Parcel &parcel)
357 {
358 version = Str16ToStr8(parcel.ReadString16());
359 auto params = parcel.ReadParcelable<TargetInfo>();
360 if (params != nullptr) {
361 targetInfo = *params;
362 delete params;
363 params = nullptr;
364 } else {
365 return false;
366 }
367 auto extSetting = parcel.ReadParcelable<TargetExtSetting>();
368 if (extSetting != nullptr) {
369 targetExtSetting = *extSetting;
370 delete extSetting;
371 extSetting = nullptr;
372 } else {
373 return false;
374 }
375 return true;
376 }
377
Marshalling(Parcel & parcel) const378 bool TargetAbilityInfo::Marshalling(Parcel &parcel) const
379 {
380 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(version));
381 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &targetInfo);
382 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &targetExtSetting);
383 return true;
384 }
385
Unmarshalling(Parcel & parcel)386 TargetAbilityInfo *TargetAbilityInfo::Unmarshalling(Parcel &parcel)
387 {
388 TargetAbilityInfo *targetAbilityInfo = new (std::nothrow) TargetAbilityInfo();
389 if (targetAbilityInfo && !targetAbilityInfo->ReadFromParcel(parcel)) {
390 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
391 delete targetAbilityInfo;
392 targetAbilityInfo = nullptr;
393 }
394 return targetAbilityInfo;
395 }
396 } // namespace AppExecFwk
397 } // namespace OHOS