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.h" |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "chrome/browser/search/search.h" |
| 9 #include "chrome/common/url_constants.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "net/http/http_status_code.h" |
| 12 #include "net/url_request/url_request.h" |
| 13 #include "net/url_request/url_request_job.h" |
| 14 #include "net/url_request/url_request_redirect_job.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Log a histogram for NTP load failure. |
| 19 void RecordNTPLoadFailure() { |
| 20 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 21 UMA_HISTOGRAM_ENUMERATION("InstantExtended.CacheableNTPLoad", |
| 22 chrome::CACHEABLE_NTP_LOAD_FAILED, |
| 23 chrome::CACHEABLE_NTP_LOAD_MAX); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 NewTabPageInterceptor::NewTabPageInterceptor(const GURL& new_tab_url) |
| 29 : new_tab_url_(new_tab_url), weak_factory_(this) { |
| 30 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 31 } |
| 32 |
| 33 NewTabPageInterceptor::~NewTabPageInterceptor() { |
| 34 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 35 } |
| 36 |
| 37 base::WeakPtr<NewTabPageInterceptor> NewTabPageInterceptor::GetWeakPtr() { |
| 38 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 39 return weak_factory_.GetWeakPtr(); |
| 40 } |
| 41 |
| 42 void NewTabPageInterceptor::SetNewTabPageURL(const GURL& new_tab_page_url) { |
| 43 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 44 new_tab_url_ = new_tab_page_url; |
| 45 } |
| 46 |
| 47 net::URLRequestJob* NewTabPageInterceptor::MaybeInterceptRequest( |
| 48 net::URLRequest* request, |
| 49 net::NetworkDelegate* network_delegate) const { |
| 50 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 51 return nullptr; |
| 52 } |
| 53 |
| 54 net::URLRequestJob* NewTabPageInterceptor::MaybeInterceptResponse( |
| 55 net::URLRequest* request, |
| 56 net::NetworkDelegate* network_delegate) const { |
| 57 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 58 if ((request->url() != new_tab_url_) || |
| 59 (request->url() == GURL(chrome::kChromeSearchLocalNtpUrl))) { |
| 60 return nullptr; |
| 61 } |
| 62 // User has canceled this navigation so it shouldn't be redirected. |
| 63 if (request->status().status() == net::URLRequestStatus::CANCELED) { |
| 64 return nullptr; |
| 65 } |
| 66 |
| 67 if (!request->status().is_success() || |
| 68 request->GetResponseCode() == net::HTTP_NO_CONTENT || |
| 69 request->GetResponseCode() >= 400) { |
| 70 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 71 base::Bind(&RecordNTPLoadFailure)); |
| 72 return new net::URLRequestRedirectJob( |
| 73 request, network_delegate, GURL(chrome::kChromeSearchLocalNtpUrl), |
| 74 net::URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
| 75 "NTP Request Interceptor"); |
| 76 } |
| 77 return nullptr; |
| 78 } |
OLD | NEW |