Roll src/third_party/WebKit bf18a82:a9cee16 (svn 185297:185304)
[chromium-blink-merge.git] / chrome / test / chromedriver / net / sync_websocket_impl.h
blob462511328ad577556628b9e7856d10713175a838
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.
5 #ifndef CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_IMPL_H_
6 #define CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_IMPL_H_
8 #include <list>
9 #include <string>
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/sync_websocket.h"
18 #include "chrome/test/chromedriver/net/websocket.h"
19 #include "net/base/completion_callback.h"
21 namespace base {
22 class WaitableEvent;
25 namespace net {
26 class URLRequestContextGetter;
29 class GURL;
31 class SyncWebSocketImpl : public SyncWebSocket {
32 public:
33 explicit SyncWebSocketImpl(net::URLRequestContextGetter* context_getter);
34 ~SyncWebSocketImpl() override;
36 // Overridden from SyncWebSocket:
37 bool IsConnected() override;
38 bool Connect(const GURL& url) override;
39 bool Send(const std::string& message) override;
40 StatusCode ReceiveNextMessage(std::string* message,
41 const base::TimeDelta& timeout) override;
42 bool HasNextMessage() override;
44 private:
45 struct CoreTraits;
46 class Core : public WebSocketListener,
47 public base::RefCountedThreadSafe<Core, CoreTraits> {
48 public:
49 explicit Core(net::URLRequestContextGetter* context_getter);
51 bool IsConnected();
52 bool Connect(const GURL& url);
53 bool Send(const std::string& message);
54 SyncWebSocket::StatusCode ReceiveNextMessage(
55 std::string* message,
56 const base::TimeDelta& timeout);
57 bool HasNextMessage();
59 // Overriden from WebSocketListener:
60 void OnMessageReceived(const std::string& message) override;
61 void OnClose() override;
63 private:
64 friend class base::RefCountedThreadSafe<Core, CoreTraits>;
65 friend class base::DeleteHelper<Core>;
66 friend struct CoreTraits;
68 ~Core() override;
70 void ConnectOnIO(const GURL& url,
71 bool* success,
72 base::WaitableEvent* event);
73 void OnConnectCompletedOnIO(bool* connected,
74 base::WaitableEvent* event,
75 int error);
76 void SendOnIO(const std::string& message,
77 bool* result,
78 base::WaitableEvent* event);
80 // OnDestruct is meant to ensure deletion on the IO thread.
81 void OnDestruct() const;
83 scoped_refptr<net::URLRequestContextGetter> context_getter_;
85 // Only accessed on IO thread.
86 scoped_ptr<WebSocket> socket_;
88 base::Lock lock_;
90 // Protected by |lock_|.
91 bool is_connected_;
93 // Protected by |lock_|.
94 std::list<std::string> received_queue_;
96 // Protected by |lock_|.
97 // Signaled when the socket closes or a message is received.
98 base::ConditionVariable on_update_event_;
101 scoped_refptr<Core> core_;
104 struct SyncWebSocketImpl::CoreTraits {
105 static void Destruct(const SyncWebSocketImpl::Core* core) {
106 core->OnDestruct();
110 #endif // CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_IMPL_H_