• 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 "distributed_bundle_info.h"
17 
18 #include "nlohmann/json.hpp"
19 #include "parcel_macro.h"
20 #include "string_ex.h"
21 
22 #include "app_log_wrapper.h"
23 #include "bundle_constants.h"
24 #include "json_util.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const std::string JSON_KEY_VERSION = "version";
30 const std::string JSON_KEY_VERSION_CODE = "versionCode";
31 const std::string JSON_KEY_COMPATIBLE_VERSION_CODE = "compatibleVersionCode";
32 const std::string JSON_KEY_VERSION_NAME = "versionName";
33 const std::string JSON_KEY_MIN_COMPATIBLE_VERSION = "minCompatibleVersion";
34 const std::string JSON_KEY_TARGET_VERSION_CODE = "targetVersionCode";
35 const std::string JSON_KEY_APP_ID = "appId";
36 const std::string JSON_KEY_MODULE_INFOS = "moduleInfos";
37 const std::string JSON_KEY_ENABLED = "enabled";
38 }
ReadFromParcel(Parcel & parcel)39 bool DistributedBundleInfo::ReadFromParcel(Parcel &parcel)
40 {
41     version = parcel.ReadUint32();
42     versionCode = parcel.ReadUint32();
43     compatibleVersionCode = parcel.ReadUint32();
44     minCompatibleVersion = parcel.ReadUint32();
45     targetVersionCode = parcel.ReadUint32();
46     bundleName = Str16ToStr8(parcel.ReadString16());
47     versionName = Str16ToStr8(parcel.ReadString16());
48     appId = Str16ToStr8(parcel.ReadString16());
49 
50     uint32_t moduleInfosSize;
51     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, moduleInfosSize);
52     for (uint32_t i = 0; i < moduleInfosSize; i++) {
53         std::unique_ptr<DistributedModuleInfo> distributedModuleInfo(parcel.ReadParcelable<DistributedModuleInfo>());
54         if (!distributedModuleInfo) {
55             APP_LOGE("ReadParcelable<DistributedModuleInfo> failed");
56             return false;
57         }
58         moduleInfos.emplace_back(*distributedModuleInfo);
59     }
60     enabled = parcel.ReadBool();
61     return true;
62 }
63 
Marshalling(Parcel & parcel) const64 bool DistributedBundleInfo::Marshalling(Parcel &parcel) const
65 {
66     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, version);
67     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
68     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, compatibleVersionCode);
69     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, minCompatibleVersion);
70     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, targetVersionCode);
71     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
72     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(versionName));
73     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(appId));
74 
75     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, moduleInfos.size());
76     for (auto &moduleInfo : moduleInfos) {
77         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &moduleInfo);
78     }
79     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, enabled);
80     return true;
81 }
82 
Unmarshalling(Parcel & parcel)83 DistributedBundleInfo *DistributedBundleInfo::Unmarshalling(Parcel &parcel)
84 {
85     DistributedBundleInfo *info = new (std::nothrow) DistributedBundleInfo();
86     if (info && !info->ReadFromParcel(parcel)) {
87         APP_LOGW("read from parcel failed");
88         delete info;
89         info = nullptr;
90     }
91     return info;
92 }
93 
ToString() const94 std::string DistributedBundleInfo::ToString() const
95 {
96     nlohmann::json jsonObject;
97     jsonObject[JSON_KEY_VERSION] = version;
98     jsonObject[Constants::BUNDLE_NAME] = bundleName;
99     jsonObject[JSON_KEY_VERSION_CODE] = versionCode;
100     jsonObject[JSON_KEY_VERSION_NAME] = versionName;
101     jsonObject[JSON_KEY_COMPATIBLE_VERSION_CODE] = compatibleVersionCode;
102     jsonObject[JSON_KEY_MIN_COMPATIBLE_VERSION] = minCompatibleVersion;
103     jsonObject[JSON_KEY_TARGET_VERSION_CODE] = targetVersionCode;
104     jsonObject[JSON_KEY_APP_ID] = appId;
105     jsonObject[JSON_KEY_MODULE_INFOS] = moduleInfos;
106     jsonObject[JSON_KEY_ENABLED] = enabled;
107     return jsonObject.dump();
108 }
109 
FromJsonString(const std::string & jsonString)110 bool DistributedBundleInfo::FromJsonString(const std::string &jsonString)
111 {
112     nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
113     if (jsonObject.is_discarded()) {
114         APP_LOGE("failed to parse DistributedBundleInfo: %{public}s.", jsonString.c_str());
115         return false;
116     }
117 
118     const auto &jsonObjectEnd = jsonObject.end();
119     int32_t parseResult = ERR_OK;
120     GetValueIfFindKey<uint32_t>(jsonObject,
121         jsonObjectEnd,
122         JSON_KEY_VERSION,
123         version,
124         JsonType::NUMBER,
125         false,
126         parseResult,
127         ArrayType::NOT_ARRAY);
128     GetValueIfFindKey<std::string>(jsonObject,
129         jsonObjectEnd,
130         Constants::BUNDLE_NAME,
131         bundleName,
132         JsonType::STRING,
133         false,
134         parseResult,
135         ArrayType::NOT_ARRAY);
136     GetValueIfFindKey<uint32_t>(jsonObject,
137         jsonObjectEnd,
138         JSON_KEY_VERSION_CODE,
139         versionCode,
140         JsonType::NUMBER,
141         false,
142         parseResult,
143         ArrayType::NOT_ARRAY);
144     GetValueIfFindKey<uint32_t>(jsonObject,
145         jsonObjectEnd,
146         JSON_KEY_COMPATIBLE_VERSION_CODE,
147         compatibleVersionCode,
148         JsonType::NUMBER,
149         false,
150         parseResult,
151         ArrayType::NOT_ARRAY);
152     GetValueIfFindKey<std::string>(jsonObject,
153         jsonObjectEnd,
154         JSON_KEY_VERSION_NAME,
155         versionName,
156         JsonType::STRING,
157         false,
158         parseResult,
159         ArrayType::NOT_ARRAY);
160     GetValueIfFindKey<uint32_t>(jsonObject,
161         jsonObjectEnd,
162         JSON_KEY_MIN_COMPATIBLE_VERSION,
163         minCompatibleVersion,
164         JsonType::NUMBER,
165         false,
166         parseResult,
167         ArrayType::NOT_ARRAY);
168     GetValueIfFindKey<uint32_t>(jsonObject,
169         jsonObjectEnd,
170         JSON_KEY_TARGET_VERSION_CODE,
171         targetVersionCode,
172         JsonType::NUMBER,
173         false,
174         parseResult,
175         ArrayType::NOT_ARRAY);
176     GetValueIfFindKey<std::string>(jsonObject,
177         jsonObjectEnd,
178         JSON_KEY_APP_ID,
179         appId,
180         JsonType::STRING,
181         false,
182         parseResult,
183         ArrayType::NOT_ARRAY);
184     GetValueIfFindKey<std::vector<DistributedModuleInfo>>(jsonObject,
185         jsonObjectEnd,
186         JSON_KEY_MODULE_INFOS,
187         moduleInfos,
188         JsonType::ARRAY,
189         false,
190         parseResult,
191         ArrayType::OBJECT);
192     GetValueIfFindKey<bool>(jsonObject,
193         jsonObjectEnd,
194         JSON_KEY_ENABLED,
195         enabled,
196         JsonType::BOOLEAN,
197         false,
198         parseResult,
199         ArrayType::NOT_ARRAY);
200     return parseResult == ERR_OK;
201 }
202 }  // namespace AppExecFwk
203 }  // namespace OHOS
204