• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 #include "implicit_start_processor.h"
16 
17 #include "ability_manager_service.h"
18 #include "ability_util.h"
19 #include "app_utils.h"
20 #include "dialog_session_manager.h"
21 #include "ecological_rule/ability_ecological_rule_mgr_service.h"
22 #include "global_constant.h"
23 #include "hitrace_meter.h"
24 #include "start_ability_utils.h"
25 #include "startup_util.h"
26 #ifdef WITH_DLP
27 #include "dlp_file_kits.h"
28 #endif // WITH_DLP
29 #ifdef APP_DOMAIN_VERIFY_ENABLED
30 #include "app_domain_verify_mgr_client.h"
31 #endif // APP_DOMAIN_VERIFY_ENABLED
32 #ifdef SUPPORT_SCREEN
33 #include "utils/ability_permission_util.h"
34 #endif // SUPPORT_SCREEN
35 
36 namespace OHOS {
37 namespace AAFwk {
38 const size_t IDENTITY_LIST_MAX_SIZE = 10;
39 const size_t TRUSTLIST_MAX_SIZE = 50;
40 
41 const std::string BLACK_ACTION_SELECT_DATA = "ohos.want.action.select";
42 const std::string ACTION_VIEW = "ohos.want.action.viewData";
43 const std::string STR_PHONE = "phone";
44 const std::string STR_DEFAULT = "default";
45 const std::string TYPE_ONLY_MATCH_WILDCARD = "reserved/wildcard";
46 const std::string SHOW_DEFAULT_PICKER_FLAG = "ohos.ability.params.showDefaultPicker";
47 const std::string PARAM_ABILITY_APPINFOS = "ohos.ability.params.appInfos";
48 const std::string ANCO_PENDING_REQUEST = "ancoPendingRequest";
49 const int NFC_CALLER_UID = 1027;
50 const int NFC_QUERY_LENGTH = 2;
51 const std::string OPEN_LINK_APP_LINKING_ONLY = "appLinkingOnly";
52 const std::string HTTP_SCHEME_NAME = "http";
53 const std::string HTTPS_SCHEME_NAME = "https";
54 const std::string FILE_SCHEME_NAME = "file";
55 const std::string APP_CLONE_INDEX = "ohos.extra.param.key.appCloneIndex";
56 const std::string APP_LAUNCH_TRUSTLIST = "ohos.params.appLaunchTrustList";
57 constexpr const char* SUPPORT_ACTION_START_SELECTOR = "persist.sys.ability.support.action_start_selector";
58 
SendAbilityEvent(const EventName & eventName,HiSysEventType type,const EventInfo & eventInfo)59 void SendAbilityEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo)
60 {
61     auto instance_ = DelayedSingleton<AbilityManagerService>::GetInstance();
62     if (instance_ == nullptr) {
63         TAG_LOGE(AAFwkTag::ABILITYMGR, "instance null.");
64         return;
65     }
66     auto taskHandler = instance_->GetTaskHandler();
67     if (taskHandler == nullptr) {
68         TAG_LOGI(AAFwkTag::ABILITYMGR, "task handler null.");
69         return;
70     }
71     taskHandler->SubmitTask([eventName, type, eventInfo]() {
72         EventReport::SendAbilityEvent(eventName, type, eventInfo);
73     });
74 }
75 
IsExtensionInWhiteList(AppExecFwk::ExtensionAbilityType type)76 bool ImplicitStartProcessor::IsExtensionInWhiteList(AppExecFwk::ExtensionAbilityType type)
77 {
78     switch (type) {
79         case AppExecFwk::ExtensionAbilityType::FORM: return true;
80         case AppExecFwk::ExtensionAbilityType::INPUTMETHOD: return true;
81         case AppExecFwk::ExtensionAbilityType::WALLPAPER: return true;
82         case AppExecFwk::ExtensionAbilityType::WINDOW: return true;
83         case AppExecFwk::ExtensionAbilityType::THUMBNAIL: return true;
84         case AppExecFwk::ExtensionAbilityType::PREVIEW: return true;
85         default: return false;
86     }
87 }
88 
IsImplicitStartAction(const Want & want)89 bool ImplicitStartProcessor::IsImplicitStartAction(const Want &want)
90 {
91     auto element = want.GetElement();
92     if (!element.GetAbilityName().empty()) {
93         return false;
94     }
95 
96     if (want.GetIntParam(AAFwk::SCREEN_MODE_KEY, ScreenMode::IDLE_SCREEN_MODE) != ScreenMode::IDLE_SCREEN_MODE) {
97         TAG_LOGI(AAFwkTag::ABILITYMGR, "not use implicit startup process");
98         return false;
99     }
100 
101     if (want.GetAction() != BLACK_ACTION_SELECT_DATA) {
102         TAG_LOGI(AAFwkTag::ABILITYMGR, "implicit start, action:%{public}s", want.GetAction().data());
103         return true;
104     }
105 
106     return false;
107 }
108 
CheckImplicitCallPermission(const AbilityRequest & abilityRequest)109 int ImplicitStartProcessor::CheckImplicitCallPermission(const AbilityRequest& abilityRequest)
110 {
111     auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
112     CHECK_POINTER_AND_RETURN(abilityMgr, ERR_INVALID_VALUE);
113     bool isBackgroundCall = true;
114     if (abilityMgr->IsCallFromBackground(abilityRequest, isBackgroundCall) != ERR_OK) {
115         return ERR_INVALID_VALUE;
116     }
117     if (!isBackgroundCall) {
118         TAG_LOGD(AAFwkTag::ABILITYMGR, "hap not background");
119         return ERR_OK;
120     }
121     auto ret = AAFwk::PermissionVerification::GetInstance()->VerifyBackgroundCallPermission(isBackgroundCall);
122     if (!ret) {
123         TAG_LOGE(AAFwkTag::ABILITYMGR, "CheckImplicitCallPermission failed");
124         return CHECK_PERMISSION_FAILED;
125     }
126     return ERR_OK;
127 }
128 
ImplicitStartAbility(AbilityRequest & request,int32_t userId,int32_t windowMode,const std::string & replaceWantString,bool isAppCloneSelector)129 int ImplicitStartProcessor::ImplicitStartAbility(AbilityRequest &request, int32_t userId, int32_t windowMode,
130     const std::string &replaceWantString, bool isAppCloneSelector)
131 {
132     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
133     TAG_LOGI(AAFwkTag::ABILITYMGR, "implicit start ability by type: %{public}d", request.callType);
134     bool isNfcCalling = (IPCSkeleton::GetCallingUid() == NFC_CALLER_UID);
135     auto sysDialogScheduler = DelayedSingleton<SystemDialogScheduler>::GetInstance();
136     CHECK_POINTER_AND_RETURN(sysDialogScheduler, ERR_INVALID_VALUE);
137 
138     auto result = CheckImplicitCallPermission(request);
139     if (ERR_OK != result) {
140         return result;
141     }
142     std::vector<DialogAppInfo> dialogAppInfos;
143     request.want.RemoveParam(APP_CLONE_INDEX);
144     GenerateRequestParam genReqParam;
145     int32_t ret = ERR_OK;
146     if (isAppCloneSelector) {
147         ret = GenerateAbilityRequestByAppIndexes(userId, request, dialogAppInfos);
148     } else {
149         ret = GenerateAbilityRequestByAction(userId, request, dialogAppInfos, genReqParam);
150     }
151     if (ret != ERR_OK) {
152         TAG_LOGE(AAFwkTag::ABILITYMGR, "generate request failed");
153         return ret;
154     }
155     AbilityUtil::WantSetParameterWindowMode(request.want, windowMode);
156 
157     auto identity = IPCSkeleton::ResetCallingIdentity();
158     auto startAbilityTask = [imp = shared_from_this(), request, userId, identity]
159         (const std::string& bundle, const std::string& abilityName) mutable {
160         TAG_LOGI(AAFwkTag::ABILITYMGR, "callback");
161 
162         // reset calling indentity
163         IPCSkeleton::SetCallingIdentity(identity);
164 
165         AAFwk::Want targetWant = request.want;
166         targetWant.SetElementName(bundle, abilityName);
167         auto callBack = [imp, targetWant, request, userId]() -> int32_t {
168             return imp->ImplicitStartAbilityInner(targetWant, request, userId);
169         };
170         return imp->CallStartAbilityInner(userId, targetWant, callBack, request.callType);
171     };
172 
173     AAFwk::Want want;
174     auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
175     int32_t tokenId = request.want.GetIntParam(Want::PARAM_RESV_CALLER_TOKEN,
176         static_cast<int32_t>(IPCSkeleton::GetCallingTokenID()));
177     AddIdentity(tokenId, identity);
178 
179     if (!isAppCloneSelector && request.want.HasParameter(APP_LAUNCH_TRUSTLIST)) {
180         TrustlistIntersectionProcess(request, dialogAppInfos, userId);
181     }
182 
183     if (dialogAppInfos.size() == 0 &&
184         (request.want.GetFlags() & Want::FLAG_START_WITHOUT_TIPS) == Want::FLAG_START_WITHOUT_TIPS) {
185             TAG_LOGI(AAFwkTag::ABILITYMGR, "hint dialog generate fail");
186             return ERR_IMPLICIT_START_ABILITY_FAIL;
187         }
188     if (dialogAppInfos.size() == 0 && AppUtils::GetInstance().IsSelectorDialogDefaultPossion()) {
189         ret = sysDialogScheduler->GetSelectorDialogWant(dialogAppInfos, request.want, want, request.callerToken);
190         if (ret != ERR_OK) {
191             TAG_LOGE(AAFwkTag::ABILITYMGR, "GetSelectorDialogWant failed");
192             return ret;
193         }
194         if (want.GetBoolParam("isCreateAppGallerySelector", false)) {
195             want.RemoveParam("isCreateAppGallerySelector");
196             bool needGrantUriPermission = true;
197             DialogSessionManager::GetInstance().CreateImplicitSelectorModalDialog(request, want, userId,
198                 dialogAppInfos, needGrantUriPermission);
199             return ERR_IMPLICIT_START_ABILITY_FAIL;
200         }
201         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed, show tips dialog");
202         Want dialogWant = sysDialogScheduler->GetTipsDialogWant(request.callerToken);
203         abilityMgr->StartAbility(dialogWant);
204         return ERR_IMPLICIT_START_ABILITY_FAIL;
205     } else if (dialogAppInfos.size() == 0 && !AppUtils::GetInstance().IsSelectorDialogDefaultPossion()) {
206         std::string type = MatchTypeAndUri(request.want);
207         ret = sysDialogScheduler->GetPcSelectorDialogWant(dialogAppInfos, request.want, want, type,
208             userId, request.callerToken);
209         if (ret != ERR_OK) {
210             TAG_LOGE(AAFwkTag::ABILITYMGR, "GetPcSelectorDialogWant failed");
211             return ret;
212         }
213         if (want.GetBoolParam("isCreateAppGallerySelector", false)) {
214             want.RemoveParam("isCreateAppGallerySelector");
215             DialogSessionManager::GetInstance().CreateImplicitSelectorModalDialog(request, want, userId,
216                 dialogAppInfos);
217             return ERR_IMPLICIT_START_ABILITY_FAIL;
218         }
219         std::vector<DialogAppInfo> dialogAllAppInfos;
220         genReqParam.isMoreHapList = true;
221         ret = GenerateAbilityRequestByAction(userId, request, dialogAllAppInfos, genReqParam);
222         if (ret != ERR_OK) {
223             TAG_LOGE(AAFwkTag::ABILITYMGR, "request failed");
224             return ret;
225         }
226         if (dialogAllAppInfos.size() == 0) {
227             Want dialogWant = sysDialogScheduler->GetTipsDialogWant(request.callerToken);
228             abilityMgr->StartAbility(dialogWant);
229             return ERR_IMPLICIT_START_ABILITY_FAIL;
230         }
231         ret = sysDialogScheduler->GetPcSelectorDialogWant(dialogAllAppInfos, request.want, want,
232             TYPE_ONLY_MATCH_WILDCARD, userId, request.callerToken);
233         if (ret != ERR_OK) {
234             TAG_LOGE(AAFwkTag::ABILITYMGR, "GetPcSelectorDialogWant failed");
235             return ret;
236         }
237         ret = abilityMgr->StartAbility(request.want, request.callerToken);
238         // reset calling indentity
239         IPCSkeleton::SetCallingIdentity(identity);
240         return ret;
241     }
242 
243     //There is a default opening method add Only one application supports
244     bool defaultPicker = false;
245     defaultPicker = request.want.GetBoolParam(SHOW_DEFAULT_PICKER_FLAG, defaultPicker);
246     if (isNfcCalling && dialogAppInfos.size() == 1 && !dialogAppInfos[0].isAppLink) {
247         TAG_LOGI(AAFwkTag::ABILITYMGR, "ImplicitQueryInfos, is nfc calling.");
248         defaultPicker = true;
249     }
250     if (dialogAppInfos.size() == 1 && !defaultPicker) {
251         auto info = dialogAppInfos.front();
252         // Compatible with the action's sunset scene
253         if (!IsActionImplicitStart(request.want, genReqParam.findDefaultApp)) {
254             TAG_LOGI(AAFwkTag::ABILITYMGR, "ImplicitQueryInfos success,target ability: %{public}s",
255                 info.abilityName.data());
256             return IN_PROCESS_CALL(startAbilityTask(info.bundleName, info.abilityName));
257         }
258     }
259 
260     if (AppUtils::GetInstance().IsSelectorDialogDefaultPossion()) {
261         TAG_LOGI(AAFwkTag::ABILITYMGR, "ImplicitQueryInfos success, multiple apps available");
262         ret = sysDialogScheduler->GetSelectorDialogWant(dialogAppInfos, request.want, want, request.callerToken);
263         if (ret != ERR_OK) {
264             TAG_LOGE(AAFwkTag::ABILITYMGR, "GetSelectorDialogWant failed");
265             return ret;
266         }
267         if (want.GetBoolParam("isCreateAppGallerySelector", false)) {
268             want.RemoveParam("isCreateAppGallerySelector");
269             if (isAppCloneSelector) {
270                 return DialogSessionManager::GetInstance().CreateCloneSelectorModalDialog(request, want,
271                     userId, dialogAppInfos, replaceWantString);
272             }
273             bool needGrantUriPermission = true;
274             return DialogSessionManager::GetInstance().CreateImplicitSelectorModalDialog(request,
275                 want, userId, dialogAppInfos, needGrantUriPermission);
276         }
277         ret = abilityMgr->ImplicitStartAbilityAsCaller(request.want, request.callerToken, nullptr);
278         // reset calling indentity
279         IPCSkeleton::SetCallingIdentity(identity);
280         return ret;
281     }
282 
283     TAG_LOGI(AAFwkTag::ABILITYMGR, "ImplicitQueryInfos success, multiple apps available in pc");
284     std::string type = MatchTypeAndUri(request.want);
285 
286     ret = sysDialogScheduler->GetPcSelectorDialogWant(dialogAppInfos, request.want, want,
287         type, userId, request.callerToken);
288     if (ret != ERR_OK) {
289         TAG_LOGE(AAFwkTag::ABILITYMGR, "GetPcSelectorDialogWant failed");
290         return ret;
291     }
292     if (want.GetBoolParam("isCreateAppGallerySelector", false)) {
293         want.RemoveParam("isCreateAppGallerySelector");
294         if (isAppCloneSelector) {
295             return DialogSessionManager::GetInstance().CreateCloneSelectorModalDialog(request, want, userId,
296                 dialogAppInfos, replaceWantString);
297         }
298         return DialogSessionManager::GetInstance().CreateImplicitSelectorModalDialog(request, want, userId,
299             dialogAppInfos);
300     }
301     ret = abilityMgr->ImplicitStartAbilityAsCaller(request.want, request.callerToken, nullptr);
302     // reset calling indentity
303     IPCSkeleton::SetCallingIdentity(identity);
304     return ret;
305 }
306 
MatchTypeAndUri(const AAFwk::Want & want)307 std::string ImplicitStartProcessor::MatchTypeAndUri(const AAFwk::Want &want)
308 {
309     std::string type = want.GetType();
310     if (type.empty()) {
311         auto uri = want.GetUriString();
312         auto suffixIndex = uri.rfind('.');
313         if (suffixIndex == std::string::npos) {
314             TAG_LOGE(AAFwkTag::ABILITYMGR, "failed, uri: %{public}s", uri.c_str());
315             return "";
316         }
317         type = uri.substr(suffixIndex);
318 #ifdef WITH_DLP
319         if (Security::DlpPermission::DlpFileKits::IsDlpFileBySuffix(type)) {
320             auto suffixDlpIndex = uri.rfind('.', suffixIndex - 1);
321             if (suffixDlpIndex == std::string::npos) {
322                 TAG_LOGE(AAFwkTag::ABILITYMGR, "failed, uri: %{public}s", uri.c_str());
323                 return "";
324             }
325             type = uri.substr(suffixDlpIndex, suffixIndex - suffixDlpIndex);
326         }
327 #endif // WITH_DLP
328     }
329     return type;
330 }
331 
ProcessLinkType(std::vector<AppExecFwk::AbilityInfo> & abilityInfos)332 static void ProcessLinkType(std::vector<AppExecFwk::AbilityInfo> &abilityInfos)
333 {
334     bool appLinkingExist = false;
335     bool defaultAppExist = false;
336     if (!abilityInfos.size()) {
337         return;
338     }
339     for (const auto &info : abilityInfos) {
340         if (info.linkType == AppExecFwk::LinkType::APP_LINK) {
341             appLinkingExist = true;
342         }
343         if (info.linkType == AppExecFwk::LinkType::DEFAULT_APP) {
344             defaultAppExist = true;
345         }
346     }
347     if (!appLinkingExist && !defaultAppExist) {
348         return;
349     }
350     TAG_LOGI(AAFwkTag::ABILITYMGR, "open applink first");
351     for (auto it = abilityInfos.begin(); it != abilityInfos.end();) {
352         if (it->linkType == AppExecFwk::LinkType::APP_LINK) {
353             it++;
354             continue;
355         }
356         if (it->linkType == AppExecFwk::LinkType::DEFAULT_APP && appLinkingExist) {
357             TAG_LOGD(AAFwkTag::ABILITYMGR, "%{public}s default deleted.", it->name.c_str());
358             it = abilityInfos.erase(it);
359             continue;
360         }
361         if (it->linkType == AppExecFwk::LinkType::DEEP_LINK) {
362             TAG_LOGD(AAFwkTag::ABILITYMGR, "%{public}s deleted.", it->name.c_str());
363             it = abilityInfos.erase(it);
364             continue;
365         }
366         it++;
367     }
368 }
369 
OnlyKeepReserveApp(std::vector<AppExecFwk::AbilityInfo> & abilityInfos,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos,const AbilityRequest & request)370 void ImplicitStartProcessor::OnlyKeepReserveApp(std::vector<AppExecFwk::AbilityInfo> &abilityInfos,
371     std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos, const AbilityRequest &request)
372 {
373     if (!request.uriReservedFlag) {
374         return;
375     }
376     if (extensionInfos.size() > 0) {
377         extensionInfos.clear();
378     }
379 
380     for (auto it = abilityInfos.begin(); it != abilityInfos.end();) {
381         if (it->bundleName == request.reservedBundleName) {
382             it++;
383             continue;
384         } else {
385             TAG_LOGI(AAFwkTag::ABILITYMGR, "reserve App %{public}s dismatch with bundleName %{public}s",
386                 request.reservedBundleName.c_str(), it->bundleName.c_str());
387             it = abilityInfos.erase(it);
388         }
389     }
390 }
391 
392 #ifdef APP_DOMAIN_VERIFY_ENABLED
IsApplinkExist(const std::vector<AppExecFwk::AbilityInfo> & abilityInfos)393 static bool IsApplinkExist(const std::vector<AppExecFwk::AbilityInfo> &abilityInfos)
394 {
395     if (!abilityInfos.size()) {
396         return false;
397     }
398     for (const auto &info : abilityInfos) {
399         if (info.linkType == AppExecFwk::LinkType::APP_LINK) {
400             return true;
401         }
402     }
403     return false;
404 }
405 
NeedQueryFromAG(const AbilityRequest & request,bool applinkExist)406 bool ImplicitStartProcessor::NeedQueryFromAG(const AbilityRequest &request, bool applinkExist)
407 {
408     bool isOpenLink = request.want.HasParameter(OPEN_LINK_APP_LINKING_ONLY);
409     if (!isOpenLink) {
410         return false;
411     }
412     bool appLinkingOnly = request.want.GetBoolParam(OPEN_LINK_APP_LINKING_ONLY, false);
413     if (appLinkingOnly) {
414         return false;
415     }
416     std::string linkUriScheme = request.want.GetUri().GetScheme();
417     if (linkUriScheme != HTTPS_SCHEME_NAME && linkUriScheme != HTTP_SCHEME_NAME) {
418         return false;
419     }
420     if (applinkExist) {
421         return false;
422     }
423     return true;
424 }
425 
ImplicitStartAG(int32_t userId,AbilityRequest & request,std::vector<DialogAppInfo> & dialogAppInfos,GenerateRequestParam & genReqParam,bool & queryAGSuccess)426 int ImplicitStartProcessor::ImplicitStartAG(int32_t userId, AbilityRequest &request,
427     std::vector<DialogAppInfo> &dialogAppInfos, GenerateRequestParam &genReqParam, bool &queryAGSuccess)
428 {
429     TAG_LOGD(AAFwkTag::ABILITYMGR, "%{public}s", __func__);
430     std::string linkUri = request.want.GetUri().ToString();
431     Want agWant;
432     auto ret = 0;
433     ret = OHOS::AppDomainVerify::AppDomainVerifyMgrClient::GetInstance()->QueryAppDetailsWant(linkUri, agWant);
434     if (ret != 0) {
435         TAG_LOGE(AAFwkTag::ABILITYMGR, "QueryAppDetailsWant failed, ret:%{public}d", ret);
436         return ret;
437     }
438     queryAGSuccess = true;
439     request.want = agWant;
440     genReqParam.fromImplicitStartAG = true;
441     return GenerateAbilityRequestByAction(userId, request, dialogAppInfos, genReqParam);
442 }
443 #endif // APP_DOMAIN_VERIFY_ENABLED
444 
GenerateAbilityRequestByAction(int32_t userId,AbilityRequest & request,std::vector<DialogAppInfo> & dialogAppInfos,GenerateRequestParam & genReqParam)445 int ImplicitStartProcessor::GenerateAbilityRequestByAction(int32_t userId, AbilityRequest &request,
446     std::vector<DialogAppInfo> &dialogAppInfos, GenerateRequestParam &genReqParam)
447 {
448     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
449     TAG_LOGD(AAFwkTag::ABILITYMGR, "%{public}s.", __func__);
450     // get abilityinfos from bms
451     auto bundleMgrHelper = GetBundleManagerHelper();
452     CHECK_POINTER_AND_RETURN(bundleMgrHelper, GET_ABILITY_SERVICE_FAILED);
453     auto abilityInfoFlag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
454         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_SKILL_URI
455         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION;
456     std::vector<AppExecFwk::AbilityInfo> abilityInfos;
457     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
458     bool withDefault = false;
459     withDefault = request.want.GetBoolParam(SHOW_DEFAULT_PICKER_FLAG, withDefault) ? false : true;
460     bool appLinkingOnly = false;
461     bool isOpenLink = false;
462     isOpenLink = request.want.HasParameter(OPEN_LINK_APP_LINKING_ONLY);
463     appLinkingOnly = request.want.GetBoolParam(OPEN_LINK_APP_LINKING_ONLY, false);
464 
465     if (IPCSkeleton::GetCallingUid() == NFC_CALLER_UID &&
466         !request.want.GetStringArrayParam(PARAM_ABILITY_APPINFOS).empty()) {
467         TAG_LOGI(AAFwkTag::ABILITYMGR, "caller source: NFC");
468         QueryBmsAppInfos(request, userId, dialogAppInfos);
469     }
470 
471     if (!StartAbilityUtils::IsCallFromAncoShellOrBroker(request.callerToken)) {
472         request.want.RemoveParam(ANCO_PENDING_REQUEST);
473     }
474 
475     if (appLinkingOnly) {
476         abilityInfoFlag = static_cast<uint32_t>(abilityInfoFlag) |
477             static_cast<uint32_t>(AppExecFwk::GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_APP_LINKING);
478     }
479 
480     if (request.uriReservedFlag) {
481         abilityInfoFlag = static_cast<uint32_t>(abilityInfoFlag) |
482             static_cast<uint32_t>(AppExecFwk::GetAbilityInfoFlag::GET_ABILITY_INFO_ONLY_SYSTEM_APP);
483     }
484 
485     if (isOpenLink) {
486         std::string linkUriScheme = request.want.GetUri().GetScheme();
487         if (linkUriScheme == HTTPS_SCHEME_NAME || linkUriScheme == HTTP_SCHEME_NAME) {
488             request.want.SetAction(ACTION_VIEW);
489         }
490     }
491 
492     IN_PROCESS_CALL_WITHOUT_RET(bundleMgrHelper->ImplicitQueryInfos(
493         request.want, abilityInfoFlag, userId, withDefault, abilityInfos, extensionInfos,
494         genReqParam.findDefaultApp));
495 
496     OnlyKeepReserveApp(abilityInfos, extensionInfos, request);
497     if (isOpenLink && extensionInfos.size() > 0) {
498         TAG_LOGI(AAFwkTag::ABILITYMGR, "clear extensionInfos");
499         extensionInfos.clear();
500     }
501     TAG_LOGI(AAFwkTag::ABILITYMGR,
502         "ImplicitQueryInfos, abilityInfo size : %{public}zu, extensionInfos size: %{public}zu", abilityInfos.size(),
503         extensionInfos.size());
504 
505     if (appLinkingOnly && abilityInfos.size() == 0) {
506         TAG_LOGE(AAFwkTag::ABILITYMGR, "not match app");
507         return ERR_IMPLICIT_START_ABILITY_FAIL;
508     }
509 
510 #ifdef APP_DOMAIN_VERIFY_ENABLED
511     ErrCode result = ERR_OK;
512     bool applinkExist = IsApplinkExist(abilityInfos);
513     bool queryAGSuccess = false;
514     if (!genReqParam.fromImplicitStartAG && NeedQueryFromAG(request, applinkExist)) {
515         result = static_cast<int>(ImplicitStartAG(userId, request, dialogAppInfos, genReqParam, queryAGSuccess));
516         if (queryAGSuccess) {
517             return result;
518         }
519     }
520 #endif // APP_DOMAIN_VERIFY_ENABLED
521 
522     if (!appLinkingOnly) {
523         ProcessLinkType(abilityInfos);
524     }
525 
526 #ifdef WITH_DLP
527     if (request.want.GetBoolParam(AbilityUtil::DLP_PARAMS_SANDBOX, false)) {
528         Security::DlpPermission::DlpFileKits::ConvertAbilityInfoWithSupportDlp(request.want, abilityInfos);
529         extensionInfos.clear();
530     }
531 #endif // WITH_DLP
532 
533     if (abilityInfos.size() + extensionInfos.size() > 1) {
534         TAG_LOGI(AAFwkTag::ABILITYMGR, "filter applications by erms");
535         bool ret = FilterAbilityList(request.want, abilityInfos, extensionInfos, userId);
536         FindAppClone(abilityInfos, extensionInfos, genReqParam.isAppCloneSelector);
537         if (!ret) {
538             TAG_LOGE(AAFwkTag::ABILITYMGR, "FilterAbilityList failed");
539         }
540     }
541 
542     auto isExtension = request.callType == AbilityCallType::START_EXTENSION_TYPE;
543 
544     Want implicitwant;
545     std::string typeName = MatchTypeAndUri(request.want);
546 
547     implicitwant.SetAction(request.want.GetAction());
548     implicitwant.SetType(TYPE_ONLY_MATCH_WILDCARD);
549     std::vector<AppExecFwk::AbilityInfo> implicitAbilityInfos;
550     std::vector<AppExecFwk::ExtensionAbilityInfo> implicitExtensionInfos;
551     std::vector<std::string> infoNames;
552     if (!AppUtils::GetInstance().IsSelectorDialogDefaultPossion() && genReqParam.isMoreHapList) {
553         IN_PROCESS_CALL_WITHOUT_RET(bundleMgrHelper->ImplicitQueryInfos(implicitwant, abilityInfoFlag, userId,
554             withDefault, implicitAbilityInfos, implicitExtensionInfos, genReqParam.findDefaultApp));
555         if (implicitAbilityInfos.size() != 0 && typeName != TYPE_ONLY_MATCH_WILDCARD) {
556             for (auto implicitAbilityInfo : implicitAbilityInfos) {
557                 infoNames.emplace_back(implicitAbilityInfo.bundleName + "#" +
558                     implicitAbilityInfo.moduleName + "#" + implicitAbilityInfo.name);
559             }
560         }
561     }
562 
563     if (abilityInfos.size() == 1) {
564         auto skillUri =  abilityInfos.front().skillUri;
565         SetTargetLinkInfo(skillUri, request.want);
566         if (abilityInfos.front().linkType == AppExecFwk::LinkType::APP_LINK) {
567             EventInfo eventInfo;
568             eventInfo.bundleName = abilityInfos.front().bundleName;
569             eventInfo.callerBundleName = request.want.GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
570             eventInfo.uri = request.want.GetUriString();
571             SendAbilityEvent(EventName::START_ABILITY_BY_APP_LINKING, HiSysEventType::BEHAVIOR, eventInfo);
572         }
573     }
574 
575     {
576         HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "for (const auto &info : abilityInfos)");
577         bool isExistDefaultApp = IsExistDefaultApp(userId, typeName);
578         for (const auto &info : abilityInfos) {
579             AddInfoParam param = {
580                 .info = info,
581                 .userId = userId,
582                 .isExtension = isExtension,
583                 .isMoreHapList = genReqParam.isMoreHapList,
584                 .withDefault = withDefault,
585                 .typeName = typeName,
586                 .infoNames = infoNames,
587                 .isExistDefaultApp = isExistDefaultApp
588             };
589             AddAbilityInfoToDialogInfos(param, dialogAppInfos);
590         }
591     }
592 
593     for (const auto &info : extensionInfos) {
594         if (!isExtension || !CheckImplicitStartExtensionIsValid(request, info)) {
595             continue;
596         }
597         DialogAppInfo dialogAppInfo;
598         dialogAppInfo.abilityName = info.name;
599         dialogAppInfo.bundleName = info.bundleName;
600         dialogAppInfo.abilityIconId = info.iconId;
601         dialogAppInfo.abilityLabelId = info.labelId;
602         dialogAppInfo.bundleIconId = info.applicationInfo.iconId;
603         dialogAppInfo.bundleLabelId = info.applicationInfo.labelId;
604         dialogAppInfo.visible = info.visible;
605         dialogAppInfo.appIndex = info.applicationInfo.appIndex;
606         dialogAppInfo.multiAppMode = info.applicationInfo.multiAppMode;
607         dialogAppInfos.emplace_back(dialogAppInfo);
608     }
609 
610     return ERR_OK;
611 }
612 
GenerateAbilityRequestByAppIndexes(int32_t userId,AbilityRequest & request,std::vector<DialogAppInfo> & dialogAppInfos)613 int ImplicitStartProcessor::GenerateAbilityRequestByAppIndexes(int32_t userId, AbilityRequest &request,
614     std::vector<DialogAppInfo> &dialogAppInfos)
615 {
616     auto appIndexes = StartAbilityUtils::GetCloneAppIndexes(request.want.GetBundle(), userId);
617     if (appIndexes.size() > AbilityRuntime::GlobalConstant::MAX_APP_CLONE_INDEX) {
618         TAG_LOGE(AAFwkTag::ABILITYMGR, "too large appIndexes");
619         return ERR_INVALID_VALUE;
620     }
621     auto bms = GetBundleManagerHelper();
622     CHECK_POINTER_AND_RETURN(bms, GET_ABILITY_SERVICE_FAILED);
623     auto abilityInfoFlag = static_cast<uint32_t>(AbilityRuntime::StartupUtil::BuildAbilityInfoFlag()) |
624         static_cast<uint32_t>(AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_SKILL);
625     std::vector<AppExecFwk::AbilityInfo> abilityInfos;
626     abilityInfos.emplace_back(request.abilityInfo);
627     for (auto &appIndex: appIndexes) {
628         AppExecFwk::AbilityInfo abilityInfo;
629         TAG_LOGD(AAFwkTag::ABILITYMGR,
630             "abilityName: %{public}s, appIndex: %{public}d, userId: %{public}d",
631             request.want.GetElement().GetAbilityName().c_str(), appIndex, userId);
632         IN_PROCESS_CALL_WITHOUT_RET(bms->QueryCloneAbilityInfo(request.want.GetElement(), abilityInfoFlag, appIndex,
633             abilityInfo, userId));
634         if (abilityInfo.name.empty() || abilityInfo.bundleName.empty()) {
635             int32_t ret = FindExtensionInfo(request.want, abilityInfoFlag, userId, appIndex, abilityInfo);
636             if (ret != ERR_OK) {
637                 TAG_LOGE(AAFwkTag::ABILITYMGR, "query info failed");
638                 return ret;
639             }
640         }
641         abilityInfos.emplace_back(abilityInfo);
642     }
643     for (const auto &info : abilityInfos) {
644         DialogAppInfo dialogAppInfo;
645         dialogAppInfo.abilityName = info.name;
646         dialogAppInfo.bundleName = info.bundleName;
647         dialogAppInfo.moduleName = info.moduleName;
648         dialogAppInfo.abilityIconId = info.iconId;
649         dialogAppInfo.abilityLabelId = info.labelId;
650         dialogAppInfo.bundleIconId = info.applicationInfo.iconId;
651         dialogAppInfo.bundleLabelId = info.applicationInfo.labelId;
652         dialogAppInfo.visible = info.visible;
653         dialogAppInfo.appIndex = info.applicationInfo.appIndex;
654         dialogAppInfo.multiAppMode = info.applicationInfo.multiAppMode;
655         dialogAppInfos.emplace_back(dialogAppInfo);
656     }
657     return ERR_OK;
658 }
659 
FindExtensionInfo(const Want & want,int32_t flags,int32_t userId,int32_t appIndex,AppExecFwk::AbilityInfo & abilityInfo)660 int ImplicitStartProcessor::FindExtensionInfo(const Want &want, int32_t flags, int32_t userId,
661     int32_t appIndex, AppExecFwk::AbilityInfo &abilityInfo)
662 {
663     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
664     auto bms = GetBundleManagerHelper();
665     CHECK_POINTER_AND_RETURN(bms, GET_ABILITY_SERVICE_FAILED);
666     AppExecFwk::ExtensionAbilityInfo extensionInfo;
667     TAG_LOGD(AAFwkTag::ABILITYMGR,
668         "abilityName: %{public}s, appIndex: %{public}d, userId: %{public}d",
669         want.GetElement().GetAbilityName().c_str(), appIndex, userId);
670     IN_PROCESS_CALL_WITHOUT_RET(bms->QueryCloneExtensionAbilityInfoWithAppIndex(want.GetElement(),
671         flags, appIndex, extensionInfo, userId));
672     if (extensionInfo.bundleName.empty() || extensionInfo.name.empty()) {
673         TAG_LOGE(AAFwkTag::ABILITYMGR, "extensionInfo empty.");
674         return RESOLVE_ABILITY_ERR;
675     }
676     if (AbilityRuntime::StartupUtil::IsSupportAppClone(extensionInfo.type)) {
677         AbilityRuntime::StartupUtil::InitAbilityInfoFromExtension(extensionInfo, abilityInfo);
678         return ERR_OK;
679     }
680     return ERR_APP_CLONE_INDEX_INVALID;
681 }
682 
QueryBmsAppInfos(AbilityRequest & request,int32_t userId,std::vector<DialogAppInfo> & dialogAppInfos)683 int ImplicitStartProcessor::QueryBmsAppInfos(AbilityRequest &request, int32_t userId,
684     std::vector<DialogAppInfo> &dialogAppInfos)
685 {
686     auto bundleMgrHelper = GetBundleManagerHelper();
687     std::vector<AppExecFwk::AbilityInfo> bmsApps;
688     auto abilityInfoFlag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
689         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_SKILL_URI
690         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION;
691     std::vector<std::string> apps = request.want.GetStringArrayParam(PARAM_ABILITY_APPINFOS);
692     for (std::string appInfoStr : apps) {
693         AppExecFwk::AbilityInfo abilityInfo;
694         std::vector<std::string> appInfos = ImplicitStartProcessor::SplitStr(appInfoStr, '/');
695         if (appInfos.empty() || appInfos.size() != NFC_QUERY_LENGTH) {
696             continue;
697         }
698         std::string bundleName = appInfos[0];
699         std::string abilityName = appInfos[1];
700         std::string queryAbilityName = bundleName.append(abilityName);
701         Want want;
702         want.SetElementName(appInfos[0], queryAbilityName);
703 
704         TAG_LOGD(AAFwkTag::ABILITYMGR,
705             "abilityName: %{public}s, userId: %{public}d", want.GetElement().GetAbilityName().c_str(), userId);
706         IN_PROCESS_CALL_WITHOUT_RET(bundleMgrHelper->QueryAbilityInfo(want, abilityInfoFlag,
707             userId, abilityInfo));
708         if (!abilityInfo.name.empty() && !abilityInfo.bundleName.empty() && !abilityInfo.moduleName.empty()) {
709             bmsApps.emplace_back(abilityInfo);
710         }
711     }
712     if (!bmsApps.empty()) {
713         for (const auto &abilityInfo : bmsApps) {
714             DialogAppInfo dialogAppInfo;
715             dialogAppInfo.abilityName = abilityInfo.name;
716             dialogAppInfo.bundleName = abilityInfo.bundleName;
717             dialogAppInfo.moduleName = abilityInfo.moduleName;
718             dialogAppInfo.abilityIconId = abilityInfo.iconId;
719             dialogAppInfo.abilityLabelId = abilityInfo.labelId;
720             dialogAppInfo.bundleIconId = abilityInfo.applicationInfo.iconId;
721             dialogAppInfo.bundleLabelId = abilityInfo.applicationInfo.labelId;
722             dialogAppInfo.visible = abilityInfo.visible;
723             dialogAppInfo.appIndex = abilityInfo.applicationInfo.appIndex;
724             dialogAppInfo.multiAppMode = abilityInfo.applicationInfo.multiAppMode;
725             dialogAppInfos.emplace_back(dialogAppInfo);
726         }
727     }
728     return ERR_OK;
729 }
730 
SplitStr(const std::string & str,char delimiter)731 std::vector<std::string> ImplicitStartProcessor::SplitStr(const std::string& str, char delimiter)
732 {
733     std::stringstream ss(str);
734     std::vector<std::string> result;
735     std::string s;
736     while (std::getline(ss, s, delimiter)) {
737         result.push_back(s);
738     }
739     return result;
740 }
741 
CheckImplicitStartExtensionIsValid(const AbilityRequest & request,const AppExecFwk::ExtensionAbilityInfo & extensionInfo)742 bool ImplicitStartProcessor::CheckImplicitStartExtensionIsValid(const AbilityRequest &request,
743     const AppExecFwk::ExtensionAbilityInfo &extensionInfo)
744 {
745     if (!request.want.GetElement().GetBundleName().empty()) {
746         return true;
747     }
748     TAG_LOGD(
749         AAFwkTag::ABILITYMGR, "ImplicitStartExtension type: %{public}d.", static_cast<int32_t>(extensionInfo.type));
750     if (!IsExtensionInWhiteList(extensionInfo.type)) {
751         TAG_LOGE(AAFwkTag::ABILITYMGR, "ImplicitStart not allowed");
752         return false;
753     }
754     return true;
755 }
756 
ImplicitStartAbilityInner(const Want & targetWant,const AbilityRequest & request,int32_t userId)757 int32_t ImplicitStartProcessor::ImplicitStartAbilityInner(const Want &targetWant,
758     const AbilityRequest &request, int32_t userId)
759 {
760     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
761     auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
762     CHECK_POINTER_AND_RETURN(abilityMgr, ERR_INVALID_VALUE);
763 
764     int32_t result = ERR_OK;
765     switch (request.callType) {
766         case AbilityCallType::START_OPTIONS_TYPE: {
767             StartOptions startOptions;
768             auto displayId = targetWant.GetIntParam(Want::PARAM_RESV_DISPLAY_ID, 0);
769             auto windowMode = targetWant.GetIntParam(Want::PARAM_RESV_WINDOW_MODE, 0);
770             startOptions.SetDisplayID(static_cast<int32_t>(displayId));
771             startOptions.SetWindowMode(static_cast<int32_t>(windowMode));
772             result = abilityMgr->ImplicitStartAbility(
773                 targetWant, startOptions, request.callerToken, userId, request.requestCode);
774             break;
775         }
776         case AbilityCallType::START_SETTINGS_TYPE: {
777             CHECK_POINTER_AND_RETURN(request.startSetting, ERR_INVALID_VALUE);
778             result = abilityMgr->ImplicitStartAbility(
779                 targetWant, *request.startSetting, request.callerToken, userId, request.requestCode);
780             break;
781         }
782         case AbilityCallType::START_EXTENSION_TYPE:
783             result = abilityMgr->ImplicitStartExtensionAbility(
784                 targetWant, request.callerToken, userId, request.extensionType);
785             break;
786         default:
787             result = abilityMgr->StartAbilityWrap(
788                 targetWant, request.callerToken, request.requestCode, false, userId, false, 0, false, true);
789             break;
790     }
791 
792     return result;
793 }
794 
CallStartAbilityInner(int32_t userId,const Want & want,const StartAbilityClosure & callBack,const AbilityCallType & callType)795 int ImplicitStartProcessor::CallStartAbilityInner(int32_t userId,
796     const Want &want, const StartAbilityClosure &callBack, const AbilityCallType &callType)
797 {
798     EventInfo eventInfo;
799     eventInfo.userId = userId;
800     eventInfo.bundleName = want.GetElement().GetBundleName();
801     eventInfo.moduleName = want.GetElement().GetModuleName();
802     eventInfo.abilityName = want.GetElement().GetAbilityName();
803 
804     if (callType == AbilityCallType::INVALID_TYPE) {
805         SendAbilityEvent(EventName::START_ABILITY, HiSysEventType::BEHAVIOR, eventInfo);
806     }
807 
808     TAG_LOGI(AAFwkTag::ABILITYMGR, "ability:%{public}s, bundle:%{public}s", eventInfo.abilityName.c_str(),
809         eventInfo.bundleName.c_str());
810 
811     auto ret = callBack();
812     if (ret != ERR_OK) {
813         eventInfo.errCode = ret;
814         if (callType == AbilityCallType::INVALID_TYPE) {
815             SendAbilityEvent(EventName::START_ABILITY_ERROR, HiSysEventType::FAULT, eventInfo);
816         }
817     }
818     return ret;
819 }
820 
GetBundleManagerHelper()821 std::shared_ptr<AppExecFwk::BundleMgrHelper> ImplicitStartProcessor::GetBundleManagerHelper()
822 {
823     if (iBundleManagerHelper_ == nullptr) {
824         iBundleManagerHelper_ = AbilityUtil::GetBundleManagerHelper();
825     }
826     return iBundleManagerHelper_;
827 }
828 
GetDefaultAppProxy()829 sptr<AppExecFwk::IDefaultApp> ImplicitStartProcessor::GetDefaultAppProxy()
830 {
831     auto bundleMgrHelper = GetBundleManagerHelper();
832     if (bundleMgrHelper == nullptr) {
833         TAG_LOGE(AAFwkTag::ABILITYMGR, "null bundleMgrHelper");
834         return nullptr;
835     }
836     auto defaultAppProxy = bundleMgrHelper->GetDefaultAppProxy();
837     if (defaultAppProxy == nullptr) {
838         TAG_LOGE(AAFwkTag::ABILITYMGR, "null defaultAppProxy");
839         return nullptr;
840     }
841     return defaultAppProxy;
842 }
843 
FilterAbilityList(const Want & want,std::vector<AppExecFwk::AbilityInfo> & abilityInfos,std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos,int32_t userId)844 bool ImplicitStartProcessor::FilterAbilityList(const Want &want, std::vector<AppExecFwk::AbilityInfo> &abilityInfos,
845     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos, int32_t userId)
846 {
847     ErmsCallerInfo callerInfo;
848     GetEcologicalCallerInfo(want, callerInfo, userId);
849     int ret = IN_PROCESS_CALL(AbilityEcologicalRuleMgrServiceClient::GetInstance()->
850         EvaluateResolveInfos(want, callerInfo, 0, abilityInfos, extensionInfos));
851     if (ret != ERR_OK) {
852         TAG_LOGE(AAFwkTag::ABILITYMGR, "resolve infos failed");
853         return false;
854     }
855     return true;
856 }
857 
GetEcologicalCallerInfo(const Want & want,ErmsCallerInfo & callerInfo,int32_t userId)858 void ImplicitStartProcessor::GetEcologicalCallerInfo(const Want &want, ErmsCallerInfo &callerInfo, int32_t userId)
859 {
860     callerInfo.packageName = want.GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
861     callerInfo.uid = want.GetIntParam(Want::PARAM_RESV_CALLER_UID, IPCSkeleton::GetCallingUid());
862     callerInfo.pid = want.GetIntParam(Want::PARAM_RESV_CALLER_PID, IPCSkeleton::GetCallingPid());
863     callerInfo.targetAppType = ErmsCallerInfo::TYPE_INVALID;
864     callerInfo.callerAppType = ErmsCallerInfo::TYPE_INVALID;
865 
866     auto bundleMgrHelper = GetBundleManagerHelper();
867     if (bundleMgrHelper == nullptr) {
868         TAG_LOGE(AAFwkTag::ABILITYMGR, "bundleMgrHelper empty");
869         return;
870     }
871 
872     std::string targetBundleName = want.GetBundle();
873     AppExecFwk::ApplicationInfo targetAppInfo;
874     TAG_LOGD(AAFwkTag::ABILITYMGR,
875         "targetBundleName: %{public}s, userId: %{public}d", targetBundleName.c_str(), userId);
876     bool getTargetResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(targetBundleName,
877         AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, targetAppInfo));
878     if (!getTargetResult) {
879         TAG_LOGE(AAFwkTag::ABILITYMGR, "get targetAppInfo failed");
880     } else if (targetAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
881         TAG_LOGD(AAFwkTag::ABILITYMGR, "the target type  is atomic service");
882         callerInfo.targetAppType = ErmsCallerInfo::TYPE_ATOM_SERVICE;
883     } else if (targetAppInfo.bundleType == AppExecFwk::BundleType::APP) {
884         TAG_LOGD(AAFwkTag::ABILITYMGR, "the target type is app");
885         callerInfo.targetAppType = ErmsCallerInfo::TYPE_HARMONY_APP;
886     } else {
887         TAG_LOGD(AAFwkTag::ABILITYMGR, "the target type is invalid type");
888     }
889 
890     std::string callerBundleName;
891     ErrCode err = IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerInfo.uid, callerBundleName));
892     if (err != ERR_OK) {
893         TAG_LOGE(AAFwkTag::ABILITYMGR, "callerBundleName empty");
894         return;
895     }
896     AppExecFwk::ApplicationInfo callerAppInfo;
897     bool getCallerResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(callerBundleName,
898         AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, callerAppInfo));
899     if (!getCallerResult) {
900         TAG_LOGD(AAFwkTag::ABILITYMGR, "get callerAppInfo failed");
901     } else if (callerAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
902         TAG_LOGD(AAFwkTag::ABILITYMGR, "the caller type  is atomic service");
903         callerInfo.callerAppType = ErmsCallerInfo::TYPE_ATOM_SERVICE;
904     } else if (callerAppInfo.bundleType == AppExecFwk::BundleType::APP) {
905         TAG_LOGD(AAFwkTag::ABILITYMGR, "the caller type is app");
906         callerInfo.callerAppType = ErmsCallerInfo::TYPE_HARMONY_APP;
907     } else {
908         TAG_LOGD(AAFwkTag::ABILITYMGR, "the caller type is invalid type");
909     }
910 }
911 
AddIdentity(int32_t tokenId,std::string identity)912 void ImplicitStartProcessor::AddIdentity(int32_t tokenId, std::string identity)
913 {
914     std::lock_guard guard(identityListLock_);
915     if (identityList_.size() == IDENTITY_LIST_MAX_SIZE) {
916         identityList_.pop_front();
917         identityList_.emplace_back(IdentityNode(tokenId, identity));
918         return;
919     }
920     identityList_.emplace_back(IdentityNode(tokenId, identity));
921 }
922 
ResetCallingIdentityAsCaller(int32_t tokenId,bool flag)923 void ImplicitStartProcessor::ResetCallingIdentityAsCaller(int32_t tokenId, bool flag)
924 {
925     std::lock_guard guard(identityListLock_);
926     for (auto it = identityList_.begin(); it != identityList_.end(); it++) {
927         if (it->tokenId == tokenId) {
928             IPCSkeleton::SetCallingIdentity(it->identity);
929             if (flag) {
930                 identityList_.erase(it);
931             }
932             return;
933         }
934     }
935 }
936 
RemoveIdentity(int32_t tokenId)937 void ImplicitStartProcessor::RemoveIdentity(int32_t tokenId)
938 {
939     std::lock_guard guard(identityListLock_);
940     for (auto it = identityList_.begin(); it != identityList_.end(); it++) {
941         if (it->tokenId == tokenId) {
942             identityList_.erase(it);
943             return;
944         }
945     }
946 }
947 
AddAbilityInfoToDialogInfos(const AddInfoParam & param,std::vector<DialogAppInfo> & dialogAppInfos)948 void ImplicitStartProcessor::AddAbilityInfoToDialogInfos(const AddInfoParam &param,
949     std::vector<DialogAppInfo> &dialogAppInfos)
950 {
951     if (param.isExtension && param.info.type != AbilityType::EXTENSION) {
952         return;
953     }
954     if (!AppUtils::GetInstance().IsSelectorDialogDefaultPossion()) {
955         bool isDefaultFlag = param.withDefault && param.isExistDefaultApp;
956         if (!param.isMoreHapList && !isDefaultFlag &&
957             std::find(param.infoNames.begin(), param.infoNames.end(),
958             (param.info.bundleName + "#" + param.info.moduleName + "#" + param.info.name)) != param.infoNames.end()) {
959             return;
960         }
961     }
962     DialogAppInfo dialogAppInfo;
963     dialogAppInfo.abilityName = param.info.name;
964     dialogAppInfo.bundleName = param.info.bundleName;
965     dialogAppInfo.moduleName = param.info.moduleName;
966     dialogAppInfo.abilityIconId = param.info.iconId;
967     dialogAppInfo.abilityLabelId = param.info.labelId;
968     dialogAppInfo.bundleIconId = param.info.applicationInfo.iconId;
969     dialogAppInfo.bundleLabelId = param.info.applicationInfo.labelId;
970     dialogAppInfo.visible = param.info.visible;
971     dialogAppInfo.appIndex = param.info.applicationInfo.appIndex;
972     dialogAppInfo.multiAppMode = param.info.applicationInfo.multiAppMode;
973     dialogAppInfo.isAppLink = (param.info.linkType == AppExecFwk::LinkType::APP_LINK);
974     dialogAppInfos.emplace_back(dialogAppInfo);
975 }
976 
IsExistDefaultApp(int32_t userId,const std::string & typeName)977 bool ImplicitStartProcessor::IsExistDefaultApp(int32_t userId, const std::string &typeName)
978 {
979     auto defaultMgr = GetDefaultAppProxy();
980     CHECK_POINTER_AND_RETURN_LOG(defaultMgr, false, "defaultMgr null");
981     AppExecFwk::BundleInfo bundleInfo;
982     TAG_LOGD(AAFwkTag::ABILITYMGR, "userId: %{public}d, typeName: %{public}s", userId, typeName.c_str());
983     ErrCode ret =
984         IN_PROCESS_CALL(defaultMgr->GetDefaultApplication(userId, typeName, bundleInfo));
985     if (ret != ERR_OK) {
986         return false;
987     }
988 
989     if (bundleInfo.abilityInfos.size() == 1) {
990         TAG_LOGI(AAFwkTag::ABILITYMGR, "find default ability");
991         return true;
992     } else if (bundleInfo.extensionInfos.size() == 1) {
993         TAG_LOGI(AAFwkTag::ABILITYMGR, "find default extension");
994         return true;
995     } else {
996         TAG_LOGI(AAFwkTag::ABILITYMGR, "getDefaultApplication failed");
997         return false;
998     }
999 }
1000 
SetTargetLinkInfo(const std::vector<AppExecFwk::SkillUriForAbilityAndExtension> & skillUri,Want & want)1001 void ImplicitStartProcessor::SetTargetLinkInfo(const std::vector<AppExecFwk::SkillUriForAbilityAndExtension> &skillUri,
1002     Want &want)
1003 {
1004     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1005     for (const auto& iter : skillUri) {
1006         if (iter.isMatch) {
1007             want.RemoveParam("send_to_erms_targetLinkFeature");
1008             want.SetParam("send_to_erms_targetLinkFeature", iter.linkFeature);
1009             want.RemoveParam("send_to_erms_targetLinkType");
1010             if (want.GetBoolParam(OPEN_LINK_APP_LINKING_ONLY, false)) {
1011                 want.SetParam("send_to_erms_targetLinkType", AbilityCallerInfo::LINK_TYPE_UNIVERSAL_LINK);
1012             } else if ((iter.scheme == "https" || iter.scheme == "http") &&
1013                 want.GetAction().compare(ACTION_VIEW) == 0) {
1014                 want.SetParam("send_to_erms_targetLinkType", AbilityCallerInfo::LINK_TYPE_WEB_LINK);
1015             } else {
1016                 want.SetParam("send_to_erms_targetLinkType", AbilityCallerInfo::LINK_TYPE_DEEP_LINK);
1017             }
1018         }
1019     }
1020 }
1021 
IsActionImplicitStart(const Want & want,bool findDeafultApp)1022 bool ImplicitStartProcessor::IsActionImplicitStart(const Want &want, bool findDeafultApp)
1023 {
1024     std::string supportStart = OHOS::system::GetParameter(SUPPORT_ACTION_START_SELECTOR, "false");
1025     if (supportStart == "false") {
1026         return false;
1027     }
1028 
1029     if (findDeafultApp) {
1030         return false;
1031     }
1032 
1033     std::string bundleName = "";
1034     if (DeepLinkReserveConfig::GetInstance().IsLinkReserved(want.GetUriString(),
1035         bundleName)) {
1036         return false;
1037     }
1038 
1039     if (want.GetUriString() == "" ||
1040         (want.GetUri().GetScheme() != "file" && want.GetUri().GetScheme() != "content" &&
1041         want.GetUri().GetScheme() != "mailto")) {
1042         return false;
1043     }
1044 
1045     if (want.GetElement().GetBundleName() != "") {
1046         return false;
1047     }
1048 
1049     return true;
1050 }
1051 
FindAppClone(std::vector<AppExecFwk::AbilityInfo> & abilityInfos,std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos,bool & isAppCloneSelector)1052 int32_t ImplicitStartProcessor::FindAppClone(std::vector<AppExecFwk::AbilityInfo> &abilityInfos,
1053     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos, bool &isAppCloneSelector)
1054 {
1055     if (abilityInfos.size() > 0 && extensionInfos.size() > 0) {
1056         return ERR_OK;
1057     }
1058 
1059     bool isExitAbilityAppClone = FindAbilityAppClone(abilityInfos);
1060     bool isExitExtensionAppClone = FindExtensionAppClone(extensionInfos);
1061     if ((abilityInfos.size() == 0 && FindExtensionAppClone(extensionInfos)) ||
1062         (extensionInfos.size() == 0 && FindAbilityAppClone(abilityInfos))) {
1063         isAppCloneSelector = true;
1064     }
1065 
1066     return ERR_OK;
1067 }
1068 
FindAbilityAppClone(std::vector<AppExecFwk::AbilityInfo> & abilityInfos)1069 bool ImplicitStartProcessor::FindAbilityAppClone(std::vector<AppExecFwk::AbilityInfo> &abilityInfos)
1070 {
1071     if (abilityInfos.size() <= 1) {
1072         return false;
1073     }
1074     std::string appCloneBundleName = "";
1075     std::string appCloneAbilityName = "";
1076     for (const auto &iter : abilityInfos) {
1077         if (appCloneBundleName == "" && appCloneAbilityName == "") {
1078             appCloneBundleName = iter.bundleName;
1079             appCloneAbilityName = iter.name;
1080         }
1081         if (iter.bundleName != appCloneBundleName || iter.name != appCloneAbilityName) {
1082             return false;
1083         }
1084     }
1085     return true;
1086 }
1087 
TrustlistIntersectionProcess(const AbilityRequest & request,std::vector<DialogAppInfo> & dialogAppInfos,int32_t userId)1088 void ImplicitStartProcessor::TrustlistIntersectionProcess(const AbilityRequest &request,
1089     std::vector<DialogAppInfo> &dialogAppInfos, int32_t userId)
1090 {
1091     if (request.want.GetUri().GetScheme() == FILE_SCHEME_NAME) {
1092         return;
1093     }
1094     std::vector<std::string> appLaunchTrustlist = request.want.GetStringArrayParam(APP_LAUNCH_TRUSTLIST);
1095     if (appLaunchTrustlist.size() <= 0) {
1096         return;
1097     }
1098     std::vector<DialogAppInfo> dialogIntersectionAppInfos;
1099     if (appLaunchTrustlist.size() > TRUSTLIST_MAX_SIZE) {
1100         TAG_LOGW(AAFwkTag::ABILITYMGR, "trustlist size is %{public}zu, more than %{public}zu.",
1101             appLaunchTrustlist.size(), TRUSTLIST_MAX_SIZE);
1102         appLaunchTrustlist.resize(TRUSTLIST_MAX_SIZE);
1103     }
1104     auto bundleMgrHelper = GetBundleManagerHelper();
1105     if (bundleMgrHelper == nullptr) {
1106         TAG_LOGE(AAFwkTag::ABILITYMGR, "bundleMgrHelper empty");
1107         return;
1108     }
1109     for (const auto &info : dialogAppInfos) {
1110         AppExecFwk::BundleInfo curBundleInfo;
1111         bool ret = bundleMgrHelper->GetBundleInfo(info.bundleName,
1112             AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, curBundleInfo, userId);
1113         if (!ret) {
1114             TAG_LOGW(AAFwkTag::ABILITYMGR,
1115                 "get bundleInfo failed, bundleName:%{public}s, userId:%{public}d.",
1116                 info.bundleName.c_str(), userId);
1117             continue;
1118         }
1119         std::string curAppIdentifier = curBundleInfo.signatureInfo.appIdentifier;
1120         auto it = std::find(appLaunchTrustlist.begin(), appLaunchTrustlist.end(), curAppIdentifier);
1121         if (it != appLaunchTrustlist.end()) {
1122             dialogIntersectionAppInfos.emplace_back(info);
1123         }
1124     }
1125     TAG_LOGI(AAFwkTag::ABILITYMGR,
1126         "trustlist size: %{public}zu, Intersection size: %{public}zu.",
1127             appLaunchTrustlist.size(), dialogIntersectionAppInfos.size());
1128     if (dialogIntersectionAppInfos.size() <= 1) {
1129         dialogAppInfos = dialogIntersectionAppInfos;
1130     }
1131     return;
1132 }
1133 
FindExtensionAppClone(std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)1134 bool ImplicitStartProcessor::FindExtensionAppClone(std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
1135 {
1136     if (extensionInfos.size() <= 1) {
1137         return false;
1138     }
1139     std::string appCloneBundleName = "";
1140     std::string appCloneAbilityName = "";
1141     for (const auto &iter : extensionInfos) {
1142         if (appCloneBundleName == "" && appCloneAbilityName == "") {
1143             appCloneBundleName = iter.bundleName;
1144             appCloneAbilityName = iter.name;
1145         }
1146         if (iter.bundleName != appCloneBundleName || iter.name != appCloneAbilityName) {
1147             return false;
1148         }
1149     }
1150     return true;
1151 }
1152 }  // namespace AAFwk
1153 }  // namespace OHOS
1154