Commit 2ba21bcd authored by Conley Owens's avatar Conley Owens Committed by Commit Bot

Replace the NTP Interceptor with Throttle

This change replaces the NewTabPageInterceptorService with a
NewTabPageNavigationThrottle.  The throttle reduces complexity, but it
is also neutral regarding the network stack / network service, so it
fixes the NewTabPageInterceptorServiceTests that were broken with the
NetworkService feature enabled.  These tests are now renamed
NewTabPageNavigationThrottleTest.

BUG=802926

Cq-Include-Trybots: master.tryserver.chromium.linux:linux_mojo
Change-Id: Iec0664df33e79d6bcc18f60aecc4017de7f65858
Reviewed-on: https://chromium-review.googlesource.com/868994
Commit-Queue: Conley Owens <cco3@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#530370}
parent bc084a8b
...@@ -318,6 +318,7 @@ ...@@ -318,6 +318,7 @@
#include "chrome/browser/payments/payment_request_factory.h" #include "chrome/browser/payments/payment_request_factory.h"
#include "chrome/browser/search/instant_service.h" #include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search/instant_service_factory.h" #include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/ui/search/new_tab_page_navigation_throttle.h"
#include "chrome/common/importer/profile_import.mojom.h" #include "chrome/common/importer/profile_import.mojom.h"
#endif #endif
...@@ -3503,6 +3504,11 @@ ChromeContentBrowserClient::CreateThrottlesForNavigation( ...@@ -3503,6 +3504,11 @@ ChromeContentBrowserClient::CreateThrottlesForNavigation(
DevToolsWindow::MaybeCreateNavigationThrottle(handle); DevToolsWindow::MaybeCreateNavigationThrottle(handle);
if (devtools_throttle) if (devtools_throttle)
throttles.push_back(std::move(devtools_throttle)); throttles.push_back(std::move(devtools_throttle));
std::unique_ptr<content::NavigationThrottle> new_tab_page_throttle =
NewTabPageNavigationThrottle::MaybeCreateThrottleFor(handle);
if (new_tab_page_throttle)
throttles.push_back(std::move(new_tab_page_throttle));
#endif #endif
return throttles; return throttles;
......
...@@ -127,9 +127,6 @@ ...@@ -127,9 +127,6 @@
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
#include "content/public/browser/android/content_protocol_handler.h" #include "content/public/browser/android/content_protocol_handler.h"
#else
#include "chrome/browser/ui/search/new_tab_page_interceptor_service.h"
#include "chrome/browser/ui/search/new_tab_page_interceptor_service_factory.h"
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -453,15 +450,6 @@ void ProfileIOData::InitializeOnUIThread(Profile* profile) { ...@@ -453,15 +450,6 @@ void ProfileIOData::InitializeOnUIThread(Profile* profile) {
params->protocol_handler_interceptor = params->protocol_handler_interceptor =
protocol_handler_registry->CreateJobInterceptorFactory(); protocol_handler_registry->CreateJobInterceptorFactory();
#if !defined(OS_ANDROID)
NewTabPageInterceptorService* new_tab_interceptor_service =
NewTabPageInterceptorServiceFactory::GetForProfile(profile);
if (new_tab_interceptor_service) {
params->new_tab_page_interceptor =
new_tab_interceptor_service->CreateInterceptor();
}
#endif
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
// Enable client certificates for the Chrome OS sign-in frame, if this feature // Enable client certificates for the Chrome OS sign-in frame, if this feature
// is not disabled by a flag. // is not disabled by a flag.
......
...@@ -855,10 +855,8 @@ split_static_library("ui") { ...@@ -855,10 +855,8 @@ split_static_library("ui") {
"scoped_tabbed_browser_displayer.h", "scoped_tabbed_browser_displayer.h",
"search/instant_controller.cc", "search/instant_controller.cc",
"search/instant_controller.h", "search/instant_controller.h",
"search/new_tab_page_interceptor_service.cc", "search/new_tab_page_navigation_throttle.cc",
"search/new_tab_page_interceptor_service.h", "search/new_tab_page_navigation_throttle.h",
"search/new_tab_page_interceptor_service_factory.cc",
"search/new_tab_page_interceptor_service_factory.h",
"search/ntp_user_data_logger.cc", "search/ntp_user_data_logger.cc",
"search/ntp_user_data_logger.h", "search/ntp_user_data_logger.h",
"search/search_ipc_router.cc", "search/search_ipc_router.cc",
......
// Copyright 2015 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 <memory>
#include "base/files/file_path.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/search/search.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/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/search_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/url_request/url_request_mock_http_job.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using content::BrowserThread;
class NewTabPageInterceptorTest : public InProcessBrowserTest {
public:
NewTabPageInterceptorTest() {}
void SetUpOnMainThread() override {
base::FilePath path =
ui_test_utils::GetTestFilePath(base::FilePath(), base::FilePath());
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&net::URLRequestMockHTTPJob::AddUrlHandlers, path));
}
void ChangeDefaultSearchProvider(const char* new_tab_path) {
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(browser()->profile());
search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
UIThreadSearchTermsData::SetGoogleBaseURL("https://mock.http/");
std::string base_url("{google:baseURL}");
TemplateURLData data;
data.SetShortName(base::ASCIIToUTF16("Google"));
data.SetKeyword(base::UTF8ToUTF16(base_url));
data.SetURL(base_url + "url?bar={searchTerms}");
data.new_tab_url = base_url + new_tab_path;
TemplateURL* template_url =
template_url_service->Add(std::make_unique<TemplateURL>(data));
template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
}
};
IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, NoInterception) {
net::EmbeddedTestServer https_test_server(
net::EmbeddedTestServer::TYPE_HTTPS);
ASSERT_TRUE(https_test_server.Start());
GURL new_tab_url = https_test_server.GetURL("/instant_extended.html");
ChangeDefaultSearchProvider("instant_extended.html");
ui_test_utils::NavigateToURL(browser(), new_tab_url);
content::WebContents* contents =
browser()->tab_strip_model()->GetWebContentsAt(0);
// A correct, 200-OK file works correctly.
EXPECT_EQ(new_tab_url,
contents->GetController().GetLastCommittedEntry()->GetURL());
}
IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 404Interception) {
GURL new_tab_url =
net::URLRequestMockHTTPJob::GetMockHttpsUrl("page404.html");
ChangeDefaultSearchProvider("page404.html");
ui_test_utils::NavigateToURL(browser(), new_tab_url);
content::WebContents* contents =
browser()->tab_strip_model()->GetWebContentsAt(0);
// 404 makes a redirect to the local NTP.
EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl),
contents->GetController().GetLastCommittedEntry()->GetURL());
}
IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, 204Interception) {
GURL new_tab_url =
net::URLRequestMockHTTPJob::GetMockHttpsUrl("page204.html");
ChangeDefaultSearchProvider("page204.html");
ui_test_utils::NavigateToURL(browser(), new_tab_url);
content::WebContents* contents =
browser()->tab_strip_model()->GetWebContentsAt(0);
// 204 makes a redirect to the local NTP.
EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl),
contents->GetController().GetLastCommittedEntry()->GetURL());
}
IN_PROC_BROWSER_TEST_F(NewTabPageInterceptorTest, FailedRequestInterception) {
GURL new_tab_url =
net::URLRequestMockHTTPJob::GetMockHttpsUrl("notarealfile.html");
ChangeDefaultSearchProvider("notarealfile.html");
ui_test_utils::NavigateToURL(browser(), new_tab_url);
content::WebContents* contents =
browser()->tab_strip_model()->GetWebContentsAt(0);
// Failed navigation makes a redirect to the local NTP.
EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl),
contents->GetController().GetLastCommittedEntry()->GetURL());
}
// Copyright 2015 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/ui/search/new_tab_page_interceptor_service.h"
#include <utility>
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/common/url_constants.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/net_errors.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_interceptor.h"
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_redirect_job.h"
#include "url/gurl.h"
// Implementation of the URLRequestInterceptor for the New Tab Page. Will look
// at incoming response from the server and possibly divert to the local NTP.
class NewTabPageInterceptor : public net::URLRequestInterceptor {
public:
explicit NewTabPageInterceptor(const GURL& new_tab_url)
: new_tab_url_(new_tab_url), weak_factory_(this) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}
~NewTabPageInterceptor() override {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
}
base::WeakPtr<NewTabPageInterceptor> GetWeakPtr() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return weak_factory_.GetWeakPtr();
}
void SetNewTabPageURL(const GURL& new_tab_page_url) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
new_tab_url_ = new_tab_page_url;
}
private:
// Overrides from net::URLRequestInterceptor:
net::URLRequestJob* MaybeInterceptRequest(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
return nullptr;
}
net::URLRequestJob* MaybeInterceptResponse(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if ((request->url() != new_tab_url_) ||
(new_tab_url_ == chrome::kChromeSearchLocalNtpUrl)) {
return nullptr;
}
// User has canceled this navigation so it shouldn't be redirected.
// TODO(maksims): Remove request->status() and use int net_error
// once MaybeInterceptResponse() starts to pass that.
if (request->status().status() == net::URLRequestStatus::CANCELED ||
(request->status().status() == net::URLRequestStatus::FAILED &&
request->status().error() == net::ERR_ABORTED)) {
return nullptr;
}
// Request to NTP was successful.
// TODO(maksims): Remove request->status() and use int net_error
// once MaybeInterceptResponse() starts to pass that.
if (request->status().is_success() &&
request->GetResponseCode() != net::HTTP_NO_CONTENT &&
request->GetResponseCode() < 400) {
return nullptr;
}
// Failure to load the NTP correctly; redirect to Local NTP.
UMA_HISTOGRAM_ENUMERATION("InstantExtended.CacheableNTPLoad",
search::CACHEABLE_NTP_LOAD_FAILED,
search::CACHEABLE_NTP_LOAD_MAX);
return new net::URLRequestRedirectJob(
request, network_delegate, GURL(chrome::kChromeSearchLocalNtpUrl),
net::URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT,
"NTP Request Interceptor");
}
GURL new_tab_url_;
base::WeakPtrFactory<NewTabPageInterceptor> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(NewTabPageInterceptor);
};
NewTabPageInterceptorService::NewTabPageInterceptorService(Profile* profile)
: profile_(profile),
template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)) {
DCHECK(profile_);
if (template_url_service_)
template_url_service_->AddObserver(this);
}
NewTabPageInterceptorService::~NewTabPageInterceptorService() {
if (template_url_service_)
template_url_service_->RemoveObserver(this);
}
void NewTabPageInterceptorService::OnTemplateURLServiceChanged() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
GURL new_tab_page_url(search::GetNewTabPageURL(profile_));
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&NewTabPageInterceptor::SetNewTabPageURL, interceptor_,
new_tab_page_url));
}
std::unique_ptr<net::URLRequestInterceptor>
NewTabPageInterceptorService::CreateInterceptor() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
std::unique_ptr<NewTabPageInterceptor> interceptor(
new NewTabPageInterceptor(search::GetNewTabPageURL(profile_)));
interceptor_ = interceptor->GetWeakPtr();
return std::move(interceptor);
}
// Copyright 2015 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_UI_SEARCH_NEW_TAB_PAGE_INTERCEPTOR_SERVICE_H_
#define CHROME_BROWSER_UI_SEARCH_NEW_TAB_PAGE_INTERCEPTOR_SERVICE_H_
#include <memory>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/search_engines/template_url_service_observer.h"
class NewTabPageInterceptor;
class Profile;
class TemplateURLService;
namespace net {
class URLRequestInterceptor;
}
// Owns a NewTabPageInterceptor.
class NewTabPageInterceptorService : public KeyedService,
public TemplateURLServiceObserver {
public:
explicit NewTabPageInterceptorService(Profile* profile);
~NewTabPageInterceptorService() override;
// TemplateURLServiceObserver override.
void OnTemplateURLServiceChanged() override;
std::unique_ptr<net::URLRequestInterceptor> CreateInterceptor();
private:
Profile* profile_;
base::WeakPtr<NewTabPageInterceptor> interceptor_;
// The TemplateURLService that we are observing. It will outlive this
// NewTabPageInterceptorService due to the dependency declared in
// NewTabPageInterceptorServiceFactory.
TemplateURLService* template_url_service_;
DISALLOW_COPY_AND_ASSIGN(NewTabPageInterceptorService);
};
#endif // CHROME_BROWSER_UI_SEARCH_NEW_TAB_PAGE_INTERCEPTOR_SERVICE_H_
// Copyright 2015 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/ui/search/new_tab_page_interceptor_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/search/new_tab_page_interceptor_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_thread.h"
// static
NewTabPageInterceptorServiceFactory*
NewTabPageInterceptorServiceFactory::GetInstance() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return base::Singleton<NewTabPageInterceptorServiceFactory>::get();
}
// static
NewTabPageInterceptorService*
NewTabPageInterceptorServiceFactory::GetForProfile(Profile* profile) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (profile->IsOffTheRecord())
return nullptr;
return static_cast<NewTabPageInterceptorService*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}
NewTabPageInterceptorServiceFactory::NewTabPageInterceptorServiceFactory()
: BrowserContextKeyedServiceFactory(
"NTP Request Interceptor Service Factory",
BrowserContextDependencyManager::GetInstance()) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DependsOn(TemplateURLServiceFactory::GetInstance());
}
NewTabPageInterceptorServiceFactory::~NewTabPageInterceptorServiceFactory() {
}
KeyedService* NewTabPageInterceptorServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Profile* profile = Profile::FromBrowserContext(context);
return new NewTabPageInterceptorService(profile);
}
// Copyright 2015 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_UI_SEARCH_NEW_TAB_PAGE_INTERCEPTOR_SERVICE_FACTORY_H_
#define CHROME_BROWSER_UI_SEARCH_NEW_TAB_PAGE_INTERCEPTOR_SERVICE_FACTORY_H_
#include "base/macros.h"
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
class NewTabPageInterceptorService;
class Profile;
namespace content {
class BrowserContext;
}
// Owns and creates NewTabPageInterceptorService instances.
class NewTabPageInterceptorServiceFactory
: public BrowserContextKeyedServiceFactory {
public:
static NewTabPageInterceptorService* GetForProfile(Profile* profile);
static NewTabPageInterceptorServiceFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<
NewTabPageInterceptorServiceFactory>;
NewTabPageInterceptorServiceFactory();
~NewTabPageInterceptorServiceFactory() override;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
DISALLOW_COPY_AND_ASSIGN(NewTabPageInterceptorServiceFactory);
};
#endif // CHROME_BROWSER_UI_SEARCH_NEW_TAB_PAGE_INTERCEPTOR_SERVICE_FACTORY_H_
// Copyright 2018 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/ui/search/new_tab_page_navigation_throttle.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "net/http/http_status_code.h"
#include "url/gurl.h"
NewTabPageNavigationThrottle::NewTabPageNavigationThrottle(
content::NavigationHandle* navigation_handle)
: content::NavigationThrottle(navigation_handle) {}
NewTabPageNavigationThrottle::~NewTabPageNavigationThrottle() = default;
const char* NewTabPageNavigationThrottle::GetNameForLogging() {
return "NewTabPageNavigationThrottle";
}
// static
std::unique_ptr<content::NavigationThrottle>
NewTabPageNavigationThrottle::MaybeCreateThrottleFor(
content::NavigationHandle* handle) {
content::WebContents* web_contents = handle->GetWebContents();
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
if (web_contents->GetVisibleURL() != chrome::kChromeUINewTabURL ||
!search::IsInstantNTPURL(handle->GetURL(), profile) ||
handle->GetURL() == chrome::kChromeSearchLocalNtpUrl) {
return nullptr;
}
return std::make_unique<NewTabPageNavigationThrottle>(handle);
}
content::NavigationThrottle::ThrottleCheckResult
NewTabPageNavigationThrottle::WillProcessResponse() {
const net::HttpResponseHeaders* headers =
navigation_handle()->GetResponseHeaders();
if (!headers)
return content::NavigationThrottle::PROCEED;
int response_code = headers->response_code();
if (response_code < 400 && response_code != net::HTTP_NO_CONTENT)
return content::NavigationThrottle::PROCEED;
return OpenLocalNewTabPage();
}
content::NavigationThrottle::ThrottleCheckResult
NewTabPageNavigationThrottle::WillFailRequest() {
return OpenLocalNewTabPage();
}
content::NavigationThrottle::ThrottleCheckResult
NewTabPageNavigationThrottle::OpenLocalNewTabPage() {
UMA_HISTOGRAM_ENUMERATION("InstantExtended.CacheableNTPLoad",
search::CACHEABLE_NTP_LOAD_FAILED,
search::CACHEABLE_NTP_LOAD_MAX);
navigation_handle()->GetWebContents()->OpenURL(
content::OpenURLParams(GURL(chrome::kChromeSearchLocalNtpUrl),
navigation_handle()->GetReferrer(),
navigation_handle()->GetFrameTreeNodeId(),
WindowOpenDisposition::CURRENT_TAB,
navigation_handle()->GetPageTransition(),
false /* is_renderer_initiated */));
return content::NavigationThrottle::CANCEL_AND_IGNORE;
}
// Copyright 2018 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_UI_SEARCH_NEW_TAB_PAGE_NAVIGATION_THROTTLE_H_
#define CHROME_BROWSER_UI_SEARCH_NEW_TAB_PAGE_NAVIGATION_THROTTLE_H_
#include <memory>
#include "content/public/browser/navigation_throttle.h"
namespace content {
class NavigationHandle;
} // namespace content
// A NavigationThrottle that opens the local New Tab Page when there is any
// issue opening the remote New Tab Page.
class NewTabPageNavigationThrottle : public content::NavigationThrottle {
public:
// Returns a NavigationThrottle when:
// - we are navigating to the new tab page, and
// - the main frame is pointed at the new tab URL.
static std::unique_ptr<content::NavigationThrottle> MaybeCreateThrottleFor(
content::NavigationHandle* handle);
explicit NewTabPageNavigationThrottle(content::NavigationHandle* handle);
~NewTabPageNavigationThrottle() override;
// content::NavigationThrottle:
ThrottleCheckResult WillFailRequest() override;
ThrottleCheckResult WillProcessResponse() override;
const char* GetNameForLogging() override;
private:
ThrottleCheckResult OpenLocalNewTabPage();
};
#endif // CHROME_BROWSER_UI_SEARCH_NEW_TAB_PAGE_NAVIGATION_THROTTLE_H_
// Copyright 2018 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/search/search.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/search/local_ntp_test_utils.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/search_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/gurl.h"
namespace {
class NewTabPageNavigationThrottleTest : public InProcessBrowserTest {
public:
NewTabPageNavigationThrottleTest()
: https_test_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
https_test_server()->AddDefaultHandlers(
base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
}
void SetNewTabPage(const std::string& ntp_url) {
// Set the new tab page.
local_ntp_test_utils::SetUserSelectedDefaultSearchProvider(
browser()->profile(), https_test_server()->base_url().spec(), ntp_url);
// Ensure we are using the newly set new_tab_url and won't be directed
// to the local new tab page.
TemplateURLService* service =
TemplateURLServiceFactory::GetForProfile(browser()->profile());
search_test_utils::WaitForTemplateURLServiceToLoad(service);
ASSERT_EQ(search::GetNewTabPageURL(browser()->profile()), ntp_url);
}
// Navigates to the New Tab Page and then returns the GURL that ultimately was
// navigated to.
GURL NavigateToNewTabPage() {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
content::WebContents* contents =
browser()->tab_strip_model()->GetWebContentsAt(0);
return contents->GetController().GetLastCommittedEntry()->GetURL();
}
net::EmbeddedTestServer* https_test_server() { return &https_test_server_; }
net::EmbeddedTestServer https_test_server_;
};
IN_PROC_BROWSER_TEST_F(NewTabPageNavigationThrottleTest, NoThrottle) {
ASSERT_TRUE(https_test_server()->Start());
std::string ntp_url =
https_test_server()->GetURL("/instant_extended.html").spec();
SetNewTabPage(ntp_url);
// A correct, 200-OK file works correctly.
EXPECT_EQ(ntp_url, NavigateToNewTabPage());
}
IN_PROC_BROWSER_TEST_F(NewTabPageNavigationThrottleTest,
FailedRequestThrottle) {
ASSERT_TRUE(https_test_server()->Start());
SetNewTabPage(https_test_server()->GetURL("/instant_extended.html").spec());
ASSERT_TRUE(https_test_server()->ShutdownAndWaitUntilComplete());
// Failed navigation makes a redirect to the local NTP.
EXPECT_EQ(chrome::kChromeSearchLocalNtpUrl, NavigateToNewTabPage());
}
IN_PROC_BROWSER_TEST_F(NewTabPageNavigationThrottleTest, LocalNewTabPage) {
ASSERT_TRUE(https_test_server()->Start());
SetNewTabPage(chrome::kChromeSearchLocalNtpUrl);
// Already going to the local NTP, so we should arrive there as expected.
EXPECT_EQ(chrome::kChromeSearchLocalNtpUrl, NavigateToNewTabPage());
}
IN_PROC_BROWSER_TEST_F(NewTabPageNavigationThrottleTest, 404Throttle) {
ASSERT_TRUE(https_test_server()->Start());
SetNewTabPage(https_test_server()->GetURL("/page404.html").spec());
// 404 makes a redirect to the local NTP.
EXPECT_EQ(chrome::kChromeSearchLocalNtpUrl, NavigateToNewTabPage());
}
IN_PROC_BROWSER_TEST_F(NewTabPageNavigationThrottleTest, 204Throttle) {
ASSERT_TRUE(https_test_server()->Start());
SetNewTabPage(https_test_server()->GetURL("/page204.html").spec());
// 204 makes a redirect to the local NTP.
EXPECT_EQ(chrome::kChromeSearchLocalNtpUrl, NavigateToNewTabPage());
}
} // namespace
...@@ -784,7 +784,7 @@ test("browser_tests") { ...@@ -784,7 +784,7 @@ test("browser_tests") {
"../browser/ui/search/local_ntp_test_utils.cc", "../browser/ui/search/local_ntp_test_utils.cc",
"../browser/ui/search/local_ntp_test_utils.h", "../browser/ui/search/local_ntp_test_utils.h",
"../browser/ui/search/local_ntp_voice_search_browsertest.cc", "../browser/ui/search/local_ntp_voice_search_browsertest.cc",
"../browser/ui/search/new_tab_page_interceptor_browsertest.cc", "../browser/ui/search/new_tab_page_navigation_throttle_browsertest.cc",
"../browser/ui/search_engines/search_engine_tab_helper_browsertest.cc", "../browser/ui/search_engines/search_engine_tab_helper_browsertest.cc",
"../browser/ui/settings_window_manager_browsertest_chromeos.cc", "../browser/ui/settings_window_manager_browsertest_chromeos.cc",
"../browser/ui/startup/startup_browser_creator_browsertest.cc", "../browser/ui/startup/startup_browser_creator_browsertest.cc",
......
...@@ -470,13 +470,6 @@ ...@@ -470,13 +470,6 @@
-ExtensionRequestLimitingThrottleBrowserTest.ThrottleRequest_RedirectCached -ExtensionRequestLimitingThrottleBrowserTest.ThrottleRequest_RedirectCached
-ExtensionRequestLimitingThrottleCommandLineBrowserTest.ThrottleRequestDisabled -ExtensionRequestLimitingThrottleCommandLineBrowserTest.ThrottleRequestDisabled
# Redirect responses using NavigationThrottle or by hooking
# NavigationURLLoaderNetworkService::URLLoaderRequestController
# instead of net::URLRequestInterceptor.
-NewTabPageInterceptorTest.204Interception
-NewTabPageInterceptorTest.404Interception
-NewTabPageInterceptorTest.FailedRequestInterception
# This requires that InterceptNetworkTransactions works # This requires that InterceptNetworkTransactions works
-ErrorPageNavigationCorrectionsFailTest.StaleCacheStatusFailedCorrections -ErrorPageNavigationCorrectionsFailTest.StaleCacheStatusFailedCorrections
......
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