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

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

Issue 710043004: Make it possible to have multiple InspectorBackends (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Split out ServerImpl, somehow breaks JS connections... 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
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 "base/lazy_instance.h"
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 namespace {
18 const int kNotConnected = -1;
19 }
20
21 class ServerImpl : public mojo::InterfaceImpl<InspectorServer> {
22 public:
23 class Delegate {
24 public:
25 virtual void SetImpl(ServerImpl* impl) = 0;
26 virtual void Listen(int32_t port) = 0;
27 };
28 ServerImpl(Delegate* delegate) : delegate_(delegate) {
29 delegate_->SetImpl(this);
Aaron Boodman 2014/11/11 16:12:50 If two InspectorServer interfaces are requested, t
eseidel 2014/11/11 18:15:26 Done.
30 }
31 ~ServerImpl() {
32 delegate_->SetImpl(nullptr);
33 }
34 void OnShutdown() {
35 delete this;
36 }
37
38 // InspectorServer:
39 void Listen(int32_t port, const mojo::Callback<void()>& callback) override;
40
41 private:
42 // InterfaceImpl:
43 void OnConnectionError() override {
44 delete this;
Aaron Boodman 2014/11/11 16:12:50 If you are just going to delete yourself on error,
eseidel 2014/11/11 18:15:26 I can't, unfortunately, since then I can't hae the
45 }
46
47 Delegate* delegate_;
48 };
49
50 class Server : public mojo::ApplicationDelegate,
51 public InspectorFrontendImpl::Delegate,
52 public ServerImpl::Delegate,
53 public mojo::InterfaceFactory<InspectorFrontendImpl>,
54 public mojo::InterfaceFactory<ServerImpl>,
55 public net::HttpServer::Delegate {
15 public: 56 public:
16 Server() {} 57 Server() : connection_id_(kNotConnected) {}
17 virtual ~Server() {} 58 virtual ~Server();
18 59
19 private: 60 private:
20 // Overridden from mojo::ApplicationDelegate: 61 // mojo::ApplicationDelegate:
21 virtual void Initialize(mojo::ApplicationImpl* app) override { 62 void Initialize(mojo::ApplicationImpl* app) override {
22 } 63 }
23 64 bool ConfigureIncomingConnection(
24 virtual bool ConfigureIncomingConnection(
25 mojo::ApplicationConnection* connection) override { 65 mojo::ApplicationConnection* connection) override {
26 connection->AddService(&frontend_factory_); 66 connection->AddService(this);
27 return true; 67 return true;
28 } 68 }
29 69
30 InspectorFrontendFactory frontend_factory_; 70 // InterfaceFactory<InspectorFrontendImpl>:
71 void Create(mojo::ApplicationConnection* connection,
72 mojo::InterfaceRequest<InspectorFrontendImpl> request) override {
73 printf("Create InspectorFrontendImpl\n");
74 WeakBindToRequest(new InspectorFrontendImpl(this), &request);
75 }
76
77 // InterfaceFactory<InspectorServer>:
78 void Create(mojo::ApplicationConnection* connection,
79 mojo::InterfaceRequest<ServerImpl> request) override {
80 printf("Create ServerImpl\n");
81 WeakBindToRequest(new ServerImpl(this), &request);
82 }
83
84 // ServerImpl::Delegate:
85 void SetImpl(ServerImpl* server_impl) override {
86 server_impl_ = server_impl;
87 }
88 void Listen(int32_t port) override;
89
90 // InspectorFrontendImpl::Delegate:
91 void Register(InspectorFrontendImpl*) override;
92 void Unregister(InspectorFrontendImpl*) override;
93
94 void SendMessage(const mojo::String& message) override;
95
96 // From 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 // TODO(eseidel): The server should control the lifetime of these agents.
111 ObserverList<InspectorFrontendImpl> agents_;
112 ServerImpl* server_impl_;
31 113
32 DISALLOW_COPY_AND_ASSIGN(Server); 114 DISALLOW_COPY_AND_ASSIGN(Server);
33 }; 115 };
34 116
117 Server::~Server()
118 {
119 CloseAllAgentConnections();
120 }
121
122 void Server::CloseAllAgentConnections() {
123 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, OnShutdown());
124 server_impl_->OnShutdown();
125 }
126
127 void Server::OnConnect(int connection_id) {
128 }
129
130 void Server::OnHttpRequest(
131 int connection_id, const net::HttpServerRequestInfo& info) {
132 web_server_->Send500(connection_id, "websockets protocol only");
133 }
134
135 void Server::OnWebSocketRequest(
136 int connection_id, const net::HttpServerRequestInfo& info) {
137 if (connection_id_ != kNotConnected) {
138 web_server_->Close(connection_id);
139 return;
140 }
141 web_server_->AcceptWebSocket(connection_id, info);
142 connection_id_ = connection_id;
143 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnConnect());
144 }
145
146 void Server::OnWebSocketMessage(
147 int connection_id, const std::string& data) {
148 DCHECK_EQ(connection_id, connection_id_);
149 printf("OnWebSocketMessage\n");
150 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnMessage(data));
151 }
152
153 void Server::OnClose(int connection_id) {
154 if (connection_id != connection_id_)
155 return;
156 connection_id_ = kNotConnected;
157 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnDisconnect());
158 }
159
160 void Server::Register(InspectorFrontendImpl* agent) {
161 printf("Server::Register\n");
162 agents_.AddObserver(agent);
163 }
164
165 void Server::Unregister(InspectorFrontendImpl* agent) {
166 printf("Server::Unregister\n");
167 agents_.RemoveObserver(agent);
168 }
169
170 void ServerImpl::Listen(int32_t port, const mojo::Callback<void()>& callback) {
171 delegate_->Listen(port);
172 callback.Run();
173 }
174
175 void Server::Listen(int32_t port) {
176 CloseAllAgentConnections(); // Assume caller represents a new app.
177
178 // TODO(eseidel): Early-out here if we're already bound to the right port.
179 web_server_.reset();
180 scoped_ptr<net::ServerSocket> server_socket(
181 new net::TCPServerSocket(NULL, net::NetLog::Source()));
182 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1);
183 web_server_.reset(new net::HttpServer(server_socket.Pass(), this));
184 }
185
186 void Server::SendMessage(const mojo::String& message) {
187 if (connection_id_ == kNotConnected)
188 return;
189 web_server_->SendOverWebSocket(connection_id_, message);
190 }
191
35 } // namespace inspector 192 } // namespace inspector
36 } // namespace sky 193 } // namespace sky
37 194
38 MojoResult MojoMain(MojoHandle shell_handle) { 195 MojoResult MojoMain(MojoHandle shell_handle) {
39 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); 196 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server);
40 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); 197 runner.set_message_loop_type(base::MessageLoop::TYPE_IO);
41 return runner.Run(shell_handle); 198 return runner.Run(shell_handle);
42 } 199 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698