• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #ifndef OHOS_ABILITY_RUNTIME_ABILITY_CONNECT_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_CONNECT_MANAGER_H
18 
19 #include <atomic>
20 #include <deque>
21 #include <list>
22 #include <map>
23 #include <string>
24 #include <unordered_map>
25 #include "cpp/mutex.h"
26 
27 #include "ability_cache_manager.h"
28 #include "ability_connect_callback_interface.h"
29 #include "task_handler_wrap.h"
30 #include "event_handler_wrap.h"
31 #include "ability_record.h"
32 #include "ability_running_info.h"
33 #include "event_report.h"
34 #include "extension_config.h"
35 #include "extension_running_info.h"
36 #include "connection_record.h"
37 #include "element_name.h"
38 #include "ui_extension/ui_extension_ability_connect_info.h"
39 #include "extension_record_manager.h"
40 #include "want.h"
41 #include "iremote_object.h"
42 #include "nocopyable.h"
43 
44 namespace OHOS {
45 namespace AAFwk {
46 using OHOS::AppExecFwk::AbilityType;
47 using UIExtensionAbilityConnectInfo = AbilityRuntime::UIExtensionAbilityConnectInfo;
48 using UIExtensionAbilityConnectManager = AbilityRuntime::ExtensionRecordManager;
49 using UIExtensionSessionInfo = AbilityRuntime::UIExtensionSessionInfo;
50 /**
51  * @class AbilityConnectManager
52  * AbilityConnectManager provides a facility for managing service ability connection.
53  */
54 class AbilityConnectManager : public std::enable_shared_from_this<AbilityConnectManager> {
55 public:
56     using ConnectMapType = std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<ConnectionRecord>>>;
57     using ServiceMapType = std::map<std::string, std::shared_ptr<AbilityRecord>>;
58     using ConnectListType = std::list<std::shared_ptr<ConnectionRecord>>;
59     using RecipientMapType = std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>>;
60     using UIExtWindowMapValType = std::pair<std::weak_ptr<AbilityRecord>, sptr<SessionInfo>>;
61     using UIExtensionMapType = std::map<sptr<IRemoteObject>, UIExtWindowMapValType>;
62     using WindowExtMapValType = std::pair<uint32_t, sptr<SessionInfo>>;
63     using WindowExtensionMapType = std::map<sptr<IRemoteObject>, WindowExtMapValType>;
64 
65     explicit AbilityConnectManager(int userId);
66     virtual ~AbilityConnectManager();
67 
68     struct LoadAbilityContext {
69         std::shared_ptr<AbilityRuntime::LoadParam> loadParam;
70         std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo;
71         std::shared_ptr<AppExecFwk::ApplicationInfo> appInfo;
72         std::shared_ptr<AAFwk::Want> want;
73     };
74 
75     /**
76      * StartAbility with request.
77      *
78      * @param abilityRequest, the request of the service ability to start.
79      * @return Returns ERR_OK on success, others on failure.
80      */
81     int32_t StartAbility(const AbilityRequest &abilityRequest);
82 
83     /**
84      * TerminateAbility with token and result want.
85      *
86      * @param token, the token of service type's ability to terminate.
87      * @return Returns ERR_OK on success, others on failure.
88      */
89     int TerminateAbility(const sptr<IRemoteObject> &token);
90 
91     /**
92      * StopServiceAbility with request.
93      *
94      * @param abilityRequest, request.
95      * @return Returns ERR_OK on success, others on failure.
96      */
97     int StopServiceAbility(const AbilityRequest &abilityRequest);
98 
99     /**
100      * ConnectAbilityLocked, connect session with service ability.
101      *
102      * @param abilityRequest, Special want for service type's ability.
103      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
104      * @param callerToken, caller ability token.
105      * @param sessionInfo the extension session info of the ability to connect.
106      * @param connectInfo the connect info.
107      * @return Returns ERR_OK on success, others on failure.
108      */
109     int32_t ConnectAbilityLocked(const AbilityRequest &abilityRequest, const sptr<IAbilityConnection> &connect,
110         const sptr<IRemoteObject> &callerToken, sptr<SessionInfo> sessionInfo = nullptr,
111         sptr<UIExtensionAbilityConnectInfo> connectInfo = nullptr);
112 
113     /**
114      * PreloadUIExtensionAbilityInner, preload uiextension ability.
115      *
116      * @param abilityRequest, Special want for service type's ability.
117      * @param hostBundleName, the caller application bundle name.
118      * @return Returns ERR_OK on success, others on failure.
119      */
120     int PreloadUIExtensionAbilityInner(const AbilityRequest &abilityRequest, std::string &hostBundleName,
121         int32_t hostPid = AAFwk::DEFAULT_INVAL_VALUE);
122 
123     /**
124      * PreloadUIExtensionAbilityLocked, preload uiextension ability.
125      *
126      * @param abilityRequest, Special want for service type's ability.
127      * @param hostBundleName, the caller application bundle name.
128      * @return Returns ERR_OK on success, others on failure.
129      */
130     int PreloadUIExtensionAbilityLocked(const AbilityRequest &abilityRequest, std::string &hostBundleName,
131         int32_t hostPid = AAFwk::DEFAULT_INVAL_VALUE);
132 
133     /**
134      * Query preload uiextension record.
135      *
136      * @param element, The uiextension ElementName.
137      * @param moduleName, The uiextension moduleName.
138      * @param hostBundleName, The uiextension caller hostBundleName.
139      * @param recordNum, The returned count of uiextension.
140      * @return Returns ERR_OK on success, others on failure.
141      */
142     int QueryPreLoadUIExtensionRecordInner(const AppExecFwk::ElementName &element,
143                                            const std::string &moduleName,
144                                            const std::string &hostBundleName,
145                                            int32_t &recordNum);
146 
147     /**
148      * UnloadUIExtensionAbility, unload uiextension ability.
149      *
150      * @param abilityRecord, uiextension ability record.
151      * @param hostBundleName, the caller application bundle name.
152      * @return Returns ERR_OK on success, others on failure.
153      */
154     int UnloadUIExtensionAbility(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord, std::string &bundleName);
155 
156     /**
157      * ClearPreloadUIExtensionRecord, clear preload uiextension record.
158      *
159      * @param abilityRecord, uiextension ability record.
160      */
161     void ClearPreloadUIExtensionRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
162 
163     /**
164      * DisconnectAbilityLocked, disconnect session with callback.
165      *
166      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
167      * @return Returns ERR_OK on success, others on failure.
168      */
169     int DisconnectAbilityLocked(const sptr<IAbilityConnection> &connect);
170 
171     /**
172      * SuspendExtensionAbilityLocked, suspend session with callback.
173      *
174      * @param connect, Callback used to notify caller the result of suspend.
175      * @return Returns ERR_OK on success, others on failure.
176      */
177     int32_t SuspendExtensionAbilityLocked(const sptr<IAbilityConnection> &connect);
178 
179     /**
180      * ResumeExtensionAbilityLocked, resume session with callback.
181      *
182      * @param connect, Callback used to notify caller the result of resume.
183      * @return Returns ERR_OK on success, others on failure.
184      */
185     int32_t ResumeExtensionAbilityLocked(const sptr<IAbilityConnection> &connect);
186 
187     /**
188      * AttachAbilityThreadLocked, ability call this interface after loaded.
189      *
190      * @param scheduler, the interface handler of kit ability.
191      * @param token, ability's token.
192      * @return Returns ERR_OK on success, others on failure.
193      */
194     int AttachAbilityThreadLocked(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token);
195 
196     void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state);
197 
198     void OnAppStateChanged(const AppInfo &info);
199 
200     /**
201      * AbilityTransitionDone, ability call this interface after lift cycle was changed.
202      *
203      * @param token, ability's token.
204      * @param state, the state of ability lift cycle.
205      * @return Returns ERR_OK on success, others on failure.
206      */
207     int AbilityTransitionDone(const sptr<IRemoteObject> &token, int state);
208 
209     /**
210      * @brief execute after the ability schedule the lifecycle
211      *
212      * @param token the ability token
213      * @param windowConfig the windowconfig
214      * @return execute error code
215      */
216     int AbilityWindowConfigTransactionDone(
217         const sptr<IRemoteObject> &token, const WindowConfig &windowConfig);
218 
219     /**
220      * ScheduleConnectAbilityDoneLocked, service ability call this interface while session was connected.
221      *
222      * @param token, service ability's token.
223      * @param remoteObject, the session proxy of service ability.
224      * @return Returns ERR_OK on success, others on failure.
225      */
226     int ScheduleConnectAbilityDoneLocked(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &remoteObject);
227 
228     /**
229      * ScheduleDisconnectAbilityDone, service ability call this interface while session was disconnected.
230      *
231      * @param token,service ability's token.
232      * @return Returns ERR_OK on success, others on failure.
233      */
234     int ScheduleDisconnectAbilityDoneLocked(const sptr<IRemoteObject> &token);
235 
236     /**
237      * ScheduleCommandAbilityDoneLocked, service ability call this interface while session was onCommanded.
238      *
239      * @param token,service ability's token.
240      * @return Returns ERR_OK on success, others on failure.
241      */
242     int ScheduleCommandAbilityDoneLocked(const sptr<IRemoteObject> &token);
243 
244     int ScheduleCommandAbilityWindowDone(
245         const sptr<IRemoteObject> &token,
246         const sptr<SessionInfo> &sessionInfo,
247         WindowCommand winCmd,
248         AbilityCommand abilityCmd);
249 
250     /**
251      * GetUIExtensionBySessionInfo.
252      *
253      * @param sessionToken, service ability's session token.
254      * @return Returns AbilityRecord shared_ptr.
255      */
256     std::shared_ptr<AbilityRecord> GetUIExtensionBySessionInfo(const sptr<SessionInfo> &sessionInfo);
257 
258     std::shared_ptr<AbilityRecord> GetExtensionByTokenFromServiceMap(const sptr<IRemoteObject> &token);
259     std::shared_ptr<AbilityRecord> GetExtensionByTokenFromTerminatingMap(const sptr<IRemoteObject> &token);
260     std::shared_ptr<AbilityRecord> GetExtensionByIdFromServiceMap(const int64_t &abilityRecordId);
261     std::shared_ptr<AbilityRecord> GetExtensionByIdFromTerminatingMap(const int64_t &abilityRecordId);
262     std::shared_ptr<AbilityRecord> GetUIExtensionBySessionFromServiceMap(const sptr<SessionInfo> &sessionInfo);
263     ConnectListType GetConnectRecordListByCallback(sptr<IAbilityConnection> callback);
264 
265     void GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info,
266         const int32_t userId, bool isPerm);
267 
268     void GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm);
269 
270     void GetExtensionRunningInfo(std::shared_ptr<AbilityRecord> &abilityRecord, const int32_t userId,
271         std::vector<ExtensionRunningInfo> &info);
272 
273     /**
274      * set from ability manager service for sequenced task
275      */
SetTaskHandler(const std::shared_ptr<TaskHandlerWrap> & taskHandler)276     inline void SetTaskHandler(const std::shared_ptr<TaskHandlerWrap> &taskHandler)
277     {
278         taskHandler_ = taskHandler;
279     }
280     /**
281      * SetEventHandler.
282      *
283      * @param handler,EventHandler
284      */
SetEventHandler(const std::shared_ptr<EventHandlerWrap> & handler)285     inline void SetEventHandler(const std::shared_ptr<EventHandlerWrap> &handler)
286     {
287         eventHandler_ = handler;
288     }
289 
GetSceneBoardTokenId()290     uint32_t GetSceneBoardTokenId() const
291     {
292         return sceneBoardTokenId_;
293     }
294 
295     /**
296      * @brief Get extensionList by pid.
297      * @param pid Process id.
298      * @param extensionList UIExtensionAbility name list.
299      */
300     int32_t GetActiveUIExtensionList(const int32_t pid, std::vector<std::string> &extensionList);
301 
302     /**
303      * @brief Get extensionList by bundleName.
304      * @param bundleName The application bundle name.
305      * @param extensionList UIExtensionAbility name list.
306      */
307     int32_t GetActiveUIExtensionList(const std::string &bundleName, std::vector<std::string> &extensionList);
308 
309     void OnLoadAbilityFailed(std::shared_ptr<AbilityRecord> abilityRecord);
310 
311     /**
312      * OnAbilityDied.
313      *
314      * @param abilityRecord, service ability record.
315      */
316     void OnAbilityDied(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
317 
318     /**
319      * DisconnectBeforeCleanup.
320      *
321      */
322     void DisconnectBeforeCleanup();
323 
324     void DumpState(std::vector<std::string> &info, bool isClient, const std::string &args = "");
325 
326     void DumpStateByUri(std::vector<std::string> &info, bool isClient, const std::string &args,
327         std::vector<std::string> &params);
328 
329     void PauseExtensions();
330 
331     void OnTimeOut(uint32_t msgId, int64_t abilityRecordId, bool isHalf = false);
332 
333     /**
334      * @brief schedule to background
335      *
336      * @param abilityRecord the ability to move
337      */
338     void MoveToBackground(const std::shared_ptr<AbilityRecord> &abilityRecord);
339 
340     void CommandAbilityWindow(const std::shared_ptr<AbilityRecord> &abilityRecord,
341         const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd);
342 
343     bool IsUIExtensionFocused(uint32_t uiExtensionTokenId, const sptr<IRemoteObject>& focusToken);
344 
345     sptr<IRemoteObject> GetUIExtensionSourceToken(const sptr<IRemoteObject> &token);
346 
347     bool IsWindowExtensionFocused(uint32_t extensionTokenId, const sptr<IRemoteObject>& focusToken);
348 
349     void HandleProcessFrozen(const std::vector<int32_t> &pidList, int32_t uid);
350 
351     void ForegroundAbilityWindowLocked(const std::shared_ptr<AbilityRecord> &abilityRecord,
352         const sptr<SessionInfo> &sessionInfo);
353 
354     void BackgroundAbilityWindowLocked(const std::shared_ptr<AbilityRecord> &abilityRecord,
355         const sptr<SessionInfo> &sessionInfo);
356 
357     void TerminateAbilityWindowLocked(const std::shared_ptr<AbilityRecord> &abilityRecord,
358         const sptr<SessionInfo> &sessionInfo);
359 
360     void RemoveLauncherDeathRecipient();
361 
362     /**
363      * @brief Get ui extension session info
364      *
365      * @param token The ability token.
366      * @param uiExtensionSessionInfo The ui extension session info.
367      * @param userId The user id.
368      * @return int32_t Returns ERR_OK on success, others on failure.
369      */
370     int32_t GetUIExtensionSessionInfo(const sptr<IRemoteObject> token, UIExtensionSessionInfo &uiExtensionSessionInfo);
371 
372     void GetUIExtensionCallerTokenList(const std::shared_ptr<AbilityRecord> &abilityRecord,
373         std::list<sptr<IRemoteObject>> &callerList);
374 
375     void CloseAssertDialog(const std::string &assertSessionId);
376 
377     void SignRestartAppFlag(int32_t uid, const std::string &instanceKey);
378 
379     std::shared_ptr<AAFwk::AbilityRecord> GetUIExtensionRootHostInfo(const sptr<IRemoteObject> token);
380     void UninstallApp(const std::string &bundleName, int32_t uid);
381 
382     int32_t UpdateKeepAliveEnableState(const std::string &bundleName, const std::string &moduleName,
383         const std::string &mainElement, bool updateEnable);
384 
385     /**
386      * Handle extension disconnect task.
387      */
388     void HandleExtensionDisconnectTask(const std::shared_ptr<ConnectionRecord> &connectRecord);
389 
390     /**
391      * GetServiceRecordByAbilityRequest.
392      *
393      * @param abilityRequest, the request of the service ability to start.
394      * @return Returns AbilityRecord shared_ptr.
395      */
396     std::shared_ptr<AbilityRecord> GetServiceRecordByAbilityRequest(const AbilityRequest &abilityRequest);
397 
398     bool HasRequestIdInLoadAbilityQueue(int32_t requestId);
399     void OnStartSpecifiedProcessResponse(const std::string &flag, int32_t requestId);
400     void OnStartSpecifiedProcessTimeoutResponse(int32_t requestId);
401     void StartSpecifiedProcess(const LoadAbilityContext &context, const std::shared_ptr<AbilityRecord> &abilityRecord);
402 
403 private:
404     /**
405      * StartAbilityLocked with request.
406      *
407      * @param abilityRequest, the request of the service ability to start.
408      * @return Returns ERR_OK on success, others on failure.
409      */
410     int32_t StartAbilityLocked(const AbilityRequest &abilityRequest);
411 
412     /**
413      * TerminateAbilityLocked with token and result want.
414      *
415      * @param token, the token of service type's ability to terminate.
416      * @param resultCode, the result code of service type's ability to terminate.
417      * @param resultWant, the result want for service type's ability to terminate.
418      * @return Returns ERR_OK on success, others on failure.
419      */
420     int TerminateAbilityLocked(const sptr<IRemoteObject> &token);
421 
422     /**
423      * StopAbilityLocked with request.
424      *
425      * @param abilityRequest, the request of the service ability to start.
426      * @return Returns ERR_OK on success, others on failure.
427      */
428     int StopServiceAbilityLocked(const AbilityRequest &abilityRequest);
429 
430     /**
431      * DisconnectAbilityLocked, disconnect session with callback.
432      *
433      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
434      * @param callerDied, bool Indicates if it is caused by the caller's death.
435      * @return Returns ERR_OK on success, others on failure.
436      */
437     int DisconnectAbilityLocked(const sptr<IAbilityConnection> &connect, bool callerDied);
438 
439     /**
440      * LoadAbility.
441      *
442      * @param abilityRecord, the ptr of the ability to load.
443      */
444     void LoadAbility(const std::shared_ptr<AbilityRecord> &abilityRecord,
445         std::function<void(const std::shared_ptr<AbilityRecord> &)> updateRecordCallback = nullptr);
446     void HandleLoadAbilityOrStartSpecifiedProcess(
447         const AbilityRuntime::LoadParam &loadParam, const std::shared_ptr<AbilityRecord> &abilityRecord);
448     /**
449      * ConnectAbility.Schedule connect ability
450      *
451      * @param abilityRecord, the ptr of the ability to connect.
452      */
453     void ConnectAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
454 
455     /**
456      * ConnectAbility.Schedule connect ability
457      *
458      * @param abilityRecord, the ptr of the ability to connect.
459      */
460     void ConnectUIServiceExtAbility(const std::shared_ptr<AbilityRecord> &abilityRecord,
461         int connectRecordId, const Want &want);
462 
463     /**
464      * ConnectAbility.Schedule Resume Connect ability
465      *
466      * @param abilityRecord, the ptr of the ability to connect.
467      */
468     void ResumeConnectAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
469 
470     /**
471      * CommandAbility. Schedule command ability
472      *
473      * @param abilityRecord, the ptr of the ability to command.
474      */
475     void CommandAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
476 
477     /**
478      * CompleteCommandAbility. complete command ability
479      *
480      * @param abilityRecord, the ptr of the ability to command.
481      */
482     void CompleteCommandAbility(std::shared_ptr<AbilityRecord> abilityRecord);
483 
484     /**
485      * TerminateDone.
486      *
487      * @param abilityRecord, the ptr of the ability to terminate.
488      */
489     void TerminateDone(const std::shared_ptr<AbilityRecord> &abilityRecord);
490 
491     /**
492      * GetServiceRecordByElementName.
493      *
494      * @param element, service ability's element.
495      * @return Returns AbilityRecord shared_ptr.
496      */
497     std::shared_ptr<AbilityRecord> GetServiceRecordByElementName(const std::string &element);
498 
499     /**
500      * GetAbilityConnectedRecordFromRecordList.
501      *
502      * @param targetService, the ptr of the connected ability.
503      * @param connectRecordList, connect record list.
504      * @return Returns ConnectionRecord shared_ptr.
505      */
506     std::shared_ptr<ConnectionRecord> GetAbilityConnectedRecordFromRecordList(
507         const std::shared_ptr<AbilityRecord> &targetService,
508         std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList);
509 
510     /**
511      * dispatch service ability life cycle .
512      *
513      * @param abilityRecord.
514      * @param state.
515      */
516     int DispatchInactive(const std::shared_ptr<AbilityRecord> &abilityRecord, int state);
517     int DispatchForeground(const std::shared_ptr<AbilityRecord> &abilityRecord);
518     int DispatchBackground(const std::shared_ptr<AbilityRecord> &abilityRecord);
519     int DispatchTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord);
520 
521     void HandleStartTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
522     void HandleStopTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
523     void HandleTerminateDisconnectTask(const ConnectListType& connectlist);
524     void HandleCommandTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
525     void HandleCommandWindowTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord,
526         const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd);
527     void HandleForegroundTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
528     void HandleConnectTimeoutTask(std::shared_ptr<AbilityRecord> abilityRecord);
529     void HandleRestartResidentTask(const AbilityRequest &abilityRequest);
530     void HandleActiveAbility(std::shared_ptr<AbilityRecord> &targetService,
531         std::shared_ptr<ConnectionRecord> &connectRecord);
532     void HandleCommandDestroy(const sptr<SessionInfo> &sessionInfo);
533     void TerminateOrCacheAbility(std::shared_ptr<AbilityRecord> abilityRecord);
534 
535     /**
536      * RemoveConnectionRecordFromMap.
537      *
538      * @param connect, the ptr of the connect record.
539      */
540     void RemoveConnectionRecordFromMap(std::shared_ptr<ConnectionRecord> connect);
541 
542     /**
543      * RemoveServiceAbility.
544      *
545      * @param service, the ptr of the ability record.
546      */
547     void RemoveServiceAbility(const std::shared_ptr<AbilityRecord> &service);
548 
549     /**
550      * GetOrCreateServiceRecord.
551      *
552      * @param abilityRequest, Special want for service type's ability.
553      * @param isCreatedByConnect, whether is created by connect ability mode.
554      * @param targetAbilityRecord, the target service ability record.
555      * @param isLoadedAbility, whether the target ability has been loaded.
556      */
557     void GetOrCreateServiceRecord(const AbilityRequest &abilityRequest, const bool isCreatedByConnect,
558         std::shared_ptr<AbilityRecord> &targetAbilityRecord, bool &isLoadedAbility);
559     void SetServiceAfterNewCreate(const AbilityRequest &abilityRequest, AbilityRecord &targetService);
560 
561     void RemoveServiceFromMapSafe(const std::string &serviceKey);
562 
563     /**
564      * GetConnectRecordListFromMap.
565      *
566      * @param connect, callback object.
567      * @param isCreatedByConnect, whether is created by connect ability mode.
568      * @param connectRecordList, the target connectRecordList.
569      * @param isCallbackConnected, whether the callback has been connected.
570      */
571     void GetConnectRecordListFromMap(
572         const sptr<IAbilityConnection> &connect, std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList);
573 
574     /**
575      * AddConnectDeathRecipient.
576      *
577      * @param connect, callback object.
578      */
579     void AddConnectDeathRecipient(sptr<IRemoteObject> connectObject);
580 
581     /**
582      * RemoteConnectDeathRecipient.
583      *
584      * @param connect, callback object.
585      */
586     void RemoveConnectDeathRecipient(sptr<IRemoteObject> connectObject);
587 
588     /**
589      * RemoteConnectDeathRecipient.
590      *
591      * @param remote, callback object.
592      */
593     void OnCallBackDied(const wptr<IRemoteObject> &remote);
594 
595     /**
596      * HandleOnCallBackDied.
597      *
598      * @param connect, callback object.
599      */
600     void HandleCallBackDiedTask(const sptr<IRemoteObject> &connect);
601 
602     /**
603      * HandleOnCallBackDied.
604      *
605      * @param abilityRecord, died ability.
606      */
607     void HandleAbilityDiedTask(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
608 
609     void HandleUIExtensionDied(const std::shared_ptr<AbilityRecord> &abilityRecord);
610 
611     void RestartAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
612 
613     /**
614      * PostTimeOutTask.
615      *
616      * @param abilityRecord, ability.
617      * @param messageId, message id.
618      */
619     void PostTimeOutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, uint32_t messageId);
620     void PostTimeOutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, int connectRecordId, uint32_t messageId);
621 
622     void CompleteForeground(const std::shared_ptr<AbilityRecord> &abilityRecord);
623     void CompleteBackground(const std::shared_ptr<AbilityRecord> &abilityRecord);
624     void PrintTimeOutLog(const std::shared_ptr<AbilityRecord> &ability, uint32_t msgId, bool isHalf = false);
625 
626     void PostRestartResidentTask(const AbilityRequest &abilityRequest);
627 
628     bool IsAbilityNeedKeepAlive(const std::shared_ptr<AbilityRecord> &abilityRecord);
629 
630     void ProcessPreload(const std::shared_ptr<AbilityRecord> &record) const;
631 
632     void HandleInactiveTimeout(const std::shared_ptr<AbilityRecord> &ability);
633     void CleanActivatingTimeoutAbility(std::shared_ptr<AbilityRecord> abilityRecord);
634     void MoveToTerminatingMap(const std::shared_ptr<AbilityRecord>& abilityRecord);
635 
636     void DoForegroundUIExtension(std::shared_ptr<AbilityRecord> abilityRecord, const AbilityRequest &abilityRequest);
637     void DoBackgroundAbilityWindow(const std::shared_ptr<AbilityRecord> &abilityRecord,
638         const sptr<SessionInfo> &sessionInfo);
639 
640     /**
641      * When a service is under starting, enque the request and handle it after the service starting completes
642      */
643     void EnqueueStartServiceReq(const AbilityRequest &abilityRequest, const std::string &serviceUri = "");
644     /**
645      * After the service starting completes, complete the request list
646      */
647     void CompleteStartServiceReq(const std::string &serviceUri);
648 
649     void AddUIExtWindowDeathRecipient(const sptr<IRemoteObject> &session);
650     void RemoveUIExtWindowDeathRecipient(const sptr<IRemoteObject> &session);
651     void OnUIExtWindowDied(const wptr<IRemoteObject> &remote);
652     void HandleUIExtWindowDiedTask(const sptr<IRemoteObject> &remote);
653 
654     /**
655      * Post an extension's disconnect task, auto disconnect when extension connected timeout.
656      */
657     void PostExtensionDelayDisconnectTask(const std::shared_ptr<ConnectionRecord> &connectRecord);
658 
659     /**
660      * Remove the extension's disconnect task.
661      */
662     void RemoveExtensionDelayDisconnectTask(const std::shared_ptr<ConnectionRecord> &connectRecord);
663 
664 private:
665     bool IsNeedToRestart(const std::shared_ptr<AbilityRecord> &abilityRecord,
666         const std::string &bundleName, const std::string &abilityName);
667     void TerminateRecord(std::shared_ptr<AbilityRecord> abilityRecord);
668     int DisconnectRecordNormal(ConnectListType &list, std::shared_ptr<ConnectionRecord> connectRecord,
669         bool callerDied) const;
670     void DisconnectRecordForce(ConnectListType &list, std::shared_ptr<ConnectionRecord> connectRecord);
671     int TerminateAbilityInner(const sptr<IRemoteObject> &token);
672     bool IsLauncher(std::shared_ptr<AbilityRecord> serviceExtension) const;
673     void KillProcessesByUserId() const;
674     void SetLastExitReason(const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &targetService);
675     inline bool IsUIExtensionAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
676     inline bool IsCacheExtensionAbilityByInfo(const AppExecFwk::AbilityInfo &abilityInfo);
677     inline bool IsCacheExtensionAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
678     inline bool CheckUIExtensionAbilityLoaded(const AbilityRequest &abilityRequest);
679     inline bool CheckUIExtensionAbilitySessionExist(const std::shared_ptr<AbilityRecord> &abilityRecord);
680     inline void RemoveUIExtensionAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
681     inline void AddUIExtensionAbilityRecordToTerminatedList(const std::shared_ptr<AbilityRecord> &abilityRecord);
682     inline bool IsCallerValid(const std::shared_ptr<AbilityRecord> &abilityRecord);
683     int32_t GetOrCreateExtensionRecord(const AbilityRequest &abilityRequest, bool isCreatedByConnect,
684         const std::string &hostBundleName, std::shared_ptr<AbilityRecord> &extensionRecord, bool &isLoaded);
685     int32_t GetOrCreateTargetServiceRecord(
686         const AbilityRequest &abilityRequest, const sptr<UIExtensionAbilityConnectInfo> &connectInfo,
687         std::shared_ptr<AbilityRecord> &targetService, bool &isLoadedAbility);
688     void HandleNotifyAssertFaultDialogDied(const std::shared_ptr<AbilityRecord> &abilityRecord);
689     EventInfo BuildEventInfo(const std::shared_ptr<AbilityRecord> &abilityRecord);
690     void UpdateUIExtensionInfo(const std::shared_ptr<AbilityRecord> &abilityRecord,
691         int32_t hostPid = AAFwk::DEFAULT_INVAL_VALUE);
692     std::string GenerateBundleName(const AbilityRequest &abilityRequest) const;
693 
694     bool AddToServiceMap(const std::string &key, std::shared_ptr<AbilityRecord> abilityRecord);
695     ServiceMapType GetServiceMap();
696 
697     void AddConnectObjectToMap(sptr<IRemoteObject> connectObject, const ConnectListType &connectRecordList,
698         bool updateOnly);
699 
700     void KeepAbilityAlive(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
701     void ProcessEliminateAbilityRecord(std::shared_ptr<AbilityRecord> eliminateRecord);
702     std::string GetServiceKey(const std::shared_ptr<AbilityRecord> &service);
703 
704     int32_t ReportXiaoYiToRSSIfNeeded(const AppExecFwk::AbilityInfo &abilityInfo);
705     int32_t ReportAbilityStartInfoToRSS(const AppExecFwk::AbilityInfo &abilityInfo);
706     void ReportEventToRSS(const AppExecFwk::AbilityInfo &abilityInfo,
707         const std::shared_ptr<AbilityRecord> abilityRecord, sptr<IRemoteObject> callerToken);
708 
709     void SetExtensionLoadParam(AbilityRuntime::LoadParam &loadParam, std::shared_ptr<AbilityRecord> abilityRecord);
710     bool IsStrictMode(std::shared_ptr<AbilityRecord> abilityRecord);
711     bool NeedExtensionControl(std::shared_ptr<AbilityRecord> abilityRecord);
712     bool GetTimeoutMsgContent(uint32_t msgId, std::string &msgContent, int &typeId);
713 
714     void UpdateUIExtensionBindInfo(
715         const std::shared_ptr<AbilityRecord> &abilityRecord, std::string callerBundleName, int32_t notifyProcessBind);
716 private:
717     const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask";
718     const std::string TASK_ON_ABILITY_DIED = "OnAbilityDiedTask";
719 
720     int userId_;
721     std::atomic_uint32_t sceneBoardTokenId_ = 0;
722 
723     ConnectMapType connectMap_;
724     ServiceMapType serviceMap_;
725     RecipientMapType recipientMap_;
726     RecipientMapType uiExtRecipientMap_;
727     UIExtensionMapType uiExtensionMap_;
728     WindowExtensionMapType windowExtensionMap_;
729 
730     std::list<std::shared_ptr<AbilityRecord>> terminatingExtensionList_;
731     std::shared_ptr<TaskHandlerWrap> taskHandler_;
732     std::shared_ptr<EventHandlerWrap> eventHandler_;
733     std::vector<AbilityRequest> restartResidentTaskList_;
734     std::unordered_map<std::string, std::shared_ptr<std::list<AbilityRequest>>> startServiceReqList_;
735 
736     std::unique_ptr<UIExtensionAbilityConnectManager> uiExtensionAbilityRecordMgr_ = nullptr;
737     ffrt::mutex serialMutex_;
738     std::mutex connectMapMutex_;
739     ffrt::mutex serviceMapMutex_;
740     std::mutex recipientMapMutex_;
741     std::mutex uiExtRecipientMapMutex_;
742     std::mutex uiExtensionMapMutex_;
743     std::mutex windowExtensionMapMutex_;
744     std::mutex startServiceReqListLock_;
745     std::mutex loadAbilityQueueLock_;
746     std::deque<std::map<int32_t, LoadAbilityContext>> loadAbilityQueue_;
747 
748     DISALLOW_COPY_AND_MOVE(AbilityConnectManager);
749 };
750 }  // namespace AAFwk
751 }  // namespace OHOS
752 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_CONNECT_MANAGER_H
753