1 // Copyright (c) 2012 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.
5 #include "chrome/browser/net/preconnect.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "net/http/http_network_session.h"
12 #include "net/http/http_request_info.h"
13 #include "net/http/http_stream_factory.h"
14 #include "net/http/http_transaction_factory.h"
15 #include "net/log/net_log.h"
16 #include "net/ssl/ssl_config_service.h"
17 #include "net/url_request/http_user_agent_settings.h"
18 #include "net/url_request/url_request_context.h"
19 #include "net/url_request/url_request_context_getter.h"
21 using content::BrowserThread
;
23 namespace chrome_browser_net
{
25 void PreconnectOnUIThread(
27 const GURL
& first_party_for_cookies
,
28 UrlInfo::ResolutionMotivation motivation
,
30 net::URLRequestContextGetter
* getter
) {
31 // Prewarm connection to Search URL.
32 BrowserThread::PostTask(
35 base::Bind(&PreconnectOnIOThread
, url
, first_party_for_cookies
,
36 motivation
, count
, make_scoped_refptr(getter
)));
41 void PreconnectOnIOThread(
43 const GURL
& first_party_for_cookies
,
44 UrlInfo::ResolutionMotivation motivation
,
46 net::URLRequestContextGetter
* getter
) {
47 if (!BrowserThread::CurrentlyOn(BrowserThread::IO
)) {
48 LOG(DFATAL
) << "This must be run only on the IO thread.";
53 // We are now commited to doing the async preconnection call.
54 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectMotivation", motivation
,
55 UrlInfo::MAX_MOTIVATED
);
57 net::URLRequestContext
* context
= getter
->GetURLRequestContext();
58 net::HttpTransactionFactory
* factory
= context
->http_transaction_factory();
59 net::HttpNetworkSession
* session
= factory
->GetSession();
61 std::string user_agent
;
62 if (context
->http_user_agent_settings())
63 user_agent
= context
->http_user_agent_settings()->GetUserAgent();
64 net::HttpRequestInfo request_info
;
65 request_info
.url
= url
;
66 request_info
.method
= "GET";
67 request_info
.extra_headers
.SetHeader(net::HttpRequestHeaders::kUserAgent
,
70 net::NetworkDelegate
* delegate
= context
->network_delegate();
71 if (delegate
->CanEnablePrivacyMode(url
, first_party_for_cookies
))
72 request_info
.privacy_mode
= net::PRIVACY_MODE_ENABLED
;
74 // Translate the motivation from UrlRequest motivations to HttpRequest
77 case UrlInfo::OMNIBOX_MOTIVATED
:
78 request_info
.motivation
= net::HttpRequestInfo::OMNIBOX_MOTIVATED
;
80 case UrlInfo::LEARNED_REFERAL_MOTIVATED
:
81 request_info
.motivation
= net::HttpRequestInfo::PRECONNECT_MOTIVATED
;
83 case UrlInfo::MOUSE_OVER_MOTIVATED
:
84 case UrlInfo::SELF_REFERAL_MOTIVATED
:
85 case UrlInfo::EARLY_LOAD_MOTIVATED
:
86 request_info
.motivation
= net::HttpRequestInfo::EARLY_LOAD_MOTIVATED
;
89 // Other motivations should never happen here.
94 // Setup the SSL Configuration.
95 net::SSLConfig ssl_config
;
96 session
->ssl_config_service()->GetSSLConfig(&ssl_config
);
97 session
->GetNextProtos(&ssl_config
.next_protos
);
99 // All preconnects should perform EV certificate verification.
100 ssl_config
.verify_ev_cert
= true;
102 net::HttpStreamFactory
* http_stream_factory
= session
->http_stream_factory();
103 http_stream_factory
->PreconnectStreams(count
, request_info
, ssl_config
,
107 } // namespace chrome_browser_net