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 IDistributedBms::Message::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 IDistributedBms::Message::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 IDistributedBms::Message::GET_ABILITY_INFO_WITH_LOCALE, data, remoteAbilityInfo);
144 if (result == OHOS::IPC_STUB_UNKNOW_TRANS_ERR) {
145 return GetParcelableInfo<RemoteAbilityInfo>(
146 IDistributedBms::Message::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 IDistributedBms::Message::GET_ABILITY_INFOS_WITH_LOCALE, data, remoteAbilityInfos);
179 if (result == OHOS::IPC_STUB_UNKNOW_TRANS_ERR) {
180 return GetParcelableInfos<RemoteAbilityInfo>(
181 IDistributedBms::Message::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 IDistributedBms::Message::GET_DISTRIBUTED_BUNDLE_INFO, data, distributedBundleInfo);
205 if (result == OHOS::NO_ERROR) {
206 return true;
207 }
208 return false;
209 }
210
211 template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,Parcel & data)212 bool DistributedBmsProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, Parcel &data)
213 {
214 if (!data.WriteInt32(parcelableVector.size())) {
215 APP_LOGE("write ParcelableVector failed");
216 return false;
217 }
218
219 for (auto &parcelable : parcelableVector) {
220 if (!data.WriteParcelable(&parcelable)) {
221 APP_LOGE("write ParcelableVector failed");
222 return false;
223 }
224 }
225 return true;
226 }
227
228 template<typename T>
GetParcelableInfo(IDistributedBms::Message code,MessageParcel & data,T & parcelableInfo)229 int32_t DistributedBmsProxy::GetParcelableInfo(IDistributedBms::Message code, MessageParcel &data, T &parcelableInfo)
230 {
231 MessageParcel reply;
232 int32_t result = SendRequest(code, data, reply);
233 if (result != OHOS::NO_ERROR) {
234 APP_LOGE("reply result false");
235 return result;
236 }
237
238 if (!reply.ReadBool()) {
239 APP_LOGE("reply result false");
240 return ERR_APPEXECFWK_PARCEL_ERROR;
241 }
242
243 std::unique_ptr<T> info(reply.ReadParcelable<T>());
244 if (!info) {
245 APP_LOGE("readParcelableInfo failed");
246 return ERR_APPEXECFWK_PARCEL_ERROR;
247 }
248 parcelableInfo = *info;
249 APP_LOGD("get parcelable info success");
250 return OHOS::NO_ERROR;
251 }
252
253 template<typename T>
GetParcelableInfos(IDistributedBms::Message code,MessageParcel & data,std::vector<T> & parcelableInfos)254 int32_t DistributedBmsProxy::GetParcelableInfos(
255 IDistributedBms::Message code, MessageParcel &data, std::vector<T> &parcelableInfos)
256 {
257 MessageParcel reply;
258 int32_t result = SendRequest(code, data, reply);
259 if (result != OHOS::NO_ERROR) {
260 APP_LOGE("reply result false");
261 return result;
262 }
263
264 if (!reply.ReadBool()) {
265 APP_LOGE("reply result false");
266 return ERR_APPEXECFWK_PARCEL_ERROR;
267 }
268
269 int32_t infoSize = reply.ReadInt32();
270 for (int32_t i = 0; i < infoSize; i++) {
271 std::unique_ptr<T> info(reply.ReadParcelable<T>());
272 if (!info) {
273 APP_LOGE("Read Parcelable infos failed");
274 return ERR_APPEXECFWK_PARCEL_ERROR;
275 }
276 parcelableInfos.emplace_back(*info);
277 }
278 APP_LOGD("get parcelable infos success");
279 return OHOS::NO_ERROR;
280 }
281
SendRequest(IDistributedBms::Message code,MessageParcel & data,MessageParcel & reply)282 int32_t DistributedBmsProxy::SendRequest(IDistributedBms::Message code, MessageParcel &data, MessageParcel &reply)
283 {
284 APP_LOGD("DistributedBmsProxy SendRequest");
285 sptr<IRemoteObject> remote = Remote();
286 MessageOption option(MessageOption::TF_SYNC);
287 if (remote == nullptr) {
288 APP_LOGE("fail to send %{public}d cmd to service due to remote object is null", code);
289 return ERR_APPEXECFWK_FAILED_GET_REMOTE_PROXY;
290 }
291 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
292 if (result != OHOS::NO_ERROR) {
293 APP_LOGE("fail to send %{public}d cmd to service due to transact error:%{public}d", code, result);
294 }
295 return result;
296 }
297
CheckElementName(const ElementName & elementName)298 int32_t DistributedBmsProxy::CheckElementName(const ElementName &elementName)
299 {
300 if (elementName.GetBundleName().empty()) {
301 APP_LOGE("fail to GetRemoteAbilityInfo due to bundleName empty");
302 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
303 }
304 if (elementName.GetAbilityName().empty()) {
305 APP_LOGE("fail to GetRemoteAbilityInfo due to abilityName empty");
306 return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
307 }
308 if (elementName.GetDeviceID().empty()) {
309 APP_LOGE("fail to GetRemoteAbilityInfo due to devicedID empty");
310 return ERR_BUNDLE_MANAGER_DEVICE_ID_NOT_EXIST;
311 }
312 return ERR_OK;
313 }
314 } // namespace AppExecFwk
315 } // namespace OHOS