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