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, "nullptr"); \
58 continue; \
59 }
60
61 #define CHECK_POINTER_IS_NULLPTR(object) \
62 if (object == nullptr) { \
63 TAG_LOGE(AAFwkTag::ABILITYMGR, "nullptr"); \
64 return; \
65 }
66
67 #define CHECK_POINTER(object) \
68 if (!object) { \
69 TAG_LOGE(AAFwkTag::ABILITYMGR, "nullptr"); \
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, "nullptr"); \
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, "nullptr"); \
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
GetSysTimeNs()115 [[maybe_unused]] static int64_t GetSysTimeNs()
116 {
117 auto now = std::chrono::steady_clock::now().time_since_epoch();
118 return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
119 }
120
IsSystemDialogAbility(const std::string & bundleName,const std::string & abilityName)121 [[maybe_unused]] static bool IsSystemDialogAbility(const std::string &bundleName, const std::string &abilityName)
122 {
123 if (abilityName == AbilityConfig::SYSTEM_DIALOG_NAME && bundleName == AbilityConfig::SYSTEM_UI_BUNDLE_NAME) {
124 return true;
125 }
126
127 if (abilityName == AbilityConfig::DEVICE_MANAGER_NAME && bundleName == AbilityConfig::DEVICE_MANAGER_BUNDLE_NAME) {
128 return true;
129 }
130
131 return false;
132 }
133
134 [[maybe_unused]] static std::string ConvertBundleNameSingleton(const std::string &bundleName, const std::string &name,
135 const std::string &moduleName, const int32_t appIndex = 0)
136 {
137 std::string strName;
138 if (appIndex == 0) {
139 strName = AbilityConfig::MISSION_NAME_MARK_HEAD + bundleName +
140 AbilityConfig::MISSION_NAME_SEPARATOR + moduleName +
141 AbilityConfig::MISSION_NAME_SEPARATOR + name;
142 } else {
143 strName = AbilityConfig::MISSION_NAME_MARK_HEAD + bundleName +
144 AbilityConfig::MISSION_NAME_SEPARATOR + std::to_string(appIndex) +
145 AbilityConfig::MISSION_NAME_SEPARATOR + moduleName +
146 AbilityConfig::MISSION_NAME_SEPARATOR + name;
147 }
148
149 return strName;
150 }
151
152 static constexpr int64_t NANOSECONDS = 1000000000; // NANOSECONDS mean 10^9 nano second
153 static constexpr int64_t MICROSECONDS = 1000000; // MICROSECONDS mean 10^6 millias second
SystemTimeMillis()154 [[maybe_unused]] static int64_t SystemTimeMillis()
155 {
156 struct timespec t;
157 t.tv_sec = 0;
158 t.tv_nsec = 0;
159 clock_gettime(CLOCK_MONOTONIC, &t);
160 return (int64_t)((t.tv_sec) * NANOSECONDS + t.tv_nsec) / MICROSECONDS;
161 }
162
UTCTimeSeconds()163 [[maybe_unused]] static int64_t UTCTimeSeconds()
164 {
165 struct timespec t;
166 t.tv_sec = 0;
167 t.tv_nsec = 0;
168 clock_gettime(CLOCK_REALTIME, &t);
169 return (int64_t)(t.tv_sec);
170 }
171
IsStartFreeInstall(const Want & want)172 [[maybe_unused]] static bool IsStartFreeInstall(const Want &want)
173 {
174 auto flags = want.GetFlags();
175 if ((flags & Want::FLAG_INSTALL_ON_DEMAND) == Want::FLAG_INSTALL_ON_DEMAND) {
176 return true;
177 }
178 return false;
179 }
180
GetBundleManagerHelper()181 [[maybe_unused]] static std::shared_ptr<AppExecFwk::BundleMgrHelper> GetBundleManagerHelper()
182 {
183 return DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
184 }
185
ParseJumpInterceptorWant(Want & targetWant,const std::string callerPkg)186 [[maybe_unused]] static bool ParseJumpInterceptorWant(Want &targetWant, const std::string callerPkg)
187 {
188 if (callerPkg.empty()) {
189 TAG_LOGE(AAFwkTag::ABILITYMGR, "empty callerPkg");
190 return false;
191 }
192 targetWant.SetParam(JUMP_INTERCEPTOR_DIALOG_CALLER_PKG, callerPkg);
193 return true;
194 }
195
CheckJumpInterceptorWant(const Want & targetWant,std::string & callerPkg,std::string & targetPkg)196 [[maybe_unused]] static bool CheckJumpInterceptorWant(const Want &targetWant, std::string &callerPkg,
197 std::string &targetPkg)
198 {
199 if (!targetWant.HasParameter(JUMP_INTERCEPTOR_DIALOG_CALLER_PKG)) {
200 TAG_LOGE(AAFwkTag::ABILITYMGR, "invalid interceptor param");
201 return false;
202 }
203 callerPkg = targetWant.GetStringParam(JUMP_INTERCEPTOR_DIALOG_CALLER_PKG);
204 targetPkg = targetWant.GetElement().GetBundleName();
205 return !callerPkg.empty() && !targetPkg.empty();
206 }
207
AddAbilityJumpRuleToBms(const std::string & callerPkg,const std::string & targetPkg,int32_t userId)208 [[maybe_unused]] static bool AddAbilityJumpRuleToBms(const std::string &callerPkg, const std::string &targetPkg,
209 int32_t userId)
210 {
211 if (callerPkg.empty() || targetPkg.empty()) {
212 TAG_LOGE(AAFwkTag::ABILITYMGR, "invalid inputs");
213 return false;
214 }
215 auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
216 if (bundleMgrHelper == nullptr) {
217 TAG_LOGE(AAFwkTag::ABILITYMGR, "GetBundleManagerHelper failed");
218 return false;
219 }
220 auto appControlMgr = bundleMgrHelper->GetAppControlProxy();
221 if (appControlMgr == nullptr) {
222 TAG_LOGE(AAFwkTag::ABILITYMGR, "Get appControlMgr failed");
223 return false;
224 }
225 int ret = IN_PROCESS_CALL(appControlMgr->ConfirmAppJumpControlRule(callerPkg, targetPkg, userId));
226 return ret == ERR_OK;
227 }
228
229 #ifdef WITH_DLP
HandleDlpApp(Want & want)230 [[maybe_unused]] static bool HandleDlpApp(Want &want)
231 {
232 const std::unordered_set<std::string> whiteListDlpSet = { BUNDLE_NAME_SELECTOR_DIALOG };
233 if (whiteListDlpSet.find(want.GetBundle()) != whiteListDlpSet.end()) {
234 TAG_LOGI(AAFwkTag::ABILITYMGR, "enter special app");
235 return false;
236 }
237
238 AppExecFwk::ElementName element = want.GetElement();
239 if (want.GetBoolParam(DLP_PARAMS_SANDBOX, false) && !element.GetBundleName().empty() &&
240 !element.GetAbilityName().empty()) {
241 want.SetElementName(DEFAULT_DEVICE_ID, DLP_BUNDLE_NAME, DLP_ABILITY_NAME, DLP_MODULE_NAME);
242 want.SetParam(DLP_PARAMS_BUNDLE_NAME, element.GetBundleName());
243 want.SetParam(DLP_PARAMS_MODULE_NAME, element.GetModuleName());
244 want.SetParam(DLP_PARAMS_ABILITY_NAME, element.GetAbilityName());
245 want.RemoveParam(DLP_PARAMS_SANDBOX);
246 return true;
247 }
248
249 return false;
250 }
251 #endif // WITH_DLP
252
IsStartIncludeAtomicService(const Want & want,const int32_t userId)253 [[maybe_unused]] static bool IsStartIncludeAtomicService(const Want &want, const int32_t userId)
254 {
255 auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
256 if (bundleMgrHelper == nullptr) {
257 TAG_LOGE(AAFwkTag::ABILITYMGR, "GetBundleManagerHelper failed");
258 return false;
259 }
260
261 std::string targetBundleName = want.GetBundle();
262 AppExecFwk::ApplicationInfo targetAppInfo;
263 bool getTargetResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(targetBundleName,
264 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, targetAppInfo));
265 if (!getTargetResult) {
266 TAG_LOGE(AAFwkTag::ABILITYMGR, "Get targetAppInfo failed");
267 return false;
268 }
269 if (targetAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
270 TAG_LOGI(AAFwkTag::ABILITYMGR, "target is atomic service");
271 return true;
272 }
273
274 int callerUid = IPCSkeleton::GetCallingUid();
275 std::string callerBundleName;
276 ErrCode err = IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerUid, callerBundleName));
277 if (err != ERR_OK) {
278 TAG_LOGE(AAFwkTag::ABILITYMGR, "Get bms failed");
279 return false;
280 }
281 AppExecFwk::ApplicationInfo callerAppInfo;
282 bool getCallerResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(callerBundleName,
283 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, callerAppInfo));
284 if (!getCallerResult) {
285 TAG_LOGE(AAFwkTag::ABILITYMGR, "Get callerAppInfo failed");
286 return false;
287 }
288 if (callerAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
289 TAG_LOGI(AAFwkTag::ABILITYMGR, "caller is atomic service");
290 return true;
291 }
292 return false;
293 }
294
RemoveShowModeKey(Want & want)295 [[maybe_unused]] static void RemoveShowModeKey(Want &want)
296 {
297 if (want.HasParameter(AAFwk::SCREEN_MODE_KEY)) {
298 want.RemoveParam(AAFwk::SCREEN_MODE_KEY);
299 }
300 }
301
IsSceneBoard(const std::string & bundleName,const std::string & AbilityName)302 [[maybe_unused]] static bool IsSceneBoard(const std::string &bundleName, const std::string &AbilityName)
303 {
304 return AbilityName == AbilityConfig::SCENEBOARD_ABILITY_NAME &&
305 bundleName == AbilityConfig::SCENEBOARD_BUNDLE_NAME;
306 }
307
RemoveWindowModeKey(Want & want)308 [[maybe_unused]] static void RemoveWindowModeKey(Want &want)
309 {
310 if (want.HasParameter(Want::PARAM_RESV_WINDOW_MODE)) {
311 want.RemoveParam(Want::PARAM_RESV_WINDOW_MODE);
312 }
313 }
314
RemoveInstanceKey(Want & want)315 [[maybe_unused]] static void RemoveInstanceKey(Want &want)
316 {
317 want.RemoveParam(Want::APP_INSTANCE_KEY);
318 want.RemoveParam(Want::CREATE_APP_INSTANCE_KEY);
319 }
320
RemoveWantKey(Want & want)321 [[maybe_unused]] static void RemoveWantKey(Want &want)
322 {
323 RemoveShowModeKey(want);
324 RemoveWindowModeKey(want);
325 }
326
CheckInstanceKey(const Want & want)327 [[maybe_unused]] static int32_t CheckInstanceKey(const Want &want)
328 {
329 auto instanceKey = want.GetStringParam(Want::APP_INSTANCE_KEY);
330 auto isCreating = want.GetBoolParam(Want::CREATE_APP_INSTANCE_KEY, false);
331 if (!instanceKey.empty() || isCreating) {
332 TAG_LOGE(AAFwkTag::ABILITYMGR, "Not support multi-instance");
333 return ERR_MULTI_INSTANCE_NOT_SUPPORTED;
334 }
335 return ERR_OK;
336 }
337
WantSetParameterWindowMode(Want & want,int32_t windowMode)338 [[maybe_unused]] static void WantSetParameterWindowMode(Want &want, int32_t windowMode)
339 {
340 want.SetParam(Want::PARAM_RESV_WINDOW_MODE, windowMode);
341 }
342
ProcessWindowMode(Want & want,uint32_t accessTokenId,int32_t windowMode)343 [[maybe_unused]] static void ProcessWindowMode(Want &want, uint32_t accessTokenId, int32_t windowMode)
344 {
345 if (PermissionVerification::GetInstance()->IsSystemAppCall()) {
346 want.SetParam(Want::PARAM_RESV_WINDOW_MODE, windowMode);
347 return;
348 }
349 if (IPCSkeleton::GetCallingTokenID() == accessTokenId && (
350 windowMode == AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_PRIMARY ||
351 windowMode == AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_SECONDARY)) {
352 want.SetParam(Want::PARAM_RESV_WINDOW_MODE, windowMode);
353 TAG_LOGI(AAFwkTag::ABILITYMGR, "set windownMode for inner application split-screen mode");
354 } else if (windowMode == AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_FULLSCREEN) {
355 want.SetParam(Want::PARAM_RESV_WINDOW_MODE, windowMode);
356 TAG_LOGI(AAFwkTag::ABILITYMGR, "set windownMode for full screen mode");
357 } else {
358 RemoveWindowModeKey(want);
359 }
360 }
361
StartAppgallery(const std::string & bundleName,const int requestCode,const int32_t userId,const std::string & action)362 [[maybe_unused]] static int StartAppgallery(const std::string &bundleName, const int requestCode, const int32_t userId,
363 const std::string &action)
364 {
365 std::string appGalleryBundleName;
366 auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
367 if (bundleMgrHelper == nullptr || !bundleMgrHelper->QueryAppGalleryBundleName(appGalleryBundleName)) {
368 TAG_LOGE(AAFwkTag::ABILITYMGR, "GetBundleManagerHelper or QueryAppGalleryBundleName failed");
369 appGalleryBundleName = MARKET_BUNDLE_NAME;
370 }
371
372 TAG_LOGD(AAFwkTag::ABILITYMGR, "appGalleryBundleName:%{public}s", appGalleryBundleName.c_str());
373
374 Want want;
375 want.SetElementName(appGalleryBundleName, "");
376 want.SetAction(action);
377 want.SetParam(MARKET_CROWD_TEST_BUNDLE_PARAM, bundleName);
378 return AbilityManagerClient::GetInstance()->StartAbility(want, requestCode, userId);
379 }
380
EdmErrorType(bool isEdm)381 inline ErrCode EdmErrorType(bool isEdm)
382 {
383 if (isEdm) {
384 return ERR_EDM_APP_CONTROLLED;
385 }
386 return ERR_APP_CONTROLLED;
387 }
388 } // namespace AbilityUtil
389 } // namespace AAFwk
390 } // namespace OHOS
391
392 #endif // OHOS_ABILITY_RUNTIME_ABILITY_UTIL_H
393