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 "components/data_reduction_proxy/browser/http_auth_handler_data_reduction_proxy.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_server_bound_cert_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;
84 void PopulateNetworkSessionParams(
85 net::URLRequestContext* context,
86 net::HttpNetworkSession::Params* params) {
87 params->host_resolver = context->host_resolver();
88 params->cert_verifier = context->cert_verifier();
89 params->server_bound_cert_service = context->server_bound_cert_service();
90 params->transport_security_state = context->transport_security_state();
91 params->proxy_service = context->proxy_service();
92 params->ssl_config_service = context->ssl_config_service();
93 params->http_auth_handler_factory = context->http_auth_handler_factory();
94 params->network_delegate = context->network_delegate();
95 params->http_server_properties = context->http_server_properties();
96 params->net_log = context->net_log();
98 // TODO(sgurun) remove once crbug.com/329681 is fixed.
99 params->next_protos = net::NextProtosSpdy31();
100 params->use_alternate_protocols = true;
102 ApplyCmdlineOverridesToNetworkSessionParams(params);
105 scoped_ptr<net::URLRequestJobFactory> CreateJobFactory(
106 content::ProtocolHandlerMap* protocol_handlers,
107 content::URLRequestInterceptorScopedVector request_interceptors) {
108 scoped_ptr<AwURLRequestJobFactory> aw_job_factory(new AwURLRequestJobFactory);
109 bool set_protocol = aw_job_factory->SetProtocolHandler(
111 new net::FileProtocolHandler(
112 content::BrowserThread::GetBlockingPool()->
113 GetTaskRunnerWithShutdownBehavior(
114 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
115 DCHECK(set_protocol);
116 set_protocol = aw_job_factory->SetProtocolHandler(
117 url::kDataScheme, new net::DataProtocolHandler());
118 DCHECK(set_protocol);
119 set_protocol = aw_job_factory->SetProtocolHandler(
121 (*protocol_handlers)[url::kBlobScheme].release());
122 DCHECK(set_protocol);
123 set_protocol = aw_job_factory->SetProtocolHandler(
124 url::kFileSystemScheme,
125 (*protocol_handlers)[url::kFileSystemScheme].release());
126 DCHECK(set_protocol);
127 set_protocol = aw_job_factory->SetProtocolHandler(
128 content::kChromeUIScheme,
129 (*protocol_handlers)[content::kChromeUIScheme].release());
130 DCHECK(set_protocol);
131 set_protocol = aw_job_factory->SetProtocolHandler(
132 content::kChromeDevToolsScheme,
133 (*protocol_handlers)[content::kChromeDevToolsScheme].release());
134 DCHECK(set_protocol);
135 protocol_handlers->clear();
137 // Note that even though the content:// scheme handler is created here,
138 // it cannot be used by child processes until access to it is granted via
139 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
140 // AwContentBrowserClient.
141 request_interceptors.push_back(
142 CreateAndroidContentRequestInterceptor().release());
143 request_interceptors.push_back(
144 CreateAndroidAssetFileRequestInterceptor().release());
145 // The AwRequestInterceptor must come after the content and asset file job
146 // factories. This for WebViewClassic compatibility where it was not
147 // possible to intercept resource loads to resolvable content:// and
149 // This logical dependency is also the reason why the Content
150 // URLRequestInterceptor has to be added as an interceptor rather than as a
152 request_interceptors.push_back(new AwRequestInterceptor());
154 // The chain of responsibility will execute the handlers in reverse to the
155 // order in which the elements of the chain are created.
156 scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
157 for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
158 request_interceptors.rbegin();
159 i != request_interceptors.rend();
161 job_factory.reset(new net::URLRequestInterceptingJobFactory(
162 job_factory.Pass(), make_scoped_ptr(*i)));
164 request_interceptors.weak_clear();
166 return job_factory.Pass();
171 AwURLRequestContextGetter::AwURLRequestContextGetter(
172 const base::FilePath& partition_path, net::CookieStore* cookie_store)
173 : partition_path_(partition_path),
174 cookie_store_(cookie_store),
175 proxy_config_service_(new DataReductionProxyConfigService(
176 scoped_ptr<net::ProxyConfigService>(
177 net::ProxyService::CreateSystemProxyConfigService(
178 GetNetworkTaskRunner(),
179 NULL /* Ignored on Android */)).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 builder.set_proxy_config_service(proxy_config_service_.release());
199 builder.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
200 AwContentBrowserClient::GetAcceptLangsImpl()));
201 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
203 #if defined(SPDY_PROXY_AUTH_ORIGIN)
204 data_reduction_proxy::DataReductionProxyParams drp_params(
205 data_reduction_proxy::DataReductionProxyParams::kAllowed);
206 builder.add_http_auth_handler_factory(
207 data_reduction_proxy::HttpAuthHandlerDataReductionProxy::Scheme(),
208 new data_reduction_proxy::HttpAuthHandlerDataReductionProxy::Factory(
209 drp_params.GetAllowedProxies()));
212 url_request_context_.reset(builder.Build());
213 server_bound_cert_service_.reset(
214 new net::ServerBoundCertService(
215 new net::DefaultServerBoundCertStore(NULL),
216 base::WorkerPool::GetTaskRunner(true)));
217 url_request_context_->set_server_bound_cert_service(
218 server_bound_cert_service_.get());
219 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
220 net::HttpNetworkSession::Params network_session_params;
222 PopulateNetworkSessionParams(url_request_context_.get(),
223 &network_session_params);
225 net::HttpCache* main_cache = new net::HttpCache(
226 network_session_params,
227 new net::HttpCache::DefaultBackend(
229 net::CACHE_BACKEND_SIMPLE,
230 partition_path_.Append(FILE_PATH_LITERAL("Cache")),
231 20 * 1024 * 1024, // 20M
232 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
234 #if defined(SPDY_PROXY_AUTH_ORIGIN)
235 AwBrowserContext* browser_context = AwBrowserContext::GetDefault();
236 DCHECK(browser_context);
237 DataReductionProxySettings* drp_settings =
238 browser_context->GetDataReductionProxySettings();
240 aw_network_delegate->set_data_reduction_proxy_params(
241 drp_settings->params());
242 std::string drp_key = drp_settings->params()->key();
243 // Only precache credentials if a key is available at URLRequestContext
245 if (!drp_key.empty()) {
246 DataReductionProxySettings::InitDataReductionProxySession(
247 main_cache->GetSession(), &drp_params);
252 main_http_factory_.reset(main_cache);
253 url_request_context_->set_http_transaction_factory(main_cache);
254 url_request_context_->set_cookie_store(cookie_store_);
256 job_factory_ = CreateJobFactory(&protocol_handlers_,
257 request_interceptors_.Pass());
258 url_request_context_->set_job_factory(job_factory_.get());
261 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
263 if (!url_request_context_)
264 InitializeURLRequestContext();
266 return url_request_context_.get();
269 scoped_refptr<base::SingleThreadTaskRunner>
270 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
271 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
274 void AwURLRequestContextGetter::SetHandlersAndInterceptors(
275 content::ProtocolHandlerMap* protocol_handlers,
276 content::URLRequestInterceptorScopedVector request_interceptors) {
277 std::swap(protocol_handlers_, *protocol_handlers);
278 request_interceptors_.swap(request_interceptors);
281 DataReductionProxyConfigService*
282 AwURLRequestContextGetter::proxy_config_service() {
283 // TODO(bengr): return system config if data reduction proxy is disabled.
284 return proxy_config_service_.get();
287 } // namespace android_webview