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_content_browser_client.h"
10 #include "android_webview/browser/aw_request_interceptor.h"
11 #include "android_webview/browser/net/aw_network_delegate.h"
12 #include "android_webview/browser/net/aw_url_request_job_factory.h"
13 #include "android_webview/browser/net/init_native_callback.h"
14 #include "android_webview/common/aw_content_client.h"
15 #include "base/command_line.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/threading/sequenced_worker_pool.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/content_browser_client.h"
20 #include "content/public/browser/cookie_store_factory.h"
21 #include "content/public/common/content_client.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/url_constants.h"
24 #include "net/base/cache_type.h"
25 #include "net/cookies/cookie_store.h"
26 #include "net/dns/mapped_host_resolver.h"
27 #include "net/http/http_cache.h"
28 #include "net/http/http_stream_factory.h"
29 #include "net/proxy/proxy_service.h"
30 #include "net/url_request/data_protocol_handler.h"
31 #include "net/url_request/file_protocol_handler.h"
32 #include "net/url_request/protocol_intercept_job_factory.h"
33 #include "net/url_request/url_request_context_builder.h"
34 #include "net/url_request/url_request_context.h"
36 using content::BrowserThread;
38 namespace android_webview {
43 void ApplyCmdlineOverridesToURLRequestContextBuilder(
44 net::URLRequestContextBuilder* builder) {
45 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
46 if (command_line.HasSwitch(switches::kHostResolverRules)) {
47 // If hostname remappings were specified on the command-line, layer these
48 // rules on top of the real host resolver. This allows forwarding all
49 // requests through a designated test server.
50 scoped_ptr<net::MappedHostResolver> host_resolver(
51 new net::MappedHostResolver(
52 net::HostResolver::CreateDefaultResolver(NULL)));
53 host_resolver->SetRulesFromString(
54 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
55 builder->set_host_resolver(host_resolver.release());
59 void ApplyCmdlineOverridesToNetworkSessionParams(
60 net::HttpNetworkSession::Params* params) {
62 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
63 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
64 base::StringToInt(command_line.GetSwitchValueASCII(
65 switches::kTestingFixedHttpPort), &value);
66 params->testing_fixed_http_port = value;
68 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
69 base::StringToInt(command_line.GetSwitchValueASCII(
70 switches::kTestingFixedHttpsPort), &value);
71 params->testing_fixed_https_port = value;
75 void PopulateNetworkSessionParams(
76 net::URLRequestContext* context,
77 net::HttpNetworkSession::Params* params) {
78 params->host_resolver = context->host_resolver();
79 params->cert_verifier = context->cert_verifier();
80 params->server_bound_cert_service = context->server_bound_cert_service();
81 params->transport_security_state = context->transport_security_state();
82 params->proxy_service = context->proxy_service();
83 params->ssl_config_service = context->ssl_config_service();
84 params->http_auth_handler_factory = context->http_auth_handler_factory();
85 params->network_delegate = context->network_delegate();
86 params->http_server_properties = context->http_server_properties();
87 params->net_log = context->net_log();
88 ApplyCmdlineOverridesToNetworkSessionParams(params);
91 scoped_ptr<net::URLRequestJobFactory> CreateJobFactory(
92 content::ProtocolHandlerMap* protocol_handlers) {
93 scoped_ptr<AwURLRequestJobFactory> aw_job_factory(new AwURLRequestJobFactory);
94 bool set_protocol = aw_job_factory->SetProtocolHandler(
96 new net::FileProtocolHandler(
97 content::BrowserThread::GetBlockingPool()->
98 GetTaskRunnerWithShutdownBehavior(
99 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
100 DCHECK(set_protocol);
101 set_protocol = aw_job_factory->SetProtocolHandler(
102 content::kDataScheme, new net::DataProtocolHandler());
103 DCHECK(set_protocol);
104 set_protocol = aw_job_factory->SetProtocolHandler(
105 content::kBlobScheme,
106 (*protocol_handlers)[content::kBlobScheme].release());
107 DCHECK(set_protocol);
108 set_protocol = aw_job_factory->SetProtocolHandler(
109 content::kFileSystemScheme,
110 (*protocol_handlers)[content::kFileSystemScheme].release());
111 DCHECK(set_protocol);
112 set_protocol = aw_job_factory->SetProtocolHandler(
113 content::kChromeUIScheme,
114 (*protocol_handlers)[content::kChromeUIScheme].release());
115 DCHECK(set_protocol);
116 set_protocol = aw_job_factory->SetProtocolHandler(
117 content::kChromeDevToolsScheme,
118 (*protocol_handlers)[content::kChromeDevToolsScheme].release());
119 DCHECK(set_protocol);
120 protocol_handlers->clear();
122 // Create a chain of URLRequestJobFactories. The handlers will be invoked
123 // in the order in which they appear in the protocol_handlers vector.
124 typedef std::vector<net::URLRequestJobFactory::ProtocolHandler*>
125 ProtocolHandlerVector;
126 ProtocolHandlerVector protocol_interceptors;
128 // Note that even though the content:// scheme handler is created here,
129 // it cannot be used by child processes until access to it is granted via
130 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
131 // AwContentBrowserClient.
132 protocol_interceptors.push_back(
133 CreateAndroidContentProtocolHandler().release());
134 protocol_interceptors.push_back(
135 CreateAndroidAssetFileProtocolHandler().release());
136 // The AwRequestInterceptor must come after the content and asset file job
137 // factories. This for WebViewClassic compatibility where it was not
138 // possible to intercept resource loads to resolvable content:// and
140 // This logical dependency is also the reason why the Content
141 // ProtocolHandler has to be added as a ProtocolInterceptJobFactory rather
142 // than via SetProtocolHandler.
143 protocol_interceptors.push_back(new AwRequestInterceptor());
145 // The chain of responsibility will execute the handlers in reverse to the
146 // order in which the elements of the chain are created.
147 scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
148 for (ProtocolHandlerVector::reverse_iterator
149 i = protocol_interceptors.rbegin();
150 i != protocol_interceptors.rend();
152 job_factory.reset(new net::ProtocolInterceptJobFactory(
153 job_factory.Pass(), make_scoped_ptr(*i)));
156 return job_factory.Pass();
161 AwURLRequestContextGetter::AwURLRequestContextGetter(
162 const base::FilePath& partition_path, net::CookieStore* cookie_store)
163 : partition_path_(partition_path),
164 cookie_store_(cookie_store),
165 proxy_config_service_(net::ProxyService::CreateSystemProxyConfigService(
166 GetNetworkTaskRunner(),
167 NULL /* Ignored on Android */)) {
168 // CreateSystemProxyConfigService for Android must be called on main thread.
169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
172 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
175 void AwURLRequestContextGetter::InitializeURLRequestContext() {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
177 DCHECK(!url_request_context_);
179 net::URLRequestContextBuilder builder;
180 builder.set_user_agent(GetUserAgent());
181 builder.set_network_delegate(new AwNetworkDelegate());
182 #if !defined(DISABLE_FTP_SUPPORT)
183 builder.set_ftp_enabled(false); // Android WebView does not support ftp yet.
185 builder.set_proxy_config_service(proxy_config_service_.release());
186 builder.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
187 AwContentBrowserClient::GetAcceptLangsImpl()));
188 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
190 url_request_context_.reset(builder.Build());
191 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
192 net::HttpNetworkSession::Params network_session_params;
194 PopulateNetworkSessionParams(url_request_context_.get(),
195 &network_session_params);
197 net::HttpCache* main_cache = new net::HttpCache(
198 network_session_params,
199 new net::HttpCache::DefaultBackend(
201 net::CACHE_BACKEND_SIMPLE,
202 partition_path_.Append(FILE_PATH_LITERAL("Cache")),
203 20 * 1024 * 1024, // 20M
204 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
205 main_http_factory_.reset(main_cache);
206 url_request_context_->set_http_transaction_factory(main_cache);
207 url_request_context_->set_cookie_store(cookie_store_);
209 job_factory_ = CreateJobFactory(&protocol_handlers_);
210 url_request_context_->set_job_factory(job_factory_.get());
212 // TODO(sgurun) remove once crbug.com/329681 is fixed. Should be
214 net::HttpStreamFactory::EnableNpnSpdy31();
217 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
219 if (!url_request_context_)
220 InitializeURLRequestContext();
222 return url_request_context_.get();
225 scoped_refptr<base::SingleThreadTaskRunner>
226 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
227 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
230 void AwURLRequestContextGetter::SetProtocolHandlers(
231 content::ProtocolHandlerMap* protocol_handlers) {
232 std::swap(protocol_handlers_, *protocol_handlers);
235 } // namespace android_webview