File: global_fetch.cc

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 (263 lines) | stat: -rw-r--r-- 11,074 bytes parent folder | download | duplicates (4)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// 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.

#include "third_party/blink/renderer/core/fetch/global_fetch.h"

#include "base/feature_list.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_deferred_request_init.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_request_init.h"
#include "third_party/blink/renderer/core/execution_context/navigator_base.h"
#include "third_party/blink/renderer/core/fetch/fetch_later_result.h"
#include "third_party/blink/renderer/core/fetch/fetch_manager.h"
#include "third_party/blink/renderer/core/fetch/request.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/supplementable.h"

namespace blink {

namespace {

void MeasureFetchProperties(ExecutionContext* execution_context,
                            FetchRequestData* data) {
  // 'redirect' measurement
  if (data->Redirect() == network::mojom::RedirectMode::kError)
    UseCounter::Count(execution_context, WebFeature::kFetchRedirectError);
  else if (data->Redirect() == network::mojom::RedirectMode::kManual)
    UseCounter::Count(execution_context, WebFeature::kFetchRedirectManual);

  // 'cache' measurement: https://crbug.com/959789
  if (data->CacheMode() == mojom::FetchCacheMode::kBypassCache)
    UseCounter::Count(execution_context, WebFeature::kFetchCacheReload);
}

template <typename T>
class GlobalFetchImpl final : public GarbageCollected<GlobalFetchImpl<T>>,
                              public GlobalFetch::ScopedFetcher,
                              public Supplement<T> {
 public:
  static const char kSupplementName[];

  static ScopedFetcher* From(T& supplementable,
                             ExecutionContext* execution_context) {
    GlobalFetchImpl* supplement =
        Supplement<T>::template From<GlobalFetchImpl>(supplementable);
    if (!supplement) {
      supplement = MakeGarbageCollected<GlobalFetchImpl>(supplementable,
                                                         execution_context);
      Supplement<T>::ProvideTo(supplementable, supplement);
    }
    return supplement;
  }

  explicit GlobalFetchImpl(T& supplementable,
                           ExecutionContext* execution_context)
      : Supplement<T>(supplementable),
        fetch_manager_(MakeGarbageCollected<FetchManager>(execution_context)),
        // TODO(crbug.com/1356128): FetchLater is only supported in Document.
        fetch_later_manager_(
            base::FeatureList::IsEnabled(blink::features::kFetchLaterAPI) &&
                    execution_context->IsWindow()
                ? MakeGarbageCollected<FetchLaterManager>(execution_context)
                : nullptr) {}

  ScriptPromise<Response> Fetch(ScriptState* script_state,
                                const V8RequestInfo* input,
                                const RequestInit* init,
                                ExceptionState& exception_state) override {
    fetch_count_ += 1;

    ExecutionContext* execution_context = fetch_manager_->GetExecutionContext();
    if (!script_state->ContextIsValid() || !execution_context) {
      // TODO(yhirano): Should this be moved to bindings?
      exception_state.ThrowTypeError("The global scope is shutting down.");
      return EmptyPromise();
    }

    // "Let |r| be the associated request of the result of invoking the
    // initial value of Request as constructor with |input| and |init| as
    // arguments. If this throws an exception, reject |p| with it."
    Request* r = Request::Create(script_state, input, init, exception_state);
    if (exception_state.HadException())
      return EmptyPromise();

    probe::WillSendXMLHttpOrFetchNetworkRequest(execution_context, r->url());
    FetchRequestData* request_data =
        r->PassRequestData(script_state, exception_state);
    MeasureFetchProperties(execution_context, request_data);

    // Even if this was checked at the beginning of the function, it might
    // have been set to nullptr during Request::Create.
    if (!fetch_manager_->GetExecutionContext()) {
      exception_state.ThrowTypeError("The global scope is shutting down.");
      return EmptyPromise();
    }

    auto promise = fetch_manager_->Fetch(script_state, request_data,
                                         r->signal(), exception_state);
    if (exception_state.HadException())
      return EmptyPromise();

    return promise;
  }

  FetchLaterResult* FetchLater(ScriptState* script_state,
                               const V8RequestInfo* input,
                               const DeferredRequestInit* init,
                               ExceptionState& exception_state) override {
    if (!base::FeatureList::IsEnabled(blink::features::kFetchLaterAPI) ||
        !fetch_later_manager_) {
      exception_state.ThrowTypeError(
          "FetchLater is not supported in this scope.");
      return nullptr;
    }
    ExecutionContext* ec = fetch_later_manager_->GetExecutionContext();
    if (!script_state->ContextIsValid() || !ec) {
      exception_state.ThrowTypeError("The global scope is shutting down.");
      return nullptr;
    }

    // https://whatpr.org/fetch/1647.html#dom-global-fetch-later
    // Run the fetchLater(input, init) method steps:

    // 1. Let `r` be the result of invoking the initial value of Request as
    // constructor with `input` and `init` as arguments. This may throw an
    // exception.
    Request* r =
        Request::Create(script_state, input,
                        static_cast<const RequestInit*>(init), exception_state);
    if (exception_state.HadException()) {
      return nullptr;
    }

    probe::WillSendXMLHttpOrFetchNetworkRequest(ec, r->url());
    FetchRequestData* request_data =
        r->PassRequestData(script_state, exception_state);
    MeasureFetchProperties(ec, request_data);
    // 5. If init is given and init ["activateAfter"] exists, then set
    // `activate_after` to init ["activateAfter"].
    std::optional<DOMHighResTimeStamp> activate_after =
        (init->hasActivateAfter() ? std::make_optional(init->activateAfter())
                                  : std::nullopt);
    auto* result = fetch_later_manager_->FetchLater(script_state, request_data,
                                                    r->signal(), activate_after,
                                                    exception_state);
    if (exception_state.HadException()) {
      return nullptr;
    }

    return result;
  }

  uint32_t FetchCount() const override { return fetch_count_; }

  void UpdateDeferredBytesQuota(const KURL& url,
                                uint64_t& quota_for_url_origin,
                                uint64_t& total_quota) const override {
    DCHECK(base::FeatureList::IsEnabled(blink::features::kFetchLaterAPI));
    CHECK(fetch_later_manager_);
    return fetch_later_manager_->UpdateDeferredBytesQuota(
        url, quota_for_url_origin, total_quota);
  }

  void Trace(Visitor* visitor) const override {
    visitor->Trace(fetch_manager_);
    visitor->Trace(fetch_later_manager_);
    ScopedFetcher::Trace(visitor);
    Supplement<T>::Trace(visitor);
  }

 private:
  Member<FetchManager> fetch_manager_;
  Member<FetchLaterManager> fetch_later_manager_;
  uint32_t fetch_count_ = 0;
};

// static
template <typename T>
const char GlobalFetchImpl<T>::kSupplementName[] = "GlobalFetchImpl";

}  // namespace

GlobalFetch::ScopedFetcher::~ScopedFetcher() {}

FetchLaterResult* GlobalFetch::ScopedFetcher::FetchLater(
    ScriptState* script_state,
    const V8RequestInfo* input,
    const DeferredRequestInit* init,
    ExceptionState& exception_state) {
  NOTREACHED();
}

void GlobalFetch::ScopedFetcher::UpdateDeferredBytesQuota(
    const KURL& url,
    uint64_t& quota_for_url_origin,
    uint64_t& total_quota) const {
  NOTREACHED();
}

GlobalFetch::ScopedFetcher* GlobalFetch::ScopedFetcher::From(
    LocalDOMWindow& window) {
  return GlobalFetchImpl<LocalDOMWindow>::From(window,
                                               window.GetExecutionContext());
}

GlobalFetch::ScopedFetcher* GlobalFetch::ScopedFetcher::From(
    WorkerGlobalScope& worker) {
  return GlobalFetchImpl<WorkerGlobalScope>::From(worker,
                                                  worker.GetExecutionContext());
}

GlobalFetch::ScopedFetcher* GlobalFetch::ScopedFetcher::From(
    NavigatorBase& navigator) {
  return GlobalFetchImpl<NavigatorBase>::From(navigator,
                                              navigator.GetExecutionContext());
}

void GlobalFetch::ScopedFetcher::Trace(Visitor* visitor) const {}

ScriptPromise<Response> GlobalFetch::fetch(ScriptState* script_state,
                                           LocalDOMWindow& window,
                                           const V8RequestInfo* input,
                                           const RequestInit* init,
                                           ExceptionState& exception_state) {
  UseCounter::Count(window.GetExecutionContext(), WebFeature::kFetch);
  if (!window.GetFrame()) {
    exception_state.ThrowTypeError("The global scope is shutting down.");
    return EmptyPromise();
  }
  return ScopedFetcher::From(window)->Fetch(script_state, input, init,
                                            exception_state);
}

ScriptPromise<Response> GlobalFetch::fetch(ScriptState* script_state,
                                           WorkerGlobalScope& worker,
                                           const V8RequestInfo* input,
                                           const RequestInit* init,
                                           ExceptionState& exception_state) {
  UseCounter::Count(worker.GetExecutionContext(), WebFeature::kFetch);
  return ScopedFetcher::From(worker)->Fetch(script_state, input, init,
                                            exception_state);
}

FetchLaterResult* GlobalFetch::fetchLater(ScriptState* script_state,
                                          LocalDOMWindow& window,
                                          const V8RequestInfo* input,
                                          const DeferredRequestInit* init,
                                          ExceptionState& exception_state) {
  UseCounter::Count(window.GetExecutionContext(), WebFeature::kFetchLater);
  if (!window.GetFrame()) {
    exception_state.ThrowTypeError("The global scope is shutting down.");
    return nullptr;
  }
  return ScopedFetcher::From(window)->FetchLater(script_state, input, init,
                                                 exception_state);
}

}  // namespace blink