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 "../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
Init(const sptr<AppExecFwk::IBundleMgr> & bundleMgr)26 void AbilityInfoManager::Init(const sptr<AppExecFwk::IBundleMgr>& bundleMgr)
27 {
28 if (bundleMgr == nullptr) {
29 TLOGE(WmsLogTag::WMS_LIFE, "bundleMgr is nullptr");
30 return;
31 }
32 bundleMgr_ = bundleMgr;
33 }
34
SetCurrentUserId(int32_t userId)35 void AbilityInfoManager::SetCurrentUserId(int32_t userId)
36 {
37 TLOGI(WmsLogTag::WMS_LIFE, "userId: %{public}d", userId);
38 std::unique_lock<std::mutex> lock(applicationInfoMutex_);
39 userId_ = userId;
40 }
41
IsAnco(const std::string & bundleName,const std::string & abilityName,const std::string & moduleName)42 bool AbilityInfoManager::IsAnco(const std::string& bundleName, const std::string& abilityName,
43 const std::string& moduleName)
44 {
45 bool isAnco = false;
46 std::unique_lock<std::mutex> lock(applicationInfoMutex_);
47 auto iter = applicationInfoMap_.find(bundleName);
48 if (iter == applicationInfoMap_.end()) {
49 if (bundleMgr_ == nullptr) {
50 TLOGE(WmsLogTag::WMS_LIFE, "bundleMgr is nullptr!");
51 return isAnco;
52 }
53 AAFwk::Want want;
54 want.SetElementName("", bundleName, abilityName, moduleName);
55 auto abilityInfoFlag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION;
56 AppExecFwk::AbilityInfo abilityInfo;
57 bool ret = bundleMgr_->QueryAbilityInfo(want, abilityInfoFlag, userId_, abilityInfo);
58 if (!ret) {
59 TLOGE(WmsLogTag::WMS_LIFE, "Get ability info from BMS failed!");
60 return isAnco;
61 }
62 applicationInfoMap_[bundleName] = abilityInfo.applicationInfo.codePath;
63 TLOGI(WmsLogTag::WMS_LIFE, "bundleName: %{public}s, abilityName: %{public}s, moduleName: %{public}s, "
64 "userId: %{public}d, abilityInfoFlag: %{public}d, codePath: %{public}s", bundleName.c_str(),
65 abilityName.c_str(), moduleName.c_str(), userId_, abilityInfoFlag,
66 abilityInfo.applicationInfo.codePath.c_str());
67 isAnco = abilityInfo.applicationInfo.codePath == std::to_string(CollaboratorType::RESERVE_TYPE) ||
68 abilityInfo.applicationInfo.codePath == std::to_string(CollaboratorType::OTHERS_TYPE);
69 } else {
70 TLOGI(WmsLogTag::WMS_LIFE, "applicationInfo already in applicationInfoMap_, codePath: %{public}s",
71 iter->second.c_str());
72 isAnco = iter->second == std::to_string(CollaboratorType::RESERVE_TYPE) ||
73 iter->second == std::to_string(CollaboratorType::OTHERS_TYPE);
74 }
75 return isAnco;
76 }
77
RemoveAppInfo(const std::string & bundleName)78 void AbilityInfoManager::RemoveAppInfo(const std::string& bundleName)
79 {
80 std::unique_lock<std::mutex> lock(applicationInfoMutex_);
81 applicationInfoMap_.erase(bundleName);
82 }
83 } // namespace OHOS::Rosen