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