| OLD | NEW |
| (Empty) |
| 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 #include "mojo/application/application_runner_chromium.h" | |
| 6 #include "mojo/common/weak_binding_set.h" | |
| 7 #include "mojo/common/weak_interface_ptr_set.h" | |
| 8 #include "mojo/public/c/system/main.h" | |
| 9 #include "mojo/public/cpp/application/application_delegate.h" | |
| 10 #include "mojo/public/cpp/application/application_impl.h" | |
| 11 #include "mojo/public/cpp/bindings/binding.h" | |
| 12 #include "net/server/http_server.h" | |
| 13 #include "net/socket/tcp_server_socket.h" | |
| 14 #include "sky/services/inspector/inspector.mojom.h" | |
| 15 | |
| 16 | |
| 17 namespace sky { | |
| 18 namespace inspector { | |
| 19 | |
| 20 namespace { | |
| 21 const int kNotConnected = -1; | |
| 22 } | |
| 23 | |
| 24 class Server : public mojo::ApplicationDelegate, | |
| 25 public InspectorFrontend, | |
| 26 public InspectorServer, | |
| 27 public mojo::InterfaceFactory<InspectorFrontend>, | |
| 28 public mojo::InterfaceFactory<InspectorServer>, | |
| 29 public net::HttpServer::Delegate { | |
| 30 public: | |
| 31 Server() : connection_id_(kNotConnected), server_binding_(this) {} | |
| 32 virtual ~Server(); | |
| 33 | |
| 34 private: | |
| 35 // mojo::ApplicationDelegate: | |
| 36 void Initialize(mojo::ApplicationImpl* app) override { | |
| 37 } | |
| 38 bool ConfigureIncomingConnection( | |
| 39 mojo::ApplicationConnection* connection) override { | |
| 40 connection->AddService<InspectorFrontend>(this); | |
| 41 connection->AddService<InspectorServer>(this); | |
| 42 if (connection->GetServiceProvider()) { | |
| 43 // The application connecting to us may implement InspectorBackend, | |
| 44 // attempt to establish a connection to find out. If it doesn't then this | |
| 45 // pipe will close. | |
| 46 InspectorBackendPtr backend; | |
| 47 connection->ConnectToService(&backend); | |
| 48 backends_.AddInterfacePtr(backend.Pass()); | |
| 49 } | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 // InterfaceFactory<InspectorFrontend>: | |
| 54 void Create(mojo::ApplicationConnection* connection, | |
| 55 mojo::InterfaceRequest<InspectorFrontend> request) override { | |
| 56 frontend_bindings_.AddBinding(this, request.Pass()); | |
| 57 } | |
| 58 | |
| 59 // InterfaceFactory<InspectorServer>: | |
| 60 void Create(mojo::ApplicationConnection* connection, | |
| 61 mojo::InterfaceRequest<InspectorServer> request) override { | |
| 62 server_binding_.Bind(request.PassMessagePipe()); | |
| 63 } | |
| 64 | |
| 65 // InspectorServer: | |
| 66 void Listen(int32_t port, const mojo::Closure& callback) override; | |
| 67 | |
| 68 // InspectorFrontend: | |
| 69 void SendMessage(const mojo::String& message) override; | |
| 70 | |
| 71 // net::HttpServer::Delegate: | |
| 72 void OnConnect(int connection_id) override; | |
| 73 void OnHttpRequest( | |
| 74 int connection_id, const net::HttpServerRequestInfo& info) override; | |
| 75 void OnWebSocketRequest( | |
| 76 int connection_id, const net::HttpServerRequestInfo& info) override; | |
| 77 void OnWebSocketMessage( | |
| 78 int connection_id, const std::string& data) override; | |
| 79 void OnClose(int connection_id) override; | |
| 80 | |
| 81 int connection_id_; | |
| 82 scoped_ptr<net::HttpServer> web_server_; | |
| 83 | |
| 84 mojo::WeakInterfacePtrSet<InspectorBackend> backends_; | |
| 85 mojo::WeakBindingSet<InspectorFrontend> frontend_bindings_; | |
| 86 mojo::Binding<InspectorServer> server_binding_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(Server); | |
| 89 }; | |
| 90 | |
| 91 Server::~Server() | |
| 92 { | |
| 93 } | |
| 94 | |
| 95 void Server::OnConnect(int connection_id) { | |
| 96 } | |
| 97 | |
| 98 void Server::OnHttpRequest( | |
| 99 int connection_id, const net::HttpServerRequestInfo& info) { | |
| 100 web_server_->Send500(connection_id, "websockets protocol only"); | |
| 101 } | |
| 102 | |
| 103 void Server::OnWebSocketRequest( | |
| 104 int connection_id, const net::HttpServerRequestInfo& info) { | |
| 105 if (connection_id_ != kNotConnected) { | |
| 106 web_server_->Close(connection_id); | |
| 107 return; | |
| 108 } | |
| 109 web_server_->AcceptWebSocket(connection_id, info); | |
| 110 connection_id_ = connection_id; | |
| 111 backends_.ForAllPtrs([](InspectorBackend* backend) { backend->OnConnect(); }); | |
| 112 } | |
| 113 | |
| 114 void Server::OnWebSocketMessage( | |
| 115 int connection_id, const std::string& data) { | |
| 116 DCHECK_EQ(connection_id, connection_id_); | |
| 117 backends_.ForAllPtrs( | |
| 118 [data](InspectorBackend* backend) { backend->OnMessage(data); }); | |
| 119 } | |
| 120 | |
| 121 void Server::OnClose(int connection_id) { | |
| 122 if (connection_id != connection_id_) | |
| 123 return; | |
| 124 connection_id_ = kNotConnected; | |
| 125 backends_.ForAllPtrs( | |
| 126 [](InspectorBackend* backend) { backend->OnDisconnect(); }); | |
| 127 } | |
| 128 | |
| 129 void Server::Listen(int32_t port, const mojo::Closure& callback) { | |
| 130 backends_.CloseAll(); // Assume caller represents a new app. | |
| 131 | |
| 132 // TODO(eseidel): Early-out here if we're already bound to the right port. | |
| 133 web_server_.reset(); | |
| 134 scoped_ptr<net::ServerSocket> server_socket( | |
| 135 new net::TCPServerSocket(NULL, net::NetLog::Source())); | |
| 136 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); | |
| 137 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); | |
| 138 callback.Run(); | |
| 139 } | |
| 140 | |
| 141 void Server::SendMessage(const mojo::String& message) { | |
| 142 if (connection_id_ == kNotConnected) | |
| 143 return; | |
| 144 web_server_->SendOverWebSocket(connection_id_, message); | |
| 145 } | |
| 146 | |
| 147 } // namespace inspector | |
| 148 } // namespace sky | |
| 149 | |
| 150 MojoResult MojoMain(MojoHandle shell_handle) { | |
| 151 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); | |
| 152 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | |
| 153 return runner.Run(shell_handle); | |
| 154 } | |
| OLD | NEW |