Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / android_webview / browser / net / aw_url_request_context_getter.cc
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.
4
5 #include "android_webview/browser/net/aw_url_request_context_getter.h"
6
7 #include <vector>
8
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/browser/data_reduction_proxy_auth_request_handler.h"
21 #include "components/data_reduction_proxy/browser/data_reduction_proxy_config_service.h"
22 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/content_browser_client.h"
25 #include "content/public/browser/cookie_store_factory.h"
26 #include "content/public/common/content_client.h"
27 #include "content/public/common/content_switches.h"
28 #include "content/public/common/url_constants.h"
29 #include "net/base/cache_type.h"
30 #include "net/cookies/cookie_store.h"
31 #include "net/dns/mapped_host_resolver.h"
32 #include "net/http/http_cache.h"
33 #include "net/http/http_stream_factory.h"
34 #include "net/proxy/proxy_service.h"
35 #include "net/socket/next_proto.h"
36 #include "net/ssl/default_channel_id_store.h"
37 #include "net/url_request/data_protocol_handler.h"
38 #include "net/url_request/file_protocol_handler.h"
39 #include "net/url_request/url_request_context_builder.h"
40 #include "net/url_request/url_request_context.h"
41 #include "net/url_request/url_request_intercepting_job_factory.h"
42 #include "net/url_request/url_request_interceptor.h"
43
44 using content::BrowserThread;
45 using data_reduction_proxy::DataReductionProxySettings;
46
47 namespace android_webview {
48
49
50 namespace {
51
52 void ApplyCmdlineOverridesToURLRequestContextBuilder(
53     net::URLRequestContextBuilder* builder) {
54   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
55   if (command_line.HasSwitch(switches::kHostResolverRules)) {
56     // If hostname remappings were specified on the command-line, layer these
57     // rules on top of the real host resolver. This allows forwarding all
58     // requests through a designated test server.
59     scoped_ptr<net::MappedHostResolver> host_resolver(
60         new net::MappedHostResolver(
61             net::HostResolver::CreateDefaultResolver(NULL)));
62     host_resolver->SetRulesFromString(
63         command_line.GetSwitchValueASCII(switches::kHostResolverRules));
64     builder->set_host_resolver(host_resolver.release());
65   }
66 }
67
68 void ApplyCmdlineOverridesToNetworkSessionParams(
69     net::HttpNetworkSession::Params* params) {
70   int value;
71   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
72   if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
73     base::StringToInt(command_line.GetSwitchValueASCII(
74         switches::kTestingFixedHttpPort), &value);
75     params->testing_fixed_http_port = value;
76   }
77   if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
78     base::StringToInt(command_line.GetSwitchValueASCII(
79         switches::kTestingFixedHttpsPort), &value);
80     params->testing_fixed_https_port = value;
81   }
82   if (command_line.HasSwitch(switches::kIgnoreCertificateErrors)) {
83     params->ignore_certificate_errors = true;
84   }
85 }
86
87 void PopulateNetworkSessionParams(
88     net::URLRequestContext* context,
89     net::HttpNetworkSession::Params* params) {
90   params->host_resolver = context->host_resolver();
91   params->cert_verifier = context->cert_verifier();
92   params->channel_id_service = context->channel_id_service();
93   params->transport_security_state = context->transport_security_state();
94   params->proxy_service = context->proxy_service();
95   params->ssl_config_service = context->ssl_config_service();
96   params->http_auth_handler_factory = context->http_auth_handler_factory();
97   params->network_delegate = context->network_delegate();
98   params->http_server_properties = context->http_server_properties();
99   params->net_log = context->net_log();
100   // TODO(sgurun) remove once crbug.com/329681 is fixed.
101   params->next_protos = net::NextProtosSpdy31();
102   params->use_alternate_protocols = true;
103
104   ApplyCmdlineOverridesToNetworkSessionParams(params);
105 }
106
107 scoped_ptr<net::URLRequestJobFactory> CreateJobFactory(
108     content::ProtocolHandlerMap* protocol_handlers,
109     content::URLRequestInterceptorScopedVector request_interceptors) {
110   scoped_ptr<AwURLRequestJobFactory> aw_job_factory(new AwURLRequestJobFactory);
111   bool set_protocol = aw_job_factory->SetProtocolHandler(
112       url::kFileScheme,
113       new net::FileProtocolHandler(
114           content::BrowserThread::GetBlockingPool()->
115               GetTaskRunnerWithShutdownBehavior(
116                   base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
117   DCHECK(set_protocol);
118   set_protocol = aw_job_factory->SetProtocolHandler(
119       url::kDataScheme, new net::DataProtocolHandler());
120   DCHECK(set_protocol);
121   set_protocol = aw_job_factory->SetProtocolHandler(
122       url::kBlobScheme,
123       (*protocol_handlers)[url::kBlobScheme].release());
124   DCHECK(set_protocol);
125   set_protocol = aw_job_factory->SetProtocolHandler(
126       url::kFileSystemScheme,
127       (*protocol_handlers)[url::kFileSystemScheme].release());
128   DCHECK(set_protocol);
129   set_protocol = aw_job_factory->SetProtocolHandler(
130       content::kChromeUIScheme,
131       (*protocol_handlers)[content::kChromeUIScheme].release());
132   DCHECK(set_protocol);
133   set_protocol = aw_job_factory->SetProtocolHandler(
134       content::kChromeDevToolsScheme,
135       (*protocol_handlers)[content::kChromeDevToolsScheme].release());
136   DCHECK(set_protocol);
137   protocol_handlers->clear();
138
139   // Note that even though the content:// scheme handler is created here,
140   // it cannot be used by child processes until access to it is granted via
141   // ChildProcessSecurityPolicy::GrantScheme(). This is done in
142   // AwContentBrowserClient.
143   request_interceptors.push_back(
144       CreateAndroidContentRequestInterceptor().release());
145   request_interceptors.push_back(
146       CreateAndroidAssetFileRequestInterceptor().release());
147   // The AwRequestInterceptor must come after the content and asset file job
148   // factories. This for WebViewClassic compatibility where it was not
149   // possible to intercept resource loads to resolvable content:// and
150   // file:// URIs.
151   // This logical dependency is also the reason why the Content
152   // URLRequestInterceptor has to be added as an interceptor rather than as a
153   // ProtocolHandler.
154   request_interceptors.push_back(new AwRequestInterceptor());
155
156   // The chain of responsibility will execute the handlers in reverse to the
157   // order in which the elements of the chain are created.
158   scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
159   for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
160            request_interceptors.rbegin();
161        i != request_interceptors.rend();
162        ++i) {
163     job_factory.reset(new net::URLRequestInterceptingJobFactory(
164         job_factory.Pass(), make_scoped_ptr(*i)));
165   }
166   request_interceptors.weak_clear();
167
168   return job_factory.Pass();
169 }
170
171 }  // namespace
172
173 AwURLRequestContextGetter::AwURLRequestContextGetter(
174     const base::FilePath& partition_path, net::CookieStore* cookie_store,
175     scoped_ptr<data_reduction_proxy::DataReductionProxyConfigService>
176         config_service)
177     : partition_path_(partition_path),
178       cookie_store_(cookie_store) {
179   data_reduction_proxy_config_service_ = config_service.Pass();
180   // CreateSystemProxyConfigService for Android must be called on main thread.
181   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
182 }
183
184 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
185 }
186
187 void AwURLRequestContextGetter::InitializeURLRequestContext() {
188   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
189   DCHECK(!url_request_context_);
190
191   net::URLRequestContextBuilder builder;
192   builder.set_user_agent(GetUserAgent());
193   AwNetworkDelegate* aw_network_delegate = new AwNetworkDelegate();
194   builder.set_network_delegate(aw_network_delegate);
195 #if !defined(DISABLE_FTP_SUPPORT)
196   builder.set_ftp_enabled(false);  // Android WebView does not support ftp yet.
197 #endif
198   if (data_reduction_proxy_config_service_.get()) {
199     builder.set_proxy_config_service(
200         data_reduction_proxy_config_service_.release());
201   } else {
202     builder.set_proxy_config_service(
203         net::ProxyService::CreateSystemProxyConfigService(
204             GetNetworkTaskRunner(), NULL /* Ignored on Android */ ));
205   }
206   builder.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
207       AwContentBrowserClient::GetAcceptLangsImpl()));
208   ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
209
210   url_request_context_.reset(builder.Build());
211   // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
212   net::HttpNetworkSession::Params network_session_params;
213
214   PopulateNetworkSessionParams(url_request_context_.get(),
215                                &network_session_params);
216
217   net::HttpCache* main_cache = new net::HttpCache(
218       network_session_params,
219       new net::HttpCache::DefaultBackend(
220           net::DISK_CACHE,
221           net::CACHE_BACKEND_SIMPLE,
222           partition_path_.Append(FILE_PATH_LITERAL("Cache")),
223           20 * 1024 * 1024,  // 20M
224           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
225
226 #if defined(SPDY_PROXY_AUTH_ORIGIN)
227   AwBrowserContext* browser_context = AwBrowserContext::GetDefault();
228   DCHECK(browser_context);
229   DataReductionProxySettings* data_reduction_proxy_settings =
230       browser_context->GetDataReductionProxySettings();
231   DCHECK(data_reduction_proxy_settings);
232   data_reduction_proxy_auth_request_handler_.reset(
233       new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
234           data_reduction_proxy::kClientAndroidWebview,
235           data_reduction_proxy_settings->params(),
236           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
237
238   aw_network_delegate->set_data_reduction_proxy_params(
239       data_reduction_proxy_settings->params());
240   aw_network_delegate->set_data_reduction_proxy_auth_request_handler(
241       data_reduction_proxy_auth_request_handler_.get());
242 #endif
243
244   main_http_factory_.reset(main_cache);
245   url_request_context_->set_http_transaction_factory(main_cache);
246   url_request_context_->set_cookie_store(cookie_store_);
247
248   job_factory_ = CreateJobFactory(&protocol_handlers_,
249                                   request_interceptors_.Pass());
250   url_request_context_->set_job_factory(job_factory_.get());
251 }
252
253 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
254   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
255   if (!url_request_context_)
256     InitializeURLRequestContext();
257
258   return url_request_context_.get();
259 }
260
261 scoped_refptr<base::SingleThreadTaskRunner>
262 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
263   return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
264 }
265
266 void AwURLRequestContextGetter::SetHandlersAndInterceptors(
267     content::ProtocolHandlerMap* protocol_handlers,
268     content::URLRequestInterceptorScopedVector request_interceptors) {
269   std::swap(protocol_handlers_, *protocol_handlers);
270   request_interceptors_.swap(request_interceptors);
271 }
272
273 data_reduction_proxy::DataReductionProxyAuthRequestHandler*
274 AwURLRequestContextGetter::GetDataReductionProxyAuthRequestHandler() const {
275   return data_reduction_proxy_auth_request_handler_.get();
276 }
277
278 }  // namespace android_webview