| 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 "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/common/weak_binding_set.h" |
| 7 #include "mojo/common/weak_interface_ptr_set.h" |
| 7 #include "mojo/public/c/system/main.h" | 8 #include "mojo/public/c/system/main.h" |
| 8 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
| 9 #include "mojo/public/cpp/application/application_impl.h" | 10 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "mojo/public/cpp/bindings/binding.h" | 11 #include "mojo/public/cpp/bindings/binding.h" |
| 11 #include "net/server/http_server.h" | 12 #include "net/server/http_server.h" |
| 12 #include "net/socket/tcp_server_socket.h" | 13 #include "net/socket/tcp_server_socket.h" |
| 13 #include "sky/services/inspector/inspector.mojom.h" | 14 #include "sky/services/inspector/inspector.mojom.h" |
| 14 | 15 |
| 15 | 16 |
| 16 namespace sky { | 17 namespace sky { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 31 virtual ~Server(); | 32 virtual ~Server(); |
| 32 | 33 |
| 33 private: | 34 private: |
| 34 // mojo::ApplicationDelegate: | 35 // mojo::ApplicationDelegate: |
| 35 void Initialize(mojo::ApplicationImpl* app) override { | 36 void Initialize(mojo::ApplicationImpl* app) override { |
| 36 } | 37 } |
| 37 bool ConfigureIncomingConnection( | 38 bool ConfigureIncomingConnection( |
| 38 mojo::ApplicationConnection* connection) override { | 39 mojo::ApplicationConnection* connection) override { |
| 39 connection->AddService<InspectorFrontend>(this); | 40 connection->AddService<InspectorFrontend>(this); |
| 40 connection->AddService<InspectorServer>(this); | 41 connection->AddService<InspectorServer>(this); |
| 42 // The application connecting to us may implement InspectorBackend, |
| 43 // attempt to establish a connection to find out. If it doesn't then this |
| 44 // pipe will close. |
| 45 InspectorBackendPtr backend; |
| 46 connection->ConnectToService(&backend); |
| 47 backends_.AddInterfacePtr(backend.Pass()); |
| 41 return true; | 48 return true; |
| 42 } | 49 } |
| 43 | 50 |
| 44 // InterfaceFactory<InspectorFrontend>: | 51 // InterfaceFactory<InspectorFrontend>: |
| 45 void Create(mojo::ApplicationConnection* connection, | 52 void Create(mojo::ApplicationConnection* connection, |
| 46 mojo::InterfaceRequest<InspectorFrontend> request) override { | 53 mojo::InterfaceRequest<InspectorFrontend> request) override { |
| 47 frontend_bindings_.AddBinding(this, request.Pass()); | 54 frontend_bindings_.AddBinding(this, request.Pass()); |
| 48 } | 55 } |
| 49 | 56 |
| 50 // InterfaceFactory<InspectorServer>: | 57 // InterfaceFactory<InspectorServer>: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 65 int connection_id, const net::HttpServerRequestInfo& info) override; | 72 int connection_id, const net::HttpServerRequestInfo& info) override; |
| 66 void OnWebSocketRequest( | 73 void OnWebSocketRequest( |
| 67 int connection_id, const net::HttpServerRequestInfo& info) override; | 74 int connection_id, const net::HttpServerRequestInfo& info) override; |
| 68 void OnWebSocketMessage( | 75 void OnWebSocketMessage( |
| 69 int connection_id, const std::string& data) override; | 76 int connection_id, const std::string& data) override; |
| 70 void OnClose(int connection_id) override; | 77 void OnClose(int connection_id) override; |
| 71 | 78 |
| 72 int connection_id_; | 79 int connection_id_; |
| 73 scoped_ptr<net::HttpServer> web_server_; | 80 scoped_ptr<net::HttpServer> web_server_; |
| 74 | 81 |
| 82 mojo::WeakInterfacePtrSet<InspectorBackend> backends_; |
| 75 mojo::WeakBindingSet<InspectorFrontend> frontend_bindings_; | 83 mojo::WeakBindingSet<InspectorFrontend> frontend_bindings_; |
| 76 mojo::Binding<InspectorServer> server_binding_; | 84 mojo::Binding<InspectorServer> server_binding_; |
| 77 | 85 |
| 78 DISALLOW_COPY_AND_ASSIGN(Server); | 86 DISALLOW_COPY_AND_ASSIGN(Server); |
| 79 }; | 87 }; |
| 80 | 88 |
| 81 Server::~Server() | 89 Server::~Server() |
| 82 { | 90 { |
| 83 } | 91 } |
| 84 | 92 |
| 85 void Server::OnConnect(int connection_id) { | 93 void Server::OnConnect(int connection_id) { |
| 86 } | 94 } |
| 87 | 95 |
| 88 void Server::OnHttpRequest( | 96 void Server::OnHttpRequest( |
| 89 int connection_id, const net::HttpServerRequestInfo& info) { | 97 int connection_id, const net::HttpServerRequestInfo& info) { |
| 90 web_server_->Send500(connection_id, "websockets protocol only"); | 98 web_server_->Send500(connection_id, "websockets protocol only"); |
| 91 } | 99 } |
| 92 | 100 |
| 93 void Server::OnWebSocketRequest( | 101 void Server::OnWebSocketRequest( |
| 94 int connection_id, const net::HttpServerRequestInfo& info) { | 102 int connection_id, const net::HttpServerRequestInfo& info) { |
| 95 if (connection_id_ != kNotConnected) { | 103 if (connection_id_ != kNotConnected) { |
| 96 web_server_->Close(connection_id); | 104 web_server_->Close(connection_id); |
| 97 return; | 105 return; |
| 98 } | 106 } |
| 99 web_server_->AcceptWebSocket(connection_id, info); | 107 web_server_->AcceptWebSocket(connection_id, info); |
| 100 connection_id_ = connection_id; | 108 connection_id_ = connection_id; |
| 101 frontend_bindings_.ForAllBindings( | 109 backends_.ForAllPtrs([](InspectorBackend* backend) { backend->OnConnect(); }); |
| 102 [](InspectorFrontend::Client* client) { client->OnConnect(); }); | |
| 103 } | 110 } |
| 104 | 111 |
| 105 void Server::OnWebSocketMessage( | 112 void Server::OnWebSocketMessage( |
| 106 int connection_id, const std::string& data) { | 113 int connection_id, const std::string& data) { |
| 107 DCHECK_EQ(connection_id, connection_id_); | 114 DCHECK_EQ(connection_id, connection_id_); |
| 108 frontend_bindings_.ForAllBindings( | 115 backends_.ForAllPtrs( |
| 109 [data](InspectorFrontend::Client* client) { client->OnMessage(data); }); | 116 [data](InspectorBackend* backend) { backend->OnMessage(data); }); |
| 110 } | 117 } |
| 111 | 118 |
| 112 void Server::OnClose(int connection_id) { | 119 void Server::OnClose(int connection_id) { |
| 113 if (connection_id != connection_id_) | 120 if (connection_id != connection_id_) |
| 114 return; | 121 return; |
| 115 connection_id_ = kNotConnected; | 122 connection_id_ = kNotConnected; |
| 116 frontend_bindings_.ForAllBindings( | 123 backends_.ForAllPtrs( |
| 117 [](InspectorFrontend::Client* client) { client->OnDisconnect(); }); | 124 [](InspectorBackend* backend) { backend->OnDisconnect(); }); |
| 118 } | 125 } |
| 119 | 126 |
| 120 void Server::Listen(int32_t port, const mojo::Closure& callback) { | 127 void Server::Listen(int32_t port, const mojo::Closure& callback) { |
| 121 frontend_bindings_.CloseAllBindings(); // Assume caller represents a new app. | 128 backends_.CloseAll(); // Assume caller represents a new app. |
| 122 | 129 |
| 123 // 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. |
| 124 web_server_.reset(); | 131 web_server_.reset(); |
| 125 scoped_ptr<net::ServerSocket> server_socket( | 132 scoped_ptr<net::ServerSocket> server_socket( |
| 126 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 133 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 127 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); | 134 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); |
| 128 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 135 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
| 129 callback.Run(); | 136 callback.Run(); |
| 130 } | 137 } |
| 131 | 138 |
| 132 void Server::SendMessage(const mojo::String& message) { | 139 void Server::SendMessage(const mojo::String& message) { |
| 133 if (connection_id_ == kNotConnected) | 140 if (connection_id_ == kNotConnected) |
| 134 return; | 141 return; |
| 135 web_server_->SendOverWebSocket(connection_id_, message); | 142 web_server_->SendOverWebSocket(connection_id_, message); |
| 136 } | 143 } |
| 137 | 144 |
| 138 } // namespace inspector | 145 } // namespace inspector |
| 139 } // namespace sky | 146 } // namespace sky |
| 140 | 147 |
| 141 MojoResult MojoMain(MojoHandle shell_handle) { | 148 MojoResult MojoMain(MojoHandle shell_handle) { |
| 142 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); | 149 mojo::ApplicationRunnerChromium runner(new sky::inspector::Server); |
| 143 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | 150 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); |
| 144 return runner.Run(shell_handle); | 151 return runner.Run(shell_handle); |
| 145 } | 152 } |
| OLD | NEW |