Reduce page_cycler repeat count from 10 to 6
[chromium-blink-merge.git] / android_webview / browser / net / aw_url_request_context_getter.cc
blobf00ef59a53c6d9d23a5bbf459ba467629eb4a1c8
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/command_line.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/threading/worker_pool.h"
20 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_service.h"
22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h"
23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"
24 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/content_browser_client.h"
27 #include "content/public/browser/cookie_store_factory.h"
28 #include "content/public/common/content_client.h"
29 #include "content/public/common/content_switches.h"
30 #include "content/public/common/url_constants.h"
31 #include "net/base/cache_type.h"
32 #include "net/base/net_log.h"
33 #include "net/cookies/cookie_store.h"
34 #include "net/dns/mapped_host_resolver.h"
35 #include "net/http/http_cache.h"
36 #include "net/http/http_stream_factory.h"
37 #include "net/proxy/proxy_service.h"
38 #include "net/socket/next_proto.h"
39 #include "net/ssl/default_channel_id_store.h"
40 #include "net/url_request/data_protocol_handler.h"
41 #include "net/url_request/file_protocol_handler.h"
42 #include "net/url_request/url_request_context_builder.h"
43 #include "net/url_request/url_request_context.h"
44 #include "net/url_request/url_request_intercepting_job_factory.h"
45 #include "net/url_request/url_request_interceptor.h"
47 using content::BrowserThread;
48 using data_reduction_proxy::DataReductionProxySettings;
50 namespace android_webview {
53 namespace {
55 void ApplyCmdlineOverridesToURLRequestContextBuilder(
56 net::URLRequestContextBuilder* builder) {
57 const base::CommandLine& command_line =
58 *base::CommandLine::ForCurrentProcess();
59 if (command_line.HasSwitch(switches::kHostResolverRules)) {
60 // If hostname remappings were specified on the command-line, layer these
61 // rules on top of the real host resolver. This allows forwarding all
62 // requests through a designated test server.
63 scoped_ptr<net::MappedHostResolver> host_resolver(
64 new net::MappedHostResolver(
65 net::HostResolver::CreateDefaultResolver(NULL)));
66 host_resolver->SetRulesFromString(
67 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
68 builder->set_host_resolver(host_resolver.release());
72 void ApplyCmdlineOverridesToNetworkSessionParams(
73 net::HttpNetworkSession::Params* params) {
74 int value;
75 const base::CommandLine& command_line =
76 *base::CommandLine::ForCurrentProcess();
77 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
78 base::StringToInt(command_line.GetSwitchValueASCII(
79 switches::kTestingFixedHttpPort), &value);
80 params->testing_fixed_http_port = value;
82 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
83 base::StringToInt(command_line.GetSwitchValueASCII(
84 switches::kTestingFixedHttpsPort), &value);
85 params->testing_fixed_https_port = value;
87 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors)) {
88 params->ignore_certificate_errors = true;
92 void PopulateNetworkSessionParams(
93 net::URLRequestContext* context,
94 net::HttpNetworkSession::Params* params) {
95 params->host_resolver = context->host_resolver();
96 params->cert_verifier = context->cert_verifier();
97 params->channel_id_service = context->channel_id_service();
98 params->transport_security_state = context->transport_security_state();
99 params->proxy_service = context->proxy_service();
100 params->ssl_config_service = context->ssl_config_service();
101 params->http_auth_handler_factory = context->http_auth_handler_factory();
102 params->network_delegate = context->network_delegate();
103 params->http_server_properties = context->http_server_properties();
104 params->net_log = context->net_log();
105 // TODO(sgurun) remove once crbug.com/329681 is fixed.
106 params->next_protos = net::NextProtosSpdy31();
107 params->use_alternate_protocols = true;
109 ApplyCmdlineOverridesToNetworkSessionParams(params);
112 scoped_ptr<net::URLRequestJobFactory> CreateJobFactory(
113 content::ProtocolHandlerMap* protocol_handlers,
114 content::URLRequestInterceptorScopedVector request_interceptors) {
115 scoped_ptr<AwURLRequestJobFactory> aw_job_factory(new AwURLRequestJobFactory);
116 bool set_protocol = aw_job_factory->SetProtocolHandler(
117 url::kFileScheme,
118 new net::FileProtocolHandler(
119 content::BrowserThread::GetBlockingPool()->
120 GetTaskRunnerWithShutdownBehavior(
121 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
122 DCHECK(set_protocol);
123 set_protocol = aw_job_factory->SetProtocolHandler(
124 url::kDataScheme, new net::DataProtocolHandler());
125 DCHECK(set_protocol);
126 set_protocol = aw_job_factory->SetProtocolHandler(
127 url::kBlobScheme,
128 (*protocol_handlers)[url::kBlobScheme].release());
129 DCHECK(set_protocol);
130 set_protocol = aw_job_factory->SetProtocolHandler(
131 url::kFileSystemScheme,
132 (*protocol_handlers)[url::kFileSystemScheme].release());
133 DCHECK(set_protocol);
134 set_protocol = aw_job_factory->SetProtocolHandler(
135 content::kChromeUIScheme,
136 (*protocol_handlers)[content::kChromeUIScheme].release());
137 DCHECK(set_protocol);
138 set_protocol = aw_job_factory->SetProtocolHandler(
139 content::kChromeDevToolsScheme,
140 (*protocol_handlers)[content::kChromeDevToolsScheme].release());
141 DCHECK(set_protocol);
142 protocol_handlers->clear();
144 // Note that even though the content:// scheme handler is created here,
145 // it cannot be used by child processes until access to it is granted via
146 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
147 // AwContentBrowserClient.
148 request_interceptors.push_back(
149 CreateAndroidContentRequestInterceptor().release());
150 request_interceptors.push_back(
151 CreateAndroidAssetFileRequestInterceptor().release());
152 // The AwRequestInterceptor must come after the content and asset file job
153 // factories. This for WebViewClassic compatibility where it was not
154 // possible to intercept resource loads to resolvable content:// and
155 // file:// URIs.
156 // This logical dependency is also the reason why the Content
157 // URLRequestInterceptor has to be added as an interceptor rather than as a
158 // ProtocolHandler.
159 request_interceptors.push_back(new AwRequestInterceptor());
161 // The chain of responsibility will execute the handlers in reverse to the
162 // order in which the elements of the chain are created.
163 scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
164 for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
165 request_interceptors.rbegin();
166 i != request_interceptors.rend();
167 ++i) {
168 job_factory.reset(new net::URLRequestInterceptingJobFactory(
169 job_factory.Pass(), make_scoped_ptr(*i)));
171 request_interceptors.weak_clear();
173 return job_factory.Pass();
176 } // namespace
178 AwURLRequestContextGetter::AwURLRequestContextGetter(
179 const base::FilePath& cache_path, net::CookieStore* cookie_store,
180 scoped_ptr<data_reduction_proxy::DataReductionProxyConfigService>
181 config_service)
182 : cache_path_(cache_path),
183 cookie_store_(cookie_store),
184 net_log_(new net::NetLog()) {
185 data_reduction_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 data_reduction_proxy::DataReductionProxyNetworkDelegate::
223 ProxyConfigGetter());
225 builder.set_network_delegate(data_reduction_proxy_network_delegate);
226 #if !defined(DISABLE_FTP_SUPPORT)
227 builder.set_ftp_enabled(false); // Android WebView does not support ftp yet.
228 #endif
229 DCHECK(data_reduction_proxy_config_service_.get());
230 // Android provides a local HTTP proxy that handles all the proxying.
231 // Create the proxy without a resolver since we rely on this local HTTP proxy.
232 // TODO(sgurun) is this behavior guaranteed through SDK?
233 builder.set_proxy_service(
234 net::ProxyService::CreateWithoutProxyResolver(
235 data_reduction_proxy_config_service_.release(),
236 net_log_.get()));
237 builder.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
238 AwContentBrowserClient::GetAcceptLangsImpl()));
239 builder.set_net_log(net_log_.get());
240 builder.set_channel_id_enabled(false);
241 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
243 url_request_context_.reset(builder.Build());
244 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
245 net::HttpNetworkSession::Params network_session_params;
247 PopulateNetworkSessionParams(url_request_context_.get(),
248 &network_session_params);
250 net::HttpCache* main_cache = new net::HttpCache(
251 network_session_params,
252 new net::HttpCache::DefaultBackend(
253 net::DISK_CACHE,
254 net::CACHE_BACKEND_SIMPLE,
255 cache_path_,
256 20 * 1024 * 1024, // 20M
257 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
259 main_http_factory_.reset(main_cache);
260 url_request_context_->set_http_transaction_factory(main_cache);
261 url_request_context_->set_cookie_store(cookie_store_.get());
263 job_factory_ = CreateJobFactory(&protocol_handlers_,
264 request_interceptors_.Pass());
266 job_factory_.reset(new net::URLRequestInterceptingJobFactory(
267 job_factory_.Pass(), make_scoped_ptr(
268 new data_reduction_proxy::DataReductionProxyInterceptor(
269 data_reduction_proxy_settings->params(), NULL,
270 browser_context->GetDataReductionProxyEventStore()))));
271 url_request_context_->set_job_factory(job_factory_.get());
274 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
276 if (!url_request_context_)
277 InitializeURLRequestContext();
279 return url_request_context_.get();
282 scoped_refptr<base::SingleThreadTaskRunner>
283 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
284 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
287 void AwURLRequestContextGetter::SetHandlersAndInterceptors(
288 content::ProtocolHandlerMap* protocol_handlers,
289 content::URLRequestInterceptorScopedVector request_interceptors) {
290 std::swap(protocol_handlers_, *protocol_handlers);
291 request_interceptors_.swap(request_interceptors);
294 data_reduction_proxy::DataReductionProxyAuthRequestHandler*
295 AwURLRequestContextGetter::GetDataReductionProxyAuthRequestHandler() const {
296 return data_reduction_proxy_auth_request_handler_.get();
299 net::NetLog* AwURLRequestContextGetter::GetNetLog() {
300 return net_log_.get();
303 void AwURLRequestContextGetter::SetKeyOnIO(const std::string& key) {
304 DCHECK(data_reduction_proxy_auth_request_handler_);
305 data_reduction_proxy_auth_request_handler_->InitAuthentication(key);
308 } // namespace android_webview