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