1 /*
2 * Copyright (c) 2021-2023 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 "form_bms_helper.h"
17
18 #include "ability_manager_interface.h"
19 #include "fms_log_wrapper.h"
20 #include "form_mgr_errors.h"
21 #include "if_system_ability_manager.h"
22 #include "in_process_call_wrapper.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
FormBmsHelper()29 FormBmsHelper::FormBmsHelper()
30 {
31 HILOG_INFO("called");
32 }
33
~FormBmsHelper()34 FormBmsHelper::~FormBmsHelper()
35 {
36 HILOG_INFO("called");
37 }
38
GetBundleMgr()39 sptr<IBundleMgr> FormBmsHelper::GetBundleMgr()
40 {
41 HILOG_DEBUG("called.");
42 if (iBundleMgr_ == nullptr) {
43 std::lock_guard<std::mutex> lock(ibundleMutex_);
44 if (iBundleMgr_ == nullptr) {
45 sptr<ISystemAbilityManager> systemAbilityManager =
46 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
47 auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
48 if (remoteObject == nullptr) {
49 HILOG_ERROR("error, failed to get bundle manager service.");
50 return nullptr;
51 }
52
53 iBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
54 if (iBundleMgr_ == nullptr) {
55 HILOG_ERROR("error, failed to get bundle manager service");
56 return nullptr;
57 }
58 }
59 }
60
61 return iBundleMgr_;
62 }
63
GetBundleInstaller()64 sptr<IBundleInstaller> FormBmsHelper::GetBundleInstaller()
65 {
66 HILOG_DEBUG("called.");
67 if (bundleInstallerProxy_ == nullptr) {
68 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
69 if (iBundleMgr != nullptr) {
70 bundleInstallerProxy_ = iBundleMgr->GetBundleInstaller();
71 }
72 }
73 return bundleInstallerProxy_;
74 }
75
76
SetBundleManager(const sptr<IBundleMgr> & bundleManager)77 void FormBmsHelper::SetBundleManager(const sptr<IBundleMgr> &bundleManager)
78 {
79 HILOG_DEBUG("SetBundleManager called.");
80 iBundleMgr_ = bundleManager;
81 }
82
83 /**
84 * @brief Notify module removable.
85 * @param bundleName Provider ability bundleName.
86 * @param moduleName Provider ability moduleName.
87 */
NotifyModuleRemovable(const std::string & bundleName,const std::string & moduleName)88 void FormBmsHelper::NotifyModuleRemovable(const std::string &bundleName, const std::string &moduleName)
89 {
90 HILOG_DEBUG("notify module removable, bundleName:%{public}s, moduleName:%{public}s",
91 bundleName.c_str(), moduleName.c_str());
92 if (bundleName.empty() || moduleName.empty()) {
93 return;
94 }
95
96 std::string key = GenerateModuleKey(bundleName, moduleName);
97 HILOG_DEBUG("begin to notify %{public}s removable", key.c_str());
98 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
99 if (iBundleMgr == nullptr) {
100 HILOG_ERROR("failed to get IBundleMgr.");
101 return;
102 }
103
104 std::string originId = IPCSkeleton::ResetCallingIdentity();
105
106 IPCSkeleton::SetCallingIdentity(originId);
107 }
108 /**
109 * @brief Notify module not removable.
110 * @param bundleName Provider ability bundleName.
111 * @param moduleName Provider ability moduleName.
112 */
NotifyModuleNotRemovable(const std::string & bundleName,const std::string & moduleName)113 void FormBmsHelper::NotifyModuleNotRemovable(const std::string &bundleName, const std::string &moduleName)
114 {
115 HILOG_INFO("bundleName:%{public}s, moduleName:%{public}s",
116 bundleName.c_str(), moduleName.c_str());
117 if (bundleName.empty() || moduleName.empty()) {
118 return;
119 }
120 std::string key = GenerateModuleKey(bundleName, moduleName);
121 HILOG_DEBUG("begin to notify %{public}s not removable", key.c_str());
122 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
123 if (iBundleMgr == nullptr) {
124 HILOG_ERROR("failed to get IBundleMgr.");
125 return;
126 }
127
128 if (!IN_PROCESS_CALL(iBundleMgr->SetModuleRemovable(bundleName, moduleName, false))) {
129 HILOG_ERROR("set not removable failed.");
130 return;
131 }
132 return;
133 }
134
GenerateModuleKey(const std::string & bundleName,const std::string & moduleName) const135 std::string FormBmsHelper::GenerateModuleKey(const std::string &bundleName, const std::string &moduleName) const
136 {
137 return bundleName + "#" + moduleName;
138 }
139
GetBundlePackInfo(const std::string & bundleName,const int32_t userId,BundlePackInfo & bundlePackInfo)140 bool FormBmsHelper::GetBundlePackInfo(const std::string &bundleName, const int32_t userId,
141 BundlePackInfo &bundlePackInfo)
142 {
143 HILOG_INFO("bundleName:%{public}s", bundleName.c_str());
144 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
145 if (iBundleMgr == nullptr) {
146 HILOG_ERROR("failed to get IBundleMgr.");
147 return false;
148 }
149
150 if (IN_PROCESS_CALL(iBundleMgr->GetBundlePackInfo(bundleName, GET_PACK_INFO_ALL, bundlePackInfo, userId))
151 != ERR_OK) {
152 HILOG_ERROR("error, failed to get bundle pack info.");
153 return false;
154 }
155
156 HILOG_DEBUG("get bundle pack info success");
157 return true;
158 }
159
GetAbilityInfo(const AAFwk::Want & want,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)160 bool FormBmsHelper::GetAbilityInfo(const AAFwk::Want &want, int32_t userId, AbilityInfo &abilityInfo,
161 ExtensionAbilityInfo &extensionInfo)
162 {
163 HILOG_DEBUG("GetAbilityInfo called.");
164 ElementName element = want.GetElement();
165 std::string bundleName = element.GetBundleName();
166 std::string abilityName = element.GetAbilityName();
167 if (bundleName.empty() || abilityName.empty()) {
168 HILOG_ERROR("invalid want in explicit query ability info");
169 return false;
170 }
171
172 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
173 if (iBundleMgr == nullptr) {
174 HILOG_ERROR("iBundleMgr is nullptr");
175 return false;
176 }
177 IN_PROCESS_CALL(iBundleMgr->QueryAbilityInfo(want, AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT,
178 userId, abilityInfo));
179 if (abilityInfo.name.empty() || abilityInfo.bundleName.empty()) {
180 HILOG_INFO("get ability info empty, try to get extension info.");
181 std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
182 IN_PROCESS_CALL(iBundleMgr->QueryExtensionAbilityInfos(want, AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT,
183 userId, extensionInfos));
184 if (extensionInfos.empty()) {
185 HILOG_ERROR("get extension info failed.");
186 return false;
187 }
188 extensionInfo = extensionInfos.front();
189 if (extensionInfo.bundleName.empty() || extensionInfo.name.empty()) {
190 HILOG_ERROR("get extension info empty.");
191 return false;
192 }
193 }
194 return true;
195 }
196
GetAbilityInfoByAction(const std::string & action,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionAbilityInfo)197 bool FormBmsHelper::GetAbilityInfoByAction(const std::string &action, int32_t userId,
198 AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionAbilityInfo)
199 {
200 HILOG_DEBUG("called.");
201 if (action.empty()) {
202 HILOG_ERROR("input parasm error.");
203 return false;
204 }
205
206 Want wantAction;
207 wantAction.SetAction(action);
208 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
209 if (iBundleMgr == nullptr) {
210 HILOG_ERROR("iBundleMgr is nullptr");
211 return false;
212 }
213
214 return (IN_PROCESS_CALL(iBundleMgr->ImplicitQueryInfoByPriority(wantAction,
215 AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT, userId, abilityInfo, extensionAbilityInfo)));
216 }
217
GetBundleInfo(const std::string & bundleName,int32_t userId,BundleInfo & bundleInfo)218 bool FormBmsHelper::GetBundleInfo(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)
219 {
220 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
221 if (iBundleMgr == nullptr) {
222 HILOG_ERROR("iBundleMgr is nullptr");
223 return false;
224 }
225
226 int32_t flags = BundleFlag::GET_BUNDLE_WITH_ABILITIES;
227 return (IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId)));
228 }
229
GetBundleInfoWithPermission(const std::string & bundleName,int32_t userId,BundleInfo & bundleInfo)230 bool FormBmsHelper::GetBundleInfoWithPermission(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)
231 {
232 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
233 if (iBundleMgr == nullptr) {
234 HILOG_ERROR("iBundleMgr is nullptr");
235 return false;
236 }
237
238 int32_t flags = BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION;
239 return (IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId)));
240 }
241
GetCallerBundleName(std::string & callerBundleName)242 int32_t FormBmsHelper::GetCallerBundleName(std::string &callerBundleName)
243 {
244 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
245 if (iBundleMgr == nullptr) {
246 HILOG_ERROR("failed to get IBundleMgr.");
247 return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
248 }
249 auto callingUid = IPCSkeleton::GetCallingUid();
250 if (IN_PROCESS_CALL(iBundleMgr->GetNameForUid(callingUid, callerBundleName)) != ERR_OK) {
251 HILOG_ERROR("failed to get form config info.");
252 return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
253 }
254 return ERR_OK;
255 }
256
GetBundleNameByUid(const int32_t uid,std::string & bundleName)257 int32_t FormBmsHelper::GetBundleNameByUid(const int32_t uid, std::string &bundleName)
258 {
259 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
260 if (iBundleMgr == nullptr) {
261 HILOG_ERROR("failed to get IBundleMgr.");
262 return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
263 }
264
265 if (IN_PROCESS_CALL(iBundleMgr->GetNameForUid(uid, bundleName)) != ERR_OK) {
266 HILOG_ERROR("failed to get bundle name by uid.");
267 return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
268 }
269 return ERR_OK;
270 }
271
GetUidByBundleName(const std::string & bundleName,const int32_t userId)272 int32_t FormBmsHelper::GetUidByBundleName(const std::string &bundleName, const int32_t userId)
273 {
274 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
275 if (iBundleMgr == nullptr) {
276 HILOG_ERROR("failed to get IBundleMgr.");
277 return INVALID_UID;
278 }
279 return IN_PROCESS_CALL(iBundleMgr->GetUidByBundleName(bundleName, userId));
280 }
281
GetCompileMode(const std::string & bundleName,const std::string & moduleName,int32_t userId,int32_t & compileMode)282 bool FormBmsHelper::GetCompileMode(const std::string &bundleName, const std::string &moduleName,
283 int32_t userId, int32_t &compileMode)
284 {
285 HILOG_DEBUG("called.");
286 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
287 if (iBundleMgr == nullptr) {
288 HILOG_ERROR("iBundleMgr is nullptr");
289 return false;
290 }
291 int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
292 BundleInfo bundleInfo;
293 if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
294 HILOG_ERROR("Get bundle info failed.");
295 return false;
296 }
297
298 for (const auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
299 if (!moduleName.empty() && hapModuleInfo.moduleName != moduleName) {
300 continue;
301 }
302 compileMode = static_cast<int32_t>(hapModuleInfo.compileMode);
303 return true;
304 }
305
306 HILOG_ERROR("Get compile mode failed.");
307 return false;
308 }
309
GetCompatibleVersion(const std::string & bundleName,int32_t userId,int32_t & compatibleVersion)310 bool FormBmsHelper::GetCompatibleVersion(const std::string& bundleName, int32_t userId, int32_t& compatibleVersion)
311 {
312 HILOG_DEBUG("called.");
313 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
314 if (iBundleMgr == nullptr) {
315 HILOG_ERROR("iBundleMgr is nullptr");
316 return false;
317 }
318 int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
319 BundleInfo bundleInfo;
320 if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
321 HILOG_ERROR("Get bundle info failed.");
322 return false;
323 }
324
325 compatibleVersion = static_cast<int32_t>(bundleInfo.compatibleVersion);
326 return true;
327 }
328
GetProxyDataInfos(const std::string & bundleName,const std::string & moduleName,int32_t userId,std::vector<ProxyData> & proxyData)329 ErrCode FormBmsHelper::GetProxyDataInfos(const std::string &bundleName, const std::string &moduleName,
330 int32_t userId, std::vector<ProxyData> &proxyData)
331 {
332 HILOG_DEBUG("called.");
333 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
334 if (iBundleMgr == nullptr) {
335 HILOG_ERROR("iBundleMgr is nullptr");
336 return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
337 }
338
339 return IN_PROCESS_CALL(iBundleMgr->GetProxyDataInfos(bundleName, moduleName, proxyData, userId));
340 }
341
GetAllProxyDataInfos(int32_t userId,std::vector<ProxyData> & proxyData)342 ErrCode FormBmsHelper::GetAllProxyDataInfos(int32_t userId, std::vector<ProxyData> &proxyData)
343 {
344 HILOG_DEBUG("called.");
345 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
346 if (iBundleMgr == nullptr) {
347 HILOG_ERROR("iBundleMgr is nullptr");
348 return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
349 }
350
351 return IN_PROCESS_CALL(iBundleMgr->GetAllProxyDataInfos(proxyData, userId));
352 }
353
GetApplicationInfo(const std::string & bundleName,int32_t userId,ApplicationInfo & appInfo)354 ErrCode FormBmsHelper::GetApplicationInfo(const std::string &bundleName, int32_t userId, ApplicationInfo &appInfo)
355 {
356 HILOG_DEBUG("called.");
357 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
358 if (iBundleMgr == nullptr) {
359 HILOG_ERROR("iBundleMgr is nullptr");
360 return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
361 }
362
363 return IN_PROCESS_CALL(iBundleMgr->GetApplicationInfoV9(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
364 userId, appInfo));
365 }
366
RegisterBundleEventCallback()367 ErrCode FormBmsHelper::RegisterBundleEventCallback()
368 {
369 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
370 if (iBundleMgr == nullptr) {
371 return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
372 }
373 formBundleEventCallback_ = new (std::nothrow) FormBundleEventCallback();
374 if (formBundleEventCallback_ == nullptr) {
375 HILOG_ERROR("fail, allocate formBundleEventCallback_ failed!");
376 return ERR_APPEXECFWK_FORM_COMMON_CODE;
377 }
378 if (!iBundleMgr->RegisterBundleEventCallback(formBundleEventCallback_)) {
379 HILOG_ERROR("fail, RegisterBundleEventCallback failed!");
380 return ERR_APPEXECFWK_FORM_COMMON_CODE;
381 }
382 return ERR_OK;
383 }
384
UnregisterBundleEventCallback()385 ErrCode FormBmsHelper::UnregisterBundleEventCallback()
386 {
387 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
388 if (iBundleMgr == nullptr) {
389 return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
390 }
391 if (!iBundleMgr->UnregisterBundleEventCallback(formBundleEventCallback_)) {
392 HILOG_ERROR("fail, RegisterBundleEventCallback failed!");
393 return ERR_APPEXECFWK_FORM_COMMON_CODE;
394 }
395 formBundleEventCallback_ = nullptr;
396 return ERR_OK;
397 }
398 } // namespace AppExecFwk
399 } // namespace OHOS
400