Commit c216f2bc authored by Sophie Chang's avatar Sophie Chang Committed by Chromium LUCI CQ

Do not register performance hints opt types with optguide if in incognito

Now that optimization guide works in incognito, we want to make sure
that Performance Hints doesn't actually attempt to work if in incognito

Bug: 1151087
Change-Id: I83e343762912397a52a305999bb62ae519ec7edb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2601642
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Reviewed-by: default avatarJosh Simmons <jds@google.com>
Cr-Commit-Position: refs/heads/master@{#839204}
parent 87902394
...@@ -105,9 +105,19 @@ JNI_PerformanceHintsObserver_IsContextMenuPerformanceInfoEnabled(JNIEnv* env) { ...@@ -105,9 +105,19 @@ JNI_PerformanceHintsObserver_IsContextMenuPerformanceInfoEnabled(JNIEnv* env) {
PerformanceHintsObserver::PerformanceHintsObserver( PerformanceHintsObserver::PerformanceHintsObserver(
content::WebContents* web_contents) content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) { : content::WebContentsObserver(web_contents) {
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
// Given that we throw things away if the user is unable to fetch from
// the remote Optimization Guide, do not instantiate anything else since
// we will throw everything away anyway. Although FAST_HOST_HINTS and some
// hints that were fetched for the original profile are available in
// incognito, these do not provide sufficient coverage.
if (profile->IsOffTheRecord())
return;
optimization_guide_decider_ = optimization_guide_decider_ =
OptimizationGuideKeyedServiceFactory::GetForProfile( OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
Profile::FromBrowserContext(web_contents->GetBrowserContext()));
std::vector<optimization_guide::proto::OptimizationType> opts; std::vector<optimization_guide::proto::OptimizationType> opts;
opts.push_back(optimization_guide::proto::PERFORMANCE_HINTS); opts.push_back(optimization_guide::proto::PERFORMANCE_HINTS);
if (features::AreFastHostHintsEnabled()) { if (features::AreFastHostHintsEnabled()) {
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "components/optimization_guide/optimization_guide_switches.h" #include "components/optimization_guide/optimization_guide_switches.h"
#include "content/public/browser/navigation_handle.h" #include "content/public/browser/navigation_handle.h"
#include "content/public/test/mock_navigation_handle.h" #include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
using optimization_guide::OptimizationGuideDecision; using optimization_guide::OptimizationGuideDecision;
...@@ -90,6 +91,17 @@ class PerformanceHintsObserverTest : public ChromeRenderViewHostTestHarness { ...@@ -90,6 +91,17 @@ class PerformanceHintsObserverTest : public ChromeRenderViewHostTestHarness {
MockOptimizationGuideKeyedService>(context); MockOptimizationGuideKeyedService>(context);
}))); })));
mock_otr_optimization_guide_keyed_service_ =
static_cast<MockOptimizationGuideKeyedService*>(
OptimizationGuideKeyedServiceFactory::GetInstance()
->SetTestingFactoryAndUse(
profile()->GetPrimaryOTRProfile(),
base::BindRepeating([](content::BrowserContext* context)
-> std::unique_ptr<KeyedService> {
return std::make_unique<
MockOptimizationGuideKeyedService>(context);
})));
// By default, all sources will return no hints. // By default, all sources will return no hints.
ON_CALL(*mock_optimization_guide_keyed_service_, ON_CALL(*mock_optimization_guide_keyed_service_,
CanApplyOptimization( CanApplyOptimization(
...@@ -127,10 +139,26 @@ class PerformanceHintsObserverTest : public ChromeRenderViewHostTestHarness { ...@@ -127,10 +139,26 @@ class PerformanceHintsObserverTest : public ChromeRenderViewHostTestHarness {
std::unique_ptr<content::MockNavigationHandle> test_handle_; std::unique_ptr<content::MockNavigationHandle> test_handle_;
MockOptimizationGuideKeyedService* mock_optimization_guide_keyed_service_ = MockOptimizationGuideKeyedService* mock_optimization_guide_keyed_service_ =
nullptr; nullptr;
MockOptimizationGuideKeyedService*
mock_otr_optimization_guide_keyed_service_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(PerformanceHintsObserverTest); DISALLOW_COPY_AND_ASSIGN(PerformanceHintsObserverTest);
}; };
TEST_F(PerformanceHintsObserverTest, IncognitoDoesNotRegisterPerformanceHints) {
std::unique_ptr<content::WebContents> incognito_web_contents(
content::WebContentsTester::CreateTestWebContents(
profile()->GetPrimaryOTRProfile(), nullptr));
EXPECT_CALL(*mock_otr_optimization_guide_keyed_service_,
RegisterOptimizationTypes(testing::UnorderedElementsAre(
optimization_guide::proto::PERFORMANCE_HINTS,
optimization_guide::proto::FAST_HOST_HINTS)))
.Times(0);
PerformanceHintsObserver::CreateForWebContents(incognito_web_contents.get());
}
TEST_F(PerformanceHintsObserverTest, RegisterPerformanceHints) { TEST_F(PerformanceHintsObserverTest, RegisterPerformanceHints) {
EXPECT_CALL(*mock_optimization_guide_keyed_service_, EXPECT_CALL(*mock_optimization_guide_keyed_service_,
RegisterOptimizationTypes(testing::UnorderedElementsAre( RegisterOptimizationTypes(testing::UnorderedElementsAre(
......
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