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