OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // TODO(ukai): code is similar with http_network_transaction.cc. We should | 5 // TODO(ukai): code is similar with http_network_transaction.cc. We should |
6 // think about ways to share code, if possible. | 6 // think about ways to share code, if possible. |
7 | 7 |
8 #include "net/socket_stream/socket_stream.h" | 8 #include "net/socket_stream/socket_stream.h" |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 void SocketStream::SetUserData(const void* key, UserData* data) { | 82 void SocketStream::SetUserData(const void* key, UserData* data) { |
83 user_data_[key] = linked_ptr<UserData>(data); | 83 user_data_[key] = linked_ptr<UserData>(data); |
84 } | 84 } |
85 | 85 |
86 void SocketStream::set_context(URLRequestContext* context) { | 86 void SocketStream::set_context(URLRequestContext* context) { |
87 scoped_refptr<URLRequestContext> prev_context = context_; | 87 scoped_refptr<URLRequestContext> prev_context = context_; |
88 | 88 |
89 context_ = context; | 89 context_ = context; |
90 | 90 |
91 if (prev_context != context) { | 91 if (prev_context != context) { |
92 net_log_.EndEvent(NetLog::TYPE_REQUEST_ALIVE); | 92 net_log_.EndEvent(NetLog::TYPE_REQUEST_ALIVE, NULL); |
93 net_log_ = BoundNetLog(); | 93 net_log_ = BoundNetLog(); |
94 | 94 |
95 if (context) { | 95 if (context) { |
96 net_log_ = BoundNetLog::Make( | 96 net_log_ = BoundNetLog::Make( |
97 context->net_log(), | 97 context->net_log(), |
98 NetLog::SOURCE_SOCKET_STREAM); | 98 NetLog::SOURCE_SOCKET_STREAM); |
99 | 99 |
100 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); | 100 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE, NULL); |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 if (context_) { | 104 if (context_) { |
105 host_resolver_ = context_->host_resolver(); | 105 host_resolver_ = context_->host_resolver(); |
106 http_auth_handler_factory_ = context_->http_auth_handler_factory(); | 106 http_auth_handler_factory_ = context_->http_auth_handler_factory(); |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 void SocketStream::Connect() { | 110 void SocketStream::Connect() { |
111 DCHECK(MessageLoop::current()) << | 111 DCHECK(MessageLoop::current()) << |
112 "The current MessageLoop must exist"; | 112 "The current MessageLoop must exist"; |
113 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << | 113 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
114 "The current MessageLoop must be TYPE_IO"; | 114 "The current MessageLoop must be TYPE_IO"; |
115 if (context_) | 115 if (context_) |
116 ssl_config_service()->GetSSLConfig(&ssl_config_); | 116 ssl_config_service()->GetSSLConfig(&ssl_config_); |
117 DCHECK_EQ(next_state_, STATE_NONE); | 117 DCHECK_EQ(next_state_, STATE_NONE); |
118 | 118 |
119 AddRef(); // Released in Finish() | 119 AddRef(); // Released in Finish() |
120 // Open a connection asynchronously, so that delegate won't be called | 120 // Open a connection asynchronously, so that delegate won't be called |
121 // back before returning Connect(). | 121 // back before returning Connect(). |
122 next_state_ = STATE_RESOLVE_PROXY; | 122 next_state_ = STATE_RESOLVE_PROXY; |
123 net_log_.BeginEventWithString(NetLog::TYPE_SOCKET_STREAM_CONNECT, | 123 net_log_.BeginEvent( |
124 "url", url_.possibly_invalid_spec()); | 124 NetLog::TYPE_SOCKET_STREAM_CONNECT, |
| 125 new NetLogStringParameter("url", url_.possibly_invalid_spec())); |
125 MessageLoop::current()->PostTask( | 126 MessageLoop::current()->PostTask( |
126 FROM_HERE, | 127 FROM_HERE, |
127 NewRunnableMethod(this, &SocketStream::DoLoop, OK)); | 128 NewRunnableMethod(this, &SocketStream::DoLoop, OK)); |
128 } | 129 } |
129 | 130 |
130 bool SocketStream::SendData(const char* data, int len) { | 131 bool SocketStream::SendData(const char* data, int len) { |
131 DCHECK(MessageLoop::current()) << | 132 DCHECK(MessageLoop::current()) << |
132 "The current MessageLoop must exist"; | 133 "The current MessageLoop must exist"; |
133 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << | 134 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
134 "The current MessageLoop must be TYPE_IO"; | 135 "The current MessageLoop must be TYPE_IO"; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 206 |
206 MessageLoop::current()->PostTask( | 207 MessageLoop::current()->PostTask( |
207 FROM_HERE, | 208 FROM_HERE, |
208 NewRunnableMethod(this, &SocketStream::DoRestartWithAuth)); | 209 NewRunnableMethod(this, &SocketStream::DoRestartWithAuth)); |
209 } | 210 } |
210 | 211 |
211 void SocketStream::DetachDelegate() { | 212 void SocketStream::DetachDelegate() { |
212 if (!delegate_) | 213 if (!delegate_) |
213 return; | 214 return; |
214 delegate_ = NULL; | 215 delegate_ = NULL; |
215 net_log_.AddEvent(NetLog::TYPE_CANCELLED); | 216 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); |
216 Close(); | 217 Close(); |
217 } | 218 } |
218 | 219 |
219 void SocketStream::Finish(int result) { | 220 void SocketStream::Finish(int result) { |
220 DCHECK(MessageLoop::current()) << | 221 DCHECK(MessageLoop::current()) << |
221 "The current MessageLoop must exist"; | 222 "The current MessageLoop must exist"; |
222 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << | 223 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
223 "The current MessageLoop must be TYPE_IO"; | 224 "The current MessageLoop must be TYPE_IO"; |
224 DCHECK_LE(result, OK); | 225 DCHECK_LE(result, OK); |
225 if (result == OK) | 226 if (result == OK) |
(...skipping 28 matching lines...) Expand all Loading... |
254 } | 255 } |
255 | 256 |
256 int SocketStream::DidEstablishConnection() { | 257 int SocketStream::DidEstablishConnection() { |
257 if (!socket_.get() || !socket_->IsConnected()) { | 258 if (!socket_.get() || !socket_->IsConnected()) { |
258 next_state_ = STATE_CLOSE; | 259 next_state_ = STATE_CLOSE; |
259 return ERR_CONNECTION_FAILED; | 260 return ERR_CONNECTION_FAILED; |
260 } | 261 } |
261 next_state_ = STATE_READ_WRITE; | 262 next_state_ = STATE_READ_WRITE; |
262 metrics_->OnConnected(); | 263 metrics_->OnConnected(); |
263 | 264 |
264 net_log_.EndEvent(NetLog::TYPE_SOCKET_STREAM_CONNECT); | 265 net_log_.EndEvent(NetLog::TYPE_SOCKET_STREAM_CONNECT, NULL); |
265 if (delegate_) | 266 if (delegate_) |
266 delegate_->OnConnected(this, max_pending_send_allowed_); | 267 delegate_->OnConnected(this, max_pending_send_allowed_); |
267 | 268 |
268 return OK; | 269 return OK; |
269 } | 270 } |
270 | 271 |
271 int SocketStream::DidReceiveData(int result) { | 272 int SocketStream::DidReceiveData(int result) { |
272 DCHECK(read_buf_); | 273 DCHECK(read_buf_); |
273 DCHECK_GT(result, 0); | 274 DCHECK_GT(result, 0); |
274 net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_RECEIVED); | 275 net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_RECEIVED, NULL); |
275 int len = result; | 276 int len = result; |
276 metrics_->OnRead(len); | 277 metrics_->OnRead(len); |
277 if (delegate_) { | 278 if (delegate_) { |
278 // Notify recevied data to delegate. | 279 // Notify recevied data to delegate. |
279 delegate_->OnReceivedData(this, read_buf_->data(), len); | 280 delegate_->OnReceivedData(this, read_buf_->data(), len); |
280 } | 281 } |
281 read_buf_ = NULL; | 282 read_buf_ = NULL; |
282 return OK; | 283 return OK; |
283 } | 284 } |
284 | 285 |
285 int SocketStream::DidSendData(int result) { | 286 int SocketStream::DidSendData(int result) { |
286 DCHECK_GT(result, 0); | 287 DCHECK_GT(result, 0); |
287 net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_SENT); | 288 net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_SENT, NULL); |
288 int len = result; | 289 int len = result; |
289 metrics_->OnWrite(len); | 290 metrics_->OnWrite(len); |
290 current_write_buf_ = NULL; | 291 current_write_buf_ = NULL; |
291 if (delegate_) | 292 if (delegate_) |
292 delegate_->OnSentData(this, len); | 293 delegate_->OnSentData(this, len); |
293 | 294 |
294 int remaining_size = write_buf_size_ - write_buf_offset_ - len; | 295 int remaining_size = write_buf_size_ - write_buf_offset_ - len; |
295 if (remaining_size == 0) { | 296 if (remaining_size == 0) { |
296 if (!pending_write_bufs_.empty()) { | 297 if (!pending_write_bufs_.empty()) { |
297 write_buf_size_ = pending_write_bufs_.front()->size(); | 298 write_buf_size_ = pending_write_bufs_.front()->size(); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 return; | 400 return; |
400 default: | 401 default: |
401 NOTREACHED() << "bad state"; | 402 NOTREACHED() << "bad state"; |
402 Finish(result); | 403 Finish(result); |
403 return; | 404 return; |
404 } | 405 } |
405 // If the connection is not established yet and had actual errors, | 406 // If the connection is not established yet and had actual errors, |
406 // close the connection. | 407 // close the connection. |
407 if (state != STATE_READ_WRITE && result < ERR_IO_PENDING) { | 408 if (state != STATE_READ_WRITE && result < ERR_IO_PENDING) { |
408 DCHECK_EQ(next_state_, STATE_CLOSE); | 409 DCHECK_EQ(next_state_, STATE_CLOSE); |
409 net_log_.EndEventWithInteger(NetLog::TYPE_SOCKET_STREAM_CONNECT, | 410 net_log_.EndEvent(NetLog::TYPE_SOCKET_STREAM_CONNECT, |
410 "net_error", result); | 411 new NetLogIntegerParameter("net_error", result)); |
411 } | 412 } |
412 } while (result != ERR_IO_PENDING); | 413 } while (result != ERR_IO_PENDING); |
413 } | 414 } |
414 | 415 |
415 int SocketStream::DoResolveProxy() { | 416 int SocketStream::DoResolveProxy() { |
416 DCHECK(!pac_request_); | 417 DCHECK(!pac_request_); |
417 next_state_ = STATE_RESOLVE_PROXY_COMPLETE; | 418 next_state_ = STATE_RESOLVE_PROXY_COMPLETE; |
418 | 419 |
419 if (!proxy_url_.is_valid()) { | 420 if (!proxy_url_.is_valid()) { |
420 next_state_ = STATE_CLOSE; | 421 next_state_ = STATE_CLOSE; |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
940 | 941 |
941 SSLConfigService* SocketStream::ssl_config_service() const { | 942 SSLConfigService* SocketStream::ssl_config_service() const { |
942 return context_->ssl_config_service(); | 943 return context_->ssl_config_service(); |
943 } | 944 } |
944 | 945 |
945 ProxyService* SocketStream::proxy_service() const { | 946 ProxyService* SocketStream::proxy_service() const { |
946 return context_->proxy_service(); | 947 return context_->proxy_service(); |
947 } | 948 } |
948 | 949 |
949 } // namespace net | 950 } // namespace net |
OLD | NEW |