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