Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: headless/lib/browser/headless_url_request_context_getter.cc

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

Powered by Google App Engine
This is Rietveld 408576698