[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 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 "content/shell/shell_url_request_context_getter.h" |
| 6 | |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | cbfe749 | 2012-10-03 18:40:42 | [diff] [blame] | 9 | #include "base/string_number_conversions.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 10 | #include "base/string_util.h" |
[email protected] | 27c0573 | 2013-02-15 21:55:49 | [diff] [blame] | 11 | #include "base/strings/string_split.h" |
[email protected] | 5bab49ec | 2012-05-04 21:13:19 | [diff] [blame] | 12 | #include "base/threading/worker_pool.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 13 | #include "content/public/browser/browser_thread.h" |
[email protected] | cbfe749 | 2012-10-03 18:40:42 | [diff] [blame] | 14 | #include "content/public/common/content_switches.h" |
[email protected] | 6bd3007 | 2013-02-08 18:17:11 | [diff] [blame] | 15 | #include "content/public/common/url_constants.h" |
[email protected] | 33bc283 | 2012-03-29 08:18:17 | [diff] [blame] | 16 | #include "content/shell/shell_network_delegate.h" |
[email protected] | 6a2a489 | 2013-01-20 01:19:38 | [diff] [blame] | 17 | #include "content/shell/shell_switches.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 18 | #include "net/cert/cert_verifier.h" |
[email protected] | 48839bfc | 2012-03-16 18:19:11 | [diff] [blame] | 19 | #include "net/cookies/cookie_monster.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 20 | #include "net/dns/host_resolver.h" |
| 21 | #include "net/dns/mapped_host_resolver.h" |
[email protected] | 1e528d5 | 2013-03-21 21:38:01 | [diff] [blame] | 22 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 23 | #include "net/http/http_auth_handler_factory.h" |
| 24 | #include "net/http/http_cache.h" |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 25 | #include "net/http/http_network_session.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 26 | #include "net/http/http_server_properties_impl.h" |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 27 | #include "net/proxy/proxy_service.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 28 | #include "net/ssl/default_server_bound_cert_store.h" |
| 29 | #include "net/ssl/server_bound_cert_service.h" |
| 30 | #include "net/ssl/ssl_config_service_defaults.h" |
[email protected] | 6bd3007 | 2013-02-08 18:17:11 | [diff] [blame] | 31 | #include "net/url_request/protocol_intercept_job_factory.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 32 | #include "net/url_request/static_http_user_agent_settings.h" |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 33 | #include "net/url_request/url_request_context.h" |
[email protected] | 98cce3ae | 2011-10-15 16:19:41 | [diff] [blame] | 34 | #include "net/url_request/url_request_context_storage.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 35 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 36 | |
| 37 | namespace content { |
| 38 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 39 | namespace { |
| 40 | |
| 41 | void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory, |
| 42 | ProtocolHandlerMap* protocol_handlers) { |
| 43 | for (ProtocolHandlerMap::iterator it = |
| 44 | protocol_handlers->begin(); |
| 45 | it != protocol_handlers->end(); |
| 46 | ++it) { |
| 47 | bool set_protocol = job_factory->SetProtocolHandler( |
| 48 | it->first, it->second.release()); |
| 49 | DCHECK(set_protocol); |
| 50 | } |
| 51 | protocol_handlers->clear(); |
| 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 56 | ShellURLRequestContextGetter::ShellURLRequestContextGetter( |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 57 | bool ignore_certificate_errors, |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 58 | const base::FilePath& base_path, |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 59 | MessageLoop* io_loop, |
[email protected] | 6bd3007 | 2013-02-08 18:17:11 | [diff] [blame] | 60 | MessageLoop* file_loop, |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 61 | ProtocolHandlerMap* protocol_handlers) |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 62 | : ignore_certificate_errors_(ignore_certificate_errors), |
| 63 | base_path_(base_path), |
[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame] | 64 | io_loop_(io_loop), |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 65 | file_loop_(file_loop) { |
[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame] | 66 | // Must first be created on the UI thread. |
| 67 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 68 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 69 | std::swap(protocol_handlers_, *protocol_handlers); |
| 70 | |
[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame] | 71 | // We must create the proxy config service on the UI loop on Linux because it |
| 72 | // must synchronously run on the glib message loop. This will be passed to |
| 73 | // the URLRequestContextStorage on the IO thread in GetURLRequestContext(). |
[email protected] | 6a2a489 | 2013-01-20 01:19:38 | [diff] [blame] | 74 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { |
| 75 | proxy_config_service_.reset( |
| 76 | net::ProxyService::CreateSystemProxyConfigService( |
| 77 | io_loop_->message_loop_proxy(), file_loop_)); |
| 78 | } |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | ShellURLRequestContextGetter::~ShellURLRequestContextGetter() { |
| 82 | } |
| 83 | |
| 84 | net::URLRequestContext* ShellURLRequestContextGetter::GetURLRequestContext() { |
| 85 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 86 | |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 87 | if (!url_request_context_) { |
[email protected] | cbfe749 | 2012-10-03 18:40:42 | [diff] [blame] | 88 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 89 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 90 | url_request_context_.reset(new net::URLRequestContext()); |
[email protected] | 33bc283 | 2012-03-29 08:18:17 | [diff] [blame] | 91 | network_delegate_.reset(new ShellNetworkDelegate); |
[email protected] | 833b700d | 2013-02-18 19:43:00 | [diff] [blame] | 92 | if (command_line.HasSwitch(switches::kDumpRenderTree)) |
| 93 | ShellNetworkDelegate::SetAcceptAllCookies(false); |
[email protected] | 33bc283 | 2012-03-29 08:18:17 | [diff] [blame] | 94 | url_request_context_->set_network_delegate(network_delegate_.get()); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 95 | storage_.reset( |
| 96 | new net::URLRequestContextStorage(url_request_context_.get())); |
[email protected] | 98cce3ae | 2011-10-15 16:19:41 | [diff] [blame] | 97 | storage_->set_cookie_store(new net::CookieMonster(NULL, NULL)); |
[email protected] | 9c4eff2 | 2012-03-20 22:42:29 | [diff] [blame] | 98 | storage_->set_server_bound_cert_service(new net::ServerBoundCertService( |
[email protected] | 5bab49ec | 2012-05-04 21:13:19 | [diff] [blame] | 99 | new net::DefaultServerBoundCertStore(NULL), |
| 100 | base::WorkerPool::GetTaskRunner(true))); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 101 | storage_->set_http_user_agent_settings( |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 102 | new net::StaticHttpUserAgentSettings("en-us,en", EmptyString())); |
[email protected] | 98cce3ae | 2011-10-15 16:19:41 | [diff] [blame] | 103 | |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 104 | scoped_ptr<net::HostResolver> host_resolver( |
| 105 | net::HostResolver::CreateDefaultResolver(NULL)); |
| 106 | |
[email protected] | 9f59fac | 2012-03-21 23:18:11 | [diff] [blame] | 107 | storage_->set_cert_verifier(net::CertVerifier::CreateDefault()); |
[email protected] | 6a2a489 | 2013-01-20 01:19:38 | [diff] [blame] | 108 | if (command_line.HasSwitch(switches::kDumpRenderTree)) { |
| 109 | storage_->set_proxy_service(net::ProxyService::CreateDirect()); |
| 110 | } else { |
| 111 | // TODO(jam): use v8 if possible, look at chrome code. |
| 112 | storage_->set_proxy_service( |
| 113 | net::ProxyService::CreateUsingSystemProxyResolver( |
| 114 | proxy_config_service_.release(), |
| 115 | 0, |
| 116 | NULL)); |
| 117 | } |
[email protected] | 98cce3ae | 2011-10-15 16:19:41 | [diff] [blame] | 118 | storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults); |
| 119 | storage_->set_http_auth_handler_factory( |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 120 | net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); |
[email protected] | 98cce3ae | 2011-10-15 16:19:41 | [diff] [blame] | 121 | storage_->set_http_server_properties(new net::HttpServerPropertiesImpl); |
| 122 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 123 | base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache")); |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 124 | net::HttpCache::DefaultBackend* main_backend = |
| 125 | new net::HttpCache::DefaultBackend( |
| 126 | net::DISK_CACHE, |
| 127 | cache_path, |
| 128 | 0, |
| 129 | BrowserThread::GetMessageLoopProxyForThread( |
| 130 | BrowserThread::CACHE)); |
| 131 | |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 132 | net::HttpNetworkSession::Params network_session_params; |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 133 | network_session_params.cert_verifier = |
| 134 | url_request_context_->cert_verifier(); |
| 135 | network_session_params.server_bound_cert_service = |
| 136 | url_request_context_->server_bound_cert_service(); |
| 137 | network_session_params.proxy_service = |
| 138 | url_request_context_->proxy_service(); |
| 139 | network_session_params.ssl_config_service = |
| 140 | url_request_context_->ssl_config_service(); |
| 141 | network_session_params.http_auth_handler_factory = |
| 142 | url_request_context_->http_auth_handler_factory(); |
| 143 | network_session_params.network_delegate = |
[email protected] | b9d4bfd | 2013-03-13 11:26:17 | [diff] [blame] | 144 | network_delegate_.get(); |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 145 | network_session_params.http_server_properties = |
| 146 | url_request_context_->http_server_properties(); |
| 147 | network_session_params.ignore_certificate_errors = |
| 148 | ignore_certificate_errors_; |
[email protected] | cbfe749 | 2012-10-03 18:40:42 | [diff] [blame] | 149 | if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) { |
| 150 | int value; |
| 151 | base::StringToInt(command_line.GetSwitchValueASCII( |
| 152 | switches::kTestingFixedHttpPort), &value); |
| 153 | network_session_params.testing_fixed_http_port = value; |
| 154 | } |
| 155 | if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) { |
| 156 | int value; |
| 157 | base::StringToInt(command_line.GetSwitchValueASCII( |
| 158 | switches::kTestingFixedHttpsPort), &value); |
| 159 | network_session_params.testing_fixed_https_port = value; |
| 160 | } |
| 161 | if (command_line.HasSwitch(switches::kHostResolverRules)) { |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 162 | scoped_ptr<net::MappedHostResolver> mapped_host_resolver( |
| 163 | new net::MappedHostResolver(host_resolver.Pass())); |
| 164 | mapped_host_resolver->SetRulesFromString( |
[email protected] | cbfe749 | 2012-10-03 18:40:42 | [diff] [blame] | 165 | command_line.GetSwitchValueASCII(switches::kHostResolverRules)); |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 166 | host_resolver = mapped_host_resolver.Pass(); |
[email protected] | cbfe749 | 2012-10-03 18:40:42 | [diff] [blame] | 167 | } |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 168 | |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 169 | // Give |storage_| ownership at the end in case it's |mapped_host_resolver|. |
| 170 | storage_->set_host_resolver(host_resolver.Pass()); |
| 171 | network_session_params.host_resolver = |
| 172 | url_request_context_->host_resolver(); |
| 173 | |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 174 | net::HttpCache* main_cache = new net::HttpCache( |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 175 | network_session_params, main_backend); |
[email protected] | 98cce3ae | 2011-10-15 16:19:41 | [diff] [blame] | 176 | storage_->set_http_transaction_factory(main_cache); |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 177 | |
[email protected] | 1e528d5 | 2013-03-21 21:38:01 | [diff] [blame] | 178 | #if !defined(DISABLE_FTP_SUPPORT) |
| 179 | storage_->set_ftp_transaction_factory( |
| 180 | new net::FtpNetworkLayer(network_session_params.host_resolver)); |
| 181 | #endif |
| 182 | |
[email protected] | 6bd3007 | 2013-02-08 18:17:11 | [diff] [blame] | 183 | scoped_ptr<net::URLRequestJobFactoryImpl> job_factory( |
| 184 | new net::URLRequestJobFactoryImpl()); |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 185 | InstallProtocolHandlers(job_factory.get(), &protocol_handlers_); |
| 186 | storage_->set_job_factory(job_factory.release()); |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 189 | return url_request_context_.get(); |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | 4969b012 | 2012-06-16 01:58:28 | [diff] [blame] | 192 | scoped_refptr<base::SingleThreadTaskRunner> |
| 193 | ShellURLRequestContextGetter::GetNetworkTaskRunner() const { |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 194 | return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
| 195 | } |
| 196 | |
[email protected] | 98cce3ae | 2011-10-15 16:19:41 | [diff] [blame] | 197 | net::HostResolver* ShellURLRequestContextGetter::host_resolver() { |
| 198 | return url_request_context_->host_resolver(); |
| 199 | } |
| 200 | |
[email protected] | c5f1e33 | 2011-09-27 01:08:03 | [diff] [blame] | 201 | } // namespace content |