OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "webkit/tools/test_shell/simple_socket_stream_bridge.h" | 7 #include "webkit/tools/test_shell/simple_socket_stream_bridge.h" |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 virtual void Close(); | 39 virtual void Close(); |
40 | 40 |
41 // net::SocketStream::Delegate methods. | 41 // net::SocketStream::Delegate methods. |
42 virtual void OnConnected(net::SocketStream* req, | 42 virtual void OnConnected(net::SocketStream* req, |
43 int max_pending_send_allowed); | 43 int max_pending_send_allowed); |
44 virtual void OnSentData(net::SocketStream* req, | 44 virtual void OnSentData(net::SocketStream* req, |
45 int amount_sent); | 45 int amount_sent); |
46 virtual void OnReceivedData(net::SocketStream* req, | 46 virtual void OnReceivedData(net::SocketStream* req, |
47 const char* data, int len); | 47 const char* data, int len); |
48 virtual void OnClose(net::SocketStream* req); | 48 virtual void OnClose(net::SocketStream* req); |
| 49 virtual void OnError(const net::SocketStream* req, int error); |
49 | 50 |
50 private: | 51 private: |
51 virtual ~WebSocketStreamHandleBridgeImpl(); | 52 virtual ~WebSocketStreamHandleBridgeImpl(); |
52 | 53 |
53 // Runs on |g_io_thread|; | 54 // Runs on |g_io_thread|; |
54 void DoConnect(const GURL& url); | 55 void DoConnect(const GURL& url); |
55 void DoSend(std::vector<char>* data); | 56 void DoSend(std::vector<char>* data); |
56 void DoClose(); | 57 void DoClose(); |
57 | 58 |
58 // Runs on |message_loop_|; | 59 // Runs on |message_loop_|; |
59 void DoOnConnected(int max_amount_send_allowed); | 60 void DoOnConnected(int max_amount_send_allowed); |
60 void DoOnSentData(int amount_sent); | 61 void DoOnSentData(int amount_sent); |
61 void DoOnReceivedData(std::vector<char>* data); | 62 void DoOnReceivedData(std::vector<char>* data); |
62 void DoOnClose(); | 63 void DoOnClose(); |
| 64 void DoOnError(int error); |
63 | 65 |
64 int socket_id_; | 66 int socket_id_; |
65 MessageLoop* message_loop_; | 67 MessageLoop* message_loop_; |
66 WebKit::WebSocketStreamHandle* handle_; | 68 WebKit::WebSocketStreamHandle* handle_; |
67 webkit_glue::WebSocketStreamHandleDelegate* delegate_; | 69 webkit_glue::WebSocketStreamHandleDelegate* delegate_; |
68 | 70 |
69 scoped_refptr<net::SocketStreamJob> socket_; | 71 scoped_refptr<net::SocketStreamJob> socket_; |
70 // Number of pending tasks to handle net::SocketStream::Delegate methods. | 72 // Number of pending tasks to handle net::SocketStream::Delegate methods. |
71 int num_pending_tasks_; | 73 int num_pending_tasks_; |
72 | 74 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 void WebSocketStreamHandleBridgeImpl::OnClose(net::SocketStream* socket) { | 149 void WebSocketStreamHandleBridgeImpl::OnClose(net::SocketStream* socket) { |
148 ++num_pending_tasks_; | 150 ++num_pending_tasks_; |
149 // Release socket_ on IO thread. | 151 // Release socket_ on IO thread. |
150 socket_ = NULL; | 152 socket_ = NULL; |
151 socket_id_ = kNoSocketId; | 153 socket_id_ = kNoSocketId; |
152 message_loop_->PostTask( | 154 message_loop_->PostTask( |
153 FROM_HERE, | 155 FROM_HERE, |
154 NewRunnableMethod(this, &WebSocketStreamHandleBridgeImpl::DoOnClose)); | 156 NewRunnableMethod(this, &WebSocketStreamHandleBridgeImpl::DoOnClose)); |
155 } | 157 } |
156 | 158 |
| 159 void WebSocketStreamHandleBridgeImpl::OnError(const net::SocketStream* socket, |
| 160 int error) { |
| 161 ++num_pending_tasks_; |
| 162 message_loop_->PostTask( |
| 163 FROM_HERE, |
| 164 NewRunnableMethod(this, &WebSocketStreamHandleBridgeImpl::DoOnError, |
| 165 error)); |
| 166 } |
| 167 |
157 void WebSocketStreamHandleBridgeImpl::DoConnect(const GURL& url) { | 168 void WebSocketStreamHandleBridgeImpl::DoConnect(const GURL& url) { |
158 DCHECK(MessageLoop::current() == g_io_thread); | 169 DCHECK(MessageLoop::current() == g_io_thread); |
159 socket_ = net::SocketStreamJob::CreateSocketStreamJob(url, this); | 170 socket_ = net::SocketStreamJob::CreateSocketStreamJob(url, this); |
160 socket_->set_context(g_request_context); | 171 socket_->set_context(g_request_context); |
161 socket_->Connect(); | 172 socket_->Connect(); |
162 } | 173 } |
163 | 174 |
164 void WebSocketStreamHandleBridgeImpl::DoSend(std::vector<char>* data) { | 175 void WebSocketStreamHandleBridgeImpl::DoSend(std::vector<char>* data) { |
165 DCHECK(MessageLoop::current() == g_io_thread); | 176 DCHECK(MessageLoop::current() == g_io_thread); |
166 scoped_ptr<std::vector<char> > scoped_data(data); | 177 scoped_ptr<std::vector<char> > scoped_data(data); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 DCHECK_EQ(num_pending_tasks_, 0); | 219 DCHECK_EQ(num_pending_tasks_, 0); |
209 DCHECK(!socket_); | 220 DCHECK(!socket_); |
210 DCHECK_EQ(socket_id_, kNoSocketId); | 221 DCHECK_EQ(socket_id_, kNoSocketId); |
211 webkit_glue::WebSocketStreamHandleDelegate* delegate = delegate_; | 222 webkit_glue::WebSocketStreamHandleDelegate* delegate = delegate_; |
212 delegate_ = NULL; | 223 delegate_ = NULL; |
213 if (delegate) | 224 if (delegate) |
214 delegate->DidClose(handle_); | 225 delegate->DidClose(handle_); |
215 Release(); | 226 Release(); |
216 } | 227 } |
217 | 228 |
| 229 void WebSocketStreamHandleBridgeImpl::DoOnError(int error) { |
| 230 DCHECK(MessageLoop::current() == message_loop_); |
| 231 --num_pending_tasks_; |
| 232 if (delegate_) |
| 233 delegate_->DidFail(handle_, error); |
| 234 } |
| 235 |
218 } // namespace | 236 } // namespace |
219 | 237 |
220 /* static */ | 238 /* static */ |
221 void SimpleSocketStreamBridge::InitializeOnIOThread( | 239 void SimpleSocketStreamBridge::InitializeOnIOThread( |
222 net::URLRequestContext* request_context) { | 240 net::URLRequestContext* request_context) { |
223 g_io_thread = MessageLoop::current(); | 241 g_io_thread = MessageLoop::current(); |
224 g_request_context = request_context; | 242 g_request_context = request_context; |
225 } | 243 } |
226 | 244 |
227 void SimpleSocketStreamBridge::Cleanup() { | 245 void SimpleSocketStreamBridge::Cleanup() { |
228 g_io_thread = NULL; | 246 g_io_thread = NULL; |
229 g_request_context = NULL; | 247 g_request_context = NULL; |
230 } | 248 } |
231 | 249 |
232 namespace webkit_glue { | 250 namespace webkit_glue { |
233 | 251 |
234 /* static */ | 252 /* static */ |
235 WebSocketStreamHandleBridge* WebSocketStreamHandleBridge::Create( | 253 WebSocketStreamHandleBridge* WebSocketStreamHandleBridge::Create( |
236 WebKit::WebSocketStreamHandle* handle, | 254 WebKit::WebSocketStreamHandle* handle, |
237 WebSocketStreamHandleDelegate* delegate) { | 255 WebSocketStreamHandleDelegate* delegate) { |
238 return new WebSocketStreamHandleBridgeImpl(handle, delegate); | 256 return new WebSocketStreamHandleBridgeImpl(handle, delegate); |
239 } | 257 } |
240 | 258 |
241 } // namespace webkit_glue | 259 } // namespace webkit_glue |
OLD | NEW |