Commit 40069dee authored by rajendrant's avatar rajendrant Committed by Commit Bot

Redirect only the public subresource URLs

This CL uses optimization guide to fetch the public image URLs as hints
and sends to renderer where URL loader throttles allow redirect only
for the subresource URLs in the list.

Change-Id: Id654d041c376420a6e4d2a4daf3fdb51b24056f9
Bug: 1042869
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1996944
Commit-Queue: rajendrant <rajendrant@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Reviewed-by: default avatarSophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#735105}
parent f75bb041
......@@ -1751,6 +1751,8 @@ jumbo_static_library("browser") {
"subresource_filter/subresource_filter_profile_context.h",
"subresource_filter/subresource_filter_profile_context_factory.cc",
"subresource_filter/subresource_filter_profile_context_factory.h",
"subresource_redirect/subresource_redirect_observer.cc",
"subresource_redirect/subresource_redirect_observer.h",
"sync/bookmark_sync_service_factory.cc",
"sync/bookmark_sync_service_factory.h",
"sync/chrome_sync_client.cc",
......
file://components/data_reduction_proxy/OWNERS
# COMPONENT: Internals>Network>DataProxy
// 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 "chrome/browser/subresource_redirect/subresource_redirect_observer.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
#include "components/optimization_guide/optimization_guide_decider.h"
#include "components/optimization_guide/proto/performance_hints_metadata.pb.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/loader/previews_resource_loading_hints.mojom.h"
#include "url/gurl.h"
namespace subresource_redirect {
namespace {
// Returns the OptimizationGuideDecider when LiteMode and the subresource
// redirect feature are enabled.
optimization_guide::OptimizationGuideDecider*
GetOptimizationGuideDeciderFromWebContents(content::WebContents* web_contents) {
DCHECK(base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect));
if (!web_contents)
return nullptr;
if (Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
if (data_reduction_proxy::DataReductionProxySettings::
IsDataSaverEnabledByUser(profile->IsOffTheRecord(),
profile->GetPrefs())) {
return OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
}
}
return nullptr;
}
// Pass down the |images_hints| to the appropriate renderer for the navigation
// |navigation_handle|.
void SetResourceLoadingImageHints(
content::NavigationHandle* navigation_handle,
blink::mojom::CompressPublicImagesHintsPtr images_hints) {
mojo::AssociatedRemote<blink::mojom::PreviewsResourceLoadingHintsReceiver>
loading_hints_agent;
if (navigation_handle->GetRenderFrameHost()
->GetRemoteAssociatedInterfaces()) {
navigation_handle->GetRenderFrameHost()
->GetRemoteAssociatedInterfaces()
->GetInterface(&loading_hints_agent);
loading_hints_agent->SetCompressPublicImagesHints(std::move(images_hints));
}
}
} // namespace
// static
void SubresourceRedirectObserver::MaybeCreateForWebContents(
content::WebContents* web_contents) {
if (base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect)) {
SubresourceRedirectObserver::CreateForWebContents(web_contents);
}
}
SubresourceRedirectObserver::SubresourceRedirectObserver(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {
auto* optimization_guide_decider =
GetOptimizationGuideDeciderFromWebContents(web_contents);
if (optimization_guide_decider) {
optimization_guide_decider->RegisterOptimizationTypesAndTargets(
{optimization_guide::proto::COMPRESS_PUBLIC_IMAGES}, {});
}
}
void SubresourceRedirectObserver::ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) {
DCHECK(navigation_handle);
if (!navigation_handle->IsInMainFrame() ||
navigation_handle->IsSameDocument()) {
return;
}
auto* optimization_guide_decider = GetOptimizationGuideDeciderFromWebContents(
navigation_handle->GetWebContents());
if (!optimization_guide_decider)
return;
optimization_guide::OptimizationMetadata optimization_metadata;
if (optimization_guide_decider->CanApplyOptimization(
navigation_handle, optimization_guide::proto::COMPRESS_PUBLIC_IMAGES,
&optimization_metadata) !=
optimization_guide::OptimizationGuideDecision::kTrue) {
return;
}
std::vector<std::string> public_image_urls;
public_image_urls.reserve(
optimization_metadata.public_image_metadata.url_size());
for (const auto& url : optimization_metadata.public_image_metadata.url())
public_image_urls.push_back(url);
// Pass down the image URLs to renderer even if it could be empty. This acts
// as a signal that the image hint fetch has finished, for coverage metrics
// purposes.
SetResourceLoadingImageHints(
navigation_handle,
blink::mojom::CompressPublicImagesHints::New(public_image_urls));
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(SubresourceRedirectObserver)
} // namespace subresource_redirect
// 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 CHROME_BROWSER_SUBRESOURCE_REDIRECT_SUBRESOURCE_REDIRECT_OBSERVER_H_
#define CHROME_BROWSER_SUBRESOURCE_REDIRECT_SUBRESOURCE_REDIRECT_OBSERVER_H_
#include "base/macros.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
namespace content {
class NavigationHandle;
class WebContents;
} // namespace content
namespace subresource_redirect {
// Sends the public image URL hints to renderer.
class SubresourceRedirectObserver
: public content::WebContentsObserver,
public content::WebContentsUserData<SubresourceRedirectObserver> {
public:
static void MaybeCreateForWebContents(content::WebContents* web_contents);
~SubresourceRedirectObserver() override = default;
SubresourceRedirectObserver(const SubresourceRedirectObserver&) = delete;
SubresourceRedirectObserver& operator=(const SubresourceRedirectObserver&) =
delete;
private:
friend class content::WebContentsUserData<SubresourceRedirectObserver>;
explicit SubresourceRedirectObserver(content::WebContents* web_contents);
// content::WebContentsObserver.
void ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) override;
WEB_CONTENTS_USER_DATA_KEY_DECL();
};
} // namespace subresource_redirect
#endif // CHROME_BROWSER_SUBRESOURCE_REDIRECT_SUBRESOURCE_REDIRECT_OBSERVER_H_
......@@ -59,6 +59,7 @@
#include "chrome/browser/ssl/connection_help_tab_helper.h"
#include "chrome/browser/ssl/security_state_tab_helper.h"
#include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h"
#include "chrome/browser/subresource_redirect/subresource_redirect_observer.h"
#include "chrome/browser/sync/sessions/sync_sessions_router_tab_helper.h"
#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router_factory.h"
#include "chrome/browser/sync/sync_encryption_keys_tab_helper.h"
......@@ -277,6 +278,8 @@ void TabHelpers::AttachTabHelpers(WebContents* web_contents) {
if (SiteEngagementService::IsEnabled())
SiteEngagementService::Helper::CreateForWebContents(web_contents);
SoundContentSettingObserver::CreateForWebContents(web_contents);
subresource_redirect::SubresourceRedirectObserver::MaybeCreateForWebContents(
web_contents);
sync_sessions::SyncSessionsRouterTabHelper::CreateForWebContents(
web_contents,
sync_sessions::SyncSessionsWebContentsRouterFactory::GetForProfile(
......
......@@ -96,6 +96,8 @@ jumbo_static_library("renderer") {
"sandbox_status_extension_android.h",
"subresource_redirect/subresource_redirect_experiments.cc",
"subresource_redirect/subresource_redirect_experiments.h",
"subresource_redirect/subresource_redirect_hints_agent.cc",
"subresource_redirect/subresource_redirect_hints_agent.h",
"subresource_redirect/subresource_redirect_params.cc",
"subresource_redirect/subresource_redirect_params.h",
"subresource_redirect/subresource_redirect_url_loader_throttle.cc",
......
......@@ -34,7 +34,9 @@ const blink::WebVector<blink::WebString> convert_to_web_vector(
ResourceLoadingHintsAgent::ResourceLoadingHintsAgent(
blink::AssociatedInterfaceRegistry* associated_interfaces,
content::RenderFrame* render_frame)
: content::RenderFrameObserver(render_frame) {
: content::RenderFrameObserver(render_frame),
content::RenderFrameObserverTracker<ResourceLoadingHintsAgent>(
render_frame) {
DCHECK(render_frame);
DCHECK(IsMainFrame());
......@@ -100,4 +102,10 @@ void ResourceLoadingHintsAgent::SetResourceLoadingHints(
}
}
void ResourceLoadingHintsAgent::SetCompressPublicImagesHints(
blink::mojom::CompressPublicImagesHintsPtr images_hints) {
subresource_redirect_hints_agent_.SetCompressPublicImagesHints(
std::move(images_hints));
}
} // namespace previews
......@@ -11,7 +11,9 @@
#include "base/logging.h"
#include "base/macros.h"
#include "base/optional.h"
#include "chrome/renderer/subresource_redirect/subresource_redirect_hints_agent.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_frame_observer_tracker.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "services/service_manager/public/cpp/interface_provider.h"
......@@ -29,13 +31,19 @@ namespace previews {
class ResourceLoadingHintsAgent
: public content::RenderFrameObserver,
public blink::mojom::PreviewsResourceLoadingHintsReceiver,
public base::SupportsWeakPtr<ResourceLoadingHintsAgent> {
public base::SupportsWeakPtr<ResourceLoadingHintsAgent>,
public content::RenderFrameObserverTracker<ResourceLoadingHintsAgent> {
public:
ResourceLoadingHintsAgent(
blink::AssociatedInterfaceRegistry* associated_interfaces,
content::RenderFrame* render_frame);
~ResourceLoadingHintsAgent() override;
subresource_redirect::SubresourceRedirectHintsAgent&
subresource_redirect_hints_agent() {
return subresource_redirect_hints_agent_;
}
private:
// content::RenderFrameObserver:
void DidCreateNewDocument() override;
......@@ -46,6 +54,8 @@ class ResourceLoadingHintsAgent
// blink::mojom::PreviewsResourceLoadingHintsReceiver:
void SetResourceLoadingHints(blink::mojom::PreviewsResourceLoadingHintsPtr
resource_loading_hints) override;
void SetCompressPublicImagesHints(
blink::mojom::CompressPublicImagesHintsPtr images_hints) override;
void SetReceiver(
mojo::PendingAssociatedReceiver<
......@@ -59,6 +69,9 @@ class ResourceLoadingHintsAgent
mojo::AssociatedReceiver<blink::mojom::PreviewsResourceLoadingHintsReceiver>
receiver_{this};
subresource_redirect::SubresourceRedirectHintsAgent
subresource_redirect_hints_agent_;
DISALLOW_COPY_AND_ASSIGN(ResourceLoadingHintsAgent);
};
......
tbansal@chromium.org
robertogden@chromium.org
file://components/data_reduction_proxy/OWNERS
# COMPONENT: Internals>Network>DataProxy
\ No newline at end of file
# COMPONENT: Internals>Network>DataProxy
// 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 "chrome/renderer/subresource_redirect/subresource_redirect_hints_agent.h"
namespace subresource_redirect {
SubresourceRedirectHintsAgent::SubresourceRedirectHintsAgent() = default;
SubresourceRedirectHintsAgent::~SubresourceRedirectHintsAgent() = default;
void SubresourceRedirectHintsAgent::SetCompressPublicImagesHints(
blink::mojom::CompressPublicImagesHintsPtr images_hints) {
public_image_urls_ = images_hints->image_urls;
}
bool SubresourceRedirectHintsAgent::ShouldRedirectImage(const GURL& url) const {
GURL::Replacements rep;
rep.ClearRef();
// TODO(rajendrant): Skip redirection if the URL contains username or password
return public_image_urls_.find(url.ReplaceComponents(rep).spec()) !=
public_image_urls_.end();
}
} // namespace subresource_redirect
// 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 CHROME_RENDERER_SUBRESOURCE_REDIRECT_SUBRESOURCE_REDIRECT_HINTS_AGENT_H_
#define CHROME_RENDERER_SUBRESOURCE_REDIRECT_SUBRESOURCE_REDIRECT_HINTS_AGENT_H_
#include "base/containers/flat_set.h"
#include "base/macros.h"
#include "third_party/blink/public/mojom/loader/previews_resource_loading_hints.mojom.h"
#include "url/gurl.h"
namespace subresource_redirect {
// Holds the public image URL hints to be queried by URL loader throttles.
class SubresourceRedirectHintsAgent {
public:
SubresourceRedirectHintsAgent();
~SubresourceRedirectHintsAgent();
SubresourceRedirectHintsAgent(const SubresourceRedirectHintsAgent&) = delete;
SubresourceRedirectHintsAgent& operator=(
const SubresourceRedirectHintsAgent&) = delete;
void SetCompressPublicImagesHints(
blink::mojom::CompressPublicImagesHintsPtr images_hints);
bool ShouldRedirectImage(const GURL& url) const;
private:
base::flat_set<std::string> public_image_urls_;
};
} // namespace subresource_redirect
#endif // CHROME_RENDERER_SUBRESOURCE_REDIRECT_SUBRESOURCE_REDIRECT_HINTS_AGENT_H_
......@@ -5,12 +5,15 @@
#include "chrome/renderer/subresource_redirect/subresource_redirect_url_loader_throttle.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/renderer/previews/resource_loading_hints_agent.h"
#include "chrome/renderer/subresource_redirect/subresource_redirect_experiments.h"
#include "chrome/renderer/subresource_redirect/subresource_redirect_hints_agent.h"
#include "chrome/renderer/subresource_redirect/subresource_redirect_params.h"
#include "chrome/renderer/subresource_redirect/subresource_redirect_util.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h"
#include "content/public/common/previews_state.h"
#include "content/public/common/resource_type.h"
#include "content/public/renderer/render_frame.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
......@@ -25,7 +28,8 @@ namespace subresource_redirect {
std::unique_ptr<SubresourceRedirectURLLoaderThrottle>
SubresourceRedirectURLLoaderThrottle::MaybeCreateThrottle(
const blink::WebURLRequest& request,
content::ResourceType resource_type) {
content::ResourceType resource_type,
int render_frame_id) {
if (base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect) &&
resource_type == content::ResourceType::kImage &&
(request.GetPreviewsState() &
......@@ -34,13 +38,15 @@ SubresourceRedirectURLLoaderThrottle::MaybeCreateThrottle(
// TODO(rajendrant): Verify that data saver is enabled as well, to not
// trigger the subresource redirect for incognito profiles.
return base::WrapUnique<SubresourceRedirectURLLoaderThrottle>(
new SubresourceRedirectURLLoaderThrottle());
new SubresourceRedirectURLLoaderThrottle(render_frame_id));
}
return nullptr;
}
SubresourceRedirectURLLoaderThrottle::SubresourceRedirectURLLoaderThrottle() =
default;
SubresourceRedirectURLLoaderThrottle::SubresourceRedirectURLLoaderThrottle(
int render_frame_id)
: render_frame_id_(render_frame_id) {}
SubresourceRedirectURLLoaderThrottle::~SubresourceRedirectURLLoaderThrottle() =
default;
......@@ -54,6 +60,13 @@ void SubresourceRedirectURLLoaderThrottle::WillStartRequest(
content::PreviewsTypes::SUBRESOURCE_REDIRECT_ON);
DCHECK(request->url.SchemeIs(url::kHttpsScheme));
auto* subresource_redirect_hints_agent = GetSubresourceRedirectHintsAgent();
if (!subresource_redirect_hints_agent)
return;
if (!subresource_redirect_hints_agent->ShouldRedirectImage(request->url))
return;
// Image subresources that have paths that do not end in one of the
// following common formats are commonly single pixel images that will not
// benefit from being sent to the compression server.
......@@ -64,6 +77,18 @@ void SubresourceRedirectURLLoaderThrottle::WillStartRequest(
*defer = false;
}
SubresourceRedirectHintsAgent*
SubresourceRedirectURLLoaderThrottle::GetSubresourceRedirectHintsAgent() {
if (content::RenderFrame* render_frame =
content::RenderFrame::FromRoutingID(render_frame_id_)) {
if (auto* resource_loading_hints_agent =
previews::ResourceLoadingHintsAgent::Get(render_frame)) {
return &resource_loading_hints_agent->subresource_redirect_hints_agent();
}
}
return nullptr;
}
void SubresourceRedirectURLLoaderThrottle::WillRedirectRequest(
net::RedirectInfo* redirect_info,
const network::mojom::URLResponseHead& response_head,
......
......@@ -15,16 +15,22 @@ class WebURLRequest;
namespace subresource_redirect {
class SubresourceRedirectHintsAgent;
// This class handles internal redirects for subresouces on HTTPS sites to
// compressed versions of subresources.
class SubresourceRedirectURLLoaderThrottle : public blink::URLLoaderThrottle {
public:
static std::unique_ptr<SubresourceRedirectURLLoaderThrottle>
MaybeCreateThrottle(const blink::WebURLRequest& request,
content::ResourceType resource_type);
content::ResourceType resource_type,
int render_frame_id);
~SubresourceRedirectURLLoaderThrottle() override;
// virtual for testing.
virtual SubresourceRedirectHintsAgent* GetSubresourceRedirectHintsAgent();
// blink::URLLoaderThrottle:
void WillStartRequest(network::ResourceRequest* request,
bool* defer) override;
......@@ -47,7 +53,13 @@ class SubresourceRedirectURLLoaderThrottle : public blink::URLLoaderThrottle {
void DetachFromCurrentSequence() override;
private:
SubresourceRedirectURLLoaderThrottle();
friend class TestSubresourceRedirectURLLoaderThrottle;
explicit SubresourceRedirectURLLoaderThrottle(int render_frame_id);
// Render frame id to get the hints agent of the render frame.
const int render_frame_id_;
DISALLOW_COPY_AND_ASSIGN(SubresourceRedirectURLLoaderThrottle);
};
......
......@@ -5,6 +5,7 @@
#include "chrome/renderer/subresource_redirect/subresource_redirect_url_loader_throttle.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/renderer/subresource_redirect/subresource_redirect_hints_agent.h"
#include "chrome/renderer/subresource_redirect/subresource_redirect_util.h"
#include "content/public/common/previews_state.h"
#include "content/public/common/resource_type.h"
......@@ -15,17 +16,43 @@
namespace subresource_redirect {
int kRenderFrameID = 1;
class TestSubresourceRedirectURLLoaderThrottle
: public SubresourceRedirectURLLoaderThrottle {
public:
explicit TestSubresourceRedirectURLLoaderThrottle(
std::vector<std::string> public_image_urls)
: SubresourceRedirectURLLoaderThrottle(kRenderFrameID) {
subresource_redirect_hints_agent_.SetCompressPublicImagesHints(
blink::mojom::CompressPublicImagesHints::New(public_image_urls));
}
SubresourceRedirectHintsAgent* GetSubresourceRedirectHintsAgent() override {
return &subresource_redirect_hints_agent_;
}
private:
SubresourceRedirectHintsAgent subresource_redirect_hints_agent_;
};
namespace {
std::unique_ptr<SubresourceRedirectURLLoaderThrottle>
CreateSubresourceRedirectURLLoaderThrottle(const GURL& url,
content::ResourceType resource_type,
int previews_state) {
CreateSubresourceRedirectURLLoaderThrottle(
const GURL& url,
content::ResourceType resource_type,
int previews_state,
const std::vector<std::string>& public_image_urls) {
blink::WebURLRequest request;
request.SetUrl(url);
request.SetPreviewsState(previews_state);
return SubresourceRedirectURLLoaderThrottle::MaybeCreateThrottle(
request, resource_type);
DCHECK(SubresourceRedirectURLLoaderThrottle::MaybeCreateThrottle(
request, resource_type, kRenderFrameID)
.get() != nullptr);
return std::make_unique<TestSubresourceRedirectURLLoaderThrottle>(
public_image_urls);
}
TEST(SubresourceRedirectURLLoaderThrottleTest, TestMaybeCreateThrottle) {
......@@ -76,29 +103,42 @@ TEST(SubresourceRedirectURLLoaderThrottleTest, TestMaybeCreateThrottle) {
request.SetUrl(GURL(test_case.url));
EXPECT_EQ(test_case.expected_is_throttle_created,
SubresourceRedirectURLLoaderThrottle::MaybeCreateThrottle(
request, test_case.resource_type) != nullptr);
request, test_case.resource_type, kRenderFrameID) != nullptr);
}
}
TEST(SubresourceRedirectURLLoaderThrottleTest, TestGetSubresourceURL) {
struct TestCase {
GURL original_url;
GURL redirected_subresource_url;
GURL redirected_subresource_url; // Empty URL means there will be no
// redirect.
};
const TestCase kTestCases[]{
{
GURL("https://www.test.com/test.jpg"),
GetSubresourceURLForURL(GURL("https://www.test.com/test.jpg")),
GURL("https://www.test.com/public_img.jpg"),
GetSubresourceURLForURL(GURL("https://www.test.com/public_img.jpg")),
},
{
GURL("https://www.test.com/test.jpg#test"),
GetSubresourceURLForURL(GURL("https://www.test.com/test.jpg#test")),
GURL("https://www.test.com/public_img.jpg#anchor"),
GetSubresourceURLForURL(
GURL("https://www.test.com/public_img.jpg#anchor")),
},
{
GURL("https://www.test.com/test.jpg?foo=bar&baz"),
GURL("https://www.test.com/"
"public_img.jpg?public_arg1=bar&public_arg2"),
GetSubresourceURLForURL(
GURL("https://www.test.com/test.jpg?foo=bar&baz")),
GURL("https://www.test.com/"
"public_img.jpg?public_arg1=bar&public_arg2")),
},
// Private images will not be redirected.
{
GURL("https://www.test.com/private_img.jpg"),
GURL(),
},
{
GURL("https://www.test.com/public_img.jpg&private_arg1=foo"),
GURL(),
},
};
base::test::ScopedFeatureList scoped_feature_list;
......@@ -108,16 +148,23 @@ TEST(SubresourceRedirectURLLoaderThrottleTest, TestGetSubresourceURL) {
for (const TestCase& test_case : kTestCases) {
auto throttle = CreateSubresourceRedirectURLLoaderThrottle(
test_case.original_url, content::ResourceType::kImage,
content::PreviewsTypes::SUBRESOURCE_REDIRECT_ON);
content::PreviewsTypes::SUBRESOURCE_REDIRECT_ON,
{"https://www.test.com/public_img.jpg",
"https://www.test.com/public_img.jpg#anchor",
"https://www.test.com/public_img.jpg?public_arg1=bar&public_arg2"});
network::ResourceRequest request;
request.url = test_case.original_url;
request.resource_type = static_cast<int>(content::ResourceType::kImage);
request.previews_state = content::PreviewsTypes::SUBRESOURCE_REDIRECT_ON;
bool defer = false;
bool defer = true;
throttle->WillStartRequest(&request, &defer);
EXPECT_FALSE(defer);
EXPECT_EQ(request.url, test_case.redirected_subresource_url);
EXPECT_EQ(defer, test_case.redirected_subresource_url.is_empty());
if (!test_case.redirected_subresource_url.is_empty()) {
EXPECT_EQ(request.url, test_case.redirected_subresource_url);
} else {
EXPECT_EQ(request.url, test_case.original_url);
}
}
}
......@@ -128,7 +175,8 @@ TEST(SubresourceRedirectURLLoaderThrottleTest, DeferOverridenToFalse) {
auto throttle = CreateSubresourceRedirectURLLoaderThrottle(
GURL("https://www.test.com/test.jpg"), content::ResourceType::kImage,
content::PreviewsTypes::SUBRESOURCE_REDIRECT_ON);
content::PreviewsTypes::SUBRESOURCE_REDIRECT_ON,
{"https://www.test.com/test.jpg"});
network::ResourceRequest request;
request.url = GURL("https://www.test.com/test.jpg");
request.resource_type = static_cast<int>(content::ResourceType::kImage);
......
......@@ -259,7 +259,7 @@ URLLoaderThrottleProviderImpl::CreateThrottles(
#endif // defined(OS_CHROMEOS)
auto throttle = subresource_redirect::SubresourceRedirectURLLoaderThrottle::
MaybeCreateThrottle(request, resource_type);
MaybeCreateThrottle(request, resource_type, render_frame_id);
if (throttle)
throttles.push_back(std::move(throttle));
......
<html>
<head></head>
<img alt="long_placeholder_text" src="image.png#fragment" />
<script>
function checkImage() {
sendValueToTest(document.images[0].complete);
}
function imageSrc() {
sendValueToTest(document.images[0].src);
}
function sendValueToTest(value) {
window.domAutomationController.send(value);
}
</script>
</html>
<html>
<head></head>
<img src="image.png" />
<img src="image.png?foo" />
<script>
function checkBothImagesLoaded() {
sendValueToTest(document.images[0].complete && document.images[1].complete);
}
function imageSrc() {
sendValueToTest(document.images[0].src);
}
function sendValueToTest(value) {
window.domAutomationController.send(value);
}
</script>
</html>
......@@ -212,6 +212,36 @@ TestHintsComponentCreator::CreateHintsComponentInfoWithMixPageHints(
return WriteConfigToFileAndReturnHintsComponentInfo(config);
}
optimization_guide::HintsComponentInfo
TestHintsComponentCreator::CreateHintsComponentInfoWithPublicImageHints(
const std::vector<std::string>& page_hint_host_suffixes,
const std::string& page_pattern,
const std::vector<std::string>& public_image_urls) {
optimization_guide::proto::Configuration config;
for (const auto& page_hint_site : page_hint_host_suffixes) {
optimization_guide::proto::Hint* hint = config.add_hints();
hint->set_key(page_hint_site);
hint->set_key_representation(optimization_guide::proto::HOST_SUFFIX);
hint->set_version(GetDefaultHintVersionString());
optimization_guide::proto::PageHint* page_hint = hint->add_page_hints();
page_hint->set_page_pattern(page_pattern);
optimization_guide::proto::Optimization* optimization =
page_hint->add_whitelisted_optimizations();
optimization->set_optimization_type(
optimization_guide::proto::COMPRESS_PUBLIC_IMAGES);
optimization_guide::proto::PublicImageMetadata* public_image_metadata =
optimization->mutable_public_image_metadata();
for (auto url : public_image_urls) {
public_image_metadata->add_url(url);
}
}
return WriteConfigToFileAndReturnHintsComponentInfo(config);
}
base::FilePath TestHintsComponentCreator::GetFilePath(
std::string file_path_suffix) {
base::ScopedAllowBlockingForTesting allow_blocking;
......
......@@ -67,6 +67,16 @@ class TestHintsComponentCreator {
const std::vector<std::string>& experimental_resource_patterns,
const std::vector<std::string>& default_resource_patterns);
// Creates component data based on |whitelisted_host_suffixes| and
// |page_pattern| with page hints for type |optimization_type| blocking
// resources specified by |resource_patterns|, and returns the
// HintsComponentInfo for it.
optimization_guide::HintsComponentInfo
CreateHintsComponentInfoWithPublicImageHints(
const std::vector<std::string>& page_hint_host_suffixes,
const std::string& page_pattern,
const std::vector<std::string>& public_image_urls);
private:
// Returns the scoped temp directory path with the |file_path_suffix| that is
// valid for the lifetime of this instance.
......
......@@ -19,9 +19,19 @@ struct PreviewsResourceLoadingHints {
array<string> subresources_to_block;
};
// Image loading hints passed by the browser to renderers. Send at most once
// per page load from browser to renderer. The hints are provided to the
// renderer and aid in redirecting these public images to compressed versions.
struct CompressPublicImagesHints {
array<string> image_urls;
};
// Render process implemented interface that receives hints from the browser
// about how to load resources.
interface PreviewsResourceLoadingHintsReceiver {
// Sends the resource loading hints from the browser to renderers.
SetResourceLoadingHints(PreviewsResourceLoadingHints previews_resource_loading_hints);
// Sends the public image URL hints from the browser to renderers.
SetCompressPublicImagesHints(CompressPublicImagesHints images_hints);
};
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