1 /*
2  * Copyright (c) 2023 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 "iservice_registry.h"
19 #include "system_ability_definition.h"
20 
21 #include "standby_service_errors.h"
22 #include "standby_service_log.h"
23 
24 namespace OHOS {
25 namespace DevStandbyMgr {
26 IMPLEMENT_SINGLE_INSTANCE(BundleManagerHelper);
BundleManagerHelper()27 BundleManagerHelper::BundleManagerHelper()
28 {
29 }
30 
~BundleManagerHelper()31 BundleManagerHelper::~BundleManagerHelper()
32 {
33 }
34 
GetClientBundleName(int32_t uid)35 std::string WEAK_FUNC BundleManagerHelper::GetClientBundleName(int32_t uid)
36 {
37     std::string bundle {""};
38     std::lock_guard<std::mutex> lock(connectionMutex_);
39     Connect();
40     if (bundleMgr_ != nullptr) {
41         bundleMgr_->GetNameForUid(uid, bundle);
42     }
43     STANDBYSERVICE_LOGD("get client Bundle Name: %{public}s", bundle.c_str());
44     return bundle;
45 }
46 
GetApplicationInfo(const std::string & appName,const AppExecFwk::ApplicationFlag flag,const int userId,AppExecFwk::ApplicationInfo & appInfo)47 bool WEAK_FUNC BundleManagerHelper::GetApplicationInfo(const std::string &appName, const
48     AppExecFwk::ApplicationFlag flag, const int userId, AppExecFwk::ApplicationInfo &appInfo)
49 {
50     STANDBYSERVICE_LOGD("start get application info");
51     std::lock_guard<std::mutex> lock(connectionMutex_);
52 
53     Connect();
54     STANDBYSERVICE_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
55     if (bundleMgr_ == nullptr || !bundleMgr_->GetApplicationInfo(appName, flag, userId, appInfo)) {
56         return false;
57     }
58     return true;
59 }
60 
GetApplicationInfos(const AppExecFwk::ApplicationFlag flag,int userId,std::vector<AppExecFwk::ApplicationInfo> & appInfos)61 bool WEAK_FUNC BundleManagerHelper::GetApplicationInfos(const AppExecFwk::ApplicationFlag flag, int userId,
62     std::vector<AppExecFwk::ApplicationInfo> &appInfos)
63 {
64     std::lock_guard<std::mutex> lock(connectionMutex_);
65 
66     Connect();
67     STANDBYSERVICE_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
68     if (bundleMgr_ == nullptr || !bundleMgr_->GetApplicationInfos(flag, userId, appInfos)) {
69         return false;
70     }
71     return true;
72 }
73 
CheckIsSystemAppByUid(const int uid,bool & isSystemApp)74 bool WEAK_FUNC BundleManagerHelper::CheckIsSystemAppByUid(const int uid, bool& isSystemApp)
75 {
76     std::lock_guard<std::mutex> lock(connectionMutex_);
77     Connect();
78     STANDBYSERVICE_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
79     if (bundleMgr_ == nullptr) {
80         return false;
81     }
82     isSystemApp = bundleMgr_->CheckIsSystemAppByUid(uid);
83     return true;
84 }
85 
Connect()86 bool WEAK_FUNC BundleManagerHelper::Connect()
87 {
88     if (bundleMgr_ != nullptr) {
89         return true;
90     }
91 
92     sptr<ISystemAbilityManager> systemAbilityManager =
93         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
94     if (systemAbilityManager == nullptr) {
95         STANDBYSERVICE_LOGE("get SystemAbilityManager failed");
96         return false;
97     }
98 
99     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
100     if (remoteObject == nullptr) {
101         STANDBYSERVICE_LOGE("get Bundle Manager failed");
102         return false;
103     }
104 
105     bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
106     if (bundleMgr_ == nullptr) {
107         STANDBYSERVICE_LOGE("get bundleMgr failed");
108         return false;
109     }
110     return true;
111 }
112 }  // namespace DevStandbyMgr
113 }  // namespace OHOS