OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/shell/browser/shell_url_request_context_getter.h" | 5 #include "headless/lib/browser/headless_url_request_context_getter.h" |
6 | 6 |
7 #include "base/command_line.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
10 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
13 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
14 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
15 #include "base/threading/worker_pool.h" | 13 #include "base/threading/worker_pool.h" |
16 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/cookie_store_factory.h" | 15 #include "content/public/browser/cookie_store_factory.h" |
18 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
19 #include "content/shell/browser/shell_network_delegate.h" | |
20 #include "content/shell/common/shell_content_client.h" | |
21 #include "content/shell/common/shell_switches.h" | |
22 #include "net/base/cache_type.h" | 17 #include "net/base/cache_type.h" |
23 #include "net/cert/cert_verifier.h" | 18 #include "net/cert/cert_verifier.h" |
24 #include "net/cookies/cookie_monster.h" | 19 #include "net/cookies/cookie_monster.h" |
25 #include "net/dns/host_resolver.h" | 20 #include "net/dns/host_resolver.h" |
26 #include "net/dns/mapped_host_resolver.h" | 21 #include "net/dns/mapped_host_resolver.h" |
27 #include "net/http/http_auth_handler_factory.h" | 22 #include "net/http/http_auth_handler_factory.h" |
28 #include "net/http/http_cache.h" | 23 #include "net/http/http_cache.h" |
29 #include "net/http/http_network_session.h" | 24 #include "net/http/http_network_session.h" |
30 #include "net/http/http_server_properties_impl.h" | 25 #include "net/http/http_server_properties_impl.h" |
31 #include "net/http/transport_security_state.h" | 26 #include "net/http/transport_security_state.h" |
32 #include "net/proxy/proxy_service.h" | 27 #include "net/proxy/proxy_service.h" |
33 #include "net/ssl/channel_id_service.h" | 28 #include "net/ssl/channel_id_service.h" |
34 #include "net/ssl/default_channel_id_store.h" | 29 #include "net/ssl/default_channel_id_store.h" |
35 #include "net/ssl/ssl_config_service_defaults.h" | 30 #include "net/ssl/ssl_config_service_defaults.h" |
36 #include "net/url_request/data_protocol_handler.h" | 31 #include "net/url_request/data_protocol_handler.h" |
37 #include "net/url_request/file_protocol_handler.h" | 32 #include "net/url_request/file_protocol_handler.h" |
38 #include "net/url_request/static_http_user_agent_settings.h" | 33 #include "net/url_request/static_http_user_agent_settings.h" |
39 #include "net/url_request/url_request_context.h" | 34 #include "net/url_request/url_request_context.h" |
40 #include "net/url_request/url_request_context_storage.h" | 35 #include "net/url_request/url_request_context_storage.h" |
41 #include "net/url_request/url_request_intercepting_job_factory.h" | 36 #include "net/url_request/url_request_intercepting_job_factory.h" |
42 #include "net/url_request/url_request_job_factory_impl.h" | 37 #include "net/url_request/url_request_job_factory_impl.h" |
43 #include "url/url_constants.h" | 38 #include "url/url_constants.h" |
| 39 #include "headless/lib/browser/headless_network_delegate.h" |
44 | 40 |
45 namespace content { | 41 namespace headless { |
46 | 42 |
47 namespace { | 43 namespace { |
48 | 44 |
49 void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory, | 45 void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory, |
50 ProtocolHandlerMap* protocol_handlers) { | 46 content::ProtocolHandlerMap* protocol_handlers) { |
51 for (ProtocolHandlerMap::iterator it = | 47 for (content::ProtocolHandlerMap::iterator it = |
52 protocol_handlers->begin(); | 48 protocol_handlers->begin(); |
53 it != protocol_handlers->end(); | 49 it != protocol_handlers->end(); |
54 ++it) { | 50 ++it) { |
55 bool set_protocol = job_factory->SetProtocolHandler( | 51 bool set_protocol = job_factory->SetProtocolHandler( |
56 it->first, make_scoped_ptr(it->second.release())); | 52 it->first, make_scoped_ptr(it->second.release())); |
57 DCHECK(set_protocol); | 53 DCHECK(set_protocol); |
58 } | 54 } |
59 protocol_handlers->clear(); | 55 protocol_handlers->clear(); |
60 } | 56 } |
61 | 57 |
62 } // namespace | 58 } // namespace |
63 | 59 |
64 ShellURLRequestContextGetter::ShellURLRequestContextGetter( | 60 HeadlessURLRequestContextGetter::HeadlessURLRequestContextGetter( |
65 bool ignore_certificate_errors, | 61 bool ignore_certificate_errors, |
66 const base::FilePath& base_path, | 62 const base::FilePath& base_path, |
67 base::MessageLoop* io_loop, | 63 base::MessageLoop* io_loop, |
68 base::MessageLoop* file_loop, | 64 base::MessageLoop* file_loop, |
69 ProtocolHandlerMap* protocol_handlers, | 65 content::ProtocolHandlerMap* protocol_handlers, |
70 URLRequestInterceptorScopedVector request_interceptors, | 66 content::URLRequestInterceptorScopedVector request_interceptors, |
71 net::NetLog* net_log) | 67 net::NetLog* net_log) |
72 : ignore_certificate_errors_(ignore_certificate_errors), | 68 : ignore_certificate_errors_(ignore_certificate_errors), |
73 base_path_(base_path), | 69 base_path_(base_path), |
74 io_loop_(io_loop), | 70 io_loop_(io_loop), |
75 file_loop_(file_loop), | 71 file_loop_(file_loop), |
76 net_log_(net_log), | 72 net_log_(net_log), |
77 request_interceptors_(request_interceptors.Pass()) { | 73 request_interceptors_(request_interceptors.Pass()) { |
78 // Must first be created on the UI thread. | 74 // Must first be created on the UI thread. |
79 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
80 | 76 |
81 std::swap(protocol_handlers_, *protocol_handlers); | 77 std::swap(protocol_handlers_, *protocol_handlers); |
82 | 78 |
83 // We must create the proxy config service on the UI loop on Linux because it | 79 // We must create the proxy config service on the UI loop on Linux because it |
84 // must synchronously run on the glib message loop. This will be passed to | 80 // must synchronously run on the glib message loop. This will be passed to |
85 // the URLRequestContextStorage on the IO thread in GetURLRequestContext(). | 81 // the URLRequestContextStorage on the IO thread in GetURLRequestContext(). |
86 proxy_config_service_ = GetProxyConfigService(); | 82 proxy_config_service_ = GetProxyConfigService(); |
87 } | 83 } |
88 | 84 |
89 ShellURLRequestContextGetter::~ShellURLRequestContextGetter() { | 85 HeadlessURLRequestContextGetter::~HeadlessURLRequestContextGetter() { |
90 } | 86 } |
91 | 87 |
92 scoped_ptr<net::NetworkDelegate> | 88 scoped_ptr<net::NetworkDelegate> |
93 ShellURLRequestContextGetter::CreateNetworkDelegate() { | 89 HeadlessURLRequestContextGetter::CreateNetworkDelegate() { |
94 return make_scoped_ptr(new ShellNetworkDelegate); | 90 return make_scoped_ptr(new HeadlessNetworkDelegate); |
95 } | 91 } |
96 | 92 |
97 scoped_ptr<net::ProxyConfigService> | 93 scoped_ptr<net::ProxyConfigService> |
98 ShellURLRequestContextGetter::GetProxyConfigService() { | 94 HeadlessURLRequestContextGetter::GetProxyConfigService() { |
99 return net::ProxyService::CreateSystemProxyConfigService( | 95 return net::ProxyService::CreateSystemProxyConfigService( |
100 io_loop_->task_runner(), file_loop_->task_runner()); | 96 io_loop_->task_runner(), file_loop_->task_runner()); |
101 } | 97 } |
102 | 98 |
103 scoped_ptr<net::ProxyService> ShellURLRequestContextGetter::GetProxyService() { | 99 scoped_ptr<net::ProxyService> HeadlessURLRequestContextGetter::GetProxyService()
{ |
104 // TODO(jam): use v8 if possible, look at chrome code. | 100 // TODO(jam): use v8 if possible, look at chrome code. |
105 return net::ProxyService::CreateUsingSystemProxyResolver( | 101 return net::ProxyService::CreateUsingSystemProxyResolver( |
106 proxy_config_service_.Pass(), 0, url_request_context_->net_log()); | 102 proxy_config_service_.Pass(), 0, url_request_context_->net_log()); |
107 } | 103 } |
108 | 104 |
109 net::URLRequestContext* ShellURLRequestContextGetter::GetURLRequestContext() { | 105 net::URLRequestContext* HeadlessURLRequestContextGetter::GetURLRequestContext()
{ |
110 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 106 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
111 | 107 |
112 if (!url_request_context_) { | 108 if (!url_request_context_) { |
113 const base::CommandLine& command_line = | |
114 *base::CommandLine::ForCurrentProcess(); | |
115 | |
116 url_request_context_.reset(new net::URLRequestContext()); | 109 url_request_context_.reset(new net::URLRequestContext()); |
117 url_request_context_->set_net_log(net_log_); | 110 url_request_context_->set_net_log(net_log_); |
118 network_delegate_ = CreateNetworkDelegate(); | 111 network_delegate_ = CreateNetworkDelegate(); |
119 url_request_context_->set_network_delegate(network_delegate_.get()); | 112 url_request_context_->set_network_delegate(network_delegate_.get()); |
120 storage_.reset( | 113 storage_.reset( |
121 new net::URLRequestContextStorage(url_request_context_.get())); | 114 new net::URLRequestContextStorage(url_request_context_.get())); |
122 storage_->set_cookie_store(CreateCookieStore(CookieStoreConfig())); | 115 storage_->set_cookie_store(content::CreateCookieStore(content::CookieStoreCo
nfig())); |
123 storage_->set_channel_id_service(make_scoped_ptr( | 116 storage_->set_channel_id_service(make_scoped_ptr( |
124 new net::ChannelIDService(new net::DefaultChannelIDStore(NULL), | 117 new net::ChannelIDService(new net::DefaultChannelIDStore(NULL), |
125 base::WorkerPool::GetTaskRunner(true)))); | 118 base::WorkerPool::GetTaskRunner(true)))); |
| 119 // TODO: headless user agent fix |
126 storage_->set_http_user_agent_settings(make_scoped_ptr( | 120 storage_->set_http_user_agent_settings(make_scoped_ptr( |
127 new net::StaticHttpUserAgentSettings("en-us,en", GetShellUserAgent()))); | 121 new net::StaticHttpUserAgentSettings("en-us,en", "HeadlessUserAgent"))); |
128 | 122 |
129 scoped_ptr<net::HostResolver> host_resolver( | 123 scoped_ptr<net::HostResolver> host_resolver( |
130 net::HostResolver::CreateDefaultResolver( | 124 net::HostResolver::CreateDefaultResolver( |
131 url_request_context_->net_log())); | 125 url_request_context_->net_log())); |
132 | 126 |
133 storage_->set_cert_verifier(net::CertVerifier::CreateDefault()); | 127 storage_->set_cert_verifier(net::CertVerifier::CreateDefault()); |
134 storage_->set_transport_security_state( | 128 storage_->set_transport_security_state( |
135 make_scoped_ptr(new net::TransportSecurityState)); | 129 make_scoped_ptr(new net::TransportSecurityState)); |
136 storage_->set_proxy_service(GetProxyService()); | 130 storage_->set_proxy_service(GetProxyService()); |
137 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults); | 131 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults); |
138 storage_->set_http_auth_handler_factory( | 132 storage_->set_http_auth_handler_factory( |
139 net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); | 133 net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); |
140 storage_->set_http_server_properties( | 134 storage_->set_http_server_properties( |
141 make_scoped_ptr(new net::HttpServerPropertiesImpl())); | 135 make_scoped_ptr(new net::HttpServerPropertiesImpl())); |
142 | 136 |
143 base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache")); | 137 base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache")); |
144 scoped_ptr<net::HttpCache::DefaultBackend> main_backend( | 138 scoped_ptr<net::HttpCache::DefaultBackend> main_backend( |
145 new net::HttpCache::DefaultBackend( | 139 new net::HttpCache::DefaultBackend( |
146 net::DISK_CACHE, | 140 net::DISK_CACHE, |
147 #if defined(OS_ANDROID) | |
148 // TODO(rdsmith): Remove when default backend for Android is | |
149 // changed to simple cache. | |
150 net::CACHE_BACKEND_SIMPLE, | |
151 #else | |
152 net::CACHE_BACKEND_DEFAULT, | 141 net::CACHE_BACKEND_DEFAULT, |
153 #endif | |
154 cache_path, | 142 cache_path, |
155 0, | 143 0, |
156 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE))); | 144 content::BrowserThread::GetMessageLoopProxyForThread(content::Browse
rThread::CACHE))); |
157 | 145 |
158 net::HttpNetworkSession::Params network_session_params; | 146 net::HttpNetworkSession::Params network_session_params; |
159 network_session_params.cert_verifier = | 147 network_session_params.cert_verifier = |
160 url_request_context_->cert_verifier(); | 148 url_request_context_->cert_verifier(); |
161 network_session_params.transport_security_state = | 149 network_session_params.transport_security_state = |
162 url_request_context_->transport_security_state(); | 150 url_request_context_->transport_security_state(); |
163 network_session_params.channel_id_service = | 151 network_session_params.channel_id_service = |
164 url_request_context_->channel_id_service(); | 152 url_request_context_->channel_id_service(); |
165 network_session_params.proxy_service = | 153 network_session_params.proxy_service = |
166 url_request_context_->proxy_service(); | 154 url_request_context_->proxy_service(); |
167 network_session_params.ssl_config_service = | 155 network_session_params.ssl_config_service = |
168 url_request_context_->ssl_config_service(); | 156 url_request_context_->ssl_config_service(); |
169 network_session_params.http_auth_handler_factory = | 157 network_session_params.http_auth_handler_factory = |
170 url_request_context_->http_auth_handler_factory(); | 158 url_request_context_->http_auth_handler_factory(); |
171 network_session_params.network_delegate = | 159 network_session_params.network_delegate = |
172 network_delegate_.get(); | 160 network_delegate_.get(); |
173 network_session_params.http_server_properties = | 161 network_session_params.http_server_properties = |
174 url_request_context_->http_server_properties(); | 162 url_request_context_->http_server_properties(); |
175 network_session_params.net_log = | 163 network_session_params.net_log = |
176 url_request_context_->net_log(); | 164 url_request_context_->net_log(); |
177 network_session_params.ignore_certificate_errors = | 165 network_session_params.ignore_certificate_errors = |
178 ignore_certificate_errors_; | 166 ignore_certificate_errors_; |
179 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) { | |
180 int value; | |
181 base::StringToInt(command_line.GetSwitchValueASCII( | |
182 switches::kTestingFixedHttpPort), &value); | |
183 network_session_params.testing_fixed_http_port = value; | |
184 } | |
185 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) { | |
186 int value; | |
187 base::StringToInt(command_line.GetSwitchValueASCII( | |
188 switches::kTestingFixedHttpsPort), &value); | |
189 network_session_params.testing_fixed_https_port = value; | |
190 } | |
191 if (command_line.HasSwitch(switches::kHostResolverRules)) { | |
192 scoped_ptr<net::MappedHostResolver> mapped_host_resolver( | |
193 new net::MappedHostResolver(host_resolver.Pass())); | |
194 mapped_host_resolver->SetRulesFromString( | |
195 command_line.GetSwitchValueASCII(switches::kHostResolverRules)); | |
196 host_resolver = mapped_host_resolver.Pass(); | |
197 } | |
198 | 167 |
199 // Give |storage_| ownership at the end in case it's |mapped_host_resolver|. | 168 // Give |storage_| ownership at the end in case it's |mapped_host_resolver|. |
200 storage_->set_host_resolver(host_resolver.Pass()); | 169 storage_->set_host_resolver(host_resolver.Pass()); |
201 network_session_params.host_resolver = | 170 network_session_params.host_resolver = |
202 url_request_context_->host_resolver(); | 171 url_request_context_->host_resolver(); |
203 | 172 |
204 storage_->set_http_network_session( | 173 storage_->set_http_network_session( |
205 make_scoped_ptr(new net::HttpNetworkSession(network_session_params))); | 174 make_scoped_ptr(new net::HttpNetworkSession(network_session_params))); |
206 storage_->set_http_transaction_factory(make_scoped_ptr( | 175 storage_->set_http_transaction_factory(make_scoped_ptr( |
207 new net::HttpCache(storage_->http_network_session(), | 176 new net::HttpCache(storage_->http_network_session(), |
208 main_backend.Pass(), | 177 main_backend.Pass(), |
209 true /* set_up_quic_server_info */))); | 178 true /* set_up_quic_server_info */))); |
210 | 179 |
211 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory( | 180 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory( |
212 new net::URLRequestJobFactoryImpl()); | 181 new net::URLRequestJobFactoryImpl()); |
213 // Keep ProtocolHandlers added in sync with | 182 |
214 // ShellContentBrowserClient::IsHandledURL(). | |
215 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_); | 183 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_); |
216 bool set_protocol = job_factory->SetProtocolHandler( | 184 bool set_protocol = job_factory->SetProtocolHandler( |
217 url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler)); | 185 url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler)); |
218 DCHECK(set_protocol); | 186 DCHECK(set_protocol); |
219 #if !defined(DISABLE_FILE_SUPPORT) | 187 #if !defined(DISABLE_FILE_SUPPORT) |
220 set_protocol = job_factory->SetProtocolHandler( | 188 set_protocol = job_factory->SetProtocolHandler( |
221 url::kFileScheme, | 189 url::kFileScheme, |
222 make_scoped_ptr(new net::FileProtocolHandler( | 190 make_scoped_ptr(new net::FileProtocolHandler( |
223 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 191 content::BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdown
Behavior( |
224 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)))); | 192 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)))); |
225 DCHECK(set_protocol); | 193 DCHECK(set_protocol); |
226 #endif | 194 #endif |
227 | 195 |
228 // Set up interceptors in the reverse order. | 196 // Set up interceptors in the reverse order. |
229 scoped_ptr<net::URLRequestJobFactory> top_job_factory = | 197 scoped_ptr<net::URLRequestJobFactory> top_job_factory = |
230 job_factory.Pass(); | 198 job_factory.Pass(); |
231 for (URLRequestInterceptorScopedVector::reverse_iterator i = | 199 for (content::URLRequestInterceptorScopedVector::reverse_iterator i = |
232 request_interceptors_.rbegin(); | 200 request_interceptors_.rbegin(); |
233 i != request_interceptors_.rend(); | 201 i != request_interceptors_.rend(); |
234 ++i) { | 202 ++i) { |
235 top_job_factory.reset(new net::URLRequestInterceptingJobFactory( | 203 top_job_factory.reset(new net::URLRequestInterceptingJobFactory( |
236 top_job_factory.Pass(), make_scoped_ptr(*i))); | 204 top_job_factory.Pass(), make_scoped_ptr(*i))); |
237 } | 205 } |
238 request_interceptors_.weak_clear(); | 206 request_interceptors_.weak_clear(); |
239 | 207 |
240 storage_->set_job_factory(top_job_factory.Pass()); | 208 storage_->set_job_factory(top_job_factory.Pass()); |
241 } | 209 } |
242 | 210 |
243 return url_request_context_.get(); | 211 return url_request_context_.get(); |
244 } | 212 } |
245 | 213 |
246 scoped_refptr<base::SingleThreadTaskRunner> | 214 scoped_refptr<base::SingleThreadTaskRunner> |
247 ShellURLRequestContextGetter::GetNetworkTaskRunner() const { | 215 HeadlessURLRequestContextGetter::GetNetworkTaskRunner() const { |
248 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | 216 return content::BrowserThread::GetMessageLoopProxyForThread(content::BrowserTh
read::IO); |
249 } | 217 } |
250 | 218 |
251 net::HostResolver* ShellURLRequestContextGetter::host_resolver() { | 219 net::HostResolver* HeadlessURLRequestContextGetter::host_resolver() { |
252 return url_request_context_->host_resolver(); | 220 return url_request_context_->host_resolver(); |
253 } | 221 } |
254 | 222 |
255 } // namespace content | 223 } // namespace headless |
OLD | NEW |