• 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 #include "resource_helper.h"
16 #include "app_log_wrapper.h"
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19 #include "bundle_errors.h"
20 #include "bundle_mgr_interface.h"
21 #include "bundle_mgr_proxy.h"
22 #include "common_func.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 
27 sptr<IBundleResource> ResourceHelper::bundleResourceMgr_ = nullptr;
28 std::mutex ResourceHelper::bundleResourceMutex_;
29 sptr<IRemoteObject::DeathRecipient> ResourceHelper::deathRecipient_(sptr<BundleResourceMgrDeathRecipient>::MakeSptr());
30 
OnRemoteDied(const wptr<IRemoteObject> & remote)31 void ResourceHelper::BundleResourceMgrDeathRecipient::OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject>& remote)
32 {
33     APP_LOGI("BundleManagerService dead");
34     std::lock_guard<std::mutex> lock(bundleResourceMutex_);
35     bundleResourceMgr_ = nullptr;
36 };
37 
GetBundleResourceMgr()38 sptr<IBundleResource> ResourceHelper::GetBundleResourceMgr()
39 {
40     std::lock_guard<std::mutex> lock(bundleResourceMutex_);
41     if (bundleResourceMgr_ == nullptr) {
42         auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
43         if (systemAbilityManager == nullptr) {
44             APP_LOGE("systemAbilityManager is null");
45             return nullptr;
46         }
47         auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
48         if (bundleMgrSa == nullptr) {
49             APP_LOGE("bundleMgrSa is null");
50             return nullptr;
51         }
52         auto bundleMgr = OHOS::iface_cast<IBundleMgr>(bundleMgrSa);
53         if (bundleMgr == nullptr) {
54             APP_LOGE("iface_cast failed");
55             return nullptr;
56         }
57         bundleMgr->AsObject()->AddDeathRecipient(deathRecipient_);
58         bundleResourceMgr_ = bundleMgr->GetBundleResourceProxy();
59     }
60     return bundleResourceMgr_;
61 }
62 
InnerGetBundleResourceInfo(const std::string & bundleName,uint32_t flags,int32_t appIndex,BundleResourceInfo & resourceInfo)63 ErrCode ResourceHelper::InnerGetBundleResourceInfo(
64     const std::string &bundleName, uint32_t flags, int32_t appIndex, BundleResourceInfo &resourceInfo)
65 {
66     APP_LOGD("start");
67     auto bundleResourceProxy = ResourceHelper::GetBundleResourceMgr();
68     if (bundleResourceProxy == nullptr) {
69         APP_LOGE("bundleResourceProxy is null");
70         return ERROR_BUNDLE_SERVICE_EXCEPTION;
71     }
72     ErrCode ret = bundleResourceProxy->GetBundleResourceInfo(bundleName, flags, resourceInfo, appIndex);
73     if (ret != ERR_OK) {
74         APP_LOGE("failed, bundleName is %{public}s, errCode: %{public}d", bundleName.c_str(), ret);
75     }
76     return CommonFunc::ConvertErrCode(ret);
77 }
78 
InnerGetLauncherAbilityResourceInfo(const std::string & bundleName,uint32_t flags,int32_t appIndex,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfo)79 ErrCode ResourceHelper::InnerGetLauncherAbilityResourceInfo(
80     const std::string &bundleName, uint32_t flags, int32_t appIndex,
81     std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfo)
82 {
83     APP_LOGD("start");
84     auto bundleResourceProxy = ResourceHelper::GetBundleResourceMgr();
85     if (bundleResourceProxy == nullptr) {
86         APP_LOGE("bundleResourceProxy is null");
87         return ERROR_BUNDLE_SERVICE_EXCEPTION;
88     }
89     ErrCode ret = bundleResourceProxy->GetLauncherAbilityResourceInfo(bundleName,
90         flags, launcherAbilityResourceInfo, appIndex);
91     if (ret != ERR_OK) {
92         APP_LOGE("failed, bundleName is %{public}s, errCode: %{public}d", bundleName.c_str(), ret);
93     }
94     return CommonFunc::ConvertErrCode(ret);
95 }
96 
InnerGetAllBundleResourceInfo(uint32_t flags,std::vector<BundleResourceInfo> & bundleResourceInfos)97 ErrCode ResourceHelper::InnerGetAllBundleResourceInfo(uint32_t flags,
98     std::vector<BundleResourceInfo> &bundleResourceInfos)
99 {
100     auto bundleResourceProxy = ResourceHelper::GetBundleResourceMgr();
101     if (bundleResourceProxy == nullptr) {
102         APP_LOGE("bundleResourceProxy is null");
103         return ERROR_BUNDLE_SERVICE_EXCEPTION;
104     }
105     ErrCode ret = bundleResourceProxy->GetAllBundleResourceInfo(flags, bundleResourceInfos);
106     if (ret != ERR_OK) {
107         APP_LOGE("failed, errCode: %{public}d", ret);
108     }
109     return CommonFunc::ConvertErrCode(ret);
110 }
111 
InnerGetAllLauncherAbilityResourceInfo(uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfos)112 ErrCode ResourceHelper::InnerGetAllLauncherAbilityResourceInfo(uint32_t flags,
113     std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfos)
114 {
115     auto bundleResourceProxy = ResourceHelper::GetBundleResourceMgr();
116     if (bundleResourceProxy == nullptr) {
117         APP_LOGE("bundleResourceProxy is null");
118         return ERROR_BUNDLE_SERVICE_EXCEPTION;
119     }
120     ErrCode ret = bundleResourceProxy->GetAllLauncherAbilityResourceInfo(flags, launcherAbilityResourceInfos);
121     if (ret != ERR_OK) {
122         APP_LOGE("failed, errCode: %{public}d", ret);
123     }
124     return CommonFunc::ConvertErrCode(ret);
125 }
126 
InnerGetExtensionAbilityResourceInfo(const std::string & bundleName,ExtensionAbilityType extensionAbilityType,uint32_t flags,int32_t appIndex,std::vector<LauncherAbilityResourceInfo> & extensionAbilityResourceInfos)127 ErrCode ResourceHelper::InnerGetExtensionAbilityResourceInfo(const std::string& bundleName,
128     ExtensionAbilityType extensionAbilityType, uint32_t flags, int32_t appIndex,
129     std::vector<LauncherAbilityResourceInfo>& extensionAbilityResourceInfos)
130 {
131     APP_LOGD("InnerGetExtensionAbilityResourceInfo start");
132     auto bundleResourceProxy = ResourceHelper::GetBundleResourceMgr();
133     if (bundleResourceProxy == nullptr) {
134         APP_LOGE("bundleResourceProxy is null");
135         return ERROR_BUNDLE_SERVICE_EXCEPTION;
136     }
137     ErrCode ret = bundleResourceProxy->GetExtensionAbilityResourceInfo(
138         bundleName, extensionAbilityType, flags, extensionAbilityResourceInfos, appIndex);
139     if (ret != ERR_OK) {
140         APP_LOGE("failed, bundleName is %{public}s, errCode: %{public}d", bundleName.c_str(), ret);
141     }
142     return CommonFunc::ConvertErrCode(ret);
143 }
144 } // AppExecFwk
145 } // OHOS