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"
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"
44 using content::BrowserThread
;
45 using data_reduction_proxy::DataReductionProxySettings
;
47 namespace android_webview
{
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());
68 void ApplyCmdlineOverridesToNetworkSessionParams(
69 net::HttpNetworkSession::Params
* params
) {
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
;
77 if (command_line
.HasSwitch(switches::kTestingFixedHttpsPort
)) {
78 base::StringToInt(command_line
.GetSwitchValueASCII(
79 switches::kTestingFixedHttpsPort
), &value
);
80 params
->testing_fixed_https_port
= value
;
82 if (command_line
.HasSwitch(switches::kIgnoreCertificateErrors
)) {
83 params
->ignore_certificate_errors
= true;
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;
104 ApplyCmdlineOverridesToNetworkSessionParams(params
);
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(
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(
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();
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
151 // This logical dependency is also the reason why the Content
152 // URLRequestInterceptor has to be added as an interceptor rather than as a
154 request_interceptors
.push_back(new AwRequestInterceptor());
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();
163 job_factory
.reset(new net::URLRequestInterceptingJobFactory(
164 job_factory
.Pass(), make_scoped_ptr(*i
)));
166 request_interceptors
.weak_clear();
168 return job_factory
.Pass();
173 AwURLRequestContextGetter::AwURLRequestContextGetter(
174 const base::FilePath
& partition_path
, net::CookieStore
* cookie_store
,
175 scoped_ptr
<data_reduction_proxy::DataReductionProxyConfigService
>
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
));
184 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
187 void AwURLRequestContextGetter::InitializeURLRequestContext() {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
189 DCHECK(!url_request_context_
);
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.
198 if (data_reduction_proxy_config_service_
.get()) {
199 builder
.set_proxy_config_service(
200 data_reduction_proxy_config_service_
.release());
202 builder
.set_proxy_config_service(
203 net::ProxyService::CreateSystemProxyConfigService(
204 GetNetworkTaskRunner(), NULL
/* Ignored on Android */ ));
206 builder
.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
207 AwContentBrowserClient::GetAcceptLangsImpl()));
208 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder
);
210 url_request_context_
.reset(builder
.Build());
211 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
212 net::HttpNetworkSession::Params network_session_params
;
214 PopulateNetworkSessionParams(url_request_context_
.get(),
215 &network_session_params
);
217 net::HttpCache
* main_cache
= new net::HttpCache(
218 network_session_params
,
219 new net::HttpCache::DefaultBackend(
221 net::CACHE_BACKEND_SIMPLE
,
222 partition_path_
.Append(FILE_PATH_LITERAL("Cache")),
223 20 * 1024 * 1024, // 20M
224 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
)));
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::kAndroidWebViewProtocolVersion
,
236 data_reduction_proxy_settings
->params(),
237 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
)));
239 aw_network_delegate
->set_data_reduction_proxy_params(
240 data_reduction_proxy_settings
->params());
241 aw_network_delegate
->set_data_reduction_proxy_auth_request_handler(
242 data_reduction_proxy_auth_request_handler_
.get());
245 main_http_factory_
.reset(main_cache
);
246 url_request_context_
->set_http_transaction_factory(main_cache
);
247 url_request_context_
->set_cookie_store(cookie_store_
);
249 job_factory_
= CreateJobFactory(&protocol_handlers_
,
250 request_interceptors_
.Pass());
251 url_request_context_
->set_job_factory(job_factory_
.get());
254 net::URLRequestContext
* AwURLRequestContextGetter::GetURLRequestContext() {
255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
256 if (!url_request_context_
)
257 InitializeURLRequestContext();
259 return url_request_context_
.get();
262 scoped_refptr
<base::SingleThreadTaskRunner
>
263 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
264 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
);
267 void AwURLRequestContextGetter::SetHandlersAndInterceptors(
268 content::ProtocolHandlerMap
* protocol_handlers
,
269 content::URLRequestInterceptorScopedVector request_interceptors
) {
270 std::swap(protocol_handlers_
, *protocol_handlers
);
271 request_interceptors_
.swap(request_interceptors
);
274 data_reduction_proxy::DataReductionProxyAuthRequestHandler
*
275 AwURLRequestContextGetter::GetDataReductionProxyAuthRequestHandler() const {
276 return data_reduction_proxy_auth_request_handler_
.get();
279 } // namespace android_webview