Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / android_webview / browser / net / aw_url_request_context_getter.cc
blobbdfb0145f66ab2076563fd11d48a1b5f87088716
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"
7 #include <vector>
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 {
52 namespace {
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.release());
71 void ApplyCmdlineOverridesToNetworkSessionParams(
72 net::HttpNetworkSession::Params* params) {
73 int value;
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(
118 url::kFileScheme,
119 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, new net::DataProtocolHandler());
126 DCHECK(set_protocol);
127 set_protocol = aw_job_factory->SetProtocolHandler(
128 url::kBlobScheme,
129 (*protocol_handlers)[url::kBlobScheme].release());
130 DCHECK(set_protocol);
131 set_protocol = aw_job_factory->SetProtocolHandler(
132 url::kFileSystemScheme,
133 (*protocol_handlers)[url::kFileSystemScheme].release());
134 DCHECK(set_protocol);
135 set_protocol = aw_job_factory->SetProtocolHandler(
136 content::kChromeUIScheme,
137 (*protocol_handlers)[content::kChromeUIScheme].release());
138 DCHECK(set_protocol);
139 set_protocol = aw_job_factory->SetProtocolHandler(
140 content::kChromeDevToolsScheme,
141 (*protocol_handlers)[content::kChromeDevToolsScheme].release());
142 DCHECK(set_protocol);
143 protocol_handlers->clear();
145 // Note that even though the content:// scheme handler is created here,
146 // it cannot be used by child processes until access to it is granted via
147 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
148 // AwContentBrowserClient.
149 request_interceptors.push_back(
150 CreateAndroidContentRequestInterceptor().release());
151 request_interceptors.push_back(
152 CreateAndroidAssetFileRequestInterceptor().release());
153 // The AwRequestInterceptor must come after the content and asset file job
154 // factories. This for WebViewClassic compatibility where it was not
155 // possible to intercept resource loads to resolvable content:// and
156 // file:// URIs.
157 // This logical dependency is also the reason why the Content
158 // URLRequestInterceptor has to be added as an interceptor rather than as a
159 // ProtocolHandler.
160 request_interceptors.push_back(new AwRequestInterceptor());
162 // The chain of responsibility will execute the handlers in reverse to the
163 // order in which the elements of the chain are created.
164 scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
165 for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
166 request_interceptors.rbegin();
167 i != request_interceptors.rend();
168 ++i) {
169 job_factory.reset(new net::URLRequestInterceptingJobFactory(
170 job_factory.Pass(), make_scoped_ptr(*i)));
172 request_interceptors.weak_clear();
174 return job_factory.Pass();
177 } // namespace
179 AwURLRequestContextGetter::AwURLRequestContextGetter(
180 const base::FilePath& cache_path, net::CookieStore* cookie_store,
181 scoped_ptr<net::ProxyConfigService> config_service)
182 : cache_path_(cache_path),
183 cookie_store_(cookie_store),
184 net_log_(new net::NetLog()) {
185 proxy_config_service_ = config_service.Pass();
186 http_user_agent_settings_.reset(
187 new AwHttpUserAgentSettings());
188 // CreateSystemProxyConfigService for Android must be called on main thread.
189 DCHECK_CURRENTLY_ON(BrowserThread::UI);
192 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
195 void AwURLRequestContextGetter::InitializeURLRequestContext() {
196 DCHECK_CURRENTLY_ON(BrowserThread::IO);
197 DCHECK(!url_request_context_);
199 net::URLRequestContextBuilder builder;
200 scoped_ptr<AwNetworkDelegate> aw_network_delegate(new AwNetworkDelegate());
202 AwBrowserContext* browser_context = AwBrowserContext::GetDefault();
203 DCHECK(browser_context);
205 builder.set_network_delegate(
206 browser_context->GetDataReductionProxyIOData()->CreateNetworkDelegate(
207 aw_network_delegate.Pass(),
208 false /* No UMA is produced to track bypasses. */ ).release());
209 #if !defined(DISABLE_FTP_SUPPORT)
210 builder.set_ftp_enabled(false); // Android WebView does not support ftp yet.
211 #endif
212 DCHECK(proxy_config_service_.get());
213 // Android provides a local HTTP proxy that handles all the proxying.
214 // Create the proxy without a resolver since we rely on this local HTTP proxy.
215 // TODO(sgurun) is this behavior guaranteed through SDK?
216 builder.set_proxy_service(
217 net::ProxyService::CreateWithoutProxyResolver(
218 proxy_config_service_.release(),
219 net_log_.get()));
220 builder.set_net_log(net_log_.get());
221 builder.SetCookieAndChannelIdStores(cookie_store_, NULL);
222 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
224 url_request_context_.reset(builder.Build());
225 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
226 net::HttpNetworkSession::Params network_session_params;
228 PopulateNetworkSessionParams(url_request_context_.get(),
229 &network_session_params);
231 net::HttpCache* main_cache = new net::HttpCache(
232 network_session_params,
233 new net::HttpCache::DefaultBackend(
234 net::DISK_CACHE,
235 net::CACHE_BACKEND_SIMPLE,
236 cache_path_,
237 20 * 1024 * 1024, // 20M
238 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
240 main_http_factory_.reset(main_cache);
241 url_request_context_->set_http_transaction_factory(main_cache);
243 job_factory_ = CreateJobFactory(&protocol_handlers_,
244 request_interceptors_.Pass());
246 job_factory_.reset(new net::URLRequestInterceptingJobFactory(
247 job_factory_.Pass(),
248 browser_context->GetDataReductionProxyIOData()->CreateInterceptor()));
249 url_request_context_->set_job_factory(job_factory_.get());
250 url_request_context_->set_http_user_agent_settings(
251 http_user_agent_settings_.get());
254 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
255 DCHECK_CURRENTLY_ON(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 net::NetLog* AwURLRequestContextGetter::GetNetLog() {
275 return net_log_.get();
278 void AwURLRequestContextGetter::SetKeyOnIO(const std::string& key) {
279 DCHECK(AwBrowserContext::GetDefault()->GetDataReductionProxyIOData());
280 AwBrowserContext::GetDefault()->GetDataReductionProxyIOData()->
281 request_options()->SetKeyOnIO(key);
284 } // namespace android_webview