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
|
// Copyright 2018 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_RESOURCE_FETCHER_PROPERTIES_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_RESOURCE_FETCHER_PROPERTIES_H_
#include "third_party/blink/public/mojom/service_worker/controller_service_worker_mode.mojom-blink.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/loader_freeze_mode.h"
#include "third_party/blink/renderer/platform/loader/fetch/url_loader/url_loader.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/scheduler/public/frame_status.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
namespace blink {
class FetchClientSettingsObject;
// ResourceFetcherProperties consists of properties of the global context (e.g.,
// Frame, Worker) necessary to fetch resources. FetchClientSettingsObject
// implementing https://html.spec.whatwg.org/C/webappapis.html#settings-object
// is one such example.
//
// This class consists of pure virtual getters. Do not put operations. Do not
// put getters for a specific request such as
// GetCachePolicy(const ResourceRequest&, ResourceType). Do not put a function
// with default implementation.
//
// Storing a non-null ResourceFetcherProperties in an object that can be valid
// after the associated ResourceFetcher is detached is dangerous. Use
// DetachedResourceFetcherProperties below.
//
// The distinction between FetchClientSettingsObject and
// ResourceFetcherProperties is sometimes ambiguous. Put a property in
// FetchClientSettingsObject when the property is clearly defined in the spec.
// Otherwise, put it to this class.
class PLATFORM_EXPORT ResourceFetcherProperties
: public GarbageCollected<ResourceFetcherProperties> {
public:
using ControllerServiceWorkerMode = mojom::ControllerServiceWorkerMode;
ResourceFetcherProperties() = default;
virtual ~ResourceFetcherProperties() = default;
virtual void Trace(Visitor*) const {}
// Returns the client settings object bound to this global context.
virtual const FetchClientSettingsObject& GetFetchClientSettingsObject()
const = 0;
// Returns whether this global context is the outermost main frame.
virtual bool IsOutermostMainFrame() const = 0;
// Returns whether a controller service worker exists and if it has a fetch
// handler.
virtual ControllerServiceWorkerMode GetControllerServiceWorkerMode()
const = 0;
// Returns an identifier for the service worker controlling this global
// context. This function cannot be called when
// GetControllerServiceWorkerMode returns kNoController.
virtual int64_t ServiceWorkerId() const = 0;
// Returns whether this global context is suspended, which means we should
// defer making a new request.
// https://html.spec.whatwg.org/C/webappapis.html#pause
virtual bool IsPaused() const = 0;
// Returns the freezing mode set to this context.
virtual LoaderFreezeMode FreezeMode() const = 0;
// Returns whether this global context is detached. Note that in some cases
// the loading pipeline continues working after detached (e.g., for fetch()
// operations with "keepalive" specified).
virtual bool IsDetached() const = 0;
// Returns whether the main resource for this global context is loaded.
virtual bool IsLoadComplete() const = 0;
// Returns whether we should disallow a sub resource loading.
virtual bool ShouldBlockLoadingSubResource() const = 0;
// Returns whether we should de-prioritize requests in sub frames.
// TODO(yhirano): Make this ShouldDepriotizeRequest once the related
// histograms get deprecated. See https://crbug.com/800035.
virtual bool IsSubframeDeprioritizationEnabled() const = 0;
// Returns the scheduling status of the associated frame. Returns |kNone|
// if there is no such a frame.
virtual scheduler::FrameStatus GetFrameStatus() const = 0;
virtual int GetOutstandingThrottledLimit() const = 0;
};
// A delegating ResourceFetcherProperties subclass which can be retained
// even when the associated ResourceFetcher is detached.
class PLATFORM_EXPORT DetachableResourceFetcherProperties final
: public ResourceFetcherProperties {
public:
explicit DetachableResourceFetcherProperties(
const ResourceFetcherProperties& properties)
: properties_(properties) {}
~DetachableResourceFetcherProperties() override = default;
void Detach();
void Trace(Visitor* visitor) const override;
// ResourceFetcherProperties implementation
// Add a test in resource_fetcher_test.cc when you change behaviors.
const FetchClientSettingsObject& GetFetchClientSettingsObject()
const override {
return properties_ ? properties_->GetFetchClientSettingsObject()
: *fetch_client_settings_object_;
}
bool IsOutermostMainFrame() const override {
return properties_ ? properties_->IsOutermostMainFrame()
: is_outermost_main_frame_;
}
ControllerServiceWorkerMode GetControllerServiceWorkerMode() const override {
return properties_ ? properties_->GetControllerServiceWorkerMode()
: ControllerServiceWorkerMode::kNoController;
}
int64_t ServiceWorkerId() const override {
// When detached, GetControllerServiceWorkerMode returns kNoController, so
// this function must not be called.
DCHECK(properties_);
return properties_->ServiceWorkerId();
}
bool IsPaused() const override {
return properties_ ? properties_->IsPaused() : paused_;
}
LoaderFreezeMode FreezeMode() const override {
return properties_ ? properties_->FreezeMode() : freeze_mode_;
}
bool IsDetached() const override {
return properties_ ? properties_->IsDetached() : true;
}
bool IsLoadComplete() const override {
return properties_ ? properties_->IsLoadComplete() : load_complete_;
}
bool ShouldBlockLoadingSubResource() const override {
// Returns true when detached in order to preserve the existing behavior.
return properties_ ? properties_->ShouldBlockLoadingSubResource() : true;
}
bool IsSubframeDeprioritizationEnabled() const override {
return properties_ ? properties_->IsSubframeDeprioritizationEnabled()
: is_subframe_deprioritization_enabled_;
}
scheduler::FrameStatus GetFrameStatus() const override {
return properties_ ? properties_->GetFrameStatus()
: scheduler::FrameStatus::kNone;
}
int GetOutstandingThrottledLimit() const override {
return properties_ ? properties_->GetOutstandingThrottledLimit()
: outstanding_throttled_limit_;
}
private:
// |properties_| is null if and only if detached.
Member<const ResourceFetcherProperties> properties_;
// The following members are used when detached.
Member<const FetchClientSettingsObject> fetch_client_settings_object_;
bool is_outermost_main_frame_ = false;
bool paused_ = false;
LoaderFreezeMode freeze_mode_;
bool load_complete_ = false;
bool is_subframe_deprioritization_enabled_ = false;
int outstanding_throttled_limit_ = 0;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_RESOURCE_FETCHER_PROPERTIES_H_
|