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