Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / io_thread.cc
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.
4
5 #include "chrome/browser/io_thread.h"
6
7 #include <vector>
8
9 #include "base/base64.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/debug/leak_tracker.h"
15 #include "base/debug/trace_event.h"
16 #include "base/logging.h"
17 #include "base/metrics/field_trial.h"
18 #include "base/prefs/pref_registry_simple.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/stl_util.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h"
24 #include "base/threading/sequenced_worker_pool.h"
25 #include "base/threading/thread.h"
26 #include "base/threading/worker_pool.h"
27 #include "base/time/time.h"
28 #include "build/build_config.h"
29 #include "chrome/browser/browser_process.h"
30 #include "chrome/browser/net/async_dns_field_trial.h"
31 #include "chrome/browser/net/chrome_net_log.h"
32 #include "chrome/browser/net/chrome_network_delegate.h"
33 #include "chrome/browser/net/connect_interceptor.h"
34 #include "chrome/browser/net/dns_probe_service.h"
35 #include "chrome/browser/net/pref_proxy_config_tracker.h"
36 #include "chrome/browser/net/proxy_service_factory.h"
37 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
38 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
39 #include "chrome/common/chrome_content_client.h"
40 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/chrome_version_info.h"
42 #include "chrome/common/pref_names.h"
43 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler.h"
44 #include "components/data_reduction_proxy/browser/data_reduction_proxy_delegate.h"
45 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
46 #include "components/data_reduction_proxy/browser/data_reduction_proxy_prefs.h"
47 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.h"
48 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
49 #include "components/policy/core/common/policy_service.h"
50 #include "components/variations/variations_associated_data.h"
51 #include "content/public/browser/browser_thread.h"
52 #include "content/public/browser/cookie_store_factory.h"
53 #include "net/base/host_mapping_rules.h"
54 #include "net/base/net_util.h"
55 #include "net/cert/cert_verifier.h"
56 #include "net/cert/cert_verify_proc.h"
57 #include "net/cert/ct_known_logs.h"
58 #include "net/cert/ct_log_verifier.h"
59 #include "net/cert/ct_verifier.h"
60 #include "net/cert/multi_log_ct_verifier.h"
61 #include "net/cert/multi_threaded_cert_verifier.h"
62 #include "net/cookies/cookie_store.h"
63 #include "net/dns/host_cache.h"
64 #include "net/dns/host_resolver.h"
65 #include "net/dns/mapped_host_resolver.h"
66 #include "net/ftp/ftp_network_layer.h"
67 #include "net/http/http_auth_filter.h"
68 #include "net/http/http_auth_handler_factory.h"
69 #include "net/http/http_network_layer.h"
70 #include "net/http/http_server_properties_impl.h"
71 #include "net/proxy/proxy_config_service.h"
72 #include "net/proxy/proxy_script_fetcher_impl.h"
73 #include "net/proxy/proxy_service.h"
74 #include "net/quic/crypto/crypto_protocol.h"
75 #include "net/quic/quic_protocol.h"
76 #include "net/socket/tcp_client_socket.h"
77 #include "net/spdy/spdy_session.h"
78 #include "net/ssl/channel_id_service.h"
79 #include "net/ssl/default_channel_id_store.h"
80 #include "net/url_request/data_protocol_handler.h"
81 #include "net/url_request/file_protocol_handler.h"
82 #include "net/url_request/ftp_protocol_handler.h"
83 #include "net/url_request/static_http_user_agent_settings.h"
84 #include "net/url_request/url_fetcher.h"
85 #include "net/url_request/url_request_context.h"
86 #include "net/url_request/url_request_context_getter.h"
87 #include "net/url_request/url_request_job_factory_impl.h"
88 #include "net/url_request/url_request_throttler_manager.h"
89 #include "url/url_constants.h"
90
91 #if defined(ENABLE_CONFIGURATION_POLICY)
92 #include "policy/policy_constants.h"
93 #endif
94
95 #if defined(ENABLE_EXTENSIONS)
96 #include "chrome/browser/extensions/event_router_forwarder.h"
97 #endif
98
99 #if defined(USE_NSS) || defined(OS_IOS)
100 #include "net/ocsp/nss_ocsp.h"
101 #endif
102
103 #if defined(OS_CHROMEOS)
104 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
105 #include "chromeos/network/host_resolver_impl_chromeos.h"
106 #endif
107
108 using content::BrowserThread;
109
110 class SafeBrowsingURLRequestContext;
111
112 // The IOThread object must outlive any tasks posted to the IO thread before the
113 // Quit task, so base::Bind() calls are not refcounted.
114
115 namespace {
116
117 const char kTCPFastOpenFieldTrialName[] = "TCPFastOpen";
118 const char kTCPFastOpenHttpsEnabledGroupName[] = "HttpsEnabled";
119
120 const char kQuicFieldTrialName[] = "QUIC";
121 const char kQuicFieldTrialEnabledGroupName[] = "Enabled";
122 const char kQuicFieldTrialHttpsEnabledGroupName[] = "HttpsEnabled";
123 const char kQuicFieldTrialPacketLengthSuffix[] = "BytePackets";
124 const char kQuicFieldTrialPacingSuffix[] = "WithPacing";
125 const char kQuicFieldTrialTimeBasedLossDetectionSuffix[] =
126     "WithTimeBasedLossDetection";
127
128 // The SPDY trial composes two different trial plus control groups:
129 //  * A "holdback" group with SPDY disabled, and corresponding control
130 //  (SPDY/3.1). The primary purpose of the holdback group is to encourage site
131 //  operators to do feature detection rather than UA-sniffing. As such, this
132 //  trial runs continuously.
133 //  * A SPDY/4 experiment, for SPDY/4 (aka HTTP/2) vs SPDY/3.1 comparisons and
134 //  eventual SPDY/4 deployment.
135 const char kSpdyFieldTrialName[] = "SPDY";
136 const char kSpdyFieldTrialHoldbackGroupName[] = "SpdyDisabled";
137 const char kSpdyFieldTrialHoldbackControlGroupName[] = "Control";
138 const char kSpdyFieldTrialSpdy4GroupName[] = "Spdy4Enabled";
139 const char kSpdyFieldTrialSpdy4ControlGroupName[] = "Spdy4Control";
140
141 #if defined(OS_MACOSX) && !defined(OS_IOS)
142 void ObserveKeychainEvents() {
143   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
144   net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents();
145 }
146 #endif
147
148 // Used for the "system" URLRequestContext.
149 class SystemURLRequestContext : public net::URLRequestContext {
150  public:
151   SystemURLRequestContext() {
152 #if defined(USE_NSS) || defined(OS_IOS)
153     net::SetURLRequestContextForNSSHttpIO(this);
154 #endif
155   }
156
157  private:
158   virtual ~SystemURLRequestContext() {
159     AssertNoURLRequests();
160 #if defined(USE_NSS) || defined(OS_IOS)
161     net::SetURLRequestContextForNSSHttpIO(NULL);
162 #endif
163   }
164 };
165
166 scoped_ptr<net::HostResolver> CreateGlobalHostResolver(net::NetLog* net_log) {
167   TRACE_EVENT0("startup", "IOThread::CreateGlobalHostResolver");
168   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
169
170   net::HostResolver::Options options;
171
172   // Use the concurrency override from the command-line, if any.
173   if (command_line.HasSwitch(switches::kHostResolverParallelism)) {
174     std::string s =
175         command_line.GetSwitchValueASCII(switches::kHostResolverParallelism);
176
177     // Parse the switch (it should be a positive integer formatted as decimal).
178     int n;
179     if (base::StringToInt(s, &n) && n > 0) {
180       options.max_concurrent_resolves = static_cast<size_t>(n);
181     } else {
182       LOG(ERROR) << "Invalid switch for host resolver parallelism: " << s;
183     }
184   }
185
186   // Use the retry attempts override from the command-line, if any.
187   if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) {
188     std::string s =
189         command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts);
190     // Parse the switch (it should be a non-negative integer).
191     int n;
192     if (base::StringToInt(s, &n) && n >= 0) {
193       options.max_retry_attempts = static_cast<size_t>(n);
194     } else {
195       LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s;
196     }
197   }
198
199   scoped_ptr<net::HostResolver> global_host_resolver;
200 #if defined OS_CHROMEOS
201   global_host_resolver =
202       chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
203                                                                net_log);
204 #else
205   global_host_resolver =
206       net::HostResolver::CreateSystemResolver(options, net_log);
207 #endif
208
209   // Determine if we should disable IPv6 support.
210   if (command_line.HasSwitch(switches::kEnableIPv6)) {
211     // Disable IPv6 probing.
212     global_host_resolver->SetDefaultAddressFamily(
213         net::ADDRESS_FAMILY_UNSPECIFIED);
214   } else if (command_line.HasSwitch(switches::kDisableIPv6)) {
215     global_host_resolver->SetDefaultAddressFamily(net::ADDRESS_FAMILY_IPV4);
216   }
217
218   // If hostname remappings were specified on the command-line, layer these
219   // rules on top of the real host resolver. This allows forwarding all requests
220   // through a designated test server.
221   if (!command_line.HasSwitch(switches::kHostResolverRules))
222     return global_host_resolver.PassAs<net::HostResolver>();
223
224   scoped_ptr<net::MappedHostResolver> remapped_resolver(
225       new net::MappedHostResolver(global_host_resolver.Pass()));
226   remapped_resolver->SetRulesFromString(
227       command_line.GetSwitchValueASCII(switches::kHostResolverRules));
228   return remapped_resolver.PassAs<net::HostResolver>();
229 }
230
231 // TODO(willchan): Remove proxy script fetcher context since it's not necessary
232 // now that I got rid of refcounting URLRequestContexts.
233 // See IOThread::Globals for details.
234 net::URLRequestContext*
235 ConstructProxyScriptFetcherContext(IOThread::Globals* globals,
236                                    net::NetLog* net_log) {
237   net::URLRequestContext* context = new net::URLRequestContext;
238   context->set_net_log(net_log);
239   context->set_host_resolver(globals->host_resolver.get());
240   context->set_cert_verifier(globals->cert_verifier.get());
241   context->set_transport_security_state(
242       globals->transport_security_state.get());
243   context->set_cert_transparency_verifier(
244       globals->cert_transparency_verifier.get());
245   context->set_http_auth_handler_factory(
246       globals->http_auth_handler_factory.get());
247   context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
248   context->set_http_transaction_factory(
249       globals->proxy_script_fetcher_http_transaction_factory.get());
250   context->set_job_factory(
251       globals->proxy_script_fetcher_url_request_job_factory.get());
252   context->set_cookie_store(globals->system_cookie_store.get());
253   context->set_channel_id_service(
254       globals->system_channel_id_service.get());
255   context->set_network_delegate(globals->system_network_delegate.get());
256   context->set_http_user_agent_settings(
257       globals->http_user_agent_settings.get());
258   // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
259   // system URLRequestContext too. There's no reason this should be tied to a
260   // profile.
261   return context;
262 }
263
264 net::URLRequestContext*
265 ConstructSystemRequestContext(IOThread::Globals* globals,
266                               net::NetLog* net_log) {
267   net::URLRequestContext* context = new SystemURLRequestContext;
268   context->set_net_log(net_log);
269   context->set_host_resolver(globals->host_resolver.get());
270   context->set_cert_verifier(globals->cert_verifier.get());
271   context->set_transport_security_state(
272       globals->transport_security_state.get());
273   context->set_cert_transparency_verifier(
274       globals->cert_transparency_verifier.get());
275   context->set_http_auth_handler_factory(
276       globals->http_auth_handler_factory.get());
277   context->set_proxy_service(globals->system_proxy_service.get());
278   context->set_http_transaction_factory(
279       globals->system_http_transaction_factory.get());
280   context->set_job_factory(globals->system_url_request_job_factory.get());
281   context->set_cookie_store(globals->system_cookie_store.get());
282   context->set_channel_id_service(
283       globals->system_channel_id_service.get());
284   context->set_throttler_manager(globals->throttler_manager.get());
285   context->set_network_delegate(globals->system_network_delegate.get());
286   context->set_http_user_agent_settings(
287       globals->http_user_agent_settings.get());
288   return context;
289 }
290
291 int GetSwitchValueAsInt(const CommandLine& command_line,
292                         const std::string& switch_name) {
293   int value;
294   if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
295                          &value)) {
296     return 0;
297   }
298   return value;
299 }
300
301 // Returns the value associated with |key| in |params| or "" if the
302 // key is not present in the map.
303 const std::string& GetVariationParam(
304     const std::map<std::string, std::string>& params,
305     const std::string& key) {
306   std::map<std::string, std::string>::const_iterator it = params.find(key);
307   if (it == params.end())
308     return base::EmptyString();
309
310   return it->second;
311 }
312
313 }  // namespace
314
315 class IOThread::LoggingNetworkChangeObserver
316     : public net::NetworkChangeNotifier::IPAddressObserver,
317       public net::NetworkChangeNotifier::ConnectionTypeObserver,
318       public net::NetworkChangeNotifier::NetworkChangeObserver {
319  public:
320   // |net_log| must remain valid throughout our lifetime.
321   explicit LoggingNetworkChangeObserver(net::NetLog* net_log)
322       : net_log_(net_log) {
323     net::NetworkChangeNotifier::AddIPAddressObserver(this);
324     net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
325     net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
326   }
327
328   virtual ~LoggingNetworkChangeObserver() {
329     net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
330     net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
331     net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
332   }
333
334   // NetworkChangeNotifier::IPAddressObserver implementation.
335   virtual void OnIPAddressChanged() OVERRIDE {
336     VLOG(1) << "Observed a change to the network IP addresses";
337
338     net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED);
339   }
340
341   // NetworkChangeNotifier::ConnectionTypeObserver implementation.
342   virtual void OnConnectionTypeChanged(
343       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE {
344     std::string type_as_string =
345         net::NetworkChangeNotifier::ConnectionTypeToString(type);
346
347     VLOG(1) << "Observed a change to network connectivity state "
348             << type_as_string;
349
350     net_log_->AddGlobalEntry(
351         net::NetLog::TYPE_NETWORK_CONNECTIVITY_CHANGED,
352         net::NetLog::StringCallback("new_connection_type", &type_as_string));
353   }
354
355   // NetworkChangeNotifier::NetworkChangeObserver implementation.
356   virtual void OnNetworkChanged(
357       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE {
358     std::string type_as_string =
359         net::NetworkChangeNotifier::ConnectionTypeToString(type);
360
361     VLOG(1) << "Observed a network change to state " << type_as_string;
362
363     net_log_->AddGlobalEntry(
364         net::NetLog::TYPE_NETWORK_CHANGED,
365         net::NetLog::StringCallback("new_connection_type", &type_as_string));
366   }
367
368  private:
369   net::NetLog* net_log_;
370   DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver);
371 };
372
373 class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
374  public:
375   explicit SystemURLRequestContextGetter(IOThread* io_thread);
376
377   // Implementation for net::UrlRequestContextGetter.
378   virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE;
379   virtual scoped_refptr<base::SingleThreadTaskRunner>
380       GetNetworkTaskRunner() const OVERRIDE;
381
382  protected:
383   virtual ~SystemURLRequestContextGetter();
384
385  private:
386   IOThread* const io_thread_;  // Weak pointer, owned by BrowserProcess.
387   scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
388
389   base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
390 };
391
392 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
393     IOThread* io_thread)
394     : io_thread_(io_thread),
395       network_task_runner_(
396           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) {
397 }
398
399 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
400
401 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
402   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
403   DCHECK(io_thread_->globals()->system_request_context.get());
404
405   return io_thread_->globals()->system_request_context.get();
406 }
407
408 scoped_refptr<base::SingleThreadTaskRunner>
409 SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
410   return network_task_runner_;
411 }
412
413 IOThread::Globals::
414 SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
415     Globals* globals)
416     : globals_(globals) {
417   DCHECK(globals_);
418 }
419
420 IOThread::Globals::
421 SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
422   if (globals_->system_request_context.get())
423     globals_->system_request_context->AssertNoURLRequests();
424 }
425
426 IOThread::Globals::Globals()
427     : system_request_context_leak_checker(this),
428       enable_ssl_connect_job_waiting(false),
429       ignore_certificate_errors(false),
430       testing_fixed_http_port(0),
431       testing_fixed_https_port(0),
432       enable_user_alternate_protocol_ports(false) {
433 }
434
435 IOThread::Globals::~Globals() {}
436
437 // |local_state| is passed in explicitly in order to (1) reduce implicit
438 // dependencies and (2) make IOThread more flexible for testing.
439 IOThread::IOThread(
440     PrefService* local_state,
441     policy::PolicyService* policy_service,
442     ChromeNetLog* net_log,
443     extensions::EventRouterForwarder* extension_event_router_forwarder)
444     : net_log_(net_log),
445 #if defined(ENABLE_EXTENSIONS)
446       extension_event_router_forwarder_(extension_event_router_forwarder),
447 #endif
448       globals_(NULL),
449       is_spdy_disabled_by_policy_(false),
450       creation_time_(base::TimeTicks::Now()),
451       weak_factory_(this) {
452   auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
453   negotiate_disable_cname_lookup_ = local_state->GetBoolean(
454       prefs::kDisableAuthNegotiateCnameLookup);
455   negotiate_enable_port_ = local_state->GetBoolean(
456       prefs::kEnableAuthNegotiatePort);
457   auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist);
458   auth_delegate_whitelist_ = local_state->GetString(
459       prefs::kAuthNegotiateDelegateWhitelist);
460   gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
461   pref_proxy_config_tracker_.reset(
462       ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
463           local_state));
464   ChromeNetworkDelegate::InitializePrefsOnUIThread(
465       &system_enable_referrers_,
466       NULL,
467       NULL,
468       local_state);
469   ssl_config_service_manager_.reset(
470       SSLConfigServiceManager::CreateDefaultManager(local_state));
471
472   base::Value* dns_client_enabled_default = new base::FundamentalValue(
473       chrome_browser_net::ConfigureAsyncDnsFieldTrial());
474   local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
475                                    dns_client_enabled_default);
476
477   dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
478                            local_state,
479                            base::Bind(&IOThread::UpdateDnsClientEnabled,
480                                       base::Unretained(this)));
481   dns_client_enabled_.MoveToThread(
482       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
483
484   quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
485                             local_state);
486   quick_check_enabled_.MoveToThread(
487       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
488
489 #if defined(ENABLE_CONFIGURATION_POLICY)
490   is_spdy_disabled_by_policy_ = policy_service->GetPolicies(
491       policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())).Get(
492           policy::key::kDisableSpdy) != NULL;
493 #endif  // ENABLE_CONFIGURATION_POLICY
494
495   BrowserThread::SetDelegate(BrowserThread::IO, this);
496 }
497
498 IOThread::~IOThread() {
499   // This isn't needed for production code, but in tests, IOThread may
500   // be multiply constructed.
501   BrowserThread::SetDelegate(BrowserThread::IO, NULL);
502
503   pref_proxy_config_tracker_->DetachFromPrefService();
504   DCHECK(!globals_);
505 }
506
507 IOThread::Globals* IOThread::globals() {
508   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
509   return globals_;
510 }
511
512 void IOThread::SetGlobalsForTesting(Globals* globals) {
513   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
514   DCHECK(!globals || !globals_);
515   globals_ = globals;
516 }
517
518 ChromeNetLog* IOThread::net_log() {
519   return net_log_;
520 }
521
522 void IOThread::ChangedToOnTheRecord() {
523   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
524   BrowserThread::PostTask(
525       BrowserThread::IO,
526       FROM_HERE,
527       base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
528                  base::Unretained(this)));
529 }
530
531 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
532   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
533   if (!system_url_request_context_getter_.get()) {
534     InitSystemRequestContext();
535   }
536   return system_url_request_context_getter_.get();
537 }
538
539 void IOThread::Init() {
540   // Prefer to use InitAsync unless you need initialization to block
541   // the UI thread
542 }
543
544 void IOThread::InitAsync() {
545   TRACE_EVENT0("startup", "IOThread::InitAsync");
546   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
547
548 #if defined(USE_NSS) || defined(OS_IOS)
549   net::SetMessageLoopForNSSHttpIO();
550 #endif
551
552   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
553
554   DCHECK(!globals_);
555   globals_ = new Globals;
556
557   // Add an observer that will emit network change events to the ChromeNetLog.
558   // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
559   // logging the network change before other IO thread consumers respond to it.
560   network_change_observer_.reset(
561       new LoggingNetworkChangeObserver(net_log_));
562
563   // Setup the HistogramWatcher to run on the IO thread.
564   net::NetworkChangeNotifier::InitHistogramWatcher();
565
566 #if defined(ENABLE_EXTENSIONS)
567   globals_->extension_event_router_forwarder =
568       extension_event_router_forwarder_;
569 #endif
570
571   ChromeNetworkDelegate* network_delegate =
572       new ChromeNetworkDelegate(extension_event_router_forwarder(),
573                                 &system_enable_referrers_);
574
575   if (command_line.HasSwitch(switches::kEnableClientHints))
576     network_delegate->SetEnableClientHints();
577
578 #if defined(ENABLE_EXTENSIONS)
579   if (command_line.HasSwitch(switches::kDisableExtensionsHttpThrottling))
580     network_delegate->NeverThrottleRequests();
581 #endif
582
583   globals_->system_network_delegate.reset(network_delegate);
584   globals_->host_resolver = CreateGlobalHostResolver(net_log_);
585   UpdateDnsClientEnabled();
586 #if defined(OS_CHROMEOS)
587   // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
588   globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
589       new chromeos::CertVerifyProcChromeOS()));
590 #else
591   globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
592       net::CertVerifyProc::CreateDefault()));
593 #endif
594
595   globals_->transport_security_state.reset(new net::TransportSecurityState());
596
597   net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
598   globals_->cert_transparency_verifier.reset(ct_verifier);
599
600   // Add built-in logs
601   ct_verifier->AddLogs(net::ct::CreateLogVerifiersForKnownLogs());
602
603   // Add logs from command line
604   if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
605     std::string switch_value = command_line.GetSwitchValueASCII(
606         switches::kCertificateTransparencyLog);
607     std::vector<std::string> logs;
608     base::SplitString(switch_value, ',', &logs);
609     for (std::vector<std::string>::iterator it = logs.begin(); it != logs.end();
610          ++it) {
611       const std::string& curr_log = *it;
612       size_t delim_pos = curr_log.find(":");
613       CHECK(delim_pos != std::string::npos)
614           << "CT log description not provided (switch format"
615              " is 'description:base64_key')";
616       std::string log_description(curr_log.substr(0, delim_pos));
617       std::string ct_public_key_data;
618       CHECK(base::Base64Decode(curr_log.substr(delim_pos + 1),
619                                &ct_public_key_data))
620           << "Unable to decode CT public key.";
621       scoped_ptr<net::CTLogVerifier> external_log_verifier(
622           net::CTLogVerifier::Create(ct_public_key_data, log_description));
623       CHECK(external_log_verifier) << "Unable to parse CT public key.";
624       VLOG(1) << "Adding log with description " << log_description;
625       ct_verifier->AddLog(external_log_verifier.Pass());
626     }
627   }
628
629   globals_->ssl_config_service = GetSSLConfigService();
630
631   SetupDataReductionProxy(network_delegate);
632
633   globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory(
634       globals_->host_resolver.get()));
635   globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
636   // For the ProxyScriptFetcher, we use a direct ProxyService.
637   globals_->proxy_script_fetcher_proxy_service.reset(
638       net::ProxyService::CreateDirectWithNetLog(net_log_));
639   // In-memory cookie store.
640   globals_->system_cookie_store =
641         content::CreateCookieStore(content::CookieStoreConfig());
642   // In-memory channel ID store.
643   globals_->system_channel_id_service.reset(
644       new net::ChannelIDService(
645           new net::DefaultChannelIDStore(NULL),
646           base::WorkerPool::GetTaskRunner(true)));
647   globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
648   globals_->host_mapping_rules.reset(new net::HostMappingRules());
649   globals_->http_user_agent_settings.reset(
650       new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
651   if (command_line.HasSwitch(switches::kHostRules)) {
652     TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
653     globals_->host_mapping_rules->SetRulesFromString(
654         command_line.GetSwitchValueASCII(switches::kHostRules));
655     TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
656   }
657   if (command_line.HasSwitch(switches::kEnableSSLConnectJobWaiting))
658     globals_->enable_ssl_connect_job_waiting = true;
659   if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
660     globals_->ignore_certificate_errors = true;
661   if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
662     globals_->testing_fixed_http_port =
663         GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
664   }
665   if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
666     globals_->testing_fixed_https_port =
667         GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
668   }
669   ConfigureQuic(command_line);
670   if (command_line.HasSwitch(
671           switches::kEnableUserAlternateProtocolPorts)) {
672     globals_->enable_user_alternate_protocol_ports = true;
673   }
674   InitializeNetworkOptions(command_line);
675
676   net::HttpNetworkSession::Params session_params;
677   InitializeNetworkSessionParams(&session_params);
678   session_params.net_log = net_log_;
679   session_params.proxy_service =
680       globals_->proxy_script_fetcher_proxy_service.get();
681
682   TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:HttpNetworkSession");
683   scoped_refptr<net::HttpNetworkSession> network_session(
684       new net::HttpNetworkSession(session_params));
685   globals_->proxy_script_fetcher_http_transaction_factory
686       .reset(new net::HttpNetworkLayer(network_session.get()));
687   TRACE_EVENT_END0("startup", "IOThread::InitAsync:HttpNetworkSession");
688   scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
689       new net::URLRequestJobFactoryImpl());
690   job_factory->SetProtocolHandler(url::kDataScheme,
691                                   new net::DataProtocolHandler());
692   job_factory->SetProtocolHandler(
693       url::kFileScheme,
694       new net::FileProtocolHandler(
695           content::BrowserThread::GetBlockingPool()->
696               GetTaskRunnerWithShutdownBehavior(
697                   base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
698 #if !defined(DISABLE_FTP_SUPPORT)
699   globals_->proxy_script_fetcher_ftp_transaction_factory.reset(
700       new net::FtpNetworkLayer(globals_->host_resolver.get()));
701   job_factory->SetProtocolHandler(
702       url::kFtpScheme,
703       new net::FtpProtocolHandler(
704           globals_->proxy_script_fetcher_ftp_transaction_factory.get()));
705 #endif
706   globals_->proxy_script_fetcher_url_request_job_factory =
707       job_factory.PassAs<net::URLRequestJobFactory>();
708
709   globals_->throttler_manager.reset(new net::URLRequestThrottlerManager());
710   globals_->throttler_manager->set_net_log(net_log_);
711   // Always done in production, disabled only for unit tests.
712   globals_->throttler_manager->set_enable_thread_checks(true);
713
714   globals_->proxy_script_fetcher_context.reset(
715       ConstructProxyScriptFetcherContext(globals_, net_log_));
716
717 #if defined(OS_MACOSX) && !defined(OS_IOS)
718   // Start observing Keychain events. This needs to be done on the UI thread,
719   // as Keychain services requires a CFRunLoop.
720   BrowserThread::PostTask(BrowserThread::UI,
721                           FROM_HERE,
722                           base::Bind(&ObserveKeychainEvents));
723 #endif
724
725   // InitSystemRequestContext turns right around and posts a task back
726   // to the IO thread, so we can't let it run until we know the IO
727   // thread has started.
728   //
729   // Note that since we are at BrowserThread::Init time, the UI thread
730   // is blocked waiting for the thread to start.  Therefore, posting
731   // this task to the main thread's message loop here is guaranteed to
732   // get it onto the message loop while the IOThread object still
733   // exists.  However, the message might not be processed on the UI
734   // thread until after IOThread is gone, so use a weak pointer.
735   BrowserThread::PostTask(BrowserThread::UI,
736                           FROM_HERE,
737                           base::Bind(&IOThread::InitSystemRequestContext,
738                                      weak_factory_.GetWeakPtr()));
739 }
740
741 void IOThread::CleanUp() {
742   base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
743
744 #if defined(USE_NSS) || defined(OS_IOS)
745   net::ShutdownNSSHttpIO();
746 #endif
747
748   system_url_request_context_getter_ = NULL;
749
750   // Release objects that the net::URLRequestContext could have been pointing
751   // to.
752
753   // Shutdown the HistogramWatcher on the IO thread.
754   net::NetworkChangeNotifier::ShutdownHistogramWatcher();
755
756   // This must be reset before the ChromeNetLog is destroyed.
757   network_change_observer_.reset();
758
759   system_proxy_config_service_.reset();
760
761   delete globals_;
762   globals_ = NULL;
763
764   base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
765 }
766
767 void IOThread::InitializeNetworkOptions(const CommandLine& command_line) {
768   // Only handle use-spdy command line flags if "spdy.disabled" preference is
769   // not disabled via policy.
770   if (is_spdy_disabled_by_policy_) {
771     base::FieldTrial* trial = base::FieldTrialList::Find(kSpdyFieldTrialName);
772     if (trial)
773       trial->Disable();
774   } else {
775     if (command_line.HasSwitch(switches::kTrustedSpdyProxy)) {
776       globals_->trusted_spdy_proxy.set(
777           command_line.GetSwitchValueASCII(switches::kTrustedSpdyProxy));
778     }
779     if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
780       net::URLFetcher::SetIgnoreCertificateRequests(true);
781
782     if (command_line.HasSwitch(switches::kUseSpdy)) {
783       std::string spdy_mode =
784           command_line.GetSwitchValueASCII(switches::kUseSpdy);
785       EnableSpdy(spdy_mode);
786     } else if (command_line.HasSwitch(switches::kEnableSpdy4)) {
787       globals_->next_protos = net::NextProtosSpdy4Http2();
788       globals_->use_alternate_protocols.set(true);
789     } else if (command_line.HasSwitch(switches::kDisableSpdy31)) {
790       globals_->next_protos = net::NextProtosSpdy3();
791       globals_->use_alternate_protocols.set(true);
792     } else if (command_line.HasSwitch(switches::kEnableNpnHttpOnly)) {
793       globals_->next_protos = net::NextProtosHttpOnly();
794       globals_->use_alternate_protocols.set(false);
795     } else if (command_line.HasSwitch(switches::kEnableWebSocketOverSpdy)) {
796       // Use the current SPDY default (SPDY/3.1).
797       globals_->next_protos = net::NextProtosSpdy31();
798       globals_->use_alternate_protocols.set(true);
799     } else {
800       // No SPDY command-line flags have been specified. Examine trial groups.
801       ConfigureSpdyFromTrial(
802           base::FieldTrialList::FindFullName(kSpdyFieldTrialName), globals_);
803     }
804
805     if (command_line.HasSwitch(switches::kEnableWebSocketOverSpdy))
806       globals_->enable_websocket_over_spdy.set(true);
807   }
808
809   ConfigureTCPFastOpen(command_line);
810
811   // TODO(rch): Make the client socket factory a per-network session
812   // instance, constructed from a NetworkSession::Params, to allow us
813   // to move this option to IOThread::Globals &
814   // HttpNetworkSession::Params.
815 }
816
817 void IOThread::ConfigureTCPFastOpen(const CommandLine& command_line) {
818   const std::string trial_group =
819       base::FieldTrialList::FindFullName(kTCPFastOpenFieldTrialName);
820   if (trial_group == kTCPFastOpenHttpsEnabledGroupName)
821     globals_->enable_tcp_fast_open_for_ssl.set(true);
822   bool always_enable_if_supported =
823       command_line.HasSwitch(switches::kEnableTcpFastOpen);
824   // Check for OS support of TCP FastOpen, and turn it on for all connections
825   // if indicated by user.
826   net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_if_supported);
827 }
828
829 void IOThread::ConfigureSpdyFromTrial(const std::string& spdy_trial_group,
830                                       Globals* globals) {
831   if (spdy_trial_group == kSpdyFieldTrialHoldbackGroupName) {
832     // TODO(jgraettinger): Use net::NextProtosHttpOnly() instead?
833     net::HttpStreamFactory::set_spdy_enabled(false);
834   } else if (spdy_trial_group == kSpdyFieldTrialHoldbackControlGroupName) {
835     // Use the current SPDY default (SPDY/3.1).
836     globals->next_protos = net::NextProtosSpdy31();
837     globals->use_alternate_protocols.set(true);
838   } else if (spdy_trial_group == kSpdyFieldTrialSpdy4GroupName) {
839     globals->next_protos = net::NextProtosSpdy4Http2();
840     globals->use_alternate_protocols.set(true);
841   } else if (spdy_trial_group == kSpdyFieldTrialSpdy4ControlGroupName) {
842     // This control group is pinned at SPDY/3.1.
843     globals->next_protos = net::NextProtosSpdy31();
844     globals->use_alternate_protocols.set(true);
845   } else {
846     // Use the current SPDY default (SPDY/3.1).
847     globals->next_protos = net::NextProtosSpdy31();
848     globals->use_alternate_protocols.set(true);
849   }
850 }
851
852 void IOThread::EnableSpdy(const std::string& mode) {
853   static const char kOff[] = "off";
854   static const char kSSL[] = "ssl";
855   static const char kDisableSSL[] = "no-ssl";
856   static const char kDisablePing[] = "no-ping";
857   static const char kExclude[] = "exclude";  // Hosts to exclude
858   static const char kDisableCompression[] = "no-compress";
859   static const char kDisableAltProtocols[] = "no-alt-protocols";
860   static const char kForceAltProtocols[] = "force-alt-protocols";
861   static const char kSingleDomain[] = "single-domain";
862
863   static const char kInitialMaxConcurrentStreams[] = "init-max-streams";
864
865   std::vector<std::string> spdy_options;
866   base::SplitString(mode, ',', &spdy_options);
867
868   for (std::vector<std::string>::iterator it = spdy_options.begin();
869        it != spdy_options.end(); ++it) {
870     const std::string& element = *it;
871     std::vector<std::string> name_value;
872     base::SplitString(element, '=', &name_value);
873     const std::string& option =
874         name_value.size() > 0 ? name_value[0] : std::string();
875     const std::string value =
876         name_value.size() > 1 ? name_value[1] : std::string();
877
878     if (option == kOff) {
879       net::HttpStreamFactory::set_spdy_enabled(false);
880     } else if (option == kDisableSSL) {
881       globals_->spdy_default_protocol.set(net::kProtoSPDY3);
882       globals_->force_spdy_over_ssl.set(false);
883       globals_->force_spdy_always.set(true);
884     } else if (option == kSSL) {
885       globals_->spdy_default_protocol.set(net::kProtoSPDY3);
886       globals_->force_spdy_over_ssl.set(true);
887       globals_->force_spdy_always.set(true);
888     } else if (option == kDisablePing) {
889       globals_->enable_spdy_ping_based_connection_checking.set(false);
890     } else if (option == kExclude) {
891       globals_->forced_spdy_exclusions.insert(
892           net::HostPortPair::FromURL(GURL(value)));
893     } else if (option == kDisableCompression) {
894       globals_->enable_spdy_compression.set(false);
895     } else if (option == kDisableAltProtocols) {
896       globals_->use_alternate_protocols.set(false);
897     } else if (option == kForceAltProtocols) {
898       net::AlternateProtocolInfo pair(443, net::NPN_SPDY_3, 1);
899       net::HttpServerPropertiesImpl::ForceAlternateProtocol(pair);
900     } else if (option == kSingleDomain) {
901       DVLOG(1) << "FORCING SINGLE DOMAIN";
902       globals_->force_spdy_single_domain.set(true);
903     } else if (option == kInitialMaxConcurrentStreams) {
904       int streams;
905       if (base::StringToInt(value, &streams))
906         globals_->initial_max_spdy_concurrent_streams.set(streams);
907     } else if (option.empty() && it == spdy_options.begin()) {
908       continue;
909     } else {
910       LOG(DFATAL) << "Unrecognized spdy option: " << option;
911     }
912   }
913 }
914
915 // static
916 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
917   registry->RegisterStringPref(prefs::kAuthSchemes,
918                                "basic,digest,ntlm,negotiate,"
919                                "spdyproxy");
920   registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
921   registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
922   registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
923   registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
924                                std::string());
925   registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
926   registry->RegisterStringPref(
927       data_reduction_proxy::prefs::kDataReductionProxy, std::string());
928   registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
929   data_reduction_proxy::RegisterPrefs(registry);
930   registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
931   registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
932 }
933
934 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
935     net::HostResolver* resolver) {
936   net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
937   if (!auth_server_whitelist_.empty()) {
938     auth_filter_default_credentials =
939         new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
940   }
941   net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
942   if (!auth_delegate_whitelist_.empty()) {
943     auth_filter_delegate =
944         new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
945   }
946   globals_->url_security_manager.reset(
947       net::URLSecurityManager::Create(auth_filter_default_credentials,
948                                       auth_filter_delegate));
949   std::vector<std::string> supported_schemes;
950   base::SplitString(auth_schemes_, ',', &supported_schemes);
951
952   scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
953       net::HttpAuthHandlerRegistryFactory::Create(
954           supported_schemes, globals_->url_security_manager.get(),
955           resolver, gssapi_library_name_, negotiate_disable_cname_lookup_,
956           negotiate_enable_port_));
957   return registry_factory.release();
958 }
959
960 void IOThread::ClearHostCache() {
961   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
962
963   net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
964   if (host_cache)
965     host_cache->clear();
966 }
967
968 void IOThread::InitializeNetworkSessionParams(
969     net::HttpNetworkSession::Params* params) {
970   InitializeNetworkSessionParamsFromGlobals(*globals_, params);
971 }
972
973 // static
974 void IOThread::InitializeNetworkSessionParamsFromGlobals(
975     const IOThread::Globals& globals,
976     net::HttpNetworkSession::Params* params) {
977   params->host_resolver = globals.host_resolver.get();
978   params->cert_verifier = globals.cert_verifier.get();
979   params->channel_id_service = globals.system_channel_id_service.get();
980   params->transport_security_state = globals.transport_security_state.get();
981   params->ssl_config_service = globals.ssl_config_service.get();
982   params->http_auth_handler_factory = globals.http_auth_handler_factory.get();
983   params->http_server_properties =
984       globals.http_server_properties->GetWeakPtr();
985   params->network_delegate = globals.system_network_delegate.get();
986   params->host_mapping_rules = globals.host_mapping_rules.get();
987   params->enable_ssl_connect_job_waiting =
988       globals.enable_ssl_connect_job_waiting;
989   params->ignore_certificate_errors = globals.ignore_certificate_errors;
990   params->testing_fixed_http_port = globals.testing_fixed_http_port;
991   params->testing_fixed_https_port = globals.testing_fixed_https_port;
992   globals.enable_tcp_fast_open_for_ssl.CopyToIfSet(
993       &params->enable_tcp_fast_open_for_ssl);
994
995   globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
996       &params->spdy_initial_max_concurrent_streams);
997   globals.force_spdy_single_domain.CopyToIfSet(
998       &params->force_spdy_single_domain);
999   globals.enable_spdy_compression.CopyToIfSet(
1000       &params->enable_spdy_compression);
1001   globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1002       &params->enable_spdy_ping_based_connection_checking);
1003   globals.spdy_default_protocol.CopyToIfSet(
1004       &params->spdy_default_protocol);
1005   params->next_protos = globals.next_protos;
1006   globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1007   globals.force_spdy_over_ssl.CopyToIfSet(&params->force_spdy_over_ssl);
1008   globals.force_spdy_always.CopyToIfSet(&params->force_spdy_always);
1009   params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1010   globals.use_alternate_protocols.CopyToIfSet(
1011       &params->use_alternate_protocols);
1012   globals.alternate_protocol_probability_threshold.CopyToIfSet(
1013       &params->alternate_protocol_probability_threshold);
1014   globals.enable_websocket_over_spdy.CopyToIfSet(
1015       &params->enable_websocket_over_spdy);
1016
1017   globals.enable_quic.CopyToIfSet(&params->enable_quic);
1018   globals.enable_quic_time_based_loss_detection.CopyToIfSet(
1019       &params->enable_quic_time_based_loss_detection);
1020   globals.quic_always_require_handshake_confirmation.CopyToIfSet(
1021       &params->quic_always_require_handshake_confirmation);
1022   globals.quic_disable_connection_pooling.CopyToIfSet(
1023       &params->quic_disable_connection_pooling);
1024   globals.enable_quic_port_selection.CopyToIfSet(
1025       &params->enable_quic_port_selection);
1026   globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1027   globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1028   globals.quic_supported_versions.CopyToIfSet(
1029       &params->quic_supported_versions);
1030   params->quic_connection_options = globals.quic_connection_options;
1031
1032   globals.origin_to_force_quic_on.CopyToIfSet(
1033       &params->origin_to_force_quic_on);
1034   params->enable_user_alternate_protocol_ports =
1035       globals.enable_user_alternate_protocol_ports;
1036   params->proxy_delegate = globals.data_reduction_proxy_delegate.get();
1037 }
1038
1039 base::TimeTicks IOThread::creation_time() const {
1040   return creation_time_;
1041 }
1042
1043 net::SSLConfigService* IOThread::GetSSLConfigService() {
1044   return ssl_config_service_manager_->Get();
1045 }
1046
1047 void IOThread::ChangedToOnTheRecordOnIOThread() {
1048   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1049
1050   // Clear the host cache to avoid showing entries from the OTR session
1051   // in about:net-internals.
1052   ClearHostCache();
1053 }
1054
1055 void IOThread::InitSystemRequestContext() {
1056   if (system_url_request_context_getter_.get())
1057     return;
1058   // If we're in unit_tests, IOThread may not be run.
1059   if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1060     return;
1061   system_proxy_config_service_.reset(
1062       ProxyServiceFactory::CreateProxyConfigService(
1063           pref_proxy_config_tracker_.get()));
1064   system_url_request_context_getter_ =
1065       new SystemURLRequestContextGetter(this);
1066   // Safe to post an unretained this pointer, since IOThread is
1067   // guaranteed to outlive the IO BrowserThread.
1068   BrowserThread::PostTask(
1069       BrowserThread::IO,
1070       FROM_HERE,
1071       base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1072                  base::Unretained(this)));
1073 }
1074
1075 void IOThread::InitSystemRequestContextOnIOThread() {
1076   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1077   DCHECK(!globals_->system_proxy_service.get());
1078   DCHECK(system_proxy_config_service_.get());
1079
1080   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
1081   globals_->system_proxy_service.reset(
1082       ProxyServiceFactory::CreateProxyService(
1083           net_log_,
1084           globals_->proxy_script_fetcher_context.get(),
1085           globals_->system_network_delegate.get(),
1086           system_proxy_config_service_.release(),
1087           command_line,
1088           quick_check_enabled_.GetValue()));
1089   DCHECK(globals_->data_reduction_proxy_params);
1090
1091   net::HttpNetworkSession::Params system_params;
1092   InitializeNetworkSessionParams(&system_params);
1093   system_params.net_log = net_log_;
1094   system_params.proxy_service = globals_->system_proxy_service.get();
1095
1096   globals_->system_http_transaction_factory.reset(
1097       new net::HttpNetworkLayer(
1098           new net::HttpNetworkSession(system_params)));
1099   globals_->system_url_request_job_factory.reset(
1100       new net::URLRequestJobFactoryImpl());
1101   globals_->system_request_context.reset(
1102       ConstructSystemRequestContext(globals_, net_log_));
1103   globals_->system_request_context->set_ssl_config_service(
1104       globals_->ssl_config_service.get());
1105   globals_->system_request_context->set_http_server_properties(
1106       globals_->http_server_properties->GetWeakPtr());
1107 }
1108
1109 void IOThread::UpdateDnsClientEnabled() {
1110   globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1111 }
1112
1113 void IOThread::ConfigureQuic(const CommandLine& command_line) {
1114   // Always fetch the field trial group to ensure it is reported correctly.
1115   // The command line flags will be associated with a group that is reported
1116   // so long as trial is actually queried.
1117   std::string group =
1118       base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1119   VariationParameters params;
1120   if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1121     params.clear();
1122   }
1123
1124   ConfigureQuicGlobals(command_line, group, params, globals_);
1125 }
1126
1127 void IOThread::SetupDataReductionProxy(
1128     ChromeNetworkDelegate* network_delegate) {
1129   // TODO(kundaji): Move flags initialization to DataReductionProxyParams and
1130   // merge with flag initialization in
1131   // data_reduction_proxy_chrome_settings_factory.cc.
1132   int flags = data_reduction_proxy::DataReductionProxyParams::kAllowed |
1133       data_reduction_proxy::DataReductionProxyParams::kFallbackAllowed |
1134       data_reduction_proxy::DataReductionProxyParams::kAlternativeAllowed;
1135   if (data_reduction_proxy::DataReductionProxyParams::
1136       IsIncludedInPromoFieldTrial()) {
1137     flags |= data_reduction_proxy::DataReductionProxyParams::kPromoAllowed;
1138   }
1139   if (data_reduction_proxy::DataReductionProxyParams::
1140       IsIncludedInHoldbackFieldTrial()) {
1141     flags |= data_reduction_proxy::DataReductionProxyParams::kHoldback;
1142   }
1143   globals_->data_reduction_proxy_params.reset(
1144       new data_reduction_proxy::DataReductionProxyParams(flags));
1145   globals_->data_reduction_proxy_auth_request_handler.reset(
1146       new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
1147           DataReductionProxyChromeSettings::GetClient(),
1148           globals_->data_reduction_proxy_params.get(),
1149           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
1150   globals_->data_reduction_proxy_delegate.reset(
1151       new data_reduction_proxy::DataReductionProxyDelegate(
1152           globals_->data_reduction_proxy_auth_request_handler.get()));
1153   // This is the same as in ProfileImplIOData except that we do not collect
1154   // usage stats.
1155   network_delegate->set_data_reduction_proxy_params(
1156       globals_->data_reduction_proxy_params.get());
1157   network_delegate->set_data_reduction_proxy_auth_request_handler(
1158       globals_->data_reduction_proxy_auth_request_handler.get());
1159   network_delegate->set_on_resolve_proxy_handler(
1160       base::Bind(data_reduction_proxy::OnResolveProxyHandler));
1161 }
1162
1163 // static
1164 void IOThread::ConfigureQuicGlobals(
1165     const base::CommandLine& command_line,
1166     base::StringPiece quic_trial_group,
1167     const VariationParameters& quic_trial_params,
1168     IOThread::Globals* globals) {
1169   bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group);
1170   globals->enable_quic.set(enable_quic);
1171   if (enable_quic) {
1172     globals->enable_quic_time_based_loss_detection.set(
1173         ShouldEnableQuicTimeBasedLossDetection(command_line, quic_trial_group,
1174                                                quic_trial_params));
1175     globals->quic_always_require_handshake_confirmation.set(
1176         ShouldQuicAlwaysRequireHandshakeConfirmation(quic_trial_params));
1177     globals->quic_disable_connection_pooling.set(
1178         ShouldQuicDisableConnectionPooling(quic_trial_params));
1179     globals->enable_quic_port_selection.set(
1180         ShouldEnableQuicPortSelection(command_line));
1181     globals->quic_connection_options =
1182         GetQuicConnectionOptions(command_line, quic_trial_params);
1183     if (ShouldEnableQuicPacing(command_line, quic_trial_group,
1184                                quic_trial_params)) {
1185       globals->quic_connection_options.push_back(net::kPACE);
1186     }
1187   }
1188
1189   size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1190                                                     quic_trial_group,
1191                                                     quic_trial_params);
1192   if (max_packet_length != 0) {
1193     globals->quic_max_packet_length.set(max_packet_length);
1194   }
1195
1196   std::string quic_user_agent_id =
1197       chrome::VersionInfo::GetVersionStringModifier();
1198   if (!quic_user_agent_id.empty())
1199     quic_user_agent_id.push_back(' ');
1200   chrome::VersionInfo version_info;
1201   quic_user_agent_id.append(version_info.ProductNameAndVersionForUserAgent());
1202   globals->quic_user_agent_id.set(quic_user_agent_id);
1203
1204   net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1205   if (version != net::QUIC_VERSION_UNSUPPORTED) {
1206     net::QuicVersionVector supported_versions;
1207     supported_versions.push_back(version);
1208     globals->quic_supported_versions.set(supported_versions);
1209   }
1210
1211   double threshold =
1212       GetAlternateProtocolProbabilityThreshold(command_line, quic_trial_params);
1213   if (threshold >=0 && threshold <= 1) {
1214     globals->alternate_protocol_probability_threshold.set(threshold);
1215     globals->http_server_properties->SetAlternateProtocolProbabilityThreshold(
1216         threshold);
1217   }
1218
1219   if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1220     net::HostPortPair quic_origin =
1221         net::HostPortPair::FromString(
1222             command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1223     if (!quic_origin.IsEmpty()) {
1224       globals->origin_to_force_quic_on.set(quic_origin);
1225     }
1226   }
1227 }
1228
1229 bool IOThread::ShouldEnableQuic(const CommandLine& command_line,
1230                                 base::StringPiece quic_trial_group) {
1231   if (command_line.HasSwitch(switches::kDisableQuic))
1232     return false;
1233
1234   if (command_line.HasSwitch(switches::kEnableQuic))
1235     return true;
1236
1237   return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1238       quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1239 }
1240
1241 bool IOThread::ShouldEnableQuicPortSelection(
1242       const CommandLine& command_line) {
1243   if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1244     return false;
1245
1246   if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1247     return true;
1248
1249   return false;  // Default to disabling port selection on all channels.
1250 }
1251
1252 bool IOThread::ShouldEnableQuicPacing(
1253     const CommandLine& command_line,
1254     base::StringPiece quic_trial_group,
1255     const VariationParameters& quic_trial_params) {
1256   if (command_line.HasSwitch(switches::kEnableQuicPacing))
1257     return true;
1258
1259   if (command_line.HasSwitch(switches::kDisableQuicPacing))
1260     return false;
1261
1262   if (LowerCaseEqualsASCII(
1263       GetVariationParam(quic_trial_params, "enable_pacing"),
1264       "true"))
1265     return true;
1266
1267   return quic_trial_group.ends_with(kQuicFieldTrialPacingSuffix);
1268 }
1269
1270 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1271     const CommandLine& command_line,
1272     const VariationParameters& quic_trial_params) {
1273   if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1274     return ParseQuicConnectionOptions(
1275         command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1276   }
1277
1278   VariationParameters::const_iterator it =
1279       quic_trial_params.find("connection_options");
1280   if (it == quic_trial_params.end()) {
1281     // TODO(rch): remove support for deprecated congestion_options.
1282     it = quic_trial_params.find("congestion_options");
1283     if (it == quic_trial_params.end())
1284       return net::QuicTagVector();
1285   }
1286
1287   return ParseQuicConnectionOptions(it->second);
1288 }
1289
1290 // static
1291 net::QuicTagVector IOThread::ParseQuicConnectionOptions(
1292     const std::string& connection_options) {
1293   net::QuicTagVector options;
1294   std::vector<std::string> tokens;
1295   base::SplitString(connection_options, ',', &tokens);
1296   // Tokens are expected to be no more than 4 characters long, but we
1297   // handle overflow gracefully.
1298   for (std::vector<std::string>::iterator token = tokens.begin();
1299        token != tokens.end(); ++token) {
1300     uint32 option = 0;
1301     for (size_t i = token->length() ; i > 0; --i) {
1302       option <<= 8;
1303       option |= static_cast<unsigned char>((*token)[i - 1]);
1304     }
1305     options.push_back(static_cast<net::QuicTag>(option));
1306   }
1307   return options;
1308 }
1309
1310 // static
1311 double IOThread::GetAlternateProtocolProbabilityThreshold(
1312     const base::CommandLine& command_line,
1313     const VariationParameters& quic_trial_params) {
1314   double value;
1315   if (command_line.HasSwitch(
1316           switches::kAlternateProtocolProbabilityThreshold)) {
1317     if (base::StringToDouble(
1318             command_line.GetSwitchValueASCII(
1319                 switches::kAlternateProtocolProbabilityThreshold),
1320             &value)) {
1321       return value;
1322     }
1323   }
1324   if (command_line.HasSwitch(switches::kEnableQuic)) {
1325     return 0;
1326   }
1327   if (base::StringToDouble(
1328           GetVariationParam(quic_trial_params,
1329                             "alternate_protocol_probability_threshold"),
1330           &value)) {
1331     return value;
1332   }
1333   return -1;
1334 }
1335
1336 // static
1337 bool IOThread::ShouldEnableQuicTimeBasedLossDetection(
1338     const CommandLine& command_line,
1339     base::StringPiece quic_trial_group,
1340     const VariationParameters& quic_trial_params) {
1341   if (command_line.HasSwitch(switches::kEnableQuicTimeBasedLossDetection))
1342     return true;
1343
1344   if (command_line.HasSwitch(switches::kDisableQuicTimeBasedLossDetection))
1345     return false;
1346
1347   if (LowerCaseEqualsASCII(
1348       GetVariationParam(quic_trial_params, "enable_time_based_loss_detection"),
1349       "true"))
1350     return true;
1351
1352   return quic_trial_group.ends_with(
1353       kQuicFieldTrialTimeBasedLossDetectionSuffix);
1354 }
1355
1356 // static
1357 bool IOThread::ShouldQuicAlwaysRequireHandshakeConfirmation(
1358     const VariationParameters& quic_trial_params) {
1359   return LowerCaseEqualsASCII(
1360       GetVariationParam(quic_trial_params,
1361                         "always_require_handshake_confirmation"),
1362       "true");
1363 }
1364
1365 // static
1366 bool IOThread::ShouldQuicDisableConnectionPooling(
1367     const VariationParameters& quic_trial_params) {
1368   return LowerCaseEqualsASCII(
1369       GetVariationParam(quic_trial_params, "disable_connection_pooling"),
1370       "true");
1371 }
1372
1373 // static
1374 size_t IOThread::GetQuicMaxPacketLength(
1375     const CommandLine& command_line,
1376     base::StringPiece quic_trial_group,
1377     const VariationParameters& quic_trial_params) {
1378   if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1379     unsigned value;
1380     if (!base::StringToUint(
1381             command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1382             &value)) {
1383       return 0;
1384     }
1385     return value;
1386   }
1387
1388   unsigned value;
1389   if (base::StringToUint(GetVariationParam(quic_trial_params,
1390                                            "max_packet_length"),
1391                          &value)) {
1392     return value;
1393   }
1394
1395   // Format of the packet length group names is:
1396   //   (Https)?Enabled<length>BytePackets.
1397   base::StringPiece length_str(quic_trial_group);
1398   if (length_str.starts_with(kQuicFieldTrialEnabledGroupName)) {
1399     length_str.remove_prefix(strlen(kQuicFieldTrialEnabledGroupName));
1400   } else if (length_str.starts_with(kQuicFieldTrialHttpsEnabledGroupName)) {
1401     length_str.remove_prefix(strlen(kQuicFieldTrialHttpsEnabledGroupName));
1402   } else {
1403     return 0;
1404   }
1405   if (!length_str.ends_with(kQuicFieldTrialPacketLengthSuffix)) {
1406     return 0;
1407   }
1408   length_str.remove_suffix(strlen(kQuicFieldTrialPacketLengthSuffix));
1409   if (!base::StringToUint(length_str, &value)) {
1410     return 0;
1411   }
1412   return value;
1413 }
1414
1415 // static
1416 net::QuicVersion IOThread::GetQuicVersion(
1417     const CommandLine& command_line,
1418     const VariationParameters& quic_trial_params) {
1419   if (command_line.HasSwitch(switches::kQuicVersion)) {
1420     return ParseQuicVersion(
1421         command_line.GetSwitchValueASCII(switches::kQuicVersion));
1422   }
1423
1424   return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1425 }
1426
1427 // static
1428 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1429   net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1430   for (size_t i = 0; i < supported_versions.size(); ++i) {
1431     net::QuicVersion version = supported_versions[i];
1432     if (net::QuicVersionToString(version) == quic_version) {
1433       return version;
1434     }
1435   }
1436
1437   return net::QUIC_VERSION_UNSUPPORTED;
1438 }