Commit 2d08e2c6 authored by Josh Simmons's avatar Josh Simmons Committed by Commit Bot

Updating PerformanceHintsObserver to use the new async

OptimizationGuideDecider API.

Bug: 1030813
Change-Id: I2d0e73e22ec52f1c09e218e50fe11cd737525d48
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2029228Reviewed-by: default avatarMegan Jablonski <megjablon@chromium.org>
Reviewed-by: default avatarRobert Ogden <robertogden@chromium.org>
Commit-Queue: Josh Simmons <jds@google.com>
Cr-Commit-Position: refs/heads/master@{#736959}
parent 6bd84dfa
...@@ -32,10 +32,13 @@ PerformanceHintsObserver::PerformanceHintsObserver( ...@@ -32,10 +32,13 @@ PerformanceHintsObserver::PerformanceHintsObserver(
} }
} }
PerformanceHintsObserver::~PerformanceHintsObserver() = default; PerformanceHintsObserver::~PerformanceHintsObserver() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
base::Optional<PerformanceHint> PerformanceHintsObserver::HintForURL( base::Optional<PerformanceHint> PerformanceHintsObserver::HintForURL(
const GURL& url) const { const GURL& url) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!url.is_valid()) { if (!url.is_valid()) {
return base::nullopt; return base::nullopt;
} }
...@@ -50,6 +53,7 @@ base::Optional<PerformanceHint> PerformanceHintsObserver::HintForURL( ...@@ -50,6 +53,7 @@ base::Optional<PerformanceHint> PerformanceHintsObserver::HintForURL(
void PerformanceHintsObserver::DidFinishNavigation( void PerformanceHintsObserver::DidFinishNavigation(
content::NavigationHandle* navigation_handle) { content::NavigationHandle* navigation_handle) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(navigation_handle); DCHECK(navigation_handle);
if (!navigation_handle->IsInMainFrame() || if (!navigation_handle->IsInMainFrame() ||
navigation_handle->IsSameDocument() || navigation_handle->IsSameDocument() ||
...@@ -70,25 +74,24 @@ void PerformanceHintsObserver::DidFinishNavigation( ...@@ -70,25 +74,24 @@ void PerformanceHintsObserver::DidFinishNavigation(
return; return;
} }
optimization_guide::OptimizationMetadata optimization_metadata; optimization_guide_decider_->CanApplyOptimizationAsync(
OptimizationGuideDecision decision = navigation_handle, optimization_guide::proto::PERFORMANCE_HINTS,
optimization_guide_decider_->CanApplyOptimization( base::BindOnce(&PerformanceHintsObserver::ProcessPerformanceHint,
navigation_handle, optimization_guide::proto::PERFORMANCE_HINTS, weak_factory_.GetWeakPtr()));
&optimization_metadata); }
void PerformanceHintsObserver::ProcessPerformanceHint(
OptimizationGuideDecision decision,
const optimization_guide::OptimizationMetadata& optimization_metadata) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (decision != OptimizationGuideDecision::kTrue) { if (decision != OptimizationGuideDecision::kTrue) {
// Apply results are counted under // Apply results are counted under
// OptimizationGuide.ApplyDecision.PerformanceHints. // OptimizationGuide.ApplyDecision.PerformanceHints.
return; return;
} }
if (optimization_metadata.performance_hints_metadata.performance_hints() for (const PerformanceHint& hint :
.size() <= 0) { optimization_metadata.performance_hints_metadata.performance_hints()) {
return;
}
for (PerformanceHint& hint : *optimization_metadata.performance_hints_metadata
.mutable_performance_hints()) {
hints_.emplace_back(URLPatternWithWildcards(hint.wildcard_pattern()), hint); hints_.emplace_back(URLPatternWithWildcards(hint.wildcard_pattern()), hint);
} }
} }
......
...@@ -10,7 +10,10 @@ ...@@ -10,7 +10,10 @@
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/sequence_checker.h"
#include "components/optimization_guide/optimization_guide_decider.h"
#include "components/optimization_guide/proto/hints.pb.h" #include "components/optimization_guide/proto/hints.pb.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h" #include "content/public/browser/web_contents_user_data.h"
...@@ -21,7 +24,6 @@ class WebContents; ...@@ -21,7 +24,6 @@ class WebContents;
} // namespace content } // namespace content
namespace optimization_guide { namespace optimization_guide {
class OptimizationGuideDecider;
class URLPatternWithWildcards; class URLPatternWithWildcards;
namespace proto { namespace proto {
class PerformanceHint; class PerformanceHint;
...@@ -44,6 +46,13 @@ class PerformanceHintsObserver ...@@ -44,6 +46,13 @@ class PerformanceHintsObserver
PerformanceHintsObserver(const PerformanceHintsObserver&) = delete; PerformanceHintsObserver(const PerformanceHintsObserver&) = delete;
PerformanceHintsObserver& operator=(const PerformanceHintsObserver&) = delete; PerformanceHintsObserver& operator=(const PerformanceHintsObserver&) = delete;
// This callback populates |hints_| with performance information for links on
// the current page and is called by |optimization_guide_decider_| when a
// definite decision has been reached.
void ProcessPerformanceHint(
optimization_guide::OptimizationGuideDecision decision,
const optimization_guide::OptimizationMetadata& optimization_metadata);
// Returns a PerformanceHint for a link to |url|, if one exists. // Returns a PerformanceHint for a link to |url|, if one exists.
base::Optional<optimization_guide::proto::PerformanceHint> HintForURL( base::Optional<optimization_guide::proto::PerformanceHint> HintForURL(
const GURL& url) const; const GURL& url) const;
...@@ -66,6 +75,10 @@ class PerformanceHintsObserver ...@@ -66,6 +75,10 @@ class PerformanceHintsObserver
optimization_guide::proto::PerformanceHint>> optimization_guide::proto::PerformanceHint>>
hints_; hints_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<PerformanceHintsObserver> weak_factory_{this};
WEB_CONTENTS_USER_DATA_KEY_DECL(); WEB_CONTENTS_USER_DATA_KEY_DECL();
}; };
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.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/optimization_guide/optimization_guide_keyed_service_factory.h"
...@@ -48,6 +49,10 @@ class MockOptimizationGuideKeyedService : public OptimizationGuideKeyedService { ...@@ -48,6 +49,10 @@ class MockOptimizationGuideKeyedService : public OptimizationGuideKeyedService {
content::NavigationHandle*, content::NavigationHandle*,
optimization_guide::proto::OptimizationType, optimization_guide::proto::OptimizationType,
optimization_guide::OptimizationMetadata*)); optimization_guide::OptimizationMetadata*));
MOCK_METHOD3(CanApplyOptimizationAsync,
void(content::NavigationHandle*,
optimization_guide::proto::OptimizationType,
optimization_guide::OptimizationGuideDecisionCallback));
}; };
class PerformanceHintsObserverTest : public ChromeRenderViewHostTestHarness { class PerformanceHintsObserverTest : public ChromeRenderViewHostTestHarness {
...@@ -107,22 +112,20 @@ TEST_F(PerformanceHintsObserverTest, HasPerformanceHints) { ...@@ -107,22 +112,20 @@ TEST_F(PerformanceHintsObserverTest, HasPerformanceHints) {
optimization_guide::proto::PERFORMANCE_HINTS), optimization_guide::proto::PERFORMANCE_HINTS),
testing::IsEmpty())); testing::IsEmpty()));
optimization_guide::OptimizationMetadata output; optimization_guide::OptimizationMetadata metadata;
auto* hint = output.performance_hints_metadata.add_performance_hints(); auto* hint = metadata.performance_hints_metadata.add_performance_hints();
hint->set_wildcard_pattern("test.com"); hint->set_wildcard_pattern("test.com");
hint->set_performance_class(optimization_guide::proto::PERFORMANCE_SLOW); hint->set_performance_class(optimization_guide::proto::PERFORMANCE_SLOW);
hint = output.performance_hints_metadata.add_performance_hints(); hint = metadata.performance_hints_metadata.add_performance_hints();
hint->set_wildcard_pattern("othersite.net"); hint->set_wildcard_pattern("othersite.net");
hint->set_performance_class(optimization_guide::proto::PERFORMANCE_FAST); hint->set_performance_class(optimization_guide::proto::PERFORMANCE_FAST);
EXPECT_CALL(*mock_optimization_guide_keyed_service_, EXPECT_CALL(*mock_optimization_guide_keyed_service_,
CanApplyOptimization(testing::_, CanApplyOptimizationAsync(
optimization_guide::proto::PERFORMANCE_HINTS, testing::_, optimization_guide::proto::PERFORMANCE_HINTS,
testing::NotNull())) base::test::IsNotNullCallback()))
.WillOnce(testing::WithArgs<2>(testing::Invoke( .WillOnce(base::test::RunOnceCallback<2>(
[&output](optimization_guide::OptimizationMetadata* metadata) { optimization_guide::OptimizationGuideDecision::kTrue,
*metadata = output; testing::ByRef(metadata)));
return optimization_guide::OptimizationGuideDecision::kTrue;
})));
PerformanceHintsObserver::CreateForWebContents(web_contents()); PerformanceHintsObserver::CreateForWebContents(web_contents());
PerformanceHintsObserver* observer = PerformanceHintsObserver* observer =
...@@ -148,12 +151,31 @@ TEST_F(PerformanceHintsObserverTest, HasPerformanceHints) { ...@@ -148,12 +151,31 @@ TEST_F(PerformanceHintsObserverTest, HasPerformanceHints) {
} }
TEST_F(PerformanceHintsObserverTest, NoHintsForPage) { TEST_F(PerformanceHintsObserverTest, NoHintsForPage) {
optimization_guide::OptimizationMetadata metadata;
EXPECT_CALL(*mock_optimization_guide_keyed_service_, EXPECT_CALL(*mock_optimization_guide_keyed_service_,
CanApplyOptimization(testing::_, CanApplyOptimizationAsync(
optimization_guide::proto::PERFORMANCE_HINTS, testing::_, optimization_guide::proto::PERFORMANCE_HINTS,
testing::NotNull())) base::test::IsNotNullCallback()))
.WillOnce(testing::Return( .WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kFalse)); optimization_guide::OptimizationGuideDecision::kFalse,
testing::ByRef(metadata)));
PerformanceHintsObserver::CreateForWebContents(web_contents());
PerformanceHintsObserver* observer =
PerformanceHintsObserver::FromWebContents(web_contents());
ASSERT_NE(observer, nullptr);
CallDidFinishNavigation(observer);
EXPECT_THAT(observer->HintForURL(GURL("https://www.nohint.com")),
Eq(base::nullopt));
}
TEST_F(PerformanceHintsObserverTest, PerformanceInfoRequestedBeforeCallback) {
optimization_guide::OptimizationMetadata metadata;
EXPECT_CALL(*mock_optimization_guide_keyed_service_,
CanApplyOptimizationAsync(
testing::_, optimization_guide::proto::PERFORMANCE_HINTS,
base::test::IsNotNullCallback()));
PerformanceHintsObserver::CreateForWebContents(web_contents()); PerformanceHintsObserver::CreateForWebContents(web_contents());
PerformanceHintsObserver* observer = PerformanceHintsObserver* observer =
...@@ -184,7 +206,7 @@ TEST_F(PerformanceHintsObserverTest, NoErrorPageHints) { ...@@ -184,7 +206,7 @@ TEST_F(PerformanceHintsObserverTest, NoErrorPageHints) {
test_handle_->set_is_error_page(true); test_handle_->set_is_error_page(true);
EXPECT_CALL(*mock_optimization_guide_keyed_service_, EXPECT_CALL(*mock_optimization_guide_keyed_service_,
CanApplyOptimization(testing::_, testing::_, testing::_)) CanApplyOptimizationAsync(testing::_, testing::_, testing::_))
.Times(0); .Times(0);
PerformanceHintsObserver::CreateForWebContents(web_contents()); PerformanceHintsObserver::CreateForWebContents(web_contents());
...@@ -209,7 +231,7 @@ TEST_F(PerformanceHintsObserverTest, DontFetchForSubframe) { ...@@ -209,7 +231,7 @@ TEST_F(PerformanceHintsObserverTest, DontFetchForSubframe) {
test_handle_->set_is_error_page(false); test_handle_->set_is_error_page(false);
EXPECT_CALL(*mock_optimization_guide_keyed_service_, EXPECT_CALL(*mock_optimization_guide_keyed_service_,
CanApplyOptimization(testing::_, testing::_, testing::_)) CanApplyOptimizationAsync(testing::_, testing::_, testing::_))
.Times(0); .Times(0);
PerformanceHintsObserver::CreateForWebContents(web_contents()); PerformanceHintsObserver::CreateForWebContents(web_contents());
......
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