File: tcp_socket.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (160 lines) | stat: -rw-r--r-- 6,368 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_DIRECT_SOCKETS_TCP_SOCKET_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_DIRECT_SOCKETS_TCP_SOCKET_H_

#include <optional>

#include "base/gtest_prod_util.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "services/network/public/mojom/tcp_socket.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_property.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/direct_sockets/socket.h"
#include "third_party/blink/renderer/modules/direct_sockets/tcp_readable_stream_wrapper.h"
#include "third_party/blink/renderer/modules/direct_sockets/tcp_writable_stream_wrapper.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/exception_code.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_v8_reference.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/loader/fetch/unique_identifier.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/scheduler/public/frame_or_worker_scheduler.h"

namespace net {
class IPEndPoint;
}  // namespace net

namespace blink {
class SocketCloseOptions;
class TCPSocketOpenInfo;
class TCPSocketOptions;

// TCPSocket interface from tcp_socket.idl
class MODULES_EXPORT TCPSocket final
    : public ScriptWrappable,
      public Socket,
      public ActiveScriptWrappable<TCPSocket>,
      public network::mojom::blink::SocketObserver {
  DEFINE_WRAPPERTYPEINFO();

 public:
  // IDL definitions
  static TCPSocket* Create(ScriptState*,
                           const String& remote_address,
                           const uint16_t remote_port,
                           const TCPSocketOptions*,
                           ExceptionState&);

  // Socket:
  ScriptPromise<TCPSocketOpenInfo> opened(ScriptState*) const;
  ScriptPromise<IDLUndefined> close(ScriptState*, ExceptionState&) override;

 public:
  // Constructor for use from TCPServerSocket.
  static TCPSocket* CreateFromAcceptedConnection(
      ScriptState*,
      mojo::PendingRemote<network::mojom::blink::TCPConnectedSocket>,
      mojo::PendingReceiver<network::mojom::blink::SocketObserver>,
      const net::IPEndPoint& remote_addr,
      mojo::ScopedDataPipeConsumerHandle receive_stream,
      mojo::ScopedDataPipeProducerHandle send_stream);

  explicit TCPSocket(ScriptState*);
  ~TCPSocket() override;

  // Validates options and calls
  // DirectSocketsServiceMojoRemote::OpenTCPSocket(...).
  bool Open(const String& remote_address,
            const uint16_t remote_port,
            const TCPSocketOptions*,
            ExceptionState&);

  // On net::OK initializes readable/writable streams and resolves opened
  // promise. Otherwise rejects the opened promise.
  void OnTCPSocketOpened(
      mojo::PendingRemote<network::mojom::blink::TCPConnectedSocket>,
      mojo::PendingReceiver<network::mojom::blink::SocketObserver>,
      int32_t result,
      const std::optional<net::IPEndPoint>& local_addr,
      const std::optional<net::IPEndPoint>& peer_addr,
      mojo::ScopedDataPipeConsumerHandle receive_stream,
      mojo::ScopedDataPipeProducerHandle send_stream);

  void Trace(Visitor*) const override;

  // ActiveScriptWrappable:
  bool HasPendingActivity() const override;

  // ExecutionContextLifecycleStateObserver:
  void ContextDestroyed() override;

  // Socket:
  void SetState(State state) override;

 private:
  void FinishOpenOrAccept(
      mojo::PendingRemote<network::mojom::blink::TCPConnectedSocket>,
      mojo::PendingReceiver<network::mojom::blink::SocketObserver>,
      const net::IPEndPoint& peer_addr,
      const std::optional<net::IPEndPoint>& local_addr,
      mojo::ScopedDataPipeConsumerHandle receive_stream,
      mojo::ScopedDataPipeProducerHandle send_stream);

  // Invoked if mojo pipe for |service_| breaks.
  void OnServiceConnectionError() override;

  // Invoked if mojo pipe for |socket_observer_| breaks.
  void OnSocketConnectionError();

  // Resets mojo resources held by this class.
  void ReleaseResources();

  // network::mojom::blink::SocketObserver:
  void OnReadError(int32_t net_error) override;
  void OnWriteError(int32_t net_error) override;

  // Invoked when one of the streams (readable or writable) closes.
  // `exception` is non-empty iff the stream closed with an error.
  void OnStreamClosed(v8::Local<v8::Value> exception, int net_error);
  void OnBothStreamsClosed();

  HeapMojoRemote<network::mojom::blink::TCPConnectedSocket> tcp_socket_;
  HeapMojoReceiver<network::mojom::blink::SocketObserver, TCPSocket>
      socket_observer_;

  Member<ScriptPromiseProperty<TCPSocketOpenInfo, DOMException>> opened_;

  Member<TCPReadableStreamWrapper> readable_stream_wrapper_;
  Member<TCPWritableStreamWrapper> writable_stream_wrapper_;

  // Always less or equal to 2 (readable + writable).
  int streams_closed_count_ = 0;

  // Stores the first encountered stream error to be reported after both streams
  // close.
  TraceWrapperV8Reference<v8::Value> stream_error_;
  // Stores the net error when the socket is aborted
  int abort_net_error_;

  // Unique id for devtools inspector_network_agent.
  uint64_t inspector_id_ = CreateUniqueIdentifier();

  FRIEND_TEST_ALL_PREFIXES(TCPSocketTest, OnSocketObserverConnectionError);
  FRIEND_TEST_ALL_PREFIXES(TCPSocketCloseTest, OnErrorOrClose);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_DIRECT_SOCKETS_TCP_SOCKET_H_