| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "blimp/engine/app/blimp_url_request_context_getter.h" | 5 #include "headless/lib/browser/headless_url_request_context_getter.h" |
| 6 | |
| 7 #include <algorithm> | |
| 8 | 6 |
| 9 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/strings/string_split.h" | |
| 15 #include "base/strings/string_util.h" | |
| 16 #include "base/threading/sequenced_worker_pool.h" | |
| 17 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
| 18 #include "blimp/engine/app/blimp_network_delegate.h" | |
| 19 #include "blimp/engine/common/blimp_content_client.h" | |
| 20 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/cookie_store_factory.h" | 12 #include "content/public/browser/cookie_store_factory.h" |
| 22 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
| 23 #include "net/base/cache_type.h" | |
| 24 #include "net/cert/cert_verifier.h" | 14 #include "net/cert/cert_verifier.h" |
| 25 #include "net/cookies/cookie_monster.h" | |
| 26 #include "net/dns/host_resolver.h" | 15 #include "net/dns/host_resolver.h" |
| 27 #include "net/dns/mapped_host_resolver.h" | 16 #include "net/dns/mapped_host_resolver.h" |
| 28 #include "net/http/http_auth_handler_factory.h" | 17 #include "net/http/http_auth_handler_factory.h" |
| 29 #include "net/http/http_cache.h" | 18 #include "net/http/http_cache.h" |
| 30 #include "net/http/http_network_session.h" | 19 #include "net/http/http_network_session.h" |
| 31 #include "net/http/http_server_properties_impl.h" | 20 #include "net/http/http_server_properties_impl.h" |
| 32 #include "net/http/transport_security_state.h" | 21 #include "net/http/transport_security_state.h" |
| 33 #include "net/proxy/proxy_config_service.h" | |
| 34 #include "net/proxy/proxy_service.h" | 22 #include "net/proxy/proxy_service.h" |
| 35 #include "net/ssl/channel_id_service.h" | 23 #include "net/ssl/channel_id_service.h" |
| 36 #include "net/ssl/default_channel_id_store.h" | 24 #include "net/ssl/default_channel_id_store.h" |
| 37 #include "net/ssl/ssl_config_service_defaults.h" | 25 #include "net/ssl/ssl_config_service_defaults.h" |
| 38 #include "net/url_request/data_protocol_handler.h" | 26 #include "net/url_request/data_protocol_handler.h" |
| 39 #include "net/url_request/file_protocol_handler.h" | 27 #include "net/url_request/file_protocol_handler.h" |
| 40 #include "net/url_request/static_http_user_agent_settings.h" | 28 #include "net/url_request/static_http_user_agent_settings.h" |
| 41 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
| 42 #include "net/url_request/url_request_context_storage.h" | 30 #include "net/url_request/url_request_context_storage.h" |
| 43 #include "net/url_request/url_request_intercepting_job_factory.h" | 31 #include "net/url_request/url_request_intercepting_job_factory.h" |
| 44 #include "net/url_request/url_request_job_factory_impl.h" | 32 #include "net/url_request/url_request_job_factory_impl.h" |
| 45 #include "url/url_constants.h" | |
| 46 | 33 |
| 47 namespace blimp { | 34 namespace headless { |
| 48 namespace engine { | |
| 49 | 35 |
| 50 namespace { | 36 namespace { |
| 51 | 37 |
| 52 void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory, | 38 void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory, |
| 53 content::ProtocolHandlerMap* protocol_handlers) { | 39 content::ProtocolHandlerMap* protocol_handlers) { |
| 54 for (content::ProtocolHandlerMap::iterator it = protocol_handlers->begin(); | 40 for (content::ProtocolHandlerMap::iterator it = protocol_handlers->begin(); |
| 55 it != protocol_handlers->end(); ++it) { | 41 it != protocol_handlers->end(); ++it) { |
| 56 bool set_protocol = job_factory->SetProtocolHandler( | 42 bool set_protocol = job_factory->SetProtocolHandler( |
| 57 it->first, make_scoped_ptr(it->second.release())); | 43 it->first, make_scoped_ptr(it->second.release())); |
| 58 DCHECK(set_protocol); | 44 DCHECK(set_protocol); |
| 59 } | 45 } |
| 60 protocol_handlers->clear(); | 46 protocol_handlers->clear(); |
| 61 } | 47 } |
| 62 | 48 |
| 63 } // namespace | 49 } // namespace |
| 64 | 50 |
| 65 BlimpURLRequestContextGetter::BlimpURLRequestContextGetter( | 51 HeadlessURLRequestContextGetter::HeadlessURLRequestContextGetter( |
| 66 bool ignore_certificate_errors, | 52 bool ignore_certificate_errors, |
| 67 const base::FilePath& base_path, | 53 const base::FilePath& base_path, |
| 68 const scoped_refptr<base::SingleThreadTaskRunner>& io_loop_task_runner, | 54 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 69 const scoped_refptr<base::SingleThreadTaskRunner>& file_loop_task_runner, | 55 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 70 content::ProtocolHandlerMap* protocol_handlers, | 56 content::ProtocolHandlerMap* protocol_handlers, |
| 71 content::URLRequestInterceptorScopedVector request_interceptors, | 57 content::URLRequestInterceptorScopedVector request_interceptors, |
| 72 net::NetLog* net_log) | 58 net::NetLog* net_log, |
| 59 const HeadlessBrowser::Options& options) |
| 73 : ignore_certificate_errors_(ignore_certificate_errors), | 60 : ignore_certificate_errors_(ignore_certificate_errors), |
| 74 base_path_(base_path), | 61 base_path_(base_path), |
| 75 io_loop_task_runner_(io_loop_task_runner), | 62 io_task_runner_(std::move(io_task_runner)), |
| 76 file_loop_task_runner_(file_loop_task_runner), | 63 file_task_runner_(std::move(file_task_runner)), |
| 77 net_log_(net_log), | 64 net_log_(net_log), |
| 65 options_(options), |
| 78 request_interceptors_(std::move(request_interceptors)) { | 66 request_interceptors_(std::move(request_interceptors)) { |
| 79 // Must first be created on the UI thread. | 67 // Must first be created on the UI thread. |
| 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 81 | 69 |
| 82 std::swap(protocol_handlers_, *protocol_handlers); | 70 std::swap(protocol_handlers_, *protocol_handlers); |
| 83 | 71 |
| 84 // We must create the proxy config service on the UI loop on Linux because it | 72 // We must create the proxy config service on the UI loop on Linux because it |
| 85 // must synchronously run on the glib message loop. This will be passed to | 73 // must synchronously run on the glib message loop. This will be passed to |
| 86 // the URLRequestContextStorage on the IO thread in GetURLRequestContext(). | 74 // the URLRequestContextStorage on the IO thread in GetURLRequestContext(). |
| 87 proxy_config_service_ = GetProxyConfigService(); | 75 proxy_config_service_ = GetProxyConfigService(); |
| 88 } | 76 } |
| 89 | 77 |
| 90 BlimpURLRequestContextGetter::~BlimpURLRequestContextGetter() {} | 78 HeadlessURLRequestContextGetter::~HeadlessURLRequestContextGetter() {} |
| 91 | 79 |
| 92 scoped_ptr<net::NetworkDelegate> | 80 scoped_ptr<net::NetworkDelegate> |
| 93 BlimpURLRequestContextGetter::CreateNetworkDelegate() { | 81 HeadlessURLRequestContextGetter::CreateNetworkDelegate() { |
| 94 return make_scoped_ptr(new BlimpNetworkDelegate); | 82 return nullptr; |
| 95 } | 83 } |
| 96 | 84 |
| 97 scoped_ptr<net::ProxyConfigService> | 85 scoped_ptr<net::ProxyConfigService> |
| 98 BlimpURLRequestContextGetter::GetProxyConfigService() { | 86 HeadlessURLRequestContextGetter::GetProxyConfigService() { |
| 99 return net::ProxyService::CreateSystemProxyConfigService( | 87 return net::ProxyService::CreateSystemProxyConfigService(io_task_runner_, |
| 100 io_loop_task_runner_, file_loop_task_runner_); | 88 file_task_runner_); |
| 101 } | 89 } |
| 102 | 90 |
| 103 scoped_ptr<net::ProxyService> BlimpURLRequestContextGetter::GetProxyService() { | 91 scoped_ptr<net::ProxyService> |
| 92 HeadlessURLRequestContextGetter::GetProxyService() { |
| 104 return net::ProxyService::CreateUsingSystemProxyResolver( | 93 return net::ProxyService::CreateUsingSystemProxyResolver( |
| 105 std::move(proxy_config_service_), 0, url_request_context_->net_log()); | 94 std::move(proxy_config_service_), 0, url_request_context_->net_log()); |
| 106 } | 95 } |
| 107 | 96 |
| 108 net::URLRequestContext* BlimpURLRequestContextGetter::GetURLRequestContext() { | 97 net::URLRequestContext* |
| 98 HeadlessURLRequestContextGetter::GetURLRequestContext() { |
| 109 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 99 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 110 | 100 |
| 111 if (!url_request_context_) { | 101 if (!url_request_context_) { |
| 112 const base::CommandLine& command_line = | 102 const base::CommandLine& command_line = |
| 113 *base::CommandLine::ForCurrentProcess(); | 103 *base::CommandLine::ForCurrentProcess(); |
| 114 | 104 |
| 115 url_request_context_.reset(new net::URLRequestContext()); | 105 url_request_context_.reset(new net::URLRequestContext()); |
| 116 url_request_context_->set_net_log(net_log_); | 106 url_request_context_->set_net_log(net_log_); |
| 117 network_delegate_ = CreateNetworkDelegate(); | 107 network_delegate_ = CreateNetworkDelegate(); |
| 118 url_request_context_->set_network_delegate(network_delegate_.get()); | 108 url_request_context_->set_network_delegate(network_delegate_.get()); |
| 119 storage_.reset( | 109 storage_.reset( |
| 120 new net::URLRequestContextStorage(url_request_context_.get())); | 110 new net::URLRequestContextStorage(url_request_context_.get())); |
| 121 storage_->set_cookie_store( | 111 storage_->set_cookie_store( |
| 122 content::CreateCookieStore(content::CookieStoreConfig())); | 112 content::CreateCookieStore(content::CookieStoreConfig())); |
| 123 storage_->set_channel_id_service(make_scoped_ptr( | 113 storage_->set_channel_id_service(make_scoped_ptr( |
| 124 new net::ChannelIDService(new net::DefaultChannelIDStore(NULL), | 114 new net::ChannelIDService(new net::DefaultChannelIDStore(nullptr), |
| 125 base::WorkerPool::GetTaskRunner(true)))); | 115 base::WorkerPool::GetTaskRunner(true)))); |
| 126 storage_->set_http_user_agent_settings( | 116 // TODO(skyostil): Make language settings configurable. |
| 127 make_scoped_ptr(new net::StaticHttpUserAgentSettings( | 117 storage_->set_http_user_agent_settings(make_scoped_ptr( |
| 128 "en-us,en", GetBlimpEngineUserAgent()))); | 118 new net::StaticHttpUserAgentSettings("en-us,en", options_.user_agent))); |
| 129 | 119 |
| 130 scoped_ptr<net::HostResolver> host_resolver( | 120 scoped_ptr<net::HostResolver> host_resolver( |
| 131 net::HostResolver::CreateDefaultResolver( | 121 net::HostResolver::CreateDefaultResolver( |
| 132 url_request_context_->net_log())); | 122 url_request_context_->net_log())); |
| 133 | 123 |
| 134 storage_->set_cert_verifier(net::CertVerifier::CreateDefault()); | 124 storage_->set_cert_verifier(net::CertVerifier::CreateDefault()); |
| 135 storage_->set_transport_security_state( | 125 storage_->set_transport_security_state( |
| 136 make_scoped_ptr(new net::TransportSecurityState)); | 126 make_scoped_ptr(new net::TransportSecurityState)); |
| 137 storage_->set_proxy_service(GetProxyService()); | 127 storage_->set_proxy_service(GetProxyService()); |
| 138 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults); | 128 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 166 } |
| 177 | 167 |
| 178 // 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|. |
| 179 storage_->set_host_resolver(std::move(host_resolver)); | 169 storage_->set_host_resolver(std::move(host_resolver)); |
| 180 network_session_params.host_resolver = | 170 network_session_params.host_resolver = |
| 181 url_request_context_->host_resolver(); | 171 url_request_context_->host_resolver(); |
| 182 | 172 |
| 183 storage_->set_http_network_session( | 173 storage_->set_http_network_session( |
| 184 make_scoped_ptr(new net::HttpNetworkSession(network_session_params))); | 174 make_scoped_ptr(new net::HttpNetworkSession(network_session_params))); |
| 185 storage_->set_http_transaction_factory(make_scoped_ptr(new net::HttpCache( | 175 storage_->set_http_transaction_factory(make_scoped_ptr(new net::HttpCache( |
| 186 storage_->http_network_session(), std::move(main_backend), true))); | 176 storage_->http_network_session(), std::move(main_backend), |
| 177 true /* set_up_quic_server_info */))); |
| 187 | 178 |
| 188 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory( | 179 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory( |
| 189 new net::URLRequestJobFactoryImpl()); | 180 new net::URLRequestJobFactoryImpl()); |
| 190 // Keep ProtocolHandlers added in sync with | 181 |
| 191 // BlimpContentBrowserClient::IsHandledURL(). | |
| 192 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_); | 182 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_); |
| 193 bool set_protocol = job_factory->SetProtocolHandler( | 183 bool set_protocol = job_factory->SetProtocolHandler( |
| 194 url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler)); | 184 url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler)); |
| 195 DCHECK(set_protocol); | 185 DCHECK(set_protocol); |
| 186 set_protocol = job_factory->SetProtocolHandler( |
| 187 url::kFileScheme, |
| 188 make_scoped_ptr(new net::FileProtocolHandler( |
| 189 content::BrowserThread::GetBlockingPool() |
| 190 ->GetTaskRunnerWithShutdownBehavior( |
| 191 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)))); |
| 192 DCHECK(set_protocol); |
| 196 | 193 |
| 197 // Set up interceptors in the reverse order so that the last inceptor is at | 194 // Set up interceptors in the reverse order so that the last inceptor is at |
| 198 // the end of the linked list of job factories. | 195 // the end of the linked list of job factories. |
| 199 scoped_ptr<net::URLRequestJobFactory> top_job_factory = | 196 scoped_ptr<net::URLRequestJobFactory> top_job_factory = |
| 200 std::move(job_factory); | 197 std::move(job_factory); |
| 201 for (auto i = request_interceptors_.rbegin(); | 198 for (auto i = request_interceptors_.rbegin(); |
| 202 i != request_interceptors_.rend(); ++i) { | 199 i != request_interceptors_.rend(); ++i) { |
| 203 top_job_factory.reset(new net::URLRequestInterceptingJobFactory( | 200 top_job_factory.reset(new net::URLRequestInterceptingJobFactory( |
| 204 std::move(top_job_factory), make_scoped_ptr(*i))); | 201 std::move(top_job_factory), make_scoped_ptr(*i))); |
| 205 } | 202 } |
| 206 request_interceptors_.weak_clear(); | 203 request_interceptors_.weak_clear(); |
| 207 // Save the head of the job factory list at storage_. | 204 // Save the head of the job factory list at storage_. |
| 208 storage_->set_job_factory(std::move(top_job_factory)); | 205 storage_->set_job_factory(std::move(top_job_factory)); |
| 209 } | 206 } |
| 210 | 207 |
| 211 return url_request_context_.get(); | 208 return url_request_context_.get(); |
| 212 } | 209 } |
| 213 | 210 |
| 214 scoped_refptr<base::SingleThreadTaskRunner> | 211 scoped_refptr<base::SingleThreadTaskRunner> |
| 215 BlimpURLRequestContextGetter::GetNetworkTaskRunner() const { | 212 HeadlessURLRequestContextGetter::GetNetworkTaskRunner() const { |
| 216 return content::BrowserThread::GetMessageLoopProxyForThread( | 213 return content::BrowserThread::GetMessageLoopProxyForThread( |
| 217 content::BrowserThread::IO); | 214 content::BrowserThread::IO); |
| 218 } | 215 } |
| 219 | 216 |
| 220 net::HostResolver* BlimpURLRequestContextGetter::host_resolver() { | 217 net::HostResolver* HeadlessURLRequestContextGetter::host_resolver() const { |
| 221 return url_request_context_->host_resolver(); | 218 return url_request_context_->host_resolver(); |
| 222 } | 219 } |
| 223 | 220 |
| 224 } // namespace engine | 221 } // namespace headless |
| 225 } // namespace blimp | |
| OLD | NEW |