OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cronet/android/url_request_context_adapter.h" | 5 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
6 | |
7 #include <limits> | |
8 | 6 |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
13 #include "components/cronet/url_request_context_config.h" | 10 #include "components/cronet/url_request_context_config.h" |
14 #include "net/android/network_change_notifier_factory_android.h" | |
15 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
16 #include "net/base/net_log_logger.h" | 12 #include "net/base/net_log_logger.h" |
17 #include "net/base/net_util.h" | |
18 #include "net/base/network_change_notifier.h" | |
19 #include "net/cert/cert_verifier.h" | 13 #include "net/cert/cert_verifier.h" |
20 #include "net/http/http_auth_handler_factory.h" | 14 #include "net/http/http_auth_handler_factory.h" |
21 #include "net/http/http_network_layer.h" | 15 #include "net/http/http_network_layer.h" |
22 #include "net/http/http_server_properties.h" | 16 #include "net/http/http_server_properties.h" |
| 17 #include "net/proxy/proxy_config_service_fixed.h" |
23 #include "net/proxy/proxy_service.h" | 18 #include "net/proxy/proxy_service.h" |
24 #include "net/ssl/ssl_config_service_defaults.h" | 19 #include "net/ssl/ssl_config_service_defaults.h" |
25 #include "net/url_request/static_http_user_agent_settings.h" | 20 #include "net/url_request/static_http_user_agent_settings.h" |
| 21 #include "net/url_request/url_request_context.h" |
26 #include "net/url_request/url_request_context_builder.h" | 22 #include "net/url_request/url_request_context_builder.h" |
27 #include "net/url_request/url_request_context_storage.h" | 23 #include "net/url_request/url_request_context_storage.h" |
28 #include "net/url_request/url_request_job_factory_impl.h" | 24 #include "net/url_request/url_request_job_factory_impl.h" |
29 | 25 |
30 namespace { | 26 namespace { |
31 | 27 |
32 // MessageLoop on the main thread, which is where objects that receive Java | |
33 // notifications generally live. | |
34 base::MessageLoop* g_main_message_loop = nullptr; | |
35 | |
36 net::NetworkChangeNotifier* g_network_change_notifier = nullptr; | |
37 | |
38 class BasicNetworkDelegate : public net::NetworkDelegate { | 28 class BasicNetworkDelegate : public net::NetworkDelegate { |
39 public: | 29 public: |
40 BasicNetworkDelegate() {} | 30 BasicNetworkDelegate() {} |
41 virtual ~BasicNetworkDelegate() {} | 31 virtual ~BasicNetworkDelegate() {} |
42 | 32 |
43 private: | 33 private: |
44 // net::NetworkDelegate implementation. | 34 // net::NetworkDelegate implementation. |
45 int OnBeforeURLRequest(net::URLRequest* request, | 35 int OnBeforeURLRequest(net::URLRequest* request, |
46 const net::CompletionCallback& callback, | 36 const net::CompletionCallback& callback, |
47 GURL* new_url) override { | 37 GURL* new_url) override { |
(...skipping 16 matching lines...) Expand all Loading... |
64 scoped_refptr<net::HttpResponseHeaders>* _response_headers, | 54 scoped_refptr<net::HttpResponseHeaders>* _response_headers, |
65 GURL* allowed_unsafe_redirect_url) override { | 55 GURL* allowed_unsafe_redirect_url) override { |
66 return net::OK; | 56 return net::OK; |
67 } | 57 } |
68 | 58 |
69 void OnBeforeRedirect(net::URLRequest* request, | 59 void OnBeforeRedirect(net::URLRequest* request, |
70 const GURL& new_location) override {} | 60 const GURL& new_location) override {} |
71 | 61 |
72 void OnResponseStarted(net::URLRequest* request) override {} | 62 void OnResponseStarted(net::URLRequest* request) override {} |
73 | 63 |
74 void OnRawBytesRead(const net::URLRequest& request, | 64 void OnRawBytesRead(const net::URLRequest& request, int bytes_read) override { |
75 int bytes_read) override {} | 65 } |
76 | 66 |
77 void OnCompleted(net::URLRequest* request, bool started) override {} | 67 void OnCompleted(net::URLRequest* request, bool started) override {} |
78 | 68 |
79 void OnURLRequestDestroyed(net::URLRequest* request) override {} | 69 void OnURLRequestDestroyed(net::URLRequest* request) override {} |
80 | 70 |
81 void OnPACScriptError(int line_number, | 71 void OnPACScriptError(int line_number, const base::string16& error) override { |
82 const base::string16& error) override {} | 72 } |
83 | 73 |
84 NetworkDelegate::AuthRequiredResponse OnAuthRequired( | 74 NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
85 net::URLRequest* request, | 75 net::URLRequest* request, |
86 const net::AuthChallengeInfo& auth_info, | 76 const net::AuthChallengeInfo& auth_info, |
87 const AuthCallback& callback, | 77 const AuthCallback& callback, |
88 net::AuthCredentials* credentials) override { | 78 net::AuthCredentials* credentials) override { |
89 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | 79 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; |
90 } | 80 } |
91 | 81 |
92 bool OnCanGetCookies(const net::URLRequest& request, | 82 bool OnCanGetCookies(const net::URLRequest& request, |
93 const net::CookieList& cookie_list) override { | 83 const net::CookieList& cookie_list) override { |
94 return false; | 84 return false; |
95 } | 85 } |
96 | 86 |
97 bool OnCanSetCookie(const net::URLRequest& request, | 87 bool OnCanSetCookie(const net::URLRequest& request, |
98 const std::string& cookie_line, | 88 const std::string& cookie_line, |
99 net::CookieOptions* options) override { | 89 net::CookieOptions* options) override { |
100 return false; | 90 return false; |
101 } | 91 } |
102 | 92 |
103 bool OnCanAccessFile(const net::URLRequest& request, | 93 bool OnCanAccessFile(const net::URLRequest& request, |
104 const base::FilePath& path) const override { | 94 const base::FilePath& path) const override { |
105 return false; | 95 return false; |
106 } | 96 } |
107 | 97 |
108 bool OnCanThrottleRequest( | 98 bool OnCanThrottleRequest(const net::URLRequest& request) const override { |
109 const net::URLRequest& request) const override { | |
110 return false; | 99 return false; |
111 } | 100 } |
112 | 101 |
113 int OnBeforeSocketStreamConnect( | 102 int OnBeforeSocketStreamConnect( |
114 net::SocketStream* stream, | 103 net::SocketStream* stream, |
115 const net::CompletionCallback& callback) override { | 104 const net::CompletionCallback& callback) override { |
116 return net::OK; | 105 return net::OK; |
117 } | 106 } |
118 | 107 |
119 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | 108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
120 }; | 109 }; |
121 | 110 |
122 } // namespace | 111 } // namespace |
123 | 112 |
124 namespace cronet { | 113 namespace cronet { |
125 | 114 |
126 URLRequestContextAdapter::URLRequestContextAdapter( | 115 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter() { |
127 URLRequestContextAdapterDelegate* delegate, | |
128 std::string user_agent) { | |
129 delegate_ = delegate; | |
130 user_agent_ = user_agent; | |
131 } | 116 } |
132 | 117 |
133 void URLRequestContextAdapter::Initialize( | 118 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
134 scoped_ptr<URLRequestContextConfig> config) { | 119 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 120 StopNetLogOnNetworkThread(); |
| 121 } |
| 122 |
| 123 void CronetURLRequestContextAdapter::Initialize( |
| 124 scoped_ptr<URLRequestContextConfig> config, |
| 125 const base::Closure& java_init_network_thread) { |
135 network_thread_ = new base::Thread("network"); | 126 network_thread_ = new base::Thread("network"); |
136 base::Thread::Options options; | 127 base::Thread::Options options; |
137 options.message_loop_type = base::MessageLoop::TYPE_IO; | 128 options.message_loop_type = base::MessageLoop::TYPE_IO; |
138 network_thread_->StartWithOptions(options); | 129 network_thread_->StartWithOptions(options); |
139 config_ = config.Pass(); | 130 |
| 131 GetNetworkTaskRunner()->PostTask( |
| 132 FROM_HERE, |
| 133 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
| 134 base::Unretained(this), |
| 135 Passed(&config), |
| 136 java_init_network_thread)); |
140 } | 137 } |
141 | 138 |
142 void URLRequestContextAdapter::InitRequestContextOnMainThread() { | 139 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
143 if (!base::MessageLoop::current()) { | 140 scoped_ptr<URLRequestContextConfig> config, |
144 DCHECK(!g_main_message_loop); | 141 const base::Closure& java_init_network_thread) { |
145 g_main_message_loop = new base::MessageLoopForUI(); | |
146 base::MessageLoopForUI::current()->Start(); | |
147 } | |
148 DCHECK_EQ(g_main_message_loop, base::MessageLoop::current()); | |
149 if (!g_network_change_notifier) { | |
150 net::NetworkChangeNotifier::SetFactory( | |
151 new net::NetworkChangeNotifierFactoryAndroid()); | |
152 g_network_change_notifier = net::NetworkChangeNotifier::Create(); | |
153 } | |
154 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | |
155 GetNetworkTaskRunner(), NULL)); | |
156 GetNetworkTaskRunner()->PostTask( | |
157 FROM_HERE, | |
158 base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread, | |
159 this)); | |
160 } | |
161 | |
162 void URLRequestContextAdapter::InitRequestContextOnNetworkThread() { | |
163 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 142 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
164 DCHECK(config_); | |
165 // TODO(mmenke): Add method to have the builder enable SPDY. | 143 // TODO(mmenke): Add method to have the builder enable SPDY. |
166 net::URLRequestContextBuilder context_builder; | 144 net::URLRequestContextBuilder context_builder; |
167 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 145 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
168 context_builder.set_proxy_config_service(proxy_config_service_.get()); | 146 context_builder.set_proxy_config_service( |
169 config_->ConfigureURLRequestContextBuilder(&context_builder); | 147 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
| 148 config->ConfigureURLRequestContextBuilder(&context_builder); |
170 | 149 |
171 context_.reset(context_builder.Build()); | 150 context_.reset(context_builder.Build()); |
172 | 151 |
173 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 152 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
174 if (config_->enable_quic) { | 153 if (config->enable_quic) { |
175 context_->http_server_properties() | 154 context_->http_server_properties() |
176 ->SetAlternateProtocolProbabilityThreshold(0.0f); | 155 ->SetAlternateProtocolProbabilityThreshold(0.0f); |
177 for (size_t hint = 0; hint < config_->quic_hints.size(); ++hint) { | 156 for (auto hint = config->quic_hints.begin(); |
178 const URLRequestContextConfig::QuicHint& quic_hint = | 157 hint != config->quic_hints.end(); ++hint) { |
179 *config_->quic_hints[hint]; | 158 const URLRequestContextConfig::QuicHint& quic_hint = **hint; |
180 if (quic_hint.host.empty()) { | 159 if (quic_hint.host.empty()) { |
181 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; | 160 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; |
182 continue; | 161 continue; |
183 } | 162 } |
184 | 163 |
185 url::CanonHostInfo host_info; | 164 url::CanonHostInfo host_info; |
186 std::string canon_host(net::CanonicalizeHost(quic_hint.host, &host_info)); | 165 std::string canon_host(net::CanonicalizeHost(quic_hint.host, &host_info)); |
187 if (!host_info.IsIPAddress() && | 166 if (!host_info.IsIPAddress() && |
188 !net::IsCanonicalizedHostCompliant(canon_host)) { | 167 !net::IsCanonicalizedHostCompliant(canon_host)) { |
189 LOG(ERROR) << "Invalid QUIC hint host: " << quic_hint.host; | 168 LOG(ERROR) << "Invalid QUIC hint host: " << quic_hint.host; |
(...skipping 16 matching lines...) Expand all Loading... |
206 | 185 |
207 net::HostPortPair quic_hint_host_port_pair(canon_host, | 186 net::HostPortPair quic_hint_host_port_pair(canon_host, |
208 quic_hint.port); | 187 quic_hint.port); |
209 context_->http_server_properties()->SetAlternateProtocol( | 188 context_->http_server_properties()->SetAlternateProtocol( |
210 quic_hint_host_port_pair, | 189 quic_hint_host_port_pair, |
211 static_cast<uint16>(quic_hint.alternate_port), | 190 static_cast<uint16>(quic_hint.alternate_port), |
212 net::AlternateProtocol::QUIC, | 191 net::AlternateProtocol::QUIC, |
213 1.0f); | 192 1.0f); |
214 } | 193 } |
215 } | 194 } |
216 config_.reset(NULL); | |
217 | 195 |
218 if (VLOG_IS_ON(2)) { | 196 java_init_network_thread.Run(); |
219 net_log_observer_.reset(new NetLogObserver()); | |
220 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | |
221 net::NetLog::LOG_ALL_BUT_BYTES); | |
222 } | |
223 | |
224 is_context_initialized_ = true; | |
225 while (!tasks_waiting_for_context_.empty()) { | |
226 tasks_waiting_for_context_.front().Run(); | |
227 tasks_waiting_for_context_.pop(); | |
228 } | |
229 | |
230 delegate_->OnContextInitialized(this); | |
231 } | 197 } |
232 | 198 |
233 void URLRequestContextAdapter::PostTaskToNetworkThread( | 199 void CronetURLRequestContextAdapter::Destroy() { |
234 const tracked_objects::Location& posted_from, | 200 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
235 const RunAfterContextInitTask& callback) { | 201 // Stick network_thread_ in a local, as |this| may be destroyed from the |
236 GetNetworkTaskRunner()->PostTask( | 202 // network thread before delete network_thread is called. |
237 posted_from, | 203 base::Thread* network_thread = network_thread_; |
238 base::Bind( | 204 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); |
239 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, | 205 // Deleting thread stops it after all tasks are completed. |
240 this, | 206 delete network_thread; |
241 callback)); | |
242 } | 207 } |
243 | 208 |
244 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( | 209 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
245 const RunAfterContextInitTask& callback) { | |
246 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
247 if (is_context_initialized_) { | |
248 callback.Run(); | |
249 return; | |
250 } | |
251 tasks_waiting_for_context_.push(callback); | |
252 } | |
253 | |
254 URLRequestContextAdapter::~URLRequestContextAdapter() { | |
255 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
256 if (net_log_observer_) { | |
257 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); | |
258 net_log_observer_.reset(); | |
259 } | |
260 StopNetLogHelper(); | |
261 // TODO(mef): Ensure that |network_thread_| is destroyed properly. | |
262 } | |
263 | |
264 const std::string& URLRequestContextAdapter::GetUserAgent( | |
265 const GURL& url) const { | |
266 return user_agent_; | |
267 } | |
268 | |
269 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { | |
270 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
271 if (!context_) { | 210 if (!context_) { |
272 LOG(ERROR) << "URLRequestContext is not set up"; | 211 LOG(ERROR) << "URLRequestContext is not set up"; |
273 } | 212 } |
274 return context_.get(); | 213 return context_.get(); |
275 } | 214 } |
276 | 215 |
277 scoped_refptr<base::SingleThreadTaskRunner> | 216 scoped_refptr<base::SingleThreadTaskRunner> |
278 URLRequestContextAdapter::GetNetworkTaskRunner() const { | 217 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
279 return network_thread_->message_loop_proxy(); | 218 return network_thread_->task_runner(); |
280 } | 219 } |
281 | 220 |
282 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { | 221 void CronetURLRequestContextAdapter::StartNetLogToFile( |
283 PostTaskToNetworkThread( | 222 const std::string& file_name) { |
| 223 GetNetworkTaskRunner()->PostTask( |
284 FROM_HERE, | 224 FROM_HERE, |
285 base::Bind( | 225 base::Bind( |
286 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name)); | 226 &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread, |
| 227 base::Unretained(this), |
| 228 file_name)); |
287 } | 229 } |
288 | 230 |
289 void URLRequestContextAdapter::StopNetLog() { | 231 void CronetURLRequestContextAdapter::StopNetLog() { |
290 PostTaskToNetworkThread( | 232 GetNetworkTaskRunner()->PostTask( |
291 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this)); | 233 FROM_HERE, |
| 234 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, |
| 235 base::Unretained(this))); |
292 } | 236 } |
293 | 237 |
294 void URLRequestContextAdapter::StartNetLogToFileHelper( | 238 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( |
295 const std::string& file_name) { | 239 const std::string& file_name) { |
296 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 240 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
297 // Do nothing if already logging to a file. | 241 // Do nothing if already logging to a file. |
298 if (net_log_logger_) | 242 if (net_log_logger_) |
299 return; | 243 return; |
300 | 244 |
301 base::FilePath file_path(file_name); | 245 base::FilePath file_path(file_name); |
302 FILE* file = base::OpenFile(file_path, "w"); | 246 FILE* file = base::OpenFile(file_path, "w"); |
303 if (!file) | 247 if (!file) |
304 return; | 248 return; |
305 | 249 |
306 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); | 250 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); |
307 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 251 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); |
308 net_log_logger_->StartObserving(context_->net_log()); | 252 net_log_logger_->StartObserving(context_->net_log()); |
309 } | 253 } |
310 | 254 |
311 void URLRequestContextAdapter::StopNetLogHelper() { | 255 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
312 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 256 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
313 if (net_log_logger_) { | 257 if (net_log_logger_) { |
314 net_log_logger_->StopObserving(); | 258 net_log_logger_->StopObserving(); |
315 net_log_logger_.reset(); | 259 net_log_logger_.reset(); |
316 } | 260 } |
317 } | 261 } |
318 | 262 |
319 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | |
320 VLOG(2) << "Net log entry: type=" << entry.type() | |
321 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | |
322 } | |
323 | |
324 } // namespace cronet | 263 } // namespace cronet |
OLD | NEW |