summaryrefslogtreecommitdiffstats
path: root/mojo/spy/websocket_server.h
blob: c6dc07696828892d9569b8e1dc8cf8b45ae03849 (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
// Copyright 2014 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 MOJO_SPY_WEBSOCKET_SERVER_H_
#define MOJO_SPY_WEBSOCKET_SERVER_H_

#include <string>

#include "base/macros.h"
#include "mojo/spy/common.h"
#include "mojo/spy/public/spy.mojom.h"
#include "net/server/http_server.h"

namespace base {
class Time;
};

class GURL;

namespace mojo {

class WebSocketServer : public net::HttpServer::Delegate,
                        public spy_api::SpyClient {
 public:
  // Pass 0 in |port| to listen in one available port.
  explicit WebSocketServer(int port, ScopedMessagePipeHandle server_pipe);
  virtual ~WebSocketServer();
  // Begin accepting HTTP requests. Must be called from an IO MessageLoop.
  bool Start();
  // Returns the listening port, useful if 0 was passed to the contructor.
  int port() const { return port_; }

  // Maintains a log of the message passed in.
  void LogMessageInfo(
      const mojo::MojoRequestHeader& message_header,
      const GURL& url,
      const base::Time& message_time);

 protected:
  // Overridden from net::HttpServer::Delegate.
  virtual void OnConnect(int connection_id) override {}
  virtual void OnHttpRequest(
      int connection_id,
      const net::HttpServerRequestInfo& info) override;
  virtual void OnWebSocketRequest(
      int connection_id,
      const net::HttpServerRequestInfo& info) override;
  virtual void OnWebSocketMessage(
      int connection_id,
      const std::string& data) override;
  virtual void OnClose(int connection_id) override;

  // Overriden form spy_api::SpyClient.
  virtual void OnFatalError(spy_api::Result result) override;
  virtual void OnSessionEnd(spy_api::Result result) override;
  virtual void OnClientConnection(
      const mojo::String& name,
      uint32_t id,
      spy_api::ConnectionOptions options) override;
  virtual void OnMessage(spy_api::MessagePtr message) override;

  // Callbacks from calling spy_api::SpyServer.
  void OnStartSession(spy_api::Result, mojo::String);

  bool Connected() const;

 private:
  int port_;
  int connection_id_;
  scoped_ptr<net::HttpServer> web_server_;
  spy_api::SpyServerPtr spy_server_;

  DISALLOW_COPY_AND_ASSIGN(WebSocketServer);
};

}  // namespace mojo

#endif  // MOJO_SPY_WEBSOCKET_SERVER_H_