• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_connect_callback_interface.h"
26 #include "task_handler_wrap.h"
27 #include "event_handler_wrap.h"
28 #include "ability_record.h"
29 #include "ability_running_info.h"
30 #include "extension_running_info.h"
31 #include "connection_record.h"
32 #include "element_name.h"
33 #include "want.h"
34 #include "iremote_object.h"
35 #include "nocopyable.h"
36 
37 namespace OHOS {
38 namespace AAFwk {
39 using OHOS::AppExecFwk::AbilityType;
40 /**
41  * @class AbilityConnectManager
42  * AbilityConnectManager provides a facility for managing service ability connection.
43  */
44 class AbilityConnectManager : public std::enable_shared_from_this<AbilityConnectManager> {
45 public:
46     using ConnectMapType = std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<ConnectionRecord>>>;
47     using ServiceMapType = std::map<std::string, std::shared_ptr<AbilityRecord>>;
48     using ConnectListType = std::list<std::shared_ptr<ConnectionRecord>>;
49     using RecipientMapType = std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>>;
50     using UIExtWindowMapValType = std::pair<std::weak_ptr<AbilityRecord>, sptr<SessionInfo>>;
51     using UIExtensionMapType = std::map<sptr<IRemoteObject>, UIExtWindowMapValType>;
52     using WindowExtMapValType = std::pair<uint32_t, sptr<SessionInfo>>;
53     using WindowExtensionMapType = std::map<sptr<IRemoteObject>, WindowExtMapValType>;
54 
55     explicit AbilityConnectManager(int userId);
56     virtual ~AbilityConnectManager();
57 
58     /**
59      * StartAbility with request.
60      *
61      * @param abilityRequest, the request of the service ability to start.
62      * @return Returns ERR_OK on success, others on failure.
63      */
64     int StartAbility(const AbilityRequest &abilityRequest);
65 
66     /**
67      * TerminateAbility with token and result want.
68      *
69      * @param token, the token of service type's ability to terminate.
70      * @return Returns ERR_OK on success, others on failure.
71      */
72     int TerminateAbility(const sptr<IRemoteObject> &token);
73 
74     /**
75      * StopServiceAbility with request.
76      *
77      * @param abilityRequest, request.
78      * @return Returns ERR_OK on success, others on failure.
79      */
80     int StopServiceAbility(const AbilityRequest &abilityRequest);
81 
82     /**
83      * ConnectAbilityLocked, connect session with service ability.
84      *
85      * @param abilityRequest, Special want for service type's ability.
86      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
87      * @param callerToken, caller ability token.
88      * @param sessionInfo the extension session info of the ability to connect.
89      * @return Returns ERR_OK on success, others on failure.
90      */
91     int ConnectAbilityLocked(const AbilityRequest &abilityRequest, const sptr<IAbilityConnection> &connect,
92         const sptr<IRemoteObject> &callerToken, sptr<SessionInfo> sessionInfo = nullptr);
93 
94     /**
95      * DisconnectAbilityLocked, disconnect session with callback.
96      *
97      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
98      * @return Returns ERR_OK on success, others on failure.
99      */
100     int DisconnectAbilityLocked(const sptr<IAbilityConnection> &connect);
101 
102     /**
103      * AttachAbilityThreadLocked, ability call this interface after loaded.
104      *
105      * @param scheduler, the interface handler of kit ability.
106      * @param token, ability's token.
107      * @return Returns ERR_OK on success, others on failure.
108      */
109     int AttachAbilityThreadLocked(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token);
110 
111     void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state);
112 
113     void OnAppStateChanged(const AppInfo &info);
114 
115     /**
116      * AbilityTransitionDone, ability call this interface after lift cycle was changed.
117      *
118      * @param token, ability's token.
119      * @param state, the state of ability lift cycle.
120      * @return Returns ERR_OK on success, others on failure.
121      */
122     int AbilityTransitionDone(const sptr<IRemoteObject> &token, int state);
123 
124     /**
125      * ScheduleConnectAbilityDoneLocked, service ability call this interface while session was connected.
126      *
127      * @param token, service ability's token.
128      * @param remoteObject, the session proxy of service ability.
129      * @return Returns ERR_OK on success, others on failure.
130      */
131     int ScheduleConnectAbilityDoneLocked(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &remoteObject);
132 
133     /**
134      * ScheduleDisconnectAbilityDone, service ability call this interface while session was disconnected.
135      *
136      * @param token,service ability's token.
137      * @return Returns ERR_OK on success, others on failure.
138      */
139     int ScheduleDisconnectAbilityDoneLocked(const sptr<IRemoteObject> &token);
140 
141     /**
142      * ScheduleCommandAbilityDoneLocked, service ability call this interface while session was onCommanded.
143      *
144      * @param token,service ability's token.
145      * @return Returns ERR_OK on success, others on failure.
146      */
147     int ScheduleCommandAbilityDoneLocked(const sptr<IRemoteObject> &token);
148 
149     int ScheduleCommandAbilityWindowDone(
150         const sptr<IRemoteObject> &token,
151         const sptr<SessionInfo> &sessionInfo,
152         WindowCommand winCmd,
153         AbilityCommand abilityCmd);
154 
155     /**
156      * GetServiceRecordByElementName.
157      *
158      * @param element, service ability's element.
159      * @return Returns AbilityRecord shared_ptr.
160      */
161     std::shared_ptr<AbilityRecord> GetServiceRecordByElementName(const std::string &element);
162 
163     /**
164      * GetUIExtensioBySessionInfo.
165      *
166      * @param sessionToken, service ability's session token.
167      * @return Returns AbilityRecord shared_ptr.
168      */
169     std::shared_ptr<AbilityRecord> GetUIExtensioBySessionInfo(const sptr<SessionInfo> &sessionInfo);
170 
171     std::shared_ptr<AbilityRecord> GetExtensionByTokenFromServiceMap(const sptr<IRemoteObject> &token);
172     std::shared_ptr<AbilityRecord> GetExtensionByTokenFromTerminatingMap(const sptr<IRemoteObject> &token);
173     ConnectListType GetConnectRecordListByCallback(sptr<IAbilityConnection> callback);
174 
175     void GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info,
176         const int32_t userId, bool isPerm);
177 
178     void GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm);
179 
180     void GetExtensionRunningInfo(std::shared_ptr<AbilityRecord> &abilityRecord, const int32_t userId,
181         std::vector<ExtensionRunningInfo> &info);
182 
183     /**
184      * set from ability manager service for sequenced task
185      */
SetTaskHandler(const std::shared_ptr<TaskHandlerWrap> & taskHandler)186     inline void SetTaskHandler(const std::shared_ptr<TaskHandlerWrap> &taskHandler)
187     {
188         taskHandler_ = taskHandler;
189     }
190     /**
191      * SetEventHandler.
192      *
193      * @param handler,EventHandler
194      */
SetEventHandler(const std::shared_ptr<EventHandlerWrap> & handler)195     inline void SetEventHandler(const std::shared_ptr<EventHandlerWrap> &handler)
196     {
197         eventHandler_ = handler;
198     }
199 
200     /**
201      * GetConnectMap.
202      *
203      * @return Returns connection record list.
204      */
GetConnectMap()205     inline const ConnectMapType &GetConnectMap() const
206     {
207         return connectMap_;
208     }
209 
210     /**
211      * GetServiceMap.
212      *
213      * @return Returns service ability record map.
214      */
GetServiceMap()215     inline const ServiceMapType &GetServiceMap() const
216     {
217         return serviceMap_;
218     }
219 
220     /**
221      * OnAbilityDied.
222      *
223      * @param abilityRecord, service ability record.
224      */
225     void OnAbilityDied(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
226 
227     void DumpState(std::vector<std::string> &info, bool isClient, const std::string &args = "");
228 
229     void DumpStateByUri(std::vector<std::string> &info, bool isClient, const std::string &args,
230         std::vector<std::string> &params);
231 
232     void StopAllExtensions();
233 
234     void StartRootLauncher(const std::shared_ptr<AbilityRecord> &abilityRecord);
235     void OnTimeOut(uint32_t msgId, int64_t abilityRecordId);
236 
237     void MoveToForeground(const std::shared_ptr<AbilityRecord> &abilityRecord);
238     /**
239      * @brief schedule to background
240      *
241      * @param abilityRecord the ability to move
242      */
243     void MoveToBackground(const std::shared_ptr<AbilityRecord> &abilityRecord);
244 
245     void CommandAbilityWindow(const std::shared_ptr<AbilityRecord> &abilityRecord,
246         const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd);
247 
248     bool IsUIExtensionFocused(uint32_t uiExtensionTokenId, const sptr<IRemoteObject>& focusToken);
249 
250     bool IsWindowExtensionFocused(uint32_t extensionTokenId, const sptr<IRemoteObject>& focusToken);
251 
252     // MSG 0 - 20 represents timeout message
253     static constexpr uint32_t LOAD_TIMEOUT_MSG = 0;
254     static constexpr uint32_t CONNECT_TIMEOUT_MSG = 1;
255 
256 private:
257     /**
258      * StartAbilityLocked with request.
259      *
260      * @param abilityRequest, the request of the service ability to start.
261      * @return Returns ERR_OK on success, others on failure.
262      */
263     int StartAbilityLocked(const AbilityRequest &abilityRequest);
264 
265     /**
266      * TerminateAbilityLocked with token and result want.
267      *
268      * @param token, the token of service type's ability to terminate.
269      * @param resultCode, the result code of service type's ability to terminate.
270      * @param resultWant, the result want for service type's ability to terminate.
271      * @return Returns ERR_OK on success, others on failure.
272      */
273     int TerminateAbilityLocked(const sptr<IRemoteObject> &token);
274 
275     /**
276      * StopAbilityLocked with request.
277      *
278      * @param abilityRequest, the request of the service ability to start.
279      * @return Returns ERR_OK on success, others on failure.
280      */
281     int StopServiceAbilityLocked(const AbilityRequest &abilityRequest);
282 
283     /**
284      * DisconnectAbilityLocked, disconnect session with callback.
285      *
286      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
287      * @param force, Indicates forcing to disconnect and clear. For example, it is called when the source
288      * dies and the connection has not completed yet.
289      * @return Returns ERR_OK on success, others on failure.
290      */
291     int DisconnectAbilityLocked(const sptr<IAbilityConnection> &connect, bool force);
292 
293     /**
294      * LoadAbility.
295      *
296      * @param abilityRecord, the ptr of the ability to load.
297      */
298     void LoadAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
299 
300     /**
301      * ConnectAbility.Schedule connect ability
302      *
303      * @param abilityRecord, the ptr of the ability to connect.
304      */
305     void ConnectAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
306 
307     /**
308      * CommandAbility. Schedule command ability
309      *
310      * @param abilityRecord, the ptr of the ability to command.
311      */
312     void CommandAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
313 
314     /**
315      * CompleteCommandAbility. complete command ability
316      *
317      * @param abilityRecord, the ptr of the ability to command.
318      */
319     void CompleteCommandAbility(std::shared_ptr<AbilityRecord> abilityRecord);
320 
321     /**
322      * TerminateDone.
323      *
324      * @param abilityRecord, the ptr of the ability to terminate.
325      */
326     void TerminateDone(const std::shared_ptr<AbilityRecord> &abilityRecord);
327 
328     /**
329      * dispatch service ability life cycle .
330      *
331      * @param abilityRecord.
332      * @param state.
333      */
334     int DispatchInactive(const std::shared_ptr<AbilityRecord> &abilityRecord, int state);
335     int DispatchForeground(const std::shared_ptr<AbilityRecord> &abilityRecord);
336     int DispatchBackground(const std::shared_ptr<AbilityRecord> &abilityRecord);
337     int DispatchTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord);
338 
339     void HandleStartTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, int resultCode);
340     void HandleStopTimeoutTask(std::shared_ptr<AbilityRecord> abilityRecord);
341     void HandleTerminateDisconnectTask(const ConnectListType& connectlist);
342     void HandleCommandTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
343     void HandleCommandWindowTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord,
344         const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd);
345     void HandleRestartResidentTask(const AbilityRequest &abilityRequest);
346     void HandleActiveAbility(std::shared_ptr<AbilityRecord> &targetService,
347         std::shared_ptr<ConnectionRecord> &connectRecord);
348     void HandleCommandDestroy(const sptr<SessionInfo> &sessionInfo);
349 
350     /**
351      * IsAbilityConnected.
352      *
353      * @param abilityRecord, the ptr of the connected ability.
354      * @param connectRecordList, connect record list.
355      * @return true: ability is connected, false: ability is not connected
356      */
357     bool IsAbilityConnected(const std::shared_ptr<AbilityRecord> &abilityRecord,
358         const std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList);
359 
360     /**
361      * RemoveConnectionRecordFromMap.
362      *
363      * @param connect, the ptr of the connect record.
364      */
365     void RemoveConnectionRecordFromMap(const std::shared_ptr<ConnectionRecord> &connect);
366 
367     /**
368      * RemoveServiceAbility.
369      *
370      * @param service, the ptr of the ability record.
371      */
372     void RemoveServiceAbility(const std::shared_ptr<AbilityRecord> &service);
373 
374     /**
375      * GetOrCreateServiceRecord.
376      *
377      * @param abilityRequest, Special want for service type's ability.
378      * @param isCreatedByConnect, whether is created by connect ability mode.
379      * @param targetAbilityRecord, the target service ability record.
380      * @param isLoadedAbility, whether the target ability has been loaded.
381      */
382     void GetOrCreateServiceRecord(const AbilityRequest &abilityRequest, const bool isCreatedByConnect,
383         std::shared_ptr<AbilityRecord> &targetAbilityRecord, bool &isLoadedAbility);
384 
385     /**
386      * GetConnectRecordListFromMap.
387      *
388      * @param connect, callback object.
389      * @param isCreatedByConnect, whether is created by connect ability mode.
390      * @param connectRecordList, the target connectRecordList.
391      * @param isCallbackConnected, whether the callback has been connected.
392      */
393     void GetConnectRecordListFromMap(
394         const sptr<IAbilityConnection> &connect, std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList);
395 
396     /**
397      * AddConnectDeathRecipient.
398      *
399      * @param connect, callback object.
400      */
401     void AddConnectDeathRecipient(const sptr<IAbilityConnection> &connect);
402 
403     /**
404      * RemoteConnectDeathRecipient.
405      *
406      * @param connect, callback object.
407      */
408     void RemoveConnectDeathRecipient(const sptr<IAbilityConnection> &connect);
409 
410     /**
411      * RemoteConnectDeathRecipient.
412      *
413      * @param remote, callback object.
414      */
415     void OnCallBackDied(const wptr<IRemoteObject> &remote);
416 
417     /**
418      * HandleOnCallBackDied.
419      *
420      * @param connect, callback object.
421      */
422     void HandleCallBackDiedTask(const sptr<IRemoteObject> &connect);
423 
424     /**
425      * HandleOnCallBackDied.
426      *
427      * @param abilityRecord, died ability.
428      */
429     void HandleAbilityDiedTask(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
430     void HandleUIExtensionDied(const std::shared_ptr<AbilityRecord> &abilityRecord);
431 
432     void RestartAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
433 
434     /**
435      * PostTimeOutTask.
436      *
437      * @param abilityRecord, ability.
438      * @param messageId, message id.
439      */
440     void PostTimeOutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, uint32_t messageId);
441 
442     void CompleteForeground(const std::shared_ptr<AbilityRecord> &abilityRecord);
443     void CompleteBackground(const std::shared_ptr<AbilityRecord> &abilityRecord);
444     void PrintTimeOutLog(const std::shared_ptr<AbilityRecord> &ability, uint32_t msgId);
445 
446     void PostRestartResidentTask(const AbilityRequest &abilityRequest);
447 
448     bool IsAbilityNeedKeepAlive(const std::shared_ptr<AbilityRecord> &abilityRecord);
449 
450     void ProcessPreload(const std::shared_ptr<AbilityRecord> &record) const;
451 
452     std::shared_ptr<AbilityRecord> GetAbilityRecordById(int64_t abilityRecordId);
453     void HandleInactiveTimeout(const std::shared_ptr<AbilityRecord> &ability);
454     void MoveToTerminatingMap(const std::shared_ptr<AbilityRecord>& abilityRecord);
455 
456     /**
457      * When a service is under starting, enque the request and handle it after the service starting completes
458      */
459     void EnqueueStartServiceReq(const AbilityRequest &abilityRequest);
460     /**
461      * After the service starting completes, complete the request list
462      */
463     void CompleteStartServiceReq(const std::string &serviceUri);
464 
465     void AddUIExtWindowDeathRecipient(const sptr<IRemoteObject> &session);
466     void RemoveUIExtWindowDeathRecipient(const sptr<IRemoteObject> &session);
467     void OnUIExtWindowDied(const wptr<IRemoteObject> &remote);
468     void HandleUIExtWindowDiedTask(const sptr<IRemoteObject> &remote);
469 
470 private:
471     void TerminateRecord(std::shared_ptr<AbilityRecord> abilityRecord);
472     int DisconnectRecordNormal(ConnectListType &list, std::shared_ptr<ConnectionRecord> connectRecord) const;
473     void DisconnectRecordForce(ConnectListType &list, std::shared_ptr<ConnectionRecord> connectRecord);
474     std::shared_ptr<AbilityRecord> GetServiceRecordByElementNameInner(const std::string &element);
475     std::shared_ptr<AbilityRecord> GetExtensionFromServiceMapInner(const sptr<IRemoteObject> &token);
476     std::shared_ptr<AbilityRecord> GetExtensionFromTerminatingMapInner(const sptr<IRemoteObject> &token);
477     int TerminateAbilityInner(const sptr<IRemoteObject> &token);
478 
479 private:
480     const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask";
481     const std::string TASK_ON_ABILITY_DIED = "OnAbilityDiedTask";
482 
483     ffrt::mutex Lock_;
484     ConnectMapType connectMap_;
485     ServiceMapType serviceMap_;
486     ServiceMapType terminatingExtensionMap_;
487     RecipientMapType recipientMap_;
488     RecipientMapType uiExtRecipientMap_;
489     std::shared_ptr<TaskHandlerWrap> taskHandler_;
490     std::shared_ptr<EventHandlerWrap> eventHandler_;
491     int userId_;
492     std::vector<AbilityRequest> restartResidentTaskList_;
493     std::unordered_map<std::string, std::shared_ptr<std::list<AbilityRequest>>> startServiceReqList_;
494     ffrt::mutex startServiceReqListLock_;
495     UIExtensionMapType uiExtensionMap_;
496     WindowExtensionMapType windowExtensionMap_;
497 
498     DISALLOW_COPY_AND_MOVE(AbilityConnectManager);
499 };
500 }  // namespace AAFwk
501 }  // namespace OHOS
502 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_CONNECT_MANAGER_H
503