Revert of Disable spdy tests for constant failures on Volantis bot (patchset #2 id...
[chromium-blink-merge.git] / android_webview / browser / net / aw_url_request_context_getter.cc
bloba3d0a239f22ee1e1409058be537c23f1ac8efa17
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 "android_webview/browser/net/aw_url_request_context_getter.h"
7 #include <vector>
9 #include "android_webview/browser/aw_browser_context.h"
10 #include "android_webview/browser/aw_content_browser_client.h"
11 #include "android_webview/browser/aw_request_interceptor.h"
12 #include "android_webview/browser/net/aw_network_delegate.h"
13 #include "android_webview/browser/net/aw_url_request_job_factory.h"
14 #include "android_webview/browser/net/init_native_callback.h"
15 #include "android_webview/common/aw_content_client.h"
16 #include "base/bind.h"
17 #include "base/command_line.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/threading/worker_pool.h"
21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h"
23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h"
24 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"
25 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/content_browser_client.h"
28 #include "content/public/browser/cookie_store_factory.h"
29 #include "content/public/common/content_client.h"
30 #include "content/public/common/content_switches.h"
31 #include "content/public/common/url_constants.h"
32 #include "net/base/cache_type.h"
33 #include "net/base/net_log.h"
34 #include "net/cookies/cookie_store.h"
35 #include "net/dns/mapped_host_resolver.h"
36 #include "net/http/http_cache.h"
37 #include "net/http/http_stream_factory.h"
38 #include "net/proxy/proxy_service.h"
39 #include "net/socket/next_proto.h"
40 #include "net/ssl/default_channel_id_store.h"
41 #include "net/url_request/data_protocol_handler.h"
42 #include "net/url_request/file_protocol_handler.h"
43 #include "net/url_request/url_request_context_builder.h"
44 #include "net/url_request/url_request_context.h"
45 #include "net/url_request/url_request_intercepting_job_factory.h"
46 #include "net/url_request/url_request_interceptor.h"
48 using content::BrowserThread;
49 using data_reduction_proxy::DataReductionProxySettings;
51 namespace android_webview {
54 namespace {
56 void ApplyCmdlineOverridesToURLRequestContextBuilder(
57 net::URLRequestContextBuilder* builder) {
58 const base::CommandLine& command_line =
59 *base::CommandLine::ForCurrentProcess();
60 if (command_line.HasSwitch(switches::kHostResolverRules)) {
61 // If hostname remappings were specified on the command-line, layer these
62 // rules on top of the real host resolver. This allows forwarding all
63 // requests through a designated test server.
64 scoped_ptr<net::MappedHostResolver> host_resolver(
65 new net::MappedHostResolver(
66 net::HostResolver::CreateDefaultResolver(NULL)));
67 host_resolver->SetRulesFromString(
68 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
69 builder->set_host_resolver(host_resolver.release());
73 void ApplyCmdlineOverridesToNetworkSessionParams(
74 net::HttpNetworkSession::Params* params) {
75 int value;
76 const base::CommandLine& command_line =
77 *base::CommandLine::ForCurrentProcess();
78 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
79 base::StringToInt(command_line.GetSwitchValueASCII(
80 switches::kTestingFixedHttpPort), &value);
81 params->testing_fixed_http_port = value;
83 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
84 base::StringToInt(command_line.GetSwitchValueASCII(
85 switches::kTestingFixedHttpsPort), &value);
86 params->testing_fixed_https_port = value;
88 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors)) {
89 params->ignore_certificate_errors = true;
93 void PopulateNetworkSessionParams(
94 net::URLRequestContext* context,
95 net::HttpNetworkSession::Params* params) {
96 params->host_resolver = context->host_resolver();
97 params->cert_verifier = context->cert_verifier();
98 params->channel_id_service = context->channel_id_service();
99 params->transport_security_state = context->transport_security_state();
100 params->proxy_service = context->proxy_service();
101 params->ssl_config_service = context->ssl_config_service();
102 params->http_auth_handler_factory = context->http_auth_handler_factory();
103 params->network_delegate = context->network_delegate();
104 params->http_server_properties = context->http_server_properties();
105 params->net_log = context->net_log();
106 // TODO(sgurun) remove once crbug.com/329681 is fixed.
107 params->next_protos = net::NextProtosSpdy31();
108 params->use_alternate_protocols = true;
110 ApplyCmdlineOverridesToNetworkSessionParams(params);
113 scoped_ptr<net::URLRequestJobFactory> CreateJobFactory(
114 content::ProtocolHandlerMap* protocol_handlers,
115 content::URLRequestInterceptorScopedVector request_interceptors) {
116 scoped_ptr<AwURLRequestJobFactory> aw_job_factory(new AwURLRequestJobFactory);
117 bool set_protocol = aw_job_factory->SetProtocolHandler(
118 url::kFileScheme,
119 new net::FileProtocolHandler(
120 content::BrowserThread::GetBlockingPool()->
121 GetTaskRunnerWithShutdownBehavior(
122 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
123 DCHECK(set_protocol);
124 set_protocol = aw_job_factory->SetProtocolHandler(
125 url::kDataScheme, new net::DataProtocolHandler());
126 DCHECK(set_protocol);
127 set_protocol = aw_job_factory->SetProtocolHandler(
128 url::kBlobScheme,
129 (*protocol_handlers)[url::kBlobScheme].release());
130 DCHECK(set_protocol);
131 set_protocol = aw_job_factory->SetProtocolHandler(
132 url::kFileSystemScheme,
133 (*protocol_handlers)[url::kFileSystemScheme].release());
134 DCHECK(set_protocol);
135 set_protocol = aw_job_factory->SetProtocolHandler(
136 content::kChromeUIScheme,
137 (*protocol_handlers)[content::kChromeUIScheme].release());
138 DCHECK(set_protocol);
139 set_protocol = aw_job_factory->SetProtocolHandler(
140 content::kChromeDevToolsScheme,
141 (*protocol_handlers)[content::kChromeDevToolsScheme].release());
142 DCHECK(set_protocol);
143 protocol_handlers->clear();
145 // Note that even though the content:// scheme handler is created here,
146 // it cannot be used by child processes until access to it is granted via
147 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
148 // AwContentBrowserClient.
149 request_interceptors.push_back(
150 CreateAndroidContentRequestInterceptor().release());
151 request_interceptors.push_back(
152 CreateAndroidAssetFileRequestInterceptor().release());
153 // The AwRequestInterceptor must come after the content and asset file job
154 // factories. This for WebViewClassic compatibility where it was not
155 // possible to intercept resource loads to resolvable content:// and
156 // file:// URIs.
157 // This logical dependency is also the reason why the Content
158 // URLRequestInterceptor has to be added as an interceptor rather than as a
159 // ProtocolHandler.
160 request_interceptors.push_back(new AwRequestInterceptor());
162 // The chain of responsibility will execute the handlers in reverse to the
163 // order in which the elements of the chain are created.
164 scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
165 for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
166 request_interceptors.rbegin();
167 i != request_interceptors.rend();
168 ++i) {
169 job_factory.reset(new net::URLRequestInterceptingJobFactory(
170 job_factory.Pass(), make_scoped_ptr(*i)));
172 request_interceptors.weak_clear();
174 return job_factory.Pass();
177 } // namespace
179 AwURLRequestContextGetter::AwURLRequestContextGetter(
180 const base::FilePath& cache_path, net::CookieStore* cookie_store,
181 scoped_ptr<net::ProxyConfigService> config_service)
182 : cache_path_(cache_path),
183 cookie_store_(cookie_store),
184 net_log_(new net::NetLog()) {
185 proxy_config_service_ = config_service.Pass();
186 // CreateSystemProxyConfigService for Android must be called on main thread.
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
190 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
193 void AwURLRequestContextGetter::InitializeURLRequestContext() {
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
195 DCHECK(!url_request_context_);
197 net::URLRequestContextBuilder builder;
198 builder.set_user_agent(GetUserAgent());
199 scoped_ptr<AwNetworkDelegate> aw_network_delegate(new AwNetworkDelegate());
201 AwBrowserContext* browser_context = AwBrowserContext::GetDefault();
202 DCHECK(browser_context);
204 // Compression statistics are not gathered for WebView, so
205 // DataReductionProxyStatisticsPrefs is not instantiated and passed to the
206 // network delegate.
207 DataReductionProxySettings* data_reduction_proxy_settings =
208 browser_context->GetDataReductionProxySettings();
209 DCHECK(data_reduction_proxy_settings);
210 data_reduction_proxy_auth_request_handler_.reset(
211 new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
212 data_reduction_proxy::Client::WEBVIEW_ANDROID,
213 data_reduction_proxy_settings->params(),
214 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
216 data_reduction_proxy::DataReductionProxyNetworkDelegate*
217 data_reduction_proxy_network_delegate =
218 new data_reduction_proxy::DataReductionProxyNetworkDelegate(
219 aw_network_delegate.Pass(),
220 data_reduction_proxy_settings->params(),
221 data_reduction_proxy_auth_request_handler_.get(),
222 base::Bind(
223 &data_reduction_proxy::DataReductionProxyConfigurator::
224 GetProxyConfigOnIOThread,
225 base::Unretained(
226 browser_context->GetDataReductionProxyConfigurator())));
227 data_reduction_proxy_network_delegate->InitProxyConfigOverrider(
228 base::Bind(data_reduction_proxy::OnResolveProxyHandler));
230 builder.set_network_delegate(data_reduction_proxy_network_delegate);
231 #if !defined(DISABLE_FTP_SUPPORT)
232 builder.set_ftp_enabled(false); // Android WebView does not support ftp yet.
233 #endif
234 DCHECK(proxy_config_service_.get());
235 // Android provides a local HTTP proxy that handles all the proxying.
236 // Create the proxy without a resolver since we rely on this local HTTP proxy.
237 // TODO(sgurun) is this behavior guaranteed through SDK?
238 builder.set_proxy_service(
239 net::ProxyService::CreateWithoutProxyResolver(
240 proxy_config_service_.release(),
241 net_log_.get()));
242 builder.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
243 AwContentBrowserClient::GetAcceptLangsImpl()));
244 builder.set_net_log(net_log_.get());
245 builder.set_channel_id_enabled(false);
246 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
248 url_request_context_.reset(builder.Build());
249 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
250 net::HttpNetworkSession::Params network_session_params;
252 PopulateNetworkSessionParams(url_request_context_.get(),
253 &network_session_params);
255 net::HttpCache* main_cache = new net::HttpCache(
256 network_session_params,
257 new net::HttpCache::DefaultBackend(
258 net::DISK_CACHE,
259 net::CACHE_BACKEND_SIMPLE,
260 cache_path_,
261 20 * 1024 * 1024, // 20M
262 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
264 main_http_factory_.reset(main_cache);
265 url_request_context_->set_http_transaction_factory(main_cache);
266 url_request_context_->set_cookie_store(cookie_store_.get());
268 job_factory_ = CreateJobFactory(&protocol_handlers_,
269 request_interceptors_.Pass());
271 job_factory_.reset(new net::URLRequestInterceptingJobFactory(
272 job_factory_.Pass(), make_scoped_ptr(
273 new data_reduction_proxy::DataReductionProxyInterceptor(
274 data_reduction_proxy_settings->params(), NULL,
275 browser_context->GetDataReductionProxyEventStore()))));
276 url_request_context_->set_job_factory(job_factory_.get());
279 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
281 if (!url_request_context_)
282 InitializeURLRequestContext();
284 return url_request_context_.get();
287 scoped_refptr<base::SingleThreadTaskRunner>
288 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
289 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
292 void AwURLRequestContextGetter::SetHandlersAndInterceptors(
293 content::ProtocolHandlerMap* protocol_handlers,
294 content::URLRequestInterceptorScopedVector request_interceptors) {
295 std::swap(protocol_handlers_, *protocol_handlers);
296 request_interceptors_.swap(request_interceptors);
299 data_reduction_proxy::DataReductionProxyAuthRequestHandler*
300 AwURLRequestContextGetter::GetDataReductionProxyAuthRequestHandler() const {
301 return data_reduction_proxy_auth_request_handler_.get();
304 net::NetLog* AwURLRequestContextGetter::GetNetLog() {
305 return net_log_.get();
308 void AwURLRequestContextGetter::SetKeyOnIO(const std::string& key) {
309 DCHECK(data_reduction_proxy_auth_request_handler_);
310 data_reduction_proxy_auth_request_handler_->InitAuthentication(key);
313 } // namespace android_webview