• 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_manager_helper.h"
17 
18 #include "bundle_constants.h"
19 #include "bundle_mgr_client.h"
20 #include "common_event_constant.h"
21 #include "event_log_wrapper.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "nlohmann/json.hpp"
25 #include "os_account_manager_helper.h"
26 #include "system_ability_definition.h"
27 
28 namespace OHOS {
29 namespace EventFwk {
30 const std::string META_NAME_STATIC_SUBSCRIBER = "ohos.extension.staticSubscriber";
31 
32 using namespace OHOS::AppExecFwk::Constants;
33 
BundleManagerHelper()34 BundleManagerHelper::BundleManagerHelper() : sptrBundleMgr_(nullptr), bmsDeath_(nullptr)
35 {}
36 
~BundleManagerHelper()37 BundleManagerHelper::~BundleManagerHelper()
38 {}
39 
GetBundleName(const uid_t uid)40 std::string BundleManagerHelper::GetBundleName(const uid_t uid)
41 {
42     EVENT_LOGD("enter");
43 
44     std::lock_guard<ffrt::mutex> lock(mutex_);
45     std::string bundleName = "";
46 
47     if (!GetBundleMgrProxyAsync()) {
48         return bundleName;
49     }
50     std::string identity = IPCSkeleton::ResetCallingIdentity();
51     sptrBundleMgr_->GetNameForUid(uid, bundleName);
52     IPCSkeleton::SetCallingIdentity(identity);
53     return bundleName;
54 }
55 
QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos,const int32_t & userId)56 bool BundleManagerHelper::QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos,
57     const int32_t &userId)
58 {
59     EVENT_LOGD("enter");
60 
61     std::lock_guard<ffrt::mutex> lock(mutex_);
62 
63     if (!GetBundleMgrProxy()) {
64         return false;
65     }
66 
67     return sptrBundleMgr_->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::STATICSUBSCRIBER,
68         userId, extensionInfos);
69 }
70 
QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)71 bool BundleManagerHelper::QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
72 {
73     EVENT_LOGD("enter");
74     int userId = SUBSCRIBE_USER_SYSTEM_BEGIN;
75     DelayedSingleton<OsAccountManagerHelper>::GetInstance()->GetCurrentActiveUserId(userId);
76     EVENT_LOGD("active userId = %{public}d", userId);
77 
78     std::lock_guard<ffrt::mutex> lock(mutex_);
79     if (!GetBundleMgrProxy()) {
80         return false;
81     }
82     sptrBundleMgr_->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::STATICSUBSCRIBER,
83         userId, extensionInfos);
84     return true;
85 }
86 
GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo & extension,std::vector<std::string> & profileInfos)87 bool BundleManagerHelper::GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo &extension,
88                                            std::vector<std::string> &profileInfos)
89 {
90     EVENT_LOGD("enter");
91 
92     std::lock_guard<ffrt::mutex> lock(mutex_);
93 
94     if (!GetBundleMgrProxy()) {
95         return false;
96     }
97 
98     AppExecFwk::BundleMgrClient client;
99     return client.GetResConfigFile(extension, META_NAME_STATIC_SUBSCRIBER, profileInfos, false);
100 }
101 
CheckIsSystemAppByUid(const uid_t uid)102 bool BundleManagerHelper::CheckIsSystemAppByUid(const uid_t uid)
103 {
104     EVENT_LOGD("enter");
105 
106     std::lock_guard<ffrt::mutex> lock(mutex_);
107 
108     bool isSystemApp = false;
109 
110     if (!GetBundleMgrProxy()) {
111         return isSystemApp;
112     }
113 
114     isSystemApp = sptrBundleMgr_->CheckIsSystemAppByUid(uid);
115 
116     return isSystemApp;
117 }
118 
CheckIsSystemAppByBundleName(const std::string & bundleName,const int32_t & userId)119 bool BundleManagerHelper::CheckIsSystemAppByBundleName(const std::string &bundleName, const int32_t &userId)
120 {
121     EVENT_LOGD("enter");
122 
123     std::lock_guard<ffrt::mutex> lock(mutex_);
124 
125     bool isSystemApp = false;
126 
127     if (!GetBundleMgrProxy()) {
128         return isSystemApp;
129     }
130     std::string identity = IPCSkeleton::ResetCallingIdentity();
131     int32_t uid = sptrBundleMgr_->GetUidByBundleName(bundleName, userId);
132     IPCSkeleton::SetCallingIdentity(identity);
133     if (uid < 0) {
134         EVENT_LOGW("get invalid uid from bundle %{public}s of userId %{public}d", bundleName.c_str(), userId);
135     }
136     isSystemApp = sptrBundleMgr_->CheckIsSystemAppByUid(uid);
137 
138     return isSystemApp;
139 }
140 
GetBundleMgrProxyAsync()141 bool BundleManagerHelper::GetBundleMgrProxyAsync()
142 {
143     return GetBundleMgrProxyInner(true);
144 }
145 
GetBundleMgrProxy()146 bool BundleManagerHelper::GetBundleMgrProxy()
147 {
148     return GetBundleMgrProxyInner(false);
149 }
150 
GetBundleMgrProxyInner(bool isAsync)151 bool BundleManagerHelper::GetBundleMgrProxyInner(bool isAsync)
152 {
153     EVENT_LOGD("enter");
154     sptr<IRemoteObject> remoteObject;
155 
156     if (!sptrBundleMgr_) {
157         sptr<ISystemAbilityManager> systemAbilityManager =
158             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
159         if (!systemAbilityManager) {
160             EVENT_LOGE("Failed to get system ability mgr.");
161             return false;
162         }
163 
164         if (isAsync) {
165             remoteObject = systemAbilityManager->CheckSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
166         } else {
167             remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
168         }
169 
170         if (!remoteObject) {
171             EVENT_LOGE("Failed to get bundle manager service.");
172             return false;
173         }
174 
175         sptrBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
176         if ((!sptrBundleMgr_) || (!sptrBundleMgr_->AsObject())) {
177             EVENT_LOGE("Failed to get system bundle manager services ability");
178             return false;
179         }
180 
181         bmsDeath_ = new (std::nothrow) BMSDeathRecipient();
182         if (!bmsDeath_) {
183             EVENT_LOGE("Failed to create death Recipient ptr BMSDeathRecipient");
184             return false;
185         }
186         if (!sptrBundleMgr_->AsObject()->AddDeathRecipient(bmsDeath_)) {
187             EVENT_LOGW("Failed to add death recipient");
188         }
189     }
190 
191     return true;
192 }
193 
ClearBundleManagerHelper()194 void BundleManagerHelper::ClearBundleManagerHelper()
195 {
196     EVENT_LOGD("enter");
197 
198     std::lock_guard<ffrt::mutex> lock(mutex_);
199 
200     if ((sptrBundleMgr_ != nullptr) && (sptrBundleMgr_->AsObject() != nullptr)) {
201         sptrBundleMgr_->AsObject()->RemoveDeathRecipient(bmsDeath_);
202     }
203     sptrBundleMgr_ = nullptr;
204 }
205 
GetApplicationInfos(const AppExecFwk::ApplicationFlag & flag,std::vector<AppExecFwk::ApplicationInfo> & appInfos)206 bool BundleManagerHelper::GetApplicationInfos(const AppExecFwk::ApplicationFlag &flag,
207     std::vector<AppExecFwk::ApplicationInfo> &appInfos)
208 {
209     EVENT_LOGD("enter");
210     int userId = SUBSCRIBE_USER_SYSTEM_BEGIN;
211     DelayedSingleton<OsAccountManagerHelper>::GetInstance()->GetCurrentActiveUserId(userId);
212     EVENT_LOGD("active userId = %{public}d", userId);
213 
214     std::lock_guard<ffrt::mutex> lock(mutex_);
215     if (!GetBundleMgrProxy()) {
216         return false;
217     }
218     return sptrBundleMgr_->GetApplicationInfos(flag, userId, appInfos);
219 }
220 
GetDefaultUidByBundleName(const std::string & bundle,const int32_t userId)221 int32_t BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId)
222 {
223     int32_t uid = -1;
224 
225     std::lock_guard<ffrt::mutex> lock(mutex_);
226     if (!GetBundleMgrProxy()) {
227         return uid;
228     }
229     return sptrBundleMgr_->GetUidByBundleName(bundle, userId);
230 }
231 }  // namespace EventFwk
232 }  // namespace OHOS