• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 #include "tokenid_kit.h"
22 
23 #include "continuous_task_log.h"
24 
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
27 #ifdef BGTASK_MGR_UNIT_TEST
28 #define WEAK_FUNC __attribute__((weak))
29 #else
30 #define WEAK_FUNC
31 #endif
32 
33 using OHOS::AppExecFwk::Constants::PERMISSION_GRANTED;
34 
BundleManagerHelper()35 BundleManagerHelper::BundleManagerHelper()
36 {
37     bundleMgrDeathRecipient_ = new (std::nothrow) RemoteDeathRecipient(
38         [this](const wptr<IRemoteObject> &object) { this->OnRemoteDied(object); });
39 }
40 
~BundleManagerHelper()41 BundleManagerHelper::~BundleManagerHelper()
42 {
43     std::lock_guard<std::mutex> lock(connectionMutex_);
44     Disconnect();
45 }
46 
GetClientBundleName(int32_t uid)47 std::string BundleManagerHelper::GetClientBundleName(int32_t uid)
48 {
49     std::string bundle {""};
50     std::lock_guard<std::mutex> lock(connectionMutex_);
51     Connect();
52     if (bundleMgr_ != nullptr) {
53         bundleMgr_->GetNameForUid(uid, bundle);
54     }
55     BGTASK_LOGD("get client Bundle Name: %{public}s", bundle.c_str());
56     return bundle;
57 }
58 
CheckPermission(const std::string & permission)59 bool BundleManagerHelper::CheckPermission(const std::string &permission)
60 {
61     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
62     int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
63     if (ret != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
64         BGTASK_LOGE("CheckPermission: %{public}s failed", permission.c_str());
65         return false;
66     }
67     return true;
68 }
69 
CheckACLPermission(const std::string & permission,uint64_t callingTokenId)70 bool BundleManagerHelper::CheckACLPermission(const std::string &permission, uint64_t callingTokenId)
71 {
72     int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callingTokenId, permission);
73     if (ret != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
74         BGTASK_LOGD("CheckPermission: %{public}s failed", permission.c_str());
75         return false;
76     }
77     return true;
78 }
79 
IsSystemApp(uint64_t fullTokenId)80 bool BundleManagerHelper::IsSystemApp(uint64_t fullTokenId)
81 {
82     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
83 }
84 
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)85 bool BundleManagerHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
86     AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
87 {
88     std::lock_guard<std::mutex> lock(connectionMutex_);
89 
90     Connect();
91 
92     if (bundleMgr_ != nullptr && bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo, userId)) {
93         return true;
94     }
95     return false;
96 }
97 
GetApplicationInfo(const std::string & appName,const AppExecFwk::ApplicationFlag flag,const int userId,AppExecFwk::ApplicationInfo & appInfo)98 bool WEAK_FUNC BundleManagerHelper::GetApplicationInfo(const std::string &appName,
99     const AppExecFwk::ApplicationFlag flag, const int userId, AppExecFwk::ApplicationInfo &appInfo)
100 {
101     BGTASK_LOGD("start get application info");
102     std::lock_guard<std::mutex> lock(connectionMutex_);
103 
104     Connect();
105     BGTASK_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
106     if (bundleMgr_ != nullptr && bundleMgr_->GetApplicationInfo(appName, flag, userId, appInfo)) {
107         return true;
108     }
109     return false;
110 }
111 
QueryAbilityInfo(const AAFwk::Want & want,int32_t flags,int32_t userId,AppExecFwk::AbilityInfo & abilityInfo)112 bool BundleManagerHelper::QueryAbilityInfo(const AAFwk::Want &want, int32_t flags, int32_t userId,
113     AppExecFwk::AbilityInfo &abilityInfo)
114 {
115     std::lock_guard<std::mutex> lock(connectionMutex_);
116     Connect();
117     if (bundleMgr_ != nullptr && bundleMgr_->QueryAbilityInfo(want, flags, userId, abilityInfo)) {
118         return true;
119     }
120     return false;
121 }
122 
GetBundleResourceInfo(const std::string & bundleName,const AppExecFwk::ResourceFlag flags,AppExecFwk::BundleResourceInfo & bundleResourceInfo)123 bool BundleManagerHelper::GetBundleResourceInfo(const std::string &bundleName, const AppExecFwk::ResourceFlag flags,
124     AppExecFwk::BundleResourceInfo &bundleResourceInfo)
125 {
126     std::lock_guard<std::mutex> lock(connectionMutex_);
127     Connect();
128     if (bundleMgr_ == nullptr) {
129         return false;
130     }
131     sptr<AppExecFwk::IBundleResource> bundleResourceProxy = bundleMgr_->GetBundleResourceProxy();
132     if (bundleResourceProxy == nullptr) {
133         return false;
134     }
135     ErrCode result = bundleResourceProxy->
136         GetBundleResourceInfo(bundleName, static_cast<uint32_t>(flags), bundleResourceInfo);
137     if (result != ERR_OK) {
138         BGTASK_LOGE("GetBundleResourceInfo fail, errcode: %{public}d", result);
139         return false;
140     }
141     return true;
142 }
143 
Connect()144 bool BundleManagerHelper::Connect()
145 {
146     if (bundleMgr_ != nullptr) {
147         return true;
148     }
149 
150     sptr<ISystemAbilityManager> systemAbilityManager =
151         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
152     if (systemAbilityManager == nullptr) {
153         BGTASK_LOGE("get SystemAbilityManager failed");
154         return false;
155     }
156 
157     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
158     if (remoteObject == nullptr) {
159         BGTASK_LOGE("get Bundle Manager failed");
160         return false;
161     }
162 
163     bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
164     if (bundleMgr_ != nullptr && bundleMgrDeathRecipient_ != nullptr) {
165         bundleMgr_->AsObject()->AddDeathRecipient(bundleMgrDeathRecipient_);
166         return true;
167     }
168     BGTASK_LOGE("get bundleMgr failed");
169     return false;
170 }
171 
Disconnect()172 void BundleManagerHelper::Disconnect()
173 {
174     if (bundleMgr_ != nullptr && bundleMgr_->AsObject() != nullptr) {
175         bundleMgr_->AsObject()->RemoveDeathRecipient(bundleMgrDeathRecipient_);
176         bundleMgr_ = nullptr;
177     }
178 }
179 
OnRemoteDied(const wptr<IRemoteObject> & object)180 void BundleManagerHelper::OnRemoteDied(const wptr<IRemoteObject> &object)
181 {
182     std::lock_guard<std::mutex> lock(connectionMutex_);
183     Disconnect();
184 }
185 }  // namespace BackgroundTaskMgr
186 }  // namespace OHOS