File: response_body_loader.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 (162 lines) | stat: -rw-r--r-- 6,993 bytes parent folder | download | duplicates (11)
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
161
162
// Copyright 2019 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_PLATFORM_LOADER_FETCH_RESPONSE_BODY_LOADER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_RESPONSE_BODY_LOADER_H_

#include "base/containers/span.h"
#include "base/memory/scoped_refptr.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "third_party/blink/public/mojom/navigation/renderer_eviction_reason.mojom-blink-forward.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/bytes_consumer.h"
#include "third_party/blink/renderer/platform/loader/fetch/loader_freeze_mode.h"
#include "third_party/blink/renderer/platform/loader/fetch/response_body_loader_client.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace base {
class SingleThreadTaskRunner;
}  // namespace base

namespace blink {

class BackForwardCacheLoaderHelper;
class ResponseBodyLoader;

// See ResponseBodyLoader for details. This is a virtual interface to expose
// only Drain functions.
class PLATFORM_EXPORT ResponseBodyLoaderDrainableInterface
    : public GarbageCollected<ResponseBodyLoaderDrainableInterface> {
 public:
  virtual ~ResponseBodyLoaderDrainableInterface() = default;

  // Drains the response body and returns it. This function must not be called
  // when the load has already been started or aborted, or the body has already
  // been drained. This function may return an invalid handle when it is
  // unable to convert the body to a data pipe, even when the body itself is
  // valid. In that case, this function is no-op. If this function returns a
  // valid handle, the caller is responsible for reading the body and providing
  // the information to the client this function provides.
  // Note that the notification from the client is *synchronously* propergated
  // to the original client ResponseBodyLoader owns,  e.g., when the caller
  // calls ResponseBodyLoaderClient::DidCancelLoadingBody, it synchronously
  // cancels the resource loading (if |this| is associated with
  // blink::ResourceLoader). A user of this function should ensure that calling
  // the client's method doesn't lead to a reentrant problem.
  // Note that the drained datapipe is not subject to the freezing effect.
  virtual mojo::ScopedDataPipeConsumerHandle DrainAsDataPipe(
      ResponseBodyLoaderClient** client) = 0;

  // Drains the response body and returns it. This function must not be called
  // when the load has already been started or aborted, or the body has already
  // been drained. Unlike DrainAsDataPipe, this function always succeeds.
  // This ResponseBodyLoader will still monitor the loading signals, and report
  // them back to the associated client asynchronously.
  // Note that the drained BytesConsumer is subject to the freezing effect, and
  // the loading is cancelled when freezing is for back-forward cache.
  virtual BytesConsumer& DrainAsBytesConsumer() = 0;

  virtual void Trace(Visitor*) const {}
};

// ResponseBodyLoader reads the response body and reports the contents to the
// associated client. There are two ways:
//  - By calling Start(), ResponseBodyLoader reads the response body. and
//    reports the contents to the client. Abort() aborts reading.
//  - By calling DrainAsDataPipe, a user can "drain" the contents from
//    ResponseBodyLoader. The caller is responsible for reading the body and
//    providing the information to the client this function provides.
//  - By calling DrainBytesConsumer, a user can "drain" the contents from
//    ResponseBodyLoader.
// A ResponseBodyLoader is bound to the thread on which it is created.
class PLATFORM_EXPORT ResponseBodyLoader final
    : public ResponseBodyLoaderDrainableInterface,
      private ResponseBodyLoaderClient,
      private BytesConsumer::Client {
 public:
  ResponseBodyLoader(
      BytesConsumer&,
      ResponseBodyLoaderClient&,
      scoped_refptr<base::SingleThreadTaskRunner>,
      BackForwardCacheLoaderHelper* back_forward_cache_loader_helper);

  // ResponseBodyLoaderDrainableInterface implementation.
  mojo::ScopedDataPipeConsumerHandle DrainAsDataPipe(
      ResponseBodyLoaderClient**) override;
  BytesConsumer& DrainAsBytesConsumer() override;

  // Starts loading.
  void Start();

  // Aborts loading. This is expected to be called from the client's side, and
  // does not report the failure to the client. This doesn't affect a
  // drained data pipe. This function cannot be called when suspended.
  void Abort();

  // Suspends loading.
  void Suspend(LoaderFreezeMode);

  // Resumes loading.
  void Resume();

  bool IsAborted() const { return aborted_; }
  bool IsSuspended() const {
    return suspended_state_ != LoaderFreezeMode::kNone;
  }
  bool IsSuspendedForBackForwardCache() const {
    return suspended_state_ == LoaderFreezeMode::kBufferIncoming;
  }
  bool IsDrained() const {
    return drained_as_datapipe_ || drained_as_bytes_consumer_;
  }

  // Evicts the back-forward cache entry if the response body has already been
  // passed and drained as bytes consumer.
  void EvictFromBackForwardCacheIfDrainedAsBytesConsumer();

  void Trace(Visitor*) const override;

 private:
  class Buffer;
  class DelegatingBytesConsumer;

  // ResponseBodyLoaderClient implementation.
  void DidReceiveData(base::span<const char> data) override;
  void DidReceiveDecodedData(
      const String& data,
      std::unique_ptr<ParkableStringImpl::SecureDigest> digest) override;
  void DidFinishLoadingBody() override;
  void DidFailLoadingBody() override;
  void DidCancelLoadingBody() override;

  void EvictFromBackForwardCache(mojom::blink::RendererEvictionReason);
  void DidBufferLoadWhileInBackForwardCache(size_t num_bytes);

  // BytesConsumer::Client implementation.
  void OnStateChange() override;
  String DebugName() const override { return "ResponseBodyLoader"; }

  const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  Member<Buffer> body_buffer_;
  Member<BytesConsumer> bytes_consumer_;
  Member<DelegatingBytesConsumer> delegating_bytes_consumer_;
  const Member<ResponseBodyLoaderClient> client_;
  WeakMember<BackForwardCacheLoaderHelper> back_forward_cache_loader_helper_;
  LoaderFreezeMode suspended_state_ = LoaderFreezeMode::kNone;
  bool started_ = false;
  bool aborted_ = false;
  bool drained_as_datapipe_ = false;
  bool drained_as_bytes_consumer_ = false;
  bool finish_signal_is_pending_ = false;
  bool fail_signal_is_pending_ = false;
  bool cancel_signal_is_pending_ = false;
  bool in_two_phase_read_ = false;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_RESPONSE_BODY_LOADER_H_