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 #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 
IsSystemApp(uint64_t fullTokenId)70 bool BundleManagerHelper::IsSystemApp(uint64_t fullTokenId)
71 {
72     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
73 }
74 
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)75 bool BundleManagerHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
76     AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
77 {
78     std::lock_guard<std::mutex> lock(connectionMutex_);
79 
80     Connect();
81 
82     if (bundleMgr_ != nullptr && bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo, userId)) {
83         return true;
84     }
85     return false;
86 }
87 
GetApplicationInfo(const std::string & appName,const AppExecFwk::ApplicationFlag flag,const int userId,AppExecFwk::ApplicationInfo & appInfo)88 bool WEAK_FUNC BundleManagerHelper::GetApplicationInfo(const std::string &appName,
89     const AppExecFwk::ApplicationFlag flag, const int userId, AppExecFwk::ApplicationInfo &appInfo)
90 {
91     BGTASK_LOGD("start get application info");
92     std::lock_guard<std::mutex> lock(connectionMutex_);
93 
94     Connect();
95     BGTASK_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
96     if (bundleMgr_ != nullptr && bundleMgr_->GetApplicationInfo(appName, flag, userId, appInfo)) {
97         return true;
98     }
99     return false;
100 }
101 
QueryAbilityInfo(const AAFwk::Want & want,int32_t flags,int32_t userId,AppExecFwk::AbilityInfo & abilityInfo)102 bool BundleManagerHelper::QueryAbilityInfo(const AAFwk::Want &want, int32_t flags, int32_t userId,
103     AppExecFwk::AbilityInfo &abilityInfo)
104 {
105     std::lock_guard<std::mutex> lock(connectionMutex_);
106     Connect();
107     if (bundleMgr_ != nullptr && bundleMgr_->QueryAbilityInfo(want, flags, userId, abilityInfo)) {
108         return true;
109     }
110     return false;
111 }
112 
Connect()113 bool BundleManagerHelper::Connect()
114 {
115     if (bundleMgr_ != nullptr) {
116         return true;
117     }
118 
119     sptr<ISystemAbilityManager> systemAbilityManager =
120         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
121     if (systemAbilityManager == nullptr) {
122         BGTASK_LOGE("get SystemAbilityManager failed");
123         return false;
124     }
125 
126     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
127     if (remoteObject == nullptr) {
128         BGTASK_LOGE("get Bundle Manager failed");
129         return false;
130     }
131 
132     bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
133     if (bundleMgr_ != nullptr && bundleMgrDeathRecipient_ != nullptr) {
134         bundleMgr_->AsObject()->AddDeathRecipient(bundleMgrDeathRecipient_);
135         return true;
136     }
137     BGTASK_LOGE("get bundleMgr failed");
138     return false;
139 }
140 
Disconnect()141 void BundleManagerHelper::Disconnect()
142 {
143     if (bundleMgr_ != nullptr && bundleMgr_->AsObject() != nullptr) {
144         bundleMgr_->AsObject()->RemoveDeathRecipient(bundleMgrDeathRecipient_);
145         bundleMgr_ = nullptr;
146     }
147 }
148 
OnRemoteDied(const wptr<IRemoteObject> & object)149 void BundleManagerHelper::OnRemoteDied(const wptr<IRemoteObject> &object)
150 {
151     std::lock_guard<std::mutex> lock(connectionMutex_);
152     Disconnect();
153 }
154 }  // namespace BackgroundTaskMgr
155 }  // namespace OHOS