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