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