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, | |
162 int error_code) { | |
tyoshino (SeeGerritForStatus)
2013/04/15 14:23:28
(optional) L161 and 162 can fit in one line
| |
163 base::subtle::NoBarrier_AtomicIncrement(&num_pending_tasks_, 1); | |
164 message_loop_->PostTask( | |
165 FROM_HERE, | |
166 base::Bind(&WebSocketStreamHandleBridgeImpl::DoOnError, this, | |
167 error_code, net::ErrorToString(error_code))); | |
168 } | |
169 | |
157 void WebSocketStreamHandleBridgeImpl::DoConnect(const GURL& url) { | 170 void WebSocketStreamHandleBridgeImpl::DoConnect(const GURL& url) { |
158 DCHECK(MessageLoop::current() == g_io_thread); | 171 DCHECK(MessageLoop::current() == g_io_thread); |
159 socket_ = net::SocketStreamJob::CreateSocketStreamJob( | 172 socket_ = net::SocketStreamJob::CreateSocketStreamJob( |
160 url, this, g_request_context->transport_security_state(), | 173 url, this, g_request_context->transport_security_state(), |
161 g_request_context->ssl_config_service()); | 174 g_request_context->ssl_config_service()); |
162 socket_->set_context(g_request_context); | 175 socket_->set_context(g_request_context); |
163 socket_->Connect(); | 176 socket_->Connect(); |
164 } | 177 } |
165 | 178 |
166 void WebSocketStreamHandleBridgeImpl::DoSend(std::vector<char>* data) { | 179 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); | 223 DCHECK_EQ(num_pending_tasks_, 0); |
211 DCHECK(!socket_); | 224 DCHECK(!socket_); |
212 DCHECK_EQ(socket_id_, kNoSocketId); | 225 DCHECK_EQ(socket_id_, kNoSocketId); |
213 webkit_glue::WebSocketStreamHandleDelegate* delegate = delegate_; | 226 webkit_glue::WebSocketStreamHandleDelegate* delegate = delegate_; |
214 delegate_ = NULL; | 227 delegate_ = NULL; |
215 if (delegate) | 228 if (delegate) |
216 delegate->DidClose(handle_); | 229 delegate->DidClose(handle_); |
217 Release(); | 230 Release(); |
218 } | 231 } |
219 | 232 |
233 void WebSocketStreamHandleBridgeImpl::DoOnError( | |
234 int error_code, | |
235 const char* error_msg) { | |
tyoshino (SeeGerritForStatus)
2013/04/15 14:23:28
optional) L234 and 235 can fit in one line
| |
236 DCHECK(MessageLoop::current() == message_loop_); | |
237 base::subtle::NoBarrier_AtomicIncrement(&num_pending_tasks_, -1); | |
238 if (delegate_) | |
239 delegate_->DidFail(handle_, error_code, ASCIIToUTF16(error_msg)); | |
240 } | |
241 | |
220 } // namespace | 242 } // namespace |
221 | 243 |
222 /* static */ | 244 /* static */ |
223 void SimpleSocketStreamBridge::InitializeOnIOThread( | 245 void SimpleSocketStreamBridge::InitializeOnIOThread( |
224 net::URLRequestContext* request_context) { | 246 net::URLRequestContext* request_context) { |
225 g_io_thread = MessageLoop::current(); | 247 g_io_thread = MessageLoop::current(); |
226 g_request_context = request_context; | 248 g_request_context = request_context; |
227 } | 249 } |
228 | 250 |
229 void SimpleSocketStreamBridge::Cleanup() { | 251 void SimpleSocketStreamBridge::Cleanup() { |
230 g_io_thread = NULL; | 252 g_io_thread = NULL; |
231 g_request_context = NULL; | 253 g_request_context = NULL; |
232 } | 254 } |
233 | 255 |
234 /* static */ | 256 /* static */ |
235 webkit_glue::WebSocketStreamHandleBridge* SimpleSocketStreamBridge::Create( | 257 webkit_glue::WebSocketStreamHandleBridge* SimpleSocketStreamBridge::Create( |
236 WebKit::WebSocketStreamHandle* handle, | 258 WebKit::WebSocketStreamHandle* handle, |
237 webkit_glue::WebSocketStreamHandleDelegate* delegate) { | 259 webkit_glue::WebSocketStreamHandleDelegate* delegate) { |
238 return new WebSocketStreamHandleBridgeImpl(handle, delegate); | 260 return new WebSocketStreamHandleBridgeImpl(handle, delegate); |
239 } | 261 } |
OLD | NEW |