• 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_AAFWK_ABILITY_CONNECT_MANAGER_H
17 #define OHOS_AAFWK_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 "ohos/aafwk/content/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     /**
125      * OnAbilityRequestDone, app manager service call this interface after ability request done.
126      *
127      * @param token, ability's token.
128      * @param state, the state of ability lift cycle.
129      */
130     void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state);
131 
132     void OnAppStateChanged(const AppInfo &info);
133 
134     /**
135      * AbilityTransitionDone, ability call this interface after lift cycle was changed.
136      *
137      * @param token, ability's token.
138      * @param state, the state of ability lift cycle.
139      * @return Returns ERR_OK on success, others on failure.
140      */
141     int AbilityTransitionDone(const sptr<IRemoteObject> &token, int state);
142 
143     /**
144      * ScheduleConnectAbilityDoneLocked, service ability call this interface while session was connected.
145      *
146      * @param token, service ability's token.
147      * @param remoteObject, the session proxy of service ability.
148      * @return Returns ERR_OK on success, others on failure.
149      */
150     int ScheduleConnectAbilityDoneLocked(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &remoteObject);
151 
152     /**
153      * ScheduleDisconnectAbilityDone, service ability call this interface while session was disconnected.
154      *
155      * @param token,service ability's token.
156      * @return Returns ERR_OK on success, others on failure.
157      */
158     int ScheduleDisconnectAbilityDoneLocked(const sptr<IRemoteObject> &token);
159 
160     /**
161      * ScheduleCommandAbilityDoneLocked, service ability call this interface while session was onCommanded.
162      *
163      * @param token,service ability's token.
164      * @return Returns ERR_OK on success, others on failure.
165      */
166     int ScheduleCommandAbilityDoneLocked(const sptr<IRemoteObject> &token);
167 
168     /**
169      * GetServiceRecordByElementName.
170      *
171      * @param element, service ability's element.
172      * @return Returns AbilityRecord shared_ptr.
173      */
174     std::shared_ptr<AbilityRecord> GetServiceRecordByElementName(const std::string &element);
175 
176     /**
177      * GetServiceRecordByToken.
178      *
179      * @param token, service ability's token.
180      * @return Returns AbilityRecord shared_ptr.
181      */
182     std::shared_ptr<AbilityRecord> GetServiceRecordByToken(const sptr<IRemoteObject> &token);
183     ConnectListType GetConnectRecordListByCallback(sptr<IAbilityConnection> callback);
184     void RemoveAll();
185 
186     void GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info,
187         const int32_t userId, bool isPerm);
188 
189     void GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm);
190 
191     void GetExtensionRunningInfo(std::shared_ptr<AbilityRecord> &abilityRecord, const int32_t userId,
192         std::vector<ExtensionRunningInfo> &info);
193 
194     /**
195      * SetEventHandler.
196      *
197      * @param handler,EventHandler
198      */
SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & handler)199     inline void SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
200     {
201         eventHandler_ = handler;
202     }
203 
204     /**
205      * GetConnectMap.
206      *
207      * @return Returns connection record list.
208      */
GetConnectMap()209     inline const ConnectMapType &GetConnectMap() const
210     {
211         return connectMap_;
212     }
213 
214     /**
215      * GetServiceMap.
216      *
217      * @return Returns service ability record map.
218      */
GetServiceMap()219     inline const ServiceMapType &GetServiceMap() const
220     {
221         return serviceMap_;
222     }
223 
224     /**
225      * OnAbilityDied.
226      *
227      * @param abilityRecord, service ability record.
228      */
229     void OnAbilityDied(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
230 
231     void DumpState(std::vector<std::string> &info, bool isClient, const std::string &args = "") const;
232 
233     void StopAllExtensions();
234 
235     void StartRootLauncher(const std::shared_ptr<AbilityRecord> &abilityRecord);
236     void OnTimeOut(uint32_t msgId, int64_t eventId);
237 
238     // MSG 0 - 20 represents timeout message
239     static constexpr uint32_t LOAD_TIMEOUT_MSG = 0;
240     static constexpr uint32_t CONNECT_TIMEOUT_MSG = 1;
241 
242 private:
243     /**
244      * StartAbilityLocked with request.
245      *
246      * @param abilityRequest, the request of the service ability to start.
247      * @return Returns ERR_OK on success, others on failure.
248      */
249     int StartAbilityLocked(const AbilityRequest &abilityRequest);
250 
251     /**
252      * TerminateAbilityLocked with token and result want.
253      *
254      * @param token, the token of service type's ability to terminate.
255      * @param resultCode, the result code of service type's ability to terminate.
256      * @param resultWant, the result want for service type's ability to terminate.
257      * @return Returns ERR_OK on success, others on failure.
258      */
259     int TerminateAbilityLocked(const sptr<IRemoteObject> &token);
260 
261     /**
262      * Destroys this Service ability if the number of times it
263      * has been started equals the number represented by
264      * the given startId.
265      *
266      * @param token ability's token.
267      * @param startId is incremented by 1 every time this ability is started.
268      * @return Returns true if the startId matches the number of startup times
269      * and this Service ability will be destroyed; returns false otherwise.
270      */
271     int TerminateAbilityResultLocked(const sptr<IRemoteObject> &token, int startId);
272 
273     /**
274      * StopAbilityLocked with request.
275      *
276      * @param abilityRequest, the request of the service ability to start.
277      * @return Returns ERR_OK on success, others on failure.
278      */
279     int StopServiceAbilityLocked(const AbilityRequest &abilityRequest);
280 
281     /**
282      * LoadAbility.
283      *
284      * @param abilityRecord, the ptr of the ability to load.
285      */
286     void LoadAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
287 
288     /**
289      * ConnectAbility.Schedule connect ability
290      *
291      * @param abilityRecord, the ptr of the ability to connect.
292      */
293     void ConnectAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
294 
295     /**
296      * CommandAbility. Schedule command ability
297      *
298      * @param abilityRecord, the ptr of the ability to command.
299      */
300     void CommandAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
301 
302     /**
303      * CompleteCommandAbility. complete command ability
304      *
305      * @param abilityRecord, the ptr of the ability to command.
306      */
307     void CompleteCommandAbility(std::shared_ptr<AbilityRecord> abilityRecord);
308 
309     /**
310      * TerminateDone.
311      *
312      * @param abilityRecord, the ptr of the ability to terminate.
313      */
314     void TerminateDone(const std::shared_ptr<AbilityRecord> &abilityRecord);
315 
316     /**
317      * dispatch service ability life cycle .
318      *
319      * @param abilityRecord.
320      * @param state.
321      */
322     int DispatchInactive(const std::shared_ptr<AbilityRecord> &abilityRecord, int state);
323     int DispatchTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord);
324 
325     void HandleStartTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, int resultCode);
326     void HandleStopTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
327     void HandleDisconnectTask(const ConnectListType &connectlist);
328     void HandleTerminateDisconnectTask(const ConnectListType& connectlist);
329     void HandleCommandTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
330 
331     /**
332      * IsAbilityConnected.
333      *
334      * @param abilityRecord, the ptr of the connected ability.
335      * @param connectRecordList, connect record list.
336      * @return true: ability is connected, false: ability is not connected
337      */
338     bool IsAbilityConnected(const std::shared_ptr<AbilityRecord> &abilityRecord,
339         const std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList);
340 
341     /**
342      * RemoveConnectionRecordFromMap.
343      *
344      * @param connect, the ptr of the connect record.
345      */
346     void RemoveConnectionRecordFromMap(const std::shared_ptr<ConnectionRecord> &connect);
347 
348     /**
349      * RemoveServiceAbility.
350      *
351      * @param service, the ptr of the ability record.
352      */
353     void RemoveServiceAbility(const std::shared_ptr<AbilityRecord> &service);
354 
355     /**
356      * GetOrCreateServiceRecord.
357      *
358      * @param abilityRequest, Special want for service type's ability.
359      * @param isCreatedByConnect, whether is created by connect ability mode.
360      * @param targetAbilityRecord, the target service ability record.
361      * @param isLoadedAbility, whether the target ability has been loaded.
362      */
363     void GetOrCreateServiceRecord(const AbilityRequest &abilityRequest, const bool isCreatedByConnect,
364         std::shared_ptr<AbilityRecord> &targetAbilityRecord, bool &isLoadedAbility);
365 
366     /**
367      * GetConnectRecordListFromMap.
368      *
369      * @param connect, callback object.
370      * @param isCreatedByConnect, whether is created by connect ability mode.
371      * @param connectRecordList, the target connectRecordList.
372      * @param isCallbackConnected, whether the callback has been connected.
373      */
374     void GetConnectRecordListFromMap(
375         const sptr<IAbilityConnection> &connect, std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList);
376 
377     /**
378      * AddConnectDeathRecipient.
379      *
380      * @param connect, callback object.
381      */
382     void AddConnectDeathRecipient(const sptr<IAbilityConnection> &connect);
383 
384     /**
385      * RemoteConnectDeathRecipient.
386      *
387      * @param connect, callback object.
388      */
389     void RemoveConnectDeathRecipient(const sptr<IAbilityConnection> &connect);
390 
391     /**
392      * RemoteConnectDeathRecipient.
393      *
394      * @param remote, callback object.
395      */
396     void OnCallBackDied(const wptr<IRemoteObject> &remote);
397 
398     /**
399      * HandleOnCallBackDied.
400      *
401      * @param connect, callback object.
402      */
403     void HandleCallBackDiedTask(const sptr<IRemoteObject> &connect);
404 
405     /**
406      * HandleOnCallBackDied.
407      *
408      * @param abilityRecord, died ability.
409      */
410     void HandleAbilityDiedTask(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId);
411 
412     /**
413      * PostTimeOutTask.
414      *
415      * @param abilityRecord, ability.
416      * @param messageId, message id.
417      */
418     void PostTimeOutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, uint32_t messageId);
419 
420     bool IsAbilityNeedRestart(const std::shared_ptr<AbilityRecord> &abilityRecord);
421 
422     std::shared_ptr<AbilityRecord> GetAbilityRecordByEventId(int64_t eventId);
423     void HandleInactiveTimeout(const std::shared_ptr<AbilityRecord> &ability);
424 
425 private:
426     const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask";
427     const std::string TASK_ON_ABILITY_DIED = "OnAbilityDiedTask";
428 
429     std::recursive_mutex Lock_;
430     ConnectMapType connectMap_;
431     ServiceMapType serviceMap_;
432     RecipientMapType recipientMap_;
433     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
434     int userId_;
435 
436     DISALLOW_COPY_AND_MOVE(AbilityConnectManager);
437 };
438 }  // namespace AAFwk
439 }  // namespace OHOS
440 #endif  // OHOS_AAFWK_ABILITY_CONNECT_MANAGER_H
441