File: tls_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 (92 lines) | stat: -rw-r--r-- 3,487 bytes parent folder | download | duplicates (8)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_API_SOCKET_TLS_SOCKET_H_
#define EXTENSIONS_BROWSER_API_SOCKET_TLS_SOCKET_H_

#include <stdint.h>

#include <memory>
#include <string>

#include "extensions/browser/api/socket/mojo_data_pump.h"
#include "extensions/browser/api/socket/socket.h"
#include "extensions/browser/api/socket/socket_api.h"
#include "extensions/browser/api/socket/tcp_socket.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "services/network/public/mojom/tls_socket.mojom.h"

namespace extensions {

class MojoDataPump;

// TLS Sockets from the chrome.socket and chrome.sockets.tcp APIs. A regular
// TCPSocket is converted to a TLSSocket via chrome.socket.secure() or
// chrome.sockets.tcp.secure(). The inheritance here is for interface API
// compatibility, not for the implementation that comes with it. TLSSocket
// does not use its superclass's socket state, so all methods are overridden
// here to prevent any access of ResumableTCPSocket's socket state. Except
// for the implementation of a write queue in Socket::Write() (a super-super
// class of ResumableTCPSocket). That implementation only queues and
// serializes invocations to WriteImpl(), implemented here, and does not
// touch any socket state.
class TLSSocket : public ResumableTCPSocket {
 public:
  TLSSocket(mojo::PendingRemote<network::mojom::TLSClientSocket> tls_socket,
            const net::IPEndPoint& local_addr,
            const net::IPEndPoint& peer_addr,
            mojo::ScopedDataPipeConsumerHandle receive_stream,
            mojo::ScopedDataPipeProducerHandle send_stream,
            const std::string& owner_extension_id);

  TLSSocket(const TLSSocket&) = delete;
  TLSSocket& operator=(const TLSSocket&) = delete;

  ~TLSSocket() override;

  // Fails.
  void Connect(const net::AddressList& address,
               net::CompletionOnceCallback callback) override;
  // Forwards.
  void Disconnect(bool socket_destroying) override;

  // Attempts to read |count| bytes of decrypted data from the TLS socket,
  // invoking |callback| with the actual number of bytes read, or a network
  // error code if an error occurred.
  void Read(int count, ReadCompletionCallback callback) override;

  // Fails. TLSSocket is only a client.
  void Listen(const std::string& address,
              uint16_t port,
              int backlog,
              ListenCallback callback) override;

  // Forwards.
  bool IsConnected() override;

  bool GetPeerAddress(net::IPEndPoint* address) override;
  bool GetLocalAddress(net::IPEndPoint* address) override;

  // Returns TYPE_TLS.
  SocketType GetSocketType() const override;

 private:
  int WriteImpl(net::IOBuffer* io_buffer,
                int io_buffer_size,
                net::CompletionOnceCallback callback) override;
  void OnWriteComplete(net::CompletionOnceCallback callback, int result);
  void OnReadComplete(int result, scoped_refptr<net::IOBuffer> io_buffer);

  mojo::Remote<network::mojom::TLSClientSocket> tls_socket_;
  std::optional<net::IPEndPoint> local_addr_;
  std::optional<net::IPEndPoint> peer_addr_;
  std::unique_ptr<MojoDataPump> mojo_data_pump_;
  ReadCompletionCallback read_callback_;
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_SOCKET_TLS_SOCKET_H_