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

Disable Search prefetch when JS is disabled

JS is a key factor in understanding when the page loads, so clietns who
do not allow JS to run (overall or on the DSE) should not get prefetch
behavior at all.

Bug: 1151134
Change-Id: I51005f5453d9bb14bd54fe0885c446b0e97581e4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2551625
Commit-Queue: Ryan Sturm <ryansturm@chromium.org>
Reviewed-by: default avatarRobert Ogden <robertogden@chromium.org>
Cr-Commit-Position: refs/heads/master@{#829792}
parent b050a611
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/location.h" #include "base/location.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/net/prediction_options.h" #include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/prefetch/search_prefetch/field_trial_settings.h" #include "chrome/browser/prefetch/search_prefetch/field_trial_settings.h"
#include "chrome/browser/prefetch/search_prefetch/full_body_search_prefetch_request.h" #include "chrome/browser/prefetch/search_prefetch/full_body_search_prefetch_request.h"
...@@ -15,8 +16,12 @@ ...@@ -15,8 +16,12 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/search_engines/ui_thread_search_terms_data.h" #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
#include "chrome/common/pref_names.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/omnibox/browser/autocomplete_controller.h" #include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/base_search_provider.h" #include "components/omnibox/browser/base_search_provider.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "url/origin.h" #include "url/origin.h"
...@@ -58,6 +63,19 @@ bool SearchPrefetchService::MaybePrefetchURL(const GURL& url) { ...@@ -58,6 +63,19 @@ bool SearchPrefetchService::MaybePrefetchURL(const GURL& url) {
return false; return false;
} }
if (!profile_->GetPrefs() ||
!profile_->GetPrefs()->GetBoolean(prefs::kWebKitJavascriptEnabled)) {
return false;
}
auto* content_settings =
HostContentSettingsMapFactory::GetForProfile(profile_);
if (!content_settings ||
content_settings->GetContentSetting(
url, url, ContentSettingsType::JAVASCRIPT) == CONTENT_SETTING_BLOCK) {
return false;
}
auto* template_url_service = auto* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile_); TemplateURLServiceFactory::GetForProfile(profile_);
if (!template_url_service || if (!template_url_service ||
...@@ -137,6 +155,20 @@ SearchPrefetchService::TakePrefetchResponse(const GURL& url) { ...@@ -137,6 +155,20 @@ SearchPrefetchService::TakePrefetchResponse(const GURL& url) {
!template_url_service->GetDefaultSearchProvider()) !template_url_service->GetDefaultSearchProvider())
return nullptr; return nullptr;
// The user may have disabled JS since the prefetch occured.
if (!profile_->GetPrefs() ||
!profile_->GetPrefs()->GetBoolean(prefs::kWebKitJavascriptEnabled)) {
return nullptr;
}
auto* content_settings =
HostContentSettingsMapFactory::GetForProfile(profile_);
if (!content_settings ||
content_settings->GetContentSetting(
url, url, ContentSettingsType::JAVASCRIPT) == CONTENT_SETTING_BLOCK) {
return nullptr;
}
base::string16 search_terms; base::string16 search_terms;
template_url_service->GetDefaultSearchProvider()->ExtractSearchTermsFromURL( template_url_service->GetDefaultSearchProvider()->ExtractSearchTermsFromURL(
url, template_url_service->search_terms_data(), &search_terms); url, template_url_service->search_terms_data(), &search_terms);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/net/prediction_options.h" #include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/prefetch/search_prefetch/field_trial_settings.h" #include "chrome/browser/prefetch/search_prefetch/field_trial_settings.h"
#include "chrome/browser/prefetch/search_prefetch/search_prefetch_service.h" #include "chrome/browser/prefetch/search_prefetch/search_prefetch_service.h"
...@@ -21,6 +22,8 @@ ...@@ -21,6 +22,8 @@
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/search_test_utils.h" #include "chrome/test/base/search_test_utils.h"
#include "chrome/test/base/ui_test_utils.h" #include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/omnibox/browser/autocomplete_input.h" #include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_match.h" #include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_provider.h" #include "components/omnibox/browser/autocomplete_provider.h"
...@@ -1068,6 +1071,113 @@ IN_PROC_BROWSER_TEST_P(SearchPrefetchServiceEnabledBrowserTest, ...@@ -1068,6 +1071,113 @@ IN_PROC_BROWSER_TEST_P(SearchPrefetchServiceEnabledBrowserTest,
EXPECT_TRUE(prefetch_status.has_value()); EXPECT_TRUE(prefetch_status.has_value());
} }
IN_PROC_BROWSER_TEST_P(SearchPrefetchServiceEnabledBrowserTest,
NoPrefetchWhenJSDisabled) {
browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled,
false);
auto* search_prefetch_service =
SearchPrefetchServiceFactory::GetForProfile(browser()->profile());
EXPECT_NE(nullptr, search_prefetch_service);
std::string search_terms = "prefetch_content";
GURL prefetch_url = GetSearchServerQueryURL(search_terms);
EXPECT_FALSE(search_prefetch_service->MaybePrefetchURL(prefetch_url));
}
IN_PROC_BROWSER_TEST_P(SearchPrefetchServiceEnabledBrowserTest,
NoPrefetchWhenJSDisabledOnDSE) {
auto* search_prefetch_service =
SearchPrefetchServiceFactory::GetForProfile(browser()->profile());
EXPECT_NE(nullptr, search_prefetch_service);
std::string search_terms = "prefetch_content";
GURL prefetch_url = GetSearchServerQueryURL(search_terms);
HostContentSettingsMapFactory::GetForProfile(browser()->profile())
->SetContentSettingDefaultScope(prefetch_url, GURL(),
ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_BLOCK);
EXPECT_FALSE(search_prefetch_service->MaybePrefetchURL(prefetch_url));
auto prefetch_status =
search_prefetch_service->GetSearchPrefetchStatusForTesting(
base::ASCIIToUTF16(search_terms));
EXPECT_FALSE(prefetch_status.has_value());
}
IN_PROC_BROWSER_TEST_P(SearchPrefetchServiceEnabledBrowserTest,
NoServeWhenJSDisabled) {
auto* search_prefetch_service =
SearchPrefetchServiceFactory::GetForProfile(browser()->profile());
EXPECT_NE(nullptr, search_prefetch_service);
std::string search_terms = "prefetch_content";
GURL prefetch_url = GetSearchServerQueryURL(search_terms);
EXPECT_TRUE(search_prefetch_service->MaybePrefetchURL(prefetch_url));
auto prefetch_status =
search_prefetch_service->GetSearchPrefetchStatusForTesting(
base::ASCIIToUTF16(search_terms));
ASSERT_TRUE(prefetch_status.has_value());
EXPECT_EQ(SearchPrefetchStatus::kInFlight, prefetch_status.value());
WaitUntilStatusChanges(base::ASCIIToUTF16(search_terms));
prefetch_status = search_prefetch_service->GetSearchPrefetchStatusForTesting(
base::ASCIIToUTF16(search_terms));
ASSERT_TRUE(prefetch_status.has_value());
EXPECT_EQ(SearchPrefetchStatus::kCanBeServed, prefetch_status.value());
browser()->profile()->GetPrefs()->SetBoolean(prefs::kWebKitJavascriptEnabled,
false);
ui_test_utils::NavigateToURL(browser(), prefetch_url);
// The prefetch request and the new non-prefetched served request.
EXPECT_EQ(2u, search_server_request_count());
}
IN_PROC_BROWSER_TEST_P(SearchPrefetchServiceEnabledBrowserTest,
NoServeWhenJSDisabledOnDSE) {
auto* search_prefetch_service =
SearchPrefetchServiceFactory::GetForProfile(browser()->profile());
EXPECT_NE(nullptr, search_prefetch_service);
std::string search_terms = "prefetch_content";
GURL prefetch_url = GetSearchServerQueryURL(search_terms);
EXPECT_TRUE(search_prefetch_service->MaybePrefetchURL(prefetch_url));
auto prefetch_status =
search_prefetch_service->GetSearchPrefetchStatusForTesting(
base::ASCIIToUTF16(search_terms));
ASSERT_TRUE(prefetch_status.has_value());
EXPECT_EQ(SearchPrefetchStatus::kInFlight, prefetch_status.value());
WaitUntilStatusChanges(base::ASCIIToUTF16(search_terms));
prefetch_status = search_prefetch_service->GetSearchPrefetchStatusForTesting(
base::ASCIIToUTF16(search_terms));
ASSERT_TRUE(prefetch_status.has_value());
EXPECT_EQ(SearchPrefetchStatus::kCanBeServed, prefetch_status.value());
HostContentSettingsMapFactory::GetForProfile(browser()->profile())
->SetContentSettingDefaultScope(prefetch_url, GURL(),
ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_BLOCK);
ui_test_utils::NavigateToURL(browser(), prefetch_url);
// The prefetch request and the new non-prefetched served request.
EXPECT_EQ(2u, search_server_request_count());
}
IN_PROC_BROWSER_TEST_P(SearchPrefetchServiceEnabledBrowserTest, IN_PROC_BROWSER_TEST_P(SearchPrefetchServiceEnabledBrowserTest,
OnlyStreamedResponseCanServePartialRequest) { OnlyStreamedResponseCanServePartialRequest) {
set_hang_requests_after_start(true); set_hang_requests_after_start(true);
......
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