blob: 879682d44481d1e1e4b179b37d582016e8665302 [file] [log] [blame]
// 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);
~WebSocketServer() override;
// 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.
void OnConnect(int connection_id) override {}
void OnHttpRequest(int connection_id,
const net::HttpServerRequestInfo& info) override;
void OnWebSocketRequest(int connection_id,
const net::HttpServerRequestInfo& info) override;
void OnWebSocketMessage(int connection_id, const std::string& data) override;
void OnClose(int connection_id) override;
// Overriden form spy_api::SpyClient.
void OnFatalError(spy_api::Result result) override;
void OnSessionEnd(spy_api::Result result) override;
void OnClientConnection(const mojo::String& name,
uint32_t id,
spy_api::ConnectionOptions options) override;
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_