OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/atomicops.h" | 9 #include "base/atomicops.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/utf_string_conversions.h" |
13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
14 #include "net/socket_stream/socket_stream_job.h" | 15 #include "net/socket_stream/socket_stream_job.h" |
15 #include "net/websockets/websocket_job.h" | 16 #include "net/websockets/websocket_job.h" |
16 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
17 #include "third_party/WebKit/Source/Platform/chromium/public/WebSocketStreamHand
le.h" | 18 #include "third_party/WebKit/Source/Platform/chromium/public/WebSocketStreamHand
le.h" |
18 #include "webkit/glue/websocketstreamhandle_bridge.h" | 19 #include "webkit/glue/websocketstreamhandle_bridge.h" |
19 #include "webkit/glue/websocketstreamhandle_delegate.h" | 20 #include "webkit/glue/websocketstreamhandle_delegate.h" |
20 | 21 |
21 using webkit_glue::WebSocketStreamHandleBridge; | 22 using webkit_glue::WebSocketStreamHandleBridge; |
22 | 23 |
(...skipping 18 matching lines...) Expand all Loading... |
41 virtual void Close() OVERRIDE; | 42 virtual void Close() OVERRIDE; |
42 | 43 |
43 // net::SocketStream::Delegate methods. | 44 // net::SocketStream::Delegate methods. |
44 virtual void OnConnected(net::SocketStream* req, | 45 virtual void OnConnected(net::SocketStream* req, |
45 int max_pending_send_allowed) OVERRIDE; | 46 int max_pending_send_allowed) OVERRIDE; |
46 virtual void OnSentData(net::SocketStream* req, | 47 virtual void OnSentData(net::SocketStream* req, |
47 int amount_sent) OVERRIDE; | 48 int amount_sent) OVERRIDE; |
48 virtual void OnReceivedData(net::SocketStream* req, | 49 virtual void OnReceivedData(net::SocketStream* req, |
49 const char* data, int len) OVERRIDE; | 50 const char* data, int len) OVERRIDE; |
50 virtual void OnClose(net::SocketStream* req) OVERRIDE; | 51 virtual void OnClose(net::SocketStream* req) OVERRIDE; |
| 52 virtual void OnError(const net::SocketStream* req, int error_code) OVERRIDE; |
51 | 53 |
52 private: | 54 private: |
53 virtual ~WebSocketStreamHandleBridgeImpl(); | 55 virtual ~WebSocketStreamHandleBridgeImpl(); |
54 | 56 |
55 // Runs on |g_io_thread|; | 57 // Runs on |g_io_thread|; |
56 void DoConnect(const GURL& url); | 58 void DoConnect(const GURL& url); |
57 void DoSend(std::vector<char>* data); | 59 void DoSend(std::vector<char>* data); |
58 void DoClose(); | 60 void DoClose(); |
59 | 61 |
60 // Runs on |message_loop_|; | 62 // Runs on |message_loop_|; |
61 void DoOnConnected(int max_amount_send_allowed); | 63 void DoOnConnected(int max_amount_send_allowed); |
62 void DoOnSentData(int amount_sent); | 64 void DoOnSentData(int amount_sent); |
63 void DoOnReceivedData(std::vector<char>* data); | 65 void DoOnReceivedData(std::vector<char>* data); |
64 void DoOnClose(); | 66 void DoOnClose(); |
| 67 void DoOnError(int error_code, const char* error_msg); |
65 | 68 |
66 int socket_id_; | 69 int socket_id_; |
67 MessageLoop* message_loop_; | 70 MessageLoop* message_loop_; |
68 WebKit::WebSocketStreamHandle* handle_; | 71 WebKit::WebSocketStreamHandle* handle_; |
69 webkit_glue::WebSocketStreamHandleDelegate* delegate_; | 72 webkit_glue::WebSocketStreamHandleDelegate* delegate_; |
70 | 73 |
71 scoped_refptr<net::SocketStreamJob> socket_; | 74 scoped_refptr<net::SocketStreamJob> socket_; |
72 // Number of pending tasks to handle net::SocketStream::Delegate methods. | 75 // Number of pending tasks to handle net::SocketStream::Delegate methods. |
73 base::subtle::Atomic32 num_pending_tasks_; | 76 base::subtle::Atomic32 num_pending_tasks_; |
74 | 77 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 void WebSocketStreamHandleBridgeImpl::OnClose(net::SocketStream* socket) { | 150 void WebSocketStreamHandleBridgeImpl::OnClose(net::SocketStream* socket) { |
148 base::subtle::NoBarrier_AtomicIncrement(&num_pending_tasks_, 1); | 151 base::subtle::NoBarrier_AtomicIncrement(&num_pending_tasks_, 1); |
149 // Release socket_ on IO thread. | 152 // Release socket_ on IO thread. |
150 socket_ = NULL; | 153 socket_ = NULL; |
151 socket_id_ = kNoSocketId; | 154 socket_id_ = kNoSocketId; |
152 message_loop_->PostTask( | 155 message_loop_->PostTask( |
153 FROM_HERE, | 156 FROM_HERE, |
154 base::Bind(&WebSocketStreamHandleBridgeImpl::DoOnClose, this)); | 157 base::Bind(&WebSocketStreamHandleBridgeImpl::DoOnClose, this)); |
155 } | 158 } |
156 | 159 |
| 160 void WebSocketStreamHandleBridgeImpl::OnError( |
| 161 const net::SocketStream* socket, int error_code) { |
| 162 base::subtle::NoBarrier_AtomicIncrement(&num_pending_tasks_, 1); |
| 163 message_loop_->PostTask( |
| 164 FROM_HERE, |
| 165 base::Bind(&WebSocketStreamHandleBridgeImpl::DoOnError, this, |
| 166 error_code, net::ErrorToString(error_code))); |
| 167 } |
| 168 |
157 void WebSocketStreamHandleBridgeImpl::DoConnect(const GURL& url) { | 169 void WebSocketStreamHandleBridgeImpl::DoConnect(const GURL& url) { |
158 DCHECK(MessageLoop::current() == g_io_thread); | 170 DCHECK(MessageLoop::current() == g_io_thread); |
159 socket_ = net::SocketStreamJob::CreateSocketStreamJob( | 171 socket_ = net::SocketStreamJob::CreateSocketStreamJob( |
160 url, this, g_request_context->transport_security_state(), | 172 url, this, g_request_context->transport_security_state(), |
161 g_request_context->ssl_config_service()); | 173 g_request_context->ssl_config_service()); |
162 socket_->set_context(g_request_context); | 174 socket_->set_context(g_request_context); |
163 socket_->Connect(); | 175 socket_->Connect(); |
164 } | 176 } |
165 | 177 |
166 void WebSocketStreamHandleBridgeImpl::DoSend(std::vector<char>* data) { | 178 void WebSocketStreamHandleBridgeImpl::DoSend(std::vector<char>* data) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 DCHECK_EQ(num_pending_tasks_, 0); | 222 DCHECK_EQ(num_pending_tasks_, 0); |
211 DCHECK(!socket_); | 223 DCHECK(!socket_); |
212 DCHECK_EQ(socket_id_, kNoSocketId); | 224 DCHECK_EQ(socket_id_, kNoSocketId); |
213 webkit_glue::WebSocketStreamHandleDelegate* delegate = delegate_; | 225 webkit_glue::WebSocketStreamHandleDelegate* delegate = delegate_; |
214 delegate_ = NULL; | 226 delegate_ = NULL; |
215 if (delegate) | 227 if (delegate) |
216 delegate->DidClose(handle_); | 228 delegate->DidClose(handle_); |
217 Release(); | 229 Release(); |
218 } | 230 } |
219 | 231 |
| 232 void WebSocketStreamHandleBridgeImpl::DoOnError( |
| 233 int error_code, const char* error_msg) { |
| 234 DCHECK(MessageLoop::current() == message_loop_); |
| 235 base::subtle::NoBarrier_AtomicIncrement(&num_pending_tasks_, -1); |
| 236 if (delegate_) |
| 237 delegate_->DidFail(handle_, error_code, ASCIIToUTF16(error_msg)); |
| 238 } |
| 239 |
220 } // namespace | 240 } // namespace |
221 | 241 |
222 /* static */ | 242 /* static */ |
223 void SimpleSocketStreamBridge::InitializeOnIOThread( | 243 void SimpleSocketStreamBridge::InitializeOnIOThread( |
224 net::URLRequestContext* request_context) { | 244 net::URLRequestContext* request_context) { |
225 g_io_thread = MessageLoop::current(); | 245 g_io_thread = MessageLoop::current(); |
226 g_request_context = request_context; | 246 g_request_context = request_context; |
227 } | 247 } |
228 | 248 |
229 void SimpleSocketStreamBridge::Cleanup() { | 249 void SimpleSocketStreamBridge::Cleanup() { |
230 g_io_thread = NULL; | 250 g_io_thread = NULL; |
231 g_request_context = NULL; | 251 g_request_context = NULL; |
232 } | 252 } |
233 | 253 |
234 /* static */ | 254 /* static */ |
235 webkit_glue::WebSocketStreamHandleBridge* SimpleSocketStreamBridge::Create( | 255 webkit_glue::WebSocketStreamHandleBridge* SimpleSocketStreamBridge::Create( |
236 WebKit::WebSocketStreamHandle* handle, | 256 WebKit::WebSocketStreamHandle* handle, |
237 webkit_glue::WebSocketStreamHandleDelegate* delegate) { | 257 webkit_glue::WebSocketStreamHandleDelegate* delegate) { |
238 return new WebSocketStreamHandleBridgeImpl(handle, delegate); | 258 return new WebSocketStreamHandleBridgeImpl(handle, delegate); |
239 } | 259 } |
OLD | NEW |