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 "distributed_bms_proxy.h"
17
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "parcel_macro.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
DistributedBmsProxy(const sptr<IRemoteObject> & object)24 DistributedBmsProxy::DistributedBmsProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IDistributedBms>(object)
25 {
26 APP_LOGI("DistributedBmsProxy instance is created");
27 }
28
~DistributedBmsProxy()29 DistributedBmsProxy::~DistributedBmsProxy()
30 {
31 APP_LOGI("DistributedBmsProxy instance is destroyed");
32 }
33
GetRemoteAbilityInfo(const OHOS::AppExecFwk::ElementName & elementName,RemoteAbilityInfo & remoteAbilityInfo)34 int32_t DistributedBmsProxy::GetRemoteAbilityInfo(
35 const OHOS::AppExecFwk::ElementName &elementName, RemoteAbilityInfo &remoteAbilityInfo)
36 {
37 APP_LOGD("DistributedBmsProxy GetRemoteAbilityInfo");
38 return GetRemoteAbilityInfo(elementName, "", remoteAbilityInfo);
39 }
40
GetRemoteAbilityInfo(const OHOS::AppExecFwk::ElementName & elementName,const std::string & localeInfo,RemoteAbilityInfo & remoteAbilityInfo)41 int32_t DistributedBmsProxy::GetRemoteAbilityInfo(const OHOS::AppExecFwk::ElementName &elementName,
42 const std::string &localeInfo,
43 RemoteAbilityInfo &remoteAbilityInfo)
44 {
45 APP_LOGD("DistributedBmsProxy GetRemoteAbilityInfoWithLocale");
46 int32_t checkRet = CheckElementName(elementName);
47 if (checkRet != ERR_OK) {
48 APP_LOGE("DistributedBmsProxy GetRemoteAbilityInfoWithLocale check elementName failed");
49 return checkRet;
50 }
51 MessageParcel data;
52 MessageParcel reply;
53 if (!data.WriteInterfaceToken(GetDescriptor())) {
54 APP_LOGE("fail to GetRemoteAbilityInfo due to write InterfaceToken fail");
55 return ERR_APPEXECFWK_PARCEL_ERROR;
56 }
57 if (!data.WriteParcelable(&elementName)) {
58 APP_LOGE("DistributedBmsProxy GetRemoteAbilityInfo write elementName error");
59 return ERR_APPEXECFWK_PARCEL_ERROR;
60 }
61 if (!data.WriteString(localeInfo)) {
62 APP_LOGE("DistributedBmsProxy GetRemoteAbilityInfo write localeInfo error");
63 return ERR_APPEXECFWK_PARCEL_ERROR;
64 }
65
66 int32_t result = GetParcelableInfo<RemoteAbilityInfo>(
67 DistributedInterfaceCode::GET_REMOTE_ABILITY_INFO_WITH_LOCALE, data, remoteAbilityInfo);
68 if (result != OHOS::NO_ERROR) {
69 APP_LOGE("fail to query ability info mutiparam from server, result:%{public}d", result);
70 }
71 return result;
72 }
73
GetRemoteAbilityInfos(const std::vector<ElementName> & elementNames,std::vector<RemoteAbilityInfo> & remoteAbilityInfos)74 int32_t DistributedBmsProxy::GetRemoteAbilityInfos(
75 const std::vector<ElementName> &elementNames, std::vector<RemoteAbilityInfo> &remoteAbilityInfos)
76 {
77 APP_LOGD("DistributedBmsProxy GetRemoteAbilityInfos");
78 return GetRemoteAbilityInfos(elementNames, "", remoteAbilityInfos);
79 }
80
GetRemoteAbilityInfos(const std::vector<ElementName> & elementNames,const std::string & localeInfo,std::vector<RemoteAbilityInfo> & remoteAbilityInfos)81 int32_t DistributedBmsProxy::GetRemoteAbilityInfos(const std::vector<ElementName> &elementNames,
82 const std::string &localeInfo,
83 std::vector<RemoteAbilityInfo> &remoteAbilityInfos)
84 {
85 APP_LOGD("DistributedBmsProxy GetRemoteAbilityInfosWithLocale");
86 for (const auto &elementName : elementNames) {
87 int32_t checkRet = CheckElementName(elementName);
88 if (checkRet != ERR_OK) {
89 APP_LOGE("DistributedBmsProxy GetRemoteAbilityInfosWithLocale check elementName failed");
90 return checkRet;
91 }
92 }
93 MessageParcel data;
94 MessageParcel reply;
95 if (!data.WriteInterfaceToken(GetDescriptor())) {
96 APP_LOGE("fail to GetRemoteAbilityInfos due to write InterfaceToken fail");
97 return ERR_APPEXECFWK_PARCEL_ERROR;
98 }
99 if (!WriteParcelableVector(elementNames, data)) {
100 APP_LOGE("DistributedBmsProxy GetRemoteAbilityInfos write elementName error");
101 return ERR_APPEXECFWK_PARCEL_ERROR;
102 }
103 if (!data.WriteString(localeInfo)) {
104 APP_LOGE("DistributedBmsProxy GetRemoteAbilityInfos write localeInfo error");
105 return ERR_APPEXECFWK_PARCEL_ERROR;
106 }
107
108 int32_t result = GetParcelableInfos<RemoteAbilityInfo>(
109 DistributedInterfaceCode::GET_REMOTE_ABILITY_INFOS_WITH_LOCALE, data, remoteAbilityInfos);
110 if (result != OHOS::NO_ERROR) {
111 APP_LOGE("fail to query remote ability infos mutiparam from server");
112 }
113 return result;
114 }
115
GetAbilityInfo(const OHOS::AppExecFwk::ElementName & elementName,RemoteAbilityInfo & remoteAbilityInfo)116 int32_t DistributedBmsProxy::GetAbilityInfo(
117 const OHOS::AppExecFwk::ElementName &elementName, RemoteAbilityInfo &remoteAbilityInfo)
118 {
119 APP_LOGD("DistributedBmsProxy GetAbilityInfo");
120 return GetAbilityInfo(elementName, "", remoteAbilityInfo);
121 }
122
GetAbilityInfo(const OHOS::AppExecFwk::ElementName & elementName,const std::string & localeInfo,RemoteAbilityInfo & remoteAbilityInfo)123 int32_t DistributedBmsProxy::GetAbilityInfo(const OHOS::AppExecFwk::ElementName &elementName,
124 const std::string &localeInfo,
125 RemoteAbilityInfo &remoteAbilityInfo)
126 {
127 APP_LOGD("DistributedBmsProxy GetAbilityInfoWithLocale");
128 MessageParcel data;
129 MessageParcel reply;
130 if (!data.WriteInterfaceToken(GetDescriptor())) {
131 APP_LOGE("fail to GetAbilityInfo due to write InterfaceToken fail");
132 return ERR_APPEXECFWK_PARCEL_ERROR;
133 }
134 if (!data.WriteParcelable(&elementName)) {
135 APP_LOGE("DistributedBmsProxy GetAbilityInfo write elementName error");
136 return ERR_APPEXECFWK_PARCEL_ERROR;
137 }
138 if (!data.WriteString(localeInfo)) {
139 APP_LOGE("DistributedBmsProxy GetRemoteAbilityInfos write localeInfo error");
140 return ERR_APPEXECFWK_PARCEL_ERROR;
141 }
142 int32_t result = GetParcelableInfo<RemoteAbilityInfo>(
143 DistributedInterfaceCode::GET_ABILITY_INFO_WITH_LOCALE, data, remoteAbilityInfo);
144 if (result == OHOS::IPC_STUB_UNKNOW_TRANS_ERR) {
145 return GetParcelableInfo<RemoteAbilityInfo>(
146 DistributedInterfaceCode::GET_ABILITY_INFO, data, remoteAbilityInfo);
147 }
148 return result;
149 }
150
GetAbilityInfos(const std::vector<ElementName> & elementNames,std::vector<RemoteAbilityInfo> & remoteAbilityInfos)151 int32_t DistributedBmsProxy::GetAbilityInfos(
152 const std::vector<ElementName> &elementNames, std::vector<RemoteAbilityInfo> &remoteAbilityInfos)
153 {
154 APP_LOGD("DistributedBmsProxy GetAbilityInfos");
155 return GetAbilityInfos(elementNames, "", remoteAbilityInfos);
156 }
157
GetAbilityInfos(const std::vector<ElementName> & elementNames,const std::string & localeInfo,std::vector<RemoteAbilityInfo> & remoteAbilityInfos)158 int32_t DistributedBmsProxy::GetAbilityInfos(const std::vector<ElementName> &elementNames,
159 const std::string &localeInfo,
160 std::vector<RemoteAbilityInfo> &remoteAbilityInfos)
161 {
162 APP_LOGD("DistributedBmsProxy GetAbilityInfos");
163 MessageParcel data;
164 MessageParcel reply;
165 if (!data.WriteInterfaceToken(GetDescriptor())) {
166 APP_LOGE("fail to GetAbilityInfos due to write InterfaceToken fail");
167 return ERR_APPEXECFWK_PARCEL_ERROR;
168 }
169 if (!WriteParcelableVector(elementNames, data)) {
170 APP_LOGE("DistributedBmsProxy GetAbilityInfos write elementName error");
171 return ERR_APPEXECFWK_PARCEL_ERROR;
172 }
173 if (!data.WriteString(localeInfo)) {
174 APP_LOGE("DistributedBmsProxy GetRemoteAbilityInfos write localeInfo error");
175 return ERR_APPEXECFWK_PARCEL_ERROR;
176 }
177 int32_t result = GetParcelableInfos<RemoteAbilityInfo>(
178 DistributedInterfaceCode::GET_ABILITY_INFOS_WITH_LOCALE, data, remoteAbilityInfos);
179 if (result == OHOS::IPC_STUB_UNKNOW_TRANS_ERR) {
180 return GetParcelableInfos<RemoteAbilityInfo>(
181 DistributedInterfaceCode::GET_ABILITY_INFOS, data, remoteAbilityInfos);
182 }
183 return result;
184 }
185
GetDistributedBundleInfo(const std::string & networkId,const std::string & bundleName,DistributedBundleInfo & distributedBundleInfo)186 bool DistributedBmsProxy::GetDistributedBundleInfo(const std::string &networkId, const std::string &bundleName,
187 DistributedBundleInfo &distributedBundleInfo)
188 {
189 APP_LOGD("DistributedBmsProxy GetDistributedBundleInfo");
190 MessageParcel data;
191 if (!data.WriteInterfaceToken(GetDescriptor())) {
192 APP_LOGE("fail to GetDistributedBundleInfo due to write InterfaceToken fail");
193 return false;
194 }
195 if (!data.WriteString(networkId)) {
196 APP_LOGE("DistributedBmsProxy GetDistributedBundleInfo write networkId error");
197 return false;
198 }
199 if (!data.WriteString(bundleName)) {
200 APP_LOGE("DistributedBmsProxy GetDistributedBundleInfo write bundleName error");
201 return false;
202 }
203 int32_t result = GetParcelableInfo<DistributedBundleInfo>(
204 DistributedInterfaceCode::GET_DISTRIBUTED_BUNDLE_INFO, data, distributedBundleInfo);
205 if (result == OHOS::NO_ERROR) {
206 return true;
207 }
208 return false;
209 }
210
GetDistributedBundleName(const std::string & networkId,uint32_t accessTokenId,std::string & bundleName)211 int32_t DistributedBmsProxy::GetDistributedBundleName(const std::string &networkId, uint32_t accessTokenId,
212 std::string &bundleName)
213 {
214 MessageParcel data;
215 if (!data.WriteInterfaceToken(GetDescriptor())) {
216 APP_LOGE("fail to get distributed bundleInfo list due to write InterfaceToken fail");
217 return ERR_APPEXECFWK_PARCEL_ERROR;
218 }
219 if (!data.WriteString(networkId)) {
220 APP_LOGE("distributed mms proxy get distributed bundleInfo list write networkId error");
221 return ERR_APPEXECFWK_PARCEL_ERROR;
222 }
223 if (!data.WriteUint32(accessTokenId)) {
224 APP_LOGE("distributed mms proxy get distributed bundleInfo list write accessTokenId error");
225 return ERR_APPEXECFWK_PARCEL_ERROR;
226 }
227 MessageParcel reply;
228 int32_t result = SendRequest(DistributedInterfaceCode::GET_DISTRIBUTED_BUNDLE_NAME, data, reply);
229 if (result == OHOS::NO_ERROR) {
230 bundleName = reply.ReadString();
231 }
232 return result;
233 }
234
235 template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,Parcel & data)236 bool DistributedBmsProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, Parcel &data)
237 {
238 if (!data.WriteInt32(parcelableVector.size())) {
239 APP_LOGE("write ParcelableVector failed");
240 return false;
241 }
242
243 for (auto &parcelable : parcelableVector) {
244 if (!data.WriteParcelable(&parcelable)) {
245 APP_LOGE("write ParcelableVector failed");
246 return false;
247 }
248 }
249 return true;
250 }
251
252 template<typename T>
GetParcelableInfo(DistributedInterfaceCode code,MessageParcel & data,T & parcelableInfo)253 int32_t DistributedBmsProxy::GetParcelableInfo(DistributedInterfaceCode code, MessageParcel &data, T &parcelableInfo)
254 {
255 MessageParcel reply;
256 int32_t result = SendRequest(code, data, reply);
257 if (result != OHOS::NO_ERROR) {
258 APP_LOGE("reply result false");
259 return result;
260 }
261
262 if (!reply.ReadBool()) {
263 APP_LOGE("reply result false");
264 return ERR_APPEXECFWK_PARCEL_ERROR;
265 }
266
267 std::unique_ptr<T> info(reply.ReadParcelable<T>());
268 if (!info) {
269 APP_LOGE("readParcelableInfo failed");
270 return ERR_APPEXECFWK_PARCEL_ERROR;
271 }
272 parcelableInfo = *info;
273 APP_LOGD("get parcelable info success");
274 return OHOS::NO_ERROR;
275 }
276
277 template<typename T>
GetParcelableInfos(DistributedInterfaceCode code,MessageParcel & data,std::vector<T> & parcelableInfos)278 int32_t DistributedBmsProxy::GetParcelableInfos(
279 DistributedInterfaceCode code, MessageParcel &data, std::vector<T> &parcelableInfos)
280 {
281 MessageParcel reply;
282 int32_t result = SendRequest(code, data, reply);
283 if (result != OHOS::NO_ERROR) {
284 APP_LOGE("reply result false");
285 return result;
286 }
287
288 if (!reply.ReadBool()) {
289 APP_LOGE("reply result false");
290 return ERR_APPEXECFWK_PARCEL_ERROR;
291 }
292
293 int32_t infoSize = reply.ReadInt32();
294 CONTAINER_SECURITY_VERIFY(reply, infoSize, &parcelableInfos);
295 for (int32_t i = 0; i < infoSize; i++) {
296 std::unique_ptr<T> info(reply.ReadParcelable<T>());
297 if (!info) {
298 APP_LOGE("Read Parcelable infos failed");
299 return ERR_APPEXECFWK_PARCEL_ERROR;
300 }
301 parcelableInfos.emplace_back(*info);
302 }
303 APP_LOGD("get parcelable infos success");
304 return OHOS::NO_ERROR;
305 }
306
SendRequest(DistributedInterfaceCode code,MessageParcel & data,MessageParcel & reply)307 int32_t DistributedBmsProxy::SendRequest(DistributedInterfaceCode code, MessageParcel &data, MessageParcel &reply)
308 {
309 APP_LOGD("DistributedBmsProxy SendRequest");
310 sptr<IRemoteObject> remote = Remote();
311 MessageOption option(MessageOption::TF_SYNC);
312 if (remote == nullptr) {
313 APP_LOGE("fail to send %{public}d cmd to service due to remote object is null", code);
314 return ERR_APPEXECFWK_FAILED_GET_REMOTE_PROXY;
315 }
316 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
317 if (result != OHOS::NO_ERROR) {
318 APP_LOGE("fail to send %{public}d cmd to service due to transact error:%{public}d", code, result);
319 }
320 return result;
321 }
322
CheckElementName(const ElementName & elementName)323 int32_t DistributedBmsProxy::CheckElementName(const ElementName &elementName)
324 {
325 if (elementName.GetBundleName().empty()) {
326 APP_LOGE("fail to GetRemoteAbilityInfo due to bundleName empty");
327 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
328 }
329 if (elementName.GetAbilityName().empty()) {
330 APP_LOGE("fail to GetRemoteAbilityInfo due to abilityName empty");
331 return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
332 }
333 if (elementName.GetDeviceID().empty()) {
334 APP_LOGE("fail to GetRemoteAbilityInfo due to devicedID empty");
335 return ERR_BUNDLE_MANAGER_DEVICE_ID_NOT_EXIST;
336 }
337 return ERR_OK;
338 }
339 } // namespace AppExecFwk
340 } // namespace OHOS