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 "install_result.h"
17
18 #include "app_log_tag_wrapper.h"
19 #include "app_log_wrapper.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 char* JSON_KEY_RESULT_TRANSACTID = "transactId";
29 const char* JSON_KEY_RESULT_RESULTMSG = "resultMsg";
30 const char* JSON_KEY_RESULT_RETCODE = "retCode";
31 const char* JSON_KEY_PROGRESS_DOWNLOADSIZE = "downloadSize";
32 const char* JSON_KEY_PROGRESS_TOTALSIZE = "totalSize";
33 const char* JSON_KEY_INSTALLRESULT_VERSION = "version";
34 const char* JSON_KEY_INSTALLRESULT_RESULT = "result";
35 const char* JSON_KEY_INSTALLRESULT_PROGRESS = "progress";
36 } // namespace
37
to_json(nlohmann::json & jsonObject,const Result & result)38 void to_json(nlohmann::json &jsonObject, const Result &result)
39 {
40 jsonObject = nlohmann::json {
41 {JSON_KEY_RESULT_TRANSACTID, result.transactId},
42 {JSON_KEY_RESULT_RESULTMSG, result.resultMsg},
43 {JSON_KEY_RESULT_RETCODE, result.retCode}
44 };
45 }
46
from_json(const nlohmann::json & jsonObject,Result & result)47 void from_json(const nlohmann::json &jsonObject, Result &result)
48 {
49 const auto &jsonObjectEnd = jsonObject.end();
50 int32_t parseResult = ERR_OK;
51 GetValueIfFindKey<std::string>(jsonObject,
52 jsonObjectEnd,
53 JSON_KEY_RESULT_TRANSACTID,
54 result.transactId,
55 JsonType::STRING,
56 false,
57 parseResult,
58 ArrayType::NOT_ARRAY);
59 GetValueIfFindKey<std::string>(jsonObject,
60 jsonObjectEnd,
61 JSON_KEY_RESULT_RESULTMSG,
62 result.resultMsg,
63 JsonType::STRING,
64 false,
65 parseResult,
66 ArrayType::NOT_ARRAY);
67 GetValueIfFindKey<std::uint32_t>(jsonObject,
68 jsonObjectEnd,
69 JSON_KEY_RESULT_RETCODE,
70 result.retCode,
71 JsonType::NUMBER,
72 false,
73 parseResult,
74 ArrayType::NOT_ARRAY);
75 if (parseResult != ERR_OK) {
76 LOG_E(BMS_TAG_DEFAULT, "read module result from jsonObject error, error code : %{public}d", parseResult);
77 }
78 }
79
to_json(nlohmann::json & jsonObject,const Progress & progress)80 void to_json(nlohmann::json &jsonObject, const Progress &progress)
81 {
82 jsonObject = nlohmann::json {
83 {JSON_KEY_PROGRESS_DOWNLOADSIZE, progress.downloadSize},
84 {JSON_KEY_PROGRESS_TOTALSIZE, progress.totalSize}
85 };
86 }
87
from_json(const nlohmann::json & jsonObject,Progress & progress)88 void from_json(const nlohmann::json &jsonObject, Progress &progress)
89 {
90 const auto &jsonObjectEnd = jsonObject.end();
91 int32_t parseResult = ERR_OK;
92 GetValueIfFindKey<std::uint32_t>(jsonObject,
93 jsonObjectEnd,
94 JSON_KEY_PROGRESS_DOWNLOADSIZE,
95 progress.downloadSize,
96 JsonType::NUMBER,
97 false,
98 parseResult,
99 ArrayType::NOT_ARRAY);
100 GetValueIfFindKey<std::uint32_t>(jsonObject,
101 jsonObjectEnd,
102 JSON_KEY_PROGRESS_TOTALSIZE,
103 progress.totalSize,
104 JsonType::NUMBER,
105 false,
106 parseResult,
107 ArrayType::NOT_ARRAY);
108 if (parseResult != ERR_OK) {
109 LOG_E(BMS_TAG_DEFAULT, "read module progress from jsonObject error, error code : %{public}d", parseResult);
110 }
111 }
112
to_json(nlohmann::json & jsonObject,const InstallResult & installResult)113 void to_json(nlohmann::json &jsonObject, const InstallResult &installResult)
114 {
115 jsonObject = nlohmann::json {
116 {JSON_KEY_INSTALLRESULT_VERSION, installResult.version},
117 {JSON_KEY_INSTALLRESULT_RESULT, installResult.result},
118 {JSON_KEY_INSTALLRESULT_PROGRESS, installResult.progress}
119 };
120 }
121
from_json(const nlohmann::json & jsonObject,InstallResult & installResult)122 void from_json(const nlohmann::json &jsonObject, InstallResult &installResult)
123 {
124 const auto &jsonObjectEnd = jsonObject.end();
125 int32_t parseResult = ERR_OK;
126 GetValueIfFindKey<std::string>(jsonObject,
127 jsonObjectEnd,
128 JSON_KEY_INSTALLRESULT_VERSION,
129 installResult.version,
130 JsonType::STRING,
131 false,
132 parseResult,
133 ArrayType::NOT_ARRAY);
134 GetValueIfFindKey<Result>(jsonObject,
135 jsonObjectEnd,
136 JSON_KEY_INSTALLRESULT_RESULT,
137 installResult.result,
138 JsonType::OBJECT,
139 false,
140 parseResult,
141 ArrayType::NOT_ARRAY);
142 GetValueIfFindKey<Progress>(jsonObject,
143 jsonObjectEnd,
144 JSON_KEY_INSTALLRESULT_PROGRESS,
145 installResult.progress,
146 JsonType::OBJECT,
147 false,
148 parseResult,
149 ArrayType::NOT_ARRAY);
150 if (parseResult != ERR_OK) {
151 LOG_E(BMS_TAG_DEFAULT, "read module installResult from jsonObject error: %{public}d", parseResult);
152 }
153 }
154
ReadFromParcel(Parcel & parcel)155 bool Result::ReadFromParcel(Parcel &parcel)
156 {
157 transactId = Str16ToStr8(parcel.ReadString16());
158 resultMsg = Str16ToStr8(parcel.ReadString16());
159 retCode = parcel.ReadInt32();
160 return true;
161 }
162
Marshalling(Parcel & parcel) const163 bool Result::Marshalling(Parcel &parcel) const
164 {
165 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(transactId));
166 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(resultMsg));
167 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, retCode);
168 return true;
169 }
170
Unmarshalling(Parcel & parcel)171 Result *Result::Unmarshalling(Parcel &parcel)
172 {
173 Result *result = new (std::nothrow) Result();
174 if (result && !result->ReadFromParcel(parcel)) {
175 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
176 delete result;
177 result = nullptr;
178 }
179 return result;
180 }
181
ReadFromParcel(Parcel & parcel)182 bool Progress::ReadFromParcel(Parcel &parcel)
183 {
184 downloadSize = parcel.ReadInt32();
185 totalSize = parcel.ReadInt32();
186 return true;
187 }
188
Marshalling(Parcel & parcel) const189 bool Progress::Marshalling(Parcel &parcel) const
190 {
191 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, downloadSize);
192 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, totalSize);
193 return true;
194 }
195
Unmarshalling(Parcel & parcel)196 Progress *Progress::Unmarshalling(Parcel &parcel)
197 {
198 Progress *progress = new (std::nothrow) Progress();
199 if (progress && !progress->ReadFromParcel(parcel)) {
200 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
201 delete progress;
202 progress = nullptr;
203 }
204 return progress;
205 }
206
ReadFromParcel(Parcel & parcel)207 bool InstallResult::ReadFromParcel(Parcel &parcel)
208 {
209 version = Str16ToStr8(parcel.ReadString16());
210 auto paramsResult = parcel.ReadParcelable<Result>();
211 if (paramsResult != nullptr) {
212 result = *paramsResult;
213 delete paramsResult;
214 paramsResult = nullptr;
215 } else {
216 return false;
217 }
218
219 auto paramsProgress = parcel.ReadParcelable<Progress>();
220 if (paramsProgress != nullptr) {
221 progress = *paramsProgress;
222 delete paramsProgress;
223 paramsProgress = nullptr;
224 } else {
225 return false;
226 }
227 return true;
228 }
229
Marshalling(Parcel & parcel) const230 bool InstallResult::Marshalling(Parcel &parcel) const
231 {
232 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(version));
233 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &result);
234 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &progress);
235 return true;
236 }
237
Unmarshalling(Parcel & parcel)238 InstallResult *InstallResult::Unmarshalling(Parcel &parcel)
239 {
240 InstallResult *installResult = new (std::nothrow) InstallResult();
241 if (installResult && !installResult->ReadFromParcel(parcel)) {
242 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
243 delete installResult;
244 installResult = nullptr;
245 }
246 return installResult;
247 }
248 } // namespace AppExecFwk
249 } // namespace OHOS