/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_AAFWK_ABILITY_CONNECT_MANAGER_H #define OHOS_AAFWK_ABILITY_CONNECT_MANAGER_H #include #include #include #include "ability_connect_callback_interface.h" #include "ability_event_handler.h" #include "ability_record.h" #include "connection_record.h" #include "element_name.h" #include "ohos/aafwk/content/want.h" #include "iremote_object.h" #include "nocopyable.h" namespace OHOS { namespace AAFwk { using OHOS::AppExecFwk::AbilityType; /** * @class AbilityConnectManager * AbilityConnectManager provides a facility for managing service ability connection. */ class AbilityConnectManager : public std::enable_shared_from_this { public: using ConnectMapType = std::map, std::list>>; using ServiceMapType = std::map>; using ConnectListType = std::list>; using RecipientMapType = std::map, sptr>; AbilityConnectManager(); virtual ~AbilityConnectManager(); /** * StartAbility with request. * * @param abilityRequest, the request of the service ability to start. * @return Returns ERR_OK on success, others on failure. */ int StartAbility(const AbilityRequest &abilityRequest); /** * TerminateAbility with token and result want. * * @param token, the token of service type's ability to terminate. * @return Returns ERR_OK on success, others on failure. */ int TerminateAbility(const sptr &token); /** * TerminateAbility, terminate the special ability. * * @param caller, caller ability record. * @param requestCode, abililty request code * @return Returns ERR_OK on success, others on failure. */ int TerminateAbility(const std::shared_ptr &caller, int requestCode); /** * StopServiceAbility with request. * * @param abilityRequest, request. * @return Returns ERR_OK on success, others on failure. */ int StopServiceAbility(const AbilityRequest &abilityRequest); /** * Destroys this Service ability if the number of times it * has been started equals the number represented by * the given startId. * * @param token ability's token. * @param startId is incremented by 1 every time this ability is started. * @return Returns true if the startId matches the number of startup times * and this Service ability will be destroyed; returns false otherwise. */ int TerminateAbilityResult(const sptr &token, int startId); /** * ConnectAbilityLocked, connect session with service ability. * * @param abilityRequest, Special want for service type's ability. * @param connect, Callback used to notify caller the result of connecting or disconnecting. * @param callerToken, caller ability token. * @return Returns ERR_OK on success, others on failure. */ int ConnectAbilityLocked(const AbilityRequest &abilityRequest, const sptr &connect, const sptr &callerToken); /** * DisconnectAbilityLocked, disconnect session with callback. * * @param connect, Callback used to notify caller the result of connecting or disconnecting. * @return Returns ERR_OK on success, others on failure. */ int DisconnectAbilityLocked(const sptr &connect); /** * AttachAbilityThreadLocked, ability call this interface after loaded. * * @param scheduler, the interface handler of kit ability. * @param token, ability's token. * @return Returns ERR_OK on success, others on failure. */ int AttachAbilityThreadLocked(const sptr &scheduler, const sptr &token); /** * OnAbilityRequestDone, app manager service call this interface after ability request done. * * @param token, ability's token. * @param state, the state of ability lift cycle. */ void OnAbilityRequestDone(const sptr &token, const int32_t state); void OnAppStateChanged(const AppInfo &info); /** * AbilityTransitionDone, ability call this interface after lift cycle was changed. * * @param token, ability's token. * @param state, the state of ability lift cycle. * @return Returns ERR_OK on success, others on failure. */ int AbilityTransitionDone(const sptr &token, int state); /** * ScheduleConnectAbilityDoneLocked, service ability call this interface while session was connected. * * @param token, service ability's token. * @param remoteObject, the session proxy of service ability. * @return Returns ERR_OK on success, others on failure. */ int ScheduleConnectAbilityDoneLocked(const sptr &token, const sptr &remoteObject); /** * ScheduleDisconnectAbilityDone, service ability call this interface while session was disconnected. * * @param token,service ability's token. * @return Returns ERR_OK on success, others on failure. */ int ScheduleDisconnectAbilityDoneLocked(const sptr &token); /** * ScheduleCommandAbilityDoneLocked, service ability call this interface while session was onCommanded. * * @param token,service ability's token. * @return Returns ERR_OK on success, others on failure. */ int ScheduleCommandAbilityDoneLocked(const sptr &token); /** * GetServiceRecordByElementName. * * @param element, service ability's element. * @return Returns AbilityRecord shared_ptr. */ std::shared_ptr GetServiceRecordByElementName(const std::string &element); /** * GetServiceRecordByToken. * * @param token, service ability's token. * @return Returns AbilityRecord shared_ptr. */ std::shared_ptr GetServiceRecordByToken(const sptr &token); ConnectListType GetConnectRecordListByCallback(sptr callback); void RemoveAll(); /** * SetEventHandler. * * @param handler,EventHandler */ inline void SetEventHandler(const std::shared_ptr &handler) { eventHandler_ = handler; } /** * GetConnectMap. * * @return Returns connection record list. */ inline const ConnectMapType &GetConnectMap() const { return connectMap_; } /** * GetServiceMap. * * @return Returns service ability record map. */ inline const ServiceMapType &GetServiceMap() const { return serviceMap_; } /** * OnAbilityDied. * * @param abilityRecord, service ability record. */ void OnAbilityDied(const std::shared_ptr &abilityRecord); void DumpState(std::vector &info, const std::string &args = "") const; // MSG 0 - 20 represents timeout message static constexpr uint32_t LOAD_TIMEOUT_MSG = 0; static constexpr uint32_t CONNECT_TIMEOUT_MSG = 1; private: /** * StartAbilityLocked with request. * * @param abilityRequest, the request of the service ability to start. * @return Returns ERR_OK on success, others on failure. */ int StartAbilityLocked(const AbilityRequest &abilityRequest); /** * TerminateAbilityLocked with token and result want. * * @param token, the token of service type's ability to terminate. * @param resultCode, the result code of service type's ability to terminate. * @param resultWant, the result want for service type's ability to terminate. * @return Returns ERR_OK on success, others on failure. */ int TerminateAbilityLocked(const sptr &token); /** * Destroys this Service ability if the number of times it * has been started equals the number represented by * the given startId. * * @param token ability's token. * @param startId is incremented by 1 every time this ability is started. * @return Returns true if the startId matches the number of startup times * and this Service ability will be destroyed; returns false otherwise. */ int TerminateAbilityResultLocked(const sptr &token, int startId); /** * StopAbilityLocked with request. * * @param abilityRequest, the request of the service ability to start. * @return Returns ERR_OK on success, others on failure. */ int StopServiceAbilityLocked(const AbilityRequest &abilityRequest); /** * LoadAbility. * * @param abilityRecord, the ptr of the ability to load. */ void LoadAbility(const std::shared_ptr &abilityRecord); /** * ConnectAbility.Schedule connect ability * * @param abilityRecord, the ptr of the ability to connect. */ void ConnectAbility(const std::shared_ptr &abilityRecord); /** * CommandAbility. Schedule command ability * * @param abilityRecord, the ptr of the ability to command. */ void CommandAbility(const std::shared_ptr &abilityRecord); /** * CompleteCommandAbility. complete command ability * * @param abilityRecord, the ptr of the ability to command. */ void CompleteCommandAbility(std::shared_ptr abilityRecord); /** * TerminateDone. * * @param abilityRecord, the ptr of the ability to terminate. */ void TerminateDone(const std::shared_ptr &abilityRecord); /** * dispatch service ability life cycle . * * @param abilityRecord. * @param state. */ int DispatchInactive(const std::shared_ptr &abilityRecord, int state); int DispatchTerminate(const std::shared_ptr &abilityRecord); void HandleStartTimeoutTask(const std::shared_ptr &abilityRecord, int resultCode); void HandleStopTimeoutTask(const std::shared_ptr &abilityRecord); void HandleDisconnectTask(const ConnectListType &connectlist); /** * IsAbilityConnected. * * @param abilityRecord, the ptr of the connected ability. * @param connectRecordList, connect record list. * @return true: ability is connected, false: ability is not connected */ bool IsAbilityConnected(const std::shared_ptr &abilityRecord, const std::list> &connectRecordList); /** * RemoveConnectionRecordFromMap. * * @param connect, the ptr of the connect record. */ void RemoveConnectionRecordFromMap(const std::shared_ptr &connect); /** * RemoveServiceAbility. * * @param service, the ptr of the ability record. */ void RemoveServiceAbility(const std::shared_ptr &service); /** * GetOrCreateServiceRecord. * * @param abilityRequest, Special want for service type's ability. * @param isCreatedByConnect, whether is created by connect ability mode. * @param targetAbilityRecord, the target service ability record. * @param isLoadedAbility, whether the target ability has been loaded. */ void GetOrCreateServiceRecord(const AbilityRequest &abilityRequest, const bool isCreatedByConnect, std::shared_ptr &targetAbilityRecord, bool &isLoadedAbility); /** * GetConnectRecordListFromMap. * * @param connect, callback object. * @param isCreatedByConnect, whether is created by connect ability mode. * @param connectRecordList, the target connectRecordList. * @param isCallbackConnected, whether the callback has been connected. */ void GetConnectRecordListFromMap( const sptr &connect, std::list> &connectRecordList); /** * AddConnectDeathRecipient. * * @param connect, callback object. */ void AddConnectDeathRecipient(const sptr &connect); /** * RemoteConnectDeathRecipient. * * @param connect, callback object. */ void RemoveConnectDeathRecipient(const sptr &connect); /** * RemoteConnectDeathRecipient. * * @param remote, callback object. */ void OnCallBackDied(const wptr &remote); /** * HandleOnCallBackDied. * * @param connect, callback object. */ void HandleCallBackDiedTask(const sptr &connect); /** * HandleOnCallBackDied. * * @param abilityRecord, died ability. */ void HandleAbilityDiedTask(const std::shared_ptr &abilityRecord); /** * PostTimeOutTask. * * @param abilityRecord, ability. * @param messageId, message id. */ void PostTimeOutTask(const std::shared_ptr &abilityRecord, uint32_t messageId); private: const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask"; const std::string TASK_ON_ABILITY_DIED = "OnAbilityDiedTask"; std::recursive_mutex Lock_; ConnectMapType connectMap_; ServiceMapType serviceMap_; RecipientMapType recipientMap_; std::shared_ptr eventHandler_; DISALLOW_COPY_AND_MOVE(AbilityConnectManager); }; } // namespace AAFwk } // namespace OHOS #endif // OHOS_AAFWK_ABILITY_CONNECT_MANAGER_H