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