| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/preconnect.h" | 5 #include "chrome/browser/net/preconnect.h" |
| 6 | 6 |
| 7 #include "base/histogram.h" | 7 #include "base/histogram.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "chrome/browser/profile.h" | 9 #include "chrome/browser/profile.h" |
| 10 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
| 11 #include "chrome/common/net/url_request_context_getter.h" | 11 #include "chrome/common/net/url_request_context_getter.h" |
| 12 #include "net/base/host_port_pair.h" | 12 #include "net/base/host_port_pair.h" |
| 13 #include "net/http/http_network_session.h" | 13 #include "net/http/http_network_session.h" |
| 14 #include "net/http/http_transaction_factory.h" | 14 #include "net/http/http_transaction_factory.h" |
| 15 #include "net/proxy/proxy_service.h" | 15 #include "net/proxy/proxy_service.h" |
| 16 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
| 17 | 17 |
| 18 namespace chrome_browser_net { | 18 namespace chrome_browser_net { |
| 19 | 19 |
| 20 // static | 20 // static |
| 21 bool Preconnect::preconnect_despite_proxy_ = false; | 21 bool Preconnect::preconnect_despite_proxy_ = false; |
| 22 | 22 |
| 23 // We will deliberately leak this singular instance, which is used only for | 23 // We will deliberately leak this singular instance, which is used only for |
| 24 // callbacks. | 24 // callbacks. |
| 25 // static | 25 // static |
| 26 Preconnect* Preconnect::callback_instance_; | 26 Preconnect* Preconnect::callback_instance_; |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 bool Preconnect::PreconnectOnUIThread(const GURL& url) { | 29 void Preconnect::PreconnectOnUIThread(const GURL& url, |
| 30 UrlInfo::ResolutionMotivation motivation) { |
| 30 // Try to do connection warming for this search provider. | 31 // Try to do connection warming for this search provider. |
| 31 URLRequestContextGetter* getter = Profile::GetDefaultRequestContext(); | 32 URLRequestContextGetter* getter = Profile::GetDefaultRequestContext(); |
| 32 if (!getter) | 33 if (!getter) |
| 33 return false; | 34 return; |
| 34 // Prewarm connection to Search URL. | 35 // Prewarm connection to Search URL. |
| 35 ChromeThread::PostTask( | 36 ChromeThread::PostTask( |
| 36 ChromeThread::IO, | 37 ChromeThread::IO, |
| 37 FROM_HERE, | 38 FROM_HERE, |
| 38 NewRunnableFunction(Preconnect::PreconnectOnIOThread, url)); | 39 NewRunnableFunction(Preconnect::PreconnectOnIOThread, url, motivation)); |
| 39 return true; | 40 return; |
| 40 } | 41 } |
| 41 | 42 |
| 42 enum ProxyStatus { | 43 enum ProxyStatus { |
| 43 PROXY_STATUS_IGNORED, | 44 PROXY_STATUS_IGNORED, |
| 44 PROXY_UNINITIALIZED, | 45 PROXY_UNINITIALIZED, |
| 45 PROXY_NOT_USED, | 46 PROXY_NOT_USED, |
| 46 PROXY_PAC_RESOLVER, | 47 PROXY_PAC_RESOLVER, |
| 47 PROXY_HAS_RULES, | 48 PROXY_HAS_RULES, |
| 48 PROXY_MAX, | 49 PROXY_MAX, |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 static void HistogramPreconnectStatus(ProxyStatus status) { | 52 static void HistogramPreconnectStatus(ProxyStatus status) { |
| 52 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectProxyStatus", status, PROXY_MAX); | 53 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectProxyStatus", status, PROXY_MAX); |
| 53 } | 54 } |
| 54 | 55 |
| 55 // static | 56 // static |
| 56 void Preconnect::PreconnectOnIOThread(const GURL& url) { | 57 void Preconnect::PreconnectOnIOThread(const GURL& url, |
| 57 // TODO(jar): This does not handle proxies currently. | 58 UrlInfo::ResolutionMotivation motivation) { |
| 58 URLRequestContextGetter* getter = Profile::GetDefaultRequestContext(); | 59 URLRequestContextGetter* getter = Profile::GetDefaultRequestContext(); |
| 59 if (!getter) | 60 if (!getter) |
| 60 return; | 61 return; |
| 61 if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) { | 62 if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) { |
| 62 LOG(DFATAL) << "This must be run only on the IO thread."; | 63 LOG(DFATAL) << "This must be run only on the IO thread."; |
| 63 return; | 64 return; |
| 64 } | 65 } |
| 65 URLRequestContext* context = getter->GetURLRequestContext(); | 66 URLRequestContext* context = getter->GetURLRequestContext(); |
| 66 | 67 |
| 67 if (preconnect_despite_proxy_) { | 68 if (preconnect_despite_proxy_) { |
| 68 HistogramPreconnectStatus(PROXY_STATUS_IGNORED); | 69 HistogramPreconnectStatus(PROXY_STATUS_IGNORED); |
| 69 } else { | 70 } else { |
| 70 // Currently we avoid all preconnects if there is a proxy configuration. | 71 // Currently we avoid all preconnects if there is a proxy configuration. |
| 71 net::ProxyService* proxy_service = context->proxy_service(); | 72 net::ProxyService* proxy_service = context->proxy_service(); |
| 72 if (!proxy_service->config_has_been_initialized()) { | 73 if (!proxy_service->config_has_been_initialized()) { |
| 73 HistogramPreconnectStatus(PROXY_UNINITIALIZED); | 74 HistogramPreconnectStatus(PROXY_UNINITIALIZED); |
| 74 } else { | 75 } else { |
| 75 if (proxy_service->config().MayRequirePACResolver()) { | 76 if (proxy_service->config().MayRequirePACResolver()) { |
| 76 HistogramPreconnectStatus(PROXY_PAC_RESOLVER); | 77 HistogramPreconnectStatus(PROXY_PAC_RESOLVER); |
| 77 return; | 78 return; |
| 78 } | 79 } |
| 79 if (!proxy_service->config().proxy_rules().empty()) { | 80 if (!proxy_service->config().proxy_rules().empty()) { |
| 80 HistogramPreconnectStatus(PROXY_HAS_RULES); | 81 HistogramPreconnectStatus(PROXY_HAS_RULES); |
| 81 return; | 82 return; |
| 82 } | 83 } |
| 83 HistogramPreconnectStatus(PROXY_NOT_USED); | 84 HistogramPreconnectStatus(PROXY_NOT_USED); |
| 84 } | 85 } |
| 85 } | 86 } |
| 86 | 87 |
| 88 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectMotivation", motivation, |
| 89 UrlInfo::MAX_MOTIVATED); |
| 90 |
| 87 net::HttpTransactionFactory* factory = context->http_transaction_factory(); | 91 net::HttpTransactionFactory* factory = context->http_transaction_factory(); |
| 88 net::HttpNetworkSession* session = factory->GetSession(); | 92 net::HttpNetworkSession* session = factory->GetSession(); |
| 89 | 93 |
| 90 net::ClientSocketHandle handle; | 94 net::ClientSocketHandle handle; |
| 91 if (!callback_instance_) | 95 if (!callback_instance_) |
| 92 callback_instance_ = new Preconnect; | 96 callback_instance_ = new Preconnect; |
| 93 | 97 |
| 94 scoped_refptr<net::TCPSocketParams> tcp_params = | 98 scoped_refptr<net::TCPSocketParams> tcp_params = |
| 95 new net::TCPSocketParams(url.host(), url.EffectiveIntPort(), net::LOW, | 99 new net::TCPSocketParams(url.host(), url.EffectiveIntPort(), net::LOW, |
| 96 GURL(), false); | 100 GURL(), false); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 126 handle.Init(group_name, tcp_params, net::LOWEST, callback_instance_, pool, | 130 handle.Init(group_name, tcp_params, net::LOWEST, callback_instance_, pool, |
| 127 net::BoundNetLog()); | 131 net::BoundNetLog()); |
| 128 handle.Reset(); | 132 handle.Reset(); |
| 129 } | 133 } |
| 130 | 134 |
| 131 void Preconnect::RunWithParams(const Tuple1<int>& params) { | 135 void Preconnect::RunWithParams(const Tuple1<int>& params) { |
| 132 // This will rarely be called, as we reset the connection just after creating. | 136 // This will rarely be called, as we reset the connection just after creating. |
| 133 NOTREACHED(); | 137 NOTREACHED(); |
| 134 } | 138 } |
| 135 } // chrome_browser_net | 139 } // chrome_browser_net |
| OLD | NEW |