• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 std::string JSON_KEY_RESULT_VERSION = "version";
29 const std::string JSON_KEY_RESULT_TRANSACT_ID = "transactId";
30 const std::string JSON_KEY_RESULT_RETCODE = "retCode";
31 const std::string JSON_KEY_RESULT_RESULT_MSG = "resultMsg";
32 const std::string JSON_KEY_SUMMARY_ABILITY_INFO = "abilityInfo";
33 const std::string JSON_KEY_SUMMARY_ABILITY_INFO_LOGO_URL = "logoUrl";
34 const std::string JSON_KEY_SUMMARY_ABILITY_INFO_LABEL = "label";
35 const std::string JSON_KEY_SUMMARY_ABILITY_INFO_DEVICE_TYPE = "deviceType";
36 const std::string 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 module result from jsonObject error, error code : %{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 module result from jsonObject error, error code : %{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     for (auto i = 0; i < deviceTypeSize; i++) {
187         deviceType.emplace_back(Str16ToStr8(parcel.ReadString16()));
188     }
189 
190     int32_t rpcIdSize;
191     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, rpcIdSize);
192     for (auto i = 0; i < rpcIdSize; i++) {
193         rpcId.emplace_back(Str16ToStr8(parcel.ReadString16()));
194     }
195     return true;
196 }
197 
Marshalling(Parcel & parcel) const198 bool SummaryAbilityInfo::Marshalling(Parcel &parcel) const
199 {
200     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
201     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
202     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName));
203     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(logoUrl));
204     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(label));
205     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, deviceType.size());
206     for (auto &type : deviceType) {
207         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(type));
208     }
209     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, rpcId.size());
210     for (auto &id : rpcId) {
211         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(id));
212     }
213     return true;
214 }
215 
Unmarshalling(Parcel & parcel)216 SummaryAbilityInfo *SummaryAbilityInfo::Unmarshalling(Parcel &parcel)
217 {
218     SummaryAbilityInfo *result = new (std::nothrow) SummaryAbilityInfo();
219     if (result && !result->ReadFromParcel(parcel)) {
220         APP_LOGE("read from parcel failed");
221         delete result;
222         result = nullptr;
223     }
224     return result;
225 }
226 
ReadFromParcel(Parcel & parcel)227 bool RpcIdResult::ReadFromParcel(Parcel &parcel)
228 {
229     version = Str16ToStr8(parcel.ReadString16());
230     transactId = Str16ToStr8(parcel.ReadString16());
231     retCode = parcel.ReadInt32();
232     resultMsg = Str16ToStr8(parcel.ReadString16());
233     std::unique_ptr<SummaryAbilityInfo> summaryAbilityInfo(parcel.ReadParcelable<SummaryAbilityInfo>());
234     if (!summaryAbilityInfo) {
235         APP_LOGE("ReadParcelable<SummaryAbilityInfo> failed");
236         return false;
237     }
238     abilityInfo = *summaryAbilityInfo;
239     return true;
240 }
241 
Marshalling(Parcel & parcel) const242 bool RpcIdResult::Marshalling(Parcel &parcel) const
243 {
244     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(version));
245     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(transactId));
246     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, retCode);
247     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &abilityInfo);
248     return true;
249 }
250 
Unmarshalling(Parcel & parcel)251 RpcIdResult *RpcIdResult::Unmarshalling(Parcel &parcel)
252 {
253     RpcIdResult *result = new (std::nothrow) RpcIdResult();
254     if (result && !result->ReadFromParcel(parcel)) {
255         APP_LOGE("read from parcel failed");
256         delete result;
257         result = nullptr;
258     }
259     return result;
260 }
261 }
262 }