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_SYNC_WEBSOCKET_H_ | |
6 #define CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_H_ | |
7 | |
8 #include <list> | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/synchronization/condition_variable.h" | |
16 #include "base/synchronization/lock.h" | |
17 #include "chrome/test/chromedriver/net/websocket.h" | |
18 #include "net/base/completion_callback.h" | |
19 #include "net/socket_stream/socket_stream.h" | |
20 | |
21 namespace base { | |
22 class WaitableEvent; | |
23 } | |
24 | |
25 namespace net { | |
26 class URLRequestContextGetter; | |
27 } | |
28 | |
29 class GURL; | |
30 | |
31 // Proxy for using a WebSocket running on a background thread synchronously. | |
32 class SyncWebSocket { | |
33 public: | |
34 explicit SyncWebSocket(net::URLRequestContextGetter* context_getter); | |
35 virtual ~SyncWebSocket(); | |
36 | |
37 // Connects to the WebSocket server. Returns true on success. | |
38 bool Connect(const GURL& url); | |
39 | |
40 // Sends message. Returns true on success. | |
41 bool Send(const std::string& message); | |
42 | |
43 // Reads next message. Blocks until at least one message is received or | |
44 // the socket is closed. Returns true on success and modifies |message|. | |
45 bool ReadNextMessage(std::string* message); | |
Takashi Toyoshima
2012/11/28 09:11:30
ReceiveNextMessage looks better here.
Same on othe
kkania
2012/11/28 16:02:12
Done.
| |
46 | |
47 private: | |
48 struct CoreTraits; | |
49 class Core : public WebSocketListener, | |
50 public base::RefCountedThreadSafe<Core, CoreTraits> { | |
51 public: | |
52 explicit Core(net::URLRequestContextGetter* context_getter); | |
53 | |
54 bool Connect(const GURL& url); | |
55 | |
56 bool Send(const std::string& message); | |
57 | |
58 bool ReadNextMessage(std::string* message); | |
59 | |
60 // Overriden from WebSocketListener: | |
61 virtual void OnMessageReceived(const std::string& message) OVERRIDE; | |
62 virtual void OnClose() OVERRIDE; | |
63 | |
64 private: | |
65 friend class base::RefCountedThreadSafe<Core, CoreTraits>; | |
66 friend class base::DeleteHelper<Core>; | |
67 friend struct CoreTraits; | |
68 | |
69 virtual ~Core(); | |
70 | |
71 void ConnectOnIO(const GURL& url, | |
72 bool* success, | |
73 base::WaitableEvent* event); | |
74 void OnConnectCompletedOnIO(bool* connected, | |
75 base::WaitableEvent* event, | |
76 int error); | |
77 void SendOnIO(const std::string& message, | |
78 bool* result, | |
79 base::WaitableEvent* event); | |
80 | |
81 // OnDestruct is meant to ensure deletion on the IO thread. | |
82 void OnDestruct() const; | |
83 | |
84 scoped_refptr<net::URLRequestContextGetter> context_getter_; | |
85 | |
86 // Only accessed on IO thread. | |
87 scoped_ptr<WebSocket> socket_; | |
88 | |
89 base::Lock lock_; | |
90 | |
91 // Protected by |lock_|. | |
92 bool closed_; | |
93 | |
94 // Protected by |lock_|. | |
95 std::list<std::string> received_queue_; | |
96 | |
97 // Protected by |lock_|. | |
98 // Signaled when the socket closes or a message is received. | |
99 base::ConditionVariable on_update_event_; | |
100 }; | |
101 | |
102 scoped_refptr<Core> core_; | |
103 }; | |
104 | |
105 struct SyncWebSocket::CoreTraits { | |
106 static void Destruct(const SyncWebSocket::Core* core) { | |
107 core->OnDestruct(); | |
108 } | |
109 }; | |
110 | |
111 #endif // CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_H_ | |
OLD | NEW |