• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/server/web_socket_parse_result.h"
14 #include "net/traffic_annotation/network_traffic_annotation.h"
15 #include "net/websockets/websocket_frame.h"
16 
17 namespace net {
18 
19 class HttpConnection;
20 class HttpServer;
21 class HttpServerRequestInfo;
22 class WebSocketEncoder;
23 
24 class WebSocket final {
25  public:
26   WebSocket(HttpServer* server, HttpConnection* connection);
27 
28   void Accept(const HttpServerRequestInfo& request,
29               const NetworkTrafficAnnotationTag traffic_annotation);
30   WebSocketParseResult Read(std::string* message);
31   void Send(std::string_view message,
32             WebSocketFrameHeader::OpCodeEnum op_code,
33             const NetworkTrafficAnnotationTag traffic_annotation);
34 
35   WebSocket(const WebSocket&) = delete;
36   WebSocket& operator=(const WebSocket&) = delete;
37 
38   ~WebSocket();
39 
40  private:
41   void Fail();
42   void SendErrorResponse(const std::string& message,
43                          const NetworkTrafficAnnotationTag traffic_annotation);
44 
45   const raw_ptr<HttpServer> server_;
46   const raw_ptr<HttpConnection> connection_;
47   std::unique_ptr<WebSocketEncoder> encoder_;
48   bool closed_ = false;
49   std::unique_ptr<NetworkTrafficAnnotationTag> traffic_annotation_ = nullptr;
50 };
51 
52 }  // namespace net
53 
54 #endif  // NET_SERVER_WEB_SOCKET_H_
55