android_webview: explicitly use gcc for target binaries.
[chromium-blink-merge.git] / chrome / test / chromedriver / net / sync_websocket_impl.h
blob4a42471789a85be0eecb11ab23780403992920a9
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"
20 #include "net/socket_stream/socket_stream.h"
22 namespace base {
23 class WaitableEvent;
26 namespace net {
27 class URLRequestContextGetter;
30 class GURL;
32 class SyncWebSocketImpl : public SyncWebSocket {
33 public:
34 explicit SyncWebSocketImpl(net::URLRequestContextGetter* context_getter);
35 virtual ~SyncWebSocketImpl();
37 // Overridden from SyncWebSocket:
38 virtual bool IsConnected() override;
39 virtual bool Connect(const GURL& url) override;
40 virtual bool Send(const std::string& message) override;
41 virtual StatusCode ReceiveNextMessage(
42 std::string* message,
43 const base::TimeDelta& timeout) override;
44 virtual bool HasNextMessage() override;
46 private:
47 struct CoreTraits;
48 class Core : public WebSocketListener,
49 public base::RefCountedThreadSafe<Core, CoreTraits> {
50 public:
51 explicit Core(net::URLRequestContextGetter* context_getter);
53 bool IsConnected();
54 bool Connect(const GURL& url);
55 bool Send(const std::string& message);
56 SyncWebSocket::StatusCode ReceiveNextMessage(
57 std::string* message,
58 const base::TimeDelta& timeout);
59 bool HasNextMessage();
61 // Overriden from WebSocketListener:
62 virtual void OnMessageReceived(const std::string& message) override;
63 virtual void OnClose() override;
65 private:
66 friend class base::RefCountedThreadSafe<Core, CoreTraits>;
67 friend class base::DeleteHelper<Core>;
68 friend struct CoreTraits;
70 virtual ~Core();
72 void ConnectOnIO(const GURL& url,
73 bool* success,
74 base::WaitableEvent* event);
75 void OnConnectCompletedOnIO(bool* connected,
76 base::WaitableEvent* event,
77 int error);
78 void SendOnIO(const std::string& message,
79 bool* result,
80 base::WaitableEvent* event);
82 // OnDestruct is meant to ensure deletion on the IO thread.
83 void OnDestruct() const;
85 scoped_refptr<net::URLRequestContextGetter> context_getter_;
87 // Only accessed on IO thread.
88 scoped_ptr<WebSocket> socket_;
90 base::Lock lock_;
92 // Protected by |lock_|.
93 bool is_connected_;
95 // Protected by |lock_|.
96 std::list<std::string> received_queue_;
98 // Protected by |lock_|.
99 // Signaled when the socket closes or a message is received.
100 base::ConditionVariable on_update_event_;
103 scoped_refptr<Core> core_;
106 struct SyncWebSocketImpl::CoreTraits {
107 static void Destruct(const SyncWebSocketImpl::Core* core) {
108 core->OnDestruct();
112 #endif // CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_IMPL_H_