Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / net / preconnect.cc
blob17673c31bed256fdd2dc02dca27964b8938d7905
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"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "net/base/load_flags.h"
12 #include "net/http/http_network_session.h"
13 #include "net/http/http_request_info.h"
14 #include "net/http/http_stream_factory.h"
15 #include "net/http/http_transaction_factory.h"
16 #include "net/log/net_log.h"
17 #include "net/ssl/ssl_config_service.h"
18 #include "net/url_request/http_user_agent_settings.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h"
22 using content::BrowserThread;
24 namespace chrome_browser_net {
26 void PreconnectOnUIThread(
27 const GURL& url,
28 const GURL& first_party_for_cookies,
29 UrlInfo::ResolutionMotivation motivation,
30 int count,
31 net::URLRequestContextGetter* getter) {
32 // Prewarm connection to Search URL.
33 BrowserThread::PostTask(
34 BrowserThread::IO, FROM_HERE,
35 base::Bind(&PreconnectOnIOThread, url, first_party_for_cookies,
36 motivation, count, make_scoped_refptr(getter), true));
37 return;
40 void PreconnectOnIOThread(const GURL& url,
41 const GURL& first_party_for_cookies,
42 UrlInfo::ResolutionMotivation motivation,
43 int count,
44 net::URLRequestContextGetter* getter,
45 bool allow_credentials) {
46 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
47 LOG(DFATAL) << "This must be run only on the IO thread.";
48 return;
50 if (!getter)
51 return;
52 // We are now commited to doing the async preconnection call.
53 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectMotivation", motivation,
54 UrlInfo::MAX_MOTIVATED);
56 net::URLRequestContext* context = getter->GetURLRequestContext();
57 net::HttpTransactionFactory* factory = context->http_transaction_factory();
58 net::HttpNetworkSession* session = factory->GetSession();
60 std::string user_agent;
61 if (context->http_user_agent_settings())
62 user_agent = context->http_user_agent_settings()->GetUserAgent();
63 net::HttpRequestInfo request_info;
64 request_info.url = url;
65 request_info.method = "GET";
66 request_info.extra_headers.SetHeader(net::HttpRequestHeaders::kUserAgent,
67 user_agent);
69 net::NetworkDelegate* delegate = context->network_delegate();
70 if (delegate->CanEnablePrivacyMode(url, first_party_for_cookies))
71 request_info.privacy_mode = net::PRIVACY_MODE_ENABLED;
73 // TODO(yoav): Fix this layering violation, since when credentials are not
74 // allowed we should turn on a flag indicating that, rather then turn on
75 // private mode, even if lower layers would treat both the same.
76 if (!allow_credentials) {
77 request_info.privacy_mode = net::PRIVACY_MODE_ENABLED;
78 request_info.load_flags = net::LOAD_DO_NOT_SEND_COOKIES |
79 net::LOAD_DO_NOT_SAVE_COOKIES |
80 net::LOAD_DO_NOT_SEND_AUTH_DATA;
83 // Translate the motivation from UrlRequest motivations to HttpRequest
84 // motivations.
85 switch (motivation) {
86 case UrlInfo::OMNIBOX_MOTIVATED:
87 request_info.motivation = net::HttpRequestInfo::OMNIBOX_MOTIVATED;
88 break;
89 case UrlInfo::LEARNED_REFERAL_MOTIVATED:
90 request_info.motivation = net::HttpRequestInfo::PRECONNECT_MOTIVATED;
91 break;
92 case UrlInfo::MOUSE_OVER_MOTIVATED:
93 case UrlInfo::SELF_REFERAL_MOTIVATED:
94 case UrlInfo::EARLY_LOAD_MOTIVATED:
95 request_info.motivation = net::HttpRequestInfo::EARLY_LOAD_MOTIVATED;
96 break;
97 default:
98 // Other motivations should never happen here.
99 NOTREACHED();
100 break;
103 // Setup the SSL Configuration.
104 net::SSLConfig ssl_config;
105 session->ssl_config_service()->GetSSLConfig(&ssl_config);
106 session->GetNextProtos(&ssl_config.next_protos);
108 // All preconnects should perform EV certificate verification.
109 ssl_config.verify_ev_cert = true;
111 net::HttpStreamFactory* http_stream_factory = session->http_stream_factory();
112 http_stream_factory->PreconnectStreams(count, request_info, ssl_config,
113 ssl_config);
116 } // namespace chrome_browser_net