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