Chromium Code Reviews| 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 "base/lazy_instance.h" | |
|
abarth-chromium
2014/11/11 02:53:57
Is this needed?
| |
| 5 #include "mojo/application/application_runner_chromium.h" | 6 #include "mojo/application/application_runner_chromium.h" |
| 6 #include "mojo/public/c/system/main.h" | 7 #include "mojo/public/c/system/main.h" |
| 7 #include "mojo/public/cpp/application/application_delegate.h" | 8 #include "mojo/public/cpp/application/application_delegate.h" |
| 8 #include "mojo/public/cpp/application/application_impl.h" | 9 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "net/server/http_server.h" | |
| 11 #include "net/socket/tcp_server_socket.h" | |
| 9 #include "sky/services/inspector/inspector_frontend_impl.h" | 12 #include "sky/services/inspector/inspector_frontend_impl.h" |
| 10 | 13 |
| 11 namespace sky { | 14 namespace sky { |
| 12 namespace inspector { | 15 namespace inspector { |
| 13 | 16 |
| 14 class Server : public mojo::ApplicationDelegate { | 17 // TODO(eseidel): None of this Impl nonsense is necessary: crbug.com/431963 |
| 18 class ServerImpl : public mojo::InterfaceImpl<InspectorServer> { | |
|
abarth-chromium
2014/11/11 02:53:57
Should we move this into server.h and create anoth
Aaron Boodman
2014/11/11 16:12:50
As I said in other class, I think the opposite.
| |
| 19 public: | |
| 20 class Delegate { | |
| 21 public: | |
| 22 virtual void Register(ServerImpl* impl) = 0; | |
| 23 virtual void Unregister(ServerImpl* impl) = 0; | |
| 24 virtual void Listen(int32_t port) = 0; | |
| 25 }; | |
| 26 ServerImpl(Delegate* delegate) : delegate_(delegate) { | |
|
abarth-chromium
2014/11/11 02:53:57
explicit
| |
| 27 delegate_->Register(this); | |
| 28 } | |
| 29 virtual ~ServerImpl() { | |
| 30 delegate_->Unregister(this); | |
| 31 } | |
| 32 | |
| 33 // InspectorServer: | |
| 34 void Listen(int32_t port, const mojo::Callback<void()>& callback) override; | |
| 35 | |
| 36 void OnShutdown() { | |
| 37 delete this; | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 // InterfaceImpl: | |
| 42 void OnConnectionError() override { | |
| 43 delete this; | |
| 44 } | |
| 45 | |
| 46 Delegate* delegate_; | |
| 47 }; | |
| 48 | |
| 49 namespace { | |
| 50 const int kNotConnected = -1; | |
| 51 } | |
| 52 | |
| 53 class Server : public mojo::ApplicationDelegate, | |
| 54 public InspectorFrontendImpl::Delegate, | |
| 55 public ServerImpl::Delegate, | |
| 56 public mojo::InterfaceFactory<InspectorFrontend>, | |
| 57 public mojo::InterfaceFactory<InspectorServer>, | |
| 58 public net::HttpServer::Delegate { | |
| 15 public: | 59 public: |
| 16 Server() {} | 60 Server() : connection_id_(kNotConnected) {} |
| 17 virtual ~Server() {} | 61 virtual ~Server(); |
| 18 | 62 |
| 19 private: | 63 private: |
| 20 // Overridden from mojo::ApplicationDelegate: | 64 // mojo::ApplicationDelegate: |
| 21 virtual void Initialize(mojo::ApplicationImpl* app) override { | 65 void Initialize(mojo::ApplicationImpl* app) override { |
| 22 } | 66 } |
| 23 | 67 bool ConfigureIncomingConnection( |
| 24 virtual bool ConfigureIncomingConnection( | |
| 25 mojo::ApplicationConnection* connection) override { | 68 mojo::ApplicationConnection* connection) override { |
| 26 connection->AddService(&frontend_factory_); | 69 connection->AddService<InspectorFrontend>(this); |
| 70 connection->AddService<InspectorServer>(this); | |
| 27 return true; | 71 return true; |
| 28 } | 72 } |
| 29 | 73 |
| 30 InspectorFrontendFactory frontend_factory_; | 74 // InterfaceFactory<InspectorFrontend>: |
| 75 void Create(mojo::ApplicationConnection* connection, | |
| 76 mojo::InterfaceRequest<InspectorFrontend> request) override { | |
| 77 WeakBindToRequest(new InspectorFrontendImpl(this), &request); | |
| 78 } | |
| 79 | |
| 80 // InterfaceFactory<InspectorServer>: | |
| 81 void Create(mojo::ApplicationConnection* connection, | |
| 82 mojo::InterfaceRequest<InspectorServer> request) override { | |
| 83 WeakBindToRequest(new ServerImpl(this), &request); | |
| 84 } | |
| 85 | |
| 86 // ServerImpl::Delegate: | |
| 87 void Register(ServerImpl*) override; | |
| 88 void Unregister(ServerImpl*) override; | |
| 89 void Listen(int32_t port) override; | |
| 90 | |
| 91 // InspectorFrontendImpl::Delegate: | |
| 92 void Register(InspectorFrontendImpl*) override; | |
| 93 void Unregister(InspectorFrontendImpl*) override; | |
| 94 void SendMessage(const mojo::String& message) override; | |
| 95 | |
| 96 // net::HttpServer::Delegate: | |
| 97 void OnConnect(int connection_id) override; | |
| 98 void OnHttpRequest( | |
| 99 int connection_id, const net::HttpServerRequestInfo& info) override; | |
| 100 void OnWebSocketRequest( | |
| 101 int connection_id, const net::HttpServerRequestInfo& info) override; | |
| 102 void OnWebSocketMessage( | |
| 103 int connection_id, const std::string& data) override; | |
| 104 void OnClose(int connection_id) override; | |
| 105 | |
| 106 void CloseAllAgentConnections(); | |
| 107 | |
| 108 int connection_id_; | |
| 109 scoped_ptr<net::HttpServer> web_server_; | |
| 110 ObserverList<InspectorFrontendImpl> agents_; | |
| 111 ObserverList<ServerImpl> clients_; | |
| 31 | 112 |
| 32 DISALLOW_COPY_AND_ASSIGN(Server); | 113 DISALLOW_COPY_AND_ASSIGN(Server); |
| 33 }; | 114 }; |
| 34 | 115 |
| 116 Server::~Server() | |
| 117 { | |
|
abarth-chromium
2014/11/11 02:53:57
These lines should be merged in Chromium style.
| |
| 118 FOR_EACH_OBSERVER(ServerImpl, clients_, OnShutdown()); | |
| 119 CloseAllAgentConnections(); | |
| 120 } | |
| 121 | |
| 122 void Server::CloseAllAgentConnections() { | |
| 123 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, OnShutdown()); | |
| 124 } | |
| 125 | |
| 126 void Server::OnConnect(int connection_id) { | |
| 127 } | |
| 128 | |
| 129 void Server::OnHttpRequest( | |
| 130 int connection_id, const net::HttpServerRequestInfo& info) { | |
| 131 web_server_->Send500(connection_id, "websockets protocol only"); | |
| 132 } | |
| 133 | |
| 134 void Server::OnWebSocketRequest( | |
| 135 int connection_id, const net::HttpServerRequestInfo& info) { | |
| 136 if (connection_id_ != kNotConnected) { | |
| 137 web_server_->Close(connection_id); | |
| 138 return; | |
| 139 } | |
| 140 web_server_->AcceptWebSocket(connection_id, info); | |
| 141 connection_id_ = connection_id; | |
| 142 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnConnect()); | |
| 143 } | |
| 144 | |
| 145 void Server::OnWebSocketMessage( | |
| 146 int connection_id, const std::string& data) { | |
| 147 DCHECK_EQ(connection_id, connection_id_); | |
| 148 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnMessage(data)); | |
| 149 } | |
| 150 | |
| 151 void Server::OnClose(int connection_id) { | |
| 152 if (connection_id != connection_id_) | |
| 153 return; | |
| 154 connection_id_ = kNotConnected; | |
| 155 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnDisconnect()); | |
| 156 } | |
| 157 | |
| 158 void Server::Register(ServerImpl* client) { | |
| 159 clients_.AddObserver(client); | |
| 160 } | |
| 161 | |
| 162 void Server::Unregister(ServerImpl* client) { | |
| 163 clients_.RemoveObserver(client); | |
| 164 } | |
| 165 | |
| 166 void Server::Register(InspectorFrontendImpl* agent) { | |
| 167 agents_.AddObserver(agent); | |
| 168 } | |
| 169 | |
| 170 void Server::Unregister(InspectorFrontendImpl* agent) { | |
| 171 agents_.RemoveObserver(agent); | |
| 172 } | |
| 173 | |
| 174 void ServerImpl::Listen(int32_t port, const mojo::Callback<void()>& callback) { | |
| 175 delegate_->Listen(port); | |
| 176 callback.Run(); | |
| 177 } | |
| 178 | |
| 179 void Server::Listen(int32_t port) { | |
| 180 CloseAllAgentConnections(); // Assume caller represents a new app. | |
| 181 | |
| 182 // TODO(eseidel): Early-out here if we're already bound to the right port. | |
|
abarth-chromium
2014/11/11 02:53:57
Is this comment still accurate? The network seman
| |
| 183 web_server_.reset(); | |
| 184 scoped_ptr<net::ServerSocket> server_socket( | |
| 185 new net::TCPServerSocket(NULL, net::NetLog::Source())); | |
| 186 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); | |
| 187 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); | |
| 188 } | |
| 189 | |
| 190 void Server::SendMessage(const mojo::String& message) { | |
| 191 if (connection_id_ == kNotConnected) | |
| 192 return; | |
| 193 web_server_->SendOverWebSocket(connection_id_, message); | |
| 194 } | |
| 195 | |
| 35 } // namespace inspector | 196 } // namespace inspector |
| 36 } // namespace sky | 197 } // namespace sky |
| 37 | 198 |
| 38 MojoResult MojoMain(MojoHandle shell_handle) { | 199 MojoResult MojoMain(MojoHandle shell_handle) { |
| 39 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); | 200 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); |
| 40 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | 201 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); |
| 41 return runner.Run(shell_handle); | 202 return runner.Run(shell_handle); |
| 42 } | 203 } |
| OLD | NEW |