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