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_CONNECTION_RECORD_H 17 #define OHOS_AAFWK_CONNECTION_RECORD_H 18 19 #include "ability_connect_callback_interface.h" 20 #include "ability_record.h" 21 #include "nocopyable.h" 22 23 namespace OHOS { 24 namespace AAFwk { 25 /** 26 * @enum ConnectionState 27 * ConnectionState defines the state of connect ability. 28 */ 29 enum class ConnectionState { INIT, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED }; 30 /** 31 * @class ConnectionRecord 32 * ConnectionRecord,This class is used to record information about a connection. 33 */ 34 class ConnectionRecord : public std::enable_shared_from_this<ConnectionRecord> { 35 public: 36 ConnectionRecord(const sptr<IRemoteObject> &callerToken, const std::shared_ptr<AbilityRecord> &targetService, 37 const sptr<IAbilityConnection> &connCallback); 38 virtual ~ConnectionRecord(); 39 40 /** 41 * create a connection record by caller token , service ability and call back ipc object. 42 * 43 * @param callerToken, the token of caller ability. 44 * @param targetService, target service ability. 45 * @param callback, call back (ipc object). 46 * @return Return the connect record. 47 */ 48 static std::shared_ptr<ConnectionRecord> CreateConnectionRecord(const sptr<IRemoteObject> &callerToken, 49 const std::shared_ptr<AbilityRecord> &targetService, const sptr<IAbilityConnection> &connCallback); 50 51 /** 52 * set the connect state. 53 * 54 * @param state, target connection state. 55 */ 56 void SetConnectState(const ConnectionState &state); 57 58 /** 59 * get the connect state. 60 * 61 * @return state, target connection state. 62 */ 63 ConnectionState GetConnectState() const; 64 65 /** 66 * get the token of the ability. 67 * 68 * @return token. 69 */ 70 sptr<IRemoteObject> GetToken() const; 71 72 /** 73 * get the ability record from connection record. 74 * 75 * @return AbilityRecord. 76 */ 77 std::shared_ptr<AbilityRecord> GetAbilityRecord() const; 78 79 sptr<IAbilityConnection> GetAbilityConnectCallback() const; 80 81 /** 82 * disconnect the service ability. 83 * 84 * @return Returns ERR_OK on success, others on failure. 85 */ 86 int DisconnectAbility(); 87 88 /** 89 * force to disconnect time out event. 90 * 91 */ 92 void DisconnectTimeout(); 93 94 /** 95 * complete connect ability and invoke callback. 96 * 97 */ 98 void CompleteConnect(int resultCode); 99 100 /** 101 * complete disconnect ability and invoke callback. 102 * 103 */ 104 void CompleteDisconnect(int resultCode, bool isDied); 105 106 /** 107 * scheduler target service disconnect done. 108 * 109 */ 110 void ScheduleDisconnectAbilityDone(); 111 112 /** 113 * scheduler target service Connect done. 114 * 115 */ 116 void ScheduleConnectAbilityDone(); 117 118 /** 119 * get connection record id. 120 * 121 */ GetRecordId()122 inline int GetRecordId() const 123 { 124 return recordId_; 125 } 126 127 void ClearConnCallBack(); 128 129 std::string ConvertConnectionState(const ConnectionState &state) const; 130 131 void Dump(std::vector<std::string> &info) const; 132 133 private: 134 static int64_t connectRecordId; 135 int recordId_; // record id 136 ConnectionState state_; // service connection state 137 sptr<IRemoteObject> callerToken_; // from:caller token 138 std::shared_ptr<AbilityRecord> targetService_; // target:service need to be connected 139 sptr<IAbilityConnection> connCallback_; // service connect callback 140 141 DISALLOW_COPY_AND_MOVE(ConnectionRecord); 142 }; 143 } // namespace AAFwk 144 } // namespace OHOS 145 #endif // OHOS_AAFWK_CONNECTION_RECORD_H