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 FUNCATION_GET_ALL_OVERLAY_MODULE_INFO = "OverlayManagerHost::GetAllOverlayModuleInfo";
27 const std::string FUNCATION_GET_OVERLAY_MODULE_INFO = "OverlayManagerHost::GetOverlayModuleInfo";
28 const std::string FUNCATION_GET_OVERLAY_MODULE_INFO_NO_BUNDLENAME =
29 "OverlayManagerHost::GetOverlayModuleInfoWithoutBundleName";
30 const std::string FUNCATION_GET_TARGET_OVERLAY_MODULE_INFO = "OverlayManagerHost::GetTargetOverlayModuleInfo";
31 const std::string FUNCATION_GET_OVERLAY_MODULE_INFO_BY_BUNDLENAME =
32 "OverlayManagerHost::GetOverlayModuleInfoByBundleName";
33 const std::string FUNCATION_GET_OVERLAY_BUNDLE_INFOFOR_TARGET = "OverlayManagerHost::GetOverlayBundleInfoForTarget";
34 const std::string FUNCATION_GET_OVERLAY_MODULE_INFOFOR_TARGET = "OverlayManagerHost::GetOverlayModuleInfoForTarget";
35 const std::string FUNCATION_SET_OVERLAY_ENABLED_FOR_SELF = "OverlayManagerHost::SetOverlayEnabledForSelf";
36 const std::string FUNCATION_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(FUNCATION_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 (userId == Constants::UNSPECIFIED_USERID) {
59 userId = BundleUtil::GetUserIdByCallingUid();
60 }
61 APP_LOGD("calling userId is %{public}d", userId);
62
63 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
64 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", bundleName.c_str());
65 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
66 }
67 return BundleOverlayManager::GetInstance()->GetAllOverlayModuleInfo(bundleName, overlayModuleInfo, userId);
68 }
69
GetOverlayModuleInfo(const std::string & bundleName,const std::string & moduleName,OverlayModuleInfo & overlayModuleInfo,int32_t userId)70 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfo(const std::string &bundleName, const std::string &moduleName,
71 OverlayModuleInfo &overlayModuleInfo, int32_t userId)
72 {
73 APP_LOGD("start to get overlay moduleInfo of bundle %{public}s and module %{public}s", bundleName.c_str(),
74 moduleName.c_str());
75 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_OVERLAY_MODULE_INFO);
76 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
77 if (bundleName.empty() || moduleName.empty()) {
78 APP_LOGE("invalid param");
79 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
80 }
81 if (userId == Constants::UNSPECIFIED_USERID) {
82 userId = BundleUtil::GetUserIdByCallingUid();
83 }
84 APP_LOGD("calling userId is %{public}d", userId);
85
86 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
87 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", bundleName.c_str());
88 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
89 }
90 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfo(bundleName, moduleName, overlayModuleInfo,
91 userId);
92 }
93
GetOverlayModuleInfo(const std::string & moduleName,OverlayModuleInfo & overlayModuleInfo,int32_t userId)94 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfo(const std::string &moduleName,
95 OverlayModuleInfo &overlayModuleInfo, int32_t userId)
96 {
97 APP_LOGD("start to get overlay moduleInfo of module %{public}s", moduleName.c_str());
98 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_OVERLAY_MODULE_INFO_NO_BUNDLENAME);
99 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
100 if (moduleName.empty()) {
101 APP_LOGE("invalid param");
102 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
103 }
104 if (userId == Constants::UNSPECIFIED_USERID) {
105 userId = BundleUtil::GetUserIdByCallingUid();
106 }
107 APP_LOGD("calling userId is %{public}d", userId);
108
109 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
110 if (callingBundleName.empty()) {
111 APP_LOGE("GetCallingBundleName failed");
112 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
113 }
114 APP_LOGD("get overlay moduleInfo of bundle %{public}s", callingBundleName.c_str());
115 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfo(callingBundleName, moduleName, overlayModuleInfo,
116 userId);
117 }
118
GetTargetOverlayModuleInfo(const std::string & targetModuleName,std::vector<OverlayModuleInfo> & overlayModuleInfos,int32_t userId)119 ErrCode OverlayManagerHostImpl::GetTargetOverlayModuleInfo(const std::string &targetModuleName,
120 std::vector<OverlayModuleInfo> &overlayModuleInfos, int32_t userId)
121 {
122 APP_LOGD("start to get target overlay moduleInfo of target module %{public}s", targetModuleName.c_str());
123 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_TARGET_OVERLAY_MODULE_INFO);
124 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
125 if (targetModuleName.empty()) {
126 APP_LOGE("invalid param");
127 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
128 }
129 if (userId == Constants::UNSPECIFIED_USERID) {
130 userId = BundleUtil::GetUserIdByCallingUid();
131 }
132 APP_LOGD("calling userId is %{public}d", userId);
133
134 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
135 if (callingBundleName.empty()) {
136 APP_LOGE("GetCallingBundleName failed");
137 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
138 }
139 APP_LOGD("get target overlay moduleInfo of bundle %{public}s", callingBundleName.c_str());
140 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfoForTarget(callingBundleName, targetModuleName,
141 overlayModuleInfos, userId);
142 }
143
GetOverlayModuleInfoByBundleName(const std::string & bundleName,const std::string & moduleName,std::vector<OverlayModuleInfo> & overlayModuleInfos,int32_t userId)144 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfoByBundleName(const std::string &bundleName,
145 const std::string &moduleName, std::vector<OverlayModuleInfo> &overlayModuleInfos, int32_t userId)
146 {
147 APP_LOGD("start to get overlay moduleInfo of bundle %{public}s and module %{public}s", bundleName.c_str(),
148 moduleName.c_str());
149 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_OVERLAY_MODULE_INFO_BY_BUNDLENAME);
150 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
151 if (bundleName.empty()) {
152 APP_LOGE("invalid param");
153 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
154 }
155 if (userId == Constants::UNSPECIFIED_USERID) {
156 userId = BundleUtil::GetUserIdByCallingUid();
157 }
158 APP_LOGD("calling userId is %{public}d", userId);
159
160 if (!BundlePermissionMgr::IsSystemApp()) {
161 APP_LOGE("non-system app is not allowed to call this function");
162 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
163 }
164
165 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
166 !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
167 APP_LOGE("no permission to query overlay info of bundleName %{public}s", bundleName.c_str());
168 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
169 }
170 if (moduleName.empty()) {
171 APP_LOGD("moduleName is empty, then to query all overlay module info in specified bundle");
172 return BundleOverlayManager::GetInstance()->GetAllOverlayModuleInfo(bundleName, overlayModuleInfos, userId);
173 }
174 OverlayModuleInfo overlayModuleInfo;
175 ErrCode res = BundleOverlayManager::GetInstance()->GetOverlayModuleInfo(bundleName, moduleName, overlayModuleInfo,
176 userId);
177 if (res != ERR_OK) {
178 APP_LOGE("GetOverlayModuleInfo failed due to errcode %{public}d", res);
179 return res;
180 }
181 overlayModuleInfos.emplace_back(overlayModuleInfo);
182 return ERR_OK;
183 }
184
GetOverlayBundleInfoForTarget(const std::string & targetBundleName,std::vector<OverlayBundleInfo> & overlayBundleInfo,int32_t userId)185 ErrCode OverlayManagerHostImpl::GetOverlayBundleInfoForTarget(const std::string &targetBundleName,
186 std::vector<OverlayBundleInfo> &overlayBundleInfo, int32_t userId)
187 {
188 APP_LOGD("start to get target overlay bundleInfo of bundle %{public}s", targetBundleName.c_str());
189 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_OVERLAY_BUNDLE_INFOFOR_TARGET);
190 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
191 if (targetBundleName.empty()) {
192 APP_LOGE("invalid param");
193 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
194 }
195 if (userId == Constants::UNSPECIFIED_USERID) {
196 userId = BundleUtil::GetUserIdByCallingUid();
197 }
198 APP_LOGD("calling userId is %{public}d", userId);
199
200 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
201 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", targetBundleName.c_str());
202 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
203 }
204 return BundleOverlayManager::GetInstance()->
205 GetOverlayBundleInfoForTarget(targetBundleName, overlayBundleInfo, userId);
206 }
207
GetOverlayModuleInfoForTarget(const std::string & targetBundleName,const std::string & targetModuleName,std::vector<OverlayModuleInfo> & overlayModuleInfo,int32_t userId)208 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfoForTarget(const std::string &targetBundleName,
209 const std::string &targetModuleName, std::vector<OverlayModuleInfo> &overlayModuleInfo, int32_t userId)
210 {
211 APP_LOGD("start to get target overlay moduleInfo of target bundle %{public}s and target module %{public}s",
212 targetBundleName.c_str(), targetModuleName.c_str());
213 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_OVERLAY_MODULE_INFOFOR_TARGET);
214 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
215 if (targetBundleName.empty()) {
216 APP_LOGE("invalid param");
217 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
218 }
219 if (userId == Constants::UNSPECIFIED_USERID) {
220 userId = BundleUtil::GetUserIdByCallingUid();
221 }
222 APP_LOGD("calling userId is %{public}d", userId);
223
224 if (!BundlePermissionMgr::IsSystemApp()) {
225 APP_LOGE("non-system app is not allowed to call this function");
226 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
227 }
228
229 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
230 !BundlePermissionMgr::IsBundleSelfCalling(targetBundleName)) {
231 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", targetBundleName.c_str());
232 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
233 }
234 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfoForTarget(targetBundleName, targetModuleName,
235 overlayModuleInfo, userId);
236 }
237
SetOverlayEnabledForSelf(const std::string & moduleName,bool isEnabled,int32_t userId)238 ErrCode OverlayManagerHostImpl::SetOverlayEnabledForSelf(const std::string &moduleName, bool isEnabled,
239 int32_t userId)
240 {
241 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_SET_OVERLAY_ENABLED_FOR_SELF);
242 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
243 if (moduleName.empty()) {
244 APP_LOGE("invalid param");
245 return ERR_BUNDLEMANAGER_OVERLAY_SET_OVERLAY_PARAM_ERROR;
246 }
247 if (userId == Constants::UNSPECIFIED_USERID) {
248 userId = BundleUtil::GetUserIdByCallingUid();
249 }
250
251 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
252 if (callingBundleName.empty()) {
253 APP_LOGE("GetCallingBundleName failed");
254 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
255 }
256 APP_LOGD("set overlay enable %{public}d for bundle %{public}s", isEnabled, callingBundleName.c_str());
257 return BundleOverlayManager::GetInstance()->SetOverlayEnabled(callingBundleName, moduleName, isEnabled, userId);
258 }
259
SetOverlayEnabled(const std::string & bundleName,const std::string & moduleName,bool isEnabled,int32_t userId)260 ErrCode OverlayManagerHostImpl::SetOverlayEnabled(const std::string &bundleName, const std::string &moduleName,
261 bool isEnabled, int32_t userId)
262 {
263 APP_LOGD("start");
264 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_SET_OVERLAY_ENABLED);
265 ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
266 if (bundleName.empty() || moduleName.empty()) {
267 APP_LOGE("invalid param");
268 return ERR_BUNDLEMANAGER_OVERLAY_SET_OVERLAY_PARAM_ERROR;
269 }
270 if (userId == Constants::UNSPECIFIED_USERID) {
271 userId = BundleUtil::GetUserIdByCallingUid();
272 }
273 APP_LOGD("calling userId is %{public}d", userId);
274
275 if (!BundlePermissionMgr::IsSystemApp()) {
276 APP_LOGE("non-system app is not allowed to call this function");
277 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
278 }
279 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_CHANGE_OVERLAY_ENABLED_STATE) &&
280 !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
281 APP_LOGE("no permission to query overlay info of bundleName %{public}s", bundleName.c_str());
282 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
283 }
284 return BundleOverlayManager::GetInstance()->SetOverlayEnabled(bundleName, moduleName, isEnabled, userId);
285 }
286 } // AppExecFwk
287 } // OHOS
288