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 "rpc_id_result.h"
17
18 #include "app_log_wrapper.h"
19 #include "bundle_constants.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_VERSION = "version";
29 const char* JSON_KEY_RESULT_TRANSACT_ID = "transactId";
30 const char* JSON_KEY_RESULT_RETCODE = "retCode";
31 const char* JSON_KEY_RESULT_RESULT_MSG = "resultMsg";
32 const char* JSON_KEY_SUMMARY_ABILITY_INFO = "abilityInfo";
33 const char* JSON_KEY_SUMMARY_ABILITY_INFO_LOGO_URL = "logoUrl";
34 const char* JSON_KEY_SUMMARY_ABILITY_INFO_LABEL = "label";
35 const char* JSON_KEY_SUMMARY_ABILITY_INFO_DEVICE_TYPE = "deviceType";
36 const char* JSON_KEY_SUMMARY_ABILITY_INFO_RPC_ID = "rpcId";
37 } // namespace
38
to_json(nlohmann::json & jsonObject,const SummaryAbilityInfo & summaryAbilityInfo)39 void to_json(nlohmann::json &jsonObject, const SummaryAbilityInfo &summaryAbilityInfo)
40 {
41 jsonObject = nlohmann::json {
42 {Constants::BUNDLE_NAME, summaryAbilityInfo.bundleName},
43 {Constants::MODULE_NAME, summaryAbilityInfo.moduleName},
44 {Constants::ABILITY_NAME, summaryAbilityInfo.abilityName},
45 {JSON_KEY_SUMMARY_ABILITY_INFO_LOGO_URL, summaryAbilityInfo.logoUrl},
46 {JSON_KEY_SUMMARY_ABILITY_INFO_LABEL, summaryAbilityInfo.label},
47 {JSON_KEY_SUMMARY_ABILITY_INFO_DEVICE_TYPE, summaryAbilityInfo.deviceType},
48 {JSON_KEY_SUMMARY_ABILITY_INFO_RPC_ID, summaryAbilityInfo.rpcId},
49 };
50 }
51
from_json(const nlohmann::json & jsonObject,SummaryAbilityInfo & summaryAbilityInfo)52 void from_json(const nlohmann::json &jsonObject, SummaryAbilityInfo &summaryAbilityInfo)
53 {
54 const auto &jsonObjectEnd = jsonObject.end();
55 int32_t parseResult = ERR_OK;
56 GetValueIfFindKey<std::string>(jsonObject,
57 jsonObjectEnd,
58 Constants::BUNDLE_NAME,
59 summaryAbilityInfo.bundleName,
60 JsonType::STRING,
61 false,
62 parseResult,
63 ArrayType::NOT_ARRAY);
64 GetValueIfFindKey<std::string>(jsonObject,
65 jsonObjectEnd,
66 Constants::MODULE_NAME,
67 summaryAbilityInfo.moduleName,
68 JsonType::STRING,
69 false,
70 parseResult,
71 ArrayType::NOT_ARRAY);
72 GetValueIfFindKey<std::string>(jsonObject,
73 jsonObjectEnd,
74 Constants::ABILITY_NAME,
75 summaryAbilityInfo.abilityName,
76 JsonType::STRING,
77 false,
78 parseResult,
79 ArrayType::NOT_ARRAY);
80 GetValueIfFindKey<std::string>(jsonObject,
81 jsonObjectEnd,
82 JSON_KEY_SUMMARY_ABILITY_INFO_LOGO_URL,
83 summaryAbilityInfo.logoUrl,
84 JsonType::STRING,
85 false,
86 parseResult,
87 ArrayType::NOT_ARRAY);
88 GetValueIfFindKey<std::string>(jsonObject,
89 jsonObjectEnd,
90 JSON_KEY_SUMMARY_ABILITY_INFO_LABEL,
91 summaryAbilityInfo.label,
92 JsonType::STRING,
93 false,
94 parseResult,
95 ArrayType::NOT_ARRAY);
96 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
97 jsonObjectEnd,
98 JSON_KEY_SUMMARY_ABILITY_INFO_DEVICE_TYPE,
99 summaryAbilityInfo.deviceType,
100 JsonType::ARRAY,
101 false,
102 parseResult,
103 ArrayType::STRING);
104 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
105 jsonObjectEnd,
106 JSON_KEY_SUMMARY_ABILITY_INFO_RPC_ID,
107 summaryAbilityInfo.rpcId,
108 JsonType::ARRAY,
109 false,
110 parseResult,
111 ArrayType::STRING);
112 if (parseResult != ERR_OK) {
113 APP_LOGE("read result jsonObject error : %{public}d", parseResult);
114 }
115 }
116
to_json(nlohmann::json & jsonObject,const RpcIdResult & rpcIdResult)117 void to_json(nlohmann::json &jsonObject, const RpcIdResult &rpcIdResult)
118 {
119 jsonObject = nlohmann::json {
120 {JSON_KEY_RESULT_VERSION, rpcIdResult.version},
121 {JSON_KEY_RESULT_TRANSACT_ID, rpcIdResult.transactId},
122 {JSON_KEY_RESULT_RETCODE, rpcIdResult.retCode},
123 {JSON_KEY_RESULT_RESULT_MSG, rpcIdResult.resultMsg},
124 {JSON_KEY_SUMMARY_ABILITY_INFO, rpcIdResult.abilityInfo},
125 };
126 }
127
from_json(const nlohmann::json & jsonObject,RpcIdResult & rpcIdResult)128 void from_json(const nlohmann::json &jsonObject, RpcIdResult &rpcIdResult)
129 {
130 const auto &jsonObjectEnd = jsonObject.end();
131 int32_t parseResult = ERR_OK;
132 GetValueIfFindKey<std::string>(jsonObject,
133 jsonObjectEnd,
134 JSON_KEY_RESULT_VERSION,
135 rpcIdResult.version,
136 JsonType::STRING,
137 false,
138 parseResult,
139 ArrayType::NOT_ARRAY);
140 GetValueIfFindKey<std::string>(jsonObject,
141 jsonObjectEnd,
142 JSON_KEY_RESULT_TRANSACT_ID,
143 rpcIdResult.transactId,
144 JsonType::STRING,
145 false,
146 parseResult,
147 ArrayType::NOT_ARRAY);
148 GetValueIfFindKey<int32_t>(jsonObject,
149 jsonObjectEnd,
150 JSON_KEY_RESULT_RETCODE,
151 rpcIdResult.retCode,
152 JsonType::NUMBER,
153 false,
154 parseResult,
155 ArrayType::NOT_ARRAY);
156 GetValueIfFindKey<std::string>(jsonObject,
157 jsonObjectEnd,
158 JSON_KEY_RESULT_RESULT_MSG,
159 rpcIdResult.resultMsg,
160 JsonType::STRING,
161 false,
162 parseResult,
163 ArrayType::NOT_ARRAY);
164 GetValueIfFindKey<SummaryAbilityInfo>(jsonObject,
165 jsonObjectEnd,
166 JSON_KEY_SUMMARY_ABILITY_INFO,
167 rpcIdResult.abilityInfo,
168 JsonType::OBJECT,
169 false,
170 parseResult,
171 ArrayType::NOT_ARRAY);
172 if (parseResult != ERR_OK) {
173 APP_LOGE("read result jsonObject error : %{public}d", parseResult);
174 }
175 }
176
ReadFromParcel(Parcel & parcel)177 bool SummaryAbilityInfo::ReadFromParcel(Parcel &parcel)
178 {
179 bundleName = Str16ToStr8(parcel.ReadString16());
180 moduleName = Str16ToStr8(parcel.ReadString16());
181 abilityName = Str16ToStr8(parcel.ReadString16());
182 logoUrl = Str16ToStr8(parcel.ReadString16());
183 label = Str16ToStr8(parcel.ReadString16());
184 int32_t deviceTypeSize;
185 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, deviceTypeSize);
186 CONTAINER_SECURITY_VERIFY(parcel, deviceTypeSize, &deviceType);
187 for (auto i = 0; i < deviceTypeSize; i++) {
188 deviceType.emplace_back(Str16ToStr8(parcel.ReadString16()));
189 }
190
191 int32_t rpcIdSize;
192 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, rpcIdSize);
193 CONTAINER_SECURITY_VERIFY(parcel, rpcIdSize, &rpcId);
194 for (auto i = 0; i < rpcIdSize; i++) {
195 rpcId.emplace_back(Str16ToStr8(parcel.ReadString16()));
196 }
197 return true;
198 }
199
Marshalling(Parcel & parcel) const200 bool SummaryAbilityInfo::Marshalling(Parcel &parcel) const
201 {
202 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
203 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
204 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName));
205 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(logoUrl));
206 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(label));
207 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, deviceType.size());
208 for (auto &type : deviceType) {
209 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(type));
210 }
211 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, rpcId.size());
212 for (auto &id : rpcId) {
213 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(id));
214 }
215 return true;
216 }
217
Unmarshalling(Parcel & parcel)218 SummaryAbilityInfo *SummaryAbilityInfo::Unmarshalling(Parcel &parcel)
219 {
220 SummaryAbilityInfo *result = new (std::nothrow) SummaryAbilityInfo();
221 if (result && !result->ReadFromParcel(parcel)) {
222 APP_LOGE("read from parcel failed");
223 delete result;
224 result = nullptr;
225 }
226 return result;
227 }
228
ReadFromParcel(Parcel & parcel)229 bool RpcIdResult::ReadFromParcel(Parcel &parcel)
230 {
231 version = Str16ToStr8(parcel.ReadString16());
232 transactId = Str16ToStr8(parcel.ReadString16());
233 retCode = parcel.ReadInt32();
234 resultMsg = Str16ToStr8(parcel.ReadString16());
235 std::unique_ptr<SummaryAbilityInfo> summaryAbilityInfo(parcel.ReadParcelable<SummaryAbilityInfo>());
236 if (!summaryAbilityInfo) {
237 APP_LOGE("ReadParcelable<SummaryAbilityInfo> failed");
238 return false;
239 }
240 abilityInfo = *summaryAbilityInfo;
241 return true;
242 }
243
Marshalling(Parcel & parcel) const244 bool RpcIdResult::Marshalling(Parcel &parcel) const
245 {
246 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(version));
247 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(transactId));
248 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, retCode);
249 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &abilityInfo);
250 return true;
251 }
252
Unmarshalling(Parcel & parcel)253 RpcIdResult *RpcIdResult::Unmarshalling(Parcel &parcel)
254 {
255 RpcIdResult *result = new (std::nothrow) RpcIdResult();
256 if (result && !result->ReadFromParcel(parcel)) {
257 APP_LOGE("read from parcel failed");
258 delete result;
259 result = nullptr;
260 }
261 return result;
262 }
263 }
264 }