1 /*
2 * Copyright (c) 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 "bundle_overlay_manager_host_impl.h"
17
18 #include "bundle_permission_mgr.h"
19 #include "bundle_overlay_data_manager.h"
20 #include "bundle_overlay_manager.h"
21 #include "xcollie_helper.h"
22 #include "scope_guard.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 const std::string FUNCTION_GET_ALL_OVERLAY_MODULE_INFO = "OverlayManagerHost::GetAllOverlayModuleInfo";
27 const std::string FUNCTION_GET_OVERLAY_MODULE_INFO = "OverlayManagerHost::GetOverlayModuleInfo";
28 const std::string FUNCTION_GET_OVERLAY_MODULE_INFO_NO_BUNDLENAME =
29 "OverlayManagerHost::GetOverlayModuleInfoWithoutBundleName";
30 const std::string FUNCTION_GET_TARGET_OVERLAY_MODULE_INFO = "OverlayManagerHost::GetTargetOverlayModuleInfo";
31 const std::string FUNCTION_GET_OVERLAY_MODULE_INFO_BY_BUNDLENAME =
32 "OverlayManagerHost::GetOverlayModuleInfoByBundleName";
33 const std::string FUNCTION_GET_OVERLAY_BUNDLE_INFOFOR_TARGET = "OverlayManagerHost::GetOverlayBundleInfoForTarget";
34 const std::string FUNCTION_GET_OVERLAY_MODULE_INFOFOR_TARGET = "OverlayManagerHost::GetOverlayModuleInfoForTarget";
35 const std::string FUNCTION_SET_OVERLAY_ENABLED_FOR_SELF = "OverlayManagerHost::SetOverlayEnabledForSelf";
36 const std::string FUNCTION_SET_OVERLAY_ENABLED = "OverlayManagerHost::SetOverlayEnabled";
37
OverlayManagerHostImpl()38 OverlayManagerHostImpl::OverlayManagerHostImpl()
39 {
40 APP_LOGI("create");
41 }
42
~OverlayManagerHostImpl()43 OverlayManagerHostImpl::~OverlayManagerHostImpl()
44 {
45 APP_LOGI("destory");
46 }
47
GetAllOverlayModuleInfo(const std::string & bundleName,std::vector<OverlayModuleInfo> & overlayModuleInfo,int32_t userId)48 ErrCode OverlayManagerHostImpl::GetAllOverlayModuleInfo(const std::string &bundleName,
49 std::vector<OverlayModuleInfo> &overlayModuleInfo, int32_t userId)
50 {
51 APP_LOGD("start to get all overlay moduleInfo of bundle %{public}s", bundleName.c_str());
52 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_ALL_OVERLAY_MODULE_INFO);
53 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
54 if (bundleName.empty()) {
55 APP_LOGE("invalid param");
56 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
57 }
58 if (!BundlePermissionMgr::CheckUserFromShell(userId)) {
59 LOG_E(BMS_TAG_INSTALLER, "check shell user fail");
60 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
61 }
62 if (userId == Constants::UNSPECIFIED_USERID) {
63 userId = BundleUtil::GetUserIdByCallingUid();
64 }
65 APP_LOGD("calling userId is %{public}d", userId);
66
67 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
68 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", bundleName.c_str());
69 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
70 }
71 return BundleOverlayManager::GetInstance()->GetAllOverlayModuleInfo(bundleName, overlayModuleInfo, userId);
72 }
73
GetOverlayModuleInfo(const std::string & bundleName,const std::string & moduleName,OverlayModuleInfo & overlayModuleInfo,int32_t userId)74 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfo(const std::string &bundleName, const std::string &moduleName,
75 OverlayModuleInfo &overlayModuleInfo, int32_t userId)
76 {
77 APP_LOGD("start to get overlay moduleInfo of bundle %{public}s and module %{public}s", bundleName.c_str(),
78 moduleName.c_str());
79 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_OVERLAY_MODULE_INFO);
80 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
81 if (!BundlePermissionMgr::CheckUserFromShell(userId)) {
82 LOG_E(BMS_TAG_INSTALLER, "check shell user fail");
83 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
84 }
85 if (bundleName.empty() || moduleName.empty()) {
86 APP_LOGE("invalid param");
87 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
88 }
89 if (userId == Constants::UNSPECIFIED_USERID) {
90 userId = BundleUtil::GetUserIdByCallingUid();
91 }
92 APP_LOGD("calling userId is %{public}d", userId);
93
94 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
95 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", bundleName.c_str());
96 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
97 }
98 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfo(bundleName, moduleName, overlayModuleInfo,
99 userId);
100 }
101
GetOverlayModuleInfo(const std::string & moduleName,OverlayModuleInfo & overlayModuleInfo,int32_t userId)102 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfo(const std::string &moduleName,
103 OverlayModuleInfo &overlayModuleInfo, int32_t userId)
104 {
105 APP_LOGD("start to get overlay moduleInfo of module %{public}s", moduleName.c_str());
106 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_OVERLAY_MODULE_INFO_NO_BUNDLENAME);
107 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
108 if (moduleName.empty()) {
109 APP_LOGE("invalid param");
110 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
111 }
112 if (!BundlePermissionMgr::CheckUserFromShell(userId)) {
113 LOG_E(BMS_TAG_INSTALLER, "check shell user fail");
114 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
115 }
116 if (userId == Constants::UNSPECIFIED_USERID) {
117 userId = BundleUtil::GetUserIdByCallingUid();
118 }
119 APP_LOGD("calling userId is %{public}d", userId);
120
121 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
122 if (callingBundleName.empty()) {
123 APP_LOGE("GetCallingBundleName failed");
124 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
125 }
126 APP_LOGD("get overlay moduleInfo of bundle %{public}s", callingBundleName.c_str());
127 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfo(callingBundleName, moduleName, overlayModuleInfo,
128 userId);
129 }
130
GetTargetOverlayModuleInfo(const std::string & targetModuleName,std::vector<OverlayModuleInfo> & overlayModuleInfos,int32_t userId)131 ErrCode OverlayManagerHostImpl::GetTargetOverlayModuleInfo(const std::string &targetModuleName,
132 std::vector<OverlayModuleInfo> &overlayModuleInfos, int32_t userId)
133 {
134 APP_LOGD("start to get target overlay moduleInfo of target module %{public}s", targetModuleName.c_str());
135 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_TARGET_OVERLAY_MODULE_INFO);
136 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
137 if (targetModuleName.empty()) {
138 APP_LOGE("invalid param");
139 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
140 }
141 if (userId == Constants::UNSPECIFIED_USERID) {
142 userId = BundleUtil::GetUserIdByCallingUid();
143 }
144 APP_LOGD("calling userId is %{public}d", userId);
145
146 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
147 if (callingBundleName.empty()) {
148 APP_LOGE("GetCallingBundleName failed");
149 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
150 }
151 APP_LOGD("get target overlay moduleInfo of bundle %{public}s", callingBundleName.c_str());
152 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfoForTarget(callingBundleName, targetModuleName,
153 overlayModuleInfos, userId);
154 }
155
GetOverlayModuleInfoByBundleName(const std::string & bundleName,const std::string & moduleName,std::vector<OverlayModuleInfo> & overlayModuleInfos,int32_t userId)156 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfoByBundleName(const std::string &bundleName,
157 const std::string &moduleName, std::vector<OverlayModuleInfo> &overlayModuleInfos, int32_t userId)
158 {
159 APP_LOGD("start to get overlay moduleInfo of bundle %{public}s and module %{public}s", bundleName.c_str(),
160 moduleName.c_str());
161 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_OVERLAY_MODULE_INFO_BY_BUNDLENAME);
162 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
163 if (bundleName.empty()) {
164 APP_LOGE("invalid param");
165 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
166 }
167 if (userId == Constants::UNSPECIFIED_USERID) {
168 userId = BundleUtil::GetUserIdByCallingUid();
169 }
170 APP_LOGD("calling userId is %{public}d", userId);
171
172 if (!BundlePermissionMgr::IsSystemApp()) {
173 APP_LOGE("non-system app is not allowed to call this function");
174 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
175 }
176
177 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
178 !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
179 APP_LOGE("no permission to query overlay info of bundleName %{public}s", bundleName.c_str());
180 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
181 }
182 if (moduleName.empty()) {
183 APP_LOGD("moduleName is empty, then to query all overlay module info in specified bundle");
184 return BundleOverlayManager::GetInstance()->GetAllOverlayModuleInfo(bundleName, overlayModuleInfos, userId);
185 }
186 OverlayModuleInfo overlayModuleInfo;
187 ErrCode res = BundleOverlayManager::GetInstance()->GetOverlayModuleInfo(bundleName, moduleName, overlayModuleInfo,
188 userId);
189 if (res != ERR_OK) {
190 APP_LOGE("GetOverlayModuleInfo failed due to errcode %{public}d", res);
191 return res;
192 }
193 overlayModuleInfos.emplace_back(overlayModuleInfo);
194 return ERR_OK;
195 }
196
GetOverlayBundleInfoForTarget(const std::string & targetBundleName,std::vector<OverlayBundleInfo> & overlayBundleInfo,int32_t userId)197 ErrCode OverlayManagerHostImpl::GetOverlayBundleInfoForTarget(const std::string &targetBundleName,
198 std::vector<OverlayBundleInfo> &overlayBundleInfo, int32_t userId)
199 {
200 APP_LOGD("start to get target overlay bundleInfo of bundle %{public}s", targetBundleName.c_str());
201 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_OVERLAY_BUNDLE_INFOFOR_TARGET);
202 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
203 if (!BundlePermissionMgr::CheckUserFromShell(userId)) {
204 LOG_E(BMS_TAG_INSTALLER, "check shell user fail");
205 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
206 }
207 if (targetBundleName.empty()) {
208 APP_LOGE("invalid param");
209 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
210 }
211 if (userId == Constants::UNSPECIFIED_USERID) {
212 userId = BundleUtil::GetUserIdByCallingUid();
213 }
214 APP_LOGD("calling userId is %{public}d", userId);
215
216 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
217 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", targetBundleName.c_str());
218 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
219 }
220 return BundleOverlayManager::GetInstance()->
221 GetOverlayBundleInfoForTarget(targetBundleName, overlayBundleInfo, userId);
222 }
223
GetOverlayModuleInfoForTarget(const std::string & targetBundleName,const std::string & targetModuleName,std::vector<OverlayModuleInfo> & overlayModuleInfo,int32_t userId)224 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfoForTarget(const std::string &targetBundleName,
225 const std::string &targetModuleName, std::vector<OverlayModuleInfo> &overlayModuleInfo, int32_t userId)
226 {
227 APP_LOGD("start to get target overlay moduleInfo of target bundle %{public}s and target module %{public}s",
228 targetBundleName.c_str(), targetModuleName.c_str());
229 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_OVERLAY_MODULE_INFOFOR_TARGET);
230 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
231 if (!BundlePermissionMgr::CheckUserFromShell(userId)) {
232 LOG_E(BMS_TAG_INSTALLER, "check shell user fail");
233 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
234 }
235 if (targetBundleName.empty()) {
236 APP_LOGE("invalid param");
237 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
238 }
239 if (userId == Constants::UNSPECIFIED_USERID) {
240 userId = BundleUtil::GetUserIdByCallingUid();
241 }
242 APP_LOGD("calling userId is %{public}d", userId);
243
244 if (!BundlePermissionMgr::IsSystemApp()) {
245 APP_LOGE("non-system app is not allowed to call this function");
246 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
247 }
248
249 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
250 !BundlePermissionMgr::IsBundleSelfCalling(targetBundleName)) {
251 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", targetBundleName.c_str());
252 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
253 }
254 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfoForTarget(targetBundleName, targetModuleName,
255 overlayModuleInfo, userId);
256 }
257
SetOverlayEnabledForSelf(const std::string & moduleName,bool isEnabled,int32_t userId)258 ErrCode OverlayManagerHostImpl::SetOverlayEnabledForSelf(const std::string &moduleName, bool isEnabled,
259 int32_t userId)
260 {
261 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_SET_OVERLAY_ENABLED_FOR_SELF);
262 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
263 if (moduleName.empty()) {
264 APP_LOGE("invalid param");
265 return ERR_BUNDLEMANAGER_OVERLAY_SET_OVERLAY_PARAM_ERROR;
266 }
267 if (userId == Constants::UNSPECIFIED_USERID) {
268 userId = BundleUtil::GetUserIdByCallingUid();
269 }
270
271 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
272 if (callingBundleName.empty()) {
273 APP_LOGE("GetCallingBundleName failed");
274 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
275 }
276 APP_LOGD("set overlay enable %{public}d for bundle %{public}s", isEnabled, callingBundleName.c_str());
277 return BundleOverlayManager::GetInstance()->SetOverlayEnabled(callingBundleName, moduleName, isEnabled, userId);
278 }
279
SetOverlayEnabled(const std::string & bundleName,const std::string & moduleName,bool isEnabled,int32_t userId)280 ErrCode OverlayManagerHostImpl::SetOverlayEnabled(const std::string &bundleName, const std::string &moduleName,
281 bool isEnabled, int32_t userId)
282 {
283 APP_LOGD("start");
284 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_SET_OVERLAY_ENABLED);
285 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
286 if (bundleName.empty() || moduleName.empty()) {
287 APP_LOGE("invalid param");
288 return ERR_BUNDLEMANAGER_OVERLAY_SET_OVERLAY_PARAM_ERROR;
289 }
290 if (userId == Constants::UNSPECIFIED_USERID) {
291 userId = BundleUtil::GetUserIdByCallingUid();
292 }
293 APP_LOGD("calling userId is %{public}d", userId);
294
295 if (!BundlePermissionMgr::IsSystemApp()) {
296 APP_LOGE("non-system app is not allowed to call this function");
297 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
298 }
299 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_CHANGE_OVERLAY_ENABLED_STATE) &&
300 !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
301 APP_LOGE("no permission to query overlay info of bundleName %{public}s", bundleName.c_str());
302 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
303 }
304 return BundleOverlayManager::GetInstance()->SetOverlayEnabled(bundleName, moduleName, isEnabled, userId);
305 }
306 } // AppExecFwk
307 } // OHOS
308