• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2025 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 "accessibility_resource_bundle_manager.h"
17 #include "accessible_ability_manager_service.h"
18 #include "hilog_wrapper.h"
19 #include "system_ability_definition.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "bundle_info.h"
23 #include <cstdint>
24 #include "utils.h"
25 
26 namespace OHOS {
27 namespace Accessibility {
AccessibilityResourceBundleManager()28 AccessibilityResourceBundleManager::AccessibilityResourceBundleManager()
29 {
30 }
31 
~AccessibilityResourceBundleManager()32 AccessibilityResourceBundleManager::~AccessibilityResourceBundleManager()
33 {
34 }
35 
GetBundleMgrProxy()36 sptr<AppExecFwk::IBundleMgr> AccessibilityResourceBundleManager::GetBundleMgrProxy()
37 {
38     HILOG_DEBUG();
39     if (bundleManager_) {
40         return bundleManager_;
41     }
42 
43     sptr<ISystemAbilityManager> systemAbilityManager =
44         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45     if (!systemAbilityManager) {
46         HILOG_ERROR("failed:fail to get system ability mgr.");
47         return nullptr;
48     }
49 
50     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
51     if (!remoteObject) {
52         HILOG_ERROR("failed:fail to get bundle manager proxy.");
53         return nullptr;
54     }
55 
56     bundleManager_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
57     if (!bundleManager_) {
58         HILOG_ERROR("fail to new bundle manager.");
59         return nullptr;
60     }
61 
62     if (!bundleManagerDeathRecipient_) {
63         bundleManagerDeathRecipient_ = new(std::nothrow) BundleManagerDeathRecipient();
64         if (!bundleManagerDeathRecipient_) {
65             HILOG_ERROR("bundleManagerDeathRecipient_ is null");
66             return nullptr;
67         }
68     }
69 
70     if (!bundleManager_) {
71         HILOG_ERROR("bundleManager_ is nullptr");
72         return nullptr;
73     }
74     bundleManager_->AsObject()->AddDeathRecipient(bundleManagerDeathRecipient_);
75     return bundleManager_;
76 }
77 
GetBundleInfoV9(const std::string & bundleName,int32_t flags,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)78 ErrCode AccessibilityResourceBundleManager::GetBundleInfoV9(const std::string& bundleName, int32_t flags,
79     AppExecFwk::BundleInfo& bundleInfo, int32_t userId)
80 {
81     std::lock_guard<ffrt::mutex> lock(bundleMutex_);
82     sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleMgrProxy();
83     if (!bundleMgr) {
84         HILOG_ERROR("get bundleMgr failed");
85         return ERR_INVALID_VALUE;
86     }
87     ErrCode ret = bundleMgr->GetBundleInfoV9(bundleName, flags, bundleInfo, userId);
88     if (ret != ERR_OK) {
89         HILOG_ERROR("get bundleInfo failed");
90         return ERR_INVALID_VALUE;
91     }
92     return ERR_OK;
93 }
94 
GetBundleNameByUid(const int uid,std::string & bundleName)95 bool AccessibilityResourceBundleManager::GetBundleNameByUid(const int uid, std::string &bundleName)
96 {
97     bool ret = true;
98     std::lock_guard<ffrt::mutex> lock(bundleMutex_);
99     sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleMgrProxy();
100     do {
101         if (bundleMgr == nullptr) {
102             ret = false;
103             break;
104         }
105         ret = bundleMgr->GetBundleNameForUid(uid, bundleName);
106     } while (0);
107     if (ret == false) {
108         HILOG_ERROR("GetBundleNameByUid failed");
109         return false;
110     }
111     return ret;
112 }
113 
GetUidByBundleName(const std::string & bundleName,const std::string & abilityName,const int userId)114 int AccessibilityResourceBundleManager::GetUidByBundleName(const std::string &bundleName,
115     const std::string &abilityName, const int userId)
116 {
117     int result = -1;
118     bool ret = true;
119     std::lock_guard<ffrt::mutex> lock(bundleMutex_);
120     sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleMgrProxy();
121     do {
122         if (bundleMgr == nullptr) {
123             ret = false;
124             break;
125         }
126         result = bundleMgr->GetUidByBundleName(bundleName, userId);
127         if (result == -1) {
128             ret = false;
129             break;
130         }
131     } while (0);
132     if (ret == false) {
133         Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
134             A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
135         HILOG_ERROR("GetUidByBundleName failed");
136         return ERR_INVALID_VALUE;
137     }
138     return result;
139 }
140 
QueryExtensionAbilityInfos(const AppExecFwk::ExtensionAbilityType & extensionType,const int32_t & userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)141 bool AccessibilityResourceBundleManager::QueryExtensionAbilityInfos(
142     const AppExecFwk::ExtensionAbilityType &extensionType, const int32_t &userId,
143     std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
144 {
145     bool result = true;
146     bool ret = true;
147     std::lock_guard<ffrt::mutex> lock(bundleMutex_);
148     sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleMgrProxy();
149     do {
150         if (bundleMgr == nullptr) {
151             ret = false;
152             break;
153         }
154         result = bundleMgr->QueryExtensionAbilityInfos(extensionType, userId, extensionInfos);
155         if (result == false) {
156             ret = false;
157             break;
158         }
159     } while (0);
160     if (ret == false) {
161         Utils::RecordUnavailableEvent(A11yUnavailableEvent::QUERY_EVENT,
162             A11yError::ERROR_QUERY_PACKAGE_INFO_FAILED);
163         HILOG_ERROR("QueryExtensionAbilityInfos failed");
164         return false;
165     }
166     return result;
167 }
168 
OnRemoteDied(const wptr<IRemoteObject> & remote)169 void AccessibilityResourceBundleManager::BundleManagerDeathRecipient::OnRemoteDied(
170     const wptr<IRemoteObject> &remote)
171 {
172     Singleton<AccessibilityResourceBundleManager>::GetInstance().OnBundleManagerDied(remote);
173 }
174 
OnBundleManagerDied(const wptr<IRemoteObject> & remote)175 void AccessibilityResourceBundleManager::OnBundleManagerDied(const wptr<IRemoteObject> &remote)
176 {
177     HILOG_INFO("OnBundleManagerDied ");
178     if (bundleManager_->AsObject() == nullptr) {
179         HILOG_ERROR("bundleManager_ is nullptr");
180         return;
181     }
182     bundleManager_->AsObject()->RemoveDeathRecipient(bundleManagerDeathRecipient_);
183     bundleManager_ = nullptr;
184 }
185 } // namespace Accessibility
186 } // namespace OHOS