• 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 
23 #include "ability_connect_callback_interface.h"
24 #include "ability_event_handler.h"
25 #include "ability_record.h"
26 #include "ability_running_info.h"
27 #include "extension_running_info.h"
28 #include "connection_record.h"
29 #include "element_name.h"
30 #include "want.h"
31 #include "iremote_object.h"
32 #include "nocopyable.h"
33 
34 namespace OHOS {
35 namespace AAFwk {
36 using OHOS::AppExecFwk::AbilityType;
37 /**
38  * @class AbilityConnectManager
39  * AbilityConnectManager provides a facility for managing service ability connection.
40  */
41 class AbilityConnectManager : public std::enable_shared_from_this<AbilityConnectManager> {
42 public:
43     using ConnectMapType = std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<ConnectionRecord>>>;
44     using ServiceMapType = std::map<std::string, std::shared_ptr<AbilityRecord>>;
45     using ConnectListType = std::list<std::shared_ptr<ConnectionRecord>>;
46     using RecipientMapType = std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>>;
47 
48     explicit AbilityConnectManager(int userId);
49     virtual ~AbilityConnectManager();
50 
51     /**
52      * StartAbility with request.
53      *
54      * @param abilityRequest, the request of the service ability to start.
55      * @return Returns ERR_OK on success, others on failure.
56      */
57     int StartAbility(const AbilityRequest &abilityRequest);
58 
59     /**
60      * TerminateAbility with token and result want.
61      *
62      * @param token, the token of service type's ability to terminate.
63      * @return Returns ERR_OK on success, others on failure.
64      */
65     int TerminateAbility(const sptr<IRemoteObject> &token);
66 
67     /**
68      * TerminateAbility, terminate the special ability.
69      *
70      * @param caller, caller ability record.
71      * @param requestCode, abililty request code
72      * @return Returns ERR_OK on success, others on failure.
73      */
74     int TerminateAbility(const std::shared_ptr<AbilityRecord> &caller, int requestCode);
75 
76     /**
77      * StopServiceAbility with request.
78      *
79      * @param abilityRequest, request.
80      * @return Returns ERR_OK on success, others on failure.
81      */
82     int StopServiceAbility(const AbilityRequest &abilityRequest);
83 
84     /**
85      * Destroys this Service ability if the number of times it
86      * has been started equals the number represented by
87      * the given startId.
88      *
89      * @param token ability's token.
90      * @param startId is incremented by 1 every time this ability is started.
91      * @return Returns true if the startId matches the number of startup times
92      * and this Service ability will be destroyed; returns false otherwise.
93      */
94     int TerminateAbilityResult(const sptr<IRemoteObject> &token, int startId);
95 
96     /**
97      * ConnectAbilityLocked, connect session with service ability.
98      *
99      * @param abilityRequest, Special want for service type's ability.
100      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
101      * @param callerToken, caller ability token.
102      * @return Returns ERR_OK on success, others on failure.
103      */
104     int ConnectAbilityLocked(const AbilityRequest &abilityRequest, const sptr<IAbilityConnection> &connect,
105         const sptr<IRemoteObject> &callerToken);
106 
107     /**
108      * DisconnectAbilityLocked, disconnect session with callback.
109      *
110      * @param connect, Callback used to notify caller the result of connecting or disconnecting.
111      * @return Returns ERR_OK on success, others on failure.
112      */
113     int DisconnectAbilityLocked(const sptr<IAbilityConnection> &connect);
114 
115     /**
116      * AttachAbilityThreadLocked, ability call this interface after loaded.
117      *
118      * @param scheduler, the interface handler of kit ability.
119      * @param token, ability's token.
120      * @return Returns ERR_OK on success, others on failure.
121      */
122     int AttachAbilityThreadLocked(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token);
123 
124     void OnAppStateChanged(const AppInfo &info);
125 
126     /**
127      * AbilityTransitionDone, ability call this interface after lift cycle was changed.
128      *
129      * @param token, ability's token.
130      * @param state, the state of ability lift cycle.
131      * @return Returns ERR_OK on success, others on failure.
132      */
133     int AbilityTransitionDone(const sptr<IRemoteObject> &token, int state);
134 
135     /**
136      * ScheduleConnectAbilityDoneLocked, service ability call this interface while session was connected.
137      *
138      * @param token, service ability's token.
139      * @param remoteObject, the session proxy of service ability.
140      * @return Returns ERR_OK on success, others on failure.
141      */
142     int ScheduleConnectAbilityDoneLocked(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &remoteObject);
143 
144     /**
145      * ScheduleDisconnectAbilityDone, service ability call this interface while session was disconnected.
146      *
147      * @param token,service ability's token.
148      * @return Returns ERR_OK on success, others on failure.
149      */
150     int ScheduleDisconnectAbilityDoneLocked(const sptr<IRemoteObject> &token);
151 
152     /**
153      * ScheduleCommandAbilityDoneLocked, service ability call this interface while session was onCommanded.
154      *
155      * @param token,service ability's token.
156      * @return Returns ERR_OK on success, others on failure.
157      */
158     int ScheduleCommandAbilityDoneLocked(const sptr<IRemoteObject> &token);
159 
160     /**
161      * GetServiceRecordByElementName.
162      *
163      * @param element, service ability's element.
164      * @return Returns AbilityRecord shared_ptr.
165      */
166     std::shared_ptr<AbilityRecord> GetServiceRecordByElementName(const std::string &element);
167 
168     std::shared_ptr<AbilityRecord> GetExtensionByTokenFromSeriveMap(const sptr<IRemoteObject> &token);
169     std::shared_ptr<AbilityRecord> GetExtensionByTokenFromTerminatingMap(const sptr<IRemoteObject> &token);
170     ConnectListType GetConnectRecordListByCallback(sptr<IAbilityConnection> callback);
171 
172     void GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info,
173         const int32_t userId, bool isPerm);
174 
175     void GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm);
176 
177     void GetExtensionRunningInfo(std::shared_ptr<AbilityRecord> &abilityRecord, const int32_t userId,
178         std::vector<ExtensionRunningInfo> &info);
179 
180     /**
181      * SetEventHandler.
182      *
183      * @param handler,EventHandler
184      */
SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & handler)185     inline void SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
186     {
187         eventHandler_ = handler;
188     }
189 
190     /**
191      * GetConnectMap.
192      *
193      * @return Returns connection record list.
194      */
GetConnectMap()195     inline const ConnectMapType &GetConnectMap() const
196     {
197         return connectMap_;
198     }
199 
200     /**
201      * GetServiceMap.
202      *
203      * @return Returns service ability record map.
204      */
GetServiceMap()205     inline const ServiceMapType &GetServiceMap() const
206     {
207         return serviceMap_;
208     }
209 
210     /**
211      * OnAbilityDied.
212      *
213      * @param abilityRecord, service ability record.
214      */
215     void OnAbilityDied(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
216 
217     void DumpState(std::vector<std::string> &info, bool isClient, const std::string &args = "");
218 
219     void DumpStateByUri(std::vector<std::string> &info, bool isClient, const std::string &args,
220         std::vector<std::string> &params);
221 
222     void StopAllExtensions();
223 
224     void StartRootLauncher(const std::shared_ptr<AbilityRecord> &abilityRecord);
225     void OnTimeOut(uint32_t msgId, int64_t eventId);
226 
227     // MSG 0 - 20 represents timeout message
228     static constexpr uint32_t LOAD_TIMEOUT_MSG = 0;
229     static constexpr uint32_t CONNECT_TIMEOUT_MSG = 1;
230 
231 private:
232     /**
233      * StartAbilityLocked with request.
234      *
235      * @param abilityRequest, the request of the service ability to start.
236      * @return Returns ERR_OK on success, others on failure.
237      */
238     int StartAbilityLocked(const AbilityRequest &abilityRequest);
239 
240     /**
241      * TerminateAbilityLocked with token and result want.
242      *
243      * @param token, the token of service type's ability to terminate.
244      * @param resultCode, the result code of service type's ability to terminate.
245      * @param resultWant, the result want for service type's ability to terminate.
246      * @return Returns ERR_OK on success, others on failure.
247      */
248     int TerminateAbilityLocked(const sptr<IRemoteObject> &token);
249 
250     /**
251      * Destroys this Service ability if the number of times it
252      * has been started equals the number represented by
253      * the given startId.
254      *
255      * @param token ability's token.
256      * @param startId is incremented by 1 every time this ability is started.
257      * @return Returns true if the startId matches the number of startup times
258      * and this Service ability will be destroyed; returns false otherwise.
259      */
260     int TerminateAbilityResultLocked(const sptr<IRemoteObject> &token, int startId);
261 
262     /**
263      * StopAbilityLocked with request.
264      *
265      * @param abilityRequest, the request of the service ability to start.
266      * @return Returns ERR_OK on success, others on failure.
267      */
268     int StopServiceAbilityLocked(const AbilityRequest &abilityRequest);
269 
270     /**
271      * LoadAbility.
272      *
273      * @param abilityRecord, the ptr of the ability to load.
274      */
275     void LoadAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
276 
277     /**
278      * ConnectAbility.Schedule connect ability
279      *
280      * @param abilityRecord, the ptr of the ability to connect.
281      */
282     void ConnectAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
283 
284     /**
285      * CommandAbility. Schedule command ability
286      *
287      * @param abilityRecord, the ptr of the ability to command.
288      */
289     void CommandAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
290 
291     /**
292      * CompleteCommandAbility. complete command ability
293      *
294      * @param abilityRecord, the ptr of the ability to command.
295      */
296     void CompleteCommandAbility(std::shared_ptr<AbilityRecord> abilityRecord);
297 
298     /**
299      * TerminateDone.
300      *
301      * @param abilityRecord, the ptr of the ability to terminate.
302      */
303     void TerminateDone(const std::shared_ptr<AbilityRecord> &abilityRecord);
304 
305     /**
306      * dispatch service ability life cycle .
307      *
308      * @param abilityRecord.
309      * @param state.
310      */
311     int DispatchInactive(const std::shared_ptr<AbilityRecord> &abilityRecord, int state);
312     int DispatchTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord);
313 
314     void HandleStartTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, int resultCode);
315     void HandleStopTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
316     void HandleTerminateDisconnectTask(const ConnectListType& connectlist);
317     void HandleCommandTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
318     void HandleRestartResidentTask(const AbilityRequest &abilityRequest);
319 
320     /**
321      * IsAbilityConnected.
322      *
323      * @param abilityRecord, the ptr of the connected ability.
324      * @param connectRecordList, connect record list.
325      * @return true: ability is connected, false: ability is not connected
326      */
327     bool IsAbilityConnected(const std::shared_ptr<AbilityRecord> &abilityRecord,
328         const std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList);
329 
330     /**
331      * RemoveConnectionRecordFromMap.
332      *
333      * @param connect, the ptr of the connect record.
334      */
335     void RemoveConnectionRecordFromMap(const std::shared_ptr<ConnectionRecord> &connect);
336 
337     /**
338      * RemoveServiceAbility.
339      *
340      * @param service, the ptr of the ability record.
341      */
342     void RemoveServiceAbility(const std::shared_ptr<AbilityRecord> &service);
343 
344     /**
345      * GetOrCreateServiceRecord.
346      *
347      * @param abilityRequest, Special want for service type's ability.
348      * @param isCreatedByConnect, whether is created by connect ability mode.
349      * @param targetAbilityRecord, the target service ability record.
350      * @param isLoadedAbility, whether the target ability has been loaded.
351      */
352     void GetOrCreateServiceRecord(const AbilityRequest &abilityRequest, const bool isCreatedByConnect,
353         std::shared_ptr<AbilityRecord> &targetAbilityRecord, bool &isLoadedAbility);
354 
355     /**
356      * GetConnectRecordListFromMap.
357      *
358      * @param connect, callback object.
359      * @param isCreatedByConnect, whether is created by connect ability mode.
360      * @param connectRecordList, the target connectRecordList.
361      * @param isCallbackConnected, whether the callback has been connected.
362      */
363     void GetConnectRecordListFromMap(
364         const sptr<IAbilityConnection> &connect, std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList);
365 
366     /**
367      * AddConnectDeathRecipient.
368      *
369      * @param connect, callback object.
370      */
371     void AddConnectDeathRecipient(const sptr<IAbilityConnection> &connect);
372 
373     /**
374      * RemoteConnectDeathRecipient.
375      *
376      * @param connect, callback object.
377      */
378     void RemoveConnectDeathRecipient(const sptr<IAbilityConnection> &connect);
379 
380     /**
381      * RemoteConnectDeathRecipient.
382      *
383      * @param remote, callback object.
384      */
385     void OnCallBackDied(const wptr<IRemoteObject> &remote);
386 
387     /**
388      * HandleOnCallBackDied.
389      *
390      * @param connect, callback object.
391      */
392     void HandleCallBackDiedTask(const sptr<IRemoteObject> &connect);
393 
394     /**
395      * HandleOnCallBackDied.
396      *
397      * @param abilityRecord, died ability.
398      */
399     void HandleAbilityDiedTask(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
400 
401     void RestartAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
402 
403     /**
404      * PostTimeOutTask.
405      *
406      * @param abilityRecord, ability.
407      * @param messageId, message id.
408      */
409     void PostTimeOutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, uint32_t messageId);
410 
411     void PostRestartResidentTask(const AbilityRequest &abilityRequest);
412 
413     bool IsAbilityNeedKeepAlive(const std::shared_ptr<AbilityRecord> &abilityRecord);
414 
415     void ProcessPreload(const std::shared_ptr<AbilityRecord> &record) const;
416 
417     std::shared_ptr<AbilityRecord> GetAbilityRecordByEventId(int64_t eventId);
418     void HandleInactiveTimeout(const std::shared_ptr<AbilityRecord> &ability);
419     void MoveToTerminatingMap(const std::shared_ptr<AbilityRecord>& abilityRecord);
420 
421 private:
422     const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask";
423     const std::string TASK_ON_ABILITY_DIED = "OnAbilityDiedTask";
424 
425     std::recursive_mutex Lock_;
426     ConnectMapType connectMap_;
427     ServiceMapType serviceMap_;
428     ServiceMapType terminatingExtensionMap_;
429     RecipientMapType recipientMap_;
430     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
431     int userId_;
432     std::vector<AbilityRequest> restartResidentTaskList_;
433 
434     DISALLOW_COPY_AND_MOVE(AbilityConnectManager);
435 };
436 }  // namespace AAFwk
437 }  // namespace OHOS
438 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_CONNECT_MANAGER_H
439