summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/loader/fetch/buffering_bytes_consumer.h
blob: 49155275231b709b632e93da8a4092ae617bf5cf (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
104
105
106
107
108
109
110
// 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_PLATFORM_LOADER_FETCH_BUFFERING_BYTES_CONSUMER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_BUFFERING_BYTES_CONSUMER_H_

#include <memory>

#include "base/memory/scoped_refptr.h"
#include "base/util/type_safety/pass_key.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/loader/fetch/bytes_consumer.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/timer.h"
#include "third_party/blink/renderer/platform/wtf/deque.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

// BufferingBytesConsumer is a BytesConsumer. It takes a BytesConsumer
// ("the original BytesConsumer") as a constructor parameter, and results read
// from the BufferingBytesConsumer are as same as results which would be read
// from the original BytesConsumer.
// BufferingBytesConsumer buffers reads chunks from the original BytesConsumer
// and store it until they are read, before read requests are issued from the
// client.
class PLATFORM_EXPORT BufferingBytesConsumer final
    : public BytesConsumer,
      private BytesConsumer::Client {
  USING_GARBAGE_COLLECTED_MIXIN(BufferingBytesConsumer);

 public:
  // Creates a BufferingBytesConsumer that waits some delay before beginning
  // to buffer data from the underlying consumer. This delay provides an
  // opportunity for the data to be drained before buffering begins. The
  // |bytes_consumer| is the original BytesConsumer. |bytes_consumer| must
  // not have a client.
  static BufferingBytesConsumer* CreateWithDelay(
      BytesConsumer* bytes_consumer,
      scoped_refptr<base::SingleThreadTaskRunner> timer_task_runner);

  // Creates a BufferingBytesConsumer that buffers immediately without any
  // delay. |bytes_consumer| is the original BytesConsumer. |bytes_consumer|
  // must not have a client.
  static BufferingBytesConsumer* Create(BytesConsumer* bytes_consumer);

  // Use the Create*() factory methods instead of direct instantiation.
  BufferingBytesConsumer(
      util::PassKey<BufferingBytesConsumer> key,
      BytesConsumer* bytes_consumer,
      scoped_refptr<base::SingleThreadTaskRunner> timer_task_runner,
      base::TimeDelta buffering_start_delay);
  ~BufferingBytesConsumer() override;

  // Attempt to start buffering data from the underlying consumer.  This will
  // only have an effect if we're currently in the kDelayed state.  If
  // buffering has already started or been explicitly stopped then this method
  // has no effect.
  void MaybeStartBuffering();

  // After this function is called, |this| will not do buffering. Already
  // buffered data still waits to be consumed, but after all the buffered data
  // is consumed, BeginRead and EndRead will result in BeginRead and EndRead
  // calls to the original BytesConsumer.
  void StopBuffering();

  // BufferingBytesConsumer
  Result BeginRead(const char** buffer, size_t* available) override;
  Result EndRead(size_t read_size) override;
  scoped_refptr<BlobDataHandle> DrainAsBlobDataHandle(BlobSizePolicy) override;
  scoped_refptr<EncodedFormData> DrainAsFormData() override;
  mojo::ScopedDataPipeConsumerHandle DrainAsDataPipe() override;
  void SetClient(BytesConsumer::Client*) override;
  void ClearClient() override;
  void Cancel() override;
  PublicState GetPublicState() const override;
  Error GetError() const override;
  String DebugName() const override { return "BufferingBytesConsumer"; }

  void Trace(Visitor*) override;

 private:
  void OnTimerFired(TimerBase*);

  // BufferingBytesConsumer::Client
  void OnStateChange() override;
  void BufferData();

  const Member<BytesConsumer> bytes_consumer_;
  TaskRunnerTimer<BufferingBytesConsumer> timer_;
  Deque<Vector<char>> buffer_;
  size_t offset_for_first_chunk_ = 0;

  enum class BufferingState {
    kDelayed,
    kStarted,
    kStopped,
  };
  BufferingState buffering_state_ = BufferingState::kDelayed;

  bool has_seen_end_of_data_ = false;
  bool has_seen_error_ = false;
  Member<BytesConsumer::Client> client_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_BUFFERING_BYTES_CONSUMER_H_