55#include " device/fido/enclave/enclave_websocket_client.h"
66
77#include < limits>
8+ #include < utility>
89
910#include " components/device_event_log/device_event_log.h"
1011#include " device/fido/fido_constants.h"
@@ -90,6 +91,7 @@ void EnclaveWebSocketClient::Write(base::span<const uint8_t> data) {
9091 data.size () > std::numeric_limits<uint32_t >::max ()) {
9192 FIDO_LOG (ERROR) << " Invalid WebSocket write." ;
9293 ClosePipe (SocketStatus::kError );
94+ // `this` may have been deleted at this point.
9395 return ;
9496 }
9597
@@ -103,6 +105,7 @@ void EnclaveWebSocketClient::Write(base::span<const uint8_t> data) {
103105 }
104106
105107 InternalWrite (data);
108+ // `this` may have been deleted at this point.
106109}
107110
108111void EnclaveWebSocketClient::Connect () {
@@ -144,6 +147,7 @@ void EnclaveWebSocketClient::InternalWrite(base::span<const uint8_t> data) {
144147 if (result != MOJO_RESULT_OK) {
145148 FIDO_LOG (ERROR) << " Failed to write to WebSocket." ;
146149 ClosePipe (SocketStatus::kError );
150+ // `this` may have been deleted at this point.
147151 }
148152}
149153
@@ -160,6 +164,7 @@ void EnclaveWebSocketClient::OnFailure(const std::string& message,
160164 << net_error << " , " << response_code;
161165
162166 ClosePipe (SocketStatus::kError );
167+ // `this` may have been deleted at this point.
163168}
164169
165170void EnclaveWebSocketClient::OnConnectionEstablished (
@@ -194,8 +199,9 @@ void EnclaveWebSocketClient::OnConnectionEstablished(
194199 state_ = State::kOpen ;
195200
196201 if (pending_write_data_) {
197- InternalWrite (*pending_write_data_);
198- pending_write_data_ = absl::nullopt ;
202+ auto write_data = std::exchange (pending_write_data_, std::nullopt ).value ();
203+ InternalWrite (write_data);
204+ // `this` may have been deleted at this point.
199205 }
200206}
201207
@@ -211,6 +217,7 @@ void EnclaveWebSocketClient::OnDataFrame(
211217 if (data_len == 0 ) {
212218 if (finish) {
213219 ProcessCompletedResponse ();
220+ // `this` may have been deleted at this point.
214221 }
215222 return ;
216223 }
@@ -224,13 +231,15 @@ void EnclaveWebSocketClient::OnDataFrame(
224231 FIDO_LOG (ERROR) << " Invalid WebSocket frame (type: "
225232 << static_cast <int >(type) << " , len: " << data_len << " )" ;
226233 ClosePipe (SocketStatus::kError );
234+ // `this` may have been deleted at this point.
227235 return ;
228236 }
229237
230238 pending_read_data_.resize (new_size);
231239 pending_read_finished_ = finish;
232240 client_receiver_.Pause ();
233241 ReadFromDataPipe (MOJO_RESULT_OK, mojo::HandleSignalsState ());
242+ // `this` may have been deleted at this point.
234243}
235244
236245void EnclaveWebSocketClient::OnDropChannel (bool was_clean,
@@ -240,6 +249,7 @@ void EnclaveWebSocketClient::OnDropChannel(bool was_clean,
240249 CHECK (state_ == State::kOpen || state_ == State::kConnecting );
241250
242251 ClosePipe (SocketStatus::kSocketClosed );
252+ // `this` may have been deleted at this point.
243253}
244254
245255void EnclaveWebSocketClient::OnClosingHandshake () {}
@@ -268,6 +278,7 @@ void EnclaveWebSocketClient::ReadFromDataPipe(MojoResult,
268278 client_receiver_.Resume ();
269279 if (pending_read_finished_) {
270280 ProcessCompletedResponse ();
281+ // `this` may have been deleted at this point.
271282 }
272283 }
273284 } else if (result == MOJO_RESULT_SHOULD_WAIT) {
@@ -276,6 +287,7 @@ void EnclaveWebSocketClient::ReadFromDataPipe(MojoResult,
276287 FIDO_LOG (ERROR) << " Reading WebSocket frame failed: "
277288 << static_cast <int >(result);
278289 ClosePipe (SocketStatus::kError );
290+ // `this` may have been deleted at this point.
279291 }
280292}
281293
@@ -301,6 +313,7 @@ void EnclaveWebSocketClient::ClosePipe(SocketStatus status) {
301313
302314void EnclaveWebSocketClient::OnMojoPipeDisconnect () {
303315 ClosePipe (SocketStatus::kSocketClosed );
316+ // `this` may have been deleted at this point.
304317}
305318
306319} // namespace device::enclave
0 commit comments