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_http_user_agent_settings.h"
13 #include "android_webview/browser/net/aw_network_delegate.h"
14 #include "android_webview/browser/net/aw_url_request_job_factory.h"
15 #include "android_webview/browser/net/init_native_callback.h"
16 #include "android_webview/common/aw_content_client.h"
17 #include "base/bind.h"
18 #include "base/command_line.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/threading/sequenced_worker_pool.h"
21 #include "base/threading/worker_pool.h"
22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.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_request_options.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/cookies/cookie_store.h"
33 #include "net/dns/mapped_host_resolver.h"
34 #include "net/http/http_cache.h"
35 #include "net/http/http_stream_factory.h"
36 #include "net/log/net_log.h"
37 #include "net/proxy/proxy_service.h"
38 #include "net/socket/next_proto.h"
39 #include "net/ssl/channel_id_service.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.h"
43 #include "net/url_request/url_request_context_builder.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
;
49 namespace android_webview
{
54 void ApplyCmdlineOverridesToURLRequestContextBuilder(
55 net::URLRequestContextBuilder
* builder
) {
56 const base::CommandLine
& command_line
=
57 *base::CommandLine::ForCurrentProcess();
58 if (command_line
.HasSwitch(switches::kHostResolverRules
)) {
59 // If hostname remappings were specified on the command-line, layer these
60 // rules on top of the real host resolver. This allows forwarding all
61 // requests through a designated test server.
62 scoped_ptr
<net::MappedHostResolver
> host_resolver(
63 new net::MappedHostResolver(
64 net::HostResolver::CreateDefaultResolver(NULL
)));
65 host_resolver
->SetRulesFromString(
66 command_line
.GetSwitchValueASCII(switches::kHostResolverRules
));
67 builder
->set_host_resolver(host_resolver
.Pass());
71 void ApplyCmdlineOverridesToNetworkSessionParams(
72 net::HttpNetworkSession::Params
* params
) {
74 const base::CommandLine
& command_line
=
75 *base::CommandLine::ForCurrentProcess();
76 if (command_line
.HasSwitch(switches::kTestingFixedHttpPort
)) {
77 base::StringToInt(command_line
.GetSwitchValueASCII(
78 switches::kTestingFixedHttpPort
), &value
);
79 params
->testing_fixed_http_port
= value
;
81 if (command_line
.HasSwitch(switches::kTestingFixedHttpsPort
)) {
82 base::StringToInt(command_line
.GetSwitchValueASCII(
83 switches::kTestingFixedHttpsPort
), &value
);
84 params
->testing_fixed_https_port
= value
;
86 if (command_line
.HasSwitch(switches::kIgnoreCertificateErrors
)) {
87 params
->ignore_certificate_errors
= true;
91 void PopulateNetworkSessionParams(
92 net::URLRequestContext
* context
,
93 net::HttpNetworkSession::Params
* params
) {
94 params
->host_resolver
= context
->host_resolver();
95 params
->cert_verifier
= context
->cert_verifier();
96 params
->channel_id_service
= context
->channel_id_service();
97 params
->transport_security_state
= context
->transport_security_state();
98 params
->proxy_service
= context
->proxy_service();
99 params
->ssl_config_service
= context
->ssl_config_service();
100 params
->http_auth_handler_factory
= context
->http_auth_handler_factory();
101 params
->network_delegate
= context
->network_delegate();
102 params
->http_server_properties
= context
->http_server_properties();
103 params
->net_log
= context
->net_log();
104 // TODO(sgurun) remove once crbug.com/329681 is fixed.
105 params
->next_protos
= net::NextProtosSpdy31();
106 params
->use_alternative_services
= true;
108 ApplyCmdlineOverridesToNetworkSessionParams(params
);
111 scoped_ptr
<net::URLRequestJobFactory
> CreateJobFactory(
112 content::ProtocolHandlerMap
* protocol_handlers
,
113 content::URLRequestInterceptorScopedVector request_interceptors
) {
114 scoped_ptr
<AwURLRequestJobFactory
> aw_job_factory(new AwURLRequestJobFactory
);
115 // Note that the registered schemes must also be specified in
116 // AwContentBrowserClient::IsHandledURL.
117 bool set_protocol
= aw_job_factory
->SetProtocolHandler(
119 make_scoped_ptr(new net::FileProtocolHandler(
120 content::BrowserThread::GetBlockingPool()
121 ->GetTaskRunnerWithShutdownBehavior(
122 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
))));
123 DCHECK(set_protocol
);
124 set_protocol
= aw_job_factory
->SetProtocolHandler(
125 url::kDataScheme
, make_scoped_ptr(new net::DataProtocolHandler()));
126 DCHECK(set_protocol
);
127 set_protocol
= aw_job_factory
->SetProtocolHandler(
129 make_scoped_ptr((*protocol_handlers
)[url::kBlobScheme
].release()));
130 DCHECK(set_protocol
);
131 set_protocol
= aw_job_factory
->SetProtocolHandler(
132 url::kFileSystemScheme
,
133 make_scoped_ptr((*protocol_handlers
)[url::kFileSystemScheme
].release()));
134 DCHECK(set_protocol
);
135 set_protocol
= aw_job_factory
->SetProtocolHandler(
136 content::kChromeUIScheme
,
138 (*protocol_handlers
)[content::kChromeUIScheme
].release()));
139 DCHECK(set_protocol
);
140 set_protocol
= aw_job_factory
->SetProtocolHandler(
141 content::kChromeDevToolsScheme
,
143 (*protocol_handlers
)[content::kChromeDevToolsScheme
].release()));
144 DCHECK(set_protocol
);
145 protocol_handlers
->clear();
147 // Note that even though the content:// scheme handler is created here,
148 // it cannot be used by child processes until access to it is granted via
149 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
150 // AwContentBrowserClient.
151 request_interceptors
.push_back(
152 CreateAndroidContentRequestInterceptor().release());
153 request_interceptors
.push_back(
154 CreateAndroidAssetFileRequestInterceptor().release());
155 // The AwRequestInterceptor must come after the content and asset file job
156 // factories. This for WebViewClassic compatibility where it was not
157 // possible to intercept resource loads to resolvable content:// and
159 // This logical dependency is also the reason why the Content
160 // URLRequestInterceptor has to be added as an interceptor rather than as a
162 request_interceptors
.push_back(new AwRequestInterceptor());
164 // The chain of responsibility will execute the handlers in reverse to the
165 // order in which the elements of the chain are created.
166 scoped_ptr
<net::URLRequestJobFactory
> job_factory(aw_job_factory
.Pass());
167 for (content::URLRequestInterceptorScopedVector::reverse_iterator i
=
168 request_interceptors
.rbegin();
169 i
!= request_interceptors
.rend();
171 job_factory
.reset(new net::URLRequestInterceptingJobFactory(
172 job_factory
.Pass(), make_scoped_ptr(*i
)));
174 request_interceptors
.weak_clear();
176 return job_factory
.Pass();
181 AwURLRequestContextGetter::AwURLRequestContextGetter(
182 const base::FilePath
& cache_path
, net::CookieStore
* cookie_store
,
183 scoped_ptr
<net::ProxyConfigService
> config_service
)
184 : cache_path_(cache_path
),
185 cookie_store_(cookie_store
),
186 net_log_(new net::NetLog()) {
187 proxy_config_service_
= config_service
.Pass();
188 http_user_agent_settings_
.reset(
189 new AwHttpUserAgentSettings());
190 // CreateSystemProxyConfigService for Android must be called on main thread.
191 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
194 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
197 void AwURLRequestContextGetter::InitializeURLRequestContext() {
198 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
199 DCHECK(!url_request_context_
);
201 net::URLRequestContextBuilder builder
;
202 scoped_ptr
<AwNetworkDelegate
> aw_network_delegate(new AwNetworkDelegate());
204 AwBrowserContext
* browser_context
= AwBrowserContext::GetDefault();
205 DCHECK(browser_context
);
207 builder
.set_network_delegate(
208 browser_context
->GetDataReductionProxyIOData()
209 ->CreateNetworkDelegate(
210 aw_network_delegate
.Pass(),
211 false /* No UMA is produced to track bypasses. */)
213 #if !defined(DISABLE_FTP_SUPPORT)
214 builder
.set_ftp_enabled(false); // Android WebView does not support ftp yet.
216 DCHECK(proxy_config_service_
.get());
217 // Android provides a local HTTP proxy that handles all the proxying.
218 // Create the proxy without a resolver since we rely on this local HTTP proxy.
219 // TODO(sgurun) is this behavior guaranteed through SDK?
220 builder
.set_proxy_service(net::ProxyService::CreateWithoutProxyResolver(
221 proxy_config_service_
.release(), net_log_
.get()));
222 builder
.set_net_log(net_log_
.get());
223 builder
.SetCookieAndChannelIdStores(cookie_store_
, NULL
);
224 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder
);
226 url_request_context_
= builder
.Build().Pass();
227 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
228 net::HttpNetworkSession::Params network_session_params
;
230 PopulateNetworkSessionParams(url_request_context_
.get(),
231 &network_session_params
);
233 net::HttpCache
* main_cache
= new net::HttpCache(
234 network_session_params
,
235 new net::HttpCache::DefaultBackend(
237 net::CACHE_BACKEND_SIMPLE
,
239 20 * 1024 * 1024, // 20M
240 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
)));
242 main_http_factory_
.reset(main_cache
);
243 url_request_context_
->set_http_transaction_factory(main_cache
);
245 job_factory_
= CreateJobFactory(&protocol_handlers_
,
246 request_interceptors_
.Pass());
248 job_factory_
.reset(new net::URLRequestInterceptingJobFactory(
250 browser_context
->GetDataReductionProxyIOData()->CreateInterceptor()));
251 url_request_context_
->set_job_factory(job_factory_
.get());
252 url_request_context_
->set_http_user_agent_settings(
253 http_user_agent_settings_
.get());
256 net::URLRequestContext
* AwURLRequestContextGetter::GetURLRequestContext() {
257 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
258 if (!url_request_context_
)
259 InitializeURLRequestContext();
261 return url_request_context_
.get();
264 scoped_refptr
<base::SingleThreadTaskRunner
>
265 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
266 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
);
269 void AwURLRequestContextGetter::SetHandlersAndInterceptors(
270 content::ProtocolHandlerMap
* protocol_handlers
,
271 content::URLRequestInterceptorScopedVector request_interceptors
) {
272 std::swap(protocol_handlers_
, *protocol_handlers
);
273 request_interceptors_
.swap(request_interceptors
);
276 net::NetLog
* AwURLRequestContextGetter::GetNetLog() {
277 return net_log_
.get();
280 void AwURLRequestContextGetter::SetKeyOnIO(const std::string
& key
) {
281 DCHECK(AwBrowserContext::GetDefault()->GetDataReductionProxyIOData());
282 AwBrowserContext::GetDefault()->GetDataReductionProxyIOData()->
283 request_options()->SetKeyOnIO(key
);
286 } // namespace android_webview