• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "form_mgr_errors.h"
20 #include "hilog_wrapper.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 
~FormBmsHelper()32 FormBmsHelper::~FormBmsHelper()
33 {}
34 
35 /**
36  * @brief Acquire a bundle manager, if it not existed.
37  * @return returns the bundle manager ipc object, or nullptr for failed.
38  */
GetBundleMgr()39 sptr<IBundleMgr> FormBmsHelper::GetBundleMgr()
40 {
41     HILOG_INFO("%{public}s called.", __func__);
42 
43     if (iBundleMgr_ == nullptr) {
44         sptr<ISystemAbilityManager> systemAbilityManager =
45         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46         auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
47         if (remoteObject == nullptr) {
48             HILOG_ERROR("%{public}s error, failed to get bundle manager service.", __func__);
49             return nullptr;
50         }
51 
52         iBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
53         if (iBundleMgr_ == nullptr) {
54             HILOG_ERROR("%{public}s error, failed to get bundle manager service", __func__);
55             return nullptr;
56         }
57     }
58     return iBundleMgr_;
59 }
60 
61 /**
62  * @brief Add the bundle manager instance for debug.
63  * @param bundleManager the bundle manager ipc object.
64  */
SetBundleManager(const sptr<IBundleMgr> & bundleManager)65 void FormBmsHelper::SetBundleManager(const sptr<IBundleMgr> &bundleManager)
66 {
67     HILOG_INFO("%{public}s called.", __func__);
68     iBundleMgr_ = bundleManager;
69 }
70 /**
71  * @brief Notify module removable.
72  * @param bundleName Provider ability bundleName.
73  * @param moduleName Provider ability moduleName.
74  */
NotifyModuleRemovable(const std::string & bundleName,const std::string & moduleName)75 void FormBmsHelper::NotifyModuleRemovable(const std::string &bundleName, const std::string &moduleName)
76 {
77     HILOG_INFO("%{public}s, bundleName:%{public}s, moduleName:%{public}s",
78         __func__, bundleName.c_str(), moduleName.c_str());
79     if (bundleName.empty() || moduleName.empty()) {
80         return;
81     }
82 
83     std::string key = GenerateModuleKey(bundleName, moduleName);
84     HILOG_INFO("%{public}s, begin to notify %{public}s removable", __func__, key.c_str());
85     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
86     if (iBundleMgr == nullptr) {
87         HILOG_ERROR("%{public}s, failed to get IBundleMgr.", __func__);
88         return;
89     }
90 
91     std::string originId = IPCSkeleton::ResetCallingIdentity();
92 
93     IPCSkeleton::SetCallingIdentity(originId);
94 }
95 /**
96  * @brief Notify module not removable.
97  * @param bundleName Provider ability bundleName.
98  * @param moduleName Provider ability moduleName.
99  */
NotifyModuleNotRemovable(const std::string & bundleName,const std::string & moduleName)100 void FormBmsHelper::NotifyModuleNotRemovable(const std::string &bundleName, const std::string &moduleName)
101 {
102     HILOG_INFO("%{public}s, bundleName:%{public}s, moduleName:%{public}s",
103         __func__, bundleName.c_str(), moduleName.c_str());
104     if (bundleName.empty() || moduleName.empty()) {
105         return;
106     }
107     std::string key = GenerateModuleKey(bundleName, moduleName);
108     HILOG_INFO("%{public}s, begin to notify %{public}s not removable", __func__, key.c_str());
109     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
110     if (iBundleMgr == nullptr) {
111         HILOG_ERROR("%{public}s, failed to get IBundleMgr.", __func__);
112         return;
113     }
114 
115     if (!IN_PROCESS_CALL(iBundleMgr->SetModuleRemovable(bundleName, moduleName, false))) {
116         HILOG_ERROR("%{public}s, set not removable failed.", __func__);
117         return;
118     }
119     return;
120 }
121 
GenerateModuleKey(const std::string & bundleName,const std::string & moduleName) const122 std::string FormBmsHelper::GenerateModuleKey(const std::string &bundleName, const std::string &moduleName) const
123 {
124     return bundleName + "#" + moduleName;
125 }
126 
GetBundlePackInfo(const std::string & bundleName,const int32_t userId,BundlePackInfo & bundlePackInfo)127 bool FormBmsHelper::GetBundlePackInfo(const std::string &bundleName, const int32_t userId,
128     BundlePackInfo &bundlePackInfo)
129 {
130     HILOG_INFO("%{public}s, bundleName:%{public}s", __func__, bundleName.c_str());
131     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
132     if (iBundleMgr == nullptr) {
133         HILOG_ERROR("%{public}s, failed to get IBundleMgr.", __func__);
134         return false;
135     }
136 
137     if (IN_PROCESS_CALL(iBundleMgr->GetBundlePackInfo(bundleName, GET_PACK_INFO_ALL, bundlePackInfo, userId))
138         != ERR_OK) {
139         HILOG_ERROR("%{public}s error, failed to get bundle pack info.", __func__);
140         return false;
141     }
142 
143     HILOG_INFO("%{public}s, get bundle pack info success", __func__);
144     return true;
145 }
146 
GetAbilityInfoByAction(const std::string & action,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionAbilityInfo)147 bool FormBmsHelper::GetAbilityInfoByAction(const std::string &action, int32_t userId,
148     AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionAbilityInfo)
149 {
150     HILOG_DEBUG("%{public}s called.", __func__);
151     if (action.empty()) {
152         HILOG_ERROR("input parasm error.");
153         return false;
154     }
155 
156     Want wantAction;
157     wantAction.SetAction(action);
158     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
159     if (iBundleMgr == nullptr) {
160         HILOG_ERROR("iBundleMgr is nullptr");
161         return false;
162     }
163 
164     return (IN_PROCESS_CALL(iBundleMgr->ImplicitQueryInfoByPriority(wantAction,
165         AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT, userId, abilityInfo, extensionAbilityInfo)));
166 }
167 
GetBundleInfo(const std::string & bundleName,int32_t userId,BundleInfo & bundleInfo)168 bool FormBmsHelper::GetBundleInfo(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)
169 {
170     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
171     if (iBundleMgr == nullptr) {
172         HILOG_ERROR("iBundleMgr is nullptr");
173         return false;
174     }
175 
176     int32_t flags = BundleFlag::GET_BUNDLE_WITH_ABILITIES;
177     return (IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId)));
178 }
179 
GetBundleInfoWithPermission(const std::string & bundleName,int32_t userId,BundleInfo & bundleInfo)180 bool FormBmsHelper::GetBundleInfoWithPermission(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)
181 {
182     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
183     if (iBundleMgr == nullptr) {
184         HILOG_ERROR("iBundleMgr is nullptr");
185         return false;
186     }
187 
188     int32_t flags = BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION;
189     return (IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId)));
190 }
191 
GetCallerBundleName(std::string & callerBundleName)192 int32_t FormBmsHelper::GetCallerBundleName(std::string &callerBundleName)
193 {
194     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
195     if (iBundleMgr == nullptr) {
196         HILOG_ERROR("%{public}s, failed to get IBundleMgr.", __func__);
197         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
198     }
199     auto callingUid = IPCSkeleton::GetCallingUid();
200     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleNameForUid(callingUid, callerBundleName))) {
201         HILOG_ERROR("%{public}s, failed to get form config info.", __func__);
202         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
203     }
204     return ERR_OK;
205 }
206 
GetUidByBundleName(const std::string & bundleName,const int32_t userId)207 int32_t FormBmsHelper::GetUidByBundleName(const std::string &bundleName, const int32_t userId)
208 {
209     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
210     if (iBundleMgr == nullptr) {
211         HILOG_ERROR("%{public}s, failed to get IBundleMgr.", __func__);
212         return INVALID_UID;
213     }
214     return IN_PROCESS_CALL(iBundleMgr->GetUidByBundleName(bundleName, userId));
215 }
216 
GetCompileMode(const std::string & bundleName,const std::string & moduleName,int32_t userId,int32_t & compileMode)217 bool FormBmsHelper::GetCompileMode(const std::string &bundleName, const std::string &moduleName,
218     int32_t userId, int32_t &compileMode)
219 {
220     HILOG_DEBUG("%{public}s called.", __func__);
221     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
222     if (iBundleMgr == nullptr) {
223         HILOG_ERROR("iBundleMgr is nullptr");
224         return false;
225     }
226     int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
227     BundleInfo bundleInfo;
228     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
229         HILOG_ERROR("Get bundle info failed.");
230         return false;
231     }
232 
233     for (const auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
234         if (!moduleName.empty() && hapModuleInfo.moduleName != moduleName) {
235             continue;
236         }
237         compileMode = static_cast<int32_t>(hapModuleInfo.compileMode);
238         return true;
239     }
240 
241     HILOG_ERROR("Get compile mode failed.");
242     return false;
243 }
244 
GetCompatibleVersionCode(const std::string & bundleName,int32_t userId,int32_t & minCompatibleVersionCode)245 bool FormBmsHelper::GetCompatibleVersionCode(
246     const std::string& bundleName, int32_t userId, int32_t& minCompatibleVersionCode)
247 {
248     HILOG_DEBUG("%{public}s called.", __func__);
249     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
250     if (iBundleMgr == nullptr) {
251         HILOG_ERROR("iBundleMgr is nullptr");
252         return false;
253     }
254     int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
255     BundleInfo bundleInfo;
256     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
257         HILOG_ERROR("Get bundle info failed.");
258         return false;
259     }
260 
261     minCompatibleVersionCode = bundleInfo.minCompatibleVersionCode;
262     return true;
263 }
264 } // namespace AppExecFwk
265 } // namespace OHOS
266