Commit a8fe1851 authored by Ryan Sturm's avatar Ryan Sturm Committed by Commit Bot

Adding Frame Accept header to Search prefetch requests

This also refactors the content/ accept header code to be in public
along with the signed exchange accept header suffix.

Bug: 1138648
Change-Id: Idf3f5d4f8b3034e301318bdb29d509ee46bc3d10
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2533331
Commit-Queue: Ryan Sturm <ryansturm@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarRobert Ogden <robertogden@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828037}
parent 0166689e
......@@ -18,6 +18,7 @@
#include "components/search_engines/template_url_service.h"
#include "components/variations/net/variations_http_headers.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/frame_accept_header.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_constants.h"
......@@ -100,6 +101,9 @@ void SearchPrefetchService::PrefetchRequest::StartPrefetchRequest(
prefetch_url_, variations::InIncognito::kNo, resource_request.get());
resource_request->headers.SetHeader(content::kCorsExemptPurposeHeaderName,
"prefetch");
resource_request->headers.SetHeader(
net::HttpRequestHeaders::kAccept,
content::FrameAcceptHeaderValue(/*allow_sxg_responses=*/true, profile));
// TODO(ryansturm): Find other headers that may need to be set.
// https://crbug.com/1138648
......
......@@ -452,6 +452,9 @@ IN_PROC_BROWSER_TEST_F(SearchPrefetchServiceEnabledBrowserTest,
EXPECT_EQ(1u, search_server_requests().size());
EXPECT_NE(std::string::npos,
search_server_requests()[0].GetURL().spec().find(search_terms));
auto headers = search_server_requests()[0].headers;
ASSERT_TRUE(base::Contains(headers, "Accept"));
EXPECT_TRUE(base::Contains(headers["Accept"], "text/html"));
EXPECT_EQ(1u, search_server_request_count());
EXPECT_EQ(1u, search_server_prefetch_request_count());
......
......@@ -53,6 +53,7 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/download_utils.h"
#include "content/public/browser/frame_accept_header.h"
#include "content/public/browser/navigation_ui_data.h"
#include "content/public/browser/shared_cors_origin_access_list.h"
#include "content/public/browser/ssl_status.h"
......@@ -278,21 +279,6 @@ void UnknownSchemeCallback(
handled_externally ? net::ERR_ABORTED : net::ERR_UNKNOWN_URL_SCHEME));
}
const char* FrameAcceptHeaderValue() {
#if BUILDFLAG(ENABLE_AV1_DECODER)
static const char kFrameAcceptHeaderValueWithAvif[] =
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,"
"image/webp,image/apng,*/*;q=0.8";
static const char* accept_value =
base::FeatureList::IsEnabled(blink::features::kAVIF)
? kFrameAcceptHeaderValueWithAvif
: network::kFrameAcceptHeaderValue;
return accept_value;
#else
return network::kFrameAcceptHeaderValue;
#endif
}
} // namespace
// TODO(kinuko): Fix the method ordering and move these methods after the ctor.
......@@ -361,13 +347,9 @@ void NavigationURLLoaderImpl::Start(
std::move(factory));
}
std::string accept_header_value = FrameAcceptHeaderValue();
if (signed_exchange_utils::IsSignedExchangeHandlingEnabled(
browser_context_)) {
accept_header_value.append(kAcceptHeaderSignedExchangeSuffix);
}
resource_request_->headers.SetHeader(net::HttpRequestHeaders::kAccept,
accept_header_value);
resource_request_->headers.SetHeader(
net::HttpRequestHeaders::kAccept,
FrameAcceptHeaderValue(/*allow_sxg_responses=*/true, browser_context_));
// Requests to WebUI scheme won't get redirected to/from other schemes
// or be intercepted, so we just let it go here.
......@@ -741,10 +723,12 @@ void NavigationURLLoaderImpl::FollowRedirectInternal(
// Don't send Accept: application/signed-exchange for fallback redirects.
if (redirect_info_.is_signed_exchange_fallback_redirect) {
std::string header_value =
FrameAcceptHeaderValue(/*allow_sxg_responses=*/false, browser_context_);
url_loader_modified_headers_.SetHeader(net::HttpRequestHeaders::kAccept,
FrameAcceptHeaderValue());
header_value);
resource_request_->headers.SetHeader(net::HttpRequestHeaders::kAccept,
FrameAcceptHeaderValue());
header_value);
}
Restart();
......
......@@ -154,6 +154,8 @@ source_set("browser_sources") {
"file_url_loader.h",
"focused_node_details.h",
"font_list_async.h",
"frame_accept_header.cc",
"frame_accept_header.h",
"frame_service_base.h",
"generated_code_cache_settings.h",
"global_request_id.h",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/browser/frame_accept_header.h"
#include "base/feature_list.h"
#include "build/build_config.h"
#include "content/browser/web_package/signed_exchange_consts.h"
#include "content/browser/web_package/signed_exchange_utils.h"
#include "services/network/public/cpp/constants.h"
#include "third_party/blink/public/common/features.h"
namespace content {
std::string FrameAcceptHeaderValue(bool allow_sxg_responses,
BrowserContext* browser_context) {
std::string header_value = network::kFrameAcceptHeaderValue;
#if BUILDFLAG(ENABLE_AV1_DECODER)
static const char kFrameAcceptHeaderValueWithAvif[] =
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,"
"image/webp,image/apng,*/*;q=0.8";
if (base::FeatureList::IsEnabled(blink::features::kAVIF))
header_value = kFrameAcceptHeaderValueWithAvif;
#endif
if (allow_sxg_responses &&
content::signed_exchange_utils::IsSignedExchangeHandlingEnabled(
browser_context)) {
header_value.append(kAcceptHeaderSignedExchangeSuffix);
}
return header_value;
}
} // namespace content
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_PUBLIC_BROWSER_FRAME_ACCEPT_HEADER_H_
#define CONTENT_PUBLIC_BROWSER_FRAME_ACCEPT_HEADER_H_
#include <string>
#include "content/common/content_export.h"
namespace content {
class BrowserContext;
// The value that should be set for the "Accept" header for frame navigations.
// Includes signed exchange and image decoder information (when applicable) that
// are not available from the network service. This may also accept signed
// exchange responses when |allow_sxg_responses| is true.
CONTENT_EXPORT std::string FrameAcceptHeaderValue(
bool allow_sxg_responses,
BrowserContext* browser_context);
} // namespace content
#endif // CONTENT_PUBLIC_BROWSER_FRAME_ACCEPT_HEADER_H_
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