summaryrefslogtreecommitdiffstats
path: root/chrome/test/chromedriver/net/sync_websocket.h
blob: ad5cf5de0181cb65fcc8a8d5f23b2b35216be1ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_H_
#define CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_H_

#include <list>
#include <string>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "chrome/test/chromedriver/net/websocket.h"
#include "net/base/completion_callback.h"
#include "net/socket_stream/socket_stream.h"

namespace base {
class WaitableEvent;
}

namespace net {
class URLRequestContextGetter;
}

class GURL;

// Proxy for using a WebSocket running on a background thread synchronously.
class SyncWebSocket {
 public:
  explicit SyncWebSocket(net::URLRequestContextGetter* context_getter);
  virtual ~SyncWebSocket();

  // Connects to the WebSocket server. Returns true on success.
  bool Connect(const GURL& url);

  // Sends message. Returns true on success.
  bool Send(const std::string& message);

  // Receives next message. Blocks until at least one message is received or
  // the socket is closed. Returns true on success and modifies |message|.
  bool ReceiveNextMessage(std::string* message);

 private:
  struct CoreTraits;
  class Core : public WebSocketListener,
               public base::RefCountedThreadSafe<Core, CoreTraits> {
   public:
    explicit Core(net::URLRequestContextGetter* context_getter);

    bool Connect(const GURL& url);

    bool Send(const std::string& message);

    bool ReceiveNextMessage(std::string* message);

    // Overriden from WebSocketListener:
    virtual void OnMessageReceived(const std::string& message) OVERRIDE;
    virtual void OnClose() OVERRIDE;

   private:
    friend class base::RefCountedThreadSafe<Core, CoreTraits>;
    friend class base::DeleteHelper<Core>;
    friend struct CoreTraits;

    virtual ~Core();

    void ConnectOnIO(const GURL& url,
                     bool* success,
                     base::WaitableEvent* event);
    void OnConnectCompletedOnIO(bool* connected,
                                base::WaitableEvent* event,
                                int error);
    void SendOnIO(const std::string& message,
                  bool* result,
                  base::WaitableEvent* event);

    // OnDestruct is meant to ensure deletion on the IO thread.
    void OnDestruct() const;

    scoped_refptr<net::URLRequestContextGetter> context_getter_;

    // Only accessed on IO thread.
    scoped_ptr<WebSocket> socket_;

    base::Lock lock_;

    // Protected by |lock_|.
    bool closed_;

    // Protected by |lock_|.
    std::list<std::string> received_queue_;

    // Protected by |lock_|.
    // Signaled when the socket closes or a message is received.
    base::ConditionVariable on_update_event_;
  };

  scoped_refptr<Core> core_;
};

struct SyncWebSocket::CoreTraits {
  static void Destruct(const SyncWebSocket::Core* core) {
    core->OnDestruct();
  }
};

#endif  // CHROME_TEST_CHROMEDRIVER_NET_SYNC_WEBSOCKET_H_