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