1 /*
2 * Copyright (c) 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 "accesstoken_kit.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 #include "continuous_task_log.h"
23
24 namespace OHOS {
25 namespace BackgroundTaskMgr {
26 using OHOS::AppExecFwk::Constants::PERMISSION_GRANTED;
27
BundleManagerHelper()28 BundleManagerHelper::BundleManagerHelper()
29 {
30 bundleMgrDeathRecipient_ = new (std::nothrow) RemoteDeathRecipient(
31 [this](const wptr<IRemoteObject> &object) { this->OnRemoteDied(object); });
32 }
33
~BundleManagerHelper()34 BundleManagerHelper::~BundleManagerHelper()
35 {
36 std::lock_guard<std::mutex> lock(connectionMutex_);
37 Disconnect();
38 }
39
GetClientBundleName(int32_t uid)40 std::string BundleManagerHelper::GetClientBundleName(int32_t uid)
41 {
42 std::string bundle {""};
43 std::lock_guard<std::mutex> lock(connectionMutex_);
44 Connect();
45 if (bundleMgr_ != nullptr) {
46 bundleMgr_->GetBundleNameForUid(uid, bundle);
47 }
48 BGTASK_LOGD("get client Bundle Name: %{public}s", bundle.c_str());
49 return bundle;
50 }
51
CheckPermission(const std::string & permission)52 bool BundleManagerHelper::CheckPermission(const std::string &permission)
53 {
54 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
55 int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
56 if (ret != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
57 BGTASK_LOGE("CheckPermission: %{public}s failed", permission.c_str());
58 return false;
59 }
60 return true;
61 }
62
IsSystemApp(int32_t uid)63 bool BundleManagerHelper::IsSystemApp(int32_t uid)
64 {
65 bool isSystemApp = false;
66 std::lock_guard<std::mutex> lock(connectionMutex_);
67 Connect();
68 if (bundleMgr_ != nullptr) {
69 isSystemApp = bundleMgr_->CheckIsSystemAppByUid(uid);
70 }
71 return isSystemApp;
72 }
73
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)74 bool BundleManagerHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
75 AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
76 {
77 std::lock_guard<std::mutex> lock(connectionMutex_);
78
79 Connect();
80
81 if (bundleMgr_ != nullptr && bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo, userId)) {
82 return true;
83 }
84 return false;
85 }
86
GetApplicationInfo(const std::string & appName,const AppExecFwk::ApplicationFlag flag,const int userId,AppExecFwk::ApplicationInfo & appInfo)87 bool BundleManagerHelper::GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag,
88 const int userId, AppExecFwk::ApplicationInfo &appInfo)
89 {
90 BGTASK_LOGD("start get application info");
91 std::lock_guard<std::mutex> lock(connectionMutex_);
92
93 Connect();
94 BGTASK_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
95 if (bundleMgr_ != nullptr && bundleMgr_->GetApplicationInfo(appName, flag, userId, appInfo)) {
96 return true;
97 }
98 return false;
99 }
100
QueryAbilityInfo(const AAFwk::Want & want,int32_t flags,int32_t userId,AppExecFwk::AbilityInfo & abilityInfo)101 bool BundleManagerHelper::QueryAbilityInfo(const AAFwk::Want &want, int32_t flags, int32_t userId,
102 AppExecFwk::AbilityInfo &abilityInfo)
103 {
104 std::lock_guard<std::mutex> lock(connectionMutex_);
105 Connect();
106 if (bundleMgr_ != nullptr && bundleMgr_->QueryAbilityInfo(want, flags, userId, abilityInfo)) {
107 return true;
108 }
109 return false;
110 }
111
Connect()112 bool BundleManagerHelper::Connect()
113 {
114 if (bundleMgr_ != nullptr) {
115 return true;
116 }
117
118 sptr<ISystemAbilityManager> systemAbilityManager =
119 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
120 if (systemAbilityManager == nullptr) {
121 BGTASK_LOGE("get SystemAbilityManager failed");
122 return false;
123 }
124
125 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
126 if (remoteObject == nullptr) {
127 BGTASK_LOGE("get Bundle Manager failed");
128 return false;
129 }
130
131 bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
132 if (bundleMgr_ != nullptr && bundleMgrDeathRecipient_ != nullptr) {
133 bundleMgr_->AsObject()->AddDeathRecipient(bundleMgrDeathRecipient_);
134 return true;
135 }
136 BGTASK_LOGE("get bundleMgr failed");
137 return false;
138 }
139
Disconnect()140 void BundleManagerHelper::Disconnect()
141 {
142 if (bundleMgr_ != nullptr && bundleMgr_->AsObject() != nullptr) {
143 bundleMgr_->AsObject()->RemoveDeathRecipient(bundleMgrDeathRecipient_);
144 bundleMgr_ = nullptr;
145 }
146 }
147
OnRemoteDied(const wptr<IRemoteObject> & object)148 void BundleManagerHelper::OnRemoteDied(const wptr<IRemoteObject> &object)
149 {
150 std::lock_guard<std::mutex> lock(connectionMutex_);
151 Disconnect();
152 }
153 } // namespace BackgroundTaskMgr
154 } // namespace OHOS