• 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 
GetUidByBundleName(const std::string & bundleName,const std::string & abilityName,const int userId)95 int AccessibilityResourceBundleManager::GetUidByBundleName(const std::string &bundleName,
96     const std::string &abilityName, const int userId)
97 {
98     int result = -1;
99     bool ret = true;
100     std::lock_guard<ffrt::mutex> lock(bundleMutex_);
101     sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleMgrProxy();
102     result = bundleMgr->GetUidByBundleName(bundleName, userId);
103     do {
104         if (bundleMgr == nullptr) {
105             ret = false;
106             break;
107         }
108         if (result == -1) {
109             ret = false;
110             break;
111         }
112     } while (0);
113     if (ret == false) {
114         Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
115             A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
116         HILOG_ERROR("get bundleMgr failed");
117         return ERR_INVALID_VALUE;
118     }
119     return result;
120 }
121 
QueryExtensionAbilityInfos(const AppExecFwk::ExtensionAbilityType & extensionType,const int32_t & userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)122 bool AccessibilityResourceBundleManager::QueryExtensionAbilityInfos(
123     const AppExecFwk::ExtensionAbilityType &extensionType, const int32_t &userId,
124     std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
125 {
126     bool result = true;
127     bool ret = true;
128     std::lock_guard<ffrt::mutex> lock(bundleMutex_);
129     sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleMgrProxy();
130     result = bundleMgr->QueryExtensionAbilityInfos(extensionType, userId, extensionInfos);
131     do {
132         if (bundleMgr == nullptr) {
133             ret = false;
134             break;
135         }
136         if (result == false) {
137             ret = false;
138             break;
139         }
140     } while(0);
141     if (ret == false) {
142         Utils::RecordUnavailableEvent(A11yUnavailableEvent::QUERY_EVENT,
143             A11yError::ERROR_QUERY_PACKAGE_INFO_FAILED);
144         HILOG_ERROR("get bundleMgr failed");
145         return false;
146     }
147     return result;
148 }
149 
OnRemoteDied(const wptr<IRemoteObject> & remote)150 void AccessibilityResourceBundleManager::BundleManagerDeathRecipient::OnRemoteDied(
151     const wptr<IRemoteObject> &remote)
152 {
153     Singleton<AccessibilityResourceBundleManager>::GetInstance().OnBundleManagerDied(remote);
154 }
155 
OnBundleManagerDied(const wptr<IRemoteObject> & remote)156 void AccessibilityResourceBundleManager::OnBundleManagerDied(const wptr<IRemoteObject> &remote)
157 {
158     HILOG_INFO("OnBundleManagerDied ");
159     if (!bundleManager_) {
160         HILOG_ERROR("bundleManager_ is nullptr");
161         return;
162     }
163     bundleManager_->AsObject()->RemoveDeathRecipient(bundleManagerDeathRecipient_);
164     bundleManager_ = nullptr;
165 }
166 } // namespace Accessibility
167 } // namespace OHOS