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_ABILITY_RUNTIME_CONNECTION_RECORD_H 17 #define OHOS_ABILITY_RUNTIME_CONNECTION_RECORD_H 18 19 #include <mutex> 20 #include "ability_connect_callback_interface.h" 21 #include "ability_record.h" 22 #include "extension_config.h" 23 #include "nocopyable.h" 24 25 namespace OHOS { 26 namespace AAFwk { 27 /** 28 * @enum ConnectionState 29 * ConnectionState defines the state of connect ability. 30 */ 31 enum class ConnectionState { INIT, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED }; 32 /** 33 * @class ConnectionRecord 34 * ConnectionRecord,This class is used to record information about a connection. 35 */ 36 class ConnectionRecord : public std::enable_shared_from_this<ConnectionRecord> { 37 public: 38 ConnectionRecord(const sptr<IRemoteObject> &callerToken, const std::shared_ptr<AbilityRecord> &targetService, 39 const sptr<IAbilityConnection> &connCallback); 40 virtual ~ConnectionRecord(); 41 42 /** 43 * create a connection record by caller token , service ability and call back ipc object. 44 * 45 * @param callerToken, the token of caller ability. 46 * @param targetService, target service ability. 47 * @param callback, call back (ipc object). 48 * @return Return the connect record. 49 */ 50 static std::shared_ptr<ConnectionRecord> CreateConnectionRecord(const sptr<IRemoteObject> &callerToken, 51 const std::shared_ptr<AbilityRecord> &targetService, const sptr<IAbilityConnection> &connCallback); 52 53 /** 54 * set the connect state. 55 * 56 * @param state, target connection state. 57 */ 58 void SetConnectState(const ConnectionState &state); 59 60 /** 61 * get the connect state. 62 * 63 * @return state, target connection state. 64 */ 65 ConnectionState GetConnectState() const; 66 67 /** 68 * get the token of the ability. 69 * 70 * @return token. 71 */ 72 sptr<IRemoteObject> GetToken() const; 73 74 /** 75 * get the ability record from connection record. 76 * 77 * @return AbilityRecord. 78 */ 79 std::shared_ptr<AbilityRecord> GetAbilityRecord() const; 80 81 sptr<IAbilityConnection> GetAbilityConnectCallback() const; 82 83 /** 84 * disconnect the service ability. 85 * 86 * @return Returns ERR_OK on success, others on failure. 87 */ 88 int DisconnectAbility(); 89 90 /** 91 * force to disconnect time out event. 92 * 93 */ 94 void DisconnectTimeout(); 95 96 /** 97 * complete connect ability and invoke callback. 98 * 99 */ 100 void CompleteConnect(); 101 102 /** 103 * complete disconnect ability and invoke callback. 104 * 105 */ 106 void CompleteDisconnect(int resultCode, bool isCallerDied, bool isTargetDied = false); 107 108 /** 109 * scheduler target service disconnect done. 110 * 111 */ 112 void ScheduleDisconnectAbilityDone(); 113 114 /** 115 * scheduler target service Connect done. 116 * 117 */ 118 void ScheduleConnectAbilityDone(); 119 120 /** 121 * cancel connect timeout task. 122 * 123 */ 124 void CancelConnectTimeoutTask(); 125 126 /** 127 * get connection record id. 128 * 129 */ GetRecordId()130 inline int GetRecordId() const 131 { 132 return recordId_; 133 } 134 135 void ClearConnCallBack(); 136 137 std::string ConvertConnectionState(const ConnectionState &state) const; 138 139 void Dump(std::vector<std::string> &info) const; 140 141 void AttachCallerInfo(); 142 int32_t GetCallerUid() const; 143 int32_t GetCallerPid() const; 144 uint32_t GetCallerTokenId() const; 145 std::string GetCallerName() const; 146 sptr<IRemoteObject> GetTargetToken() const; 147 sptr<IRemoteObject> GetConnection() const; 148 149 void SetConnectWant(const Want &want); 150 Want GetConnectWant() const; 151 private: 152 static int64_t connectRecordId; 153 int recordId_ = 0; // record id 154 ConnectionState state_; // service connection state 155 sptr<IRemoteObject> callerToken_ = nullptr; // from:caller token 156 std::shared_ptr<AbilityRecord> targetService_ = nullptr; // target:service need to be connected 157 158 mutable std::mutex callbackMutex_; 159 sptr<IAbilityConnection> connCallback_ = nullptr; // service connect callback 160 int32_t callerUid_ = 0; // caller uid 161 int32_t callerPid_ = 0; // caller pid 162 uint32_t callerTokenId_ = 0; // caller pid 163 std::string callerName_; // caller bundleName or processName 164 165 Want connectWant_; 166 167 DISALLOW_COPY_AND_MOVE(ConnectionRecord); 168 }; 169 } // namespace AAFwk 170 } // namespace OHOS 171 #endif // OHOS_ABILITY_RUNTIME_CONNECTION_RECORD_H 172