• 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 "event_log_wrapper.h"
21 #include "iservice_registry.h"
22 #include "nlohmann/json.hpp"
23 #include "os_account_manager_helper.h"
24 #include "system_ability_definition.h"
25 
26 namespace OHOS {
27 namespace EventFwk {
28 const std::string META_NAME_STATIC_SUBSCRIBER = "ohos.extension.staticSubscriber";
29 
30 using namespace OHOS::AppExecFwk::Constants;
31 
BundleManagerHelper()32 BundleManagerHelper::BundleManagerHelper() : sptrBundleMgr_(nullptr), bmsDeath_(nullptr)
33 {}
34 
~BundleManagerHelper()35 BundleManagerHelper::~BundleManagerHelper()
36 {}
37 
GetBundleName(uid_t uid)38 std::string BundleManagerHelper::GetBundleName(uid_t uid)
39 {
40     EVENT_LOGI("enter");
41 
42     std::lock_guard<std::mutex> lock(mutex_);
43     std::string bundleName = "";
44 
45     if (!GetBundleMgrProxy()) {
46         EVENT_LOGE("failed to get bms proxy");
47         return bundleName;
48     }
49 
50     sptrBundleMgr_->GetBundleNameForUid(uid, bundleName);
51 
52     return bundleName;
53 }
54 
QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos,const int32_t & userId)55 bool BundleManagerHelper::QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos,
56     const int32_t &userId)
57 {
58     EVENT_LOGI("enter");
59 
60     std::lock_guard<std::mutex> lock(mutex_);
61 
62     if (!GetBundleMgrProxy()) {
63         EVENT_LOGE("failed to get bms proxy");
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_LOGI("enter");
74 
75     std::lock_guard<std::mutex> lock(mutex_);
76 
77     if (!GetBundleMgrProxy()) {
78         EVENT_LOGE("failed to get bms proxy");
79         return false;
80     }
81     std::vector<int> osAccountIds;
82     if (DelayedSingleton<OsAccountManagerHelper>::GetInstance()->QueryActiveOsAccountIds(osAccountIds) != ERR_OK) {
83         EVENT_LOGE("failed to QueryActiveOsAccountIds!");
84         return false;
85     }
86     if (osAccountIds.size() == 0) {
87         EVENT_LOGE("no os account acquired!");
88         return false;
89     }
90     for (auto userId : osAccountIds) {
91         EVENT_LOGI("active userId = %{public}d", userId);
92         sptrBundleMgr_->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::STATICSUBSCRIBER,
93             userId, extensionInfos);
94     }
95     return true;
96 }
97 
GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo & extension,std::vector<std::string> & profileInfos)98 bool BundleManagerHelper::GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo &extension,
99                                            std::vector<std::string> &profileInfos)
100 {
101     EVENT_LOGI("enter");
102 
103     std::lock_guard<std::mutex> lock(mutex_);
104 
105     if (!GetBundleMgrProxy()) {
106         EVENT_LOGE("failed to get bms proxy");
107         return false;
108     }
109 
110     AppExecFwk::BundleMgrClient client;
111     return client.GetResConfigFile(extension, META_NAME_STATIC_SUBSCRIBER, profileInfos);
112 }
113 
CheckIsSystemAppByUid(uid_t uid)114 bool BundleManagerHelper::CheckIsSystemAppByUid(uid_t uid)
115 {
116     EVENT_LOGI("enter");
117 
118     std::lock_guard<std::mutex> lock(mutex_);
119 
120     bool isSystemApp = false;
121 
122     if (!GetBundleMgrProxy()) {
123         EVENT_LOGE("failed to get bms proxy");
124         return isSystemApp;
125     }
126 
127     isSystemApp = sptrBundleMgr_->CheckIsSystemAppByUid(uid);
128 
129     return isSystemApp;
130 }
131 
CheckIsSystemAppByBundleName(const std::string & bundleName,const int32_t & userId)132 bool BundleManagerHelper::CheckIsSystemAppByBundleName(const std::string &bundleName, const int32_t &userId)
133 {
134     EVENT_LOGI("enter");
135 
136     std::lock_guard<std::mutex> lock(mutex_);
137 
138     bool isSystemApp = false;
139 
140     if (!GetBundleMgrProxy()) {
141         EVENT_LOGE("failed to get bms proxy");
142         return isSystemApp;
143     }
144 
145     int32_t uid = sptrBundleMgr_->GetUidByBundleName(bundleName, userId);
146     isSystemApp = sptrBundleMgr_->CheckIsSystemAppByUid(uid);
147 
148     return isSystemApp;
149 }
150 
GetBundleMgrProxy()151 bool BundleManagerHelper::GetBundleMgrProxy()
152 {
153     EVENT_LOGD("enter");
154 
155     if (!sptrBundleMgr_) {
156         sptr<ISystemAbilityManager> systemAbilityManager =
157             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
158         if (!systemAbilityManager) {
159             EVENT_LOGE("Failed to get system ability mgr.");
160             return false;
161         }
162 
163         sptr<IRemoteObject> remoteObject =
164             systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
165         if (!remoteObject) {
166             EVENT_LOGE("Failed to get bundle manager service.");
167             return false;
168         }
169 
170         sptrBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
171         if ((!sptrBundleMgr_) || (!sptrBundleMgr_->AsObject())) {
172             EVENT_LOGE("Failed to get system bundle manager services ability");
173             return false;
174         }
175 
176         bmsDeath_ = new (std::nothrow) BMSDeathRecipient();
177         if (!bmsDeath_) {
178             EVENT_LOGE("Failed to create death Recipient ptr BMSDeathRecipient");
179             return false;
180         }
181         if (!sptrBundleMgr_->AsObject()->AddDeathRecipient(bmsDeath_)) {
182             EVENT_LOGW("Failed to add death recipient");
183         }
184     }
185 
186     return true;
187 }
188 
ClearBundleManagerHelper()189 void BundleManagerHelper::ClearBundleManagerHelper()
190 {
191     EVENT_LOGI("enter");
192 
193     std::lock_guard<std::mutex> lock(mutex_);
194 
195     if ((sptrBundleMgr_ != nullptr) && (sptrBundleMgr_->AsObject() != nullptr)) {
196         sptrBundleMgr_->AsObject()->RemoveDeathRecipient(bmsDeath_);
197     }
198     sptrBundleMgr_ = nullptr;
199 }
200 }  // namespace EventFwk
201 }  // namespace OHOS