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 "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_ =
31 new RemoteDeathRecipient(std::bind(&BundleManagerHelper::OnRemoteDied, this, std::placeholders::_1));
32 }
33
~BundleManagerHelper()34 BundleManagerHelper::~BundleManagerHelper()
35 {
36 std::lock_guard<std::mutex> lock(connectionMutex_);
37 Disconnect();
38 }
39
OnRemoteDied(const wptr<IRemoteObject> & object)40 void BundleManagerHelper::OnRemoteDied(const wptr<IRemoteObject> &object)
41 {
42 std::lock_guard<std::mutex> lock(connectionMutex_);
43 Disconnect();
44 }
45
GetBundleNameByUid(int32_t uid)46 std::string BundleManagerHelper::GetBundleNameByUid(int32_t uid)
47 {
48 std::string bundle;
49
50 std::lock_guard<std::mutex> lock(connectionMutex_);
51
52 Connect();
53
54 if (bundleMgr_ != nullptr) {
55 std::string identity = IPCSkeleton::ResetCallingIdentity();
56 bundleMgr_->GetNameForUid(uid, bundle);
57 IPCSkeleton::SetCallingIdentity(identity);
58 }
59
60 return bundle;
61 }
IsSystemApp(int32_t uid)62 bool BundleManagerHelper::IsSystemApp(int32_t uid)
63 {
64 bool isSystemApp = false;
65
66 std::lock_guard<std::mutex> lock(connectionMutex_);
67
68 Connect();
69
70 if (bundleMgr_ != nullptr) {
71 isSystemApp = bundleMgr_->CheckIsSystemAppByUid(uid);
72 }
73
74 return isSystemApp;
75 }
76
CheckApiCompatibility(const sptr<NotificationBundleOption> & bundleOption)77 bool BundleManagerHelper::CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption)
78 {
79 AppExecFwk::BundleInfo bundleInfo;
80 int32_t callingUserId;
81 AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId);
82 if (!GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) {
83 ANS_LOGW("Failed to GetBundleInfoByBundleName, bundlename = %{public}s",
84 bundleOption->GetBundleName().c_str());
85 return false;
86 }
87
88 for (auto abilityInfo : bundleInfo.abilityInfos) {
89 if (abilityInfo.isStageBasedModel) {
90 return false;
91 }
92 }
93 return true;
94 }
95
GetBundleInfoByBundleName(const std::string bundle,const int32_t userId,AppExecFwk::BundleInfo & bundleInfo)96 bool BundleManagerHelper::GetBundleInfoByBundleName(
97 const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo)
98 {
99 if (bundleMgr_ == nullptr) {
100 return false;
101 }
102 bool ret = false;
103 std::string identity = IPCSkeleton::ResetCallingIdentity();
104 ret = bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, userId);
105 IPCSkeleton::SetCallingIdentity(identity);
106 return ret;
107 }
108
Connect()109 void BundleManagerHelper::Connect()
110 {
111 if (bundleMgr_ != nullptr) {
112 return;
113 }
114
115 sptr<ISystemAbilityManager> systemAbilityManager =
116 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
117 if (systemAbilityManager == nullptr) {
118 return;
119 }
120
121 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
122 if (remoteObject == nullptr) {
123 return;
124 }
125
126 bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
127 if (bundleMgr_ != nullptr) {
128 bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_);
129 }
130 }
131
Disconnect()132 void BundleManagerHelper::Disconnect()
133 {
134 if (bundleMgr_ != nullptr) {
135 bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
136 bundleMgr_ = nullptr;
137 }
138 }
139
GetDefaultUidByBundleName(const std::string & bundle,const int32_t userId)140 int32_t BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId)
141 {
142 int32_t uid = -1;
143
144 std::lock_guard<std::mutex> lock(connectionMutex_);
145
146 Connect();
147
148 if (bundleMgr_ != nullptr) {
149 std::string identity = IPCSkeleton::ResetCallingIdentity();
150 uid = bundleMgr_->GetUidByBundleName(bundle, userId);
151 if (uid < 0) {
152 ANS_LOGW("get invalid uid of bundle %{public}s in userId %{public}d", bundle.c_str(), userId);
153 }
154 IPCSkeleton::SetCallingIdentity(identity);
155 }
156
157 return uid;
158 }
159
160 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
GetDistributedNotificationEnabled(const std::string & bundleName,const int32_t userId)161 bool BundleManagerHelper::GetDistributedNotificationEnabled(const std::string &bundleName, const int32_t userId)
162 {
163 std::lock_guard<std::mutex> lock(connectionMutex_);
164
165 Connect();
166
167 if (bundleMgr_ != nullptr) {
168 AppExecFwk::ApplicationInfo appInfo;
169 if (bundleMgr_->GetApplicationInfo(
170 bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, appInfo)) {
171 ANS_LOGD("APPLICATION_INFO distributed enabled %{public}d", appInfo.distributedNotificationEnabled);
172 return appInfo.distributedNotificationEnabled;
173 }
174 }
175
176 ANS_LOGD("APPLICATION_INFO distributed enabled is default");
177 return DEFAULT_DISTRIBUTED_ENABLE_IN_APPLICATION_INFO;
178 }
179 #endif
180 } // namespace Notification
181 } // namespace OHOS