1 /*
2 * Copyright (c) 2021-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_manager_helper.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "os_account_manager.h"
21 #include "system_ability_definition.h"
22
23 #include "ans_const_define.h"
24 #include "ans_log_wrapper.h"
25
26 namespace OHOS {
27 namespace Notification {
BundleManagerHelper()28 BundleManagerHelper::BundleManagerHelper()
29 {
30 deathRecipient_ = new (std::nothrow)
31 RemoteDeathRecipient(std::bind(&BundleManagerHelper::OnRemoteDied, this, std::placeholders::_1));
32 if (deathRecipient_ == nullptr) {
33 ANS_LOGE("Failed to create RemoteDeathRecipient instance");
34 }
35 }
36
~BundleManagerHelper()37 BundleManagerHelper::~BundleManagerHelper()
38 {
39 std::lock_guard<std::mutex> lock(connectionMutex_);
40 Disconnect();
41 }
42
OnRemoteDied(const wptr<IRemoteObject> & object)43 void BundleManagerHelper::OnRemoteDied(const wptr<IRemoteObject> &object)
44 {
45 std::lock_guard<std::mutex> lock(connectionMutex_);
46 Disconnect();
47 }
48
GetBundleNameByUid(int32_t uid)49 std::string BundleManagerHelper::GetBundleNameByUid(int32_t uid)
50 {
51 std::string bundle;
52
53 std::lock_guard<std::mutex> lock(connectionMutex_);
54
55 Connect();
56
57 if (bundleMgr_ != nullptr) {
58 std::string identity = IPCSkeleton::ResetCallingIdentity();
59 bundleMgr_->GetNameForUid(uid, bundle);
60 IPCSkeleton::SetCallingIdentity(identity);
61 }
62
63 return bundle;
64 }
IsSystemApp(int32_t uid)65 bool BundleManagerHelper::IsSystemApp(int32_t uid)
66 {
67 bool isSystemApp = false;
68
69 std::lock_guard<std::mutex> lock(connectionMutex_);
70
71 Connect();
72
73 if (bundleMgr_ != nullptr) {
74 isSystemApp = bundleMgr_->CheckIsSystemAppByUid(uid);
75 }
76
77 return isSystemApp;
78 }
79
CheckApiCompatibility(const sptr<NotificationBundleOption> & bundleOption)80 bool BundleManagerHelper::CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption)
81 {
82 AppExecFwk::BundleInfo bundleInfo;
83 int32_t callingUserId;
84 AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId);
85 if (!GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) {
86 ANS_LOGW("Failed to GetBundleInfoByBundleName, bundlename = %{public}s",
87 bundleOption->GetBundleName().c_str());
88 return false;
89 }
90
91 for (auto abilityInfo : bundleInfo.abilityInfos) {
92 if (abilityInfo.isStageBasedModel) {
93 return false;
94 }
95 }
96 return true;
97 }
98
GetBundleInfoByBundleName(const std::string bundle,const int32_t userId,AppExecFwk::BundleInfo & bundleInfo)99 bool BundleManagerHelper::GetBundleInfoByBundleName(
100 const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo)
101 {
102 std::lock_guard<std::mutex> lock(connectionMutex_);
103 Connect();
104
105 if (bundleMgr_ == nullptr) {
106 return false;
107 }
108 bool ret = false;
109 std::string identity = IPCSkeleton::ResetCallingIdentity();
110 ret = bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, userId);
111 IPCSkeleton::SetCallingIdentity(identity);
112 return ret;
113 }
114
Connect()115 void BundleManagerHelper::Connect()
116 {
117 if (bundleMgr_ != nullptr) {
118 return;
119 }
120
121 sptr<ISystemAbilityManager> systemAbilityManager =
122 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
123 if (systemAbilityManager == nullptr) {
124 return;
125 }
126
127 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
128 if (remoteObject == nullptr) {
129 return;
130 }
131
132 bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
133 if (bundleMgr_ != nullptr) {
134 bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_);
135 }
136 }
137
Disconnect()138 void BundleManagerHelper::Disconnect()
139 {
140 if (bundleMgr_ != nullptr) {
141 bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
142 bundleMgr_ = nullptr;
143 }
144 }
145
GetDefaultUidByBundleName(const std::string & bundle,const int32_t userId)146 int32_t BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId)
147 {
148 int32_t uid = -1;
149
150 std::lock_guard<std::mutex> lock(connectionMutex_);
151
152 Connect();
153
154 if (bundleMgr_ != nullptr) {
155 std::string identity = IPCSkeleton::ResetCallingIdentity();
156 uid = bundleMgr_->GetUidByBundleName(bundle, userId);
157 if (uid < 0) {
158 ANS_LOGW("get invalid uid of bundle %{public}s in userId %{public}d", bundle.c_str(), userId);
159 }
160 IPCSkeleton::SetCallingIdentity(identity);
161 }
162
163 return uid;
164 }
165
166 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
GetDistributedNotificationEnabled(const std::string & bundleName,const int32_t userId)167 bool BundleManagerHelper::GetDistributedNotificationEnabled(const std::string &bundleName, const int32_t userId)
168 {
169 std::lock_guard<std::mutex> lock(connectionMutex_);
170
171 Connect();
172
173 if (bundleMgr_ != nullptr) {
174 AppExecFwk::ApplicationInfo appInfo;
175 if (bundleMgr_->GetApplicationInfo(
176 bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, appInfo)) {
177 ANS_LOGD("APPLICATION_INFO distributed enabled %{public}d", appInfo.distributedNotificationEnabled);
178 return appInfo.distributedNotificationEnabled;
179 }
180 }
181
182 ANS_LOGD("APPLICATION_INFO distributed enabled is default");
183 return DEFAULT_DISTRIBUTED_ENABLE_IN_APPLICATION_INFO;
184 }
185 #endif
186
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,int32_t userId,AppExecFwk::BundleInfo & bundleInfo)187 bool BundleManagerHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
188 int32_t userId, AppExecFwk::BundleInfo &bundleInfo)
189 {
190 std::lock_guard<std::mutex> lock(connectionMutex_);
191
192 Connect();
193
194 if (bundleMgr_ == nullptr) {
195 return false;
196 }
197 int32_t callingUserId;
198 AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(userId, callingUserId);
199 std::string identity = IPCSkeleton::ResetCallingIdentity();
200 bool ret = bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo, callingUserId);
201 IPCSkeleton::SetCallingIdentity(identity);
202 return ret;
203 }
204
GetBundleInfos(const AppExecFwk::BundleFlag flag,std::vector<AppExecFwk::BundleInfo> & bundleInfos,int32_t userId)205 bool BundleManagerHelper::GetBundleInfos(
206 const AppExecFwk::BundleFlag flag, std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId)
207 {
208 std::lock_guard<std::mutex> lock(connectionMutex_);
209 Connect();
210
211 if (bundleMgr_ == nullptr) {
212 return false;
213 }
214
215 std::string identity = IPCSkeleton::ResetCallingIdentity();
216 bool ret = bundleMgr_->GetBundleInfos(flag, bundleInfos, userId);
217 IPCSkeleton::SetCallingIdentity(identity);
218 return ret;
219 }
220 } // namespace Notification
221 } // namespace OHOS
222