1 // Copyright 2014 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 MOJO_SPY_WEBSOCKET_SERVER_H_
6 #define MOJO_SPY_WEBSOCKET_SERVER_H_
10 #include "base/macros.h"
11 #include "mojo/spy/common.h"
12 #include "mojo/spy/public/spy.mojom.h"
13 #include "net/server/http_server.h"
23 class WebSocketServer
: public net::HttpServer::Delegate
,
24 public spy_api::SpyClient
{
26 // Pass 0 in |port| to listen in one available port.
27 explicit WebSocketServer(int port
, ScopedMessagePipeHandle server_pipe
);
28 ~WebSocketServer() override
;
29 // Begin accepting HTTP requests. Must be called from an IO MessageLoop.
31 // Returns the listening port, useful if 0 was passed to the contructor.
32 int port() const { return port_
; }
34 // Maintains a log of the message passed in.
36 const mojo::MojoRequestHeader
& message_header
,
38 const base::Time
& message_time
);
41 // Overridden from net::HttpServer::Delegate.
42 void OnConnect(int connection_id
) override
{}
43 void OnHttpRequest(int connection_id
,
44 const net::HttpServerRequestInfo
& info
) override
;
45 void OnWebSocketRequest(int connection_id
,
46 const net::HttpServerRequestInfo
& info
) override
;
47 void OnWebSocketMessage(int connection_id
, const std::string
& data
) override
;
48 void OnClose(int connection_id
) override
;
50 // Overriden form spy_api::SpyClient.
51 void OnFatalError(spy_api::Result result
) override
;
52 void OnSessionEnd(spy_api::Result result
) override
;
53 void OnClientConnection(const mojo::String
& name
,
55 spy_api::ConnectionOptions options
) override
;
56 void OnMessage(spy_api::MessagePtr message
) override
;
58 // Callbacks from calling spy_api::SpyServer.
59 void OnStartSession(spy_api::Result
, mojo::String
);
61 bool Connected() const;
66 scoped_ptr
<net::HttpServer
> web_server_
;
67 spy_api::SpyServerPtr spy_server_
;
69 DISALLOW_COPY_AND_ASSIGN(WebSocketServer
);
74 #endif // MOJO_SPY_WEBSOCKET_SERVER_H_