| Index: ui/devtools/devtools_server.cc
|
| diff --git a/ui/devtools/devtools_server.cc b/ui/devtools/devtools_server.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9bdb0daf35e2be50ffdb59be585d10dded1e7a67
|
| --- /dev/null
|
| +++ b/ui/devtools/devtools_server.cc
|
| @@ -0,0 +1,147 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "ui/devtools/devtools_server.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#include "base/values.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "net/log/net_log.h"
|
| +#include "net/server/http_server_request_info.h"
|
| +#include "net/socket/server_socket.h"
|
| +#include "net/socket/tcp_server_socket.h"
|
| +
|
| +namespace ui {
|
| +namespace devtools {
|
| +
|
| +namespace {
|
| +// Each attached client has an id associated with it, so that
|
| +// the user can inspect based on the id
|
| +uint32_t ClientIds = 0;
|
| +const char kChromeDeveloperToolsPrefix[] =
|
| + "chrome-devtools://devtools/bundled/inspector.html?ws=";
|
| +}
|
| +
|
| +UiDevToolsServer::UiDevToolsServer()
|
| + : thread_(new base::Thread("UiDevToolsServerThread")) {}
|
| +
|
| +UiDevToolsServer::~UiDevToolsServer() {}
|
| +
|
| +void UiDevToolsServer::Start(const std::string& address_string, uint16_t port) {
|
| + if (thread_ && thread_->IsRunning()) {
|
| + return;
|
| + }
|
| + // Start IO thread upon which all the methods will run
|
| + base::Thread::Options options;
|
| + options.message_loop_type = base::MessageLoop::TYPE_IO;
|
| + if (thread_->StartWithOptions(options)) {
|
| + thread_->task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&UiDevToolsServer::StartServer,
|
| + base::Unretained(this), address_string, port));
|
| + }
|
| +} // namespace
|
| +
|
| +void UiDevToolsServer::StartServer(const std::string& address_string,
|
| + uint16_t port) {
|
| + std::unique_ptr<net::ServerSocket> socket(
|
| + new net::TCPServerSocket(nullptr, net::NetLog::Source()));
|
| + if (socket->ListenWithAddressAndPort(address_string, port, 1) != net::OK) {
|
| + return;
|
| + }
|
| + server_.reset(new net::HttpServer(std::move(socket), this));
|
| +}
|
| +
|
| +void UiDevToolsServer::SendOverWebSocket(int connection_id,
|
| + const String& message) {
|
| + thread_->task_runner()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&net::HttpServer::SendOverWebSocket,
|
| + base::Unretained(server_.get()), connection_id, message));
|
| +}
|
| +
|
| +void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) {
|
| + clients_[ClientIds++] = std::move(client);
|
| +}
|
| +
|
| +// HttpServer::Delegate Implementation
|
| +void UiDevToolsServer::OnConnect(int connection_id) {
|
| + NOTIMPLEMENTED();
|
| +}
|
| +
|
| +void UiDevToolsServer::OnHttpRequest(int connection_id,
|
| + const net::HttpServerRequestInfo& info) {
|
| + // Display a simple html page with all the clients and the corresponding
|
| + // devtools links
|
| + // TODO(mhashmi): Remove and display all clients under chrome://inspect/#other
|
| + if (info.path.empty() || info.path == "/") {
|
| + std::string clientHTML = "<html>";
|
| + clientHTML +=
|
| + "<h3>Copy paste the corresponding links in your browser to inspect "
|
| + "them:</h3>";
|
| + for (ClientsMap::iterator it = clients_.begin(); it != clients_.end();
|
| + it++) {
|
| + net::IPEndPoint ip;
|
| + server_->GetLocalAddress(&ip);
|
| + clientHTML += base::StringPrintf(
|
| + "<p><strong>%s</strong> (%s%s/%d)</p>", it->second->name_.c_str(),
|
| + kChromeDeveloperToolsPrefix, ip.ToString().c_str(), it->first);
|
| + }
|
| + clientHTML += "</html>";
|
| + thread_->task_runner()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()),
|
| + connection_id, clientHTML, "text/html"));
|
| + }
|
| +}
|
| +
|
| +void UiDevToolsServer::OnWebSocketRequest(
|
| + int connection_id,
|
| + const net::HttpServerRequestInfo& info) {
|
| + if (info.path.empty())
|
| + return;
|
| + int target_id;
|
| + if (!base::StringToInt(info.path.substr(1), &target_id)) {
|
| + return;
|
| + }
|
| + ClientsMap::iterator it = clients_.find(target_id);
|
| + if (it == clients_.end())
|
| + return;
|
| + if (it->second->connected_client_id > 0)
|
| + return;
|
| + it->second->connected_client_id = connection_id;
|
| + connections_[connection_id] = target_id;
|
| + thread_->task_runner()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&net::HttpServer::AcceptWebSocket,
|
| + base::Unretained(server_.get()), connection_id, info));
|
| +}
|
| +
|
| +void UiDevToolsServer::OnWebSocketMessage(int connection_id,
|
| + const std::string& data) {
|
| + ConnectionsMap::iterator it = connections_.find(connection_id);
|
| + if (it == connections_.end())
|
| + return;
|
| + ClientsMap::iterator clientIt = clients_.find(it->second);
|
| + if (clientIt == clients_.end())
|
| + return;
|
| + thread_->task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&UiDevToolsClient::Dispatch,
|
| + base::Unretained(clientIt->second.get()), data));
|
| +}
|
| +
|
| +void UiDevToolsServer::OnClose(int connection_id) {
|
| + ConnectionsMap::iterator it = connections_.find(connection_id);
|
| + if (it == connections_.end())
|
| + return;
|
| + int client_id = it->second;
|
| + connections_.erase(it);
|
| + ClientsMap::iterator clientIt = clients_.find(client_id);
|
| + clientIt->second->connected_client_id = -1;
|
| +}
|
| +
|
| +} // namespace devtools
|
| +} // namespace ui
|
|
|