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

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
« no previous file with comments | « mojo/public/cpp/bindings/binding.h ('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 <algorithm>
6
7 #include "base/memory/weak_ptr.h"
5 #include "mojo/application/application_runner_chromium.h" 8 #include "mojo/application/application_runner_chromium.h"
6 #include "mojo/public/c/system/main.h" 9 #include "mojo/public/c/system/main.h"
7 #include "mojo/public/cpp/application/application_delegate.h" 10 #include "mojo/public/cpp/application/application_delegate.h"
8 #include "mojo/public/cpp/application/application_impl.h" 11 #include "mojo/public/cpp/application/application_impl.h"
9 #include "mojo/public/cpp/application/interface_factory_impl.h" 12 #include "mojo/public/cpp/application/interface_factory_impl.h"
13 #include "mojo/public/cpp/bindings/binding.h"
10 #include "mojo/public/cpp/bindings/interface_impl.h" 14 #include "mojo/public/cpp/bindings/interface_impl.h"
15 #include "mojo/public/cpp/bindings/strong_binding.h"
11 #include "net/server/http_server.h" 16 #include "net/server/http_server.h"
12 #include "net/socket/tcp_server_socket.h" 17 #include "net/socket/tcp_server_socket.h"
13 #include "sky/services/inspector/inspector.mojom.h" 18 #include "sky/services/inspector/inspector.mojom.h"
14 19
15 20
16 namespace sky { 21 namespace sky {
17 namespace inspector { 22 namespace inspector {
18 23
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 { 24 namespace {
104 const int kNotConnected = -1; 25 const int kNotConnected = -1;
105 } 26 }
106 27
28 // This provides a binding to an InspectorFrontend implementation that exposes a
jamesr 2014/11/12 20:47:38 this could be made generic and live somewhere more
29 // weak pointer to itself. The binding itself is destroyed when a connection
30 // error is detected or the binding is explicitly told to close.
31 class FrontendWeakBinding : public mojo::ErrorHandler {
32 public:
33 FrontendWeakBinding(InspectorFrontend* frontend,
34 mojo::InterfaceRequest<InspectorFrontend> request)
35 : binding_(frontend, request.Pass()), weak_ptr_factory_(this) {
36 binding_.set_error_handler(this);
37 }
38
39 void Close() { binding_.Close(); }
40
41 InspectorFrontend::Client* client() { return binding_.client(); }
42
43 base::WeakPtr<FrontendWeakBinding> GetWeakPtr() {
44 return weak_ptr_factory_.GetWeakPtr();
45 }
46
47 // mojo::ErrorHandler implementation.
48 void OnConnectionError() override { delete this; }
49
50 private:
51 mojo::Binding<InspectorFrontend> binding_;
52 base::WeakPtrFactory<FrontendWeakBinding> weak_ptr_factory_;
53 };
54
107 class Server : public mojo::ApplicationDelegate, 55 class Server : public mojo::ApplicationDelegate,
108 public InspectorFrontendImpl::Delegate, 56 public InspectorFrontend,
109 public InspectorServerImpl::Delegate, 57 public InspectorServer,
110 public mojo::InterfaceFactory<InspectorFrontend>, 58 public mojo::InterfaceFactory<InspectorFrontend>,
111 public mojo::InterfaceFactory<InspectorServer>, 59 public mojo::InterfaceFactory<InspectorServer>,
112 public net::HttpServer::Delegate { 60 public net::HttpServer::Delegate {
113 public: 61 public:
114 Server() : connection_id_(kNotConnected) {} 62 Server() : connection_id_(kNotConnected), server_binding_(this) {}
115 virtual ~Server(); 63 virtual ~Server();
116 64
117 private: 65 private:
118 // mojo::ApplicationDelegate: 66 // mojo::ApplicationDelegate:
119 void Initialize(mojo::ApplicationImpl* app) override { 67 void Initialize(mojo::ApplicationImpl* app) override {
120 } 68 }
121 bool ConfigureIncomingConnection( 69 bool ConfigureIncomingConnection(
122 mojo::ApplicationConnection* connection) override { 70 mojo::ApplicationConnection* connection) override {
123 connection->AddService<InspectorFrontend>(this); 71 connection->AddService<InspectorFrontend>(this);
124 connection->AddService<InspectorServer>(this); 72 connection->AddService<InspectorServer>(this);
125 return true; 73 return true;
126 } 74 }
127 75
128 // InterfaceFactory<InspectorFrontend>: 76 // InterfaceFactory<InspectorFrontend>:
129 void Create(mojo::ApplicationConnection* connection, 77 void Create(mojo::ApplicationConnection* connection,
130 mojo::InterfaceRequest<InspectorFrontend> request) override { 78 mojo::InterfaceRequest<InspectorFrontend> request) override {
131 // Weak instead of strong, per crbug.com/431911 79 auto binding = new FrontendWeakBinding(this, request.Pass());
132 WeakBindToRequest(new InspectorFrontendImpl(this), &request); 80 frontend_bindings_.push_back(binding->GetWeakPtr());
133 } 81 }
134 82
135 // InterfaceFactory<InspectorServer>: 83 // InterfaceFactory<InspectorServer>:
136 void Create(mojo::ApplicationConnection* connection, 84 void Create(mojo::ApplicationConnection* connection,
137 mojo::InterfaceRequest<InspectorServer> request) override { 85 mojo::InterfaceRequest<InspectorServer> request) override {
138 // Weak instead of strong, per crbug.com/431911 86 server_binding_.Bind(request.PassMessagePipe());
139 WeakBindToRequest(new InspectorServerImpl(this), &request);
140 } 87 }
141 88
142 // InspectorServerImpl::Delegate: 89 // InspectorServer:
143 void Register(InspectorServerImpl*) override; 90 void Listen(int32_t port, const mojo::Closure& callback) override;
144 void Unregister(InspectorServerImpl*) override;
145 void Listen(int32_t port) override;
146 91
147 // InspectorFrontendImpl::Delegate: 92 // InspectorFrontend:
148 void Register(InspectorFrontendImpl*) override;
149 void Unregister(InspectorFrontendImpl*) override;
150 void SendMessage(const mojo::String& message) override; 93 void SendMessage(const mojo::String& message) override;
151 94
152 // net::HttpServer::Delegate: 95 // net::HttpServer::Delegate:
153 void OnConnect(int connection_id) override; 96 void OnConnect(int connection_id) override;
154 void OnHttpRequest( 97 void OnHttpRequest(
155 int connection_id, const net::HttpServerRequestInfo& info) override; 98 int connection_id, const net::HttpServerRequestInfo& info) override;
156 void OnWebSocketRequest( 99 void OnWebSocketRequest(
157 int connection_id, const net::HttpServerRequestInfo& info) override; 100 int connection_id, const net::HttpServerRequestInfo& info) override;
158 void OnWebSocketMessage( 101 void OnWebSocketMessage(
159 int connection_id, const std::string& data) override; 102 int connection_id, const std::string& data) override;
160 void OnClose(int connection_id) override; 103 void OnClose(int connection_id) override;
161 104
162 void CloseAllAgentConnections(); 105 void CloseAllAgentConnections();
106 void ClearNullFrontendBindings();
163 107
164 int connection_id_; 108 int connection_id_;
165 scoped_ptr<net::HttpServer> web_server_; 109 scoped_ptr<net::HttpServer> web_server_;
166 ObserverList<InspectorFrontendImpl> agents_; 110
167 ObserverList<InspectorServerImpl> clients_; 111 std::vector<base::WeakPtr<FrontendWeakBinding>> frontend_bindings_;
112 mojo::Binding<InspectorServer> server_binding_;
168 113
169 DISALLOW_COPY_AND_ASSIGN(Server); 114 DISALLOW_COPY_AND_ASSIGN(Server);
170 }; 115 };
171 116
172 Server::~Server() 117 Server::~Server()
173 { 118 {
174 FOR_EACH_OBSERVER(InspectorServerImpl, clients_, OnShutdown());
175 CloseAllAgentConnections(); 119 CloseAllAgentConnections();
176 } 120 }
177 121
178 void Server::CloseAllAgentConnections() { 122 void Server::CloseAllAgentConnections() {
179 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, OnShutdown()); 123 for (const auto& it : frontend_bindings_) {
124 if (it.get())
125 it->Close();
126 }
127 frontend_bindings_.clear();
128 }
129
130 void Server::ClearNullFrontendBindings() {
131 frontend_bindings_.erase(
132 std::remove_if(frontend_bindings_.begin(), frontend_bindings_.end(),
133 [](const base::WeakPtr<FrontendWeakBinding>& p) {
134 return p.get() == nullptr;
135 }),
136 frontend_bindings_.end());
180 } 137 }
181 138
182 void Server::OnConnect(int connection_id) { 139 void Server::OnConnect(int connection_id) {
183 } 140 }
184 141
185 void Server::OnHttpRequest( 142 void Server::OnHttpRequest(
186 int connection_id, const net::HttpServerRequestInfo& info) { 143 int connection_id, const net::HttpServerRequestInfo& info) {
187 web_server_->Send500(connection_id, "websockets protocol only"); 144 web_server_->Send500(connection_id, "websockets protocol only");
188 } 145 }
189 146
190 void Server::OnWebSocketRequest( 147 void Server::OnWebSocketRequest(
191 int connection_id, const net::HttpServerRequestInfo& info) { 148 int connection_id, const net::HttpServerRequestInfo& info) {
192 if (connection_id_ != kNotConnected) { 149 if (connection_id_ != kNotConnected) {
193 web_server_->Close(connection_id); 150 web_server_->Close(connection_id);
194 return; 151 return;
195 } 152 }
196 web_server_->AcceptWebSocket(connection_id, info); 153 web_server_->AcceptWebSocket(connection_id, info);
197 connection_id_ = connection_id; 154 connection_id_ = connection_id;
198 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnConnect()); 155 ClearNullFrontendBindings();
156 for (const auto& it : frontend_bindings_) {
157 it->client()->OnConnect();
158 }
199 } 159 }
200 160
201 void Server::OnWebSocketMessage( 161 void Server::OnWebSocketMessage(
202 int connection_id, const std::string& data) { 162 int connection_id, const std::string& data) {
203 DCHECK_EQ(connection_id, connection_id_); 163 DCHECK_EQ(connection_id, connection_id_);
204 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnMessage(data)); 164 ClearNullFrontendBindings();
165 for (const auto& it : frontend_bindings_)
166 it->client()->OnMessage(data);
205 } 167 }
206 168
207 void Server::OnClose(int connection_id) { 169 void Server::OnClose(int connection_id) {
208 if (connection_id != connection_id_) 170 if (connection_id != connection_id_)
209 return; 171 return;
210 connection_id_ = kNotConnected; 172 connection_id_ = kNotConnected;
211 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnDisconnect()); 173 ClearNullFrontendBindings();
174 for (const auto& it : frontend_bindings_)
175 it->client()->OnDisconnect();
212 } 176 }
213 177
214 void Server::Register(InspectorServerImpl* client) { 178 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. 179 CloseAllAgentConnections(); // Assume caller represents a new app.
232 180
233 // TODO(eseidel): Early-out here if we're already bound to the right port. 181 // TODO(eseidel): Early-out here if we're already bound to the right port.
234 web_server_.reset(); 182 web_server_.reset();
235 scoped_ptr<net::ServerSocket> server_socket( 183 scoped_ptr<net::ServerSocket> server_socket(
236 new net::TCPServerSocket(NULL, net::NetLog::Source())); 184 new net::TCPServerSocket(NULL, net::NetLog::Source()));
237 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); 185 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1);
238 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); 186 web_server_.reset(new net::HttpServer(server_socket.Pass(), this));
187 callback.Run();
239 } 188 }
240 189
241 void Server::SendMessage(const mojo::String& message) { 190 void Server::SendMessage(const mojo::String& message) {
242 if (connection_id_ == kNotConnected) 191 if (connection_id_ == kNotConnected)
243 return; 192 return;
244 web_server_->SendOverWebSocket(connection_id_, message); 193 web_server_->SendOverWebSocket(connection_id_, message);
245 } 194 }
246 195
247 } // namespace inspector 196 } // namespace inspector
248 } // namespace sky 197 } // namespace sky
249 198
250 MojoResult MojoMain(MojoHandle shell_handle) { 199 MojoResult MojoMain(MojoHandle shell_handle) {
251 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); 200 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server);
252 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); 201 runner.set_message_loop_type(base::MessageLoop::TYPE_IO);
253 return runner.Run(shell_handle); 202 return runner.Run(shell_handle);
254 } 203 }
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/binding.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698