• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "bundle_mgr_proxy.h"
17 
18 #include <numeric>
19 #include <unistd.h>
20 
21 #include "ipc_types.h"
22 #include "parcel.h"
23 #include "string_ex.h"
24 
25 #include "app_log_wrapper.h"
26 #include "appexecfwk_errors.h"
27 #include "bundle_constants.h"
28 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
29 #include "default_app_proxy.h"
30 #endif
31 #include "hitrace_meter.h"
32 #include "json_util.h"
33 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
34 #include "quick_fix_manager_proxy.h"
35 #endif
36 #include "securec.h"
37 
38 namespace OHOS {
39 namespace AppExecFwk {
40 namespace {
41 const int32_t ASHMEM_LEN = 16;
42 
ClearAshmem(sptr<Ashmem> & optMem)43 inline void ClearAshmem(sptr<Ashmem> &optMem)
44 {
45     if (optMem != nullptr) {
46         optMem->UnmapAshmem();
47         optMem->CloseAshmem();
48     }
49 }
50 
ParseStr(const char * buf,const int itemLen,int index,std::string & result)51 bool ParseStr(const char *buf, const int itemLen, int index, std::string &result)
52 {
53     APP_LOGD("ParseStr itemLen:%{public}d index:%{public}d.", itemLen, index);
54     if (buf == nullptr || itemLen <= 0 || index < 0) {
55         APP_LOGE("param invalid.");
56         return false;
57     }
58 
59     char item[itemLen + 1];
60     if (strncpy_s(item, sizeof(item), buf + index, itemLen) != 0) {
61         APP_LOGE("ParseStr failed due to strncpy_s error.");
62         return false;
63     }
64 
65     std::string str(item, 0, itemLen);
66     result = str;
67     return true;
68 }
69 }
BundleMgrProxy(const sptr<IRemoteObject> & impl)70 BundleMgrProxy::BundleMgrProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IBundleMgr>(impl)
71 {
72     APP_LOGD("create bundle mgr proxy instance");
73 }
74 
~BundleMgrProxy()75 BundleMgrProxy::~BundleMgrProxy()
76 {
77     APP_LOGD("destroy create bundle mgr proxy instance");
78 }
79 
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)80 bool BundleMgrProxy::GetApplicationInfo(
81     const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo)
82 {
83     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
84     APP_LOGD("begin to GetApplicationInfo of %{public}s", appName.c_str());
85     if (appName.empty()) {
86         APP_LOGE("fail to GetApplicationInfo due to params empty");
87         return false;
88     }
89 
90     MessageParcel data;
91     if (!data.WriteInterfaceToken(GetDescriptor())) {
92         APP_LOGE("fail to GetApplicationInfo due to write descriptor fail");
93         return false;
94     }
95     if (!data.WriteString(appName)) {
96         APP_LOGE("fail to GetApplicationInfo due to write appName fail");
97         return false;
98     }
99     if (!data.WriteInt32(static_cast<int>(flag))) {
100         APP_LOGE("fail to GetApplicationInfo due to write flag fail");
101         return false;
102     }
103     if (!data.WriteInt32(userId)) {
104         APP_LOGE("fail to GetApplicationInfo due to write userId fail");
105         return false;
106     }
107 
108     if (!GetParcelableInfo<ApplicationInfo>(IBundleMgr::Message::GET_APPLICATION_INFO, data, appInfo)) {
109         APP_LOGE("fail to GetApplicationInfo from server");
110         return false;
111     }
112     return true;
113 }
114 
GetApplicationInfo(const std::string & appName,int32_t flags,int32_t userId,ApplicationInfo & appInfo)115 bool BundleMgrProxy::GetApplicationInfo(
116     const std::string &appName, int32_t flags, int32_t userId, ApplicationInfo &appInfo)
117 {
118     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
119     APP_LOGD("begin to GetApplicationInfo of %{public}s", appName.c_str());
120     if (appName.empty()) {
121         APP_LOGE("fail to GetApplicationInfo due to params empty");
122         return false;
123     }
124 
125     MessageParcel data;
126     if (!data.WriteInterfaceToken(GetDescriptor())) {
127         APP_LOGE("fail to GetApplicationInfo due to write MessageParcel fail");
128         return false;
129     }
130     if (!data.WriteString(appName)) {
131         APP_LOGE("fail to GetApplicationInfo due to write appName fail");
132         return false;
133     }
134     if (!data.WriteInt32(flags)) {
135         APP_LOGE("fail to GetApplicationInfo due to write flag fail");
136         return false;
137     }
138     if (!data.WriteInt32(userId)) {
139         APP_LOGE("fail to GetApplicationInfo due to write userId fail");
140         return false;
141     }
142 
143     if (!GetParcelableInfo<ApplicationInfo>(IBundleMgr::Message::GET_APPLICATION_INFO_WITH_INT_FLAGS, data, appInfo)) {
144         APP_LOGE("fail to GetApplicationInfo from server");
145         return false;
146     }
147     return true;
148 }
149 
GetApplicationInfoV9(const std::string & appName,int32_t flags,int32_t userId,ApplicationInfo & appInfo)150 ErrCode BundleMgrProxy::GetApplicationInfoV9(
151     const std::string &appName, int32_t flags, int32_t userId, ApplicationInfo &appInfo)
152 {
153     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
154     APP_LOGD("begin to GetApplicationInfoV9 of %{public}s", appName.c_str());
155     if (appName.empty()) {
156         APP_LOGE("fail to GetApplicationInfoV9 due to params empty");
157         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
158     }
159 
160     MessageParcel data;
161     if (!data.WriteInterfaceToken(GetDescriptor())) {
162         APP_LOGE("fail to GetApplicationInfoV9 due to write MessageParcel fail");
163         return ERR_APPEXECFWK_PARCEL_ERROR;
164     }
165     if (!data.WriteString(appName)) {
166         APP_LOGE("fail to GetApplicationInfoV9 due to write appName fail");
167         return ERR_APPEXECFWK_PARCEL_ERROR;
168     }
169     if (!data.WriteInt32(flags)) {
170         APP_LOGE("fail to GetApplicationInfoV9 due to write flag fail");
171         return ERR_APPEXECFWK_PARCEL_ERROR;
172     }
173     if (!data.WriteInt32(userId)) {
174         APP_LOGE("fail to GetApplicationInfoV9 due to write userId fail");
175         return ERR_APPEXECFWK_PARCEL_ERROR;
176     }
177 
178     auto res = GetParcelableInfoWithErrCode<ApplicationInfo>(
179         IBundleMgr::Message::GET_APPLICATION_INFO_WITH_INT_FLAGS_V9, data, appInfo);
180     if (res != ERR_OK) {
181         APP_LOGE("fail to GetApplicationInfoV9 from server, error code: %{public}d", res);
182         return res;
183     }
184     return ERR_OK;
185 }
186 
GetApplicationInfos(const ApplicationFlag flag,int userId,std::vector<ApplicationInfo> & appInfos)187 bool BundleMgrProxy::GetApplicationInfos(
188     const ApplicationFlag flag, int userId, std::vector<ApplicationInfo> &appInfos)
189 {
190     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
191     APP_LOGD("begin to get GetApplicationInfos of specific userId id %{private}d", userId);
192     MessageParcel data;
193     if (!data.WriteInterfaceToken(GetDescriptor())) {
194         APP_LOGE("fail to GetApplicationInfo due to write descriptor fail");
195         return false;
196     }
197     if (!data.WriteInt32(static_cast<int>(flag))) {
198         APP_LOGE("fail to GetApplicationInfo due to write flag fail");
199         return false;
200     }
201     if (!data.WriteInt32(userId)) {
202         APP_LOGE("fail to GetApplicationInfos due to write userId error");
203         return false;
204     }
205 
206     if (!GetParcelableInfos<ApplicationInfo>(IBundleMgr::Message::GET_APPLICATION_INFOS, data, appInfos)) {
207         APP_LOGE("fail to GetApplicationInfos from server");
208         return false;
209     }
210     return true;
211 }
212 
GetApplicationInfos(int32_t flags,int32_t userId,std::vector<ApplicationInfo> & appInfos)213 bool BundleMgrProxy::GetApplicationInfos(
214     int32_t flags, int32_t userId, std::vector<ApplicationInfo> &appInfos)
215 {
216     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
217     APP_LOGD("begin to get GetApplicationInfos of specific userId id %{private}d", userId);
218     MessageParcel data;
219     if (!data.WriteInterfaceToken(GetDescriptor())) {
220         APP_LOGE("fail to GetApplicationInfo due to write MessageParcel fail");
221         return false;
222     }
223     if (!data.WriteInt32(flags)) {
224         APP_LOGE("fail to GetApplicationInfo due to write flag fail");
225         return false;
226     }
227     if (!data.WriteInt32(userId)) {
228         APP_LOGE("fail to GetApplicationInfos due to write userId error");
229         return false;
230     }
231 
232     MessageParcel reply;
233     if (!SendTransactCmd(IBundleMgr::Message::GET_APPLICATION_INFOS_WITH_INT_FLAGS, data, reply)) {
234         APP_LOGE("sendTransactCmd failed");
235         return false;
236     }
237     if (!reply.ReadBool()) {
238         APP_LOGE("host returns error");
239         return false;
240     }
241     if (!GetParcelableInfosFromAshmem<ApplicationInfo>(reply, appInfos)) {
242         APP_LOGE("failed to GetApplicationInfos from server");
243         return false;
244     }
245     return true;
246 }
247 
GetApplicationInfosV9(int32_t flags,int32_t userId,std::vector<ApplicationInfo> & appInfos)248 ErrCode BundleMgrProxy::GetApplicationInfosV9(
249     int32_t flags, int32_t userId, std::vector<ApplicationInfo> &appInfos)
250 {
251     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
252     APP_LOGD("begin to get GetApplicationInfosV9 of specific userId id %{private}d", userId);
253     MessageParcel data;
254     if (!data.WriteInterfaceToken(GetDescriptor())) {
255         APP_LOGE("fail to GetApplicationInfosV9 due to write MessageParcel fail");
256         return ERR_APPEXECFWK_PARCEL_ERROR;
257     }
258     if (!data.WriteInt32(flags)) {
259         APP_LOGE("fail to GetApplicationInfosV9 due to write flag fail");
260         return ERR_APPEXECFWK_PARCEL_ERROR;
261     }
262     if (!data.WriteInt32(userId)) {
263         APP_LOGE("fail to GetApplicationInfosV9 due to write userId error");
264         return ERR_APPEXECFWK_PARCEL_ERROR;
265     }
266 
267     MessageParcel reply;
268     if (!SendTransactCmd(IBundleMgr::Message::GET_APPLICATION_INFOS_WITH_INT_FLAGS_V9, data, reply)) {
269         APP_LOGE("sendTransactCmd failed");
270         return ERR_APPEXECFWK_PARCEL_ERROR;
271     }
272     auto res = reply.ReadInt32();
273     if (res != ERR_OK) {
274         APP_LOGE("host returns error, error code: %{public}d.", res);
275         return res;
276     }
277 
278     if (!GetParcelableInfosFromAshmem<ApplicationInfo>(reply, appInfos)) {
279         APP_LOGE("failed to GetApplicationInfosV9 from server");
280         return ERR_APPEXECFWK_PARCEL_ERROR;
281     }
282     return ERR_OK;
283 }
284 
GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)285 bool BundleMgrProxy::GetBundleInfo(
286     const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId)
287 {
288     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
289     APP_LOGD("begin to get bundle info of %{public}s", bundleName.c_str());
290     if (bundleName.empty()) {
291         APP_LOGE("fail to GetBundleInfo due to params empty");
292         return false;
293     }
294 
295     MessageParcel data;
296     if (!data.WriteInterfaceToken(GetDescriptor())) {
297         APP_LOGE("fail to GetBundleInfo due to write InterfaceToken fail");
298         return false;
299     }
300     if (!data.WriteString(bundleName)) {
301         APP_LOGE("fail to GetBundleInfo due to write bundleName fail");
302         return false;
303     }
304     if (!data.WriteInt32(static_cast<int>(flag))) {
305         APP_LOGE("fail to GetBundleInfo due to write flag fail");
306         return false;
307     }
308     if (!data.WriteInt32(userId)) {
309         APP_LOGE("fail to GetBundleInfo due to write userId fail");
310         return false;
311     }
312 
313     if (!GetParcelableInfo<BundleInfo>(IBundleMgr::Message::GET_BUNDLE_INFO, data, bundleInfo)) {
314         APP_LOGE("fail to GetBundleInfo from server");
315         return false;
316     }
317     return true;
318 }
319 
GetBundleInfo(const std::string & bundleName,int32_t flags,BundleInfo & bundleInfo,int32_t userId)320 bool BundleMgrProxy::GetBundleInfo(
321     const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId)
322 {
323     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
324     APP_LOGD("begin to get bundle info of %{public}s", bundleName.c_str());
325     if (bundleName.empty()) {
326         APP_LOGE("fail to GetBundleInfo due to params empty");
327         return false;
328     }
329 
330     MessageParcel data;
331     if (!data.WriteInterfaceToken(GetDescriptor())) {
332         APP_LOGE("fail to GetBundleInfo due to write InterfaceToken fail");
333         return false;
334     }
335     if (!data.WriteString(bundleName)) {
336         APP_LOGE("fail to GetBundleInfo due to write bundleName fail");
337         return false;
338     }
339     if (!data.WriteInt32(flags)) {
340         APP_LOGE("fail to GetBundleInfo due to write flag fail");
341         return false;
342     }
343     if (!data.WriteInt32(userId)) {
344         APP_LOGE("fail to GetBundleInfo due to write userId fail");
345         return false;
346     }
347 
348     if (!GetParcelableInfo<BundleInfo>(IBundleMgr::Message::GET_BUNDLE_INFO_WITH_INT_FLAGS, data, bundleInfo)) {
349         APP_LOGE("fail to GetBundleInfo from server");
350         return false;
351     }
352     return true;
353 }
354 
GetBundleInfoV9(const std::string & bundleName,int32_t flags,BundleInfo & bundleInfo,int32_t userId)355 ErrCode BundleMgrProxy::GetBundleInfoV9(
356     const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId)
357 {
358     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
359     APP_LOGD("begin to get bundle info of %{public}s", bundleName.c_str());
360     if (bundleName.empty()) {
361         APP_LOGE("fail to GetBundleInfoV9 due to params empty");
362         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
363     }
364 
365     MessageParcel data;
366     if (!data.WriteInterfaceToken(GetDescriptor())) {
367         APP_LOGE("fail to GetBundleInfoV9 due to write InterfaceToken fail");
368         return ERR_APPEXECFWK_PARCEL_ERROR;
369     }
370     if (!data.WriteString(bundleName)) {
371         APP_LOGE("fail to GetBundleInfoV9 due to write bundleName fail");
372         return ERR_APPEXECFWK_PARCEL_ERROR;
373     }
374     if (!data.WriteInt32(flags)) {
375         APP_LOGE("fail to GetBundleInfoV9 due to write flag fail");
376         return ERR_APPEXECFWK_PARCEL_ERROR;
377     }
378     if (!data.WriteInt32(userId)) {
379         APP_LOGE("fail to GetBundleInfoV9 due to write userId fail");
380         return ERR_APPEXECFWK_PARCEL_ERROR;
381     }
382 
383     auto res = GetParcelableInfoWithErrCode<BundleInfo>(
384         IBundleMgr::Message::GET_BUNDLE_INFO_WITH_INT_FLAGS_V9, data, bundleInfo);
385     if (res != ERR_OK) {
386         APP_LOGE("fail to GetBundleInfoV9 from server, error code: %{public}d", res);
387         return res;
388     }
389     return ERR_OK;
390 }
391 
GetBundleInfoForSelf(int32_t flags,BundleInfo & bundleInfo)392 ErrCode BundleMgrProxy::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo)
393 {
394     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
395     APP_LOGD("begin to get bundle info for self");
396 
397     MessageParcel data;
398     if (!data.WriteInterfaceToken(GetDescriptor())) {
399         APP_LOGE("fail to GetBundleInfoForSelf due to write InterfaceToken fail");
400         return ERR_APPEXECFWK_PARCEL_ERROR;
401     }
402     if (!data.WriteInt32(flags)) {
403         APP_LOGE("fail to GetBundleInfoForSelf due to write flag fail");
404         return ERR_APPEXECFWK_PARCEL_ERROR;
405     }
406 
407     auto res = GetParcelableInfoWithErrCode<BundleInfo>(
408         IBundleMgr::Message::GET_BUNDLE_INFO_FOR_SELF, data, bundleInfo);
409     if (res != ERR_OK) {
410         APP_LOGE("fail to GetBundleInfoForSelf from server, error code: %{public}d", res);
411         return res;
412     }
413     return ERR_OK;
414 }
415 
GetBundlePackInfo(const std::string & bundleName,const BundlePackFlag flag,BundlePackInfo & bundlePackInfo,int32_t userId)416 ErrCode BundleMgrProxy::GetBundlePackInfo(
417     const std::string &bundleName, const BundlePackFlag flag, BundlePackInfo &bundlePackInfo, int32_t userId)
418 {
419     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
420     APP_LOGD("begin to get bundle info of %{public}s", bundleName.c_str());
421     if (bundleName.empty()) {
422         APP_LOGE("fail to GetBundlePackInfo due to params empty");
423         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
424     }
425 
426     MessageParcel data;
427     if (!data.WriteInterfaceToken(GetDescriptor())) {
428         APP_LOGE("fail to GetBundlePackInfo due to write InterfaceToken fail");
429         return ERR_APPEXECFWK_PARCEL_ERROR;
430     }
431     if (!data.WriteString(bundleName)) {
432         APP_LOGE("fail to GetBundlePackInfo due to write bundleName fail");
433         return ERR_APPEXECFWK_PARCEL_ERROR;
434     }
435     if (!data.WriteInt32(static_cast<int>(flag))) {
436         APP_LOGE("fail to GetBundlePackInfo due to write flag fail");
437         return ERR_APPEXECFWK_PARCEL_ERROR;
438     }
439     if (!data.WriteInt32(userId)) {
440         APP_LOGE("fail to GetBundlePackInfo due to write userId fail");
441         return ERR_APPEXECFWK_PARCEL_ERROR;
442     }
443 
444     return GetParcelableInfoWithErrCode<BundlePackInfo>(IBundleMgr::Message::GET_BUNDLE_PACK_INFO, data,
445         bundlePackInfo);
446 }
447 
GetBundlePackInfo(const std::string & bundleName,int32_t flags,BundlePackInfo & bundlePackInfo,int32_t userId)448 ErrCode BundleMgrProxy::GetBundlePackInfo(
449     const std::string &bundleName, int32_t flags, BundlePackInfo &bundlePackInfo, int32_t userId)
450 {
451     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
452     APP_LOGD("begin to get bundle info of %{public}s", bundleName.c_str());
453     if (bundleName.empty()) {
454         APP_LOGE("fail to GetBundlePackInfo due to params empty");
455         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
456     }
457 
458     MessageParcel data;
459     if (!data.WriteInterfaceToken(GetDescriptor())) {
460         APP_LOGE("fail to GetBundlePackInfo due to write InterfaceToken fail");
461         return ERR_APPEXECFWK_PARCEL_ERROR;
462     }
463     if (!data.WriteString(bundleName)) {
464         APP_LOGE("fail to GetBundlePackInfo due to write bundleName fail");
465         return ERR_APPEXECFWK_PARCEL_ERROR;
466     }
467     if (!data.WriteInt32(flags)) {
468         APP_LOGE("fail to GetBundlePackInfo due to write flag fail");
469         return ERR_APPEXECFWK_PARCEL_ERROR;
470     }
471     if (!data.WriteInt32(userId)) {
472         APP_LOGE("fail to GetBundlePackInfo due to write userId fail");
473         return ERR_APPEXECFWK_PARCEL_ERROR;
474     }
475 
476     return GetParcelableInfoWithErrCode<BundlePackInfo>(
477         IBundleMgr::Message::GET_BUNDLE_PACK_INFO_WITH_INT_FLAGS, data, bundlePackInfo);
478 }
479 
GetBundleInfos(const BundleFlag flag,std::vector<BundleInfo> & bundleInfos,int32_t userId)480 bool BundleMgrProxy::GetBundleInfos(
481     const BundleFlag flag, std::vector<BundleInfo> &bundleInfos, int32_t userId)
482 {
483     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
484     APP_LOGD("begin to get bundle infos");
485     MessageParcel data;
486     if (!data.WriteInterfaceToken(GetDescriptor())) {
487         APP_LOGE("fail to GetBundleInfos due to write InterfaceToken fail");
488         return false;
489     }
490     if (!data.WriteInt32(static_cast<int>(flag))) {
491         APP_LOGE("fail to GetBundleInfos due to write flag fail");
492         return false;
493     }
494     if (!data.WriteInt32(userId)) {
495         APP_LOGE("fail to GetBundleInfo due to write userId fail");
496         return false;
497     }
498 
499     MessageParcel reply;
500     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLE_INFOS, data, reply)) {
501         APP_LOGE("sendTransactCmd failed");
502         return false;
503     }
504     if (!reply.ReadBool()) {
505         APP_LOGE("host returns error");
506         return false;
507     }
508     if (!GetParcelableInfosFromAshmem<BundleInfo>(reply, bundleInfos)) {
509         APP_LOGE("fail to GetBundleInfos from server");
510         return false;
511     }
512     return true;
513 }
514 
GetBundleInfos(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)515 bool BundleMgrProxy::GetBundleInfos(
516     int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId)
517 {
518     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
519     APP_LOGD("begin to get bundle infos");
520     MessageParcel data;
521     if (!data.WriteInterfaceToken(GetDescriptor())) {
522         APP_LOGE("fail to GetBundleInfos due to write InterfaceToken fail");
523         return false;
524     }
525     if (!data.WriteInt32(flags)) {
526         APP_LOGE("fail to GetBundleInfos due to write flag fail");
527         return false;
528     }
529     if (!data.WriteInt32(userId)) {
530         APP_LOGE("fail to GetBundleInfo due to write userId fail");
531         return false;
532     }
533 
534     MessageParcel reply;
535     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLE_INFOS_WITH_INT_FLAGS, data, reply)) {
536         APP_LOGE("sendTransactCmd failed");
537         return false;
538     }
539     if (!reply.ReadBool()) {
540         APP_LOGE("host returns error");
541         return false;
542     }
543     if (!GetParcelableInfosFromAshmem<BundleInfo>(reply, bundleInfos)) {
544         APP_LOGE("fail to GetBundleInfos from server");
545         return false;
546     }
547     return true;
548 }
549 
GetBundleInfosV9(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)550 ErrCode BundleMgrProxy::GetBundleInfosV9(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId)
551 {
552     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
553     APP_LOGD("begin to get bundle infos");
554     MessageParcel data;
555     if (!data.WriteInterfaceToken(GetDescriptor())) {
556         APP_LOGE("fail to GetBundleInfosV9 due to write InterfaceToken fail");
557         return ERR_APPEXECFWK_PARCEL_ERROR;
558     }
559     if (!data.WriteInt32(flags)) {
560         APP_LOGE("fail to GetBundleInfosV9 due to write flag fail");
561         return ERR_APPEXECFWK_PARCEL_ERROR;
562     }
563     if (!data.WriteInt32(userId)) {
564         APP_LOGE("fail to GetBundleInfosV9 due to write userId fail");
565         return ERR_APPEXECFWK_PARCEL_ERROR;
566     }
567 
568     MessageParcel reply;
569     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLE_INFOS_WITH_INT_FLAGS_V9, data, reply)) {
570         APP_LOGE("sendTransactCmd failed");
571         return ERR_APPEXECFWK_PARCEL_ERROR;
572     }
573     auto res = reply.ReadInt32();
574     if (res != ERR_OK) {
575         APP_LOGE("host returns error, error code: %{public}d.", res);
576         return res;
577     }
578 
579     if (!GetParcelableInfosFromAshmem<BundleInfo>(reply, bundleInfos)) {
580         APP_LOGE("failed to GetBundleInfosV9 from server");
581         return ERR_APPEXECFWK_PARCEL_ERROR;
582     }
583     return ERR_OK;
584 }
585 
GetUidByBundleName(const std::string & bundleName,const int userId)586 int BundleMgrProxy::GetUidByBundleName(const std::string &bundleName, const int userId)
587 {
588     if (bundleName.empty()) {
589         APP_LOGE("failed to GetUidByBundleName due to bundleName empty");
590         return Constants::INVALID_UID;
591     }
592     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
593     APP_LOGD("begin to get uid of %{public}s, userId : %{public}d", bundleName.c_str(), userId);
594 
595     MessageParcel data;
596     if (!data.WriteInterfaceToken(GetDescriptor())) {
597         APP_LOGE("failed to GetUidByBundleName due to write InterfaceToken fail");
598         return Constants::INVALID_UID;
599     }
600     if (!data.WriteString(bundleName)) {
601         APP_LOGE("failed to GetUidByBundleName due to write bundleName fail");
602         return Constants::INVALID_UID;
603     }
604     if (!data.WriteInt32(userId)) {
605         APP_LOGE("failed to GetUidByBundleName due to write uid fail");
606         return Constants::INVALID_UID;
607     }
608 
609     MessageParcel reply;
610     if (!SendTransactCmd(IBundleMgr::Message::GET_UID_BY_BUNDLE_NAME, data, reply)) {
611         APP_LOGE("failed to GetUidByBundleName from server");
612         return Constants::INVALID_UID;
613     }
614     int32_t uid = reply.ReadInt32();
615     APP_LOGD("uid is %{public}d", uid);
616     return uid;
617 }
618 
GetUidByDebugBundleName(const std::string & bundleName,const int userId)619 int BundleMgrProxy::GetUidByDebugBundleName(const std::string &bundleName, const int userId)
620 {
621     if (bundleName.empty()) {
622         APP_LOGE("failed to GetUidByBundleName due to bundleName empty");
623         return Constants::INVALID_UID;
624     }
625     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
626     APP_LOGD("begin to get uid of %{public}s, userId : %{public}d", bundleName.c_str(), userId);
627 
628     MessageParcel data;
629     if (!data.WriteInterfaceToken(GetDescriptor())) {
630         APP_LOGE("failed to GetUidByBundleName due to write InterfaceToken fail");
631         return Constants::INVALID_UID;
632     }
633     if (!data.WriteString(bundleName)) {
634         APP_LOGE("failed to GetUidByBundleName due to write bundleName fail");
635         return Constants::INVALID_UID;
636     }
637     if (!data.WriteInt32(userId)) {
638         APP_LOGE("failed to GetUidByBundleName due to write uid fail");
639         return Constants::INVALID_UID;
640     }
641 
642     MessageParcel reply;
643     if (!SendTransactCmd(IBundleMgr::Message::GET_UID_BY_DEBUG_BUNDLE_NAME, data, reply)) {
644         APP_LOGE("failed to GetUidByBundleName from server");
645         return Constants::INVALID_UID;
646     }
647     int32_t uid = reply.ReadInt32();
648     APP_LOGD("uid is %{public}d", uid);
649     return uid;
650 }
651 
GetAppIdByBundleName(const std::string & bundleName,const int userId)652 std::string BundleMgrProxy::GetAppIdByBundleName(const std::string &bundleName, const int userId)
653 {
654     if (bundleName.empty()) {
655         APP_LOGE("failed to GetAppIdByBundleName due to bundleName empty");
656         return Constants::EMPTY_STRING;
657     }
658     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
659     APP_LOGD("begin to get appId of %{public}s", bundleName.c_str());
660 
661     MessageParcel data;
662     if (!data.WriteInterfaceToken(GetDescriptor())) {
663         APP_LOGE("failed to GetAppIdByBundleName due to write InterfaceToken fail");
664         return Constants::EMPTY_STRING;
665     }
666     if (!data.WriteString(bundleName)) {
667         APP_LOGE("failed to GetAppIdByBundleName due to write bundleName fail");
668         return Constants::EMPTY_STRING;
669     }
670     if (!data.WriteInt32(userId)) {
671         APP_LOGE("failed to GetAppIdByBundleName due to write uid fail");
672         return Constants::EMPTY_STRING;
673     }
674 
675     MessageParcel reply;
676     if (!SendTransactCmd(IBundleMgr::Message::GET_APPID_BY_BUNDLE_NAME, data, reply)) {
677         APP_LOGE("failed to GetAppIdByBundleName from server");
678         return Constants::EMPTY_STRING;
679     }
680     std::string appId = reply.ReadString();
681     APP_LOGD("appId is %{private}s", appId.c_str());
682     return appId;
683 }
684 
GetBundleNameForUid(const int uid,std::string & bundleName)685 bool BundleMgrProxy::GetBundleNameForUid(const int uid, std::string &bundleName)
686 {
687     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
688     APP_LOGD("begin to GetBundleNameForUid of %{public}d", uid);
689     MessageParcel data;
690     if (!data.WriteInterfaceToken(GetDescriptor())) {
691         APP_LOGE("fail to GetBundleNameForUid due to write InterfaceToken fail");
692         return false;
693     }
694     if (!data.WriteInt32(uid)) {
695         APP_LOGE("fail to GetBundleNameForUid due to write uid fail");
696         return false;
697     }
698 
699     MessageParcel reply;
700     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLE_NAME_FOR_UID, data, reply)) {
701         APP_LOGE("fail to GetBundleNameForUid from server");
702         return false;
703     }
704     if (!reply.ReadBool()) {
705         APP_LOGE("reply result false");
706         return false;
707     }
708     bundleName = reply.ReadString();
709     return true;
710 }
711 
GetBundlesForUid(const int uid,std::vector<std::string> & bundleNames)712 bool BundleMgrProxy::GetBundlesForUid(const int uid, std::vector<std::string> &bundleNames)
713 {
714     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
715     APP_LOGD("begin to GetBundlesForUid of %{public}d", uid);
716     MessageParcel data;
717     if (!data.WriteInterfaceToken(GetDescriptor())) {
718         APP_LOGE("fail to GetBundlesForUid due to write InterfaceToken fail");
719         return false;
720     }
721     if (!data.WriteInt32(uid)) {
722         APP_LOGE("fail to GetBundlesForUid due to write uid fail");
723         return false;
724     }
725 
726     MessageParcel reply;
727     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLES_FOR_UID, data, reply)) {
728         APP_LOGE("fail to GetBundlesForUid from server");
729         return false;
730     }
731     if (!reply.ReadBool()) {
732         APP_LOGE("reply result false");
733         return false;
734     }
735     if (!reply.ReadStringVector(&bundleNames)) {
736         APP_LOGE("fail to GetBundlesForUid from reply");
737         return false;
738     }
739     return true;
740 }
741 
GetNameForUid(const int uid,std::string & name)742 ErrCode BundleMgrProxy::GetNameForUid(const int uid, std::string &name)
743 {
744     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
745     APP_LOGD("begin to GetNameForUid of %{public}d", uid);
746     MessageParcel data;
747     if (!data.WriteInterfaceToken(GetDescriptor())) {
748         APP_LOGE("fail to GetNameForUid due to write InterfaceToken fail");
749         return ERR_APPEXECFWK_PARCEL_ERROR;
750     }
751     if (!data.WriteInt32(uid)) {
752         APP_LOGE("fail to GetNameForUid due to write uid fail");
753         return ERR_APPEXECFWK_PARCEL_ERROR;
754     }
755 
756     MessageParcel reply;
757     if (!SendTransactCmd(IBundleMgr::Message::GET_NAME_FOR_UID, data, reply)) {
758         APP_LOGE("fail to GetNameForUid from server");
759         return ERR_APPEXECFWK_PARCEL_ERROR;
760     }
761     ErrCode ret = reply.ReadInt32();
762     if (ret != ERR_OK) {
763         APP_LOGE("host reply errCode : %{public}d", ret);
764         return ret;
765     }
766     name = reply.ReadString();
767     return ERR_OK;
768 }
769 
GetBundleGids(const std::string & bundleName,std::vector<int> & gids)770 bool BundleMgrProxy::GetBundleGids(const std::string &bundleName, std::vector<int> &gids)
771 {
772     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
773     APP_LOGD("begin to GetBundleGids of %{public}s", bundleName.c_str());
774     MessageParcel data;
775     if (!data.WriteInterfaceToken(GetDescriptor())) {
776         APP_LOGE("fail to GetBundleGids due to write InterfaceToken fail");
777         return false;
778     }
779     if (!data.WriteString(bundleName)) {
780         APP_LOGE("fail to GetBundleGids due to write bundleName fail");
781         return false;
782     }
783 
784     MessageParcel reply;
785     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLE_GIDS, data, reply)) {
786         APP_LOGE("fail to GetBundleGids from server");
787         return false;
788     }
789     if (!reply.ReadBool()) {
790         APP_LOGE("reply result false");
791         return false;
792     }
793     if (!reply.ReadInt32Vector(&gids)) {
794         APP_LOGE("fail to GetBundleGids from reply");
795         return false;
796     }
797     return true;
798 }
799 
GetBundleGidsByUid(const std::string & bundleName,const int & uid,std::vector<int> & gids)800 bool BundleMgrProxy::GetBundleGidsByUid(const std::string &bundleName, const int &uid, std::vector<int> &gids)
801 {
802     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
803     APP_LOGD("begin to GetBundleGidsByUid of %{public}s", bundleName.c_str());
804     MessageParcel data;
805     if (!data.WriteInterfaceToken(GetDescriptor())) {
806         APP_LOGE("fail to GetBundleGidsByUid due to write InterfaceToken fail");
807         return false;
808     }
809     if (!data.WriteString(bundleName)) {
810         APP_LOGE("fail to GetBundleGidsByUid due to write bundleName fail");
811         return false;
812     }
813     if (!data.WriteInt32(uid)) {
814         APP_LOGE("fail to GetBundleGidsByUid due to write uid fail");
815         return false;
816     }
817 
818     MessageParcel reply;
819     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLE_GIDS_BY_UID, data, reply)) {
820         APP_LOGE("fail to GetBundleGidsByUid from server");
821         return false;
822     }
823     if (!reply.ReadBool()) {
824         APP_LOGE("reply result false");
825         return false;
826     }
827     if (!reply.ReadInt32Vector(&gids)) {
828         APP_LOGE("fail to GetBundleGidsByUid from reply");
829         return false;
830     }
831     return true;
832 }
833 
GetAppType(const std::string & bundleName)834 std::string BundleMgrProxy::GetAppType(const std::string &bundleName)
835 {
836     if (bundleName.empty()) {
837         APP_LOGE("failed to GetAppType due to bundleName empty");
838         return Constants::EMPTY_STRING;
839     }
840     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
841     APP_LOGD("begin to GetAppType of %{public}s", bundleName.c_str());
842 
843     MessageParcel data;
844     if (!data.WriteInterfaceToken(GetDescriptor())) {
845         APP_LOGE("failed to GetAppType due to write InterfaceToken fail");
846         return Constants::EMPTY_STRING;
847     }
848     if (!data.WriteString(bundleName)) {
849         APP_LOGE("failed to GetAppType due to write bundleName fail");
850         return Constants::EMPTY_STRING;
851     }
852 
853     MessageParcel reply;
854     if (!SendTransactCmd(IBundleMgr::Message::GET_APP_TYPE, data, reply)) {
855         APP_LOGE("failed to GetAppType from server");
856         return Constants::EMPTY_STRING;
857     }
858     std::string appType = reply.ReadString();
859     APP_LOGD("appType is %{public}s", appType.c_str());
860     return appType;
861 }
862 
CheckIsSystemAppByUid(const int uid)863 bool BundleMgrProxy::CheckIsSystemAppByUid(const int uid)
864 {
865     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
866     APP_LOGD("begin to CheckIsSystemAppByUid of %{public}d", uid);
867     MessageParcel data;
868     if (!data.WriteInterfaceToken(GetDescriptor())) {
869         APP_LOGE("fail to CheckIsSystemAppByUid due to write InterfaceToken fail");
870         return false;
871     }
872     if (!data.WriteInt32(uid)) {
873         APP_LOGE("fail to CheckIsSystemAppByUid due to write uid fail");
874         return false;
875     }
876 
877     MessageParcel reply;
878     if (!SendTransactCmd(IBundleMgr::Message::CHECK_IS_SYSTEM_APP_BY_UID, data, reply)) {
879         APP_LOGE("fail to CheckIsSystemAppByUid from server");
880         return false;
881     }
882     return reply.ReadBool();
883 }
884 
GetBundleInfosByMetaData(const std::string & metaData,std::vector<BundleInfo> & bundleInfos)885 bool BundleMgrProxy::GetBundleInfosByMetaData(const std::string &metaData, std::vector<BundleInfo> &bundleInfos)
886 {
887     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
888     APP_LOGD("begin to GetBundleInfosByMetaData of %{public}s", metaData.c_str());
889     if (metaData.empty()) {
890         APP_LOGE("fail to GetBundleInfosByMetaData due to params empty");
891         return false;
892     }
893 
894     MessageParcel data;
895     if (!data.WriteInterfaceToken(GetDescriptor())) {
896         APP_LOGE("fail to GetBundleInfosByMetaData due to write InterfaceToken fail");
897         return false;
898     }
899     if (!data.WriteString(metaData)) {
900         APP_LOGE("fail to GetBundleInfosByMetaData due to write metaData fail");
901         return false;
902     }
903 
904     if (!GetParcelableInfos<BundleInfo>(IBundleMgr::Message::GET_BUNDLE_INFOS_BY_METADATA, data, bundleInfos)) {
905         APP_LOGE("fail to GetBundleInfosByMetaData from server");
906         return false;
907     }
908     return true;
909 }
910 
QueryAbilityInfo(const Want & want,AbilityInfo & abilityInfo)911 bool BundleMgrProxy::QueryAbilityInfo(const Want &want, AbilityInfo &abilityInfo)
912 {
913     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
914     MessageParcel data;
915     if (!data.WriteInterfaceToken(GetDescriptor())) {
916         APP_LOGE("fail to QueryAbilityInfo due to write MessageParcel fail");
917         return false;
918     }
919     if (!data.WriteParcelable(&want)) {
920         APP_LOGE("fail to QueryAbilityInfo due to write want fail");
921         return false;
922     }
923 
924     if (!GetParcelableInfo<AbilityInfo>(IBundleMgr::Message::QUERY_ABILITY_INFO, data, abilityInfo)) {
925         APP_LOGE("fail to query ability info from server");
926         return false;
927     }
928     return true;
929 }
930 
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack)931 bool BundleMgrProxy::QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId,
932     AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack)
933 {
934     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
935     MessageParcel data;
936     if (!data.WriteInterfaceToken(GetDescriptor())) {
937         APP_LOGE("fail to QueryAbilityInfo due to write MessageParcel");
938         return false;
939     }
940     if (!data.WriteParcelable(&want)) {
941         APP_LOGE("fail to QueryAbilityInfo due to write want");
942         return false;
943     }
944 
945     if (!data.WriteInt32(flags)) {
946         APP_LOGE("fail to QueryAbilityInfo due to write flags");
947         return false;
948     }
949 
950     if (!data.WriteInt32(userId)) {
951         APP_LOGE("fail to QueryAbilityInfo due to write userId");
952         return false;
953     }
954 
955     if (!data.WriteObject(callBack)) {
956         APP_LOGE("fail to callBack, for write parcel");
957         return false;
958     }
959 
960     if (!GetParcelableInfo<AbilityInfo>(IBundleMgr::Message::QUERY_ABILITY_INFO_WITH_CALLBACK, data, abilityInfo)) {
961         APP_LOGE("fail to query ability info from server");
962         return false;
963     }
964     return true;
965 }
966 
SilentInstall(const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)967 bool BundleMgrProxy::SilentInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
968 {
969     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
970     APP_LOGD("begin to silent install");
971 
972     MessageParcel data;
973     if (!data.WriteInterfaceToken(GetDescriptor())) {
974         APP_LOGE("fail to SilentInstall due to write token");
975         return false;
976     }
977     if (!data.WriteParcelable(&want)) {
978         APP_LOGE("fail to SilentInstall due to write want");
979         return false;
980     }
981 
982     if (!data.WriteInt32(userId)) {
983         APP_LOGE("fail to SilentInstall due to write userId");
984         return false;
985     }
986 
987     if (!data.WriteObject(callBack)) {
988         APP_LOGE("fail to SilentInstall due to write callBack");
989         return false;
990     }
991 
992     MessageParcel reply;
993     return SendTransactCmd(IBundleMgr::Message::SILENT_INSTALL, data, reply);
994 }
995 
UpgradeAtomicService(const Want & want,int32_t userId)996 void BundleMgrProxy::UpgradeAtomicService(const Want &want, int32_t userId)
997 {
998     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
999     MessageParcel data;
1000     if (!data.WriteInterfaceToken(GetDescriptor())) {
1001         APP_LOGE("fail to UpgradeAtomicService due to write descriptor");
1002         return;
1003     }
1004     if (!data.WriteParcelable(&want)) {
1005         APP_LOGE("fail to UpgradeAtomicService due to write want");
1006         return;
1007     }
1008 
1009     if (!data.WriteInt32(userId)) {
1010         APP_LOGE("fail to UpgradeAtomicService due to write userId");
1011         return;
1012     }
1013     return;
1014 }
1015 
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo)1016 bool BundleMgrProxy::QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo)
1017 {
1018     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1019     MessageParcel data;
1020     if (!data.WriteInterfaceToken(GetDescriptor())) {
1021         APP_LOGE("fail to QueryAbilityInfo mutiparam due to write MessageParcel fail");
1022         return false;
1023     }
1024     if (!data.WriteParcelable(&want)) {
1025         APP_LOGE("fail to QueryAbilityInfo mutiparam due to write want fail");
1026         return false;
1027     }
1028     if (!data.WriteInt32(flags)) {
1029         APP_LOGE("fail to QueryAbilityInfo mutiparam due to write flags fail");
1030         return false;
1031     }
1032     if (!data.WriteInt32(userId)) {
1033         APP_LOGE("fail to QueryAbilityInfo mutiparam due to write userId error");
1034         return false;
1035     }
1036 
1037     if (!GetParcelableInfo<AbilityInfo>(IBundleMgr::Message::QUERY_ABILITY_INFO_MUTI_PARAM, data, abilityInfo)) {
1038         APP_LOGE("fail to query ability info mutiparam from server");
1039         return false;
1040     }
1041     return true;
1042 }
1043 
QueryAbilityInfos(const Want & want,std::vector<AbilityInfo> & abilityInfos)1044 bool BundleMgrProxy::QueryAbilityInfos(const Want &want, std::vector<AbilityInfo> &abilityInfos)
1045 {
1046     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1047     MessageParcel data;
1048     if (!data.WriteInterfaceToken(GetDescriptor())) {
1049         APP_LOGE("fail to QueryAbilityInfos due to write MessageParcel fail");
1050         return false;
1051     }
1052     if (!data.WriteParcelable(&want)) {
1053         APP_LOGE("fail to QueryAbilityInfos due to write want fail");
1054         return false;
1055     }
1056 
1057     if (!GetParcelableInfos<AbilityInfo>(IBundleMgr::Message::QUERY_ABILITY_INFOS, data, abilityInfos)) {
1058         APP_LOGE("fail to QueryAbilityInfos from server");
1059         return false;
1060     }
1061     return true;
1062 }
1063 
QueryAbilityInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos)1064 bool BundleMgrProxy::QueryAbilityInfos(
1065     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
1066 {
1067     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1068     MessageParcel data;
1069     if (!data.WriteInterfaceToken(GetDescriptor())) {
1070         APP_LOGE("fail to QueryAbilityInfos mutiparam due to write MessageParcel fail");
1071         return false;
1072     }
1073     if (!data.WriteParcelable(&want)) {
1074         APP_LOGE("fail to QueryAbilityInfos mutiparam due to write want fail");
1075         return false;
1076     }
1077     if (!data.WriteInt32(flags)) {
1078         APP_LOGE("fail to QueryAbilityInfos mutiparam due to write flags fail");
1079         return false;
1080     }
1081     if (!data.WriteInt32(userId)) {
1082         APP_LOGE("fail to QueryAbilityInfos mutiparam due to write userId error");
1083         return false;
1084     }
1085 
1086     MessageParcel reply;
1087     if (!SendTransactCmd(IBundleMgr::Message::QUERY_ABILITY_INFOS_MUTI_PARAM, data, reply)) {
1088         APP_LOGE("sendTransactCmd failed");
1089         return false;
1090     }
1091     if (!reply.ReadBool()) {
1092         APP_LOGE("host returns error");
1093         return false;
1094     }
1095     if (!GetParcelableInfosFromAshmem<AbilityInfo>(reply, abilityInfos)) {
1096         APP_LOGE("fail to QueryAbilityInfos mutiparam from server");
1097         return false;
1098     }
1099     return true;
1100 }
1101 
QueryAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos)1102 ErrCode BundleMgrProxy::QueryAbilityInfosV9(
1103     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
1104 {
1105     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1106     MessageParcel data;
1107     if (!data.WriteInterfaceToken(GetDescriptor())) {
1108         APP_LOGE("write interfaceToken failed");
1109         return ERR_APPEXECFWK_PARCEL_ERROR;
1110     }
1111     if (!data.WriteParcelable(&want)) {
1112         APP_LOGE("write want failed");
1113         return ERR_APPEXECFWK_PARCEL_ERROR;
1114     }
1115     if (!data.WriteInt32(flags)) {
1116         APP_LOGE("write flags failed");
1117         return ERR_APPEXECFWK_PARCEL_ERROR;
1118     }
1119     if (!data.WriteInt32(userId)) {
1120         APP_LOGE("write userId failed");
1121         return ERR_APPEXECFWK_PARCEL_ERROR;
1122     }
1123 
1124     MessageParcel reply;
1125     if (!SendTransactCmd(IBundleMgr::Message::QUERY_ABILITY_INFOS_V9, data, reply)) {
1126         APP_LOGE("sendTransactCmd failed");
1127         return ERR_APPEXECFWK_PARCEL_ERROR;
1128     }
1129     ErrCode ret = reply.ReadInt32();
1130     if (ret != ERR_OK) {
1131         APP_LOGE("host reply errCode : %{public}d", ret);
1132         return ret;
1133     }
1134     if (!GetParcelableInfosFromAshmem<AbilityInfo>(reply, abilityInfos)) {
1135         APP_LOGE("getParcelableInfosFromAshmem failed");
1136         return ERR_APPEXECFWK_PARCEL_ERROR;
1137     }
1138     return ERR_OK;
1139 }
1140 
QueryAllAbilityInfos(const Want & want,int32_t userId,std::vector<AbilityInfo> & abilityInfos)1141 bool BundleMgrProxy::QueryAllAbilityInfos(const Want &want, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
1142 {
1143     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1144     MessageParcel data;
1145     if (!data.WriteInterfaceToken(GetDescriptor())) {
1146         APP_LOGE("fail to QueryAbilityInfo due to write MessageParcel fail");
1147         return false;
1148     }
1149     if (!data.WriteParcelable(&want)) {
1150         APP_LOGE("fail to QueryAbilityInfos mutiparam due to write want fail");
1151         return false;
1152     }
1153     if (!data.WriteInt32(userId)) {
1154         APP_LOGE("fail to QueryAbilityInfo due to write want fail");
1155         return false;
1156     }
1157 
1158     MessageParcel reply;
1159     if (!SendTransactCmd(IBundleMgr::Message::QUERY_ALL_ABILITY_INFOS, data, reply)) {
1160         APP_LOGE("sendTransactCmd failed");
1161         return false;
1162     }
1163     if (!reply.ReadBool()) {
1164         APP_LOGE("host returns error");
1165         return false;
1166     }
1167     if (!GetParcelableInfosFromAshmem<AbilityInfo>(reply, abilityInfos)) {
1168         APP_LOGE("fail to QueryAbilityInfos from server");
1169         return false;
1170     }
1171     return true;
1172 }
1173 
QueryAbilityInfoByUri(const std::string & abilityUri,AbilityInfo & abilityInfo)1174 bool BundleMgrProxy::QueryAbilityInfoByUri(const std::string &abilityUri, AbilityInfo &abilityInfo)
1175 {
1176     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1177     MessageParcel data;
1178     if (!data.WriteInterfaceToken(GetDescriptor())) {
1179         APP_LOGE("fail to QueryAbilityInfoByUri due to write MessageParcel fail");
1180         return false;
1181     }
1182     if (!data.WriteString(abilityUri)) {
1183         APP_LOGE("fail to QueryAbilityInfoByUri due to write abilityUri fail");
1184         return false;
1185     }
1186 
1187     if (!GetParcelableInfo<AbilityInfo>(IBundleMgr::Message::QUERY_ABILITY_INFO_BY_URI, data, abilityInfo)) {
1188         APP_LOGE("fail to QueryAbilityInfoByUri from server");
1189         return false;
1190     }
1191     return true;
1192 }
1193 
QueryAbilityInfosByUri(const std::string & abilityUri,std::vector<AbilityInfo> & abilityInfos)1194 bool BundleMgrProxy::QueryAbilityInfosByUri(const std::string &abilityUri, std::vector<AbilityInfo> &abilityInfos)
1195 {
1196     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1197     MessageParcel data;
1198     if (!data.WriteInterfaceToken(GetDescriptor())) {
1199         APP_LOGE("fail to QueryAbilityInfosByUri due to write MessageParcel fail");
1200         return false;
1201     }
1202     if (!data.WriteString(abilityUri)) {
1203         APP_LOGE("fail to QueryAbilityInfosByUri due to write abilityUri fail");
1204         return false;
1205     }
1206 
1207     if (!GetParcelableInfos<AbilityInfo>(IBundleMgr::Message::QUERY_ABILITY_INFOS_BY_URI, data, abilityInfos)) {
1208         APP_LOGE("fail to QueryAbilityInfosByUri from server");
1209         return false;
1210     }
1211     return true;
1212 }
1213 
QueryAbilityInfoByUri(const std::string & abilityUri,int32_t userId,AbilityInfo & abilityInfo)1214 bool BundleMgrProxy::QueryAbilityInfoByUri(
1215     const std::string &abilityUri, int32_t userId, AbilityInfo &abilityInfo)
1216 {
1217     MessageParcel data;
1218     if (!data.WriteInterfaceToken(GetDescriptor())) {
1219         APP_LOGE("fail to QueryAbilityInfoByUri due to write MessageParcel fail");
1220         return false;
1221     }
1222     if (!data.WriteString(abilityUri)) {
1223         APP_LOGE("fail to QueryAbilityInfoByUri due to write abilityUri fail");
1224         return false;
1225     }
1226     if (!data.WriteInt32(userId)) {
1227         APP_LOGE("fail to QueryAbilityInfo due to write want fail");
1228         return false;
1229     }
1230 
1231     if (!GetParcelableInfo<AbilityInfo>(
1232         IBundleMgr::Message::QUERY_ABILITY_INFO_BY_URI_FOR_USERID, data, abilityInfo)) {
1233         APP_LOGE("fail to QueryAbilityInfoByUri from server");
1234         return false;
1235     }
1236     return true;
1237 }
1238 
QueryKeepAliveBundleInfos(std::vector<BundleInfo> & bundleInfos)1239 bool BundleMgrProxy::QueryKeepAliveBundleInfos(std::vector<BundleInfo> &bundleInfos)
1240 {
1241     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1242     APP_LOGD("begin to QueryKeepAliveBundleInfos");
1243 
1244     MessageParcel data;
1245     if (!data.WriteInterfaceToken(GetDescriptor())) {
1246         APP_LOGE("fail to QueryKeepAliveBundleInfos due to write InterfaceToken fail");
1247         return false;
1248     }
1249 
1250     if (!GetParcelableInfos<BundleInfo>(IBundleMgr::Message::QUERY_KEEPALIVE_BUNDLE_INFOS, data, bundleInfos)) {
1251         APP_LOGE("fail to QueryKeepAliveBundleInfos from server");
1252         return false;
1253     }
1254     return true;
1255 }
1256 
GetAbilityLabel(const std::string & bundleName,const std::string & abilityName)1257 std::string BundleMgrProxy::GetAbilityLabel(const std::string &bundleName, const std::string &abilityName)
1258 {
1259     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1260     APP_LOGD("begin to GetAbilityLabel of %{public}s", bundleName.c_str());
1261     if (bundleName.empty() || abilityName.empty()) {
1262         APP_LOGE("fail to GetAbilityLabel due to params empty");
1263         return Constants::EMPTY_STRING;
1264     }
1265 
1266     MessageParcel data;
1267     if (!data.WriteInterfaceToken(GetDescriptor())) {
1268         APP_LOGE("fail to GetAbilityLabel due to write InterfaceToken fail");
1269         return Constants::EMPTY_STRING;
1270     }
1271     if (!data.WriteString(bundleName)) {
1272         APP_LOGE("fail to GetAbilityLabel due to write bundleName fail");
1273         return Constants::EMPTY_STRING;
1274     }
1275     if (!data.WriteString(abilityName)) {
1276         APP_LOGE("fail to GetAbilityLabel due to write abilityName fail");
1277         return Constants::EMPTY_STRING;
1278     }
1279 
1280     MessageParcel reply;
1281     if (!SendTransactCmd(IBundleMgr::Message::GET_ABILITY_LABEL, data, reply)) {
1282         APP_LOGE("fail to GetAbilityLabel from server");
1283         return Constants::EMPTY_STRING;
1284     }
1285     return reply.ReadString();
1286 }
1287 
GetAbilityLabel(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,std::string & label)1288 ErrCode BundleMgrProxy::GetAbilityLabel(const std::string &bundleName, const std::string &moduleName,
1289     const std::string &abilityName, std::string &label)
1290 {
1291     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1292     APP_LOGI("begin to GetAbilityLabel of %{public}s", bundleName.c_str());
1293     if (bundleName.empty() || moduleName.empty() || abilityName.empty()) {
1294         APP_LOGE("fail to GetAbilityLabel due to params empty");
1295         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
1296     }
1297     MessageParcel data;
1298     if (!data.WriteInterfaceToken(GetDescriptor())) {
1299         APP_LOGE("fail to GetAbilityLabel due to write InterfaceToken fail");
1300         return ERR_APPEXECFWK_PARCEL_ERROR;
1301     }
1302     if (!data.WriteString(bundleName)) {
1303         APP_LOGE("fail to GetAbilityLabel due to write bundleName fail");
1304         return ERR_APPEXECFWK_PARCEL_ERROR;
1305     }
1306     if (!data.WriteString(moduleName)) {
1307         APP_LOGE("fail to GetAbilityLabel due to write moduleName fail");
1308         return ERR_APPEXECFWK_PARCEL_ERROR;
1309     }
1310     if (!data.WriteString(abilityName)) {
1311         APP_LOGE("fail to GetAbilityLabel due to write abilityName fail");
1312         return ERR_APPEXECFWK_PARCEL_ERROR;
1313     }
1314     MessageParcel reply;
1315     if (!SendTransactCmd(IBundleMgr::Message::GET_ABILITY_LABEL_WITH_MODULE_NAME, data, reply)) {
1316         return ERR_BUNDLE_MANAGER_IPC_TRANSACTION;
1317     }
1318     int32_t errCode = reply.ReadInt32();
1319     if (errCode != ERR_OK) {
1320         return errCode;
1321     }
1322     label = reply.ReadString();
1323     return ERR_OK;
1324 }
1325 
GetBundleArchiveInfo(const std::string & hapFilePath,const BundleFlag flag,BundleInfo & bundleInfo)1326 bool BundleMgrProxy::GetBundleArchiveInfo(const std::string &hapFilePath, const BundleFlag flag, BundleInfo &bundleInfo)
1327 {
1328     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1329     APP_LOGD("begin to GetBundleArchiveInfo of %{private}s", hapFilePath.c_str());
1330     if (hapFilePath.empty()) {
1331         APP_LOGE("fail to GetBundleArchiveInfo due to params empty");
1332         return false;
1333     }
1334 
1335     MessageParcel data;
1336     if (!data.WriteInterfaceToken(GetDescriptor())) {
1337         APP_LOGE("fail to GetBundleArchiveInfo due to write InterfaceToken fail");
1338         return false;
1339     }
1340     if (!data.WriteString(hapFilePath)) {
1341         APP_LOGE("fail to GetBundleArchiveInfo due to write hapFilePath fail");
1342         return false;
1343     }
1344     if (!data.WriteInt32(static_cast<int>(flag))) {
1345         APP_LOGE("fail to GetBundleArchiveInfo due to write flag fail");
1346         return false;
1347     }
1348 
1349     if (!GetParcelableInfo<BundleInfo>(IBundleMgr::Message::GET_BUNDLE_ARCHIVE_INFO, data, bundleInfo)) {
1350         APP_LOGE("fail to GetBundleArchiveInfo from server");
1351         return false;
1352     }
1353     return true;
1354 }
1355 
GetBundleArchiveInfo(const std::string & hapFilePath,int32_t flags,BundleInfo & bundleInfo)1356 bool BundleMgrProxy::GetBundleArchiveInfo(const std::string &hapFilePath, int32_t flags, BundleInfo &bundleInfo)
1357 {
1358     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1359     APP_LOGD("begin to GetBundleArchiveInfo with int flags of %{private}s", hapFilePath.c_str());
1360     if (hapFilePath.empty()) {
1361         APP_LOGE("fail to GetBundleArchiveInfo due to params empty");
1362         return false;
1363     }
1364 
1365     MessageParcel data;
1366     if (!data.WriteInterfaceToken(GetDescriptor())) {
1367         APP_LOGE("fail to GetBundleArchiveInfo due to write InterfaceToken fail");
1368         return false;
1369     }
1370     if (!data.WriteString(hapFilePath)) {
1371         APP_LOGE("fail to GetBundleArchiveInfo due to write hapFilePath fail");
1372         return false;
1373     }
1374     if (!data.WriteInt32(flags)) {
1375         APP_LOGE("fail to GetBundleArchiveInfo due to write flags fail");
1376         return false;
1377     }
1378 
1379     if (!GetParcelableInfo<BundleInfo>(IBundleMgr::Message::GET_BUNDLE_ARCHIVE_INFO_WITH_INT_FLAGS, data, bundleInfo)) {
1380         APP_LOGE("fail to GetBundleArchiveInfo from server");
1381         return false;
1382     }
1383     return true;
1384 }
1385 
GetBundleArchiveInfoV9(const std::string & hapFilePath,int32_t flags,BundleInfo & bundleInfo)1386 ErrCode BundleMgrProxy::GetBundleArchiveInfoV9(const std::string &hapFilePath, int32_t flags, BundleInfo &bundleInfo)
1387 {
1388     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1389     APP_LOGD("begin to GetBundleArchiveInfoV9 with int flags of %{private}s", hapFilePath.c_str());
1390     if (hapFilePath.empty()) {
1391         APP_LOGE("fail to GetBundleArchiveInfoV9 due to params empty");
1392         return ERR_BUNDLE_MANAGER_INVALID_HAP_PATH;
1393     }
1394 
1395     MessageParcel data;
1396     if (!data.WriteInterfaceToken(GetDescriptor())) {
1397         APP_LOGE("fail to GetBundleArchiveInfoV9 due to write InterfaceToken fail");
1398         return ERR_APPEXECFWK_PARCEL_ERROR;
1399     }
1400     if (!data.WriteString(hapFilePath)) {
1401         APP_LOGE("fail to GetBundleArchiveInfoV9 due to write hapFilePath fail");
1402         return ERR_APPEXECFWK_PARCEL_ERROR;
1403     }
1404     if (!data.WriteInt32(flags)) {
1405         APP_LOGE("fail to GetBundleArchiveInfoV9 due to write flags fail");
1406         return ERR_APPEXECFWK_PARCEL_ERROR;
1407     }
1408     return GetParcelableInfoWithErrCode<BundleInfo>(
1409         IBundleMgr::Message::GET_BUNDLE_ARCHIVE_INFO_WITH_INT_FLAGS_V9, data, bundleInfo);
1410 }
1411 
GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo)1412 bool BundleMgrProxy::GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo)
1413 {
1414     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1415     APP_LOGD("begin to GetHapModuleInfo of %{public}s", abilityInfo.package.c_str());
1416     if (abilityInfo.bundleName.empty() || abilityInfo.package.empty()) {
1417         APP_LOGE("fail to GetHapModuleInfo due to params empty");
1418         return false;
1419     }
1420 
1421     MessageParcel data;
1422     if (!data.WriteInterfaceToken(GetDescriptor())) {
1423         APP_LOGE("fail to GetHapModuleInfo due to write InterfaceToken fail");
1424         return false;
1425     }
1426     if (!data.WriteParcelable(&abilityInfo)) {
1427         APP_LOGE("fail to GetHapModuleInfo due to write abilityInfo fail");
1428         return false;
1429     }
1430 
1431     if (!GetParcelableInfo<HapModuleInfo>(IBundleMgr::Message::GET_HAP_MODULE_INFO, data, hapModuleInfo)) {
1432         APP_LOGE("fail to GetHapModuleInfo from server");
1433         return false;
1434     }
1435     return true;
1436 }
1437 
GetHapModuleInfo(const AbilityInfo & abilityInfo,int32_t userId,HapModuleInfo & hapModuleInfo)1438 bool BundleMgrProxy::GetHapModuleInfo(const AbilityInfo &abilityInfo, int32_t userId, HapModuleInfo &hapModuleInfo)
1439 {
1440     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1441     APP_LOGD("begin to GetHapModuleInfo of %{public}s", abilityInfo.package.c_str());
1442     if (abilityInfo.bundleName.empty() || abilityInfo.package.empty()) {
1443         APP_LOGE("fail to GetHapModuleInfo due to params empty");
1444         return false;
1445     }
1446 
1447     MessageParcel data;
1448     if (!data.WriteInterfaceToken(GetDescriptor())) {
1449         APP_LOGE("fail to GetHapModuleInfo due to write InterfaceToken fail");
1450         return false;
1451     }
1452     if (!data.WriteParcelable(&abilityInfo)) {
1453         APP_LOGE("fail to GetHapModuleInfo due to write abilityInfo fail");
1454         return false;
1455     }
1456     if (!data.WriteInt32(userId)) {
1457         APP_LOGE("fail to QueryAbilityInfo due to write want fail");
1458         return false;
1459     }
1460 
1461     if (!GetParcelableInfo<HapModuleInfo>(IBundleMgr::Message::GET_HAP_MODULE_INFO_WITH_USERID, data, hapModuleInfo)) {
1462         APP_LOGE("fail to GetHapModuleInfo from server");
1463         return false;
1464     }
1465     return true;
1466 }
1467 
GetLaunchWantForBundle(const std::string & bundleName,Want & want,int32_t userId)1468 ErrCode BundleMgrProxy::GetLaunchWantForBundle(const std::string &bundleName, Want &want, int32_t userId)
1469 {
1470     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1471     APP_LOGD("begin to GetLaunchWantForBundle of %{public}s", bundleName.c_str());
1472     if (bundleName.empty()) {
1473         APP_LOGE("fail to GetHapModuleInfo due to params empty");
1474         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
1475     }
1476 
1477     MessageParcel data;
1478     if (!data.WriteInterfaceToken(GetDescriptor())) {
1479         APP_LOGE("fail to GetLaunchWantForBundle due to write InterfaceToken fail");
1480         return ERR_APPEXECFWK_PARCEL_ERROR;
1481     }
1482 
1483     if (!data.WriteString(bundleName)) {
1484         APP_LOGE("fail to GetLaunchWantForBundle due to write bundleName fail");
1485         return ERR_APPEXECFWK_PARCEL_ERROR;
1486     }
1487 
1488     if (!data.WriteInt32(userId)) {
1489         APP_LOGE("fail to GetLaunchWantForBundle due to write userId fail");
1490         return false;
1491     }
1492 
1493     return GetParcelableInfoWithErrCode<Want>(
1494         IBundleMgr::Message::GET_LAUNCH_WANT_FOR_BUNDLE, data, want);
1495 }
1496 
CheckPublicKeys(const std::string & firstBundleName,const std::string & secondBundleName)1497 int BundleMgrProxy::CheckPublicKeys(const std::string &firstBundleName, const std::string &secondBundleName)
1498 {
1499     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1500     APP_LOGD(
1501         "begin to CheckPublicKeys of %{public}s and %{public}s", firstBundleName.c_str(), secondBundleName.c_str());
1502     if (firstBundleName.empty() || secondBundleName.empty()) {
1503         APP_LOGE("fail to CheckPublicKeys due to params empty");
1504         return Constants::SIGNATURE_UNKNOWN_BUNDLE;
1505     }
1506 
1507     MessageParcel data;
1508     if (!data.WriteInterfaceToken(GetDescriptor())) {
1509         APP_LOGE("fail to GetBundleInfo due to write MessageParcel fail");
1510         return Constants::SIGNATURE_UNKNOWN_BUNDLE;
1511     }
1512     if (!data.WriteString(firstBundleName)) {
1513         APP_LOGE("fail to GetBundleInfo due to write firstBundleName fail");
1514         return Constants::SIGNATURE_UNKNOWN_BUNDLE;
1515     }
1516     if (!data.WriteString(secondBundleName)) {
1517         APP_LOGE("fail to GetBundleInfo due to write secondBundleName fail");
1518         return Constants::SIGNATURE_UNKNOWN_BUNDLE;
1519     }
1520 
1521     MessageParcel reply;
1522     if (!SendTransactCmd(IBundleMgr::Message::CHECK_PUBLICKEYS, data, reply)) {
1523         APP_LOGE("fail to CheckPublicKeys from server");
1524         return Constants::SIGNATURE_UNKNOWN_BUNDLE;
1525     }
1526     return reply.ReadInt32();
1527 }
1528 
GetPermissionDef(const std::string & permissionName,PermissionDef & permissionDef)1529 ErrCode BundleMgrProxy::GetPermissionDef(const std::string &permissionName, PermissionDef &permissionDef)
1530 {
1531     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1532     APP_LOGD("begin to GetPermissionDef of %{public}s", permissionName.c_str());
1533     MessageParcel data;
1534     if (!data.WriteInterfaceToken(GetDescriptor())) {
1535         APP_LOGE("fail to GetPermissionDef due to write InterfaceToken fail");
1536         return ERR_APPEXECFWK_PARCEL_ERROR;
1537     }
1538     if (!data.WriteString(permissionName)) {
1539         APP_LOGE("fail to GetPermissionDef due to write permissionName fail");
1540         return ERR_APPEXECFWK_PARCEL_ERROR;
1541     }
1542 
1543     return GetParcelableInfoWithErrCode<PermissionDef>(IBundleMgr::Message::GET_PERMISSION_DEF, data, permissionDef);
1544 }
1545 
HasSystemCapability(const std::string & capName)1546 bool BundleMgrProxy::HasSystemCapability(const std::string &capName)
1547 {
1548     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1549     APP_LOGD("begin to HasSystemCapability of %{public}s", capName.c_str());
1550     if (capName.empty()) {
1551         APP_LOGE("fail to HasSystemCapability due to params empty");
1552         return false;
1553     }
1554 
1555     MessageParcel data;
1556     if (!data.WriteInterfaceToken(GetDescriptor())) {
1557         APP_LOGE("fail to HasSystemCapability due to write InterfaceToken fail");
1558         return false;
1559     }
1560     if (!data.WriteString(capName)) {
1561         APP_LOGE("fail to HasSystemCapability due to write capName fail");
1562         return false;
1563     }
1564 
1565     MessageParcel reply;
1566     if (!SendTransactCmd(IBundleMgr::Message::HAS_SYSTEM_CAPABILITY, data, reply)) {
1567         APP_LOGE("fail to HasSystemCapability from server");
1568         return false;
1569     }
1570     return reply.ReadBool();
1571 }
1572 
GetSystemAvailableCapabilities(std::vector<std::string> & systemCaps)1573 bool BundleMgrProxy::GetSystemAvailableCapabilities(std::vector<std::string> &systemCaps)
1574 {
1575     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1576     APP_LOGD("begin to GetSystemAvailableCapabilities");
1577     MessageParcel data;
1578     if (!data.WriteInterfaceToken(GetDescriptor())) {
1579         APP_LOGE("fail to GetSystemAvailableCapabilities due to write InterfaceToken fail");
1580         return false;
1581     }
1582 
1583     MessageParcel reply;
1584     if (!SendTransactCmd(IBundleMgr::Message::GET_SYSTEM_AVAILABLE_CAPABILITIES, data, reply)) {
1585         APP_LOGE("fail to GetSystemAvailableCapabilities from server");
1586         return false;
1587     }
1588 
1589     if (!reply.ReadStringVector(&systemCaps)) {
1590         APP_LOGE("fail to GetSystemAvailableCapabilities from reply");
1591         return false;
1592     }
1593 
1594     return true;
1595 }
1596 
IsSafeMode()1597 bool BundleMgrProxy::IsSafeMode()
1598 {
1599     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1600     APP_LOGD("begin to IsSafeMode");
1601     MessageParcel data;
1602     if (!data.WriteInterfaceToken(GetDescriptor())) {
1603         APP_LOGE("fail to IsSafeMode due to write InterfaceToken fail");
1604         return false;
1605     }
1606 
1607     MessageParcel reply;
1608     if (!SendTransactCmd(IBundleMgr::Message::IS_SAFE_MODE, data, reply)) {
1609         APP_LOGE("fail to IsSafeMode from server");
1610         return false;
1611     }
1612     return reply.ReadBool();
1613 }
1614 
CleanBundleCacheFiles(const std::string & bundleName,const sptr<ICleanCacheCallback> & cleanCacheCallback,int32_t userId)1615 ErrCode BundleMgrProxy::CleanBundleCacheFiles(
1616     const std::string &bundleName, const sptr<ICleanCacheCallback> &cleanCacheCallback, int32_t userId)
1617 {
1618     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1619     APP_LOGD("begin to CleanBundleCacheFiles of %{public}s", bundleName.c_str());
1620     if (bundleName.empty()) {
1621         APP_LOGE("fail to CleanBundleCacheFiles due to bundleName empty");
1622         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
1623     }
1624     if (cleanCacheCallback == nullptr) {
1625         APP_LOGE("fail to CleanBundleCacheFiles due to params error");
1626         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
1627     }
1628 
1629     MessageParcel data;
1630     if (!data.WriteInterfaceToken(GetDescriptor())) {
1631         APP_LOGE("fail to CleanBundleCacheFiles due to write InterfaceToken fail");
1632         return ERR_APPEXECFWK_PARCEL_ERROR;
1633     }
1634     if (!data.WriteString(bundleName)) {
1635         APP_LOGE("fail to CleanBundleCacheFiles due to write bundleName fail");
1636         return ERR_APPEXECFWK_PARCEL_ERROR;
1637     }
1638     if (!data.WriteObject<IRemoteObject>(cleanCacheCallback->AsObject())) {
1639         APP_LOGE("fail to CleanBundleCacheFiles, for write parcel failed");
1640         return ERR_APPEXECFWK_PARCEL_ERROR;
1641     }
1642     if (!data.WriteInt32(userId)) {
1643         APP_LOGE("fail to CleanBundleCacheFiles due to write userId fail");
1644         return ERR_APPEXECFWK_PARCEL_ERROR;
1645     }
1646 
1647     MessageParcel reply;
1648     if (!SendTransactCmd(IBundleMgr::Message::CLEAN_BUNDLE_CACHE_FILES, data, reply)) {
1649         APP_LOGE("fail to CleanBundleCacheFiles from server");
1650         return ERR_BUNDLE_MANAGER_IPC_TRANSACTION;
1651     }
1652     return reply.ReadInt32();
1653 }
1654 
CleanBundleDataFiles(const std::string & bundleName,const int userId)1655 bool BundleMgrProxy::CleanBundleDataFiles(const std::string &bundleName, const int userId)
1656 {
1657     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1658     APP_LOGD("begin to CleanBundleDataFiles of %{public}s", bundleName.c_str());
1659     if (bundleName.empty()) {
1660         APP_LOGE("fail to CleanBundleDataFiles due to params empty");
1661         return false;
1662     }
1663 
1664     MessageParcel data;
1665     if (!data.WriteInterfaceToken(GetDescriptor())) {
1666         APP_LOGE("fail to CleanBundleDataFiles due to write InterfaceToken fail");
1667         return false;
1668     }
1669     if (!data.WriteString(bundleName)) {
1670         APP_LOGE("fail to CleanBundleDataFiles due to write hapFilePath fail");
1671         return false;
1672     }
1673     if (!data.WriteInt32(userId)) {
1674         APP_LOGE("fail to CleanBundleDataFiles due to write userId fail");
1675         return false;
1676     }
1677 
1678     MessageParcel reply;
1679     if (!SendTransactCmd(IBundleMgr::Message::CLEAN_BUNDLE_DATA_FILES, data, reply)) {
1680         APP_LOGE("fail to CleanBundleDataFiles from server");
1681         return false;
1682     }
1683     return reply.ReadBool();
1684 }
1685 
RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)1686 bool BundleMgrProxy::RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
1687 {
1688     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1689     APP_LOGD("begin to RegisterBundleStatusCallback");
1690     if (!bundleStatusCallback || bundleStatusCallback->GetBundleName().empty()) {
1691         APP_LOGE("fail to RegisterBundleStatusCallback");
1692         return false;
1693     }
1694 
1695     MessageParcel data;
1696     if (!data.WriteInterfaceToken(GetDescriptor())) {
1697         APP_LOGE("fail to RegisterBundleStatusCallback due to write InterfaceToken fail");
1698         return false;
1699     }
1700     if (!data.WriteString(bundleStatusCallback->GetBundleName())) {
1701         APP_LOGE("fail to RegisterBundleStatusCallback due to write bundleName fail");
1702         return false;
1703     }
1704     if (!data.WriteObject<IRemoteObject>(bundleStatusCallback->AsObject())) {
1705         APP_LOGE("fail to RegisterBundleStatusCallback, for write parcel failed");
1706         return false;
1707     }
1708 
1709     MessageParcel reply;
1710     if (!SendTransactCmd(IBundleMgr::Message::REGISTER_BUNDLE_STATUS_CALLBACK, data, reply)) {
1711         APP_LOGE("fail to RegisterBundleStatusCallback from server");
1712         return false;
1713     }
1714     return reply.ReadBool();
1715 }
1716 
RegisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)1717 bool BundleMgrProxy::RegisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
1718 {
1719     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1720     APP_LOGD("begin to RegisterBundleEventCallback");
1721     if (!bundleEventCallback) {
1722         APP_LOGE("bundleEventCallback is null");
1723         return false;
1724     }
1725 
1726     MessageParcel data;
1727     if (!data.WriteInterfaceToken(GetDescriptor())) {
1728         APP_LOGE("fail to RegisterBundleEventCallback due to write InterfaceToken fail");
1729         return false;
1730     }
1731     if (!data.WriteObject<IRemoteObject>(bundleEventCallback->AsObject())) {
1732         APP_LOGE("write BundleEventCallback failed");
1733         return false;
1734     }
1735 
1736     MessageParcel reply;
1737     if (!SendTransactCmd(IBundleMgr::Message::REGISTER_BUNDLE_EVENT_CALLBACK, data, reply)) {
1738         APP_LOGE("fail to RegisterBundleEventCallback from server");
1739         return false;
1740     }
1741     return reply.ReadBool();
1742 }
1743 
UnregisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)1744 bool BundleMgrProxy::UnregisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
1745 {
1746     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1747     APP_LOGD("begin to UnregisterBundleEventCallback");
1748     if (!bundleEventCallback) {
1749         APP_LOGE("bundleEventCallback is null");
1750         return false;
1751     }
1752 
1753     MessageParcel data;
1754     if (!data.WriteInterfaceToken(GetDescriptor())) {
1755         APP_LOGE("fail to UnregisterBundleEventCallback due to write InterfaceToken fail");
1756         return false;
1757     }
1758     if (!data.WriteObject<IRemoteObject>(bundleEventCallback->AsObject())) {
1759         APP_LOGE("fail to UnregisterBundleEventCallback, for write parcel failed");
1760         return false;
1761     }
1762 
1763     MessageParcel reply;
1764     if (!SendTransactCmd(IBundleMgr::Message::UNREGISTER_BUNDLE_EVENT_CALLBACK, data, reply)) {
1765         APP_LOGE("fail to UnregisterBundleEventCallback from server");
1766         return false;
1767     }
1768     return reply.ReadBool();
1769 }
1770 
ClearBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)1771 bool BundleMgrProxy::ClearBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
1772 {
1773     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1774     APP_LOGD("begin to ClearBundleStatusCallback");
1775     if (!bundleStatusCallback) {
1776         APP_LOGE("fail to ClearBundleStatusCallback, for bundleStatusCallback is nullptr");
1777         return false;
1778     }
1779 
1780     MessageParcel data;
1781     if (!data.WriteInterfaceToken(GetDescriptor())) {
1782         APP_LOGE("fail to ClearBundleStatusCallback due to write InterfaceToken fail");
1783         return false;
1784     }
1785     if (!data.WriteObject<IRemoteObject>(bundleStatusCallback->AsObject())) {
1786         APP_LOGE("fail to ClearBundleStatusCallback, for write parcel failed");
1787         return false;
1788     }
1789 
1790     MessageParcel reply;
1791     if (!SendTransactCmd(IBundleMgr::Message::CLEAR_BUNDLE_STATUS_CALLBACK, data, reply)) {
1792         APP_LOGE("fail to CleanBundleCacheFiles from server");
1793         return false;
1794     }
1795     return reply.ReadBool();
1796 }
1797 
UnregisterBundleStatusCallback()1798 bool BundleMgrProxy::UnregisterBundleStatusCallback()
1799 {
1800     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1801     APP_LOGD("begin to UnregisterBundleStatusCallback");
1802     MessageParcel data;
1803     if (!data.WriteInterfaceToken(GetDescriptor())) {
1804         APP_LOGE("fail to UnregisterBundleStatusCallback due to write InterfaceToken fail");
1805         return false;
1806     }
1807 
1808     MessageParcel reply;
1809     if (!SendTransactCmd(IBundleMgr::Message::UNREGISTER_BUNDLE_STATUS_CALLBACK, data, reply)) {
1810         APP_LOGE("fail to UnregisterBundleStatusCallback from server");
1811         return false;
1812     }
1813     return reply.ReadBool();
1814 }
1815 
DumpInfos(const DumpFlag flag,const std::string & bundleName,int32_t userId,std::string & result)1816 bool BundleMgrProxy::DumpInfos(
1817     const DumpFlag flag, const std::string &bundleName, int32_t userId, std::string &result)
1818 {
1819     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1820     APP_LOGD("begin to dump");
1821     MessageParcel data;
1822     if (!data.WriteInterfaceToken(GetDescriptor())) {
1823         APP_LOGE("fail to dump due to write InterfaceToken fail");
1824         return false;
1825     }
1826     if (!data.WriteInt32(static_cast<int>(flag))) {
1827         APP_LOGE("fail to dump due to write flag fail");
1828         return false;
1829     }
1830     if (!data.WriteString(bundleName)) {
1831         APP_LOGE("fail to dump due to write bundleName fail");
1832         return false;
1833     }
1834     if (!data.WriteInt32(userId)) {
1835         APP_LOGE("fail to dump due to write userId fail");
1836         return false;
1837     }
1838 
1839     MessageParcel reply;
1840     if (!SendTransactCmd(IBundleMgr::Message::DUMP_INFOS, data, reply)) {
1841         APP_LOGE("fail to dump from server");
1842         return false;
1843     }
1844     if (!reply.ReadBool()) {
1845         APP_LOGE("readParcelableInfo failed");
1846         return false;
1847     }
1848     std::vector<std::string> dumpInfos;
1849     if (!reply.ReadStringVector(&dumpInfos)) {
1850         APP_LOGE("fail to dump from reply");
1851         return false;
1852     }
1853     result = std::accumulate(dumpInfos.begin(), dumpInfos.end(), result);
1854     return true;
1855 }
1856 
IsModuleRemovable(const std::string & bundleName,const std::string & moduleName,bool & isRemovable)1857 ErrCode BundleMgrProxy::IsModuleRemovable(const std::string &bundleName, const std::string &moduleName,
1858     bool &isRemovable)
1859 {
1860     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1861     APP_LOGD("begin to IsModuleRemovable of %{public}s", bundleName.c_str());
1862     if (bundleName.empty()) {
1863         APP_LOGE("fail to IsModuleRemovable due to bundleName empty");
1864         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
1865     }
1866     if (moduleName.empty()) {
1867         APP_LOGE("fail to IsModuleRemovable due to moduleName empty");
1868         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
1869     }
1870 
1871     MessageParcel data;
1872     if (!data.WriteInterfaceToken(GetDescriptor())) {
1873         APP_LOGE("fail to IsModuleRemovable due to write InterfaceToken fail");
1874         return ERR_APPEXECFWK_PARCEL_ERROR;
1875     }
1876     if (!data.WriteString(bundleName)) {
1877         APP_LOGE("fail to IsModuleRemovable due to write bundleName fail");
1878         return ERR_APPEXECFWK_PARCEL_ERROR;
1879     }
1880 
1881     if (!data.WriteString(moduleName)) {
1882         APP_LOGE("fail to IsModuleRemovable due to write moduleName fail");
1883         return ERR_APPEXECFWK_PARCEL_ERROR;
1884     }
1885     MessageParcel reply;
1886     if (!SendTransactCmd(IBundleMgr::Message::IS_MODULE_REMOVABLE, data, reply)) {
1887         APP_LOGE("fail to IsModuleRemovable from server");
1888         return false;
1889     }
1890     ErrCode ret = reply.ReadInt32();
1891     if (ret == ERR_OK) {
1892         isRemovable = reply.ReadBool();
1893     }
1894     return ret;
1895 }
1896 
SetModuleRemovable(const std::string & bundleName,const std::string & moduleName,bool isEnable)1897 bool BundleMgrProxy::SetModuleRemovable(const std::string &bundleName, const std::string &moduleName, bool isEnable)
1898 {
1899     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1900     APP_LOGD("begin to SetModuleRemovable of %{public}s", bundleName.c_str());
1901     if (bundleName.empty() || moduleName.empty()) {
1902         APP_LOGE("fail to SetModuleRemovable due to params empty");
1903         return false;
1904     }
1905 
1906     MessageParcel data;
1907     if (!data.WriteInterfaceToken(GetDescriptor())) {
1908         APP_LOGE("fail to SetModuleRemovable due to write InterfaceToken fail");
1909         return false;
1910     }
1911     if (!data.WriteString(bundleName)) {
1912         APP_LOGE("fail to SetModuleRemovable due to write bundleName fail");
1913         return false;
1914     }
1915 
1916     if (!data.WriteString(moduleName)) {
1917         APP_LOGE("fail to SetModuleRemovable due to write moduleName fail");
1918         return false;
1919     }
1920     if (!data.WriteBool(isEnable)) {
1921         APP_LOGE("fail to SetModuleRemovable due to write isEnable fail");
1922         return false;
1923     }
1924     MessageParcel reply;
1925     if (!SendTransactCmd(IBundleMgr::Message::SET_MODULE_REMOVABLE, data, reply)) {
1926         APP_LOGE("fail to SetModuleRemovable from server");
1927         return false;
1928     }
1929     return reply.ReadBool();
1930 }
1931 
GetModuleUpgradeFlag(const std::string & bundleName,const std::string & moduleName)1932 bool BundleMgrProxy::GetModuleUpgradeFlag(const std::string &bundleName, const std::string &moduleName)
1933 {
1934     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1935     APP_LOGD("begin to GetModuleUpgradeFlag of %{public}s", bundleName.c_str());
1936     if (bundleName.empty() || moduleName.empty()) {
1937         APP_LOGE("fail to GetModuleUpgradeFlag due to params empty");
1938         return false;
1939     }
1940 
1941     MessageParcel data;
1942     if (!data.WriteInterfaceToken(GetDescriptor())) {
1943         APP_LOGE("fail to GetModuleUpgradeFlag due to write InterfaceToken fail");
1944         return false;
1945     }
1946     if (!data.WriteString(bundleName)) {
1947         APP_LOGE("fail to GetModuleUpgradeFlag due to write bundleName fail");
1948         return false;
1949     }
1950 
1951     if (!data.WriteString(moduleName)) {
1952         APP_LOGE("fail to GetModuleUpgradeFlag due to write moduleName fail");
1953         return false;
1954     }
1955     MessageParcel reply;
1956     if (!SendTransactCmd(IBundleMgr::Message::IS_MODULE_NEED_UPDATE, data, reply)) {
1957         APP_LOGE("fail to GetModuleUpgradeFlag from server");
1958         return false;
1959     }
1960     return reply.ReadBool();
1961 }
1962 
SetModuleUpgradeFlag(const std::string & bundleName,const std::string & moduleName,int32_t upgradeFlag)1963 ErrCode BundleMgrProxy::SetModuleUpgradeFlag(const std::string &bundleName,
1964     const std::string &moduleName, int32_t upgradeFlag)
1965 {
1966     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1967     APP_LOGD("begin to SetModuleUpgradeFlag of %{public}s", bundleName.c_str());
1968     if (bundleName.empty()) {
1969         APP_LOGE("fail to SetModuleUpgradeFlag due to bundleName empty");
1970         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
1971     }
1972     if (moduleName.empty()) {
1973         APP_LOGE("fail to SetModuleUpgradeFlag due to moduleName empty");
1974         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
1975     }
1976 
1977     MessageParcel data;
1978     if (!data.WriteInterfaceToken(GetDescriptor())) {
1979         APP_LOGE("fail to SetModuleUpgradeFlag due to write InterfaceToken fail");
1980         return ERR_APPEXECFWK_PARCEL_ERROR;
1981     }
1982     if (!data.WriteString(bundleName)) {
1983         APP_LOGE("fail to SetModuleUpgradeFlag due to write bundleName fail");
1984         return ERR_APPEXECFWK_PARCEL_ERROR;
1985     }
1986 
1987     if (!data.WriteString(moduleName)) {
1988         APP_LOGE("fail to SetModuleUpgradeFlag due to write moduleName fail");
1989         return ERR_APPEXECFWK_PARCEL_ERROR;
1990     }
1991     if (!data.WriteInt32(upgradeFlag)) {
1992         APP_LOGE("fail to SetModuleUpgradeFlag due to write isEnable fail");
1993         return ERR_APPEXECFWK_PARCEL_ERROR;
1994     }
1995     MessageParcel reply;
1996     if (!SendTransactCmd(IBundleMgr::Message::SET_MODULE_NEED_UPDATE, data, reply)) {
1997         APP_LOGE("fail to SetModuleUpgradeFlag from server");
1998         return ERR_APPEXECFWK_PARCEL_ERROR;
1999     }
2000     return reply.ReadInt32();
2001 }
2002 
IsApplicationEnabled(const std::string & bundleName,bool & isEnable)2003 ErrCode BundleMgrProxy::IsApplicationEnabled(const std::string &bundleName, bool &isEnable)
2004 {
2005     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2006     APP_LOGD("begin to IsApplicationEnabled of %{public}s", bundleName.c_str());
2007     if (bundleName.empty()) {
2008         APP_LOGE("fail to IsApplicationEnabled due to params empty");
2009         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
2010     }
2011 
2012     MessageParcel data;
2013     if (!data.WriteInterfaceToken(GetDescriptor())) {
2014         APP_LOGE("fail to IsApplicationEnabled due to write InterfaceToken fail");
2015         return ERR_APPEXECFWK_PARCEL_ERROR;
2016     }
2017     if (!data.WriteString(bundleName)) {
2018         APP_LOGE("fail to IsApplicationEnabled due to write bundleName fail");
2019         return ERR_APPEXECFWK_PARCEL_ERROR;
2020     }
2021     MessageParcel reply;
2022     if (!SendTransactCmd(IBundleMgr::Message::IS_APPLICATION_ENABLED, data, reply)) {
2023         return ERR_BUNDLE_MANAGER_IPC_TRANSACTION;
2024     }
2025     int32_t ret = reply.ReadInt32();
2026     if (ret != NO_ERROR) {
2027         return ret;
2028     }
2029     isEnable = reply.ReadBool();
2030     return NO_ERROR;
2031 }
2032 
SetApplicationEnabled(const std::string & bundleName,bool isEnable,int32_t userId)2033 ErrCode BundleMgrProxy::SetApplicationEnabled(const std::string &bundleName, bool isEnable, int32_t userId)
2034 {
2035     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2036     APP_LOGD("begin to SetApplicationEnabled of %{public}s", bundleName.c_str());
2037     if (bundleName.empty()) {
2038         APP_LOGE("fail to SetApplicationEnabled due to params empty");
2039         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
2040     }
2041 
2042     MessageParcel data;
2043     if (!data.WriteInterfaceToken(GetDescriptor())) {
2044         APP_LOGE("fail to SetApplicationEnabled due to write InterfaceToken fail");
2045         return ERR_APPEXECFWK_PARCEL_ERROR;
2046     }
2047     if (!data.WriteString(bundleName)) {
2048         APP_LOGE("fail to SetApplicationEnabled due to write bundleName fail");
2049         return ERR_APPEXECFWK_PARCEL_ERROR;
2050     }
2051     if (!data.WriteBool(isEnable)) {
2052         APP_LOGE("fail to SetApplicationEnabled due to write isEnable fail");
2053         return ERR_APPEXECFWK_PARCEL_ERROR;
2054     }
2055     if (!data.WriteInt32(userId)) {
2056         APP_LOGE("fail to SetApplicationEnabled due to write userId fail");
2057         return ERR_APPEXECFWK_PARCEL_ERROR;
2058     }
2059     MessageParcel reply;
2060     if (!SendTransactCmd(IBundleMgr::Message::SET_APPLICATION_ENABLED, data, reply)) {
2061         return ERR_BUNDLE_MANAGER_IPC_TRANSACTION;
2062     }
2063     return reply.ReadInt32();
2064 }
2065 
IsAbilityEnabled(const AbilityInfo & abilityInfo,bool & isEnable)2066 ErrCode BundleMgrProxy::IsAbilityEnabled(const AbilityInfo &abilityInfo, bool &isEnable)
2067 {
2068     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2069     APP_LOGD("begin to IsAbilityEnabled of %{public}s", abilityInfo.name.c_str());
2070     if (abilityInfo.bundleName.empty() || abilityInfo.name.empty()) {
2071         APP_LOGE("fail to IsAbilityEnabled due to params empty");
2072         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
2073     }
2074     MessageParcel data;
2075     if (!data.WriteInterfaceToken(GetDescriptor())) {
2076         APP_LOGE("fail to IsAbilityEnabled due to write InterfaceToken fail");
2077         return ERR_APPEXECFWK_PARCEL_ERROR;
2078     }
2079     if (!data.WriteParcelable(&abilityInfo)) {
2080         APP_LOGE("fail to IsAbilityEnabled due to write abilityInfo fail");
2081         return ERR_APPEXECFWK_PARCEL_ERROR;
2082     }
2083     MessageParcel reply;
2084     if (!SendTransactCmd(IBundleMgr::Message::IS_ABILITY_ENABLED, data, reply)) {
2085         return ERR_BUNDLE_MANAGER_IPC_TRANSACTION;
2086     }
2087     int32_t ret = reply.ReadInt32();
2088     if (ret != NO_ERROR) {
2089         return ret;
2090     }
2091     isEnable = reply.ReadBool();
2092     return NO_ERROR;
2093 }
2094 
SetAbilityEnabled(const AbilityInfo & abilityInfo,bool isEnabled,int32_t userId)2095 ErrCode BundleMgrProxy::SetAbilityEnabled(const AbilityInfo &abilityInfo, bool isEnabled, int32_t userId)
2096 {
2097     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2098     APP_LOGD("begin to SetAbilityEnabled of %{public}s", abilityInfo.name.c_str());
2099     if (abilityInfo.bundleName.empty() || abilityInfo.name.empty()) {
2100         APP_LOGE("fail to SetAbilityEnabled due to params empty");
2101         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
2102     }
2103     MessageParcel data;
2104     if (!data.WriteInterfaceToken(GetDescriptor())) {
2105         APP_LOGE("fail to SetAbilityEnabled due to write InterfaceToken fail");
2106         return ERR_APPEXECFWK_PARCEL_ERROR;
2107     }
2108     if (!data.WriteParcelable(&abilityInfo)) {
2109         APP_LOGE("fail to SetAbilityEnabled due to write abilityInfo fail");
2110         return ERR_APPEXECFWK_PARCEL_ERROR;
2111     }
2112     if (!data.WriteBool(isEnabled)) {
2113         APP_LOGE("fail to SetAbilityEnabled due to write isEnabled fail");
2114         return ERR_APPEXECFWK_PARCEL_ERROR;
2115     }
2116     if (!data.WriteInt32(userId)) {
2117         APP_LOGE("fail to SetAbilityEnabled due to write userId fail");
2118         return ERR_APPEXECFWK_PARCEL_ERROR;
2119     }
2120 
2121     MessageParcel reply;
2122     if (!SendTransactCmd(IBundleMgr::Message::SET_ABILITY_ENABLED, data, reply)) {
2123         return ERR_BUNDLE_MANAGER_IPC_TRANSACTION;
2124     }
2125     return reply.ReadInt32();
2126 }
2127 
GetAbilityInfo(const std::string & bundleName,const std::string & abilityName,AbilityInfo & abilityInfo)2128 bool BundleMgrProxy::GetAbilityInfo(
2129     const std::string &bundleName, const std::string &abilityName, AbilityInfo &abilityInfo)
2130 {
2131     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2132     APP_LOGD("GetAbilityInfo bundleName :%{public}s, abilityName :%{public}s", bundleName.c_str(), abilityName.c_str());
2133     if (bundleName.empty() || abilityName.empty()) {
2134         APP_LOGE("fail to GetAbilityInfo due to params empty");
2135         return false;
2136     }
2137 
2138     MessageParcel data;
2139     if (!data.WriteInterfaceToken(GetDescriptor())) {
2140         APP_LOGE("fail to GetAbilityInfo due to write MessageParcel fail");
2141         return false;
2142     }
2143     if (!data.WriteString(bundleName)) {
2144         APP_LOGE("fail to GetAbilityInfo due to write bundleName fail");
2145         return false;
2146     }
2147     if (!data.WriteString(abilityName)) {
2148         APP_LOGE("fail to GetAbilityInfo due to write abilityName fail");
2149         return false;
2150     }
2151 
2152     if (!GetParcelableInfo<AbilityInfo>(IBundleMgr::Message::GET_ABILITY_INFO, data, abilityInfo)) {
2153         APP_LOGE("fail to GetAbilityInfo from server");
2154         return false;
2155     }
2156     return true;
2157 }
2158 
GetAbilityInfo(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,AbilityInfo & abilityInfo)2159 bool BundleMgrProxy::GetAbilityInfo(
2160     const std::string &bundleName, const std::string &moduleName,
2161     const std::string &abilityName, AbilityInfo &abilityInfo)
2162 {
2163     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2164     APP_LOGD("GetAbilityInfo:bundleName :%{public}s, moduleName :%{public}s, abilityName :%{public}s",
2165         bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
2166     if (bundleName.empty() || moduleName.empty() || abilityName.empty()) {
2167         APP_LOGE("fail to GetAbilityInfo due to params empty");
2168         return false;
2169     }
2170 
2171     MessageParcel data;
2172     if (!data.WriteInterfaceToken(GetDescriptor())) {
2173         APP_LOGE("fail to GetAbilityInfo due to write MessageParcel fail");
2174         return false;
2175     }
2176     if (!data.WriteString(bundleName)) {
2177         APP_LOGE("fail to GetAbilityInfo due to write bundleName fail");
2178         return false;
2179     }
2180     if (!data.WriteString(moduleName)) {
2181         APP_LOGE("fail to GetAbilityInfo due to write moduleName fail");
2182         return false;
2183     }
2184     if (!data.WriteString(abilityName)) {
2185         APP_LOGE("fail to GetAbilityInfo due to write abilityName fail");
2186         return false;
2187     }
2188 
2189     if (!GetParcelableInfo<AbilityInfo>(IBundleMgr::Message::GET_ABILITY_INFO_WITH_MODULE_NAME, data, abilityInfo)) {
2190         APP_LOGE("fail to GetAbilityInfo from server");
2191         return false;
2192     }
2193     return true;
2194 }
2195 
GetBundleInstaller()2196 sptr<IBundleInstaller> BundleMgrProxy::GetBundleInstaller()
2197 {
2198     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2199     APP_LOGD("begin to get bundle installer");
2200     MessageParcel data;
2201     MessageParcel reply;
2202     if (!data.WriteInterfaceToken(GetDescriptor())) {
2203         APP_LOGE("fail to GetBundleInstaller due to write InterfaceToken fail");
2204         return nullptr;
2205     }
2206     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLE_INSTALLER, data, reply)) {
2207         return nullptr;
2208     }
2209 
2210     sptr<IRemoteObject> object = reply.ReadObject<IRemoteObject>();
2211     if (object == nullptr) {
2212         APP_LOGE("read failed");
2213         return nullptr;
2214     }
2215     sptr<IBundleInstaller> installer = iface_cast<IBundleInstaller>(object);
2216     if (installer == nullptr) {
2217         APP_LOGE("bundle installer is nullptr");
2218     }
2219 
2220     APP_LOGD("get bundle installer success");
2221     return installer;
2222 }
2223 
GetBundleUserMgr()2224 sptr<IBundleUserMgr> BundleMgrProxy::GetBundleUserMgr()
2225 {
2226     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2227     MessageParcel data;
2228     MessageParcel reply;
2229     if (!data.WriteInterfaceToken(GetDescriptor())) {
2230         APP_LOGE("fail to get bundle user mgr due to write InterfaceToken fail");
2231         return nullptr;
2232     }
2233     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLE_USER_MGR, data, reply)) {
2234         return nullptr;
2235     }
2236 
2237     sptr<IRemoteObject> object = reply.ReadObject<IRemoteObject>();
2238     if (object == nullptr) {
2239         APP_LOGE("read failed");
2240         return nullptr;
2241     }
2242     sptr<IBundleUserMgr> bundleUserMgr = iface_cast<IBundleUserMgr>(object);
2243     if (bundleUserMgr == nullptr) {
2244         APP_LOGE("bundleUserMgr is nullptr");
2245     }
2246 
2247     return bundleUserMgr;
2248 }
2249 
GetAllFormsInfo(std::vector<FormInfo> & formInfos)2250 bool BundleMgrProxy::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
2251 {
2252     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2253     MessageParcel data;
2254     if (!data.WriteInterfaceToken(GetDescriptor())) {
2255         APP_LOGE("fail to GetAllFormsInfo due to write MessageParcel fail");
2256         return false;
2257     }
2258 
2259     if (!GetParcelableInfos<FormInfo>(IBundleMgr::Message::GET_ALL_FORMS_INFO, data, formInfos)) {
2260         APP_LOGE("fail to GetAllFormsInfo from server");
2261         return false;
2262     }
2263     return true;
2264 }
2265 
GetFormsInfoByApp(const std::string & bundleName,std::vector<FormInfo> & formInfos)2266 bool BundleMgrProxy::GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfos)
2267 {
2268     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2269     if (bundleName.empty()) {
2270         APP_LOGE("fail to GetFormsInfoByApp due to params empty");
2271         return false;
2272     }
2273 
2274     MessageParcel data;
2275     if (!data.WriteInterfaceToken(GetDescriptor())) {
2276         APP_LOGE("fail to GetFormsInfoByApp due to write MessageParcel fail");
2277         return false;
2278     }
2279     if (!data.WriteString(bundleName)) {
2280         APP_LOGE("fail to GetFormsInfoByApp due to write bundleName fail");
2281         return false;
2282     }
2283     if (!GetParcelableInfos<FormInfo>(IBundleMgr::Message::GET_FORMS_INFO_BY_APP, data, formInfos)) {
2284         APP_LOGE("fail to GetFormsInfoByApp from server");
2285         return false;
2286     }
2287     return true;
2288 }
2289 
GetFormsInfoByModule(const std::string & bundleName,const std::string & moduleName,std::vector<FormInfo> & formInfos)2290 bool BundleMgrProxy::GetFormsInfoByModule(
2291     const std::string &bundleName, const std::string &moduleName, std::vector<FormInfo> &formInfos)
2292 {
2293     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2294     if (bundleName.empty() || moduleName.empty()) {
2295         APP_LOGE("fail to GetFormsByModule due to params empty");
2296         return false;
2297     }
2298 
2299     MessageParcel data;
2300     if (!data.WriteInterfaceToken(GetDescriptor())) {
2301         APP_LOGE("fail to GetFormsInfoByModule due to write MessageParcel fail");
2302         return false;
2303     }
2304 
2305     if (!data.WriteString(bundleName)) {
2306         APP_LOGE("fail to GetFormsInfoByModule due to write bundleName fail");
2307         return false;
2308     }
2309 
2310     if (!data.WriteString(moduleName)) {
2311         APP_LOGE("fail to GetFormsInfoByModule due to write moduleName fail");
2312         return false;
2313     }
2314 
2315     if (!GetParcelableInfos<FormInfo>(IBundleMgr::Message::GET_FORMS_INFO_BY_MODULE, data, formInfos)) {
2316         APP_LOGE("fail to GetFormsInfoByModule from server");
2317         return false;
2318     }
2319     return true;
2320 }
2321 
GetShortcutInfos(const std::string & bundleName,std::vector<ShortcutInfo> & shortcutInfos)2322 bool BundleMgrProxy::GetShortcutInfos(const std::string &bundleName, std::vector<ShortcutInfo> &shortcutInfos)
2323 {
2324     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2325     if (bundleName.empty()) {
2326         APP_LOGE("fail to GetShortcutInfos due to params empty");
2327         return false;
2328     }
2329 
2330     MessageParcel data;
2331     if (!data.WriteInterfaceToken(GetDescriptor())) {
2332         APP_LOGE("fail to GetShortcutInfos due to write MessageParcel fail");
2333         return false;
2334     }
2335 
2336     if (!data.WriteString(bundleName)) {
2337         APP_LOGE("fail to GetShortcutInfos due to write bundleName fail");
2338         return false;
2339     }
2340 
2341     if (!GetParcelableInfos<ShortcutInfo>(IBundleMgr::Message::GET_SHORTCUT_INFO, data, shortcutInfos)) {
2342         APP_LOGE("fail to GetShortcutInfos from server");
2343         return false;
2344     }
2345     return true;
2346 }
2347 
GetShortcutInfoV9(const std::string & bundleName,std::vector<ShortcutInfo> & shortcutInfos)2348 ErrCode BundleMgrProxy::GetShortcutInfoV9(const std::string &bundleName,
2349     std::vector<ShortcutInfo> &shortcutInfos)
2350 {
2351     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2352     if (bundleName.empty()) {
2353         APP_LOGE("fail to GetShortcutInfos due to params empty");
2354         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2355     }
2356 
2357     MessageParcel data;
2358     if (!data.WriteInterfaceToken(GetDescriptor())) {
2359         APP_LOGE("fail to GetShortcutInfos due to write MessageParcel fail");
2360         return ERR_APPEXECFWK_PARCEL_ERROR;
2361     }
2362 
2363     if (!data.WriteString(bundleName)) {
2364         APP_LOGE("fail to GetShortcutInfos due to write bundleName fail");
2365         return ERR_APPEXECFWK_PARCEL_ERROR;
2366     }
2367     return GetParcelableInfosWithErrCode<ShortcutInfo>(IBundleMgr::Message::GET_SHORTCUT_INFO_V9, data, shortcutInfos);
2368 }
2369 
GetAllCommonEventInfo(const std::string & eventKey,std::vector<CommonEventInfo> & commonEventInfos)2370 bool BundleMgrProxy::GetAllCommonEventInfo(const std::string &eventKey, std::vector<CommonEventInfo> &commonEventInfos)
2371 {
2372     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2373     if (eventKey.empty()) {
2374         APP_LOGE("fail to GetAllCommonEventInfo due to eventKey empty");
2375         return false;
2376     }
2377 
2378     MessageParcel data;
2379     if (!data.WriteInterfaceToken(GetDescriptor())) {
2380         APP_LOGE("fail to GetAllCommonEventInfo due to write MessageParcel fail");
2381         return false;
2382     }
2383 
2384     if (!data.WriteString(eventKey)) {
2385         APP_LOGE("fail to GetAllCommonEventInfo due to write eventKey fail");
2386         return false;
2387     }
2388 
2389     if (!GetParcelableInfos<CommonEventInfo>(IBundleMgr::Message::GET_ALL_COMMON_EVENT_INFO, data, commonEventInfos)) {
2390         APP_LOGE("fail to GetAllCommonEventInfo from server");
2391         return false;
2392     }
2393     return true;
2394 }
2395 
GetDistributedBundleInfo(const std::string & networkId,const std::string & bundleName,DistributedBundleInfo & distributedBundleInfo)2396 bool BundleMgrProxy::GetDistributedBundleInfo(const std::string &networkId, const std::string &bundleName,
2397     DistributedBundleInfo &distributedBundleInfo)
2398 {
2399     APP_LOGD("begin to GetDistributedBundleInfo of %{public}s", bundleName.c_str());
2400     if (networkId.empty() || bundleName.empty()) {
2401         APP_LOGE("fail to GetDistributedBundleInfo due to params empty");
2402         return false;
2403     }
2404     MessageParcel data;
2405     if (!data.WriteInterfaceToken(GetDescriptor())) {
2406         APP_LOGE("fail to GetDistributedBundleInfo due to write MessageParcel fail");
2407         return false;
2408     }
2409 
2410     if (!data.WriteString(networkId)) {
2411         APP_LOGE("fail to GetDistributedBundleInfo due to write networkId fail");
2412         return false;
2413     }
2414 
2415     if (!data.WriteString(bundleName)) {
2416         APP_LOGE("fail to GetDistributedBundleInfo due to write bundleName fail");
2417         return false;
2418     }
2419     MessageParcel reply;
2420     if (!GetParcelableInfo<DistributedBundleInfo>(
2421             IBundleMgr::Message::GET_DISTRIBUTE_BUNDLE_INFO, data, distributedBundleInfo)) {
2422         APP_LOGE("fail to GetDistributedBundleInfo from server");
2423         return false;
2424     }
2425     return true;
2426 }
2427 
GetAppPrivilegeLevel(const std::string & bundleName,int32_t userId)2428 std::string BundleMgrProxy::GetAppPrivilegeLevel(const std::string &bundleName, int32_t userId)
2429 {
2430     APP_LOGD("begin to GetAppPrivilegeLevel of %{public}s", bundleName.c_str());
2431     if (bundleName.empty()) {
2432         APP_LOGE("fail to GetAppPrivilegeLevel due to params empty");
2433         return Constants::EMPTY_STRING;
2434     }
2435     MessageParcel data;
2436     if (!data.WriteInterfaceToken(GetDescriptor())) {
2437         APP_LOGE("fail to GetAppPrivilegeLevel due to write InterfaceToken fail");
2438         return Constants::EMPTY_STRING;
2439     }
2440     if (!data.WriteString(bundleName)) {
2441         APP_LOGE("fail to GetAppPrivilegeLevel due to write bundleName fail");
2442         return Constants::EMPTY_STRING;
2443     }
2444     if (!data.WriteInt32(userId)) {
2445         APP_LOGE("fail to GetAppPrivilegeLevel due to write userId fail");
2446         return Constants::EMPTY_STRING;
2447     }
2448 
2449     MessageParcel reply;
2450     if (!SendTransactCmd(IBundleMgr::Message::GET_APPLICATION_PRIVILEGE_LEVEL, data, reply)) {
2451         APP_LOGE("fail to GetAppPrivilegeLevel from server");
2452         return Constants::EMPTY_STRING;
2453     }
2454     return reply.ReadString();
2455 }
2456 
QueryExtensionAbilityInfos(const Want & want,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2457 bool BundleMgrProxy::QueryExtensionAbilityInfos(const Want &want, const int32_t &flag, const int32_t &userId,
2458     std::vector<ExtensionAbilityInfo> &extensionInfos)
2459 {
2460     MessageParcel data;
2461     if (!data.WriteInterfaceToken(GetDescriptor())) {
2462         APP_LOGE("fail to QueryExtensionAbilityInfos due to write InterfaceToken fail");
2463         return false;
2464     }
2465     if (!data.WriteParcelable(&want)) {
2466         APP_LOGE("fail to QueryExtensionAbilityInfos due to write want fail");
2467         return false;
2468     }
2469     if (!data.WriteInt32(flag)) {
2470         APP_LOGE("fail to QueryExtensionAbilityInfos due to write flag fail");
2471         return false;
2472     }
2473     if (!data.WriteInt32(userId)) {
2474         APP_LOGE("fail to QueryExtensionAbilityInfos due to write userId fail");
2475         return false;
2476     }
2477 
2478     if (!GetParcelableInfos(IBundleMgr::Message::QUERY_EXTENSION_INFO_WITHOUT_TYPE, data, extensionInfos)) {
2479         APP_LOGE("fail to obtain extensionInfos");
2480         return false;
2481     }
2482     return true;
2483 }
2484 
QueryExtensionAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2485 ErrCode BundleMgrProxy::QueryExtensionAbilityInfosV9(const Want &want, int32_t flags, int32_t userId,
2486     std::vector<ExtensionAbilityInfo> &extensionInfos)
2487 {
2488     MessageParcel data;
2489     if (!data.WriteInterfaceToken(GetDescriptor())) {
2490         APP_LOGE("fail to QueryExtensionAbilityInfosV9 due to write InterfaceToken fail");
2491         return ERR_APPEXECFWK_PARCEL_ERROR;
2492     }
2493     if (!data.WriteParcelable(&want)) {
2494         APP_LOGE("fail to QueryExtensionAbilityInfosV9 due to write want fail");
2495         return ERR_APPEXECFWK_PARCEL_ERROR;
2496     }
2497     if (!data.WriteInt32(flags)) {
2498         APP_LOGE("fail to QueryExtensionAbilityInfosV9 due to write flag fail");
2499         return ERR_APPEXECFWK_PARCEL_ERROR;
2500     }
2501     if (!data.WriteInt32(userId)) {
2502         APP_LOGE("fail to QueryExtensionAbilityInfosV9 due to write userId fail");
2503         return ERR_APPEXECFWK_PARCEL_ERROR;
2504     }
2505     return GetParcelableInfosWithErrCode(
2506         IBundleMgr::Message::QUERY_EXTENSION_INFO_WITHOUT_TYPE_V9, data, extensionInfos);
2507 }
2508 
QueryExtensionAbilityInfos(const Want & want,const ExtensionAbilityType & extensionType,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2509 bool BundleMgrProxy::QueryExtensionAbilityInfos(const Want &want, const ExtensionAbilityType &extensionType,
2510     const int32_t &flag, const int32_t &userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
2511 {
2512     MessageParcel data;
2513     if (!data.WriteInterfaceToken(GetDescriptor())) {
2514         APP_LOGE("fail to QueryExtensionAbilityInfos due to write InterfaceToken fail");
2515         return false;
2516     }
2517     if (!data.WriteParcelable(&want)) {
2518         APP_LOGE("fail to QueryExtensionAbilityInfos due to write want fail");
2519         return false;
2520     }
2521     if (!data.WriteInt32(static_cast<int32_t>(extensionType))) {
2522         APP_LOGE("fail to QueryExtensionAbilityInfos due to write type fail");
2523         return false;
2524     }
2525     if (!data.WriteInt32(flag)) {
2526         APP_LOGE("fail to QueryExtensionAbilityInfos due to write flag fail");
2527         return false;
2528     }
2529     if (!data.WriteInt32(userId)) {
2530         APP_LOGE("fail to QueryExtensionAbilityInfos due to write userId fail");
2531         return false;
2532     }
2533 
2534     if (!GetParcelableInfos(IBundleMgr::Message::QUERY_EXTENSION_INFO, data, extensionInfos)) {
2535         APP_LOGE("fail to obtain extensionInfos");
2536         return false;
2537     }
2538     return true;
2539 }
2540 
QueryExtensionAbilityInfosV9(const Want & want,const ExtensionAbilityType & extensionType,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2541 ErrCode BundleMgrProxy::QueryExtensionAbilityInfosV9(const Want &want, const ExtensionAbilityType &extensionType,
2542     int32_t flags, int32_t userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
2543 {
2544     MessageParcel data;
2545     if (!data.WriteInterfaceToken(GetDescriptor())) {
2546         APP_LOGE("fail to QueryExtensionAbilityInfosV9 due to write InterfaceToken fail");
2547         return ERR_APPEXECFWK_PARCEL_ERROR;
2548     }
2549     if (!data.WriteParcelable(&want)) {
2550         APP_LOGE("fail to QueryExtensionAbilityInfosV9 due to write want fail");
2551         return ERR_APPEXECFWK_PARCEL_ERROR;
2552     }
2553     if (!data.WriteInt32(static_cast<int32_t>(extensionType))) {
2554         APP_LOGE("fail to QueryExtensionAbilityInfosV9 due to write type fail");
2555         return ERR_APPEXECFWK_PARCEL_ERROR;
2556     }
2557     if (!data.WriteInt32(flags)) {
2558         APP_LOGE("fail to QueryExtensionAbilityInfosV9 due to write flag fail");
2559         return ERR_APPEXECFWK_PARCEL_ERROR;
2560     }
2561     if (!data.WriteInt32(userId)) {
2562         APP_LOGE("fail to QueryExtensionAbilityInfosV9 due to write userId fail");
2563         return ERR_APPEXECFWK_PARCEL_ERROR;
2564     }
2565     return GetParcelableInfosWithErrCode(IBundleMgr::Message::QUERY_EXTENSION_INFO_V9, data, extensionInfos);
2566 }
2567 
QueryExtensionAbilityInfos(const ExtensionAbilityType & extensionType,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2568 bool BundleMgrProxy::QueryExtensionAbilityInfos(const ExtensionAbilityType &extensionType, const int32_t &userId,
2569     std::vector<ExtensionAbilityInfo> &extensionInfos)
2570 {
2571     MessageParcel data;
2572     if (!data.WriteInterfaceToken(GetDescriptor())) {
2573         APP_LOGE("fail to QueryExtensionAbilityInfos due to write InterfaceToken fail");
2574         return false;
2575     }
2576     if (!data.WriteInt32(static_cast<int32_t>(extensionType))) {
2577         APP_LOGE("fail to QueryExtensionAbilityInfos due to write type fail");
2578         return false;
2579     }
2580     if (!data.WriteInt32(userId)) {
2581         APP_LOGE("fail to QueryExtensionAbilityInfos due to write userId fail");
2582         return false;
2583     }
2584 
2585     if (!GetParcelableInfos(IBundleMgr::Message::QUERY_EXTENSION_INFO_BY_TYPE, data, extensionInfos)) {
2586         APP_LOGE("fail to obtain extensionInfos");
2587         return false;
2588     }
2589     return true;
2590 }
2591 
VerifyCallingPermission(const std::string & permission)2592 bool BundleMgrProxy::VerifyCallingPermission(const std::string &permission)
2593 {
2594     APP_LOGD("VerifyCallingPermission begin");
2595     MessageParcel data;
2596     if (!data.WriteInterfaceToken(GetDescriptor())) {
2597         APP_LOGE("fail to VerifyCallingPermission due to write InterfaceToken fail");
2598         return false;
2599     }
2600 
2601     if (!data.WriteString(permission)) {
2602         APP_LOGE("fail to VerifyCallingPermission due to write bundleName fail");
2603         return false;
2604     }
2605 
2606     MessageParcel reply;
2607     if (!SendTransactCmd(IBundleMgr::Message::VERIFY_CALLING_PERMISSION, data, reply)) {
2608         APP_LOGE("fail to sendRequest");
2609         return false;
2610     }
2611     return reply.ReadBool();
2612 }
2613 
GetAccessibleAppCodePaths(int32_t userId)2614 std::vector<std::string> BundleMgrProxy::GetAccessibleAppCodePaths(int32_t userId)
2615 {
2616     APP_LOGD("GetAccessibleAppCodePaths begin");
2617     MessageParcel data;
2618     std::vector<std::string> vec;
2619     if (!data.WriteInterfaceToken(GetDescriptor())) {
2620         APP_LOGE("fail to GetAccessibleAppCodePaths due to write InterfaceToken fail");
2621         return vec;
2622     }
2623 
2624     if (!data.WriteInt32(userId)) {
2625         APP_LOGE("fail to GetAccessibleAppCodePaths due to write userId fail");
2626         return vec;
2627     }
2628 
2629     MessageParcel reply;
2630     if (!SendTransactCmd(IBundleMgr::Message::GET_ACCESSIBLE_APP_CODE_PATH, data, reply)) {
2631         APP_LOGE("fail to sendRequest");
2632         return vec;
2633     }
2634 
2635     if (!reply.ReadBool()) {
2636         APP_LOGE("reply result false");
2637         return vec;
2638     }
2639 
2640     if (!reply.ReadStringVector(&vec)) {
2641         APP_LOGE("fail to GetAccessibleAppCodePaths from reply");
2642         return vec;
2643     }
2644     return vec;
2645 }
2646 
QueryExtensionAbilityInfoByUri(const std::string & uri,int32_t userId,ExtensionAbilityInfo & extensionAbilityInfo)2647 bool BundleMgrProxy::QueryExtensionAbilityInfoByUri(const std::string &uri, int32_t userId,
2648     ExtensionAbilityInfo &extensionAbilityInfo)
2649 {
2650     APP_LOGD("begin to QueryExtensionAbilityInfoByUri");
2651     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2652     if (uri.empty()) {
2653         APP_LOGE("uri is empty");
2654         return false;
2655     }
2656     MessageParcel data;
2657     if (!data.WriteInterfaceToken(GetDescriptor())) {
2658         APP_LOGE("failed to QueryExtensionAbilityInfoByUri due to write MessageParcel fail");
2659         return false;
2660     }
2661     if (!data.WriteString(uri)) {
2662         APP_LOGE("failed to QueryExtensionAbilityInfoByUri due to write uri fail");
2663         return false;
2664     }
2665     if (!data.WriteInt32(userId)) {
2666         APP_LOGE("failed to QueryExtensionAbilityInfoByUri due to write userId fail");
2667         return false;
2668     }
2669 
2670     if (!GetParcelableInfo<ExtensionAbilityInfo>(
2671         IBundleMgr::Message::QUERY_EXTENSION_ABILITY_INFO_BY_URI, data, extensionAbilityInfo)) {
2672         APP_LOGE("failed to QueryExtensionAbilityInfoByUri from server");
2673         return false;
2674     }
2675     return true;
2676 }
2677 
ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)2678 bool BundleMgrProxy::ImplicitQueryInfoByPriority(const Want &want, int32_t flags, int32_t userId,
2679     AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo)
2680 {
2681     APP_LOGD("begin to ImplicitQueryInfoByPriority");
2682     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2683     MessageParcel data;
2684     if (!data.WriteInterfaceToken(GetDescriptor())) {
2685         APP_LOGE("fail to implicit query info by priority due to write MessageParcel fail");
2686         return false;
2687     }
2688     if (!data.WriteParcelable(&want)) {
2689         APP_LOGE("fail to implicit query info by priority due to write want fail");
2690         return false;
2691     }
2692     if (!data.WriteInt32(flags)) {
2693         APP_LOGE("fail to implicit query info by priority due to write flags fail");
2694         return false;
2695     }
2696     if (!data.WriteInt32(userId)) {
2697         APP_LOGE("fail to implicit query info by priority due to write userId error");
2698         return false;
2699     }
2700 
2701     MessageParcel reply;
2702     if (!SendTransactCmd(IBundleMgr::Message::IMPLICIT_QUERY_INFO_BY_PRIORITY, data, reply)) {
2703         return false;
2704     }
2705 
2706     if (!reply.ReadBool()) {
2707         APP_LOGE("reply result false");
2708         return false;
2709     }
2710 
2711     std::unique_ptr<AbilityInfo> abilityInfoPtr(reply.ReadParcelable<AbilityInfo>());
2712     if (abilityInfoPtr == nullptr) {
2713         APP_LOGE("read AbilityInfo failed");
2714         return false;
2715     }
2716     abilityInfo = *abilityInfoPtr;
2717 
2718     std::unique_ptr<ExtensionAbilityInfo> extensionInfoPtr(reply.ReadParcelable<ExtensionAbilityInfo>());
2719     if (extensionInfoPtr == nullptr) {
2720         APP_LOGE("read ExtensionAbilityInfo failed");
2721         return false;
2722     }
2723     extensionInfo = *extensionInfoPtr;
2724     return true;
2725 }
2726 
ImplicitQueryInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,std::vector<ExtensionAbilityInfo> & extensionInfos)2727 bool BundleMgrProxy::ImplicitQueryInfos(const Want &want, int32_t flags, int32_t userId,
2728     std::vector<AbilityInfo> &abilityInfos, std::vector<ExtensionAbilityInfo> &extensionInfos)
2729 {
2730     APP_LOGD("begin to ImplicitQueryInfos");
2731     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2732     MessageParcel data;
2733     if (!data.WriteInterfaceToken(GetDescriptor())) {
2734         APP_LOGE("WriteInterfaceToken failed.");
2735         return false;
2736     }
2737     if (!data.WriteParcelable(&want)) {
2738         APP_LOGE("WriteParcelable want failed.");
2739         return false;
2740     }
2741     if (!data.WriteInt32(flags)) {
2742         APP_LOGE("WriteInt32 flags failed.");
2743         return false;
2744     }
2745     if (!data.WriteInt32(userId)) {
2746         APP_LOGE("WriteInt32 userId failed.");
2747         return false;
2748     }
2749 
2750     MessageParcel reply;
2751     if (!SendTransactCmd(IBundleMgr::Message::IMPLICIT_QUERY_INFOS, data, reply)) {
2752         return false;
2753     }
2754     if (!reply.ReadBool()) {
2755         APP_LOGE("reply result false.");
2756         return false;
2757     }
2758     int32_t abilityInfoSize = reply.ReadInt32();
2759     for (int32_t i = 0; i < abilityInfoSize; i++) {
2760         std::unique_ptr<AbilityInfo> abilityInfoPtr(reply.ReadParcelable<AbilityInfo>());
2761         if (abilityInfoPtr == nullptr) {
2762             APP_LOGE("Read Parcelable abilityInfos failed.");
2763             return false;
2764         }
2765         abilityInfos.emplace_back(*abilityInfoPtr);
2766     }
2767     int32_t extensionInfoSize = reply.ReadInt32();
2768     for (int32_t i = 0; i < extensionInfoSize; i++) {
2769         std::unique_ptr<ExtensionAbilityInfo> extensionInfoPtr(reply.ReadParcelable<ExtensionAbilityInfo>());
2770         if (extensionInfoPtr == nullptr) {
2771             APP_LOGE("Read Parcelable extensionInfos failed.");
2772             return false;
2773         }
2774         extensionInfos.emplace_back(*extensionInfoPtr);
2775     }
2776     return true;
2777 }
2778 
GetSandboxBundleInfo(const std::string & bundleName,int32_t appIndex,int32_t userId,BundleInfo & info)2779 ErrCode BundleMgrProxy::GetSandboxBundleInfo(const std::string &bundleName, int32_t appIndex, int32_t userId,
2780     BundleInfo &info)
2781 {
2782     APP_LOGD("begin to GetSandboxBundleInfo");
2783     if (bundleName.empty() || appIndex <= Constants::INITIAL_APP_INDEX) {
2784         APP_LOGE("GetSandboxBundleInfo params are invalid");
2785         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
2786     }
2787 
2788     MessageParcel data;
2789     if (!data.WriteInterfaceToken(GetDescriptor())) {
2790         APP_LOGE("failed to GetSandboxBundleInfo due to write MessageParcel fail");
2791         return ERR_APPEXECFWK_SANDBOX_INSTALL_WRITE_PARCEL_ERROR;
2792     }
2793     if (!data.WriteString(bundleName)) {
2794         APP_LOGE("failed to GetSandboxBundleInfo due to write bundleName fail");
2795         return ERR_APPEXECFWK_SANDBOX_INSTALL_WRITE_PARCEL_ERROR;
2796     }
2797     if (!data.WriteInt32(appIndex)) {
2798         APP_LOGE("failed to GetSandboxBundleInfo due to write appIndex fail");
2799         return ERR_APPEXECFWK_SANDBOX_INSTALL_WRITE_PARCEL_ERROR;
2800     }
2801     if (!data.WriteInt32(userId)) {
2802         APP_LOGE("failed to GetSandboxBundleInfo due to write userId fail");
2803         return ERR_APPEXECFWK_SANDBOX_INSTALL_WRITE_PARCEL_ERROR;
2804     }
2805 
2806     return GetParcelableInfoWithErrCode<BundleInfo>(IBundleMgr::Message::GET_SANDBOX_APP_BUNDLE_INFO, data, info);
2807 }
2808 
GetAllDependentModuleNames(const std::string & bundleName,const std::string & moduleName,std::vector<std::string> & dependentModuleNames)2809 bool BundleMgrProxy::GetAllDependentModuleNames(const std::string &bundleName, const std::string &moduleName,
2810     std::vector<std::string> &dependentModuleNames)
2811 {
2812     APP_LOGD("begin to GetAllDependentModuleNames");
2813     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2814     if (bundleName.empty() || moduleName.empty()) {
2815         APP_LOGE("bundleName or moduleName is empty");
2816         return false;
2817     }
2818     MessageParcel data;
2819     if (!data.WriteInterfaceToken(GetDescriptor())) {
2820         APP_LOGE("failed to GetAllDependentModuleNames due to write MessageParcel fail");
2821         return false;
2822     }
2823     if (!data.WriteString(bundleName)) {
2824         APP_LOGE("failed to GetAllDependentModuleNames due to write bundleName fail");
2825         return false;
2826     }
2827     if (!data.WriteString(moduleName)) {
2828         APP_LOGE("failed to GetAllDependentModuleNames due to write moduleName fail");
2829         return false;
2830     }
2831 
2832     MessageParcel reply;
2833     if (!SendTransactCmd(IBundleMgr::Message::GET_ALL_DEPENDENT_MODULE_NAMES, data, reply)) {
2834         APP_LOGE("fail to GetAllDependentModuleNames from server");
2835         return false;
2836     }
2837     if (!reply.ReadBool()) {
2838         APP_LOGE("reply result false");
2839         return false;
2840     }
2841     if (!reply.ReadStringVector(&dependentModuleNames)) {
2842         APP_LOGE("fail to GetAllDependentModuleNames from reply");
2843         return false;
2844     }
2845     return true;
2846 }
2847 
SetDisposedStatus(const std::string & bundleName,int32_t status)2848 bool BundleMgrProxy::SetDisposedStatus(const std::string &bundleName, int32_t status)
2849 {
2850     APP_LOGD("begin to SetDisposedStatus");
2851     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2852     if (bundleName.empty()) {
2853         APP_LOGE("SetDisposedStatus bundleName is empty");
2854         return false;
2855     }
2856     MessageParcel data;
2857     if (!data.WriteInterfaceToken(GetDescriptor())) {
2858         APP_LOGE("failed to SetDisposedStatus due to write MessageParcel fail");
2859         return false;
2860     }
2861     if (!data.WriteString(bundleName)) {
2862         APP_LOGE("failed to SetDisposedStatus due to write bundleName fail");
2863         return false;
2864     }
2865     if (!data.WriteInt32(status)) {
2866         APP_LOGE("fail to SetDisposedStatus due to write status fail");
2867         return false;
2868     }
2869 
2870     MessageParcel reply;
2871     if (!SendTransactCmd(IBundleMgr::Message::SET_DISPOSED_STATUS, data, reply)) {
2872         APP_LOGE("fail to SetDisposedStatus from server");
2873         return false;
2874     }
2875     return reply.ReadBool();
2876 }
2877 
GetDisposedStatus(const std::string & bundleName)2878 int32_t BundleMgrProxy::GetDisposedStatus(const std::string &bundleName)
2879 {
2880     APP_LOGD("begin to GetDisposedStatus");
2881     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2882     if (bundleName.empty()) {
2883         APP_LOGE("GetDisposedStatus bundleName is empty");
2884         return false;
2885     }
2886     MessageParcel data;
2887     if (!data.WriteInterfaceToken(GetDescriptor())) {
2888         APP_LOGE("failed to GetDisposedStatus due to write MessageParcel fail");
2889         return false;
2890     }
2891     if (!data.WriteString(bundleName)) {
2892         APP_LOGE("failed to GetDisposedStatus due to write bundleName fail");
2893         return false;
2894     }
2895 
2896     MessageParcel reply;
2897     if (!SendTransactCmd(IBundleMgr::Message::GET_DISPOSED_STATUS, data, reply)) {
2898         APP_LOGE("fail to GetDisposedStatus from server");
2899         return false;
2900     }
2901     return reply.ReadInt32();
2902 }
2903 
ObtainCallingBundleName(std::string & bundleName)2904 bool BundleMgrProxy::ObtainCallingBundleName(std::string &bundleName)
2905 {
2906     APP_LOGD("begin to ObtainCallingBundleName");
2907     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2908 
2909     MessageParcel data;
2910     if (!data.WriteInterfaceToken(GetDescriptor())) {
2911         APP_LOGE("failed to ObtainCallingBundleName due to write MessageParcel fail");
2912         return false;
2913     }
2914 
2915     MessageParcel reply;
2916     if (!SendTransactCmd(IBundleMgr::Message::QUERY_CALLING_BUNDLE_NAME, data, reply)) {
2917         APP_LOGE("fail to ObtainCallingBundleName from server");
2918         return false;
2919     }
2920     if (!reply.ReadBool()) {
2921         APP_LOGE("reply result false");
2922         return false;
2923     }
2924     bundleName = reply.ReadString();
2925     if (bundleName.empty()) {
2926         APP_LOGE("bundleName is empty");
2927         return false;
2928     }
2929     return true;
2930 }
2931 
GetBundleStats(const std::string & bundleName,int32_t userId,std::vector<int64_t> & bundleStats)2932 bool BundleMgrProxy::GetBundleStats(const std::string &bundleName, int32_t userId,
2933     std::vector<int64_t> &bundleStats)
2934 {
2935     APP_LOGD("begin to GetBundleStats");
2936     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2937     MessageParcel data;
2938     if (!data.WriteInterfaceToken(GetDescriptor())) {
2939         APP_LOGE("failed to GetBundleStats due to write MessageParcel fail");
2940         return false;
2941     }
2942     if (!data.WriteString(bundleName)) {
2943         APP_LOGE("fail to GetBundleStats due to write bundleName fail");
2944         return false;
2945     }
2946     if (!data.WriteInt32(userId)) {
2947         APP_LOGE("fail to GetBundleStats due to write userId fail");
2948         return false;
2949     }
2950 
2951     MessageParcel reply;
2952     if (!SendTransactCmd(IBundleMgr::Message::GET_BUNDLE_STATS, data, reply)) {
2953         APP_LOGE("fail to GetBundleStats from server");
2954         return false;
2955     }
2956     if (!reply.ReadBool()) {
2957         APP_LOGE("reply result false");
2958         return false;
2959     }
2960     if (!reply.ReadInt64Vector(&bundleStats)) {
2961         APP_LOGE("fail to GetBundleStats from reply");
2962         return false;
2963     }
2964     return true;
2965 }
2966 
CheckAbilityEnableInstall(const Want & want,int32_t missionId,int32_t userId,const sptr<IRemoteObject> & callback)2967 bool BundleMgrProxy::CheckAbilityEnableInstall(
2968     const Want &want, int32_t missionId, int32_t userId, const sptr<IRemoteObject> &callback)
2969 {
2970     APP_LOGD("begin to CheckAbilityEnableInstall");
2971     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2972 
2973     MessageParcel data;
2974     if (!data.WriteInterfaceToken(GetDescriptor())) {
2975         APP_LOGE("failed to CheckAbilityEnableInstall due to write MessageParcel fail");
2976         return false;
2977     }
2978 
2979     if (!data.WriteParcelable(&want)) {
2980         APP_LOGE("fail to CheckAbilityEnableInstall due to write want fail");
2981         return false;
2982     }
2983 
2984     if (!data.WriteInt32(missionId)) {
2985         APP_LOGE("fail to CheckAbilityEnableInstall due to write missionId fail");
2986         return false;
2987     }
2988 
2989     if (!data.WriteInt32(userId)) {
2990         APP_LOGE("fail to CheckAbilityEnableInstall due to write userId fail");
2991         return false;
2992     }
2993 
2994     if (!data.WriteRemoteObject(callback)) {
2995         APP_LOGE("fail to callback, for write parcel");
2996         return false;
2997     }
2998 
2999     MessageParcel reply;
3000     if (!SendTransactCmd(IBundleMgr::Message::CHECK_ABILITY_ENABLE_INSTALL, data, reply)) {
3001         return false;
3002     }
3003     return reply.ReadBool();
3004 }
3005 
GetStringById(const std::string & bundleName,const std::string & moduleName,uint32_t resId,int32_t userId,const std::string & localeInfo)3006 std::string BundleMgrProxy::GetStringById(const std::string &bundleName, const std::string &moduleName,
3007     uint32_t resId, int32_t userId, const std::string &localeInfo)
3008 {
3009     APP_LOGD("begin to GetStringById.");
3010     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3011     if (bundleName.empty() || moduleName.empty()) {
3012         APP_LOGE("fail to GetStringById due to params empty");
3013         return Constants::EMPTY_STRING;
3014     }
3015     APP_LOGD("GetStringById bundleName: %{public}s, moduleName: %{public}s, resId:%{public}d",
3016         bundleName.c_str(), moduleName.c_str(), resId);
3017     MessageParcel data;
3018     if (!data.WriteInterfaceToken(GetDescriptor())) {
3019         APP_LOGE("fail to GetStringById due to write InterfaceToken fail");
3020         return Constants::EMPTY_STRING;
3021     }
3022     if (!data.WriteString(bundleName)) {
3023         APP_LOGE("fail to GetStringById due to write bundleName fail");
3024         return Constants::EMPTY_STRING;
3025     }
3026     if (!data.WriteString(moduleName)) {
3027         APP_LOGE("fail to GetStringById due to write moduleName fail");
3028         return Constants::EMPTY_STRING;
3029     }
3030     if (!data.WriteUint32(resId)) {
3031         APP_LOGE("fail to GetStringById due to write resId fail");
3032         return Constants::EMPTY_STRING;
3033     }
3034     if (!data.WriteInt32(userId)) {
3035         APP_LOGE("fail to GetStringById due to write userId fail");
3036         return Constants::EMPTY_STRING;
3037     }
3038     if (!data.WriteString(localeInfo)) {
3039         APP_LOGE("fail to GetStringById due to write localeInfo fail");
3040         return Constants::EMPTY_STRING;
3041     }
3042     MessageParcel reply;
3043     if (!SendTransactCmd(IBundleMgr::Message::GET_STRING_BY_ID, data, reply)) {
3044         APP_LOGE("fail to GetStringById from server");
3045         return Constants::EMPTY_STRING;
3046     }
3047     return reply.ReadString();
3048 }
3049 
GetIconById(const std::string & bundleName,const std::string & moduleName,uint32_t resId,uint32_t density,int32_t userId)3050 std::string BundleMgrProxy::GetIconById(
3051     const std::string &bundleName, const std::string &moduleName, uint32_t resId, uint32_t density, int32_t userId)
3052 {
3053     APP_LOGD("begin to GetIconById.");
3054     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3055     if (bundleName.empty() || moduleName.empty()) {
3056         APP_LOGE("fail to GetIconById due to params empty");
3057         return Constants::EMPTY_STRING;
3058     }
3059     APP_LOGD("GetIconById bundleName: %{public}s, moduleName: %{public}s, resId:%{public}d",
3060         bundleName.c_str(), moduleName.c_str(), resId);
3061     MessageParcel data;
3062     if (!data.WriteInterfaceToken(GetDescriptor())) {
3063         APP_LOGE("fail to GetIconById due to write InterfaceToken fail");
3064         return Constants::EMPTY_STRING;
3065     }
3066     if (!data.WriteString(bundleName)) {
3067         APP_LOGE("fail to GetIconById due to write bundleName fail");
3068         return Constants::EMPTY_STRING;
3069     }
3070 
3071     if (!data.WriteString(moduleName)) {
3072         APP_LOGE("fail to GetIconById due to write moduleName fail");
3073         return Constants::EMPTY_STRING;
3074     }
3075     if (!data.WriteUint32(resId)) {
3076         APP_LOGE("fail to GetIconById due to write resId fail");
3077         return Constants::EMPTY_STRING;
3078     }
3079     if (!data.WriteUint32(density)) {
3080         APP_LOGE("fail to GetIconById due to write density fail");
3081         return Constants::EMPTY_STRING;
3082     }
3083     if (!data.WriteInt32(userId)) {
3084         APP_LOGE("fail to GetIconById due to write userId fail");
3085         return Constants::EMPTY_STRING;
3086     }
3087     MessageParcel reply;
3088     if (!SendTransactCmd(IBundleMgr::Message::GET_ICON_BY_ID, data, reply)) {
3089         APP_LOGE("fail to GetIconById from server");
3090         return Constants::EMPTY_STRING;
3091     }
3092     return reply.ReadString();
3093 }
3094 
GetUdidByNetworkId(const std::string & networkId,std::string & udid)3095 int32_t BundleMgrProxy::GetUdidByNetworkId(const std::string &networkId, std::string &udid)
3096 {
3097     APP_LOGD("begin to GetUdidByNetworkId.");
3098     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3099     MessageParcel data;
3100     if (!data.WriteInterfaceToken(GetDescriptor())) {
3101         APP_LOGE("fail to GetUdidByNetworkId due to write InterfaceToken fail");
3102         return ERR_APPEXECFWK_PARCEL_ERROR;
3103     }
3104     if (!data.WriteString(networkId)) {
3105         APP_LOGE("fail to GetUdidByNetworkId due to write bundleName fail");
3106         return ERR_APPEXECFWK_PARCEL_ERROR;
3107     }
3108     MessageParcel reply;
3109     if (!SendTransactCmd(IBundleMgr::Message::GET_UDID_BY_NETWORK_ID, data, reply)) {
3110         return ERR_BUNDLE_MANAGER_IPC_TRANSACTION;
3111     }
3112     udid = reply.ReadString();
3113     return NO_ERROR;
3114 }
3115 
3116 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
GetDefaultAppProxy()3117 sptr<IDefaultApp> BundleMgrProxy::GetDefaultAppProxy()
3118 {
3119     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3120     MessageParcel data;
3121     MessageParcel reply;
3122     if (!data.WriteInterfaceToken(GetDescriptor())) {
3123         APP_LOGE("fail to get default app proxy due to write InterfaceToken failed.");
3124         return nullptr;
3125     }
3126     if (!SendTransactCmd(IBundleMgr::Message::GET_DEFAULT_APP_PROXY, data, reply)) {
3127         return nullptr;
3128     }
3129 
3130     sptr<IRemoteObject> object = reply.ReadObject<IRemoteObject>();
3131     if (object == nullptr) {
3132         APP_LOGE("reply failed.");
3133         return nullptr;
3134     }
3135     sptr<IDefaultApp> defaultAppProxy = iface_cast<IDefaultApp>(object);
3136     if (defaultAppProxy == nullptr) {
3137         APP_LOGE("defaultAppProxy is nullptr.");
3138     }
3139 
3140     return defaultAppProxy;
3141 }
3142 #endif
3143 
3144 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
GetAppControlProxy()3145 sptr<IAppControlMgr> BundleMgrProxy::GetAppControlProxy()
3146 {
3147     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3148     MessageParcel data;
3149     MessageParcel reply;
3150     if (!data.WriteInterfaceToken(GetDescriptor())) {
3151         APP_LOGE("fail to get app control proxy due to write InterfaceToken failed.");
3152         return nullptr;
3153     }
3154     if (!SendTransactCmd(IBundleMgr::Message::GET_APP_CONTROL_PROXY, data, reply)) {
3155         return nullptr;
3156     }
3157 
3158     sptr<IRemoteObject> object = reply.ReadObject<IRemoteObject>();
3159     if (object == nullptr) {
3160         APP_LOGE("reply failed.");
3161         return nullptr;
3162     }
3163     sptr<IAppControlMgr> appControlProxy = iface_cast<IAppControlMgr>(object);
3164     if (appControlProxy == nullptr) {
3165         APP_LOGE("appControlProxy is nullptr.");
3166     }
3167 
3168     return appControlProxy;
3169 }
3170 #endif
3171 
GetSandboxAbilityInfo(const Want & want,int32_t appIndex,int32_t flags,int32_t userId,AbilityInfo & info)3172 ErrCode BundleMgrProxy::GetSandboxAbilityInfo(const Want &want, int32_t appIndex, int32_t flags, int32_t userId,
3173     AbilityInfo &info)
3174 {
3175     APP_LOGD("begin to GetSandboxAbilityInfo");
3176     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3177     if (appIndex <= Constants::INITIAL_APP_INDEX || appIndex > Constants::MAX_APP_INDEX) {
3178         APP_LOGE("GetSandboxAbilityInfo params are invalid");
3179         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
3180     }
3181     MessageParcel data;
3182     if (!data.WriteInterfaceToken(GetDescriptor())) {
3183         APP_LOGE("WriteInterfaceToken failed.");
3184         return ERR_APPEXECFWK_PARCEL_ERROR;
3185     }
3186     if (!data.WriteParcelable(&want)) {
3187         APP_LOGE("WriteParcelable want failed.");
3188         return ERR_APPEXECFWK_PARCEL_ERROR;
3189     }
3190     if (!data.WriteInt32(appIndex)) {
3191         APP_LOGE("failed to GetSandboxAbilityInfo due to write appIndex fail");
3192         return ERR_APPEXECFWK_PARCEL_ERROR;
3193     }
3194     if (!data.WriteInt32(flags)) {
3195         APP_LOGE("failed to GetSandboxAbilityInfo due to write flags fail");
3196         return ERR_APPEXECFWK_PARCEL_ERROR;
3197     }
3198     if (!data.WriteInt32(userId)) {
3199         APP_LOGE("failed to GetSandboxAbilityInfo due to write userId fail");
3200         return ERR_APPEXECFWK_PARCEL_ERROR;
3201     }
3202 
3203     return GetParcelableInfoWithErrCode<AbilityInfo>(IBundleMgr::Message::GET_SANDBOX_APP_ABILITY_INFO, data, info);
3204 }
3205 
GetSandboxExtAbilityInfos(const Want & want,int32_t appIndex,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & infos)3206 ErrCode BundleMgrProxy::GetSandboxExtAbilityInfos(const Want &want, int32_t appIndex, int32_t flags, int32_t userId,
3207     std::vector<ExtensionAbilityInfo> &infos)
3208 {
3209     APP_LOGD("begin to GetSandboxExtAbilityInfos");
3210     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3211     if (appIndex <= Constants::INITIAL_APP_INDEX || appIndex > Constants::MAX_APP_INDEX) {
3212         APP_LOGE("GetSandboxExtAbilityInfos params are invalid");
3213         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
3214     }
3215     MessageParcel data;
3216     if (!data.WriteInterfaceToken(GetDescriptor())) {
3217         APP_LOGE("WriteInterfaceToken failed.");
3218         return ERR_APPEXECFWK_PARCEL_ERROR;
3219     }
3220     if (!data.WriteParcelable(&want)) {
3221         APP_LOGE("WriteParcelable want failed.");
3222         return ERR_APPEXECFWK_PARCEL_ERROR;
3223     }
3224     if (!data.WriteInt32(appIndex)) {
3225         APP_LOGE("failed to GetSandboxExtAbilityInfos due to write appIndex fail");
3226         return ERR_APPEXECFWK_PARCEL_ERROR;
3227     }
3228     if (!data.WriteInt32(flags)) {
3229         APP_LOGE("failed to GetSandboxExtAbilityInfos due to write flags fail");
3230         return ERR_APPEXECFWK_PARCEL_ERROR;
3231     }
3232     if (!data.WriteInt32(userId)) {
3233         APP_LOGE("failed to GetSandboxExtAbilityInfos due to write userId fail");
3234         return ERR_APPEXECFWK_PARCEL_ERROR;
3235     }
3236 
3237     return GetParcelableInfosWithErrCode<ExtensionAbilityInfo>(
3238         IBundleMgr::Message::GET_SANDBOX_APP_EXTENSION_INFOS, data, infos);
3239 }
3240 
GetSandboxHapModuleInfo(const AbilityInfo & abilityInfo,int32_t appIndex,int32_t userId,HapModuleInfo & info)3241 ErrCode BundleMgrProxy::GetSandboxHapModuleInfo(const AbilityInfo &abilityInfo, int32_t appIndex, int32_t userId,
3242     HapModuleInfo &info)
3243 {
3244     APP_LOGD("begin to GetSandboxHapModuleInfo");
3245     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3246     if (appIndex <= Constants::INITIAL_APP_INDEX || appIndex > Constants::MAX_APP_INDEX) {
3247         APP_LOGE("GetSandboxHapModuleInfo params are invalid");
3248         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
3249     }
3250     MessageParcel data;
3251     if (!data.WriteInterfaceToken(GetDescriptor())) {
3252         APP_LOGE("WriteInterfaceToken failed.");
3253         return ERR_APPEXECFWK_PARCEL_ERROR;
3254     }
3255     if (!data.WriteParcelable(&abilityInfo)) {
3256         APP_LOGE("WriteParcelable want failed.");
3257         return ERR_APPEXECFWK_PARCEL_ERROR;
3258     }
3259     if (!data.WriteInt32(appIndex)) {
3260         APP_LOGE("failed to GetSandboxHapModuleInfo due to write flags fail");
3261         return ERR_APPEXECFWK_PARCEL_ERROR;
3262     }
3263     if (!data.WriteInt32(userId)) {
3264         APP_LOGE("failed to GetSandboxHapModuleInfo due to write userId fail");
3265         return ERR_APPEXECFWK_PARCEL_ERROR;
3266     }
3267 
3268     return GetParcelableInfoWithErrCode<HapModuleInfo>(IBundleMgr::Message::GET_SANDBOX_MODULE_INFO, data, info);
3269 }
3270 
GetMediaData(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,std::unique_ptr<uint8_t[]> & mediaDataPtr,size_t & len,int32_t userId)3271 ErrCode BundleMgrProxy::GetMediaData(const std::string &bundleName, const std::string &moduleName,
3272     const std::string &abilityName, std::unique_ptr<uint8_t[]> &mediaDataPtr, size_t &len, int32_t userId)
3273 {
3274     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3275     APP_LOGD("begin to get media data of %{public}s, %{public}s", bundleName.c_str(), abilityName.c_str());
3276     if (bundleName.empty() || abilityName.empty()) {
3277         APP_LOGE("fail to GetMediaData due to params empty");
3278         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
3279     }
3280 
3281     MessageParcel data;
3282     if (!data.WriteInterfaceToken(GetDescriptor())) {
3283         APP_LOGE("fail to GetMediaData due to write InterfaceToken fail");
3284         return ERR_APPEXECFWK_PARCEL_ERROR;
3285     }
3286     if (!data.WriteString(bundleName)) {
3287         APP_LOGE("fail to GetMediaData due to write bundleName fail");
3288         return ERR_APPEXECFWK_PARCEL_ERROR;
3289     }
3290     if (!data.WriteString(abilityName)) {
3291         APP_LOGE("fail to GetMediaData due to write abilityName fail");
3292         return ERR_APPEXECFWK_PARCEL_ERROR;
3293     }
3294     if (!data.WriteString(moduleName)) {
3295         APP_LOGE("fail to GetMediaData due to write abilityName fail");
3296         return ERR_APPEXECFWK_PARCEL_ERROR;
3297     }
3298     if (!data.WriteInt32(userId)) {
3299         APP_LOGE("fail to GetMediaData due to write userId fail");
3300         return ERR_APPEXECFWK_PARCEL_ERROR;
3301     }
3302     MessageParcel reply;
3303     if (!SendTransactCmd(IBundleMgr::Message::GET_MEDIA_DATA, data, reply)) {
3304         APP_LOGE("SendTransactCmd result false");
3305         return ERR_APPEXECFWK_PARCEL_ERROR;
3306     }
3307     ErrCode ret = reply.ReadInt32();
3308     if (ret != ERR_OK) {
3309         APP_LOGE("host return error : %{public}d", ret);
3310         return ret;
3311     }
3312     return GetMediaDataFromAshMem(reply, mediaDataPtr, len);
3313 }
3314 
GetMediaDataFromAshMem(MessageParcel & reply,std::unique_ptr<uint8_t[]> & mediaDataPtr,size_t & len)3315 ErrCode BundleMgrProxy::GetMediaDataFromAshMem(
3316     MessageParcel &reply, std::unique_ptr<uint8_t[]> &mediaDataPtr, size_t &len)
3317 {
3318     sptr<Ashmem> ashMem = reply.ReadAshmem();
3319     if (ashMem == nullptr) {
3320         APP_LOGE("Ashmem is nullptr");
3321         return ERR_APPEXECFWK_PARCEL_ERROR;
3322     }
3323     if (!ashMem->MapReadOnlyAshmem()) {
3324         APP_LOGE("MapReadOnlyAshmem failed");
3325         ClearAshmem(ashMem);
3326         return ERR_APPEXECFWK_PARCEL_ERROR;
3327     }
3328     int32_t ashMemSize = ashMem->GetAshmemSize();
3329     int32_t offset = 0;
3330     const uint8_t* ashDataPtr = reinterpret_cast<const uint8_t*>(ashMem->ReadFromAshmem(ashMemSize, offset));
3331     if (ashDataPtr == nullptr) {
3332         APP_LOGE("ashDataPtr is nullptr");
3333         ClearAshmem(ashMem);
3334         return ERR_APPEXECFWK_PARCEL_ERROR;
3335     }
3336     len = static_cast<size_t>(ashMemSize);
3337     mediaDataPtr = std::make_unique<uint8_t[]>(len);
3338     if (memcpy_s(mediaDataPtr.get(), len, ashDataPtr, len) != 0) {
3339         mediaDataPtr.reset();
3340         len = 0;
3341         ClearAshmem(ashMem);
3342         return ERR_APPEXECFWK_PARCEL_ERROR;
3343     }
3344     ClearAshmem(ashMem);
3345     return ERR_OK;
3346 }
3347 
GetQuickFixManagerProxy()3348 sptr<IQuickFixManager> BundleMgrProxy::GetQuickFixManagerProxy()
3349 {
3350     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3351     MessageParcel data;
3352     if (!data.WriteInterfaceToken(GetDescriptor())) {
3353         APP_LOGE("fail to get quick fix manager proxy due to write InterfaceToken failed.");
3354         return nullptr;
3355     }
3356     MessageParcel reply;
3357     if (!SendTransactCmd(IBundleMgr::Message::GET_QUICK_FIX_MANAGER_PROXY, data, reply)) {
3358         return nullptr;
3359     }
3360 
3361     sptr<IRemoteObject> object = reply.ReadObject<IRemoteObject>();
3362     if (object == nullptr) {
3363         APP_LOGE("reply failed.");
3364         return nullptr;
3365     }
3366     sptr<IQuickFixManager> quickFixManagerProxy = iface_cast<IQuickFixManager>(object);
3367     if (quickFixManagerProxy == nullptr) {
3368         APP_LOGE("quickFixManagerProxy is nullptr.");
3369     }
3370 
3371     return quickFixManagerProxy;
3372 }
3373 
SetDebugMode(bool isDebug)3374 ErrCode BundleMgrProxy::SetDebugMode(bool isDebug)
3375 {
3376     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
3377     MessageParcel data;
3378     if (!data.WriteInterfaceToken(GetDescriptor())) {
3379         APP_LOGE("fail to get quick fix manager proxy due to write InterfaceToken failed.");
3380         return ERR_BUNDLEMANAGER_SET_DEBUG_MODE_PARCEL_ERROR;
3381     }
3382     if (!data.WriteBool(isDebug)) {
3383         APP_LOGE("fail to SetDebugMode due to write bundleName fail");
3384         return ERR_BUNDLEMANAGER_SET_DEBUG_MODE_PARCEL_ERROR;
3385     }
3386     MessageParcel reply;
3387     if (!SendTransactCmd(IBundleMgr::Message::SET_DEBUG_MODE, data, reply)) {
3388         return ERR_BUNDLEMANAGER_SET_DEBUG_MODE_SEND_REQUEST_ERROR;
3389     }
3390 
3391     return reply.ReadInt32();
3392 }
3393 
VerifySystemApi(int32_t beginApiVersion)3394 bool BundleMgrProxy::VerifySystemApi(int32_t beginApiVersion)
3395 {
3396     APP_LOGD("begin to verify system app");
3397     MessageParcel data;
3398     if (!data.WriteInterfaceToken(GetDescriptor())) {
3399         APP_LOGE("fail to VerifySystemApi due to write InterfaceToken fail");
3400         return false;
3401     }
3402 
3403     if (!data.WriteInt32(beginApiVersion)) {
3404         APP_LOGE("fail to VerifySystemApi due to write apiVersion fail");
3405         return false;
3406     }
3407 
3408     MessageParcel reply;
3409     if (!SendTransactCmd(IBundleMgr::Message::VERIFY_SYSTEM_API, data, reply)) {
3410         APP_LOGE("fail to sendRequest");
3411         return false;
3412     }
3413     return reply.ReadBool();
3414 }
3415 
ProcessPreload(const Want & want)3416 bool BundleMgrProxy::ProcessPreload(const Want &want)
3417 {
3418     APP_LOGD("BundleMgrProxy::ProcessPreload is called.");
3419     MessageParcel data;
3420     if (!data.WriteInterfaceToken(GetDescriptor())) {
3421         APP_LOGE("fail to ProcessPreload due to write InterfaceToken fail");
3422         return false;
3423     }
3424     if (!data.WriteParcelable(&want)) {
3425         APP_LOGE("fail to ProcessPreload due to write want fail");
3426         return false;
3427     }
3428     MessageParcel reply;
3429     MessageOption option(MessageOption::TF_ASYNC);
3430     auto res = Remote()->SendRequest(IBundleMgr::Message::PROCESS_PRELOAD, data, reply, option);
3431     if (res != ERR_OK) {
3432         APP_LOGE("SendRequest fail, error: %{public}d", res);
3433         return false;
3434     }
3435     return reply.ReadBool();
3436 }
3437 
3438 template<typename T>
GetParcelableInfo(IBundleMgr::Message code,MessageParcel & data,T & parcelableInfo)3439 bool BundleMgrProxy::GetParcelableInfo(IBundleMgr::Message code, MessageParcel &data, T &parcelableInfo)
3440 {
3441     MessageParcel reply;
3442     if (!SendTransactCmd(code, data, reply)) {
3443         return false;
3444     }
3445 
3446     if (!reply.ReadBool()) {
3447         APP_LOGE("reply result false");
3448         return false;
3449     }
3450 
3451     std::unique_ptr<T> info(reply.ReadParcelable<T>());
3452     if (info == nullptr) {
3453         APP_LOGE("readParcelableInfo failed");
3454         return false;
3455     }
3456     parcelableInfo = *info;
3457     APP_LOGD("get parcelable info success");
3458     return true;
3459 }
3460 
3461 template <typename T>
GetParcelableInfoWithErrCode(IBundleMgr::Message code,MessageParcel & data,T & parcelableInfo)3462 ErrCode BundleMgrProxy::GetParcelableInfoWithErrCode(IBundleMgr::Message code, MessageParcel &data, T &parcelableInfo)
3463 {
3464     MessageParcel reply;
3465     if (!SendTransactCmd(code, data, reply)) {
3466         APP_LOGE("SendTransactCmd failed");
3467         return ERR_APPEXECFWK_PARCEL_ERROR;
3468     }
3469 
3470     ErrCode res = reply.ReadInt32();
3471     if (res == ERR_OK) {
3472         std::unique_ptr<T> info(reply.ReadParcelable<T>());
3473         if (info == nullptr) {
3474             APP_LOGE("readParcelableInfo failed");
3475             return ERR_APPEXECFWK_PARCEL_ERROR;
3476         }
3477         parcelableInfo = *info;
3478     }
3479 
3480     APP_LOGD("GetParcelableInfoWithErrCode ErrCode : %{public}d", res);
3481     return res;
3482 }
3483 
3484 template<typename T>
GetParcelableInfos(IBundleMgr::Message code,MessageParcel & data,std::vector<T> & parcelableInfos)3485 bool BundleMgrProxy::GetParcelableInfos(IBundleMgr::Message code, MessageParcel &data, std::vector<T> &parcelableInfos)
3486 {
3487     MessageParcel reply;
3488     if (!SendTransactCmd(code, data, reply)) {
3489         return false;
3490     }
3491 
3492     if (!reply.ReadBool()) {
3493         APP_LOGE("readParcelableInfo failed");
3494         return false;
3495     }
3496 
3497     int32_t infoSize = reply.ReadInt32();
3498     for (int32_t i = 0; i < infoSize; i++) {
3499         std::unique_ptr<T> info(reply.ReadParcelable<T>());
3500         if (info == nullptr) {
3501             APP_LOGE("Read Parcelable infos failed");
3502             return false;
3503         }
3504         parcelableInfos.emplace_back(*info);
3505     }
3506     APP_LOGD("get parcelable infos success");
3507     return true;
3508 }
3509 
3510 template<typename T>
GetParcelableInfosWithErrCode(IBundleMgr::Message code,MessageParcel & data,std::vector<T> & parcelableInfos)3511 ErrCode BundleMgrProxy::GetParcelableInfosWithErrCode(IBundleMgr::Message code, MessageParcel &data,
3512     std::vector<T> &parcelableInfos)
3513 {
3514     MessageParcel reply;
3515     if (!SendTransactCmd(code, data, reply)) {
3516         APP_LOGE("SendTransactCmd failed");
3517         return ERR_APPEXECFWK_PARCEL_ERROR;
3518     }
3519 
3520     ErrCode res = reply.ReadInt32();
3521     if (res == ERR_OK) {
3522         int32_t infoSize = reply.ReadInt32();
3523         for (int32_t i = 0; i < infoSize; i++) {
3524             std::unique_ptr<T> info(reply.ReadParcelable<T>());
3525             if (info == nullptr) {
3526                 APP_LOGE("Read Parcelable infos failed");
3527                 return ERR_APPEXECFWK_PARCEL_ERROR;
3528             }
3529             parcelableInfos.emplace_back(*info);
3530         }
3531         APP_LOGD("get parcelable infos success");
3532     }
3533     APP_LOGD("GetParcelableInfosWithErrCode ErrCode : %{public}d", res);
3534     return res;
3535 }
3536 
3537 template <typename T>
GetParcelableInfosFromAshmem(MessageParcel & reply,std::vector<T> & parcelableInfos)3538 bool BundleMgrProxy::GetParcelableInfosFromAshmem(MessageParcel &reply, std::vector<T> &parcelableInfos)
3539 {
3540     APP_LOGD("Get parcelable vector from ashmem");
3541     int32_t infoSize = reply.ReadInt32();
3542     sptr<Ashmem> ashmem = reply.ReadAshmem();
3543     if (ashmem == nullptr) {
3544         APP_LOGE("Ashmem is nullptr");
3545         return false;
3546     }
3547 
3548     bool ret = ashmem->MapReadOnlyAshmem();
3549     if (!ret) {
3550         APP_LOGE("Map read only ashmem fail");
3551         ClearAshmem(ashmem);
3552         return false;
3553     }
3554 
3555     int32_t offset = 0;
3556     const char* dataStr = static_cast<const char*>(
3557         ashmem->ReadFromAshmem(ashmem->GetAshmemSize(), offset));
3558     if (dataStr == nullptr) {
3559         APP_LOGE("Data is nullptr when read from ashmem");
3560         ClearAshmem(ashmem);
3561         return false;
3562     }
3563 
3564     while (infoSize > 0) {
3565         std::string lenStr;
3566         if (!ParseStr(dataStr, ASHMEM_LEN, offset, lenStr)) {
3567             APP_LOGE("Parse lenStr fail");
3568             ClearAshmem(ashmem);
3569             return false;
3570         }
3571 
3572         int strLen = atoi(lenStr.c_str());
3573         offset += ASHMEM_LEN;
3574         std::string infoStr;
3575         if (!ParseStr(dataStr, strLen, offset, infoStr)) {
3576             APP_LOGE("Parse infoStr fail");
3577             ClearAshmem(ashmem);
3578             return false;
3579         }
3580 
3581         T info;
3582         if (!ParseInfoFromJsonStr(infoStr.c_str(), info)) {
3583             APP_LOGE("Parse info from json fail");
3584             ClearAshmem(ashmem);
3585             return false;
3586         }
3587 
3588         parcelableInfos.emplace_back(info);
3589         infoSize--;
3590         offset += strLen;
3591     }
3592 
3593     ClearAshmem(ashmem);
3594     APP_LOGD("Get parcelable vector from ashmem success");
3595     return true;
3596 }
3597 
SendTransactCmd(IBundleMgr::Message code,MessageParcel & data,MessageParcel & reply)3598 bool BundleMgrProxy::SendTransactCmd(IBundleMgr::Message code, MessageParcel &data, MessageParcel &reply)
3599 {
3600     MessageOption option(MessageOption::TF_SYNC);
3601 
3602     sptr<IRemoteObject> remote = Remote();
3603     if (remote == nullptr) {
3604         APP_LOGE("fail to send transact cmd %{public}d due to remote object", code);
3605         return false;
3606     }
3607     int32_t result = remote->SendRequest(code, data, reply, option);
3608     if (result != NO_ERROR) {
3609         APP_LOGE("receive error transact code %{public}d in transact cmd %{public}d", result, code);
3610         return false;
3611     }
3612     return true;
3613 }
3614 }  // namespace AppExecFwk
3615 }  // namespace OHOS
3616