summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/peerconnection/adapters/quic_stream_proxy.h
blob: 5881aef8e0b3aae39ccd384d519eaf4f0251432e (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// 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_PEERCONNECTION_ADAPTERS_QUIC_STREAM_PROXY_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_QUIC_STREAM_PROXY_H_

#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_checker.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

class QuicStreamHost;
class QuicTransportProxy;

// This class allows interactions with a QUIC stream that runs on a thread
// different from which it is controlled. All interactions with the QUIC
// implementation happen asynchronously.
//
// The QuicStreamProxy is owned by the QuicTransportProxy and constructed when
// either a new local QUIC stream is created or when a remote QUIC stream has
// been created. The stream proxy will be deleted in the following
// circumstances:
// 1) Reset() is called.
// 2) OnRemoteReset() is indicated.
// 3) Finish() and OnRemoteFinish() have been called.
// The client is responsible for knowing when any of these conditions have been
// met and clearing its reference accordingly.
//
// Since the QuicStreamProxy can be constructed from either the proxy or host
// thread, initialization happens in four steps:
// 1) QuicStreamProxy is constructed.
// 2) set_host is called with a WeakPtr to the corresponding host-thread object.
// 3) Initialize is called on the proxy thread.
// 4) set_delegate is called on the proxy thread.
class QuicStreamProxy final : public base::SupportsWeakPtr<QuicStreamProxy> {
 public:
  class Delegate {
   public:
    virtual ~Delegate() = default;

    // Called when the remote side resets the stream.
    virtual void OnRemoteReset() {}
    // Called when the remote side receives data and/or the finish bit.
    virtual void OnDataReceived(Vector<uint8_t> data, bool fin) {}
    // Called when data written with WriteData() has been consumed by QUIC.
    virtual void OnWriteDataConsumed(uint32_t amount) {}
  };

  QuicStreamProxy();
  ~QuicStreamProxy();

  // Sets a WeakPtr to the corresponding QuicStreamHost. This is valid on either
  // the proxy or host thread. Should happen right after construction.
  void set_host(base::WeakPtr<QuicStreamHost> stream_host);

  // Initializes the QuicStreamProxy. Must be called on the proxy thread.
  // |transport_proxy| must outlive this object.
  void Initialize(QuicTransportProxy* transport_proxy);

  // Sets the delegate for receiving remote callbacks.
  void set_delegate(Delegate* delegate);

  // The remaining methods can only be called from the proxy thread and must
  // be preceded by Initialize().

  scoped_refptr<base::SingleThreadTaskRunner> host_thread() const;

  void Reset();
  void MarkReceivedDataConsumed(uint32_t amount);
  void WriteData(Vector<uint8_t> data, bool fin);

 private:
  // Instruct the QuicTransportProxy to remove and delete this stream proxy.
  void Delete();

  // Callbacks from QuicStreamHost.
  friend class QuicStreamHost;
  void OnRemoteReset();
  void OnDataReceived(Vector<uint8_t> data, bool fin);
  void OnWriteDataConsumed(uint32_t amount);

  // Up reference. Owned by the QuicTransportProxy client.
  QuicTransportProxy* transport_proxy_ = nullptr;
  // Forward reference. Owned by the QuicTransportHost.
  base::WeakPtr<QuicStreamHost> stream_host_;
  // Back reference. Owned by the RTCQuicTransport.
  Delegate* delegate_ = nullptr;

  // |readable_| transitions to false when OnDataReceived(_, true) is called.
  bool readable_ = true;
  // |writable_| transitions to false when WriteData(_, true) is called.
  bool writable_ = true;

  THREAD_CHECKER(thread_checker_);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_QUIC_STREAM_PROXY_H_