1 /*
2 * Copyright (c) 2021-2024 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 #ifndef OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H
18
19 #include <string>
20 #include <unordered_set>
21
22 #include "ability_config.h"
23 #include "ability_manager_client.h"
24 #include "ability_manager_errors.h"
25 #include "app_jump_control_rule.h"
26 #include "bundle_mgr_helper.h"
27 #include "hilog_tag_wrapper.h"
28 #include "in_process_call_wrapper.h"
29 #include "ipc_skeleton.h"
30 #include "permission_verification.h"
31 #include "sa_mgr_client.h"
32 #include "system_ability_definition.h"
33
34 namespace OHOS {
35 namespace AAFwk {
36 namespace AbilityUtil {
37 constexpr const char* SYSTEM_BASIC = "system_basic";
38 constexpr const char* SYSTEM_CORE = "system_core";
39 constexpr const char* DEFAULT_DEVICE_ID = "";
40
41 #ifdef WITH_DLP
42 constexpr const char* DLP_BUNDLE_NAME = "com.ohos.dlpmanager";
43 constexpr const char* DLP_MODULE_NAME = "entry";
44 constexpr const char* DLP_ABILITY_NAME = "ViewAbility";
45 constexpr const char* DLP_PARAMS_SANDBOX = "ohos.dlp.params.sandbox";
46 constexpr const char* DLP_PARAMS_BUNDLE_NAME = "ohos.dlp.params.bundleName";
47 constexpr const char* DLP_PARAMS_MODULE_NAME = "ohos.dlp.params.moduleName";
48 constexpr const char* DLP_PARAMS_ABILITY_NAME = "ohos.dlp.params.abilityName";
49 #endif // WITH_DLP
50 constexpr const char* MARKET_BUNDLE_NAME = "com.huawei.hmsapp.appgallery";
51 constexpr const char* MARKET_CROWD_TEST_BUNDLE_PARAM = "crowd_test_bundle_name";
52 constexpr const char* BUNDLE_NAME_SELECTOR_DIALOG = "com.ohos.amsdialog";
53 constexpr const char* JUMP_INTERCEPTOR_DIALOG_CALLER_PKG = "interceptor_callerPkg";
54
55 #define CHECK_POINTER_CONTINUE(object) \
56 if (!object) { \
57 TAG_LOGE(AAFwkTag::ABILITYMGR, "null pointer"); \
58 continue; \
59 }
60
61 #define CHECK_POINTER_IS_NULLPTR(object) \
62 if (object == nullptr) { \
63 TAG_LOGE(AAFwkTag::ABILITYMGR, "null pointer"); \
64 return; \
65 }
66
67 #define CHECK_POINTER(object) \
68 if (!object) { \
69 TAG_LOGE(AAFwkTag::ABILITYMGR, "null pointer"); \
70 return; \
71 }
72
73 #define CHECK_POINTER_LOG(object, log) \
74 if (!object) { \
75 TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s:", log); \
76 return; \
77 }
78
79 #define CHECK_POINTER_TAG_LOG(object, tag, log) \
80 if (!object) { \
81 TAG_LOGE(tag, "%{public}s:", log); \
82 return; \
83 }
84
85 #define CHECK_POINTER_AND_RETURN(object, value) \
86 if (!object) { \
87 TAG_LOGE(AAFwkTag::ABILITYMGR, "null pointer"); \
88 return value; \
89 }
90
91 #define CHECK_POINTER_AND_RETURN_LOG(object, value, log) \
92 if (!object) { \
93 TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s:", log); \
94 return value; \
95 }
96
97 #define CHECK_POINTER_RETURN_BOOL(object) \
98 if (!object) { \
99 TAG_LOGE(AAFwkTag::ABILITYMGR, "null pointer"); \
100 return false; \
101 }
102
103 #define CHECK_RET_RETURN_RET(object, log) \
104 if (object != ERR_OK) { \
105 TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s, ret : %{public}d", log, object); \
106 return object; \
107 }
108
109 #define CHECK_TRUE_RETURN_RET(object, value, log) \
110 if (object) { \
111 TAG_LOGW(AAFwkTag::ABILITYMGR, "%{public}s", log); \
112 return value; \
113 }
114
IsSystemDialogAbility(const std::string & bundleName,const std::string & abilityName)115 [[maybe_unused]] static bool IsSystemDialogAbility(const std::string &bundleName, const std::string &abilityName)
116 {
117 if (abilityName == AbilityConfig::SYSTEM_DIALOG_NAME && bundleName == AbilityConfig::SYSTEM_UI_BUNDLE_NAME) {
118 return true;
119 }
120
121 if (abilityName == AbilityConfig::DEVICE_MANAGER_NAME && bundleName == AbilityConfig::DEVICE_MANAGER_BUNDLE_NAME) {
122 return true;
123 }
124
125 return false;
126 }
127
128 [[maybe_unused]] static std::string ConvertBundleNameSingleton(const std::string &bundleName, const std::string &name,
129 const std::string &moduleName, const int32_t appIndex = 0)
130 {
131 std::string strName;
132 if (appIndex == 0) {
133 strName = AbilityConfig::MISSION_NAME_MARK_HEAD + bundleName +
134 AbilityConfig::MISSION_NAME_SEPARATOR + moduleName +
135 AbilityConfig::MISSION_NAME_SEPARATOR + name;
136 } else {
137 strName = AbilityConfig::MISSION_NAME_MARK_HEAD + bundleName +
138 AbilityConfig::MISSION_NAME_SEPARATOR + std::to_string(appIndex) +
139 AbilityConfig::MISSION_NAME_SEPARATOR + moduleName +
140 AbilityConfig::MISSION_NAME_SEPARATOR + name;
141 }
142
143 return strName;
144 }
145
146 static constexpr int64_t NANOSECONDS = 1000000000; // NANOSECONDS mean 10^9 nano second
147 static constexpr int64_t MICROSECONDS = 1000000; // MICROSECONDS mean 10^6 millias second
SystemTimeMillis()148 [[maybe_unused]] static int64_t SystemTimeMillis()
149 {
150 struct timespec t;
151 t.tv_sec = 0;
152 t.tv_nsec = 0;
153 clock_gettime(CLOCK_MONOTONIC, &t);
154 return (int64_t)((t.tv_sec) * NANOSECONDS + t.tv_nsec) / MICROSECONDS;
155 }
156
UTCTimeSeconds()157 [[maybe_unused]] static int64_t UTCTimeSeconds()
158 {
159 struct timespec t;
160 t.tv_sec = 0;
161 t.tv_nsec = 0;
162 clock_gettime(CLOCK_REALTIME, &t);
163 return (int64_t)(t.tv_sec);
164 }
165
IsStartFreeInstall(const Want & want)166 [[maybe_unused]] static bool IsStartFreeInstall(const Want &want)
167 {
168 auto flags = want.GetFlags();
169 if ((flags & Want::FLAG_INSTALL_ON_DEMAND) == Want::FLAG_INSTALL_ON_DEMAND) {
170 return true;
171 }
172 return false;
173 }
174
GetBundleManagerHelper()175 [[maybe_unused]] static std::shared_ptr<AppExecFwk::BundleMgrHelper> GetBundleManagerHelper()
176 {
177 return DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
178 }
179
ParseJumpInterceptorWant(Want & targetWant,const std::string callerPkg)180 [[maybe_unused]] static bool ParseJumpInterceptorWant(Want &targetWant, const std::string callerPkg)
181 {
182 if (callerPkg.empty()) {
183 TAG_LOGE(AAFwkTag::ABILITYMGR, "empty callerPkg");
184 return false;
185 }
186 targetWant.SetParam(JUMP_INTERCEPTOR_DIALOG_CALLER_PKG, callerPkg);
187 return true;
188 }
189
CheckJumpInterceptorWant(const Want & targetWant,std::string & callerPkg,std::string & targetPkg)190 [[maybe_unused]] static bool CheckJumpInterceptorWant(const Want &targetWant, std::string &callerPkg,
191 std::string &targetPkg)
192 {
193 if (!targetWant.HasParameter(JUMP_INTERCEPTOR_DIALOG_CALLER_PKG)) {
194 TAG_LOGE(AAFwkTag::ABILITYMGR, "invalid interceptor param");
195 return false;
196 }
197 callerPkg = targetWant.GetStringParam(JUMP_INTERCEPTOR_DIALOG_CALLER_PKG);
198 targetPkg = targetWant.GetElement().GetBundleName();
199 return !callerPkg.empty() && !targetPkg.empty();
200 }
201
AddAbilityJumpRuleToBms(const std::string & callerPkg,const std::string & targetPkg,int32_t userId)202 [[maybe_unused]] static bool AddAbilityJumpRuleToBms(const std::string &callerPkg, const std::string &targetPkg,
203 int32_t userId)
204 {
205 if (callerPkg.empty() || targetPkg.empty()) {
206 TAG_LOGE(AAFwkTag::ABILITYMGR, "invalid inputs");
207 return false;
208 }
209 auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
210 if (bundleMgrHelper == nullptr) {
211 TAG_LOGE(AAFwkTag::ABILITYMGR, "GetBundleManagerHelper failed");
212 return false;
213 }
214 auto appControlMgr = bundleMgrHelper->GetAppControlProxy();
215 if (appControlMgr == nullptr) {
216 TAG_LOGE(AAFwkTag::ABILITYMGR, "Get appControlMgr failed");
217 return false;
218 }
219 int ret = IN_PROCESS_CALL(appControlMgr->ConfirmAppJumpControlRule(callerPkg, targetPkg, userId));
220 return ret == ERR_OK;
221 }
222
223 #ifdef WITH_DLP
HandleDlpApp(Want & want)224 [[maybe_unused]] static bool HandleDlpApp(Want &want)
225 {
226 const std::unordered_set<std::string> whiteListDlpSet = { BUNDLE_NAME_SELECTOR_DIALOG };
227 if (whiteListDlpSet.find(want.GetBundle()) != whiteListDlpSet.end()) {
228 TAG_LOGI(AAFwkTag::ABILITYMGR, "enter special app");
229 return false;
230 }
231
232 AppExecFwk::ElementName element = want.GetElement();
233 if (want.GetBoolParam(DLP_PARAMS_SANDBOX, false) && !element.GetBundleName().empty() &&
234 !element.GetAbilityName().empty()) {
235 want.SetElementName(DEFAULT_DEVICE_ID, DLP_BUNDLE_NAME, DLP_ABILITY_NAME, DLP_MODULE_NAME);
236 want.SetParam(DLP_PARAMS_BUNDLE_NAME, element.GetBundleName());
237 want.SetParam(DLP_PARAMS_MODULE_NAME, element.GetModuleName());
238 want.SetParam(DLP_PARAMS_ABILITY_NAME, element.GetAbilityName());
239 want.RemoveParam(DLP_PARAMS_SANDBOX);
240 return true;
241 }
242
243 return false;
244 }
245 #endif // WITH_DLP
246
IsStartIncludeAtomicService(const Want & want,const int32_t userId)247 [[maybe_unused]] static bool IsStartIncludeAtomicService(const Want &want, const int32_t userId)
248 {
249 auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
250 if (bundleMgrHelper == nullptr) {
251 TAG_LOGE(AAFwkTag::ABILITYMGR, "GetBundleManagerHelper failed");
252 return false;
253 }
254
255 std::string targetBundleName = want.GetBundle();
256 AppExecFwk::ApplicationInfo targetAppInfo;
257 bool getTargetResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(targetBundleName,
258 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, targetAppInfo));
259 if (!getTargetResult) {
260 TAG_LOGE(AAFwkTag::ABILITYMGR, "Get targetAppInfo failed");
261 return false;
262 }
263 if (targetAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
264 TAG_LOGI(AAFwkTag::ABILITYMGR, "target is atomic service");
265 return true;
266 }
267
268 int callerUid = IPCSkeleton::GetCallingUid();
269 std::string callerBundleName;
270 ErrCode err = IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerUid, callerBundleName));
271 if (err != ERR_OK) {
272 TAG_LOGE(AAFwkTag::ABILITYMGR, "Get bms failed");
273 return false;
274 }
275 AppExecFwk::ApplicationInfo callerAppInfo;
276 bool getCallerResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(callerBundleName,
277 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, callerAppInfo));
278 if (!getCallerResult) {
279 TAG_LOGE(AAFwkTag::ABILITYMGR, "Get callerAppInfo failed");
280 return false;
281 }
282 if (callerAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
283 TAG_LOGI(AAFwkTag::ABILITYMGR, "caller is atomic service");
284 return true;
285 }
286 return false;
287 }
288
RemoveShowModeKey(Want & want)289 [[maybe_unused]] static void RemoveShowModeKey(Want &want)
290 {
291 if (want.HasParameter(AAFwk::SCREEN_MODE_KEY)) {
292 want.RemoveParam(AAFwk::SCREEN_MODE_KEY);
293 }
294 }
295
IsSceneBoard(const std::string & bundleName,const std::string & AbilityName)296 [[maybe_unused]] static bool IsSceneBoard(const std::string &bundleName, const std::string &AbilityName)
297 {
298 return AbilityName == AbilityConfig::SCENEBOARD_ABILITY_NAME &&
299 bundleName == AbilityConfig::SCENEBOARD_BUNDLE_NAME;
300 }
301
RemoveWindowModeKey(Want & want)302 [[maybe_unused]] static void RemoveWindowModeKey(Want &want)
303 {
304 if (want.HasParameter(Want::PARAM_RESV_WINDOW_MODE)) {
305 want.RemoveParam(Want::PARAM_RESV_WINDOW_MODE);
306 }
307 }
308
RemoveInstanceKey(Want & want)309 [[maybe_unused]] static void RemoveInstanceKey(Want &want)
310 {
311 want.RemoveParam(Want::APP_INSTANCE_KEY);
312 want.RemoveParam(Want::CREATE_APP_INSTANCE_KEY);
313 }
314
RemoveWantKey(Want & want)315 [[maybe_unused]] static void RemoveWantKey(Want &want)
316 {
317 RemoveShowModeKey(want);
318 RemoveWindowModeKey(want);
319 }
320
CheckInstanceKey(const Want & want)321 [[maybe_unused]] static int32_t CheckInstanceKey(const Want &want)
322 {
323 auto instanceKey = want.GetStringParam(Want::APP_INSTANCE_KEY);
324 auto isCreating = want.GetBoolParam(Want::CREATE_APP_INSTANCE_KEY, false);
325 if (!instanceKey.empty() || isCreating) {
326 TAG_LOGE(AAFwkTag::ABILITYMGR, "Not support multi-instance");
327 return ERR_MULTI_INSTANCE_NOT_SUPPORTED;
328 }
329 return ERR_OK;
330 }
331
WantSetParameterWindowMode(Want & want,int32_t windowMode)332 [[maybe_unused]] static void WantSetParameterWindowMode(Want &want, int32_t windowMode)
333 {
334 want.SetParam(Want::PARAM_RESV_WINDOW_MODE, windowMode);
335 }
336
ProcessWindowMode(Want & want,uint32_t accessTokenId,int32_t windowMode)337 [[maybe_unused]] static void ProcessWindowMode(Want &want, uint32_t accessTokenId, int32_t windowMode)
338 {
339 if (PermissionVerification::GetInstance()->IsSystemAppCall()) {
340 want.SetParam(Want::PARAM_RESV_WINDOW_MODE, windowMode);
341 return;
342 }
343 if (IPCSkeleton::GetCallingTokenID() == accessTokenId && (
344 windowMode == AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_PRIMARY ||
345 windowMode == AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_SECONDARY)) {
346 want.SetParam(Want::PARAM_RESV_WINDOW_MODE, windowMode);
347 TAG_LOGI(AAFwkTag::ABILITYMGR, "set windownMode for inner application split-screen mode");
348 } else if (windowMode == AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_FULLSCREEN) {
349 want.SetParam(Want::PARAM_RESV_WINDOW_MODE, windowMode);
350 TAG_LOGI(AAFwkTag::ABILITYMGR, "set windownMode for full screen mode");
351 } else {
352 RemoveWindowModeKey(want);
353 }
354 }
355
StartAppgallery(const std::string & bundleName,const int requestCode,const int32_t userId,const std::string & action)356 [[maybe_unused]] static int StartAppgallery(const std::string &bundleName, const int requestCode, const int32_t userId,
357 const std::string &action)
358 {
359 std::string appGalleryBundleName;
360 auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
361 if (bundleMgrHelper == nullptr || !bundleMgrHelper->QueryAppGalleryBundleName(appGalleryBundleName)) {
362 TAG_LOGE(AAFwkTag::ABILITYMGR, "GetBundleManagerHelper or QueryAppGalleryBundleName failed");
363 appGalleryBundleName = MARKET_BUNDLE_NAME;
364 }
365
366 TAG_LOGD(AAFwkTag::ABILITYMGR, "appGalleryBundleName:%{public}s", appGalleryBundleName.c_str());
367
368 Want want;
369 want.SetElementName(appGalleryBundleName, "");
370 want.SetAction(action);
371 want.SetParam(MARKET_CROWD_TEST_BUNDLE_PARAM, bundleName);
372 return AbilityManagerClient::GetInstance()->StartAbility(want, requestCode, userId);
373 }
374
EdmErrorType(bool isEdm)375 inline ErrCode EdmErrorType(bool isEdm)
376 {
377 if (isEdm) {
378 return ERR_EDM_APP_CONTROLLED;
379 }
380 return ERR_APP_CONTROLLED;
381 }
382 } // namespace AbilityUtil
383 } // namespace AAFwk
384 } // namespace OHOS
385
386 #endif // OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H
387