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
|
// 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 THIRD_PARTY_BLINK_RENDERER_CORE_FETCH_FETCH_MANAGER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FETCH_FETCH_MANAGER_H_
#include <memory>
#include <optional>
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/tick_clock.h"
#include "third_party/blink/public/mojom/permissions/permission.mojom-blink.h"
#include "third_party/blink/public/mojom/permissions/permission_status.mojom-blink.h"
#include "third_party/blink/public/platform/child_url_loader_factory_bundle.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/dom_high_res_time_stamp.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_state_observer.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_set.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_parameters.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_load_priority.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_loader_options.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_request.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
namespace network {
struct ResourceRequest;
} // namespace network
namespace blink {
class AbortSignal;
class ExceptionState;
class ExecutionContext;
class FetchRequestData;
class Response;
class ScriptState;
class FetchLaterResult;
class CORE_EXPORT FetchManager final
: public GarbageCollected<FetchManager>,
public ExecutionContextLifecycleObserver {
public:
explicit FetchManager(ExecutionContext*);
ScriptPromise<Response> Fetch(ScriptState*,
FetchRequestData*,
AbortSignal*,
ExceptionState&);
// ExecutionContextLifecycleObserver overrides:
void ContextDestroyed() override;
void Trace(Visitor*) const override;
private:
class Loader;
// Removes loader from `loaders_`.
void OnLoaderFinished(Loader*);
HeapHashSet<Member<Loader>> loaders_;
};
class CORE_EXPORT FetchLaterManager final
: public GarbageCollected<FetchLaterManager>,
public ExecutionContextLifecycleObserver,
public mojom::blink::PermissionObserver {
public:
explicit FetchLaterManager(ExecutionContext*);
FetchLaterResult* FetchLater(ScriptState*,
FetchRequestData*,
AbortSignal*,
std::optional<DOMHighResTimeStamp>,
ExceptionState&);
// Updates two types of quotas using the Step 9 of the following algorithm:
// https://whatpr.org/fetch/1647.html#available-deferred-fetch-quota
// 9. For each deferred fetch record deferredRecord of controlDocument's
// fetch group’s deferred fetch records ...
//
// Specifically, the quotas are updated according to all the queued FetchLater
// requests from `deferred_loaders_`.
// Unlike spec, neither `quota_for_url_origin` nor `total_quota` can be
// negative. They will be set to zero if they would have become negative.
void UpdateDeferredBytesQuota(const KURL& url,
uint64_t& quota_for_url_origin,
uint64_t& total_quota) const;
// ExecutionContextLifecycleObserver overrides:
void ContextDestroyed() override;
void ContextEnteredBackForwardCache() override;
void Trace(Visitor*) const override;
// For testing only:
size_t NumLoadersForTesting() const;
void RecreateTimerForTesting(scoped_refptr<base::SingleThreadTaskRunner>,
const base::TickClock*);
private:
class DeferredLoader;
// TODO(crbug.com/1293679): Update to proper TaskType once spec is finalized.
// Using the `TaskType::kNetworkingUnfreezable` as deferred requests need to
// work when ExecutionContext is in BackForwardCache/frozen.
static constexpr TaskType kTaskType = TaskType::kNetworkingUnfreezable;
// Returns a pointer to the wrapper that provides FetchLaterLoaderFactory.
// Returns nullptr if the context is detached.
blink::ChildURLLoaderFactoryBundle* GetFactory();
// Creates a network version of ResourceRequest using `request` and `options`.
// Returns nullptr if any of the checks fail during the call.
// Note that this method must only be used to generate FetchLater requests.
std::unique_ptr<network::ResourceRequest> PrepareNetworkRequest(
ResourceRequest request,
const ResourceLoaderOptions& options) const;
// mojom::blink::PermissionObserver overrides:
void OnPermissionStatusChange(mojom::blink::PermissionStatus) override;
// Returns true if BackgroundSync permission has been enabled for the
// ExecutionContext of this.
bool IsBackgroundSyncGranted() const;
// Removes a loader from `deferred_loaders_`.
void OnDeferredLoaderFinished(DeferredLoader*);
// Every deferred loader represents a FetchLater request.
HeapHashSet<Member<DeferredLoader>> deferred_loaders_;
// Whether the ExecutionContext of `this` has permission to run deferred
// requests after the context enters BackForwardCache.
// Defaults to denied. It should be updated by
// `permission_observer_receiver_` shortly after ctor.
mojom::blink::PermissionStatus background_sync_permission_ =
mojom::blink::PermissionStatus::DENIED;
HeapMojoReceiver<mojom::blink::PermissionObserver, FetchLaterManager>
permission_observer_receiver_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_FETCH_FETCH_MANAGER_H_
|