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_active_bundle_mgr_helper.h"
17 
18 #include "bundle_active_constant.h"
19 #include "bundle_active_log.h"
20 #include "accesstoken_kit.h"
21 #include "application_info.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "tokenid_kit.h"
25 
26 namespace OHOS {
27 namespace DeviceUsageStats {
28 using OHOS::AppExecFwk::Constants::PERMISSION_GRANTED;
29 
BundleActiveBundleMgrHelper()30 BundleActiveBundleMgrHelper::BundleActiveBundleMgrHelper()
31 {
32 }
33 
~BundleActiveBundleMgrHelper()34 BundleActiveBundleMgrHelper::~BundleActiveBundleMgrHelper()
35 {
36     launcherAppMap_.clear();
37     packageContainUidMap_.clear();
38 }
39 
GetNameForUid(int32_t uid,std::string & bundleName)40 void BundleActiveBundleMgrHelper::GetNameForUid(int32_t uid, std::string& bundleName)
41 {
42     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
43     if (!Connect()) {
44         return;
45     }
46     if (!bundleMgr_) {
47         return;
48     }
49     bundleMgr_->GetNameForUid(uid, bundleName);
50     BUNDLE_ACTIVE_LOGD("get Bundle Name: %{public}s", bundleName.c_str());
51 }
52 
GetApplicationInfo(const std::string & appName,const AppExecFwk::ApplicationFlag flag,const int userId,AppExecFwk::ApplicationInfo & appInfo)53 bool BundleActiveBundleMgrHelper::GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag,
54     const int userId, AppExecFwk::ApplicationInfo &appInfo)
55 {
56     BUNDLE_ACTIVE_LOGD("start get application info");
57     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
58 
59     if (!Connect()) {
60         return false;
61     }
62     if (!bundleMgr_) {
63         return false;
64     }
65     BUNDLE_ACTIVE_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
66     if (bundleMgr_ != nullptr && bundleMgr_->GetApplicationInfo(appName, flag, userId, appInfo)) {
67         return true;
68     }
69     return false;
70 }
71 
GetApplicationInfos(const AppExecFwk::ApplicationFlag flag,const int userId,std::vector<AppExecFwk::ApplicationInfo> & appInfos)72 bool BundleActiveBundleMgrHelper::GetApplicationInfos(const AppExecFwk::ApplicationFlag flag,
73     const int userId, std::vector<AppExecFwk::ApplicationInfo> &appInfos)
74 {
75     BUNDLE_ACTIVE_LOGI("start get application infos");
76     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
77 
78     if (!Connect()) {
79         return false;
80     }
81     BUNDLE_ACTIVE_LOGI("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
82     if (bundleMgr_ != nullptr && bundleMgr_->GetApplicationInfos(flag, userId, appInfos)) {
83         return true;
84     }
85     return false;
86 }
87 
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)88 bool BundleActiveBundleMgrHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
89     AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
90 {
91     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
92 
93     if (!Connect()) {
94         return false;
95     }
96     if (!bundleMgr_) {
97         return false;
98     }
99     if (bundleMgr_ != nullptr && bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo, userId)) {
100         return true;
101     }
102     return false;
103 }
104 
Connect()105 bool BundleActiveBundleMgrHelper::Connect()
106 {
107     if (bundleMgr_ != nullptr) {
108         return true;
109     }
110 
111     sptr<ISystemAbilityManager> systemAbilityManager =
112         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
113     if (systemAbilityManager == nullptr) {
114         BUNDLE_ACTIVE_LOGE("get SystemAbilityManager failed");
115         return false;
116     }
117 
118     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
119     if (remoteObject == nullptr) {
120         BUNDLE_ACTIVE_LOGE("get Bundle Manager failed");
121         return false;
122     }
123 
124     bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
125     return bundleMgr_ ? true : false;
126 }
127 
IsLauncherApp(const std::string & bundleName,const int32_t userId)128 bool BundleActiveBundleMgrHelper::IsLauncherApp(const std::string &bundleName, const int32_t userId)
129 {
130     if (!isInitLauncherAppMap_) {
131         InitLauncherAppMap();
132     }
133     if (launcherAppMap_.find(bundleName) != launcherAppMap_.end()) {
134         BUNDLE_ACTIVE_LOGD("launcherAppMap cache, bundleName:%{public}s isLauncherApp:%{public}d",
135             bundleName.c_str(), launcherAppMap_[bundleName]);
136         return launcherAppMap_[bundleName];
137     }
138     AppExecFwk::ApplicationInfo appInfo;
139     if (!GetApplicationInfo(bundleName,
140         AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, appInfo)) {
141         BUNDLE_ACTIVE_LOGE("get applicationInfo failed.");
142         return false;
143     }
144     launcherAppMap_[bundleName] = appInfo.isLauncherApp;
145     BUNDLE_ACTIVE_LOGD("insert launcherAppMap, bundleName:%{public}s isLauncherApp:%{public}d",
146         bundleName.c_str(), launcherAppMap_[bundleName]);
147     return appInfo.isLauncherApp;
148 }
149 
InitLauncherAppMap()150 void BundleActiveBundleMgrHelper::InitLauncherAppMap()
151 {
152     BUNDLE_ACTIVE_LOGI("init laucherAppMap");
153     isInitLauncherAppMap_ = true;
154     InitSystemEvent();
155     std::vector<AppExecFwk::ApplicationInfo> appInfos;
156     if (!GetApplicationInfos(AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO,
157         AppExecFwk::Constants::ALL_USERID, appInfos)) {
158         BUNDLE_ACTIVE_LOGE("Init Launcher App Map by BMS failed");
159         return;
160     }
161     for (auto appInfo : appInfos) {
162         launcherAppMap_[appInfo.bundleName] = appInfo.isLauncherApp;
163     }
164 }
165 
InitSystemEvent()166 void BundleActiveBundleMgrHelper::InitSystemEvent()
167 {
168     launcherAppMap_[OPERATION_SYSTEM_LOCK] = false;
169     launcherAppMap_[OPERATION_SYSTEM_UNLOCK] = false;
170     launcherAppMap_[OPERATION_SYSTEM_SLEEP] = false;
171     launcherAppMap_[OPERATION_SYSTEM_WAKEUP] = false;
172 }
173 
InsertPackageUid(const std::string & bundleName,const int32_t uid)174 void BundleActiveBundleMgrHelper::InsertPackageUid(const std::string &bundleName, const int32_t uid)
175 {
176     std::lock_guard<ffrt::mutex> lock(packageContainUidMapMutex_);
177     packageContainUidMap_[bundleName].insert(uid);
178 }
179 
DeletePackageUid(const std::string & bundleName,const int32_t uid)180 void BundleActiveBundleMgrHelper::DeletePackageUid(const std::string &bundleName, const int32_t uid)
181 {
182     std::lock_guard<ffrt::mutex> lock(packageContainUidMapMutex_);
183     packageContainUidMap_[bundleName].erase(uid);
184     if (packageContainUidMap_[bundleName].size() == 0) {
185         packageContainUidMap_.erase(bundleName);
186     }
187 }
188 
DeleteMemPackage(const std::string & bundleName)189 void BundleActiveBundleMgrHelper::DeleteMemPackage(const std::string &bundleName)
190 {
191     std::lock_guard<ffrt::mutex> lock(packageContainUidMapMutex_);
192     packageContainUidMap_.erase(bundleName);
193 }
194 
GetPackageUidSet(const std::string & bundleName)195 std::set<int32_t> BundleActiveBundleMgrHelper::GetPackageUidSet(const std::string &bundleName)
196 {
197     std::lock_guard<ffrt::mutex> lock(packageContainUidMapMutex_);
198     auto iter = packageContainUidMap_.find(bundleName);
199     if (iter == packageContainUidMap_.end()) {
200         return std::set<int32_t>();
201     }
202     return iter->second;
203 }
204 
205 }  // namespace DeviceUsageStats
206 }  // namespace OHOS