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 return true;
76 }
77
CheckIsSystemAppByUid(const int uid,bool & isSystemApp)78 bool WEAK_FUNC BundleManagerHelper::CheckIsSystemAppByUid(const int uid, bool& isSystemApp)
79 {
80 std::lock_guard<std::mutex> lock(connectionMutex_);
81 Connect();
82 STANDBYSERVICE_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
83 if (bundleMgr_ == nullptr) {
84 return false;
85 }
86 isSystemApp = bundleMgr_->CheckIsSystemAppByUid(uid);
87 return true;
88 }
89
Connect()90 bool WEAK_FUNC BundleManagerHelper::Connect()
91 {
92 if (bundleMgr_ != nullptr) {
93 return true;
94 }
95
96 sptr<ISystemAbilityManager> systemAbilityManager =
97 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
98 if (systemAbilityManager == nullptr) {
99 STANDBYSERVICE_LOGE("get SystemAbilityManager failed");
100 return false;
101 }
102
103 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
104 if (remoteObject == nullptr) {
105 STANDBYSERVICE_LOGE("get Bundle Manager failed");
106 return false;
107 }
108
109 bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
110 if (bundleMgr_ == nullptr) {
111 STANDBYSERVICE_LOGE("get bundleMgr failed");
112 return false;
113 }
114 return true;
115 }
116 } // namespace DevStandbyMgr
117 } // namespace OHOS