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