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 * Suspend the service ability. 96 * 97 * @return Returns ERR_OK on success, others on failure. 98 */ 99 int32_t SuspendExtensionAbility(); 100 101 /** 102 * Resume the service ability. 103 * 104 * @return Returns ERR_OK on success, others on failure. 105 */ 106 int32_t ResumeExtensionAbility(); 107 108 /** 109 * force to disconnect time out event. 110 * 111 */ 112 void DisconnectTimeout(); 113 114 /** 115 * complete connect ability and invoke callback. 116 * 117 */ 118 void CompleteConnect(); 119 120 /** 121 * complete connect ability and just try to invoke callback. 122 * 123 */ 124 void CompleteConnectAndOnlyCallConnectDone(); 125 126 /** 127 * complete disconnect ability and invoke callback. 128 * 129 */ 130 void CompleteDisconnect(int resultCode, bool isCallerDied, bool isTargetDied = false); 131 132 /** 133 * scheduler target service disconnect done. 134 * 135 */ 136 void ScheduleDisconnectAbilityDone(); 137 138 /** 139 * scheduler target service Connect done. 140 * 141 */ 142 void ScheduleConnectAbilityDone(); 143 144 /** 145 * cancel connect timeout task. 146 * 147 */ 148 void CancelConnectTimeoutTask(); 149 150 /** 151 * get connection record id. 152 * 153 */ GetRecordId()154 inline int GetRecordId() const 155 { 156 return recordId_; 157 } 158 159 void ClearConnCallBack(); 160 161 std::string ConvertConnectionState(const ConnectionState &state) const; 162 163 void Dump(std::vector<std::string> &info) const; 164 165 void AttachCallerInfo(); 166 int32_t GetCallerUid() const; 167 int32_t GetCallerPid() const; 168 uint32_t GetCallerTokenId() const; 169 std::string GetCallerName() const; 170 sptr<IRemoteObject> GetTargetToken() const; 171 sptr<IRemoteObject> GetConnection() const; 172 173 void SetConnectWant(const Want &want); 174 Want GetConnectWant() const; 175 private: 176 static int64_t connectRecordId; 177 int32_t callerUid_ = 0; // caller uid 178 int32_t callerPid_ = 0; // caller pid 179 uint32_t callerTokenId_ = 0; // caller pid 180 int recordId_ = 0; // record id 181 ConnectionState state_; // service connection state 182 sptr<IRemoteObject> callerToken_ = nullptr; // from:caller token 183 std::shared_ptr<AbilityRecord> targetService_ = nullptr; // target:service need to be connected 184 185 mutable std::mutex callbackMutex_; 186 sptr<IAbilityConnection> connCallback_ = nullptr; // service connect callback 187 std::string callerName_; // caller bundleName or processName 188 189 Want connectWant_; 190 std::weak_ptr<AbilityConnectManager> abilityConnectManager_; 191 192 DISALLOW_COPY_AND_MOVE(ConnectionRecord); 193 }; 194 } // namespace AAFwk 195 } // namespace OHOS 196 #endif // OHOS_ABILITY_RUNTIME_CONNECTION_RECORD_H 197