[Android] Always move bookmarks at the end of child list
[chromium-blink-merge.git] / android_webview / browser / net / aw_url_request_context_getter.cc
blobb600a460bf291cff4d41ac2806f6e4d009789682
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_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/core/browser/data_reduction_proxy_auth_request_handler.h"
21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_service.h"
22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h"
23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/content_browser_client.h"
26 #include "content/public/browser/cookie_store_factory.h"
27 #include "content/public/common/content_client.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/common/url_constants.h"
30 #include "net/base/cache_type.h"
31 #include "net/base/net_log.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/proxy/proxy_service.h"
37 #include "net/socket/next_proto.h"
38 #include "net/ssl/default_channel_id_store.h"
39 #include "net/url_request/data_protocol_handler.h"
40 #include "net/url_request/file_protocol_handler.h"
41 #include "net/url_request/url_request_context_builder.h"
42 #include "net/url_request/url_request_context.h"
43 #include "net/url_request/url_request_intercepting_job_factory.h"
44 #include "net/url_request/url_request_interceptor.h"
46 using content::BrowserThread;
47 using data_reduction_proxy::DataReductionProxySettings;
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_alternate_protocols = 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 bool set_protocol = aw_job_factory->SetProtocolHandler(
116 url::kFileScheme,
117 new net::FileProtocolHandler(
118 content::BrowserThread::GetBlockingPool()->
119 GetTaskRunnerWithShutdownBehavior(
120 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
121 DCHECK(set_protocol);
122 set_protocol = aw_job_factory->SetProtocolHandler(
123 url::kDataScheme, new net::DataProtocolHandler());
124 DCHECK(set_protocol);
125 set_protocol = aw_job_factory->SetProtocolHandler(
126 url::kBlobScheme,
127 (*protocol_handlers)[url::kBlobScheme].release());
128 DCHECK(set_protocol);
129 set_protocol = aw_job_factory->SetProtocolHandler(
130 url::kFileSystemScheme,
131 (*protocol_handlers)[url::kFileSystemScheme].release());
132 DCHECK(set_protocol);
133 set_protocol = aw_job_factory->SetProtocolHandler(
134 content::kChromeUIScheme,
135 (*protocol_handlers)[content::kChromeUIScheme].release());
136 DCHECK(set_protocol);
137 set_protocol = aw_job_factory->SetProtocolHandler(
138 content::kChromeDevToolsScheme,
139 (*protocol_handlers)[content::kChromeDevToolsScheme].release());
140 DCHECK(set_protocol);
141 protocol_handlers->clear();
143 // Note that even though the content:// scheme handler is created here,
144 // it cannot be used by child processes until access to it is granted via
145 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
146 // AwContentBrowserClient.
147 request_interceptors.push_back(
148 CreateAndroidContentRequestInterceptor().release());
149 request_interceptors.push_back(
150 CreateAndroidAssetFileRequestInterceptor().release());
151 // The AwRequestInterceptor must come after the content and asset file job
152 // factories. This for WebViewClassic compatibility where it was not
153 // possible to intercept resource loads to resolvable content:// and
154 // file:// URIs.
155 // This logical dependency is also the reason why the Content
156 // URLRequestInterceptor has to be added as an interceptor rather than as a
157 // ProtocolHandler.
158 request_interceptors.push_back(new AwRequestInterceptor());
160 // The chain of responsibility will execute the handlers in reverse to the
161 // order in which the elements of the chain are created.
162 scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
163 for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
164 request_interceptors.rbegin();
165 i != request_interceptors.rend();
166 ++i) {
167 job_factory.reset(new net::URLRequestInterceptingJobFactory(
168 job_factory.Pass(), make_scoped_ptr(*i)));
170 request_interceptors.weak_clear();
172 return job_factory.Pass();
175 } // namespace
177 AwURLRequestContextGetter::AwURLRequestContextGetter(
178 const base::FilePath& cache_path, net::CookieStore* cookie_store,
179 scoped_ptr<data_reduction_proxy::DataReductionProxyConfigService>
180 config_service)
181 : cache_path_(cache_path),
182 cookie_store_(cookie_store),
183 net_log_(new net::NetLog()) {
184 data_reduction_proxy_config_service_ = config_service.Pass();
185 // CreateSystemProxyConfigService for Android must be called on main thread.
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
189 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
192 void AwURLRequestContextGetter::InitializeURLRequestContext() {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
194 DCHECK(!url_request_context_);
196 net::URLRequestContextBuilder builder;
197 builder.set_user_agent(GetUserAgent());
198 AwNetworkDelegate* aw_network_delegate = new AwNetworkDelegate();
199 builder.set_network_delegate(aw_network_delegate);
200 #if !defined(DISABLE_FTP_SUPPORT)
201 builder.set_ftp_enabled(false); // Android WebView does not support ftp yet.
202 #endif
203 DCHECK(data_reduction_proxy_config_service_.get());
204 // Android provides a local HTTP proxy that handles all the proxying.
205 // Create the proxy without a resolver since we rely on this local HTTP proxy.
206 // TODO(sgurun) is this behavior guaranteed through SDK?
207 builder.set_proxy_service(
208 net::ProxyService::CreateWithoutProxyResolver(
209 data_reduction_proxy_config_service_.release(),
210 net_log_.get()));
211 builder.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
212 AwContentBrowserClient::GetAcceptLangsImpl()));
213 builder.set_net_log(net_log_.get());
214 builder.set_channel_id_enabled(false);
215 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
217 url_request_context_.reset(builder.Build());
218 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
219 net::HttpNetworkSession::Params network_session_params;
221 PopulateNetworkSessionParams(url_request_context_.get(),
222 &network_session_params);
224 net::HttpCache* main_cache = new net::HttpCache(
225 network_session_params,
226 new net::HttpCache::DefaultBackend(
227 net::DISK_CACHE,
228 net::CACHE_BACKEND_SIMPLE,
229 cache_path_,
230 20 * 1024 * 1024, // 20M
231 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
233 AwBrowserContext* browser_context = AwBrowserContext::GetDefault();
234 DCHECK(browser_context);
235 DataReductionProxySettings* data_reduction_proxy_settings =
236 browser_context->GetDataReductionProxySettings();
237 DCHECK(data_reduction_proxy_settings);
238 data_reduction_proxy_auth_request_handler_.reset(
239 new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
240 data_reduction_proxy::Client::WEBVIEW_ANDROID,
241 data_reduction_proxy_settings->params(),
242 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
244 // Compression statistics are not gathered for WebView, so
245 // DataReductionProxyStatisticsPrefs is not instantiated and passed to the
246 // network delegate.
247 aw_network_delegate->set_data_reduction_proxy_params(
248 data_reduction_proxy_settings->params());
249 aw_network_delegate->set_data_reduction_proxy_auth_request_handler(
250 data_reduction_proxy_auth_request_handler_.get());
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_.get());
256 job_factory_ = CreateJobFactory(&protocol_handlers_,
257 request_interceptors_.Pass());
259 job_factory_.reset(new net::URLRequestInterceptingJobFactory(
260 job_factory_.Pass(), make_scoped_ptr(
261 new data_reduction_proxy::DataReductionProxyInterceptor(
262 data_reduction_proxy_settings->params(), NULL))));
263 url_request_context_->set_job_factory(job_factory_.get());
266 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
268 if (!url_request_context_)
269 InitializeURLRequestContext();
271 return url_request_context_.get();
274 scoped_refptr<base::SingleThreadTaskRunner>
275 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
276 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
279 void AwURLRequestContextGetter::SetHandlersAndInterceptors(
280 content::ProtocolHandlerMap* protocol_handlers,
281 content::URLRequestInterceptorScopedVector request_interceptors) {
282 std::swap(protocol_handlers_, *protocol_handlers);
283 request_interceptors_.swap(request_interceptors);
286 data_reduction_proxy::DataReductionProxyAuthRequestHandler*
287 AwURLRequestContextGetter::GetDataReductionProxyAuthRequestHandler() const {
288 return data_reduction_proxy_auth_request_handler_.get();
291 net::NetLog* AwURLRequestContextGetter::GetNetLog() {
292 return net_log_.get();
295 void AwURLRequestContextGetter::SetKeyOnIO(const std::string& key) {
296 DCHECK(data_reduction_proxy_auth_request_handler_);
297 data_reduction_proxy_auth_request_handler_->InitAuthentication(key);
300 } // namespace android_webview