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