1 /*
2 * Copyright (c) 2021 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 #include "ability_manager_interface.h"
18 #include "appexecfwk_errors.h"
19 #include "hilog_wrapper.h"
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
FormBmsHelper()27 FormBmsHelper::FormBmsHelper()
28 {}
29
~FormBmsHelper()30 FormBmsHelper::~FormBmsHelper()
31 {}
32
33 /**
34 * @brief Acquire a bundle manager, if it not existed.
35 * @return returns the bundle manager ipc object, or nullptr for failed.
36 */
GetBundleMgr()37 sptr<IBundleMgr> FormBmsHelper::GetBundleMgr()
38 {
39 HILOG_INFO("%{public}s called.", __func__);
40
41 if (iBundleMgr_ == nullptr) {
42 sptr<ISystemAbilityManager> systemAbilityManager =
43 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44 auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
45 if (remoteObject == nullptr) {
46 HILOG_ERROR("%{public}s error, failed to get bundle manager service.", __func__);
47 return nullptr;
48 }
49
50 iBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
51 if (iBundleMgr_ == nullptr) {
52 HILOG_ERROR("%{public}s error, failed to get bundle manager service", __func__);
53 return nullptr;
54 }
55 }
56 return iBundleMgr_;
57 }
58
59 /**
60 * @brief Add the bundle manager instance for debug.
61 * @param bundleManager the bundle manager ipc object.
62 */
SetBundleManager(const sptr<IBundleMgr> & bundleManager)63 void FormBmsHelper::SetBundleManager(const sptr<IBundleMgr> &bundleManager)
64 {
65 HILOG_INFO("%{public}s called.", __func__);
66
67 iBundleMgr_ = bundleManager;
68 }
69 /**
70 * @brief Notify module removable.
71 * @param bundleName Provider ability bundleName.
72 * @param moduleName Provider ability moduleName.
73 */
NotifyModuleRemovable(const std::string & bundleName,const std::string & moduleName)74 void FormBmsHelper::NotifyModuleRemovable(const std::string &bundleName, const std::string &moduleName)
75 {
76 HILOG_INFO("%{public}s, bundleName:%{public}s, moduleName:%{public}s",
77 __func__, bundleName.c_str(), moduleName.c_str());
78 if (bundleName.empty() || moduleName.empty()) {
79 return;
80 }
81
82 std::string key = GenerateModuleKey(bundleName, moduleName);
83 HILOG_INFO("%{public}s, begin to notify %{public}s removable", __func__, key.c_str());
84 sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
85 if (iBundleMgr == nullptr) {
86 HILOG_ERROR("%{public}s, failed to get IBundleMgr.", __func__);
87 return;
88 }
89
90 std::string originId = IPCSkeleton::ResetCallingIdentity();
91
92 IPCSkeleton::SetCallingIdentity(originId);
93 }
94 /**
95 * @brief Notify module not removable.
96 * @param bundleName Provider ability bundleName.
97 * @param moduleName Provider ability moduleName.
98 */
NotifyModuleNotRemovable(const std::string & bundleName,const std::string & moduleName) const99 void FormBmsHelper::NotifyModuleNotRemovable(const std::string &bundleName, const std::string &moduleName) const
100 {
101 std::string key = GenerateModuleKey(bundleName, moduleName);
102 HILOG_INFO("%{public}s, begin to notify %{public}s not removable", __func__, key.c_str());
103 }
104
GenerateModuleKey(const std::string & bundleName,const std::string & moduleName) const105 std::string FormBmsHelper::GenerateModuleKey(const std::string &bundleName, const std::string &moduleName) const
106 {
107 return bundleName + "#" + moduleName;
108 }
109 } // namespace AppExecFwk
110 } // namespace OHOS
111