• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "bundle_resource_helper.h"
16 
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19 
20 namespace OHOS {
21 namespace Notification {
22 
BundleResourceHelper()23 BundleResourceHelper::BundleResourceHelper()
24 {
25     deathRecipient_ = new (std::nothrow)
26         BundleDeathRecipient(std::bind(&BundleResourceHelper::OnRemoteDied, this, std::placeholders::_1));
27     if (deathRecipient_ == nullptr) {
28         ANS_LOGE("Failed to create BundleDeathRecipient instance");
29     }
30 }
31 
~BundleResourceHelper()32 BundleResourceHelper::~BundleResourceHelper()
33 {
34     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
35     Disconnect();
36 }
37 
Connect()38 void BundleResourceHelper::Connect()
39 {
40     if (bundleMgr_ != nullptr) {
41         return;
42     }
43 
44     sptr<ISystemAbilityManager> systemAbilityManager =
45         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46     if (systemAbilityManager == nullptr) {
47         return;
48     }
49 
50     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
51     if (remoteObject == nullptr) {
52         return;
53     }
54 
55     bundleMgr_ = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
56     if (bundleMgr_ != nullptr) {
57         bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_);
58     }
59 }
60 
Disconnect()61 void BundleResourceHelper::Disconnect()
62 {
63     if (bundleMgr_ != nullptr) {
64         bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
65         bundleMgr_ = nullptr;
66     }
67 }
68 
OnRemoteDied(const wptr<IRemoteObject> & object)69 void BundleResourceHelper::OnRemoteDied(const wptr<IRemoteObject> &object)
70 {
71     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
72     Disconnect();
73 }
74 
GetBundleInfo(const std::string & bundleName,AppExecFwk::BundleResourceInfo & bundleResourceInfo,const int32_t appIndex)75 ErrCode BundleResourceHelper::GetBundleInfo(const std::string &bundleName,
76     AppExecFwk::BundleResourceInfo &bundleResourceInfo, const int32_t appIndex)
77 {
78     ErrCode result = 0;
79     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
80     Connect();
81     if (bundleMgr_ == nullptr) {
82         ANS_LOGE("GetBundleInfo bundle proxy failed.");
83         return -1;
84     }
85     sptr<AppExecFwk::IBundleResource> bundleResourceProxy = bundleMgr_->GetBundleResourceProxy();
86     if (!bundleResourceProxy) {
87         ANS_LOGE("GetBundleInfo, get bundle resource proxy failed.");
88         return -1;
89     }
90 
91     std::string identity = IPCSkeleton::ResetCallingIdentity();
92     int32_t flag = static_cast<int32_t>(AppExecFwk::ResourceFlag::GET_RESOURCE_INFO_ALL);
93     result = bundleResourceProxy->GetBundleResourceInfo(bundleName, flag, bundleResourceInfo, appIndex);
94     IPCSkeleton::SetCallingIdentity(identity);
95     return result;
96 }
97 
GetAllBundleInfos(int32_t flags,std::vector<AppExecFwk::BundleInfo> & bundleInfos,int32_t userId)98 ErrCode BundleResourceHelper::GetAllBundleInfos(int32_t flags, std::vector<AppExecFwk::BundleInfo> &bundleInfos,
99     int32_t userId)
100 {
101     ErrCode result = 0;
102     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
103     Connect();
104     if (bundleMgr_ == nullptr) {
105         ANS_LOGE("GetBundleInfo bundle proxy failed.");
106         return -1;
107     }
108 
109     std::string identity = IPCSkeleton::ResetCallingIdentity();
110     result = bundleMgr_->GetBundleInfosV9(flags, bundleInfos, userId);
111     IPCSkeleton::SetCallingIdentity(identity);
112     return result;
113 }
114 
GetAllInstalledBundles(std::vector<std::pair<std::string,std::string>> & bundlesName,int32_t userId)115 ErrCode BundleResourceHelper::GetAllInstalledBundles(std::vector<std::pair<std::string, std::string>>&bundlesName,
116     int32_t userId)
117 {
118     std::vector<AppExecFwk::BundleInfo> bundleInfos;
119     int32_t flags = static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
120     ErrCode result = GetAllBundleInfos(flags, bundleInfos, userId);
121     if (result != ERR_OK) {
122         ANS_LOGE("Get installed bundle failed %{public}d.", result);
123         return result;
124     }
125 
126     for (auto& bundle : bundleInfos) {
127         if (bundle.applicationInfo.bundleType != AppExecFwk::BundleType::APP) {
128             ANS_LOGD("Get not app %{public}s", bundle.applicationInfo.bundleName.c_str());
129             continue;
130         }
131         if (bundle.applicationInfo.isSystemApp) {
132             continue;
133         }
134         AppExecFwk::BundleResourceInfo resourceInfo;
135         result = GetBundleInfo(bundle.applicationInfo.bundleName, resourceInfo);
136         if (result != ERR_OK) {
137             ANS_LOGW("Get failed %{public}s %{public}d", bundle.applicationInfo.bundleName.c_str(), result);
138             continue;
139         }
140         ANS_LOGD("Get bundle app %{public}s %{public}s", bundle.applicationInfo.bundleName.c_str(),
141             resourceInfo.label.c_str());
142         bundlesName.emplace_back(std::make_pair(bundle.applicationInfo.bundleName,
143             resourceInfo.label));
144     }
145 
146     ANS_LOGI("Get installed bundle size %{public}zu.", bundlesName.size());
147     return ERR_OK;
148 }
149 
GetApplicationInfo(const std::string & appName,int32_t flags,int32_t userId,AppExecFwk::ApplicationInfo & appInfo)150 ErrCode BundleResourceHelper::GetApplicationInfo(const std::string &appName, int32_t flags, int32_t userId,
151     AppExecFwk::ApplicationInfo &appInfo)
152 {
153     ErrCode result = 0;
154     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
155     Connect();
156     if (bundleMgr_ == nullptr) {
157         ANS_LOGE("GetBundleInfo bundle proxy failed.");
158         return -1;
159     }
160 
161     std::string identity = IPCSkeleton::ResetCallingIdentity();
162     result = bundleMgr_->GetApplicationInfoV9(appName, flags, userId, appInfo);
163     IPCSkeleton::SetCallingIdentity(identity);
164     return result;
165 }
166 
CheckSystemApp(const std::string & bundleName,int32_t userId)167 bool BundleResourceHelper::CheckSystemApp(const std::string& bundleName, int32_t userId)
168 {
169     AppExecFwk::BundleInfo bundleInfo;
170     if (GetBundleInfoV9(bundleName, userId, bundleInfo) != ERR_OK) {
171         return false;
172     }
173     ANS_LOGI("Get installed bundle %{public}s %{public}d.", bundleName.c_str(),
174         bundleInfo.applicationInfo.isSystemApp);
175     return bundleInfo.applicationInfo.isSystemApp;
176 }
177 
GetBundleInfoV9(const std::string & bundleName,int32_t userId,AppExecFwk::BundleInfo & bundleInfo)178 ErrCode BundleResourceHelper::GetBundleInfoV9(const std::string& bundleName, int32_t userId,
179     AppExecFwk::BundleInfo& bundleInfo)
180 {
181     int32_t flags = static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
182     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
183     Connect();
184     if (bundleMgr_ == nullptr) {
185         return false;
186     }
187     std::string identity = IPCSkeleton::ResetCallingIdentity();
188     int32_t result = bundleMgr_->GetBundleInfoV9(bundleName, flags, bundleInfo, userId);
189     IPCSkeleton::SetCallingIdentity(identity);
190     if (result != ERR_OK) {
191         ANS_LOGW("Get installed bundle %{public}s failed.", bundleName.c_str());
192         return result;
193     }
194     return ERR_OK;
195 }
196 }
197 }
198