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