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