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_
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"
26 class URLRequestContextGetter
;
31 class SyncWebSocketImpl
: public SyncWebSocket
{
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
;
46 class Core
: public WebSocketListener
,
47 public base::RefCountedThreadSafe
<Core
, CoreTraits
> {
49 explicit Core(net::URLRequestContextGetter
* context_getter
);
52 bool Connect(const GURL
& url
);
53 bool Send(const std::string
& message
);
54 SyncWebSocket::StatusCode
ReceiveNextMessage(
56 const base::TimeDelta
& timeout
);
57 bool HasNextMessage();
59 // Overriden from WebSocketListener:
60 void OnMessageReceived(const std::string
& message
) override
;
61 void OnClose() override
;
64 friend class base::RefCountedThreadSafe
<Core
, CoreTraits
>;
65 friend class base::DeleteHelper
<Core
>;
66 friend struct CoreTraits
;
70 void ConnectOnIO(const GURL
& url
,
72 base::WaitableEvent
* event
);
73 void OnConnectCompletedOnIO(bool* connected
,
74 base::WaitableEvent
* event
,
76 void SendOnIO(const std::string
& message
,
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_
;
90 // Protected by |lock_|.
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
) {
110 #endif // CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_IMPL_H_