Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(477)

Unified Diff: sky/services/inspector/server.cc

Issue 710043004: Make it possible to have multiple InspectorBackends (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Split out ServerImpl, somehow breaks JS connections... Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sky/services/inspector/server.cc
diff --git a/sky/services/inspector/server.cc b/sky/services/inspector/server.cc
index d052cc257b01185058697692dd63ab94c2dfa6b9..bbb6c959a0b2680bd27da88c656f42e22a108a54 100644
--- a/sky/services/inspector/server.cc
+++ b/sky/services/inspector/server.cc
@@ -2,36 +2,193 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/lazy_instance.h"
#include "mojo/application/application_runner_chromium.h"
#include "mojo/public/c/system/main.h"
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/application_impl.h"
+#include "net/server/http_server.h"
+#include "net/socket/tcp_server_socket.h"
#include "sky/services/inspector/inspector_frontend_impl.h"
namespace sky {
namespace inspector {
-class Server : public mojo::ApplicationDelegate {
+namespace {
+const int kNotConnected = -1;
+}
+
+class ServerImpl : public mojo::InterfaceImpl<InspectorServer> {
+public:
+ class Delegate {
+ public:
+ virtual void SetImpl(ServerImpl* impl) = 0;
+ virtual void Listen(int32_t port) = 0;
+ };
+ ServerImpl(Delegate* delegate) : delegate_(delegate) {
+ delegate_->SetImpl(this);
Aaron Boodman 2014/11/11 16:12:50 If two InspectorServer interfaces are requested, t
eseidel 2014/11/11 18:15:26 Done.
+ }
+ ~ServerImpl() {
+ delegate_->SetImpl(nullptr);
+ }
+ void OnShutdown() {
+ delete this;
+ }
+
+ // InspectorServer:
+ void Listen(int32_t port, const mojo::Callback<void()>& callback) override;
+
+private:
+ // InterfaceImpl:
+ void OnConnectionError() override {
+ delete this;
Aaron Boodman 2014/11/11 16:12:50 If you are just going to delete yourself on error,
eseidel 2014/11/11 18:15:26 I can't, unfortunately, since then I can't hae the
+ }
+
+ Delegate* delegate_;
+};
+
+class Server : public mojo::ApplicationDelegate,
+ public InspectorFrontendImpl::Delegate,
+ public ServerImpl::Delegate,
+ public mojo::InterfaceFactory<InspectorFrontendImpl>,
+ public mojo::InterfaceFactory<ServerImpl>,
+ public net::HttpServer::Delegate {
public:
- Server() {}
- virtual ~Server() {}
+ Server() : connection_id_(kNotConnected) {}
+ virtual ~Server();
private:
- // Overridden from mojo::ApplicationDelegate:
- virtual void Initialize(mojo::ApplicationImpl* app) override {
+ // mojo::ApplicationDelegate:
+ void Initialize(mojo::ApplicationImpl* app) override {
}
-
- virtual bool ConfigureIncomingConnection(
+ bool ConfigureIncomingConnection(
mojo::ApplicationConnection* connection) override {
- connection->AddService(&frontend_factory_);
+ connection->AddService(this);
return true;
}
- InspectorFrontendFactory frontend_factory_;
+ // InterfaceFactory<InspectorFrontendImpl>:
+ void Create(mojo::ApplicationConnection* connection,
+ mojo::InterfaceRequest<InspectorFrontendImpl> request) override {
+ printf("Create InspectorFrontendImpl\n");
+ WeakBindToRequest(new InspectorFrontendImpl(this), &request);
+ }
+
+ // InterfaceFactory<InspectorServer>:
+ void Create(mojo::ApplicationConnection* connection,
+ mojo::InterfaceRequest<ServerImpl> request) override {
+ printf("Create ServerImpl\n");
+ WeakBindToRequest(new ServerImpl(this), &request);
+ }
+
+ // ServerImpl::Delegate:
+ void SetImpl(ServerImpl* server_impl) override {
+ server_impl_ = server_impl;
+ }
+ void Listen(int32_t port) override;
+
+ // InspectorFrontendImpl::Delegate:
+ void Register(InspectorFrontendImpl*) override;
+ void Unregister(InspectorFrontendImpl*) override;
+
+ void SendMessage(const mojo::String& message) override;
+
+ // 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;
+
+ void CloseAllAgentConnections();
+
+ int connection_id_;
+ scoped_ptr<net::HttpServer> web_server_;
+ // TODO(eseidel): The server should control the lifetime of these agents.
+ ObserverList<InspectorFrontendImpl> agents_;
+ ServerImpl* server_impl_;
DISALLOW_COPY_AND_ASSIGN(Server);
};
+Server::~Server()
+{
+ CloseAllAgentConnections();
+}
+
+void Server::CloseAllAgentConnections() {
+ FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, OnShutdown());
+ server_impl_->OnShutdown();
+}
+
+void Server::OnConnect(int connection_id) {
+}
+
+void Server::OnHttpRequest(
+ int connection_id, const net::HttpServerRequestInfo& info) {
+ web_server_->Send500(connection_id, "websockets protocol only");
+}
+
+void Server::OnWebSocketRequest(
+ int connection_id, const net::HttpServerRequestInfo& info) {
+ if (connection_id_ != kNotConnected) {
+ web_server_->Close(connection_id);
+ return;
+ }
+ web_server_->AcceptWebSocket(connection_id, info);
+ connection_id_ = connection_id;
+ FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnConnect());
+}
+
+void Server::OnWebSocketMessage(
+ int connection_id, const std::string& data) {
+ DCHECK_EQ(connection_id, connection_id_);
+ printf("OnWebSocketMessage\n");
+ FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnMessage(data));
+}
+
+void Server::OnClose(int connection_id) {
+ if (connection_id != connection_id_)
+ return;
+ connection_id_ = kNotConnected;
+ FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnDisconnect());
+}
+
+void Server::Register(InspectorFrontendImpl* agent) {
+ printf("Server::Register\n");
+ agents_.AddObserver(agent);
+}
+
+void Server::Unregister(InspectorFrontendImpl* agent) {
+ printf("Server::Unregister\n");
+ agents_.RemoveObserver(agent);
+}
+
+void ServerImpl::Listen(int32_t port, const mojo::Callback<void()>& callback) {
+ delegate_->Listen(port);
+ callback.Run();
+}
+
+void Server::Listen(int32_t port) {
+ CloseAllAgentConnections(); // Assume caller represents a new app.
+
+ // TODO(eseidel): Early-out here if we're already bound to the right port.
+ web_server_.reset();
+ scoped_ptr<net::ServerSocket> server_socket(
+ new net::TCPServerSocket(NULL, net::NetLog::Source()));
+ server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1);
+ web_server_.reset(new net::HttpServer(server_socket.Pass(), this));
+}
+
+void Server::SendMessage(const mojo::String& message) {
+ if (connection_id_ == kNotConnected)
+ return;
+ web_server_->SendOverWebSocket(connection_id_, message);
+}
+
} // namespace inspector
} // namespace sky

Powered by Google App Engine
This is Rietveld 408576698