• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "implicit_start_processor.h"
16 
17 #include <string>
18 
19 #include "ability_manager_service.h"
20 #include "ability_util.h"
21 #include "app_gallery_enable_util.h"
22 #include "default_app_interface.h"
23 #include "errors.h"
24 #include "ecological_rule/ability_ecological_rule_mgr_service.h"
25 #include "event_report.h"
26 #include "hilog_wrapper.h"
27 #include "in_process_call_wrapper.h"
28 #include "parameters.h"
29 #include "scene_board_judgement.h"
30 #include "want.h"
31 
32 namespace OHOS {
33 namespace AAFwk {
34 const size_t IDENTITY_LIST_MAX_SIZE = 10;
35 const int32_t BROKER_UID = 5557;
36 
37 const std::string BLACK_ACTION_SELECT_DATA = "ohos.want.action.select";
38 const std::string STR_PHONE = "phone";
39 const std::string STR_DEFAULT = "default";
40 const std::string TYPE_ONLY_MATCH_WILDCARD = "reserved/wildcard";
41 const std::string SHOW_DEFAULT_PICKER_FLAG = "ohos.ability.params.showDefaultPicker";
42 const std::string PARAM_ABILITY_APPINFOS = "ohos.ability.params.appInfos";
43 const std::string ANCO_PENDING_REQUEST = "ancoPendingRequest";
44 const std::string SHELL_ASSISTANT_BUNDLENAME = "com.huawei.shell_assistant";
45 const int NFC_CALLER_UID = 1027;
46 const int NFC_QUERY_LENGTH = 2;
47 
48 const std::vector<std::string> ImplicitStartProcessor::blackList = {
49     std::vector<std::string>::value_type(BLACK_ACTION_SELECT_DATA),
50 };
51 
52 const std::unordered_set<AppExecFwk::ExtensionAbilityType> ImplicitStartProcessor::extensionWhiteList = {
53     AppExecFwk::ExtensionAbilityType::FORM,
54     AppExecFwk::ExtensionAbilityType::INPUTMETHOD,
55     AppExecFwk::ExtensionAbilityType::WALLPAPER,
56     AppExecFwk::ExtensionAbilityType::WINDOW,
57     AppExecFwk::ExtensionAbilityType::THUMBNAIL,
58     AppExecFwk::ExtensionAbilityType::PREVIEW
59 };
60 
IsImplicitStartAction(const Want & want)61 bool ImplicitStartProcessor::IsImplicitStartAction(const Want &want)
62 {
63     auto element = want.GetElement();
64     if (!element.GetAbilityName().empty()) {
65         return false;
66     }
67 
68     if (std::find(blackList.begin(), blackList.end(), want.GetAction()) == blackList.end()) {
69         HILOG_INFO("implicit start, the action is %{public}s", want.GetAction().data());
70         return true;
71     }
72 
73     return false;
74 }
75 
ImplicitStartAbility(AbilityRequest & request,int32_t userId)76 int ImplicitStartProcessor::ImplicitStartAbility(AbilityRequest &request, int32_t userId)
77 {
78     HILOG_INFO("implicit start ability by type: %{public}d", request.callType);
79     auto sysDialogScheduler = DelayedSingleton<SystemDialogScheduler>::GetInstance();
80     CHECK_POINTER_AND_RETURN(sysDialogScheduler, ERR_INVALID_VALUE);
81 
82     std::vector<DialogAppInfo> dialogAppInfos;
83     auto deviceType = OHOS::system::GetDeviceType();
84     HILOG_DEBUG("deviceType is %{public}s", deviceType.c_str());
85     auto ret = GenerateAbilityRequestByAction(userId, request, dialogAppInfos, deviceType, false);
86     if (ret != ERR_OK) {
87         HILOG_ERROR("generate ability request by action failed.");
88         return ret;
89     }
90 
91     auto identity = IPCSkeleton::ResetCallingIdentity();
92     auto startAbilityTask = [imp = shared_from_this(), request, userId, identity]
93         (const std::string& bundle, const std::string& abilityName) mutable {
94         HILOG_INFO("implicit start ability call back.");
95 
96         // reset calling indentity
97         IPCSkeleton::SetCallingIdentity(identity);
98 
99         AAFwk::Want targetWant = request.want;
100         targetWant.SetElementName(bundle, abilityName);
101         auto callBack = [imp, targetWant, request, userId]() -> int32_t {
102             return imp->ImplicitStartAbilityInner(targetWant, request, userId);
103         };
104         return imp->CallStartAbilityInner(userId, targetWant, callBack, request.callType);
105     };
106 
107     AAFwk::Want want;
108     auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
109     int32_t tokenId = request.want.GetIntParam(Want::PARAM_RESV_CALLER_TOKEN,
110         static_cast<int32_t>(IPCSkeleton::GetCallingTokenID()));
111     AddIdentity(tokenId, identity);
112     if (dialogAppInfos.size() == 0 && (deviceType == STR_PHONE || deviceType == STR_DEFAULT)) {
113         if ((request.want.GetFlags() & Want::FLAG_START_WITHOUT_TIPS) == Want::FLAG_START_WITHOUT_TIPS) {
114             HILOG_INFO("hint dialog doesn't generate.");
115             return ERR_IMPLICIT_START_ABILITY_FAIL;
116         }
117         ret = sysDialogScheduler->GetSelectorDialogWant(dialogAppInfos, request.want, request.callerToken);
118         if (ret != ERR_OK) {
119             HILOG_ERROR("GetSelectorDialogWant failed.");
120             return ret;
121         }
122         if (request.want.GetBoolParam("isCreateAppGallerySelector", false)) {
123             request.want.RemoveParam("isCreateAppGallerySelector");
124             NotifyCreateModalDialog(request, request.want, userId, dialogAppInfos);
125             return ERR_IMPLICIT_START_ABILITY_FAIL;
126         }
127         HILOG_ERROR("implicit query ability infos failed, show tips dialog.");
128         want = sysDialogScheduler->GetTipsDialogWant(request.callerToken);
129         abilityMgr->StartAbility(want);
130         return ERR_IMPLICIT_START_ABILITY_FAIL;
131     } else if (dialogAppInfos.size() == 0 && deviceType != STR_PHONE && deviceType != STR_DEFAULT) {
132         std::string type = MatchTypeAndUri(request.want);
133         ret = sysDialogScheduler->GetPcSelectorDialogWant(dialogAppInfos, request.want, type,
134             userId, request.callerToken);
135         if (ret != ERR_OK) {
136             HILOG_ERROR("GetPcSelectorDialogWant failed.");
137             return ret;
138         }
139         if (request.want.GetBoolParam("isCreateAppGallerySelector", false)) {
140             request.want.RemoveParam("isCreateAppGallerySelector");
141             return NotifyCreateModalDialog(request, request.want, userId, dialogAppInfos);
142         }
143         std::vector<DialogAppInfo> dialogAllAppInfos;
144         bool isMoreHapList = true;
145         ret = GenerateAbilityRequestByAction(userId, request, dialogAllAppInfos, deviceType, isMoreHapList);
146         if (ret != ERR_OK) {
147             HILOG_ERROR("generate ability request by action failed.");
148             return ret;
149         }
150         if (dialogAllAppInfos.size() == 0) {
151             if ((request.want.GetFlags() & Want::FLAG_START_WITHOUT_TIPS) == Want::FLAG_START_WITHOUT_TIPS) {
152                 HILOG_INFO("hint dialog doesn't generate.");
153                 return ERR_IMPLICIT_START_ABILITY_FAIL;
154             }
155             Want dialogWant = sysDialogScheduler->GetTipsDialogWant(request.callerToken);
156             abilityMgr->StartAbility(dialogWant);
157             return ERR_IMPLICIT_START_ABILITY_FAIL;
158         }
159         ret = sysDialogScheduler->GetPcSelectorDialogWant(dialogAllAppInfos, request.want,
160             TYPE_ONLY_MATCH_WILDCARD, userId, request.callerToken);
161         if (ret != ERR_OK) {
162             HILOG_ERROR("GetPcSelectorDialogWant failed.");
163             return ret;
164         }
165         ret = abilityMgr->StartAbility(request.want, request.callerToken);
166         // reset calling indentity
167         IPCSkeleton::SetCallingIdentity(identity);
168         return ret;
169     }
170 
171     //There is a default opening method add Only one application supports
172     bool defaultPicker = false;
173     defaultPicker = request.want.GetBoolParam(SHOW_DEFAULT_PICKER_FLAG, defaultPicker);
174     if (dialogAppInfos.size() == 1 && (!defaultPicker || deviceType == STR_PHONE || deviceType == STR_DEFAULT)) {
175         auto info = dialogAppInfos.front();
176         HILOG_INFO("ImplicitQueryInfos success, target ability: %{public}s", info.abilityName.data());
177         return IN_PROCESS_CALL(startAbilityTask(info.bundleName, info.abilityName));
178     }
179 
180     if (deviceType == STR_PHONE || deviceType == STR_DEFAULT) {
181         HILOG_INFO("ImplicitQueryInfos success, Multiple apps to choose.");
182         ret = sysDialogScheduler->GetSelectorDialogWant(dialogAppInfos, request.want, request.callerToken);
183         if (ret != ERR_OK) {
184             HILOG_ERROR("GetSelectorDialogWant failed.");
185             return ret;
186         }
187         if (request.want.GetBoolParam("isCreateAppGallerySelector", false)) {
188             request.want.RemoveParam("isCreateAppGallerySelector");
189             return NotifyCreateModalDialog(request, request.want, userId, dialogAppInfos);
190         }
191         ret = abilityMgr->StartAbilityAsCaller(request.want, request.callerToken, nullptr);
192         // reset calling indentity
193         IPCSkeleton::SetCallingIdentity(identity);
194         return ret;
195     }
196 
197     HILOG_INFO("ImplicitQueryInfos success, Multiple apps to choose in pc.");
198     std::string type = MatchTypeAndUri(request.want);
199 
200     ret = sysDialogScheduler->GetPcSelectorDialogWant(dialogAppInfos, request.want, type, userId, request.callerToken);
201     if (ret != ERR_OK) {
202         HILOG_ERROR("GetPcSelectorDialogWant failed.");
203         return ret;
204     }
205     if (request.want.GetBoolParam("isCreateAppGallerySelector", false)) {
206         request.want.RemoveParam("isCreateAppGallerySelector");
207         return NotifyCreateModalDialog(request, request.want, userId, dialogAppInfos);
208     }
209     ret = abilityMgr->StartAbilityAsCaller(request.want, request.callerToken, nullptr);
210     // reset calling indentity
211     IPCSkeleton::SetCallingIdentity(identity);
212     return ret;
213 }
214 
NotifyCreateModalDialog(AbilityRequest & abilityRequest,const Want & want,int32_t userId,std::vector<DialogAppInfo> & dialogAppInfos)215 int ImplicitStartProcessor::NotifyCreateModalDialog(AbilityRequest &abilityRequest, const Want &want, int32_t userId,
216     std::vector<DialogAppInfo> &dialogAppInfos)
217 {
218     auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
219     std::string dialogSessionId;
220     if (abilityMgr->GenerateDialogSessionRecord(abilityRequest, userId, dialogSessionId, dialogAppInfos, true)) {
221         HILOG_DEBUG("create dialog by ui extension");
222         return abilityMgr->CreateModalDialog(want, abilityRequest.callerToken, dialogSessionId);
223     }
224     HILOG_ERROR("create dialog by ui extension failed");
225     return INNER_ERR;
226 }
227 
MatchTypeAndUri(const AAFwk::Want & want)228 std::string ImplicitStartProcessor::MatchTypeAndUri(const AAFwk::Want &want)
229 {
230     std::string type = want.GetType();
231     if (type.empty()) {
232         auto uri = want.GetUriString();
233         auto suffixIndex = uri.rfind('.');
234         if (suffixIndex == std::string::npos) {
235             HILOG_ERROR("Get suffix failed, uri is %{public}s", uri.c_str());
236             return "";
237         }
238         type = uri.substr(suffixIndex);
239         if (type == ".dlp") {
240             auto suffixDlpIndex = uri.rfind('.', suffixIndex - 1);
241             if (suffixDlpIndex == std::string::npos) {
242                 HILOG_ERROR("Get suffix failed, uri is %{public}s", uri.c_str());
243                 return "";
244             }
245             type = uri.substr(suffixDlpIndex, suffixIndex - suffixDlpIndex);
246         }
247     }
248     return type;
249 }
250 
GenerateAbilityRequestByAction(int32_t userId,AbilityRequest & request,std::vector<DialogAppInfo> & dialogAppInfos,std::string & deviceType,bool isMoreHapList)251 int ImplicitStartProcessor::GenerateAbilityRequestByAction(int32_t userId,
252     AbilityRequest &request, std::vector<DialogAppInfo> &dialogAppInfos, std::string &deviceType, bool isMoreHapList)
253 {
254     HILOG_DEBUG("%{public}s.", __func__);
255     // get abilityinfos from bms
256     auto bundleMgrHelper = GetBundleManagerHelper();
257     CHECK_POINTER_AND_RETURN(bundleMgrHelper, GET_ABILITY_SERVICE_FAILED);
258     auto abilityInfoFlag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
259         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_SKILL_URI;
260     std::vector<AppExecFwk::AbilityInfo> abilityInfos;
261     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
262     bool withDefault = false;
263     withDefault = request.want.GetBoolParam(SHOW_DEFAULT_PICKER_FLAG, withDefault) ? false : true;
264 
265     if (IPCSkeleton::GetCallingUid() == NFC_CALLER_UID &&
266         !request.want.GetStringArrayParam(PARAM_ABILITY_APPINFOS).empty()) {
267         HILOG_INFO("The NFCNeed caller source is NFC.");
268         ImplicitStartProcessor::QueryBmsAppInfos(request, userId, dialogAppInfos);
269     }
270 
271     if (!IsCallFromAncoShellOrBroker(request.callerToken)) {
272         request.want.RemoveParam(ANCO_PENDING_REQUEST);
273     }
274     IN_PROCESS_CALL_WITHOUT_RET(bundleMgrHelper->ImplicitQueryInfos(
275         request.want, abilityInfoFlag, userId, withDefault, abilityInfos, extensionInfos));
276 
277     HILOG_INFO("ImplicitQueryInfos, abilityInfo size : %{public}zu, extensionInfos size: %{public}zu.",
278         abilityInfos.size(), extensionInfos.size());
279 
280     if (abilityInfos.size() + extensionInfos.size() > 1) {
281         HILOG_INFO("More than one target application, filter by erms");
282         bool ret = FilterAbilityList(request.want, abilityInfos, extensionInfos, userId);
283         if (!ret) {
284             HILOG_ERROR("FilterAbilityList failed");
285         }
286     }
287 
288     auto isExtension = request.callType == AbilityCallType::START_EXTENSION_TYPE;
289 
290     Want implicitwant;
291     std::string typeName = MatchTypeAndUri(request.want);
292 
293     implicitwant.SetAction(request.want.GetAction());
294     implicitwant.SetType(TYPE_ONLY_MATCH_WILDCARD);
295     std::vector<AppExecFwk::AbilityInfo> implicitAbilityInfos;
296     std::vector<AppExecFwk::ExtensionAbilityInfo> implicitExtensionInfos;
297     std::vector<std::string> infoNames;
298     if (deviceType != STR_PHONE && deviceType != STR_DEFAULT) {
299         IN_PROCESS_CALL_WITHOUT_RET(bundleMgrHelper->ImplicitQueryInfos(implicitwant, abilityInfoFlag, userId,
300             withDefault, implicitAbilityInfos, implicitExtensionInfos));
301         if (implicitAbilityInfos.size() != 0 && typeName != TYPE_ONLY_MATCH_WILDCARD) {
302             for (auto implicitAbilityInfo : implicitAbilityInfos) {
303                 infoNames.emplace_back(implicitAbilityInfo.bundleName + "#" +
304                     implicitAbilityInfo.moduleName + "#" + implicitAbilityInfo.name);
305             }
306         }
307     }
308     for (const auto &info : abilityInfos) {
309         AddInfoParam param = {
310             .info = info,
311             .userId = userId,
312             .isExtension = isExtension,
313             .isMoreHapList = isMoreHapList,
314             .withDefault = withDefault,
315             .deviceType = deviceType,
316             .typeName = typeName,
317             .infoNames = infoNames
318         };
319         AddAbilityInfoToDialogInfos(param, dialogAppInfos);
320     }
321 
322     for (const auto &info : extensionInfos) {
323         if (!isExtension || !CheckImplicitStartExtensionIsValid(request, info)) {
324             continue;
325         }
326         DialogAppInfo dialogAppInfo;
327         dialogAppInfo.abilityName = info.name;
328         dialogAppInfo.bundleName = info.bundleName;
329         dialogAppInfo.iconId = info.iconId;
330         dialogAppInfo.labelId = info.labelId;
331         dialogAppInfos.emplace_back(dialogAppInfo);
332     }
333 
334     return ERR_OK;
335 }
336 
QueryBmsAppInfos(AbilityRequest & request,int32_t userId,std::vector<DialogAppInfo> & dialogAppInfos)337 int ImplicitStartProcessor::QueryBmsAppInfos(AbilityRequest &request, int32_t userId,
338     std::vector<DialogAppInfo> &dialogAppInfos)
339 {
340     auto bundleMgrHelper = GetBundleManagerHelper();
341     std::vector<AppExecFwk::AbilityInfo> bmsApps;
342     auto abilityInfoFlag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
343         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_SKILL_URI;
344     std::vector<std::string> apps = request.want.GetStringArrayParam(PARAM_ABILITY_APPINFOS);
345     for (std::string appInfoStr : apps) {
346         AppExecFwk::AbilityInfo abilityInfo;
347         std::vector<std::string> appInfos = ImplicitStartProcessor::SplitStr(appInfoStr, '/');
348         if (appInfos.empty() || appInfos.size() != NFC_QUERY_LENGTH) {
349             continue;
350         }
351         std::string bundleName = appInfos[0];
352         std::string abilityName = appInfos[1];
353         std::string queryAbilityName = bundleName.append(abilityName);
354         Want want;
355         want.SetElementName(appInfos[0], queryAbilityName);
356 
357         IN_PROCESS_CALL_WITHOUT_RET(bundleMgrHelper->QueryAbilityInfo(want, abilityInfoFlag,
358             userId, abilityInfo));
359         if (!abilityInfo.name.empty() && !abilityInfo.bundleName.empty() && !abilityInfo.moduleName.empty()) {
360             bmsApps.emplace_back(abilityInfo);
361         }
362     }
363     if (!bmsApps.empty()) {
364         for (const auto &abilityInfo : bmsApps) {
365             DialogAppInfo dialogAppInfo;
366             dialogAppInfo.abilityName = abilityInfo.name;
367             dialogAppInfo.bundleName = abilityInfo.bundleName;
368             dialogAppInfo.moduleName = abilityInfo.moduleName;
369             dialogAppInfo.iconId = abilityInfo.iconId;
370             dialogAppInfo.labelId = abilityInfo.labelId;
371             dialogAppInfos.emplace_back(dialogAppInfo);
372         }
373     }
374     return ERR_OK;
375 }
376 
SplitStr(const std::string & str,char delimiter)377 std::vector<std::string> ImplicitStartProcessor::SplitStr(const std::string& str, char delimiter)
378 {
379     std::stringstream ss(str);
380     std::vector<std::string> result;
381     std::string s;
382     while (std::getline(ss, s, delimiter)) {
383         result.push_back(s);
384     }
385     return result;
386 }
387 
CheckImplicitStartExtensionIsValid(const AbilityRequest & request,const AppExecFwk::ExtensionAbilityInfo & extensionInfo)388 bool ImplicitStartProcessor::CheckImplicitStartExtensionIsValid(const AbilityRequest &request,
389     const AppExecFwk::ExtensionAbilityInfo &extensionInfo)
390 {
391     if (!request.want.GetElement().GetBundleName().empty()) {
392         return true;
393     }
394     HILOG_DEBUG("ImplicitStartExtension type: %{public}d.", static_cast<int32_t>(extensionInfo.type));
395     if (extensionWhiteList.find(extensionInfo.type) == extensionWhiteList.end()) {
396         HILOG_ERROR("The extension without UI is not allowed ImplicitStart");
397         return false;
398     }
399     return true;
400 }
401 
ImplicitStartAbilityInner(const Want & targetWant,const AbilityRequest & request,int32_t userId)402 int32_t ImplicitStartProcessor::ImplicitStartAbilityInner(const Want &targetWant,
403     const AbilityRequest &request, int32_t userId)
404 {
405     auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
406     CHECK_POINTER_AND_RETURN(abilityMgr, ERR_INVALID_VALUE);
407 
408     int32_t result = ERR_OK;
409     switch (request.callType) {
410         case AbilityCallType::START_OPTIONS_TYPE: {
411             StartOptions startOptions;
412             auto displayId = targetWant.GetIntParam(Want::PARAM_RESV_DISPLAY_ID, 0);
413             auto windowMode = targetWant.GetIntParam(Want::PARAM_RESV_WINDOW_MODE, 0);
414             startOptions.SetDisplayID(static_cast<int32_t>(displayId));
415             startOptions.SetWindowMode(static_cast<int32_t>(windowMode));
416             result = abilityMgr->StartAbility(
417                 targetWant, startOptions, request.callerToken, userId, request.requestCode);
418             break;
419         }
420         case AbilityCallType::START_SETTINGS_TYPE: {
421             CHECK_POINTER_AND_RETURN(request.startSetting, ERR_INVALID_VALUE);
422             result = abilityMgr->StartAbility(
423                 targetWant, *request.startSetting, request.callerToken, userId, request.requestCode);
424             break;
425         }
426         case AbilityCallType::START_EXTENSION_TYPE:
427             result = abilityMgr->StartExtensionAbility(
428                 targetWant, request.callerToken, userId, request.extensionType);
429             break;
430         default:
431             result = abilityMgr->StartAbilityWrap(
432                 targetWant, request.callerToken, request.requestCode, userId);
433             break;
434     }
435 
436     return result;
437 }
438 
CallStartAbilityInner(int32_t userId,const Want & want,const StartAbilityClosure & callBack,const AbilityCallType & callType)439 int ImplicitStartProcessor::CallStartAbilityInner(int32_t userId,
440     const Want &want, const StartAbilityClosure &callBack, const AbilityCallType &callType)
441 {
442     EventInfo eventInfo;
443     eventInfo.userId = userId;
444     eventInfo.bundleName = want.GetElement().GetBundleName();
445     eventInfo.moduleName = want.GetElement().GetModuleName();
446     eventInfo.abilityName = want.GetElement().GetAbilityName();
447 
448     if (callType == AbilityCallType::INVALID_TYPE) {
449         EventReport::SendAbilityEvent(EventName::START_ABILITY, HiSysEventType::BEHAVIOR, eventInfo);
450     }
451 
452     HILOG_INFO("ability:%{public}s, bundle:%{public}s", eventInfo.abilityName.c_str(), eventInfo.bundleName.c_str());
453 
454     auto ret = callBack();
455     if (ret != ERR_OK) {
456         eventInfo.errCode = ret;
457         if (callType == AbilityCallType::INVALID_TYPE) {
458             EventReport::SendAbilityEvent(EventName::START_ABILITY_ERROR, HiSysEventType::FAULT, eventInfo);
459         }
460     }
461     return ret;
462 }
463 
GetBundleManagerHelper()464 std::shared_ptr<AppExecFwk::BundleMgrHelper> ImplicitStartProcessor::GetBundleManagerHelper()
465 {
466     if (iBundleManagerHelper_ == nullptr) {
467         iBundleManagerHelper_ = AbilityUtil::GetBundleManagerHelper();
468     }
469     return iBundleManagerHelper_;
470 }
471 
GetDefaultAppProxy()472 sptr<AppExecFwk::IDefaultApp> ImplicitStartProcessor::GetDefaultAppProxy()
473 {
474     auto bundleMgrHelper = GetBundleManagerHelper();
475     if (bundleMgrHelper == nullptr) {
476         HILOG_ERROR("The bundleMgrHelper is nullptr.");
477         return nullptr;
478     }
479     auto defaultAppProxy = bundleMgrHelper->GetDefaultAppProxy();
480     if (defaultAppProxy == nullptr) {
481         HILOG_ERROR("The defaultAppProxy is nullptr.");
482         return nullptr;
483     }
484     return defaultAppProxy;
485 }
486 
FilterAbilityList(const Want & want,std::vector<AppExecFwk::AbilityInfo> & abilityInfos,std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos,int32_t userId)487 bool ImplicitStartProcessor::FilterAbilityList(const Want &want, std::vector<AppExecFwk::AbilityInfo> &abilityInfos,
488     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos, int32_t userId)
489 {
490     ErmsCallerInfo callerInfo;
491     GetEcologicalCallerInfo(want, callerInfo, userId);
492     int ret = IN_PROCESS_CALL(AbilityEcologicalRuleMgrServiceClient::GetInstance()->
493         EvaluateResolveInfos(want, callerInfo, 0, abilityInfos, extensionInfos));
494     if (ret != ERR_OK) {
495         HILOG_ERROR("Failed to evaluate resolve infos from erms.");
496         return false;
497     }
498     return true;
499 }
500 
GetEcologicalCallerInfo(const Want & want,ErmsCallerInfo & callerInfo,int32_t userId)501 void ImplicitStartProcessor::GetEcologicalCallerInfo(const Want &want, ErmsCallerInfo &callerInfo, int32_t userId)
502 {
503     callerInfo.packageName = want.GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
504     callerInfo.uid = want.GetIntParam(Want::PARAM_RESV_CALLER_UID, IPCSkeleton::GetCallingUid());
505     callerInfo.pid = want.GetIntParam(Want::PARAM_RESV_CALLER_PID, IPCSkeleton::GetCallingPid());
506     callerInfo.targetAppType = ErmsCallerInfo::TYPE_INVALID;
507     callerInfo.callerAppType = ErmsCallerInfo::TYPE_INVALID;
508 
509     auto bundleMgrHelper = GetBundleManagerHelper();
510     if (bundleMgrHelper == nullptr) {
511         HILOG_ERROR("Get Bubndle manager helper failed.");
512         return;
513     }
514 
515     std::string targetBundleName = want.GetBundle();
516     AppExecFwk::ApplicationInfo targetAppInfo;
517     bool getTargetResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(targetBundleName,
518         AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, targetAppInfo));
519     if (!getTargetResult) {
520         HILOG_ERROR("Get targetAppInfo failed.");
521     } else if (targetAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
522         HILOG_DEBUG("the target type  is atomic service");
523         callerInfo.targetAppType = ErmsCallerInfo::TYPE_ATOM_SERVICE;
524     } else if (targetAppInfo.bundleType == AppExecFwk::BundleType::APP) {
525         HILOG_DEBUG("the target type is app");
526         callerInfo.targetAppType = ErmsCallerInfo::TYPE_HARMONY_APP;
527     } else {
528         HILOG_DEBUG("the target type is invalid type");
529     }
530 
531     std::string callerBundleName;
532     ErrCode err = IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerInfo.uid, callerBundleName));
533     if (err != ERR_OK) {
534         HILOG_ERROR("Get callerBundleName failed.");
535         return;
536     }
537     AppExecFwk::ApplicationInfo callerAppInfo;
538     bool getCallerResult = IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(callerBundleName,
539         AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, callerAppInfo));
540     if (!getCallerResult) {
541         HILOG_DEBUG("Get callerAppInfo failed.");
542     } else if (callerAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
543         HILOG_DEBUG("the caller type  is atomic service");
544         callerInfo.callerAppType = ErmsCallerInfo::TYPE_ATOM_SERVICE;
545     } else if (callerAppInfo.bundleType == AppExecFwk::BundleType::APP) {
546         HILOG_DEBUG("the caller type is app");
547         callerInfo.callerAppType = ErmsCallerInfo::TYPE_HARMONY_APP;
548     } else {
549         HILOG_DEBUG("the caller type is invalid type");
550     }
551 }
552 
AddIdentity(int32_t tokenId,std::string identity)553 void ImplicitStartProcessor::AddIdentity(int32_t tokenId, std::string identity)
554 {
555     std::lock_guard guard(identityListLock_);
556     if (identityList_.size() == IDENTITY_LIST_MAX_SIZE) {
557         identityList_.pop_front();
558         identityList_.emplace_back(IdentityNode(tokenId, identity));
559         return;
560     }
561     identityList_.emplace_back(IdentityNode(tokenId, identity));
562 }
563 
ResetCallingIdentityAsCaller(int32_t tokenId)564 void ImplicitStartProcessor::ResetCallingIdentityAsCaller(int32_t tokenId)
565 {
566     std::lock_guard guard(identityListLock_);
567     for (auto it = identityList_.begin(); it != identityList_.end(); it++) {
568         if (it->tokenId == tokenId) {
569             IPCSkeleton::SetCallingIdentity(it->identity);
570             identityList_.erase(it);
571             return;
572         }
573     }
574 }
575 
AddAbilityInfoToDialogInfos(const AddInfoParam & param,std::vector<DialogAppInfo> & dialogAppInfos)576 void ImplicitStartProcessor::AddAbilityInfoToDialogInfos(const AddInfoParam &param,
577     std::vector<DialogAppInfo> &dialogAppInfos)
578 {
579     if (param.isExtension && param.info.type != AbilityType::EXTENSION) {
580         return;
581     }
582     if (param.deviceType != STR_PHONE && param.deviceType != STR_DEFAULT) {
583         bool isDefaultFlag = param.withDefault && IsExistDefaultApp(param.userId, param.typeName);
584         if (!param.isMoreHapList && !isDefaultFlag &&
585             std::find(param.infoNames.begin(), param.infoNames.end(),
586             (param.info.bundleName + "#" + param.info.moduleName + "#" + param.info.name)) != param.infoNames.end()) {
587             return;
588         }
589     }
590     DialogAppInfo dialogAppInfo;
591     dialogAppInfo.abilityName = param.info.name;
592     dialogAppInfo.bundleName = param.info.bundleName;
593     dialogAppInfo.moduleName = param.info.moduleName;
594     dialogAppInfo.iconId = param.info.iconId;
595     dialogAppInfo.labelId = param.info.labelId;
596     dialogAppInfos.emplace_back(dialogAppInfo);
597 }
598 
IsExistDefaultApp(int32_t userId,const std::string & typeName)599 bool ImplicitStartProcessor::IsExistDefaultApp(int32_t userId, const std::string &typeName)
600 {
601     auto defaultMgr = GetDefaultAppProxy();
602     AppExecFwk::BundleInfo bundleInfo;
603     ErrCode ret =
604         IN_PROCESS_CALL(defaultMgr->GetDefaultApplication(userId, typeName, bundleInfo));
605     if (ret != ERR_OK) {
606         return false;
607     }
608 
609     if (bundleInfo.abilityInfos.size() == 1) {
610         HILOG_INFO("find default ability.");
611         return true;
612     } else if (bundleInfo.extensionInfos.size() == 1) {
613         HILOG_INFO("find default extension.");
614         return true;
615     } else {
616         HILOG_INFO("GetDefaultApplication failed.");
617         return false;
618     }
619 }
620 
IsCallFromAncoShellOrBroker(const sptr<IRemoteObject> & token)621 bool ImplicitStartProcessor::IsCallFromAncoShellOrBroker(const sptr<IRemoteObject> &token)
622 {
623     auto callingUid = IPCSkeleton::GetCallingUid();
624     if (callingUid == BROKER_UID) {
625         return true;
626     }
627     auto abilityRecord = Token::GetAbilityRecordByToken(token);
628     if (!abilityRecord) {
629         return false;
630     }
631     std::string callerBundleName = abilityRecord->GetAbilityInfo().bundleName;
632     if (callerBundleName == SHELL_ASSISTANT_BUNDLENAME) {
633         return true;
634     }
635     return false;
636 }
637 }  // namespace AAFwk
638 }  // namespace OHOS
639