• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_helper.h"
17 #include "oaid_common.h"
18 #include "bundle_constants.h"
19 #include "bundle_mgr_client.h"
20 #include "bundle_mgr_interface.h"
21 #include "bundle_mgr_proxy.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 
25 namespace OHOS {
26 namespace Cloud {
BundleMgrHelper()27 BundleMgrHelper::BundleMgrHelper() : bundleMgrProxy_(nullptr), oaidDeathRecipient_(nullptr)
28 {}
29 
~BundleMgrHelper()30 BundleMgrHelper::~BundleMgrHelper()
31 {}
32 
GetBundleMgrProxy()33 bool BundleMgrHelper::GetBundleMgrProxy()
34 {
35     OAID_HILOGD(OAID_MODULE_SERVICE, "GetBundleMgrProxy");
36 
37     if (!bundleMgrProxy_) {
38         sptr<ISystemAbilityManager> systemAbilityManager =
39             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40         if (!systemAbilityManager) {
41             OAID_HILOGE(OAID_MODULE_SERVICE, "Failed to get system ability mgr.");
42             return false;
43         }
44 
45         sptr<IRemoteObject> remoteObject =
46             systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
47         if (!remoteObject) {
48             OAID_HILOGE(OAID_MODULE_SERVICE, "Failed to get bundle manager service.");
49             return false;
50         }
51 
52         bundleMgrProxy_ = iface_cast<IBundleMgr>(remoteObject);
53         if ((!bundleMgrProxy_) || (!bundleMgrProxy_->AsObject())) {
54             OAID_HILOGE(OAID_MODULE_SERVICE, "Failed to get system bundle manager services ability");
55             return false;
56         }
57 
58         oaidDeathRecipient_ = new (std::nothrow) OAIDDeathRecipient();
59         if (!oaidDeathRecipient_) {
60             OAID_HILOGE(OAID_MODULE_SERVICE, "Failed to create death Recipient ptr OAIDDeathRecipient");
61             return false;
62         }
63         if (!bundleMgrProxy_->AsObject()->AddDeathRecipient(oaidDeathRecipient_)) {
64             OAID_HILOGW(OAID_MODULE_SERVICE, "Failed to add death recipient");
65         }
66     }
67 
68     return true;
69 }
70 
GetBundleInfosV9ByReqPermission(std::vector<AppExecFwk::BundleInfo> & bundleInfos,int32_t userId)71 bool BundleMgrHelper::GetBundleInfosV9ByReqPermission(std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId)
72 {
73     OAID_HILOGI(OAID_MODULE_SERVICE, "GetBundleInfosV9ByReqPermission");
74     std::lock_guard<std::mutex> lock(mutex_);
75 
76     if (!GetBundleMgrProxy()) {
77         OAID_HILOGE(OAID_MODULE_SERVICE, "get BundleMgr Proxy fail");
78         return false;
79     }
80 
81     int32_t flag = static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION);
82     ErrCode ret = bundleMgrProxy_->GetBundleInfosV9(flag, bundleInfos, userId);
83     if (ret != ERR_OK) {
84         OAID_HILOGE(OAID_MODULE_SERVICE, "Failed to getBundleInfos");
85         return false;
86     }
87 
88     return true;
89 }
90 
GetApplicationInfoV9WithPermission(const std::string bundleName,int32_t userId,AppExecFwk::ApplicationInfo & applicationInfo)91 bool BundleMgrHelper::GetApplicationInfoV9WithPermission(
92     const std::string bundleName, int32_t userId, AppExecFwk::ApplicationInfo &applicationInfo)
93 {
94     OAID_HILOGI(OAID_MODULE_SERVICE, "GetApplicationInfoV9ByReqPermission");
95     std::lock_guard<std::mutex> lock(mutex_);
96 
97     if (!GetBundleMgrProxy()) {
98         OAID_HILOGE(OAID_MODULE_SERVICE, "get BundleMgr Proxy fail");
99         return false;
100     }
101 
102     int32_t applicationFlag =
103         static_cast<int32_t>(AppExecFwk::GetApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION);
104     ErrCode ret = bundleMgrProxy_->GetApplicationInfoV9(bundleName, applicationFlag, userId, applicationInfo);
105     if (ret != ERR_OK) {
106         OAID_HILOGE(OAID_MODULE_SERVICE, "Failed to GetApplicationInfoV9");
107         return false;
108     }
109 
110     return true;
111 }
112 
GetBundleNameByUid(const int uid,std::string & name)113 void BundleMgrHelper::GetBundleNameByUid(const int uid, std::string &name)
114 {
115     OAID_HILOGI(OAID_MODULE_SERVICE, "GetBundleNameByUid");
116     std::lock_guard<std::mutex> lock(mutex_);
117 
118     if (!GetBundleMgrProxy()) {
119         OAID_HILOGE(OAID_MODULE_SERVICE, "get BundleMgr Proxy fail");
120         return;
121     }
122 
123     ErrCode ret = bundleMgrProxy_->GetNameForUid(uid, name);
124     if (ret != ERR_OK) {
125         OAID_HILOGE(OAID_MODULE_SERVICE, "Failed to GetNameForUid");
126         return;
127     }
128     OAID_HILOGI(OAID_MODULE_SERVICE, "GetBundleNameByUid success");
129     return;
130 }
131 
ClearBundleMgrHelper()132 void BundleMgrHelper::ClearBundleMgrHelper()
133 {
134     OAID_HILOGI(OAID_MODULE_SERVICE, "ClearBundleMgrHelper");
135     std::lock_guard<std::mutex> lock(mutex_);
136 
137     if ((bundleMgrProxy_ != nullptr) && (bundleMgrProxy_->AsObject() != nullptr)) {
138         bundleMgrProxy_->AsObject()->RemoveDeathRecipient(oaidDeathRecipient_);
139     }
140     bundleMgrProxy_ = nullptr;
141 }
142 }  // namespace Cloud
143 }  // namespace OHOS