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. 4 5#ifndef MOJO_SPY_WEBSOCKET_SERVER_H_ 6#define MOJO_SPY_WEBSOCKET_SERVER_H_ 7 8#include <string> 9 10#include "mojo/spy/common.h" 11#include "mojo/spy/public/spy.mojom.h" 12#include "net/server/http_server.h" 13 14namespace base { 15class Time; 16}; 17 18class GURL; 19 20namespace mojo { 21 22class WebSocketServer : public net::HttpServer::Delegate, 23 public spy_api::SpyClient { 24 public: 25 // Pass 0 in |port| to listen in one available port. 26 explicit WebSocketServer(int port, ScopedMessagePipeHandle server_pipe); 27 virtual ~WebSocketServer(); 28 // Begin accepting HTTP requests. Must be called from an IO MessageLoop. 29 bool Start(); 30 // Returns the listening port, useful if 0 was passed to the contructor. 31 int port() const { return port_; } 32 33 // Maintains a log of the message passed in. 34 void LogMessageInfo( 35 const mojo::MojoRequestHeader& message_header, 36 const GURL& url, 37 const base::Time& message_time); 38 39 protected: 40 // Overridden from net::HttpServer::Delegate. 41 virtual void OnConnect(int connection_id) OVERRIDE {} 42 virtual void OnHttpRequest( 43 int connection_id, 44 const net::HttpServerRequestInfo& info) OVERRIDE; 45 virtual void OnWebSocketRequest( 46 int connection_id, 47 const net::HttpServerRequestInfo& info) OVERRIDE; 48 virtual void OnWebSocketMessage( 49 int connection_id, 50 const std::string& data) OVERRIDE; 51 virtual void OnClose(int connection_id) OVERRIDE; 52 53 // Overriden form spy_api::SpyClient. 54 virtual void OnFatalError(spy_api::Result result) OVERRIDE; 55 virtual void OnSessionEnd(spy_api::Result result) OVERRIDE; 56 virtual void OnClientConnection( 57 const mojo::String& name, 58 uint32_t id, 59 spy_api::ConnectionOptions options) OVERRIDE; 60 virtual void OnMessage(spy_api::MessagePtr message) OVERRIDE; 61 62 // Callbacks from calling spy_api::SpyServer. 63 void OnStartSession(spy_api::Result, mojo::String); 64 65 bool Connected() const; 66 67 private: 68 int port_; 69 int connection_id_; 70 scoped_ptr<net::HttpServer> web_server_; 71 spy_api::SpyServerPtr spy_server_; 72 73 DISALLOW_COPY_AND_ASSIGN(WebSocketServer); 74}; 75 76} // namespace mojo 77 78#endif // MOJO_SPY_WEBSOCKET_SERVER_H_ 79