1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_SERVER_WEB_SOCKET_H_ 6 #define NET_SERVER_WEB_SOCKET_H_ 7 8 #include <memory> 9 #include <string> 10 #include <string_view> 11 12 #include "base/memory/raw_ptr.h" 13 #include "net/traffic_annotation/network_traffic_annotation.h" 14 #include "net/websockets/websocket_frame.h" 15 16 namespace net { 17 18 class HttpConnection; 19 class HttpServer; 20 class HttpServerRequestInfo; 21 class WebSocketEncoder; 22 23 class WebSocket final { 24 public: 25 enum ParseResult { 26 // Final frame of a text message or compressed frame. 27 FRAME_OK_FINAL, 28 // Other frame of a text message. 29 FRAME_OK_MIDDLE, 30 FRAME_PING, 31 FRAME_PONG, 32 FRAME_INCOMPLETE, 33 FRAME_CLOSE, 34 FRAME_ERROR 35 }; 36 37 WebSocket(HttpServer* server, HttpConnection* connection); 38 39 void Accept(const HttpServerRequestInfo& request, 40 const NetworkTrafficAnnotationTag traffic_annotation); 41 ParseResult Read(std::string* message); 42 void Send(std::string_view message, 43 WebSocketFrameHeader::OpCodeEnum op_code, 44 const NetworkTrafficAnnotationTag traffic_annotation); 45 46 WebSocket(const WebSocket&) = delete; 47 WebSocket& operator=(const WebSocket&) = delete; 48 49 ~WebSocket(); 50 51 private: 52 void Fail(); 53 void SendErrorResponse(const std::string& message, 54 const NetworkTrafficAnnotationTag traffic_annotation); 55 56 // This dangling raw_ptr occurred in: 57 // browser_tests: PortForwardingDisconnectTest.DisconnectOnRelease 58 // https://ci.chromium.org/ui/p/chromium/builders/try/win-rel/170974/test-results?q=ExactID%3Aninja%3A%2F%2Fchrome%2Ftest%3Abrowser_tests%2FPortForwardingDisconnectTest.DisconnectOnRelease+VHash%3Abdbee181b3e0309b 59 const raw_ptr<HttpServer, 60 AcrossTasksDanglingUntriaged | FlakyDanglingUntriaged> 61 server_; 62 const raw_ptr<HttpConnection> connection_; 63 std::unique_ptr<WebSocketEncoder> encoder_; 64 bool closed_ = false; 65 std::unique_ptr<NetworkTrafficAnnotationTag> traffic_annotation_ = nullptr; 66 }; 67 68 } // namespace net 69 70 #endif // NET_SERVER_WEB_SOCKET_H_ 71