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

Side by Side Diff: sky/services/inspector/server.cc

Issue 711413005: Use WeakBindingSet to manage inspector connections (Closed) Base URL: git@github.com:domokit/mojo.git@connector
Patch Set: 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 unified diff | Download patch
« mojo/common/weak_binding_set.h ('K') | « sky/services/inspector/DEPS ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/common/weak_binding_set.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"
9 #include "mojo/public/cpp/application/interface_factory_impl.h" 10 #include "mojo/public/cpp/bindings/binding.h"
10 #include "mojo/public/cpp/bindings/interface_impl.h"
11 #include "net/server/http_server.h" 11 #include "net/server/http_server.h"
12 #include "net/socket/tcp_server_socket.h" 12 #include "net/socket/tcp_server_socket.h"
13 #include "sky/services/inspector/inspector.mojom.h" 13 #include "sky/services/inspector/inspector.mojom.h"
14 14
15 15
16 namespace sky { 16 namespace sky {
17 namespace inspector { 17 namespace inspector {
18 18
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> {
55 public:
56 class Delegate {
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();
67
68 private:
69 // InspectorFrontend:
70 void SendMessage(const mojo::String& message) override;
71
72 // InterfaceImpl:
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 { 19 namespace {
104 const int kNotConnected = -1; 20 const int kNotConnected = -1;
105 } 21 }
106 22
107 class Server : public mojo::ApplicationDelegate, 23 class Server : public mojo::ApplicationDelegate,
108 public InspectorFrontendImpl::Delegate, 24 public InspectorFrontend,
109 public InspectorServerImpl::Delegate, 25 public InspectorServer,
110 public mojo::InterfaceFactory<InspectorFrontend>, 26 public mojo::InterfaceFactory<InspectorFrontend>,
111 public mojo::InterfaceFactory<InspectorServer>, 27 public mojo::InterfaceFactory<InspectorServer>,
112 public net::HttpServer::Delegate { 28 public net::HttpServer::Delegate {
113 public: 29 public:
114 Server() : connection_id_(kNotConnected) {} 30 Server() : connection_id_(kNotConnected), server_binding_(this) {}
115 virtual ~Server(); 31 virtual ~Server();
116 32
117 private: 33 private:
118 // mojo::ApplicationDelegate: 34 // mojo::ApplicationDelegate:
119 void Initialize(mojo::ApplicationImpl* app) override { 35 void Initialize(mojo::ApplicationImpl* app) override {
120 } 36 }
121 bool ConfigureIncomingConnection( 37 bool ConfigureIncomingConnection(
122 mojo::ApplicationConnection* connection) override { 38 mojo::ApplicationConnection* connection) override {
123 connection->AddService<InspectorFrontend>(this); 39 connection->AddService<InspectorFrontend>(this);
124 connection->AddService<InspectorServer>(this); 40 connection->AddService<InspectorServer>(this);
125 return true; 41 return true;
126 } 42 }
127 43
128 // InterfaceFactory<InspectorFrontend>: 44 // InterfaceFactory<InspectorFrontend>:
129 void Create(mojo::ApplicationConnection* connection, 45 void Create(mojo::ApplicationConnection* connection,
130 mojo::InterfaceRequest<InspectorFrontend> request) override { 46 mojo::InterfaceRequest<InspectorFrontend> request) override {
131 // Weak instead of strong, per crbug.com/431911 47 frontend_bindings_.AddBinding(this, request.Pass());
132 WeakBindToRequest(new InspectorFrontendImpl(this), &request);
133 } 48 }
134 49
135 // InterfaceFactory<InspectorServer>: 50 // InterfaceFactory<InspectorServer>:
136 void Create(mojo::ApplicationConnection* connection, 51 void Create(mojo::ApplicationConnection* connection,
137 mojo::InterfaceRequest<InspectorServer> request) override { 52 mojo::InterfaceRequest<InspectorServer> request) override {
138 // Weak instead of strong, per crbug.com/431911 53 server_binding_.Bind(request.PassMessagePipe());
139 WeakBindToRequest(new InspectorServerImpl(this), &request);
140 } 54 }
141 55
142 // InspectorServerImpl::Delegate: 56 // InspectorServer:
143 void Register(InspectorServerImpl*) override; 57 void Listen(int32_t port, const mojo::Closure& callback) override;
144 void Unregister(InspectorServerImpl*) override;
145 void Listen(int32_t port) override;
146 58
147 // InspectorFrontendImpl::Delegate: 59 // InspectorFrontend:
148 void Register(InspectorFrontendImpl*) override;
149 void Unregister(InspectorFrontendImpl*) override;
150 void SendMessage(const mojo::String& message) override; 60 void SendMessage(const mojo::String& message) override;
151 61
152 // net::HttpServer::Delegate: 62 // net::HttpServer::Delegate:
153 void OnConnect(int connection_id) override; 63 void OnConnect(int connection_id) override;
154 void OnHttpRequest( 64 void OnHttpRequest(
155 int connection_id, const net::HttpServerRequestInfo& info) override; 65 int connection_id, const net::HttpServerRequestInfo& info) override;
156 void OnWebSocketRequest( 66 void OnWebSocketRequest(
157 int connection_id, const net::HttpServerRequestInfo& info) override; 67 int connection_id, const net::HttpServerRequestInfo& info) override;
158 void OnWebSocketMessage( 68 void OnWebSocketMessage(
159 int connection_id, const std::string& data) override; 69 int connection_id, const std::string& data) override;
160 void OnClose(int connection_id) override; 70 void OnClose(int connection_id) override;
161 71
162 void CloseAllAgentConnections(); 72 void CloseAllAgentConnections();
163 73
164 int connection_id_; 74 int connection_id_;
165 scoped_ptr<net::HttpServer> web_server_; 75 scoped_ptr<net::HttpServer> web_server_;
166 ObserverList<InspectorFrontendImpl> agents_; 76
167 ObserverList<InspectorServerImpl> clients_; 77 mojo::WeakBindingSet<InspectorFrontend> frontend_bindings_;
78 mojo::Binding<InspectorServer> server_binding_;
168 79
169 DISALLOW_COPY_AND_ASSIGN(Server); 80 DISALLOW_COPY_AND_ASSIGN(Server);
170 }; 81 };
171 82
172 Server::~Server() 83 Server::~Server()
173 { 84 {
174 FOR_EACH_OBSERVER(InspectorServerImpl, clients_, OnShutdown());
175 CloseAllAgentConnections(); 85 CloseAllAgentConnections();
176 } 86 }
177 87
178 void Server::CloseAllAgentConnections() { 88 void Server::CloseAllAgentConnections() {
179 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, OnShutdown()); 89 frontend_bindings_.CloseAllBindings();
eseidel 2014/11/14 00:30:16 I would just inline this now.
180 } 90 }
181 91
182 void Server::OnConnect(int connection_id) { 92 void Server::OnConnect(int connection_id) {
183 } 93 }
184 94
185 void Server::OnHttpRequest( 95 void Server::OnHttpRequest(
186 int connection_id, const net::HttpServerRequestInfo& info) { 96 int connection_id, const net::HttpServerRequestInfo& info) {
187 web_server_->Send500(connection_id, "websockets protocol only"); 97 web_server_->Send500(connection_id, "websockets protocol only");
188 } 98 }
189 99
190 void Server::OnWebSocketRequest( 100 void Server::OnWebSocketRequest(
191 int connection_id, const net::HttpServerRequestInfo& info) { 101 int connection_id, const net::HttpServerRequestInfo& info) {
192 if (connection_id_ != kNotConnected) { 102 if (connection_id_ != kNotConnected) {
193 web_server_->Close(connection_id); 103 web_server_->Close(connection_id);
194 return; 104 return;
195 } 105 }
196 web_server_->AcceptWebSocket(connection_id, info); 106 web_server_->AcceptWebSocket(connection_id, info);
197 connection_id_ = connection_id; 107 connection_id_ = connection_id;
198 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnConnect()); 108 frontend_bindings_.ForAllBindings(
109 [](InspectorFrontend::Client* client) { client->OnConnect(); });
199 } 110 }
200 111
201 void Server::OnWebSocketMessage( 112 void Server::OnWebSocketMessage(
202 int connection_id, const std::string& data) { 113 int connection_id, const std::string& data) {
203 DCHECK_EQ(connection_id, connection_id_); 114 DCHECK_EQ(connection_id, connection_id_);
204 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnMessage(data)); 115 frontend_bindings_.ForAllBindings(
116 [data](InspectorFrontend::Client* client) { client->OnMessage(data); });
205 } 117 }
206 118
207 void Server::OnClose(int connection_id) { 119 void Server::OnClose(int connection_id) {
208 if (connection_id != connection_id_) 120 if (connection_id != connection_id_)
209 return; 121 return;
210 connection_id_ = kNotConnected; 122 connection_id_ = kNotConnected;
211 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnDisconnect()); 123 frontend_bindings_.ForAllBindings(
124 [](InspectorFrontend::Client* client) { client->OnDisconnect(); });
212 } 125 }
213 126
214 void Server::Register(InspectorServerImpl* client) { 127 void Server::Listen(int32_t port, const mojo::Closure& callback) {
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. 128 CloseAllAgentConnections(); // Assume caller represents a new app.
232 129
233 // TODO(eseidel): Early-out here if we're already bound to the right port. 130 // TODO(eseidel): Early-out here if we're already bound to the right port.
234 web_server_.reset(); 131 web_server_.reset();
235 scoped_ptr<net::ServerSocket> server_socket( 132 scoped_ptr<net::ServerSocket> server_socket(
236 new net::TCPServerSocket(NULL, net::NetLog::Source())); 133 new net::TCPServerSocket(NULL, net::NetLog::Source()));
237 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); 134 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1);
238 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); 135 web_server_.reset(new net::HttpServer(server_socket.Pass(), this));
136 callback.Run();
239 } 137 }
240 138
241 void Server::SendMessage(const mojo::String& message) { 139 void Server::SendMessage(const mojo::String& message) {
242 if (connection_id_ == kNotConnected) 140 if (connection_id_ == kNotConnected)
243 return; 141 return;
244 web_server_->SendOverWebSocket(connection_id_, message); 142 web_server_->SendOverWebSocket(connection_id_, message);
245 } 143 }
246 144
247 } // namespace inspector 145 } // namespace inspector
248 } // namespace sky 146 } // namespace sky
249 147
250 MojoResult MojoMain(MojoHandle shell_handle) { 148 MojoResult MojoMain(MojoHandle shell_handle) {
251 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); 149 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server);
252 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); 150 runner.set_message_loop_type(base::MessageLoop::TYPE_IO);
253 return runner.Run(shell_handle); 151 return runner.Run(shell_handle);
254 } 152 }
OLDNEW
« mojo/common/weak_binding_set.h ('K') | « sky/services/inspector/DEPS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698