Wrap Pepper CDM Content Settings code in defined(ENABLE_PEPPER_CDMS).
[chromium-blink-merge.git] / content / shell / shell_url_request_context_getter.cc
blobe392c519d8fb34c8f87db32cf81d9383eae79139
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/shell_url_request_context_getter.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "base/threading/worker_pool.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/common/url_constants.h"
16 #include "content/shell/common/shell_switches.h"
17 #include "content/shell/shell_network_delegate.h"
18 #include "net/base/cache_type.h"
19 #include "net/cert/cert_verifier.h"
20 #include "net/cookies/cookie_monster.h"
21 #include "net/dns/host_resolver.h"
22 #include "net/dns/mapped_host_resolver.h"
23 #include "net/http/http_auth_handler_factory.h"
24 #include "net/http/http_cache.h"
25 #include "net/http/http_network_session.h"
26 #include "net/http/http_server_properties_impl.h"
27 #include "net/http/transport_security_state.h"
28 #include "net/proxy/proxy_service.h"
29 #include "net/ssl/default_server_bound_cert_store.h"
30 #include "net/ssl/server_bound_cert_service.h"
31 #include "net/ssl/ssl_config_service_defaults.h"
32 #include "net/url_request/data_protocol_handler.h"
33 #include "net/url_request/file_protocol_handler.h"
34 #include "net/url_request/protocol_intercept_job_factory.h"
35 #include "net/url_request/static_http_user_agent_settings.h"
36 #include "net/url_request/url_request_context.h"
37 #include "net/url_request/url_request_context_storage.h"
38 #include "net/url_request/url_request_job_factory_impl.h"
40 namespace content {
42 namespace {
44 void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory,
45 ProtocolHandlerMap* protocol_handlers) {
46 for (ProtocolHandlerMap::iterator it =
47 protocol_handlers->begin();
48 it != protocol_handlers->end();
49 ++it) {
50 bool set_protocol = job_factory->SetProtocolHandler(
51 it->first, it->second.release());
52 DCHECK(set_protocol);
54 protocol_handlers->clear();
57 } // namespace
59 ShellURLRequestContextGetter::ShellURLRequestContextGetter(
60 bool ignore_certificate_errors,
61 const base::FilePath& base_path,
62 base::MessageLoop* io_loop,
63 base::MessageLoop* file_loop,
64 ProtocolHandlerMap* protocol_handlers)
65 : ignore_certificate_errors_(ignore_certificate_errors),
66 base_path_(base_path),
67 io_loop_(io_loop),
68 file_loop_(file_loop) {
69 // Must first be created on the UI thread.
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
72 std::swap(protocol_handlers_, *protocol_handlers);
74 // We must create the proxy config service on the UI loop on Linux because it
75 // must synchronously run on the glib message loop. This will be passed to
76 // the URLRequestContextStorage on the IO thread in GetURLRequestContext().
77 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
78 proxy_config_service_.reset(
79 net::ProxyService::CreateSystemProxyConfigService(
80 io_loop_->message_loop_proxy().get(), file_loop_));
84 ShellURLRequestContextGetter::~ShellURLRequestContextGetter() {
87 net::URLRequestContext* ShellURLRequestContextGetter::GetURLRequestContext() {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
90 if (!url_request_context_) {
91 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
93 url_request_context_.reset(new net::URLRequestContext());
94 network_delegate_.reset(new ShellNetworkDelegate);
95 if (command_line.HasSwitch(switches::kDumpRenderTree))
96 ShellNetworkDelegate::SetAcceptAllCookies(false);
97 url_request_context_->set_network_delegate(network_delegate_.get());
98 storage_.reset(
99 new net::URLRequestContextStorage(url_request_context_.get()));
100 storage_->set_cookie_store(new net::CookieMonster(NULL, NULL));
101 storage_->set_server_bound_cert_service(new net::ServerBoundCertService(
102 new net::DefaultServerBoundCertStore(NULL),
103 base::WorkerPool::GetTaskRunner(true)));
104 storage_->set_http_user_agent_settings(
105 new net::StaticHttpUserAgentSettings("en-us,en", EmptyString()));
107 scoped_ptr<net::HostResolver> host_resolver(
108 net::HostResolver::CreateDefaultResolver(NULL));
110 storage_->set_cert_verifier(net::CertVerifier::CreateDefault());
111 storage_->set_transport_security_state(new net::TransportSecurityState);
112 if (command_line.HasSwitch(switches::kDumpRenderTree)) {
113 storage_->set_proxy_service(net::ProxyService::CreateDirect());
114 } else {
115 // TODO(jam): use v8 if possible, look at chrome code.
116 storage_->set_proxy_service(
117 net::ProxyService::CreateUsingSystemProxyResolver(
118 proxy_config_service_.release(),
120 NULL));
122 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults);
123 storage_->set_http_auth_handler_factory(
124 net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get()));
125 storage_->set_http_server_properties(new net::HttpServerPropertiesImpl);
127 base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
128 net::HttpCache::DefaultBackend* main_backend =
129 new net::HttpCache::DefaultBackend(
130 net::DISK_CACHE,
131 #if defined(OS_ANDROID)
132 // TODO(rdsmith): Remove when default backend for Android is
133 // changed to simple cache.
134 net::CACHE_BACKEND_SIMPLE,
135 #else
136 net::CACHE_BACKEND_DEFAULT,
137 #endif
138 cache_path,
140 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
141 .get());
143 net::HttpNetworkSession::Params network_session_params;
144 network_session_params.cert_verifier =
145 url_request_context_->cert_verifier();
146 network_session_params.transport_security_state =
147 url_request_context_->transport_security_state();
148 network_session_params.server_bound_cert_service =
149 url_request_context_->server_bound_cert_service();
150 network_session_params.proxy_service =
151 url_request_context_->proxy_service();
152 network_session_params.ssl_config_service =
153 url_request_context_->ssl_config_service();
154 network_session_params.http_auth_handler_factory =
155 url_request_context_->http_auth_handler_factory();
156 network_session_params.network_delegate =
157 network_delegate_.get();
158 network_session_params.http_server_properties =
159 url_request_context_->http_server_properties();
160 network_session_params.ignore_certificate_errors =
161 ignore_certificate_errors_;
162 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
163 int value;
164 base::StringToInt(command_line.GetSwitchValueASCII(
165 switches::kTestingFixedHttpPort), &value);
166 network_session_params.testing_fixed_http_port = value;
168 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
169 int value;
170 base::StringToInt(command_line.GetSwitchValueASCII(
171 switches::kTestingFixedHttpsPort), &value);
172 network_session_params.testing_fixed_https_port = value;
174 if (command_line.HasSwitch(switches::kHostResolverRules)) {
175 scoped_ptr<net::MappedHostResolver> mapped_host_resolver(
176 new net::MappedHostResolver(host_resolver.Pass()));
177 mapped_host_resolver->SetRulesFromString(
178 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
179 host_resolver = mapped_host_resolver.Pass();
182 // Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
183 storage_->set_host_resolver(host_resolver.Pass());
184 network_session_params.host_resolver =
185 url_request_context_->host_resolver();
187 net::HttpCache* main_cache = new net::HttpCache(
188 network_session_params, main_backend);
189 storage_->set_http_transaction_factory(main_cache);
191 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
192 new net::URLRequestJobFactoryImpl());
193 // Keep ProtocolHandlers added in sync with
194 // ShellContentBrowserClient::IsHandledURL().
195 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_);
196 bool set_protocol = job_factory->SetProtocolHandler(
197 chrome::kDataScheme,
198 new net::DataProtocolHandler);
199 DCHECK(set_protocol);
200 set_protocol = job_factory->SetProtocolHandler(
201 chrome::kFileScheme,
202 new net::FileProtocolHandler);
203 DCHECK(set_protocol);
204 storage_->set_job_factory(job_factory.release());
207 return url_request_context_.get();
210 scoped_refptr<base::SingleThreadTaskRunner>
211 ShellURLRequestContextGetter::GetNetworkTaskRunner() const {
212 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
215 net::HostResolver* ShellURLRequestContextGetter::host_resolver() {
216 return url_request_context_->host_resolver();
219 } // namespace content