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

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: Actually works 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 | « sky/services/inspector/inspector_frontend_impl.cc ('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 "base/lazy_instance.h"
abarth-chromium 2014/11/11 02:53:57 I don't think this is needed now.
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 Server : public mojo::ApplicationDelegate,
22 public InspectorFrontendImpl::Delegate,
23 public mojo::InterfaceFactory<InspectorFrontendImpl>,
24 public net::HttpServer::Delegate {
15 public: 25 public:
16 Server() {} 26 Server() : connection_id_(kNotConnected) {}
17 virtual ~Server() {} 27 virtual ~Server() {}
18 28
19 private: 29 private:
20 // Overridden from mojo::ApplicationDelegate: 30 // mojo::ApplicationDelegate:
21 virtual void Initialize(mojo::ApplicationImpl* app) override { 31 void Initialize(mojo::ApplicationImpl* app) override {
22 } 32 }
23 33 bool ConfigureIncomingConnection(
24 virtual bool ConfigureIncomingConnection(
25 mojo::ApplicationConnection* connection) override { 34 mojo::ApplicationConnection* connection) override {
26 connection->AddService(&frontend_factory_); 35 connection->AddService(this);
27 return true; 36 return true;
28 } 37 }
29 38
30 InspectorFrontendFactory frontend_factory_; 39 // InterfaceFactory<InspectorFrontendImpl>:
40 void Create(mojo::ApplicationConnection* connection,
41 mojo::InterfaceRequest<InspectorFrontendImpl> request) override {
42 BindToRequest(new InspectorFrontendImpl(this), &request);
43 }
44
45 // InspectorFrontendImpl::Delegate:
46 void Register(InspectorFrontendImpl*) override;
47 void Unregister(InspectorFrontendImpl*) override;
48
49 void Listen(int32_t port) override;
50 void SendMessage(const mojo::String& message) override;
51
52 // From net::HttpServer::Delegate
53 void OnConnect(int connection_id) override;
54 void OnHttpRequest(
55 int connection_id, const net::HttpServerRequestInfo& info) override;
56 void OnWebSocketRequest(
57 int connection_id, const net::HttpServerRequestInfo& info) override;
58 void OnWebSocketMessage(
59 int connection_id, const std::string& data) override;
60 void OnClose(int connection_id) override;
61
62 int connection_id_;
63 scoped_ptr<net::HttpServer> web_server_;
64 // TODO(eseidel): The server should control the lifetime of these agents.
65 ObserverList<InspectorFrontendImpl> agents_;
31 66
32 DISALLOW_COPY_AND_ASSIGN(Server); 67 DISALLOW_COPY_AND_ASSIGN(Server);
33 }; 68 };
34 69
70 void Server::OnConnect(int connection_id) {
71 }
72
73 void Server::OnHttpRequest(
74 int connection_id, const net::HttpServerRequestInfo& info) {
75 web_server_->Send500(connection_id, "websockets protocol only");
76 }
77
78 void Server::OnWebSocketRequest(
79 int connection_id, const net::HttpServerRequestInfo& info) {
80 if (connection_id_ != kNotConnected) {
81 web_server_->Close(connection_id);
82 return;
83 }
84 web_server_->AcceptWebSocket(connection_id, info);
85 connection_id_ = connection_id;
86 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnConnect());
87 }
88
89 void Server::OnWebSocketMessage(
90 int connection_id, const std::string& data) {
91 DCHECK_EQ(connection_id, connection_id_);
92 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnMessage(data));
93 }
94
95 void Server::OnClose(int connection_id) {
96 if (connection_id != connection_id_)
97 return;
98 connection_id_ = kNotConnected;
99 FOR_EACH_OBSERVER(InspectorFrontendImpl, agents_, client()->OnDisconnect());
100 }
101
102 void Server::Register(InspectorFrontendImpl* agent) {
103 agents_.AddObserver(agent);
104 }
105
106 void Server::Unregister(InspectorFrontendImpl* agent) {
107 agents_.RemoveObserver(agent);
108 }
109
110 void Server::Listen(int32_t port) {
111 web_server_.reset(); // Tear down any existing connection first.
112 scoped_ptr<net::ServerSocket> server_socket(
113 new net::TCPServerSocket(NULL, net::NetLog::Source()));
114 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1);
115 web_server_.reset(new net::HttpServer(server_socket.Pass(), this));
116 }
117
118 void Server::SendMessage(const mojo::String& message) {
119 if (connection_id_ == kNotConnected)
120 return;
121 web_server_->SendOverWebSocket(connection_id_, message);
122 }
123
35 } // namespace inspector 124 } // namespace inspector
36 } // namespace sky 125 } // namespace sky
37 126
38 MojoResult MojoMain(MojoHandle shell_handle) { 127 MojoResult MojoMain(MojoHandle shell_handle) {
39 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); 128 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server);
40 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); 129 runner.set_message_loop_type(base::MessageLoop::TYPE_IO);
41 return runner.Run(shell_handle); 130 return runner.Run(shell_handle);
42 } 131 }
OLDNEW
« no previous file with comments | « sky/services/inspector/inspector_frontend_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698