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