Commit 14283672 authored by Tarun Bansal's avatar Tarun Bansal Committed by Commit Bot

Send UI service provided resource loading hints to renderer

Currently, we send an empty string as a resource loading hint to the
renderer when doing previews resource loading hints optimization.
This CL changes the browser's behavior to send the resource loading
hints that are provided by Previews UI service.

The CL also enables browsertest to modify the list of hints that are
sent to the renderer. This will be used in a browsertest in a subsequent
CL to verify that subresources that match the pattern are not loaded.

Bug: 856243,856247
Change-Id: I29a393dd37090fcb8dc5706669e88e0ae91f47db
Reviewed-on: https://chromium-review.googlesource.com/1136885Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575395}
parent 94489a86
......@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
......@@ -9,7 +12,10 @@
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/subprocess_metrics_provider.h"
#include "chrome/browser/previews/previews_service.h"
#include "chrome/browser/previews/previews_service_factory.h"
#include "chrome/browser/previews/resource_loading_hints/resource_loading_hints_web_contents_observer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
......@@ -17,6 +23,7 @@
#include "components/optimization_guide/optimization_guide_service_observer.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/optimization_guide/test_component_creator.h"
#include "components/previews/content/previews_ui_service.h"
#include "components/previews/core/previews_black_list.h"
#include "components/previews/core/previews_features.h"
#include "content/public/test/browser_test_utils.h"
......@@ -121,6 +128,24 @@ class ResourceLoadingNoFeaturesBrowserTest : public InProcessBrowserTest {
cmd->AppendSwitchASCII("force-effective-connection-type", "Slow-2G");
}
void SetResourceLoadingHintsPatterns() {
Profile* profile = browser()->profile();
DCHECK(!profile->IsOffTheRecord());
PreviewsService* previews_service =
PreviewsServiceFactory::GetForProfile(profile);
previews::PreviewsUIService* previews_ui_service =
previews_service->previews_ui_service();
std::vector<std::string> hints;
hints.push_back("jpg");
hints.push_back("png");
hints.push_back("woff2");
previews_ui_service->SetResourceLoadingHintsResourcePatternsToBlock(
https_url_, hints);
}
void SetResourceLoadingHintsWhitelist(
const std::vector<std::string>&
whitelisted_resource_loading_hints_sites) {
......@@ -215,6 +240,7 @@ class ResourceLoadingHintsBrowserTest
IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
MAYBE_ResourceLoadingHintsHttpsWhitelisted) {
SetResourceLoadingHintsPatterns();
TestOptimizationGuideServiceObserver observer;
AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle();
......@@ -235,8 +261,9 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1);
histogram_tester.ExpectBucketCount(
"Previews.InfoBarAction.ResourceLoadingHints", 0, 1);
// SetResourceLoadingHintsPatterns sets 3 resource loading hints patterns.
histogram_tester.ExpectBucketCount(
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 1, 1);
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 1);
// Load the same webpage to ensure that the resource loading hints are sent
// again.
......@@ -249,13 +276,15 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 2);
histogram_tester.ExpectBucketCount(
"Previews.InfoBarAction.ResourceLoadingHints", 0, 2);
// SetResourceLoadingHintsPatterns sets 3 resource loading hints patterns.
histogram_tester.ExpectBucketCount(
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 1, 2);
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 2);
}
IN_PROC_BROWSER_TEST_F(
ResourceLoadingHintsBrowserTest,
MAYBE_ResourceLoadingHintsHttpsWhitelistedRedirectToHttps) {
SetResourceLoadingHintsPatterns();
TestOptimizationGuideServiceObserver observer;
AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle();
......@@ -274,12 +303,14 @@ IN_PROC_BROWSER_TEST_F(
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1);
histogram_tester.ExpectTotalCount(
"Previews.InfoBarAction.ResourceLoadingHints", 1);
// SetResourceLoadingHintsPatterns sets 3 resource loading hints patterns.
histogram_tester.ExpectBucketCount(
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 1, 1);
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 1);
}
IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
ResourceLoadingHintsHttpsNoWhitelisted) {
SetResourceLoadingHintsPatterns();
TestOptimizationGuideServiceObserver observer;
AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle();
......@@ -304,6 +335,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
ResourceLoadingHintsHttp) {
SetResourceLoadingHintsPatterns();
TestOptimizationGuideServiceObserver observer;
AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle();
......@@ -328,6 +360,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
ResourceLoadingHintsHttpsWhitelistedNoTransform) {
SetResourceLoadingHintsPatterns();
TestOptimizationGuideServiceObserver observer;
AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle();
......
......@@ -7,8 +7,11 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "chrome/browser/loader/chrome_navigation_data.h"
#include "chrome/browser/previews/previews_service.h"
#include "chrome/browser/previews/previews_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/previews/content/previews_content_util.h"
#include "components/previews/content/previews_ui_service.h"
#include "components/previews/core/previews_experiments.h"
#include "components/previews/core/previews_user_data.h"
#include "content/public/browser/browser_thread.h"
......@@ -28,6 +31,8 @@ ResourceLoadingHintsWebContentsObserver::
ResourceLoadingHintsWebContentsObserver(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
profile_ = Profile::FromBrowserContext(web_contents->GetBrowserContext());
}
void ResourceLoadingHintsWebContentsObserver::DidFinishNavigation(
......@@ -70,8 +75,28 @@ void ResourceLoadingHintsWebContentsObserver::SendResourceLoadingHints(
blink::mojom::PreviewsResourceLoadingHintsPtr hints_ptr =
blink::mojom::PreviewsResourceLoadingHints::New();
// TOOD(tbansal): https://crbug.com/856243. Send an actual list of resource
// URLs to block.
hints_ptr->subresources_to_block.push_back(std::string());
const std::vector<std::string>& hints =
GetResourceLoadingHintsResourcePatternsToBlock(
navigation_handle->GetURL());
if (hints.empty())
return;
for (const std::string& hint : hints)
hints_ptr->subresources_to_block.push_back(hint);
hints_receiver_ptr->SetResourceLoadingHints(std::move(hints_ptr));
}
const std::vector<std::string> ResourceLoadingHintsWebContentsObserver::
GetResourceLoadingHintsResourcePatternsToBlock(
const GURL& document_gurl) const {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(profile_);
PreviewsService* previews_service =
PreviewsServiceFactory::GetForProfile(profile_);
previews::PreviewsUIService* previews_ui_service =
previews_service->previews_ui_service();
return previews_ui_service->GetResourceLoadingHintsResourcePatternsToBlock(
document_gurl);
}
......@@ -5,10 +5,15 @@
#ifndef CHROME_BROWSER_PREVIEWS_RESOURCE_LOADING_HINTS_RESOURCE_LOADING_HINTS_WEB_CONTENTS_OBSERVER_H_
#define CHROME_BROWSER_PREVIEWS_RESOURCE_LOADING_HINTS_RESOURCE_LOADING_HINTS_WEB_CONTENTS_OBSERVER_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
class Profile;
// Observes navigation events and sends the resource loading hints mojo message
// to the renderer.
class ResourceLoadingHintsWebContentsObserver
......@@ -35,6 +40,17 @@ class ResourceLoadingHintsWebContentsObserver
void SendResourceLoadingHints(
content::NavigationHandle* navigation_handle) const;
// Returns the pattern of resources that should be blocked when loading
// |document_gurl|. The pattern may be a single substring to match against the
// URL or it may be an ordered set of substrings to match where the substrings
// are separated by the ‘*’ wildcard character (with an implicit ‘*’ at the
// beginning and end).
const std::vector<std::string> GetResourceLoadingHintsResourcePatternsToBlock(
const GURL& document_gurl) const;
// Set in constructor.
Profile* profile_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(ResourceLoadingHintsWebContentsObserver);
};
......
......@@ -99,6 +99,26 @@ void PreviewsUIService::OnIgnoreBlacklistDecisionStatusChanged(bool ignored) {
logger_->OnIgnoreBlacklistDecisionStatusChanged(ignored);
}
void PreviewsUIService::SetResourceLoadingHintsResourcePatternsToBlock(
const GURL& document_gurl,
const std::vector<std::string>& patterns) {
DCHECK(thread_checker_.CalledOnValidThread());
resource_loading_hints_document_gurl_ = document_gurl;
resource_loading_hints_patterns_to_block_ = patterns;
}
std::vector<std::string>
PreviewsUIService::GetResourceLoadingHintsResourcePatternsToBlock(
const GURL& document_gurl) const {
DCHECK(thread_checker_.CalledOnValidThread());
// TODO(tbansal): https://crbug.com/856243. Read patterns from the proto
// optimizations file from the disk, and populate the return value.
if (document_gurl != resource_loading_hints_document_gurl_)
return std::vector<std::string>();
return resource_loading_hints_patterns_to_block_;
}
PreviewsLogger* PreviewsUIService::previews_logger() const {
DCHECK(thread_checker_.CalledOnValidThread());
return logger_.get();
......
......@@ -6,6 +6,7 @@
#define COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_UI_SERVICE_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
......@@ -105,6 +106,23 @@ class PreviewsUIService {
std::vector<PreviewsEligibilityReason>&& passed_reasons,
uint64_t page_id);
// Returns the vector of subresource patterns whose loading should be blocked
// when loading |document_gurl|. The pattern may be a single substring to
// match against the URL or it may be an ordered set of substrings to match
// where the substrings are separated by the ‘*’ wildcard character (with an
// implicit ‘*’ at the beginning and end).
std::vector<std::string> GetResourceLoadingHintsResourcePatternsToBlock(
const GURL& document_gurl) const;
// Sets the vector of subresource patterns whose loading should be blocked
// when loading |document_gurl| to |patterns|. The pattern may be a single
// substring to match against the URL or it may be an ordered set of
// substrings to match where the substrings are separated by the ‘*’ wildcard
// character (with an implicit ‘*’ at the beginning and end).
void SetResourceLoadingHintsResourcePatternsToBlock(
const GURL& document_gurl,
const std::vector<std::string>& patterns);
// Expose the pointer to PreviewsLogger to extract logging messages. This
// pointer's life time is the same as of |this|, and it is guaranteed to not
// return null.
......@@ -119,6 +137,14 @@ class PreviewsUIService {
// The IO thread task runner. Used to post tasks to |previews_decider_impl_|.
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
// |resource_loading_hints_patterns_| are the set of subresource patterns
// whose loading should be blocked. The hints apply to subresources when
// fetching |resource_loading_hints_document_gurl_|.
// TODO(tbansal): https://crbug.com/856243. Consider storing this
// data in a map or an LRU cache.
GURL resource_loading_hints_document_gurl_;
std::vector<std::string> resource_loading_hints_patterns_to_block_;
// A log object to keep track of events such as previews navigations,
// blacklist actions, etc.
std::unique_ptr<PreviewsLogger> logger_;
......
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