ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / android_webview / browser / net / aw_url_request_context_getter.cc
blobca1c5617550be34d808d025fcbb0a9d38a6b7d3e
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/bind.h"
17 #include "base/command_line.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/threading/worker_pool.h"
21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"
22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"
23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.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/channel_id_service.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;
48 namespace android_webview {
51 namespace {
53 void ApplyCmdlineOverridesToURLRequestContextBuilder(
54 net::URLRequestContextBuilder* builder) {
55 const base::CommandLine& command_line =
56 *base::CommandLine::ForCurrentProcess();
57 if (command_line.HasSwitch(switches::kHostResolverRules)) {
58 // If hostname remappings were specified on the command-line, layer these
59 // rules on top of the real host resolver. This allows forwarding all
60 // requests through a designated test server.
61 scoped_ptr<net::MappedHostResolver> host_resolver(
62 new net::MappedHostResolver(
63 net::HostResolver::CreateDefaultResolver(NULL)));
64 host_resolver->SetRulesFromString(
65 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
66 builder->set_host_resolver(host_resolver.release());
70 void ApplyCmdlineOverridesToNetworkSessionParams(
71 net::HttpNetworkSession::Params* params) {
72 int value;
73 const base::CommandLine& command_line =
74 *base::CommandLine::ForCurrentProcess();
75 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
76 base::StringToInt(command_line.GetSwitchValueASCII(
77 switches::kTestingFixedHttpPort), &value);
78 params->testing_fixed_http_port = value;
80 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
81 base::StringToInt(command_line.GetSwitchValueASCII(
82 switches::kTestingFixedHttpsPort), &value);
83 params->testing_fixed_https_port = value;
85 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors)) {
86 params->ignore_certificate_errors = true;
90 void PopulateNetworkSessionParams(
91 net::URLRequestContext* context,
92 net::HttpNetworkSession::Params* params) {
93 params->host_resolver = context->host_resolver();
94 params->cert_verifier = context->cert_verifier();
95 params->channel_id_service = context->channel_id_service();
96 params->transport_security_state = context->transport_security_state();
97 params->proxy_service = context->proxy_service();
98 params->ssl_config_service = context->ssl_config_service();
99 params->http_auth_handler_factory = context->http_auth_handler_factory();
100 params->network_delegate = context->network_delegate();
101 params->http_server_properties = context->http_server_properties();
102 params->net_log = context->net_log();
103 // TODO(sgurun) remove once crbug.com/329681 is fixed.
104 params->next_protos = net::NextProtosSpdy31();
105 params->use_alternate_protocols = true;
107 ApplyCmdlineOverridesToNetworkSessionParams(params);
110 scoped_ptr<net::URLRequestJobFactory> CreateJobFactory(
111 content::ProtocolHandlerMap* protocol_handlers,
112 content::URLRequestInterceptorScopedVector request_interceptors) {
113 scoped_ptr<AwURLRequestJobFactory> aw_job_factory(new AwURLRequestJobFactory);
114 bool set_protocol = aw_job_factory->SetProtocolHandler(
115 url::kFileScheme,
116 new net::FileProtocolHandler(
117 content::BrowserThread::GetBlockingPool()->
118 GetTaskRunnerWithShutdownBehavior(
119 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
120 DCHECK(set_protocol);
121 set_protocol = aw_job_factory->SetProtocolHandler(
122 url::kDataScheme, new net::DataProtocolHandler());
123 DCHECK(set_protocol);
124 set_protocol = aw_job_factory->SetProtocolHandler(
125 url::kBlobScheme,
126 (*protocol_handlers)[url::kBlobScheme].release());
127 DCHECK(set_protocol);
128 set_protocol = aw_job_factory->SetProtocolHandler(
129 url::kFileSystemScheme,
130 (*protocol_handlers)[url::kFileSystemScheme].release());
131 DCHECK(set_protocol);
132 set_protocol = aw_job_factory->SetProtocolHandler(
133 content::kChromeUIScheme,
134 (*protocol_handlers)[content::kChromeUIScheme].release());
135 DCHECK(set_protocol);
136 set_protocol = aw_job_factory->SetProtocolHandler(
137 content::kChromeDevToolsScheme,
138 (*protocol_handlers)[content::kChromeDevToolsScheme].release());
139 DCHECK(set_protocol);
140 protocol_handlers->clear();
142 // Note that even though the content:// scheme handler is created here,
143 // it cannot be used by child processes until access to it is granted via
144 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
145 // AwContentBrowserClient.
146 request_interceptors.push_back(
147 CreateAndroidContentRequestInterceptor().release());
148 request_interceptors.push_back(
149 CreateAndroidAssetFileRequestInterceptor().release());
150 // The AwRequestInterceptor must come after the content and asset file job
151 // factories. This for WebViewClassic compatibility where it was not
152 // possible to intercept resource loads to resolvable content:// and
153 // file:// URIs.
154 // This logical dependency is also the reason why the Content
155 // URLRequestInterceptor has to be added as an interceptor rather than as a
156 // ProtocolHandler.
157 request_interceptors.push_back(new AwRequestInterceptor());
159 // The chain of responsibility will execute the handlers in reverse to the
160 // order in which the elements of the chain are created.
161 scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
162 for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
163 request_interceptors.rbegin();
164 i != request_interceptors.rend();
165 ++i) {
166 job_factory.reset(new net::URLRequestInterceptingJobFactory(
167 job_factory.Pass(), make_scoped_ptr(*i)));
169 request_interceptors.weak_clear();
171 return job_factory.Pass();
174 } // namespace
176 AwURLRequestContextGetter::AwURLRequestContextGetter(
177 const base::FilePath& cache_path, net::CookieStore* cookie_store,
178 scoped_ptr<net::ProxyConfigService> config_service)
179 : cache_path_(cache_path),
180 cookie_store_(cookie_store),
181 net_log_(new net::NetLog()) {
182 proxy_config_service_ = config_service.Pass();
183 // CreateSystemProxyConfigService for Android must be called on main thread.
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
187 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
190 void AwURLRequestContextGetter::InitializeURLRequestContext() {
191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
192 DCHECK(!url_request_context_);
194 net::URLRequestContextBuilder builder;
195 builder.set_user_agent(GetUserAgent());
196 scoped_ptr<AwNetworkDelegate> aw_network_delegate(new AwNetworkDelegate());
198 AwBrowserContext* browser_context = AwBrowserContext::GetDefault();
199 DCHECK(browser_context);
201 builder.set_network_delegate(
202 browser_context->GetDataReductionProxyIOData()->CreateNetworkDelegate(
203 aw_network_delegate.Pass(),
204 false /* No UMA is produced to track bypasses. */ ).release());
205 #if !defined(DISABLE_FTP_SUPPORT)
206 builder.set_ftp_enabled(false); // Android WebView does not support ftp yet.
207 #endif
208 DCHECK(proxy_config_service_.get());
209 // Android provides a local HTTP proxy that handles all the proxying.
210 // Create the proxy without a resolver since we rely on this local HTTP proxy.
211 // TODO(sgurun) is this behavior guaranteed through SDK?
212 builder.set_proxy_service(
213 net::ProxyService::CreateWithoutProxyResolver(
214 proxy_config_service_.release(),
215 net_log_.get()));
216 builder.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
217 AwContentBrowserClient::GetAcceptLangsImpl()));
218 builder.set_net_log(net_log_.get());
219 builder.SetCookieAndChannelIdStores(cookie_store_, NULL);
220 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
222 url_request_context_.reset(builder.Build());
223 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
224 net::HttpNetworkSession::Params network_session_params;
226 PopulateNetworkSessionParams(url_request_context_.get(),
227 &network_session_params);
229 net::HttpCache* main_cache = new net::HttpCache(
230 network_session_params,
231 new net::HttpCache::DefaultBackend(
232 net::DISK_CACHE,
233 net::CACHE_BACKEND_SIMPLE,
234 cache_path_,
235 20 * 1024 * 1024, // 20M
236 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
238 main_http_factory_.reset(main_cache);
239 url_request_context_->set_http_transaction_factory(main_cache);
241 job_factory_ = CreateJobFactory(&protocol_handlers_,
242 request_interceptors_.Pass());
244 job_factory_.reset(new net::URLRequestInterceptingJobFactory(
245 job_factory_.Pass(),
246 browser_context->GetDataReductionProxyIOData()->CreateInterceptor()));
247 url_request_context_->set_job_factory(job_factory_.get());
250 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
252 if (!url_request_context_)
253 InitializeURLRequestContext();
255 return url_request_context_.get();
258 scoped_refptr<base::SingleThreadTaskRunner>
259 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
260 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
263 void AwURLRequestContextGetter::SetHandlersAndInterceptors(
264 content::ProtocolHandlerMap* protocol_handlers,
265 content::URLRequestInterceptorScopedVector request_interceptors) {
266 std::swap(protocol_handlers_, *protocol_handlers);
267 request_interceptors_.swap(request_interceptors);
270 net::NetLog* AwURLRequestContextGetter::GetNetLog() {
271 return net_log_.get();
274 void AwURLRequestContextGetter::SetKeyOnIO(const std::string& key) {
275 DCHECK(AwBrowserContext::GetDefault()->GetDataReductionProxyIOData());
276 AwBrowserContext::GetDefault()->GetDataReductionProxyIOData()->
277 request_options()->SetKeyOnIO(key);
280 } // namespace android_webview