| 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 "sky/services/inspector/inspector_frontend_impl.h" | 5 #include "sky/services/inspector/inspector_frontend_impl.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "net/server/http_server.h" | 8 #include "net/server/http_server.h" |
| 9 #include "net/socket/tcp_server_socket.h" | 9 #include "net/socket/tcp_server_socket.h" |
| 10 | 10 |
| 11 namespace sky { | 11 namespace sky { |
| 12 namespace inspector { | 12 namespace inspector { |
| 13 namespace { | 13 namespace { |
| 14 const int kNotConnected = -1; | 14 const int kNotConnected = -1; |
| 15 static base::LazyInstance<std::map<int, InspectorFronendImpl*>> g_servers = | 15 static base::LazyInstance<std::map<int, InspectorFrontendImpl*>> g_servers = |
| 16 LAZY_INSTANCE_INITIALIZER; | 16 LAZY_INSTANCE_INITIALIZER; |
| 17 } | 17 } |
| 18 | 18 |
| 19 InspectorFronendImpl::InspectorFronendImpl() | 19 InspectorFrontendImpl::InspectorFrontendImpl() |
| 20 : connection_id_(kNotConnected) { | 20 : connection_id_(kNotConnected) { |
| 21 } | 21 } |
| 22 | 22 |
| 23 InspectorFronendImpl::~InspectorFronendImpl() { | 23 InspectorFrontendImpl::~InspectorFrontendImpl() { |
| 24 StopListening(); | 24 StopListening(); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void InspectorFronendImpl::OnConnect(int connection_id) { | 27 void InspectorFrontendImpl::OnConnect(int connection_id) { |
| 28 } | 28 } |
| 29 | 29 |
| 30 void InspectorFronendImpl::OnHttpRequest( | 30 void InspectorFrontendImpl::OnHttpRequest( |
| 31 int connection_id, const net::HttpServerRequestInfo& info) { | 31 int connection_id, const net::HttpServerRequestInfo& info) { |
| 32 web_server_->Send500(connection_id, "websockets protocol only"); | 32 web_server_->Send500(connection_id, "websockets protocol only"); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void InspectorFronendImpl::OnWebSocketRequest( | 35 void InspectorFrontendImpl::OnWebSocketRequest( |
| 36 int connection_id, const net::HttpServerRequestInfo& info) { | 36 int connection_id, const net::HttpServerRequestInfo& info) { |
| 37 if (connection_id_ != kNotConnected) { | 37 if (connection_id_ != kNotConnected) { |
| 38 web_server_->Close(connection_id); | 38 web_server_->Close(connection_id); |
| 39 return; | 39 return; |
| 40 } | 40 } |
| 41 web_server_->AcceptWebSocket(connection_id, info); | 41 web_server_->AcceptWebSocket(connection_id, info); |
| 42 connection_id_ = connection_id; | 42 connection_id_ = connection_id; |
| 43 client()->OnConnect(); | 43 client()->OnConnect(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void InspectorFronendImpl::OnWebSocketMessage( | 46 void InspectorFrontendImpl::OnWebSocketMessage( |
| 47 int connection_id, const std::string& data) { | 47 int connection_id, const std::string& data) { |
| 48 DCHECK_EQ(connection_id, connection_id_); | 48 DCHECK_EQ(connection_id, connection_id_); |
| 49 client()->OnMessage(data); | 49 client()->OnMessage(data); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void InspectorFronendImpl::OnClose(int connection_id) { | 52 void InspectorFrontendImpl::OnClose(int connection_id) { |
| 53 if (connection_id != connection_id_) | 53 if (connection_id != connection_id_) |
| 54 return; | 54 return; |
| 55 connection_id_ = kNotConnected; | 55 connection_id_ = kNotConnected; |
| 56 client()->OnDisconnect(); | 56 client()->OnDisconnect(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void InspectorFronendImpl::Listen(int32_t port) { | 59 void InspectorFrontendImpl::Listen(int32_t port) { |
| 60 Register(port); | 60 Register(port); |
| 61 scoped_ptr<net::ServerSocket> server_socket( | 61 scoped_ptr<net::ServerSocket> server_socket( |
| 62 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 62 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 63 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); | 63 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); |
| 64 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 64 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void InspectorFronendImpl::StopListening() { | 67 void InspectorFrontendImpl::StopListening() { |
| 68 if (!web_server_) | 68 if (!web_server_) |
| 69 return; | 69 return; |
| 70 web_server_.reset(); | 70 web_server_.reset(); |
| 71 Unregister(); | 71 Unregister(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void InspectorFronendImpl::Register(int port) { | 74 void InspectorFrontendImpl::Register(int port) { |
| 75 auto& servers = g_servers.Get(); | 75 auto& servers = g_servers.Get(); |
| 76 auto iter = servers.find(port); | 76 auto iter = servers.find(port); |
| 77 if (iter != servers.end()) | 77 if (iter != servers.end()) |
| 78 iter->second->StopListening(); | 78 iter->second->StopListening(); |
| 79 DCHECK(servers.find(port) == servers.end()); | 79 DCHECK(servers.find(port) == servers.end()); |
| 80 servers[port] = this; | 80 servers[port] = this; |
| 81 port_ = port; | 81 port_ = port; |
| 82 } | 82 } |
| 83 | 83 |
| 84 void InspectorFronendImpl::Unregister() { | 84 void InspectorFrontendImpl::Unregister() { |
| 85 DCHECK(g_servers.Get().find(port_)->second == this); | 85 DCHECK(g_servers.Get().find(port_)->second == this); |
| 86 g_servers.Get().erase(port_); | 86 g_servers.Get().erase(port_); |
| 87 port_ = kNotConnected; | 87 port_ = kNotConnected; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void InspectorFronendImpl::SendMessage(const mojo::String& message) { | 90 void InspectorFrontendImpl::SendMessage(const mojo::String& message) { |
| 91 if (connection_id_ == kNotConnected) | 91 if (connection_id_ == kNotConnected) |
| 92 return; | 92 return; |
| 93 web_server_->SendOverWebSocket(connection_id_, message); | 93 web_server_->SendOverWebSocket(connection_id_, message); |
| 94 } | 94 } |
| 95 | 95 |
| 96 } // namespace inspector | 96 } // namespace inspector |
| 97 } // namespace sky | 97 } // namespace sky |
| OLD | NEW |