| 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 #include "net/websockets/websocket_job.h" | 5 #include "net/websockets/websocket_job.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/singleton.h" |
| 9 #include "base/string_tokenizer.h" | 10 #include "base/string_tokenizer.h" |
| 10 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
| 13 #include "net/base/cookie_policy.h" | 14 #include "net/base/cookie_policy.h" |
| 14 #include "net/base/cookie_store.h" | 15 #include "net/base/cookie_store.h" |
| 15 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 16 #include "net/http/http_util.h" | 17 #include "net/http/http_util.h" |
| 17 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 18 #include "net/websockets/websocket_frame_handler.h" | 19 #include "net/websockets/websocket_frame_handler.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 138 |
| 138 void WebSocketJob::RestartWithAuth( | 139 void WebSocketJob::RestartWithAuth( |
| 139 const string16& username, | 140 const string16& username, |
| 140 const string16& password) { | 141 const string16& password) { |
| 141 state_ = CONNECTING; | 142 state_ = CONNECTING; |
| 142 socket_->RestartWithAuth(username, password); | 143 socket_->RestartWithAuth(username, password); |
| 143 } | 144 } |
| 144 | 145 |
| 145 void WebSocketJob::DetachDelegate() { | 146 void WebSocketJob::DetachDelegate() { |
| 146 state_ = CLOSED; | 147 state_ = CLOSED; |
| 147 Singleton<WebSocketThrottle>::get()->RemoveFromQueue(this); | 148 WebSocketThrottle::GetInstance()->RemoveFromQueue(this); |
| 148 Singleton<WebSocketThrottle>::get()->WakeupSocketIfNecessary(); | 149 WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary(); |
| 149 | 150 |
| 150 scoped_refptr<WebSocketJob> protect(this); | 151 scoped_refptr<WebSocketJob> protect(this); |
| 151 | 152 |
| 152 delegate_ = NULL; | 153 delegate_ = NULL; |
| 153 if (socket_) | 154 if (socket_) |
| 154 socket_->DetachDelegate(); | 155 socket_->DetachDelegate(); |
| 155 socket_ = NULL; | 156 socket_ = NULL; |
| 156 if (callback_) { | 157 if (callback_) { |
| 157 waiting_ = false; | 158 waiting_ = false; |
| 158 callback_ = NULL; | 159 callback_ = NULL; |
| 159 Release(); // Balanced with OnStartOpenConnection(). | 160 Release(); // Balanced with OnStartOpenConnection(). |
| 160 } | 161 } |
| 161 } | 162 } |
| 162 | 163 |
| 163 int WebSocketJob::OnStartOpenConnection( | 164 int WebSocketJob::OnStartOpenConnection( |
| 164 SocketStream* socket, CompletionCallback* callback) { | 165 SocketStream* socket, CompletionCallback* callback) { |
| 165 DCHECK(!callback_); | 166 DCHECK(!callback_); |
| 166 state_ = CONNECTING; | 167 state_ = CONNECTING; |
| 167 addresses_.Copy(socket->address_list().head(), true); | 168 addresses_.Copy(socket->address_list().head(), true); |
| 168 Singleton<WebSocketThrottle>::get()->PutInQueue(this); | 169 WebSocketThrottle::GetInstance()->PutInQueue(this); |
| 169 if (!waiting_) | 170 if (!waiting_) |
| 170 return OK; | 171 return OK; |
| 171 callback_ = callback; | 172 callback_ = callback; |
| 172 AddRef(); // Balanced when callback_ becomes NULL. | 173 AddRef(); // Balanced when callback_ becomes NULL. |
| 173 return ERR_IO_PENDING; | 174 return ERR_IO_PENDING; |
| 174 } | 175 } |
| 175 | 176 |
| 176 void WebSocketJob::OnConnected( | 177 void WebSocketJob::OnConnected( |
| 177 SocketStream* socket, int max_pending_send_allowed) { | 178 SocketStream* socket, int max_pending_send_allowed) { |
| 178 if (state_ == CLOSED) | 179 if (state_ == CLOSED) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 receive_frame_handler_->GetCurrentBufferSize()); | 231 receive_frame_handler_->GetCurrentBufferSize()); |
| 231 receive_frame_handler_->ReleaseCurrentBuffer(); | 232 receive_frame_handler_->ReleaseCurrentBuffer(); |
| 232 } | 233 } |
| 233 if (delegate_ && received_data.size() > 0) | 234 if (delegate_ && received_data.size() > 0) |
| 234 delegate_->OnReceivedData( | 235 delegate_->OnReceivedData( |
| 235 socket, received_data.data(), received_data.size()); | 236 socket, received_data.data(), received_data.size()); |
| 236 } | 237 } |
| 237 | 238 |
| 238 void WebSocketJob::OnClose(SocketStream* socket) { | 239 void WebSocketJob::OnClose(SocketStream* socket) { |
| 239 state_ = CLOSED; | 240 state_ = CLOSED; |
| 240 Singleton<WebSocketThrottle>::get()->RemoveFromQueue(this); | 241 WebSocketThrottle::GetInstance()->RemoveFromQueue(this); |
| 241 Singleton<WebSocketThrottle>::get()->WakeupSocketIfNecessary(); | 242 WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary(); |
| 242 | 243 |
| 243 scoped_refptr<WebSocketJob> protect(this); | 244 scoped_refptr<WebSocketJob> protect(this); |
| 244 | 245 |
| 245 SocketStream::Delegate* delegate = delegate_; | 246 SocketStream::Delegate* delegate = delegate_; |
| 246 delegate_ = NULL; | 247 delegate_ = NULL; |
| 247 socket_ = NULL; | 248 socket_ = NULL; |
| 248 if (callback_) { | 249 if (callback_) { |
| 249 waiting_ = false; | 250 waiting_ = false; |
| 250 callback_ = NULL; | 251 callback_ = NULL; |
| 251 Release(); // Balanced with OnStartOpenConnection(). | 252 Release(); // Balanced with OnStartOpenConnection(). |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 receive_frame_handler_->ReleaseCurrentBuffer(); | 399 receive_frame_handler_->ReleaseCurrentBuffer(); |
| 399 } | 400 } |
| 400 | 401 |
| 401 state_ = OPEN; | 402 state_ = OPEN; |
| 402 if (delegate_) | 403 if (delegate_) |
| 403 delegate_->OnReceivedData( | 404 delegate_->OnReceivedData( |
| 404 socket_, received_data.data(), received_data.size()); | 405 socket_, received_data.data(), received_data.size()); |
| 405 | 406 |
| 406 handshake_response_.reset(); | 407 handshake_response_.reset(); |
| 407 | 408 |
| 408 Singleton<WebSocketThrottle>::get()->RemoveFromQueue(this); | 409 WebSocketThrottle::GetInstance()->RemoveFromQueue(this); |
| 409 Singleton<WebSocketThrottle>::get()->WakeupSocketIfNecessary(); | 410 WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary(); |
| 410 return; | 411 return; |
| 411 } | 412 } |
| 412 | 413 |
| 413 AddRef(); // Balanced in OnCanSetCookieCompleted | 414 AddRef(); // Balanced in OnCanSetCookieCompleted |
| 414 | 415 |
| 415 int policy = OK; | 416 int policy = OK; |
| 416 if (socket_->context()->cookie_policy()) { | 417 if (socket_->context()->cookie_policy()) { |
| 417 GURL url_for_cookies = GetURLForCookies(); | 418 GURL url_for_cookies = GetURLForCookies(); |
| 418 policy = socket_->context()->cookie_policy()->CanSetCookie( | 419 policy = socket_->context()->cookie_policy()->CanSetCookie( |
| 419 url_for_cookies, | 420 url_for_cookies, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 socket_->Close(); | 500 socket_->Close(); |
| 500 return; | 501 return; |
| 501 } | 502 } |
| 502 current_buffer_ = new DrainableIOBuffer( | 503 current_buffer_ = new DrainableIOBuffer( |
| 503 send_frame_handler_->GetCurrentBuffer(), | 504 send_frame_handler_->GetCurrentBuffer(), |
| 504 send_frame_handler_->GetCurrentBufferSize()); | 505 send_frame_handler_->GetCurrentBufferSize()); |
| 505 socket_->SendData(current_buffer_->data(), current_buffer_->BytesRemaining()); | 506 socket_->SendData(current_buffer_->data(), current_buffer_->BytesRemaining()); |
| 506 } | 507 } |
| 507 | 508 |
| 508 } // namespace net | 509 } // namespace net |
| OLD | NEW |