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,DistributedBmsAclInfo * info)123 int32_t DistributedBmsProxy::GetAbilityInfo(const OHOS::AppExecFwk::ElementName &elementName,
124 const std::string &localeInfo,
125 RemoteAbilityInfo &remoteAbilityInfo,
126 DistributedBmsAclInfo *info)
127 {
128 APP_LOGD("DistributedBmsProxy GetAbilityInfoWithLocale");
129 MessageParcel data;
130 MessageParcel reply;
131 if (!data.WriteInterfaceToken(GetDescriptor())) {
132 APP_LOGE("fail to GetAbilityInfo due to write InterfaceToken fail");
133 return ERR_APPEXECFWK_PARCEL_ERROR;
134 }
135 if (!data.WriteParcelable(&elementName)) {
136 APP_LOGE("DistributedBmsProxy GetAbilityInfo write elementName error");
137 return ERR_APPEXECFWK_PARCEL_ERROR;
138 }
139 if (!data.WriteString(localeInfo)) {
140 APP_LOGE("DistributedBmsProxy GetAbilityInfo write localeInfo error");
141 return ERR_APPEXECFWK_PARCEL_ERROR;
142 }
143 bool hasInfo = true;
144 if (info == nullptr) {
145 hasInfo = false;
146 }
147 if (!data.WriteBool(hasInfo)) {
148 APP_LOGE("DistributedBmsProxy GetAbilityInfo write hasInfo error");
149 return ERR_APPEXECFWK_PARCEL_ERROR;
150 }
151 if (hasInfo && !data.WriteParcelable(info)) {
152 APP_LOGE("DistributedBmsProxy GetAbilityInfo write info error or info is null");
153 return ERR_APPEXECFWK_PARCEL_ERROR;
154 }
155 int32_t result = GetParcelableInfo<RemoteAbilityInfo>(
156 DistributedInterfaceCode::GET_ABILITY_INFO_WITH_LOCALE, data, remoteAbilityInfo);
157 if (result == OHOS::IPC_STUB_UNKNOW_TRANS_ERR) {
158 return GetParcelableInfo<RemoteAbilityInfo>(
159 DistributedInterfaceCode::GET_ABILITY_INFO, data, remoteAbilityInfo);
160 }
161 return result;
162 }
163
GetAbilityInfos(const std::vector<ElementName> & elementNames,std::vector<RemoteAbilityInfo> & remoteAbilityInfos)164 int32_t DistributedBmsProxy::GetAbilityInfos(
165 const std::vector<ElementName> &elementNames, std::vector<RemoteAbilityInfo> &remoteAbilityInfos)
166 {
167 APP_LOGD("DistributedBmsProxy GetAbilityInfos");
168 return GetAbilityInfos(elementNames, "", remoteAbilityInfos);
169 }
170
GetAbilityInfos(const std::vector<ElementName> & elementNames,const std::string & localeInfo,std::vector<RemoteAbilityInfo> & remoteAbilityInfos,DistributedBmsAclInfo * info)171 int32_t DistributedBmsProxy::GetAbilityInfos(const std::vector<ElementName> &elementNames,
172 const std::string &localeInfo,
173 std::vector<RemoteAbilityInfo> &remoteAbilityInfos,
174 DistributedBmsAclInfo *info)
175 {
176 APP_LOGD("DistributedBmsProxy GetAbilityInfos");
177 MessageParcel data;
178 MessageParcel reply;
179 if (!data.WriteInterfaceToken(GetDescriptor())) {
180 APP_LOGE("fail to GetAbilityInfos due to write InterfaceToken fail");
181 return ERR_APPEXECFWK_PARCEL_ERROR;
182 }
183 if (!WriteParcelableVector(elementNames, data)) {
184 APP_LOGE("DistributedBmsProxy GetAbilityInfos write elementName error");
185 return ERR_APPEXECFWK_PARCEL_ERROR;
186 }
187 if (!data.WriteString(localeInfo)) {
188 APP_LOGE("DistributedBmsProxy GetAbilityInfos write localeInfo error");
189 return ERR_APPEXECFWK_PARCEL_ERROR;
190 }
191 bool hasInfo = true;
192 if (info == nullptr) {
193 hasInfo = false;
194 }
195 if (!data.WriteBool(hasInfo)) {
196 APP_LOGE("DistributedBmsProxy GetAbilityInfos write hasInfo error");
197 return ERR_APPEXECFWK_PARCEL_ERROR;
198 }
199 if (hasInfo && !data.WriteParcelable(info)) {
200 APP_LOGE("DistributedBmsProxy GetAbilityInfos write info error or info is null");
201 return ERR_APPEXECFWK_PARCEL_ERROR;
202 }
203 int32_t result = GetParcelableInfos<RemoteAbilityInfo>(
204 DistributedInterfaceCode::GET_ABILITY_INFOS_WITH_LOCALE, data, remoteAbilityInfos);
205 if (result == OHOS::IPC_STUB_UNKNOW_TRANS_ERR) {
206 return GetParcelableInfos<RemoteAbilityInfo>(
207 DistributedInterfaceCode::GET_ABILITY_INFOS, data, remoteAbilityInfos);
208 }
209 return result;
210 }
211
GetDistributedBundleInfo(const std::string & networkId,const std::string & bundleName,DistributedBundleInfo & distributedBundleInfo)212 bool DistributedBmsProxy::GetDistributedBundleInfo(const std::string &networkId, const std::string &bundleName,
213 DistributedBundleInfo &distributedBundleInfo)
214 {
215 APP_LOGD("DistributedBmsProxy GetDistributedBundleInfo");
216 MessageParcel data;
217 if (!data.WriteInterfaceToken(GetDescriptor())) {
218 APP_LOGE("fail to GetDistributedBundleInfo due to write InterfaceToken fail");
219 return false;
220 }
221 if (!data.WriteString(networkId)) {
222 APP_LOGE("DistributedBmsProxy GetDistributedBundleInfo write networkId error");
223 return false;
224 }
225 if (!data.WriteString(bundleName)) {
226 APP_LOGE("DistributedBmsProxy GetDistributedBundleInfo write bundleName error");
227 return false;
228 }
229 int32_t result = GetParcelableInfo<DistributedBundleInfo>(
230 DistributedInterfaceCode::GET_DISTRIBUTED_BUNDLE_INFO, data, distributedBundleInfo);
231 if (result == OHOS::NO_ERROR) {
232 return true;
233 }
234 return false;
235 }
236
GetDistributedBundleName(const std::string & networkId,uint32_t accessTokenId,std::string & bundleName)237 int32_t DistributedBmsProxy::GetDistributedBundleName(const std::string &networkId, uint32_t accessTokenId,
238 std::string &bundleName)
239 {
240 MessageParcel data;
241 if (!data.WriteInterfaceToken(GetDescriptor())) {
242 APP_LOGE("fail to get distributed bundleInfo list due to write InterfaceToken fail");
243 return ERR_APPEXECFWK_PARCEL_ERROR;
244 }
245 if (!data.WriteString(networkId)) {
246 APP_LOGE("distributed mms proxy get distributed bundleInfo list write networkId error");
247 return ERR_APPEXECFWK_PARCEL_ERROR;
248 }
249 if (!data.WriteUint32(accessTokenId)) {
250 APP_LOGE("distributed mms proxy get distributed bundleInfo list write accessTokenId error");
251 return ERR_APPEXECFWK_PARCEL_ERROR;
252 }
253 MessageParcel reply;
254 int32_t result = SendRequest(DistributedInterfaceCode::GET_DISTRIBUTED_BUNDLE_NAME, data, reply);
255 if (result == OHOS::NO_ERROR) {
256 bundleName = reply.ReadString();
257 }
258 return result;
259 }
260
261 template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,Parcel & data)262 bool DistributedBmsProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, Parcel &data)
263 {
264 if (!data.WriteInt32(parcelableVector.size())) {
265 APP_LOGE("write ParcelableVector failed");
266 return false;
267 }
268
269 for (auto &parcelable : parcelableVector) {
270 if (!data.WriteParcelable(&parcelable)) {
271 APP_LOGE("write ParcelableVector failed");
272 return false;
273 }
274 }
275 return true;
276 }
277
278 template<typename T>
GetParcelableInfo(DistributedInterfaceCode code,MessageParcel & data,T & parcelableInfo)279 int32_t DistributedBmsProxy::GetParcelableInfo(DistributedInterfaceCode code, MessageParcel &data, T &parcelableInfo)
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 std::unique_ptr<T> info(reply.ReadParcelable<T>());
294 if (!info) {
295 APP_LOGE("readParcelableInfo failed");
296 return ERR_APPEXECFWK_PARCEL_ERROR;
297 }
298 parcelableInfo = *info;
299 APP_LOGD("get parcelable info success");
300 return OHOS::NO_ERROR;
301 }
302
303 template<typename T>
GetParcelableInfos(DistributedInterfaceCode code,MessageParcel & data,std::vector<T> & parcelableInfos)304 int32_t DistributedBmsProxy::GetParcelableInfos(
305 DistributedInterfaceCode code, MessageParcel &data, std::vector<T> &parcelableInfos)
306 {
307 MessageParcel reply;
308 int32_t result = SendRequest(code, data, reply);
309 if (result != OHOS::NO_ERROR) {
310 APP_LOGE("reply result false");
311 return result;
312 }
313
314 if (!reply.ReadBool()) {
315 APP_LOGE("reply result false");
316 return ERR_APPEXECFWK_PARCEL_ERROR;
317 }
318
319 int32_t infoSize = reply.ReadInt32();
320 CONTAINER_SECURITY_VERIFY(reply, infoSize, &parcelableInfos);
321 for (int32_t i = 0; i < infoSize; i++) {
322 std::unique_ptr<T> info(reply.ReadParcelable<T>());
323 if (!info) {
324 APP_LOGE("Read Parcelable infos failed");
325 return ERR_APPEXECFWK_PARCEL_ERROR;
326 }
327 parcelableInfos.emplace_back(*info);
328 }
329 APP_LOGD("get parcelable infos success");
330 return OHOS::NO_ERROR;
331 }
332
SendRequest(DistributedInterfaceCode code,MessageParcel & data,MessageParcel & reply)333 int32_t DistributedBmsProxy::SendRequest(DistributedInterfaceCode code, MessageParcel &data, MessageParcel &reply)
334 {
335 APP_LOGD("DistributedBmsProxy SendRequest");
336 sptr<IRemoteObject> remote = Remote();
337 MessageOption option(MessageOption::TF_SYNC);
338 if (remote == nullptr) {
339 APP_LOGE("fail to send %{public}d cmd to service due to remote object is null", code);
340 return ERR_APPEXECFWK_FAILED_GET_REMOTE_PROXY;
341 }
342 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
343 if (result != OHOS::NO_ERROR) {
344 APP_LOGE("fail to send %{public}d cmd to service due to transact error:%{public}d", code, result);
345 }
346 return result;
347 }
348
CheckElementName(const ElementName & elementName)349 int32_t DistributedBmsProxy::CheckElementName(const ElementName &elementName)
350 {
351 if (elementName.GetBundleName().empty()) {
352 APP_LOGE("fail to GetRemoteAbilityInfo due to bundleName empty");
353 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
354 }
355 if (elementName.GetAbilityName().empty()) {
356 APP_LOGE("fail to GetRemoteAbilityInfo due to abilityName empty");
357 return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
358 }
359 if (elementName.GetDeviceID().empty()) {
360 APP_LOGE("fail to GetRemoteAbilityInfo due to devicedID empty");
361 return ERR_BUNDLE_MANAGER_DEVICE_ID_NOT_EXIST;
362 }
363 return ERR_OK;
364 }
365 } // namespace AppExecFwk
366 } // namespace OHOS