Index: chrome/browser/ui/search/new_tab_page_interceptor_service.cc |
diff --git a/chrome/browser/ui/search/new_tab_page_interceptor_service.cc b/chrome/browser/ui/search/new_tab_page_interceptor_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..db9bc3f8da03e7db4e4d68baddadc006a85540c9 |
--- /dev/null |
+++ b/chrome/browser/ui/search/new_tab_page_interceptor_service.cc |
@@ -0,0 +1,112 @@ |
+// 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 "base/location.h" |
+#include "base/metrics/histogram.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_job.h" |
+#include "net/url_request/url_request_redirect_job.h" |
+ |
+NewTabPageInterceptorService::NewTabPageInterceptor::NewTabPageInterceptor( |
+ const GURL& new_tab_url) |
+ : new_tab_url_(new_tab_url), weak_factory_(this) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+} |
+ |
+NewTabPageInterceptorService::NewTabPageInterceptor::~NewTabPageInterceptor() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+} |
+ |
+base::WeakPtr<NewTabPageInterceptorService::NewTabPageInterceptor> |
+NewTabPageInterceptorService::NewTabPageInterceptor::GetWeakPtr() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ return weak_factory_.GetWeakPtr(); |
+} |
+ |
+void NewTabPageInterceptorService::NewTabPageInterceptor::SetNewTabPageURL( |
+ const GURL& new_tab_page_url) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ new_tab_url_ = new_tab_page_url; |
+} |
+ |
+net::URLRequestJob* |
+NewTabPageInterceptorService::NewTabPageInterceptor::MaybeInterceptRequest( |
+ net::URLRequest* request, |
+ net::NetworkDelegate* network_delegate) const { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ return nullptr; |
+} |
+ |
+net::URLRequestJob* |
+NewTabPageInterceptorService::NewTabPageInterceptor::MaybeInterceptResponse( |
+ net::URLRequest* request, |
+ net::NetworkDelegate* network_delegate) const { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ if ((request->url() != new_tab_url_) || |
+ (new_tab_url_ == GURL(chrome::kChromeSearchLocalNtpUrl))) { |
+ return nullptr; |
+ } |
+ // User has canceled this navigation so it shouldn't be redirected. |
+ 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. |
+ if (request->status().is_success() && |
+ request->GetResponseCode() != net::HTTP_NO_CONTENT && |
+ request->GetResponseCode() < 400) |
+ return nullptr; |
mmenke
2015/01/22 20:53:51
nit: Use braces when the if body takes more than
Mathieu
2015/01/22 21:44:22
Done.
|
+ |
+ // Failure to load the NTP correctly; redirect to Local NTP. |
+ UMA_HISTOGRAM_ENUMERATION("InstantExtended.CacheableNTPLoad", |
+ chrome::CACHEABLE_NTP_LOAD_FAILED, |
+ chrome::CACHEABLE_NTP_LOAD_MAX); |
+ return new net::URLRequestRedirectJob( |
+ request, network_delegate, GURL(chrome::kChromeSearchLocalNtpUrl), |
+ net::URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
+ "NTP Request Interceptor"); |
+} |
+ |
+NewTabPageInterceptorService::NewTabPageInterceptorService(Profile* profile) |
+ : profile_(profile), |
+ template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)) { |
+ DCHECK(profile_); |
mmenke
2015/01/22 20:53:51
nit: include base/logging.h for DCHECK
Mathieu
2015/01/22 21:44:22
Done.
|
+ 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(chrome::GetNewTabPageURL(profile_)); |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&NewTabPageInterceptor::SetNewTabPageURL, interceptor_, |
+ new_tab_page_url)); |
+} |
+ |
+scoped_ptr<net::URLRequestInterceptor> |
+NewTabPageInterceptorService::CreateInterceptor() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ scoped_ptr<NewTabPageInterceptor> interceptor( |
+ new NewTabPageInterceptor(chrome::GetNewTabPageURL(profile_))); |
+ interceptor_ = interceptor->GetWeakPtr(); |
+ return interceptor.Pass(); |
+} |