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