From f0c80dc8ccd84449c0196f761c61622652ee3149 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 28 Oct 2014 10:46:47 -0700 Subject: [PATCH] Add beginnings of an inspector backend for Sky R=eseidel@chromium.org Review URL: https://codereview.chromium.org/687673003 --- BUILD.gn | 1 + framework/inspector/console-agent.sky | 12 ++ framework/inspector/dom-agent.sky | 169 ++++++++++++++++++ framework/inspector/inspector.sky | 83 +++++++++ framework/inspector/page-agent.sky | 21 +++ framework/inspector/server/BUILD.gn | 37 ++++ framework/inspector/server/inspector.mojom | 20 +++ .../server/inspector_frontend_impl.cc | 97 ++++++++++ .../server/inspector_frontend_impl.h | 56 ++++++ framework/inspector/server/server.cc | 42 +++++ framework/inspector/worker-agent.sky | 12 ++ 11 files changed, 550 insertions(+) create mode 100644 framework/inspector/console-agent.sky create mode 100644 framework/inspector/dom-agent.sky create mode 100644 framework/inspector/inspector.sky create mode 100644 framework/inspector/page-agent.sky create mode 100644 framework/inspector/server/BUILD.gn create mode 100644 framework/inspector/server/inspector.mojom create mode 100644 framework/inspector/server/inspector_frontend_impl.cc create mode 100644 framework/inspector/server/inspector_frontend_impl.h create mode 100644 framework/inspector/server/server.cc create mode 100644 framework/inspector/worker-agent.sky diff --git a/BUILD.gn b/BUILD.gn index 4fcf80401..08240cc12 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -13,6 +13,7 @@ group("sky") { "//sky/engine/platform:platform_unittests", "//sky/engine/web:sky_unittests", "//sky/engine/wtf:unittests", + "//sky/framework/inspector/server", "//sky/viewer", ] if (use_aura) { diff --git a/framework/inspector/console-agent.sky b/framework/inspector/console-agent.sky new file mode 100644 index 000000000..9ce5b1795 --- /dev/null +++ b/framework/inspector/console-agent.sky @@ -0,0 +1,12 @@ + diff --git a/framework/inspector/dom-agent.sky b/framework/inspector/dom-agent.sky new file mode 100644 index 000000000..6e6048fda --- /dev/null +++ b/framework/inspector/dom-agent.sky @@ -0,0 +1,169 @@ + diff --git a/framework/inspector/inspector.sky b/framework/inspector/inspector.sky new file mode 100644 index 000000000..80b285f09 --- /dev/null +++ b/framework/inspector/inspector.sky @@ -0,0 +1,83 @@ + + + + + + + + + diff --git a/framework/inspector/page-agent.sky b/framework/inspector/page-agent.sky new file mode 100644 index 000000000..053954caa --- /dev/null +++ b/framework/inspector/page-agent.sky @@ -0,0 +1,21 @@ + diff --git a/framework/inspector/server/BUILD.gn b/framework/inspector/server/BUILD.gn new file mode 100644 index 000000000..d9a06a2b1 --- /dev/null +++ b/framework/inspector/server/BUILD.gn @@ -0,0 +1,37 @@ +# 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. + +import("//mojo/public/tools/bindings/mojom.gni") + +group("server") { + testonly = true + + deps = [ + ":sky_inspector_server", + ] +} + +shared_library("sky_inspector_server") { + sources = [ + "inspector_frontend_impl.cc", + "inspector_frontend_impl.h", + "server.cc", + ] + + deps = [ + "//base", + "//mojo/application", + "//mojo/public/c/system:for_shared_library", + "//mojo/public/cpp/bindings", + "//mojo/public/cpp/utility", + "//net:http_server", + ":bindings", + ] +} + +mojom("bindings") { + sources = [ + "inspector.mojom", + ] +} diff --git a/framework/inspector/server/inspector.mojom b/framework/inspector/server/inspector.mojom new file mode 100644 index 000000000..69974084d --- /dev/null +++ b/framework/inspector/server/inspector.mojom @@ -0,0 +1,20 @@ +// 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. + +module sky { + +[Client=InspectorBackend] +interface InspectorFrontend { + Listen(int32 port); + SendMessage(string message); +}; + +[Client=InspectorFrontend] +interface InspectorBackend { + OnConnect(); + OnDisconnect(); + OnMessage(string message); +}; + +} diff --git a/framework/inspector/server/inspector_frontend_impl.cc b/framework/inspector/server/inspector_frontend_impl.cc new file mode 100644 index 000000000..0865e91b9 --- /dev/null +++ b/framework/inspector/server/inspector_frontend_impl.cc @@ -0,0 +1,97 @@ +// 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. + +#include "sky/framework/inspector/server/inspector_frontend_impl.h" + +#include "base/lazy_instance.h" +#include "net/server/http_server.h" +#include "net/socket/tcp_server_socket.h" + +namespace sky { +namespace inspector { +namespace { +const int kNotConnected = -1; +static base::LazyInstance> g_servers = + LAZY_INSTANCE_INITIALIZER; +} + +InspectorFronendImpl::InspectorFronendImpl() + : connection_id_(kNotConnected) { +} + +InspectorFronendImpl::~InspectorFronendImpl() { + StopListening(); +} + +void InspectorFronendImpl::OnConnect(int connection_id) { +} + +void InspectorFronendImpl::OnHttpRequest( + int connection_id, const net::HttpServerRequestInfo& info) { + web_server_->Send500(connection_id, "websockets protocol only"); +} + +void InspectorFronendImpl::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; + client()->OnConnect(); +} + +void InspectorFronendImpl::OnWebSocketMessage( + int connection_id, const std::string& data) { + DCHECK_EQ(connection_id, connection_id_); + client()->OnMessage(data); +} + +void InspectorFronendImpl::OnClose(int connection_id) { + if (connection_id != connection_id_) + return; + connection_id_ = kNotConnected; + client()->OnDisconnect(); +} + +void InspectorFronendImpl::Listen(int32_t port) { + Register(port); + scoped_ptr 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 InspectorFronendImpl::StopListening() { + if (!web_server_) + return; + web_server_.reset(); + Unregister(); +} + +void InspectorFronendImpl::Register(int port) { + auto& servers = g_servers.Get(); + auto iter = servers.find(port); + if (iter != servers.end()) + iter->second->StopListening(); + DCHECK(servers.find(port) == servers.end()); + servers[port] = this; + port_ = port; +} + +void InspectorFronendImpl::Unregister() { + DCHECK(g_servers.Get().find(port_)->second == this); + g_servers.Get().erase(port_); + port_ = kNotConnected; +} + +void InspectorFronendImpl::SendMessage(const mojo::String& message) { + if (connection_id_ == kNotConnected) + return; + web_server_->SendOverWebSocket(connection_id_, message); +} + +} // namespace inspector +} // namespace sky diff --git a/framework/inspector/server/inspector_frontend_impl.h b/framework/inspector/server/inspector_frontend_impl.h new file mode 100644 index 000000000..cd9e726fd --- /dev/null +++ b/framework/inspector/server/inspector_frontend_impl.h @@ -0,0 +1,56 @@ +// 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 SKY_FRAMEWORK_INSPECTOR_SERVER_INSPECTOR_FRONTEND_IMPL_H_ +#define SKY_FRAMEWORK_INSPECTOR_SERVER_INSPECTOR_FRONTEND_IMPL_H_ + +#include "mojo/public/cpp/application/interface_factory_impl.h" +#include "mojo/public/cpp/bindings/interface_impl.h" +#include "net/server/http_server.h" +#include "net/socket/tcp_server_socket.h" +#include "sky/framework/inspector/server/inspector.mojom.h" + +namespace sky { +namespace inspector { + +class InspectorFronendImpl : public mojo::InterfaceImpl, + public net::HttpServer::Delegate { + public: + InspectorFronendImpl(); + virtual ~InspectorFronendImpl(); + + private: + // 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; + + // From InspectorFronend + virtual void Listen(int32_t port) override; + virtual void SendMessage(const mojo::String&) override; + + void StopListening(); + + void Register(int port); + void Unregister(); + + int port_; + int connection_id_; + scoped_ptr web_server_; + + MOJO_DISALLOW_COPY_AND_ASSIGN(InspectorFronendImpl); +}; + +typedef mojo::InterfaceFactoryImpl< + InspectorFronendImpl> InspectorFronendFactory; + +} // namespace tester +} // namespace sky + +#endif // SKY_FRAMEWORK_INSPECTOR_SERVER_INSPECTOR_FRONTEND_IMPL_H_ diff --git a/framework/inspector/server/server.cc b/framework/inspector/server/server.cc new file mode 100644 index 000000000..9f078d940 --- /dev/null +++ b/framework/inspector/server/server.cc @@ -0,0 +1,42 @@ +// 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. + +#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 "sky/framework/inspector/server/inspector_frontend_impl.h" + +namespace sky { +namespace inspector { + +class Server : public mojo::ApplicationDelegate { + public: + Server() {} + virtual ~Server() {} + + private: + // Overridden from mojo::ApplicationDelegate: + virtual void Initialize(mojo::ApplicationImpl* app) override { + } + + virtual bool ConfigureIncomingConnection( + mojo::ApplicationConnection* connection) override { + connection->AddService(&fronend_factory_); + return true; + } + + InspectorFronendFactory fronend_factory_; + + DISALLOW_COPY_AND_ASSIGN(Server); +}; + +} // namespace inspector +} // namespace sky + +MojoResult MojoMain(MojoHandle shell_handle) { + mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); + runner.set_message_loop_type(base::MessageLoop::TYPE_IO); + return runner.Run(shell_handle); +} diff --git a/framework/inspector/worker-agent.sky b/framework/inspector/worker-agent.sky new file mode 100644 index 000000000..addbc2c04 --- /dev/null +++ b/framework/inspector/worker-agent.sky @@ -0,0 +1,12 @@ + -- GitLab