OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_TEST_CHROMEDRIVER_NET_WEBSOCKET_H_ |
| 6 #define CHROME_TEST_CHROMEDRIVER_NET_WEBSOCKET_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 #include "net/base/completion_callback.h" |
| 17 #include "net/base/net_errors.h" |
| 18 #include "net/socket_stream/socket_stream.h" |
| 19 |
| 20 namespace net { |
| 21 class URLRequestContextGetter; |
| 22 class WebSocketJob; |
| 23 } // namespace net |
| 24 |
| 25 class WebSocketListener; |
| 26 |
| 27 // A text-only, non-thread safe WebSocket. Must be created and used on a single |
| 28 // thread. Intended particularly for use with net::HttpServer. |
| 29 class WebSocket : public net::SocketStream::Delegate { |
| 30 public: |
| 31 WebSocket(net::URLRequestContextGetter* context_getter, |
| 32 const GURL& url, |
| 33 WebSocketListener* listener); |
| 34 virtual ~WebSocket(); |
| 35 |
| 36 // Initializes the WebSocket connection. Invokes the given callback with |
| 37 // a net::Error. May only be called once. |
| 38 void Connect(const net::CompletionCallback& callback); |
| 39 |
| 40 // Sends the given message and returns true on success. |
| 41 bool Send(const std::string& message); |
| 42 |
| 43 // Overridden from net::SocketStream::Delegate: |
| 44 virtual void OnConnected(net::SocketStream* socket, |
| 45 int max_pending_send_allowed) OVERRIDE; |
| 46 virtual void OnSentData(net::SocketStream* socket, |
| 47 int amount_sent) OVERRIDE; |
| 48 virtual void OnReceivedData(net::SocketStream* socket, |
| 49 const char* data, |
| 50 int len) OVERRIDE; |
| 51 virtual void OnClose(net::SocketStream* socket) OVERRIDE; |
| 52 |
| 53 private: |
| 54 void OnConnectFinished(net::Error error); |
| 55 |
| 56 base::ThreadChecker thread_checker_; |
| 57 scoped_refptr<net::URLRequestContextGetter> context_getter_; |
| 58 GURL url_; |
| 59 scoped_refptr<net::WebSocketJob> web_socket_; |
| 60 WebSocketListener* listener_; |
| 61 net::CompletionCallback connect_callback_; |
| 62 std::string sec_key_; |
| 63 std::string incoming_msg_buf_; |
| 64 bool connected_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(WebSocket); |
| 67 }; |
| 68 |
| 69 // Listens for WebSocket messages and disconnects on the same thread as the |
| 70 // WebSocket. |
| 71 class WebSocketListener { |
| 72 public: |
| 73 virtual ~WebSocketListener() {} |
| 74 |
| 75 // Called when a WebSocket message is received. |
| 76 virtual void OnMessageReceived(const std::string& message) = 0; |
| 77 |
| 78 // Called when the WebSocket connection closes. Will be called at most once. |
| 79 // Will not be called if the connection was never established or if the close |
| 80 // was initiated by the client. |
| 81 virtual void OnClose() = 0; |
| 82 }; |
| 83 |
| 84 #endif // CHROME_TEST_CHROMEDRIVER_NET_WEBSOCKET_H_ |
OLD | NEW |