Commit 1e86edb8 authored by Dominic Farolino's avatar Dominic Farolino Committed by Commit Bot

Prototype Priority Hints Infrastructure

Initial infrastructure changes to support Priority Hints. This includes
adding a FetchImportanceMode enum to fetch_api_request.mojom, some
supporting getters/setters to FetchParameters as well as ResourceRequest,
and priority-modifying logic to ResourceFetcher.

R=kinuko@chromium.org, yhirano@chromium.org, yoav@yoav.ws

Bug: 821464
Change-Id: I5ab7f70f44e80d01b7d47ad4c00ae3f4817815ee
Reviewed-on: https://chromium-review.googlesource.com/1053221
Commit-Queue: Dominic Farolino <domfarolino@gmail.com>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarYoav Weiss <yoav@yoav.ws>
Reviewed-by: default avatarYutaka Hirano <yhirano@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558150}
parent 9c640fca
......@@ -86,6 +86,15 @@ enum FetchCacheMode {
kUnspecifiedForceCacheMiss,
};
// Corresponds to Fetch request's "importance mode"
// Currently discussed at: https://github.com/WICG/priority-hints
// TODO(domfarolino): add a spec link to this once specified.
enum FetchImportanceMode {
kImportanceLow,
kImportanceAuto,
kImportanceHigh
};
struct FetchAPIRequest {
network.mojom.FetchRequestMode mode;
bool is_main_resource_load;
......
......@@ -93,6 +93,10 @@ class PLATFORM_EXPORT FetchParameters {
resource_request_.SetRequestContext(context);
}
void SetFetchImportanceMode(mojom::FetchImportanceMode importance_mode) {
resource_request_.SetFetchImportanceMode(importance_mode);
}
const TextResourceDecoderOptions& DecoderOptions() const {
return decoder_options_;
}
......
......@@ -168,6 +168,56 @@ static ResourceFetcher::ResourceFetcherSet& MainThreadFetchersSet() {
return fetchers;
}
ResourceLoadPriority AdjustPriorityWithPriorityHint(
ResourceLoadPriority priority_so_far,
Resource::Type type,
const ResourceRequest& resource_request,
FetchParameters::DeferOption defer_option,
bool is_link_preload) {
mojom::FetchImportanceMode importance_mode =
resource_request.GetFetchImportanceMode();
ResourceLoadPriority new_priority = priority_so_far;
switch (importance_mode) {
case mojom::FetchImportanceMode::kImportanceAuto:
break;
case mojom::FetchImportanceMode::kImportanceHigh:
// Boost priority of
// - Late and async scripts
// - Images
// - Prefetch
if ((type == Resource::kScript &&
(FetchParameters::kLazyLoad == defer_option)) ||
type == Resource::kImage || type == Resource::kLinkPrefetch) {
new_priority = ResourceLoadPriority::kHigh;
}
DCHECK_LE(priority_so_far, new_priority);
break;
case mojom::FetchImportanceMode::kImportanceLow:
// Demote priority of:
// - Images
// Note: this will only have a real effect on in-viewport images since
// out-of-viewport images already have priority set to kLow
// - Link preloads
// For this initial implementation we do a blanket demotion regardless
// of `as` value/type. TODO(domfarolino): maybe discuss a more
// granular approach with loading team
if (type == Resource::kImage ||
resource_request.GetRequestContext() ==
WebURLRequest::RequestContext::kRequestContextFetch ||
is_link_preload) {
new_priority = ResourceLoadPriority::kLow;
}
DCHECK_LE(new_priority, priority_so_far);
break;
}
return new_priority;
}
} // namespace
ResourceLoadPriority ResourceFetcher::ComputeLoadPriority(
......@@ -224,6 +274,11 @@ ResourceLoadPriority ResourceFetcher::ComputeLoadPriority(
priority = ResourceLoadPriority::kVeryLow;
}
if (RuntimeEnabledFeatures::PriorityHintsEnabled()) {
priority = AdjustPriorityWithPriorityHint(priority, type, resource_request,
defer_option, is_link_preload);
}
// A manually set priority acts as a floor. This is used to ensure that
// synchronous requests are always given the highest possible priority, as
// well as to ensure that there isn't priority churn if images move in and out
......
......@@ -68,6 +68,7 @@ ResourceRequest::ResourceRequest(const KURL& url)
request_context_(WebURLRequest::kRequestContextUnspecified),
frame_type_(network::mojom::RequestContextFrameType::kNone),
fetch_request_mode_(network::mojom::FetchRequestMode::kNoCORS),
fetch_importance_mode_(mojom::FetchImportanceMode::kImportanceAuto),
fetch_credentials_mode_(network::mojom::FetchCredentialsMode::kInclude),
fetch_redirect_mode_(network::mojom::FetchRedirectMode::kFollow),
referrer_policy_(kReferrerPolicyDefault),
......@@ -111,6 +112,7 @@ ResourceRequest::ResourceRequest(CrossThreadResourceRequestData* data)
SetRequestContext(data->request_context_);
SetFrameType(data->frame_type_);
SetFetchRequestMode(data->fetch_request_mode_);
SetFetchImportanceMode(data->fetch_importance_mode_);
SetFetchCredentialsMode(data->fetch_credentials_mode_);
SetFetchRedirectMode(data->fetch_redirect_mode_);
SetFetchIntegrity(data->fetch_integrity_.IsolatedCopy());
......@@ -204,6 +206,7 @@ std::unique_ptr<CrossThreadResourceRequestData> ResourceRequest::CopyData()
data->request_context_ = request_context_;
data->frame_type_ = frame_type_;
data->fetch_request_mode_ = fetch_request_mode_;
data->fetch_importance_mode_ = fetch_importance_mode_;
data->fetch_credentials_mode_ = fetch_credentials_mode_;
data->fetch_redirect_mode_ = fetch_redirect_mode_;
data->fetch_integrity_ = fetch_integrity_.IsolatedCopy();
......
......@@ -286,6 +286,21 @@ class PLATFORM_EXPORT ResourceRequest final {
fetch_request_mode_ = mode;
}
// A resource request's fetch_importance_mode_ is a developer-set priority
// hint that differs from priority_. It is used in
// ResourceFetcher::ComputeLoadPriority to possibly influence the resolved
// priority of a resource request.
// This member exists both here and in FetchParameters, as opposed just in
// the latter because the fetch() API creates a ResourceRequest object long
// before its associaed FetchParameters, so this makes it easier to
// communicate an importance value down to the lower-level fetching code.
mojom::FetchImportanceMode GetFetchImportanceMode() const {
return fetch_importance_mode_;
}
void SetFetchImportanceMode(mojom::FetchImportanceMode mode) {
fetch_importance_mode_ = mode;
}
network::mojom::FetchCredentialsMode GetFetchCredentialsMode() const {
return fetch_credentials_mode_;
}
......@@ -415,6 +430,7 @@ class PLATFORM_EXPORT ResourceRequest final {
WebURLRequest::RequestContext request_context_;
network::mojom::RequestContextFrameType frame_type_;
network::mojom::FetchRequestMode fetch_request_mode_;
mojom::FetchImportanceMode fetch_importance_mode_;
network::mojom::FetchCredentialsMode fetch_credentials_mode_;
network::mojom::FetchRedirectMode fetch_redirect_mode_;
String fetch_integrity_;
......@@ -480,6 +496,7 @@ struct CrossThreadResourceRequestData {
WebURLRequest::RequestContext request_context_;
network::mojom::RequestContextFrameType frame_type_;
network::mojom::FetchRequestMode fetch_request_mode_;
mojom::FetchImportanceMode fetch_importance_mode_;
network::mojom::FetchCredentialsMode fetch_credentials_mode_;
network::mojom::FetchRedirectMode fetch_redirect_mode_;
String fetch_integrity_;
......
......@@ -1001,6 +1001,10 @@
{
name: "PrintBrowser",
},
{
name: "PriorityHints",
status: "experimental",
},
{
name: "PushMessaging",
status: "stable",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment