[Chromoting] Break up JS event handler registration into related groups.
[chromium-blink-merge.git] / android_webview / browser / net / aw_url_request_context_getter.cc
blob31d0f3106968c75be6c24008e135de673309b808
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_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_switches.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 {
41 namespace {
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) {
61 int value;
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(
95 content::kFileScheme,
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 chrome::kBlobScheme, (*protocol_handlers)[chrome::kBlobScheme].release());
106 DCHECK(set_protocol);
107 set_protocol = aw_job_factory->SetProtocolHandler(
108 content::kFileSystemScheme,
109 (*protocol_handlers)[content::kFileSystemScheme].release());
110 DCHECK(set_protocol);
111 set_protocol = aw_job_factory->SetProtocolHandler(
112 content::kChromeUIScheme,
113 (*protocol_handlers)[content::kChromeUIScheme].release());
114 DCHECK(set_protocol);
115 set_protocol = aw_job_factory->SetProtocolHandler(
116 chrome::kChromeDevToolsScheme,
117 (*protocol_handlers)[chrome::kChromeDevToolsScheme].release());
118 DCHECK(set_protocol);
119 protocol_handlers->clear();
121 // Create a chain of URLRequestJobFactories. The handlers will be invoked
122 // in the order in which they appear in the protocol_handlers vector.
123 typedef std::vector<net::URLRequestJobFactory::ProtocolHandler*>
124 ProtocolHandlerVector;
125 ProtocolHandlerVector protocol_interceptors;
127 // Note that even though the content:// scheme handler is created here,
128 // it cannot be used by child processes until access to it is granted via
129 // ChildProcessSecurityPolicy::GrantScheme(). This is done in
130 // AwContentBrowserClient.
131 protocol_interceptors.push_back(
132 CreateAndroidContentProtocolHandler().release());
133 protocol_interceptors.push_back(
134 CreateAndroidAssetFileProtocolHandler().release());
135 // The AwRequestInterceptor must come after the content and asset file job
136 // factories. This for WebViewClassic compatibility where it was not
137 // possible to intercept resource loads to resolvable content:// and
138 // file:// URIs.
139 // This logical dependency is also the reason why the Content
140 // ProtocolHandler has to be added as a ProtocolInterceptJobFactory rather
141 // than via SetProtocolHandler.
142 protocol_interceptors.push_back(new AwRequestInterceptor());
144 // The chain of responsibility will execute the handlers in reverse to the
145 // order in which the elements of the chain are created.
146 scoped_ptr<net::URLRequestJobFactory> job_factory(aw_job_factory.Pass());
147 for (ProtocolHandlerVector::reverse_iterator
148 i = protocol_interceptors.rbegin();
149 i != protocol_interceptors.rend();
150 ++i) {
151 job_factory.reset(new net::ProtocolInterceptJobFactory(
152 job_factory.Pass(), make_scoped_ptr(*i)));
155 return job_factory.Pass();
158 } // namespace
160 AwURLRequestContextGetter::AwURLRequestContextGetter(
161 const base::FilePath& partition_path, net::CookieStore* cookie_store)
162 : partition_path_(partition_path),
163 cookie_store_(cookie_store),
164 proxy_config_service_(net::ProxyService::CreateSystemProxyConfigService(
165 GetNetworkTaskRunner(),
166 NULL /* Ignored on Android */)) {
167 // CreateSystemProxyConfigService for Android must be called on main thread.
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
171 AwURLRequestContextGetter::~AwURLRequestContextGetter() {
174 void AwURLRequestContextGetter::InitializeURLRequestContext() {
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
176 DCHECK(!url_request_context_);
178 net::URLRequestContextBuilder builder;
179 builder.set_user_agent(content::GetUserAgent(GURL()));
180 builder.set_network_delegate(new AwNetworkDelegate());
181 #if !defined(DISABLE_FTP_SUPPORT)
182 builder.set_ftp_enabled(false); // Android WebView does not support ftp yet.
183 #endif
184 builder.set_proxy_config_service(proxy_config_service_.release());
185 builder.set_accept_language(net::HttpUtil::GenerateAcceptLanguageHeader(
186 AwContentBrowserClient::GetAcceptLangsImpl()));
187 ApplyCmdlineOverridesToURLRequestContextBuilder(&builder);
189 url_request_context_.reset(builder.Build());
190 // TODO(mnaganov): Fix URLRequestContextBuilder to use proper threads.
191 net::HttpNetworkSession::Params network_session_params;
193 net::BackendType cache_type = net::CACHE_BACKEND_SIMPLE;
194 if (CommandLine::ForCurrentProcess()->HasSwitch(
195 switches::kDisableSimpleCache)) {
196 cache_type = net::CACHE_BACKEND_BLOCKFILE;
198 PopulateNetworkSessionParams(url_request_context_.get(),
199 &network_session_params);
201 net::HttpCache* main_cache = new net::HttpCache(
202 network_session_params,
203 new net::HttpCache::DefaultBackend(
204 net::DISK_CACHE,
205 cache_type,
206 partition_path_.Append(FILE_PATH_LITERAL("Cache")),
207 20 * 1024 * 1024, // 20M
208 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)));
209 main_http_factory_.reset(main_cache);
210 url_request_context_->set_http_transaction_factory(main_cache);
211 url_request_context_->set_cookie_store(cookie_store_);
213 job_factory_ = CreateJobFactory(&protocol_handlers_);
214 url_request_context_->set_job_factory(job_factory_.get());
216 // TODO(sgurun) remove once crbug.com/329681 is fixed. Should be
217 // called only once.
218 net::HttpStreamFactory::EnableNpnSpdy31();
221 net::URLRequestContext* AwURLRequestContextGetter::GetURLRequestContext() {
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
223 if (!url_request_context_)
224 InitializeURLRequestContext();
226 return url_request_context_.get();
229 scoped_refptr<base::SingleThreadTaskRunner>
230 AwURLRequestContextGetter::GetNetworkTaskRunner() const {
231 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
234 void AwURLRequestContextGetter::SetProtocolHandlers(
235 content::ProtocolHandlerMap* protocol_handlers) {
236 std::swap(protocol_handlers_, *protocol_handlers);
239 } // namespace android_webview