Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/search/new_tab_page_interceptor_service.h" | |
| 6 | |
| 7 #include "base/location.h" | |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/search/search.h" | |
| 11 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 12 #include "chrome/common/url_constants.h" | |
| 13 #include "components/search_engines/template_url_service.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 #include "net/http/http_status_code.h" | |
| 17 #include "net/url_request/url_request.h" | |
| 18 #include "net/url_request/url_request_job.h" | |
| 19 #include "net/url_request/url_request_redirect_job.h" | |
| 20 | |
| 21 NewTabPageInterceptorService::NewTabPageInterceptor::NewTabPageInterceptor( | |
| 22 const GURL& new_tab_url) | |
| 23 : new_tab_url_(new_tab_url), weak_factory_(this) { | |
| 24 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 25 } | |
| 26 | |
| 27 NewTabPageInterceptorService::NewTabPageInterceptor::~NewTabPageInterceptor() { | |
| 28 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 29 } | |
| 30 | |
| 31 base::WeakPtr<NewTabPageInterceptorService::NewTabPageInterceptor> | |
| 32 NewTabPageInterceptorService::NewTabPageInterceptor::GetWeakPtr() { | |
| 33 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 34 return weak_factory_.GetWeakPtr(); | |
| 35 } | |
| 36 | |
| 37 void NewTabPageInterceptorService::NewTabPageInterceptor::SetNewTabPageURL( | |
| 38 const GURL& new_tab_page_url) { | |
| 39 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 40 new_tab_url_ = new_tab_page_url; | |
| 41 } | |
| 42 | |
| 43 net::URLRequestJob* | |
| 44 NewTabPageInterceptorService::NewTabPageInterceptor::MaybeInterceptRequest( | |
| 45 net::URLRequest* request, | |
| 46 net::NetworkDelegate* network_delegate) const { | |
| 47 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 48 return nullptr; | |
| 49 } | |
| 50 | |
| 51 net::URLRequestJob* | |
| 52 NewTabPageInterceptorService::NewTabPageInterceptor::MaybeInterceptResponse( | |
| 53 net::URLRequest* request, | |
| 54 net::NetworkDelegate* network_delegate) const { | |
| 55 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 56 if ((request->url() != new_tab_url_) || | |
| 57 (new_tab_url_ == GURL(chrome::kChromeSearchLocalNtpUrl))) { | |
| 58 return nullptr; | |
| 59 } | |
| 60 // User has canceled this navigation so it shouldn't be redirected. | |
| 61 if (request->status().status() == net::URLRequestStatus::CANCELED || | |
| 62 (request->status().status() == net::URLRequestStatus::FAILED && | |
| 63 request->status().error() == net::ERR_ABORTED)) { | |
| 64 return nullptr; | |
| 65 } | |
| 66 | |
| 67 // Request to NTP was successful. | |
| 68 if (request->status().is_success() && | |
| 69 request->GetResponseCode() != net::HTTP_NO_CONTENT && | |
| 70 request->GetResponseCode() < 400) | |
| 71 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.
| |
| 72 | |
| 73 // Failure to load the NTP correctly; redirect to Local NTP. | |
| 74 UMA_HISTOGRAM_ENUMERATION("InstantExtended.CacheableNTPLoad", | |
| 75 chrome::CACHEABLE_NTP_LOAD_FAILED, | |
| 76 chrome::CACHEABLE_NTP_LOAD_MAX); | |
| 77 return new net::URLRequestRedirectJob( | |
| 78 request, network_delegate, GURL(chrome::kChromeSearchLocalNtpUrl), | |
| 79 net::URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, | |
| 80 "NTP Request Interceptor"); | |
| 81 } | |
| 82 | |
| 83 NewTabPageInterceptorService::NewTabPageInterceptorService(Profile* profile) | |
| 84 : profile_(profile), | |
| 85 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)) { | |
| 86 DCHECK(profile_); | |
|
mmenke
2015/01/22 20:53:51
nit: include base/logging.h for DCHECK
Mathieu
2015/01/22 21:44:22
Done.
| |
| 87 if (template_url_service_) | |
| 88 template_url_service_->AddObserver(this); | |
| 89 } | |
| 90 | |
| 91 NewTabPageInterceptorService::~NewTabPageInterceptorService() { | |
| 92 if (template_url_service_) | |
| 93 template_url_service_->RemoveObserver(this); | |
| 94 } | |
| 95 | |
| 96 void NewTabPageInterceptorService::OnTemplateURLServiceChanged() { | |
| 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 98 GURL new_tab_page_url(chrome::GetNewTabPageURL(profile_)); | |
| 99 content::BrowserThread::PostTask( | |
| 100 content::BrowserThread::IO, FROM_HERE, | |
| 101 base::Bind(&NewTabPageInterceptor::SetNewTabPageURL, interceptor_, | |
| 102 new_tab_page_url)); | |
| 103 } | |
| 104 | |
| 105 scoped_ptr<net::URLRequestInterceptor> | |
| 106 NewTabPageInterceptorService::CreateInterceptor() { | |
| 107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 108 scoped_ptr<NewTabPageInterceptor> interceptor( | |
| 109 new NewTabPageInterceptor(chrome::GetNewTabPageURL(profile_))); | |
| 110 interceptor_ = interceptor->GetWeakPtr(); | |
| 111 return interceptor.Pass(); | |
| 112 } | |
| OLD | NEW |