1 /*
2 * Copyright (c) 2024 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 "ability_info_manager.h"
17
18 #include <bundlemgr/launcher_service.h>
19
20 #include "ws_common.h"
21 #include "window_manager_hilog.h"
22
23 namespace OHOS::Rosen {
24 WM_IMPLEMENT_SINGLE_INSTANCE(AbilityInfoManager);
25
26 // LCOV_EXCL_START
FindAbilityInfo(const AppExecFwk::BundleInfo & bundleInfo,const std::string & moduleName,const std::string & abilityName,AppExecFwk::AbilityInfo & abilityInfo)27 bool AbilityInfoManager::FindAbilityInfo(const AppExecFwk::BundleInfo& bundleInfo,
28 const std::string& moduleName, const std::string& abilityName, AppExecFwk::AbilityInfo& abilityInfo)
29 {
30 auto& hapModulesList = bundleInfo.hapModuleInfos;
31 for (auto& hapModule : hapModulesList) {
32 auto& abilityInfoList = hapModule.abilityInfos;
33 for (auto& ability : abilityInfoList) {
34 if (ability.moduleName == moduleName && ability.name == abilityName) {
35 abilityInfo = ability;
36 return true;
37 }
38 }
39 }
40 TLOGW(WmsLogTag::DEFAULT, "ability info not found, bundle:%{public}s", bundleInfo.name.c_str());
41 return false;
42 }
43 // LCOV_EXCL_STOP
44
Init(const sptr<AppExecFwk::IBundleMgr> & bundleMgr)45 void AbilityInfoManager::Init(const sptr<AppExecFwk::IBundleMgr>& bundleMgr)
46 {
47 if (bundleMgr == nullptr) {
48 TLOGE(WmsLogTag::WMS_LIFE, "bundleMgr is nullptr");
49 return;
50 }
51 bundleMgr_ = bundleMgr;
52 }
53
SetCurrentUserId(int32_t userId)54 void AbilityInfoManager::SetCurrentUserId(int32_t userId)
55 {
56 TLOGI(WmsLogTag::WMS_LIFE, "userId: %{public}d", userId);
57 std::unique_lock<std::mutex> lock(applicationInfoMutex_);
58 userId_ = userId;
59 }
60
61 // LCOV_EXCL_START
IsAnco(const std::string & bundleName,const std::string & abilityName,const std::string & moduleName)62 bool AbilityInfoManager::IsAnco(const std::string& bundleName, const std::string& abilityName,
63 const std::string& moduleName)
64 {
65 bool isAnco = false;
66 std::unique_lock<std::mutex> lock(applicationInfoMutex_);
67 auto iter = applicationInfoMap_.find(bundleName);
68 if (iter == applicationInfoMap_.end()) {
69 if (bundleMgr_ == nullptr) {
70 TLOGE(WmsLogTag::WMS_LIFE, "bundleMgr is nullptr!");
71 return isAnco;
72 }
73 AAFwk::Want want;
74 want.SetElementName("", bundleName, abilityName, moduleName);
75 auto abilityInfoFlag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION;
76 AppExecFwk::AbilityInfo abilityInfo;
77 bool ret = bundleMgr_->QueryAbilityInfo(want, abilityInfoFlag, userId_, abilityInfo);
78 if (!ret) {
79 TLOGE(WmsLogTag::WMS_LIFE, "Get ability info from BMS failed!");
80 return isAnco;
81 }
82 applicationInfoMap_[bundleName] = abilityInfo.applicationInfo.codePath;
83 TLOGI(WmsLogTag::WMS_LIFE, "bundleName: %{public}s, abilityName: %{public}s, moduleName: %{public}s, "
84 "userId: %{public}d, abilityInfoFlag: %{public}d, codePath: %{public}s", bundleName.c_str(),
85 abilityName.c_str(), moduleName.c_str(), userId_, abilityInfoFlag,
86 abilityInfo.applicationInfo.codePath.c_str());
87 isAnco = abilityInfo.applicationInfo.codePath == std::to_string(CollaboratorType::RESERVE_TYPE) ||
88 abilityInfo.applicationInfo.codePath == std::to_string(CollaboratorType::OTHERS_TYPE);
89 } else {
90 TLOGI(WmsLogTag::WMS_LIFE, "applicationInfo already in applicationInfoMap_, codePath: %{public}s",
91 iter->second.c_str());
92 isAnco = iter->second == std::to_string(CollaboratorType::RESERVE_TYPE) ||
93 iter->second == std::to_string(CollaboratorType::OTHERS_TYPE);
94 }
95 return isAnco;
96 }
97
RemoveAppInfo(const std::string & bundleName)98 void AbilityInfoManager::RemoveAppInfo(const std::string& bundleName)
99 {
100 std::unique_lock<std::mutex> lock(applicationInfoMutex_);
101 applicationInfoMap_.erase(bundleName);
102 }
103 // LCOV_EXCL_STOP
104 } // namespace OHOS::Rosen