suppress unaddr error at WebResourceService::StartFetch
[chromium-blink-merge.git] / android_webview / browser / net / aw_url_request_context_getter.cc
blobceb361f88fac82a71e3d954d58f29fe7cf41210f
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 "components/data_reduction_proxy/browser/data_reduction_proxy_config_service.h"
20 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
21 #include "components/data_reduction_proxy/browser/http_auth_handler_data_reduction_proxy.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/content_browser_client.h"
24 #include "content/public/browser/cookie_store_factory.h"
25 #include "content/public/common/content_client.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/common/url_constants.h"
28 #include "net/base/cache_type.h"
29 #include "net/cookies/cookie_store.h"
30 #include "net/dns/mapped_host_resolver.h"
31 #include "net/http/http_cache.h"
32 #include "net/http/http_stream_factory.h"
33 #include "net/proxy/proxy_service.h"
34 #include "net/socket/next_proto.h"
35 #include "net/url_request/data_protocol_handler.h"
36 #include "net/url_request/file_protocol_handler.h"
37 #include "net/url_request/url_request_context_builder.h"
38 #include "net/url_request/url_request_context.h"
39 #include "net/url_request/url_request_intercepting_job_factory.h"
40 #include "net/url_request/url_request_interceptor.h"
42 using content::BrowserThread;
43 using data_reduction_proxy::DataReductionProxySettings;
45 namespace android_webview {
48 namespace {
50 void ApplyCmdlineOverridesToURLRequestContextBuilder(
51 net::URLRequestContextBuilder* builder) {
52 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
53 if (command_line.HasSwitch(switches::kHostResolverRules)) {
54 // If hostname remappings were specified on the command-line, layer these
55 // rules on top of the real host resolver. This allows forwarding all
56 // requests through a designated test server.
57 scoped_ptr<net::MappedHostResolver> host_resolver(
58 new net::MappedHostResolver(
59 net::HostResolver::CreateDefaultResolver(NULL)));
60 host_resolver->SetRulesFromString(
61 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
62 builder->set_host_resolver(host_resolver.release());
66 void ApplyCmdlineOverridesToNetworkSessionParams(
67 net::HttpNetworkSession::Params* params) {
68 int value;
69 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
70 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
71 base::StringToInt(command_line.GetSwitchValueASCII(
72 switches::kTestingFixedHttpPort), &value);
73 params->testing_fixed_http_port = value;
75 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
76 base::StringToInt(command_line.GetSwitchValueASCII(
77 switches::kTestingFixedHttpsPort), &value);
78 params->testing_fixed_https_port = value;
82 void PopulateNetworkSessionParams(
83 net::URLRequestContext* context,
84 net::HttpNetworkSession::Params* params) {
85 params->host_resolver = context->host_resolver();
86 params->cert_verifier = context->cert_verifier();
87 params->server_bound_cert_service = context->server_bound_cert_service();
88 params->transport_security_state = context->transport_security_state();
89 params->proxy_service = context->proxy_service();
90 params->ssl_config_service = context->ssl_config_service();
91 params->http_auth_handler_factory = context->http_auth_handler_factory();
92 params->network_delegate = context->network_delegate();
93 params->http_server_properties = context->http_server_properties();
94 params->net_log = context->net_log();
96 // TODO(sgurun) remove once crbug.com/329681 is fixed.
97 params->next_protos = net::NextProtosSpdy31();
98 params->use_alternate_protocols = true;
100 ApplyCmdlineOverridesToNetworkSessionParams(params);
103 scoped_ptr<net::URLRequestJobFactory> CreateJobFactory(
104 content::ProtocolHandlerMap* protocol_handlers,
105 content::URLRequestInterceptorScopedVector request_interceptors) {
106 scoped_ptr<AwURLRequestJobFactory> aw_job_factory(new AwURLRequestJobFactory);
107 bool set_protocol = aw_job_factory->SetProtocolHandler(
108 url::kFileScheme,
109 new net::FileProtocolHandler(
110 content::BrowserThread::GetBlockingPool()->
111 GetTaskRunnerWithShutdownBehavior(
112 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
113 DCHECK(set_protocol);
114 set_protocol = aw_job_factory->SetProtocolHandler(
115 url::kDataScheme, new net::DataProtocolHandler());
116 DCHECK(set_protocol);
117 set_protocol = aw_job_factory->SetProtocolHandler(
118 url::kBlobScheme,
119 (*protocol_handlers)[url::kBlobScheme].release());
120 DCHECK(set_protocol);
121 set_protocol = aw_job_factory->SetProtocolHandler(
122 url::kFileSystemScheme,
123 (*protocol_handlers)[url::kFileSystemScheme].release());
124 DCHECK(set_protocol);
125 set_protocol = aw_job_factory->SetProtocolHandler(
126 content::kChromeUIScheme,
127 (*protocol_handlers)[content::kChromeUIScheme].release());
128 DCHECK(set_protocol);
129 set_protocol = aw_job_factory->SetProtocolHandler(
130 content::kChromeDevToolsScheme,
131 (*protocol_handlers)[content::kChromeDevToolsScheme].release());
132 DCHECK(set_protocol);
133 protocol_handlers->clear();
135 // Note that even though the content:// scheme handler is created here,
136 // it cannot be used by child processes until access to it is granted via
137 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
138 // AwContentBrowserClient.
139 request_interceptors.push_back(
140 CreateAndroidContentRequestInterceptor().release());
141 request_interceptors.push_back(
142 CreateAndroidAssetFileRequestInterceptor().release());
143 // The AwRequestInterceptor must come after the content and asset file job
144 // factories. This for WebViewClassic compatibility where it was not
145 // possible to intercept resource loads to resolvable content:// and
146 // file:// URIs.
147 // This logical dependency is also the reason why the Content
148 // URLRequestInterceptor has to be added as an interceptor rather than as a
149 // ProtocolHandler.
150 request_interceptors.push_back(new AwRequestInterceptor());
152 // The chain of responsibility will execute the handlers in reverse to the
153 // order in which the elements of the chain are created.
154 scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
155 for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
156 request_interceptors.rbegin();
157 i != request_interceptors.rend();
158 ++i) {
159 job_factory.reset(new net::URLRequestInterceptingJobFactory(
160 job_factory.Pass(), make_scoped_ptr(*i)));
162 request_interceptors.weak_clear();
164 return job_factory.Pass();
167 } // namespace
169 AwURLRequestContextGetter::AwURLRequestContextGetter(
170 const base::FilePath& partition_path, net::CookieStore* cookie_store)
171 : partition_path_(partition_path),
172 cookie_store_(cookie_store),
173 proxy_config_service_(new DataReductionProxyConfigService(
174 scoped_ptr<net::ProxyConfigService>(
175 net::ProxyService::CreateSystemProxyConfigService(
176 GetNetworkTaskRunner(),
177 NULL /* Ignored on Android */)).Pass())) {
178 // CreateSystemProxyConfigService for Android must be called on main thread.
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
182 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
185 void AwURLRequestContextGetter::InitializeURLRequestContext() {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
187 DCHECK(!url_request_context_);
189 net::URLRequestContextBuilder builder;
190 builder.set_user_agent(GetUserAgent());
191 AwNetworkDelegate* aw_network_delegate = new AwNetworkDelegate();
192 builder.set_network_delegate(aw_network_delegate);
193 #if !defined(DISABLE_FTP_SUPPORT)
194 builder.set_ftp_enabled(false); // Android WebView does not support ftp yet.
195 #endif
196 builder.set_proxy_config_service(proxy_config_service_.release());
197 builder.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
198 AwContentBrowserClient::GetAcceptLangsImpl()));
199 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
201 #if defined(SPDY_PROXY_AUTH_ORIGIN)
202 data_reduction_proxy::DataReductionProxyParams drp_params(
203 data_reduction_proxy::DataReductionProxyParams::kAllowed);
204 builder.add_http_auth_handler_factory(
205 data_reduction_proxy::HttpAuthHandlerDataReductionProxy::Scheme(),
206 new data_reduction_proxy::HttpAuthHandlerDataReductionProxy::Factory(
207 drp_params.GetAllowedProxies()));
208 #endif
210 url_request_context_.reset(builder.Build());
211 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
212 net::HttpNetworkSession::Params network_session_params;
214 PopulateNetworkSessionParams(url_request_context_.get(),
215 &network_session_params);
217 net::HttpCache* main_cache = new net::HttpCache(
218 network_session_params,
219 new net::HttpCache::DefaultBackend(
220 net::DISK_CACHE,
221 net::CACHE_BACKEND_SIMPLE,
222 partition_path_.Append(FILE_PATH_LITERAL("Cache")),
223 20 * 1024 * 1024, // 20M
224 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
226 #if defined(SPDY_PROXY_AUTH_ORIGIN)
227 AwBrowserContext* browser_context = AwBrowserContext::GetDefault();
228 DCHECK(browser_context);
229 DataReductionProxySettings* drp_settings =
230 browser_context->GetDataReductionProxySettings();
231 if (drp_settings) {
232 aw_network_delegate->set_data_reduction_proxy_params(
233 drp_settings->params());
234 std::string drp_key = drp_settings->params()->key();
235 // Only precache credentials if a key is available at URLRequestContext
236 // initialization.
237 if (!drp_key.empty()) {
238 DataReductionProxySettings::InitDataReductionProxySession(
239 main_cache->GetSession(), &drp_params);
242 #endif
244 main_http_factory_.reset(main_cache);
245 url_request_context_->set_http_transaction_factory(main_cache);
246 url_request_context_->set_cookie_store(cookie_store_);
248 job_factory_ = CreateJobFactory(&protocol_handlers_,
249 request_interceptors_.Pass());
250 url_request_context_->set_job_factory(job_factory_.get());
253 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
255 if (!url_request_context_)
256 InitializeURLRequestContext();
258 return url_request_context_.get();
261 scoped_refptr<base::SingleThreadTaskRunner>
262 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
263 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
266 void AwURLRequestContextGetter::SetHandlersAndInterceptors(
267 content::ProtocolHandlerMap* protocol_handlers,
268 content::URLRequestInterceptorScopedVector request_interceptors) {
269 std::swap(protocol_handlers_, *protocol_handlers);
270 request_interceptors_.swap(request_interceptors);
273 DataReductionProxyConfigService*
274 AwURLRequestContextGetter::proxy_config_service() {
275 // TODO(bengr): return system config if data reduction proxy is disabled.
276 return proxy_config_service_.get();
279 } // namespace android_webview