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

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

Issue 2024973002: headless: Allow protocol handler customization (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments Created 4 years, 6 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
« no previous file with comments | « no previous file | headless/lib/headless_browser_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 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 "headless/lib/browser/headless_url_request_context_getter.h" 5 #include "headless/lib/browser/headless_url_request_context_getter.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 file_task_runner_(std::move(file_task_runner)), 65 file_task_runner_(std::move(file_task_runner)),
66 net_log_(net_log), 66 net_log_(net_log),
67 user_agent_(options->user_agent), 67 user_agent_(options->user_agent),
68 host_resolver_rules_(options->host_resolver_rules), 68 host_resolver_rules_(options->host_resolver_rules),
69 proxy_server_(options->proxy_server), 69 proxy_server_(options->proxy_server),
70 request_interceptors_(std::move(request_interceptors)) { 70 request_interceptors_(std::move(request_interceptors)) {
71 // Must first be created on the UI thread. 71 // Must first be created on the UI thread.
72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
73 73
74 std::swap(protocol_handlers_, *protocol_handlers); 74 std::swap(protocol_handlers_, *protocol_handlers);
75 for (auto& pair : options->protocol_handlers) {
76 protocol_handlers_[pair.first] =
77 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
78 pair.second.release());
79 }
80 options->protocol_handlers.clear();
75 81
76 // We must create the proxy config service on the UI loop on Linux because it 82 // We must create the proxy config service on the UI loop on Linux because it
77 // must synchronously run on the glib message loop. This will be passed to 83 // must synchronously run on the glib message loop. This will be passed to
78 // the URLRequestContextStorage on the IO thread in GetURLRequestContext(). 84 // the URLRequestContextStorage on the IO thread in GetURLRequestContext().
79 if (proxy_server_.IsEmpty()) 85 if (proxy_server_.IsEmpty())
80 proxy_config_service_ = GetProxyConfigService(); 86 proxy_config_service_ = GetProxyConfigService();
81 } 87 }
82 88
83 HeadlessURLRequestContextGetter::~HeadlessURLRequestContextGetter() {} 89 HeadlessURLRequestContextGetter::~HeadlessURLRequestContextGetter() {}
84 90
(...skipping 13 matching lines...) Expand all
98 if (!proxy_server_.IsEmpty()) 104 if (!proxy_server_.IsEmpty())
99 return net::ProxyService::CreateFixed(proxy_server_.ToString()); 105 return net::ProxyService::CreateFixed(proxy_server_.ToString());
100 return net::ProxyService::CreateUsingSystemProxyResolver( 106 return net::ProxyService::CreateUsingSystemProxyResolver(
101 std::move(proxy_config_service_), 0, url_request_context_->net_log()); 107 std::move(proxy_config_service_), 0, url_request_context_->net_log());
102 } 108 }
103 109
104 net::URLRequestContext* 110 net::URLRequestContext*
105 HeadlessURLRequestContextGetter::GetURLRequestContext() { 111 HeadlessURLRequestContextGetter::GetURLRequestContext() {
106 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 112 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
107 113
108 if (!url_request_context_) { 114 if (!url_request_context_) {
mmenke 2016/06/01 15:32:19 I'd recommend you switch to using URLRequestContex
Sami 2016/06/01 15:44:51 Thanks for the tip; filed https://bugs.chromium.or
109 url_request_context_.reset(new net::URLRequestContext()); 115 url_request_context_.reset(new net::URLRequestContext());
110 url_request_context_->set_net_log(net_log_); 116 url_request_context_->set_net_log(net_log_);
111 network_delegate_ = CreateNetworkDelegate(); 117 network_delegate_ = CreateNetworkDelegate();
112 url_request_context_->set_network_delegate(network_delegate_.get()); 118 url_request_context_->set_network_delegate(network_delegate_.get());
113 storage_.reset( 119 storage_.reset(
114 new net::URLRequestContextStorage(url_request_context_.get())); 120 new net::URLRequestContextStorage(url_request_context_.get()));
115 storage_->set_cookie_store( 121 storage_->set_cookie_store(
116 content::CreateCookieStore(content::CookieStoreConfig())); 122 content::CreateCookieStore(content::CookieStoreConfig()));
117 storage_->set_channel_id_service(base::WrapUnique( 123 storage_->set_channel_id_service(base::WrapUnique(
118 new net::ChannelIDService(new net::DefaultChannelIDStore(nullptr), 124 new net::ChannelIDService(new net::DefaultChannelIDStore(nullptr),
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 180
175 storage_->set_http_network_session( 181 storage_->set_http_network_session(
176 base::WrapUnique(new net::HttpNetworkSession(network_session_params))); 182 base::WrapUnique(new net::HttpNetworkSession(network_session_params)));
177 storage_->set_http_transaction_factory(base::WrapUnique(new net::HttpCache( 183 storage_->set_http_transaction_factory(base::WrapUnique(new net::HttpCache(
178 storage_->http_network_session(), std::move(main_backend), 184 storage_->http_network_session(), std::move(main_backend),
179 true /* set_up_quic_server_info */))); 185 true /* set_up_quic_server_info */)));
180 186
181 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory( 187 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory(
182 new net::URLRequestJobFactoryImpl()); 188 new net::URLRequestJobFactoryImpl());
183 189
190 // Install handlers for default protocols which aren't handled by the
191 // network layer.
192 if (protocol_handlers_.find(url::kDataScheme) == protocol_handlers_.end()) {
193 protocol_handlers_[url::kDataScheme] =
194 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
195 new net::DataProtocolHandler());
196 }
197 if (protocol_handlers_.find(url::kFileScheme) == protocol_handlers_.end()) {
198 protocol_handlers_[url::kFileScheme] =
199 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
200 new net::FileProtocolHandler(
201 content::BrowserThread::GetBlockingPool()
202 ->GetTaskRunnerWithShutdownBehavior(
203 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
204 }
184 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_); 205 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_);
185 bool set_protocol = job_factory->SetProtocolHandler(
186 url::kDataScheme, base::WrapUnique(new net::DataProtocolHandler));
187 DCHECK(set_protocol);
188 set_protocol = job_factory->SetProtocolHandler(
189 url::kFileScheme,
190 base::WrapUnique(new net::FileProtocolHandler(
191 content::BrowserThread::GetBlockingPool()
192 ->GetTaskRunnerWithShutdownBehavior(
193 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
194 DCHECK(set_protocol);
195 206
196 // Set up interceptors in the reverse order so that the last inceptor is at 207 // Set up interceptors in the reverse order so that the last inceptor is at
197 // the end of the linked list of job factories. 208 // the end of the linked list of job factories.
198 std::unique_ptr<net::URLRequestJobFactory> top_job_factory = 209 std::unique_ptr<net::URLRequestJobFactory> top_job_factory =
199 std::move(job_factory); 210 std::move(job_factory);
200 for (auto i = request_interceptors_.rbegin(); 211 for (auto i = request_interceptors_.rbegin();
201 i != request_interceptors_.rend(); ++i) { 212 i != request_interceptors_.rend(); ++i) {
202 top_job_factory.reset(new net::URLRequestInterceptingJobFactory( 213 top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
203 std::move(top_job_factory), base::WrapUnique(*i))); 214 std::move(top_job_factory), base::WrapUnique(*i)));
204 } 215 }
205 request_interceptors_.weak_clear(); 216 request_interceptors_.weak_clear();
206 // Save the head of the job factory list at storage_. 217 // Save the head of the job factory list at storage_.
207 storage_->set_job_factory(std::move(top_job_factory)); 218 storage_->set_job_factory(std::move(top_job_factory));
208 } 219 }
209 220
210 return url_request_context_.get(); 221 return url_request_context_.get();
211 } 222 }
212 223
213 scoped_refptr<base::SingleThreadTaskRunner> 224 scoped_refptr<base::SingleThreadTaskRunner>
214 HeadlessURLRequestContextGetter::GetNetworkTaskRunner() const { 225 HeadlessURLRequestContextGetter::GetNetworkTaskRunner() const {
215 return content::BrowserThread::GetMessageLoopProxyForThread( 226 return content::BrowserThread::GetMessageLoopProxyForThread(
216 content::BrowserThread::IO); 227 content::BrowserThread::IO);
217 } 228 }
218 229
219 net::HostResolver* HeadlessURLRequestContextGetter::host_resolver() const { 230 net::HostResolver* HeadlessURLRequestContextGetter::host_resolver() const {
220 return url_request_context_->host_resolver(); 231 return url_request_context_->host_resolver();
221 } 232 }
222 233
223 } // namespace headless 234 } // namespace headless
OLDNEW
« no previous file with comments | « no previous file | headless/lib/headless_browser_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698