| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/application/application_runner_chromium.h" | 5 #include "mojo/application/application_runner_chromium.h" |
| 6 #include "mojo/public/c/system/main.h" | 6 #include "mojo/public/c/system/main.h" |
| 7 #include "mojo/public/cpp/application/application_delegate.h" | 7 #include "mojo/public/cpp/application/application_delegate.h" |
| 8 #include "mojo/public/cpp/application/application_impl.h" | 8 #include "mojo/public/cpp/application/application_impl.h" |
| 9 #include "sky/services/inspector/inspector_frontend_impl.h" | 9 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 10 #include "mojo/public/cpp/bindings/interface_impl.h" |
| 11 #include "net/server/http_server.h" |
| 12 #include "net/socket/tcp_server_socket.h" |
| 13 #include "sky/services/inspector/inspector.mojom.h" |
| 14 |
| 10 | 15 |
| 11 namespace sky { | 16 namespace sky { |
| 12 namespace inspector { | 17 namespace inspector { |
| 13 | 18 |
| 14 class Server : public mojo::ApplicationDelegate { | 19 // TODO(eseidel): None of this Impl nonsense is necessary: crbug.com/431963 |
| 20 class InspectorServerImpl : public mojo::InterfaceImpl<InspectorServer> { |
| 21 public: |
| 22 class Delegate { |
| 23 public: |
| 24 virtual void Register(InspectorServerImpl* impl) = 0; |
| 25 virtual void Unregister(InspectorServerImpl* impl) = 0; |
| 26 virtual void Listen(int32_t port) = 0; |
| 27 }; |
| 28 InspectorServerImpl(Delegate* delegate) : delegate_(delegate) { |
| 29 delegate_->Register(this); |
| 30 } |
| 31 virtual ~InspectorServerImpl() { |
| 32 delegate_->Unregister(this); |
| 33 } |
| 34 |
| 35 // InspectorServer: |
| 36 void Listen(int32_t port, const mojo::Callback<void()>& callback) override { |
| 37 delegate_->Listen(port); |
| 38 callback.Run(); |
| 39 } |
| 40 |
| 41 void OnShutdown() { |
| 42 delete this; |
| 43 } |
| 44 |
| 45 private: |
| 46 // InterfaceImpl: |
| 47 void OnConnectionError() override { |
| 48 delete this; // crbug.com/431911 |
| 49 } |
| 50 |
| 51 Delegate* delegate_; |
| 52 }; |
| 53 |
| 54 class InspectorFrontendImpl : public mojo::InterfaceImpl<InspectorFrontend> { |
| 15 public: | 55 public: |
| 16 Server() {} | 56 class Delegate { |
| 17 virtual ~Server() {} | 57 public: |
| 58 virtual void Register(InspectorFrontendImpl*) = 0; |
| 59 virtual void Unregister(InspectorFrontendImpl*) = 0; |
| 60 virtual void SendMessage(const mojo::String&) = 0; |
| 61 }; |
| 62 |
| 63 InspectorFrontendImpl(Delegate* delegate); |
| 64 virtual ~InspectorFrontendImpl(); |
| 65 |
| 66 void OnShutdown(); |
| 18 | 67 |
| 19 private: | 68 private: |
| 20 // Overridden from mojo::ApplicationDelegate: | 69 // InspectorFrontend: |
| 21 virtual void Initialize(mojo::ApplicationImpl* app) override { | 70 void SendMessage(const mojo::String& message) override; |
| 22 } | 71 |
| 23 | 72 // InterfaceImpl: |
| 24 virtual bool ConfigureIncomingConnection( | 73 void OnConnectionError() override; |
| 74 |
| 75 Delegate* delegate_; |
| 76 |
| 77 MOJO_DISALLOW_COPY_AND_ASSIGN(InspectorFrontendImpl); |
| 78 }; |
| 79 |
| 80 InspectorFrontendImpl::InspectorFrontendImpl(Delegate* delegate) |
| 81 : delegate_(delegate) { |
| 82 delegate_->Register(this); |
| 83 } |
| 84 |
| 85 InspectorFrontendImpl::~InspectorFrontendImpl() { |
| 86 delegate_->Unregister(this); |
| 87 } |
| 88 |
| 89 void InspectorFrontendImpl::OnShutdown() { |
| 90 client()->OnDisconnect(); |
| 91 delete this; |
| 92 } |
| 93 |
| 94 void InspectorFrontendImpl::OnConnectionError() { |
| 95 delete this; // crbug.com/431911 |
| 96 } |
| 97 |
| 98 void InspectorFrontendImpl::SendMessage(const mojo::String& message) { |
| 99 delegate_->SendMessage(message); |
| 100 } |
| 101 |
| 102 |
| 103 namespace { |
| 104 const int kNotConnected = -1; |
| 105 } |
| 106 |
| 107 class Server : public mojo::ApplicationDelegate, |
| 108 public InspectorFrontendImpl::Delegate, |
| 109 public InspectorServerImpl::Delegate, |
| 110 public mojo::InterfaceFactory<InspectorFrontend>, |
| 111 public mojo::InterfaceFactory<InspectorServer>, |
| 112 public net::HttpServer::Delegate { |
| 113 public: |
| 114 Server() : connection_id_(kNotConnected) {} |
| 115 virtual ~Server(); |
| 116 |
| 117 private: |
| 118 // mojo::ApplicationDelegate: |
| 119 void Initialize(mojo::ApplicationImpl* app) override { |
| 120 } |
| 121 bool ConfigureIncomingConnection( |
| 25 mojo::ApplicationConnection* connection) override { | 122 mojo::ApplicationConnection* connection) override { |
| 26 connection->AddService(&frontend_factory_); | 123 connection->AddService<InspectorFrontend>(this); |
| 124 connection->AddService<InspectorServer>(this); |
| 27 return true; | 125 return true; |
| 28 } | 126 } |
| 29 | 127 |
| 30 InspectorFrontendFactory frontend_factory_; | 128 // InterfaceFactory<InspectorFrontend>: |
| 129 void Create(mojo::ApplicationConnection* connection, |
| 130 mojo::InterfaceRequest<InspectorFrontend> request) override { |
| 131 // Weak instead of strong, per crbug.com/431911 |
| 132 WeakBindToRequest(new InspectorFrontendImpl(this), &request); |
| 133 } |
| 134 |
| 135 // InterfaceFactory<InspectorServer>: |
| 136 void Create(mojo::ApplicationConnection* connection, |
| 137 mojo::InterfaceRequest<InspectorServer> request) override { |
| 138 // Weak instead of strong, per crbug.com/431911 |
| 139 WeakBindToRequest(new InspectorServerImpl(this), &request); |
| 140 } |
| 141 |
| 142 // InspectorServerImpl::Delegate: |
| 143 void Register(InspectorServerImpl*) override; |
| 144 void Unregister(InspectorServerImpl*) override; |
| 145 void Listen(int32_t port) override; |
| 146 |
| 147 // InspectorFrontendImpl::Delegate: |
| 148 void Register(InspectorFrontendImpl*) override; |
| 149 void Unregister(InspectorFrontendImpl*) override; |
| 150 void SendMessage(const mojo::String& message) override; |
| 151 |
| 152 // net::HttpServer::Delegate: |
| 153 void OnConnect(int connection_id) override; |
| 154 void OnHttpRequest( |
| 155 int connection_id, const net::HttpServerRequestInfo& info) override; |
| 156 void OnWebSocketRequest( |
| 157 int connection_id, const net::HttpServerRequestInfo& info) override; |
| 158 void OnWebSocketMessage( |
| 159 int connection_id, const std::string& data) override; |
| 160 void OnClose(int connection_id) override; |
| 161 |
| 162 void CloseAllAgentConnections(); |
| 163 |
| 164 int connection_id_; |
| 165 scoped_ptr<net::HttpServer> web_server_; |
| 166 ObserverList<InspectorFrontendImpl> agents_; |
| 167 ObserverList<InspectorServerImpl> clients_; |
| 31 | 168 |
| 32 DISALLOW_COPY_AND_ASSIGN(Server); | 169 DISALLOW_COPY_AND_ASSIGN(Server); |
| 33 }; | 170 }; |
| 34 | 171 |
| 172 Server::~Server() |
| 173 { |
| 174 FOR_EACH_OBSERVER(InspectorServerImpl, clients_, OnShutdown()); |
| 175 CloseAllAgentConnections(); |
| 176 } |
| 177 |
| 178 void Server::CloseAllAgentConnections() { |
| 179 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, OnShutdown()); |
| 180 } |
| 181 |
| 182 void Server::OnConnect(int connection_id) { |
| 183 } |
| 184 |
| 185 void Server::OnHttpRequest( |
| 186 int connection_id, const net::HttpServerRequestInfo& info) { |
| 187 web_server_->Send500(connection_id, "websockets protocol only"); |
| 188 } |
| 189 |
| 190 void Server::OnWebSocketRequest( |
| 191 int connection_id, const net::HttpServerRequestInfo& info) { |
| 192 if (connection_id_ != kNotConnected) { |
| 193 web_server_->Close(connection_id); |
| 194 return; |
| 195 } |
| 196 web_server_->AcceptWebSocket(connection_id, info); |
| 197 connection_id_ = connection_id; |
| 198 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnConnect()); |
| 199 } |
| 200 |
| 201 void Server::OnWebSocketMessage( |
| 202 int connection_id, const std::string& data) { |
| 203 DCHECK_EQ(connection_id, connection_id_); |
| 204 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnMessage(data)); |
| 205 } |
| 206 |
| 207 void Server::OnClose(int connection_id) { |
| 208 if (connection_id != connection_id_) |
| 209 return; |
| 210 connection_id_ = kNotConnected; |
| 211 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnDisconnect()); |
| 212 } |
| 213 |
| 214 void Server::Register(InspectorServerImpl* client) { |
| 215 clients_.AddObserver(client); |
| 216 } |
| 217 |
| 218 void Server::Unregister(InspectorServerImpl* client) { |
| 219 clients_.RemoveObserver(client); |
| 220 } |
| 221 |
| 222 void Server::Register(InspectorFrontendImpl* agent) { |
| 223 agents_.AddObserver(agent); |
| 224 } |
| 225 |
| 226 void Server::Unregister(InspectorFrontendImpl* agent) { |
| 227 agents_.RemoveObserver(agent); |
| 228 } |
| 229 |
| 230 void Server::Listen(int32_t port) { |
| 231 CloseAllAgentConnections(); // Assume caller represents a new app. |
| 232 |
| 233 // TODO(eseidel): Early-out here if we're already bound to the right port. |
| 234 web_server_.reset(); |
| 235 scoped_ptr<net::ServerSocket> server_socket( |
| 236 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 237 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); |
| 238 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
| 239 } |
| 240 |
| 241 void Server::SendMessage(const mojo::String& message) { |
| 242 if (connection_id_ == kNotConnected) |
| 243 return; |
| 244 web_server_->SendOverWebSocket(connection_id_, message); |
| 245 } |
| 246 |
| 35 } // namespace inspector | 247 } // namespace inspector |
| 36 } // namespace sky | 248 } // namespace sky |
| 37 | 249 |
| 38 MojoResult MojoMain(MojoHandle shell_handle) { | 250 MojoResult MojoMain(MojoHandle shell_handle) { |
| 39 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); | 251 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); |
| 40 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | 252 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); |
| 41 return runner.Run(shell_handle); | 253 return runner.Run(shell_handle); |
| 42 } | 254 } |
| OLD | NEW |