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

Unified Diff: ui/devtools/devtools_server.cc

Issue 2374513002: Add ui devtools server (Closed)
Patch Set: Fix all header if directives Created 4 years, 2 months 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 side-by-side diff with in-line comments
Download patch
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..5bdc90e735f4aa965163a63d881a778762c836b3
--- /dev/null
+++ b/ui/devtools/devtools_server.cc
@@ -0,0 +1,169 @@
+// 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/command_line.h"
+#include "base/format_macros.h"
+#include "base/memory/ptr_util.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"
+#include "ui/devtools/switches.h"
+
+namespace ui {
+namespace devtools {
+
+namespace {
+constexpr int kBacklog = 1;
+const char kChromeDeveloperToolsPrefix[] =
+ "chrome-devtools://devtools/bundled/inspector.html?ws=";
+} // namespace
+
+UiDevToolsServer::UiDevToolsServer()
+ : thread_(new base::Thread("UiDevToolsServerThread")) {}
+
+UiDevToolsServer::~UiDevToolsServer() {}
+
+// static
+std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create() {
+ std::unique_ptr<UiDevToolsServer> server;
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableUiDevTools)) {
+ // TODO(mhashmi): Change port if more than one inspectable clients
+ int port;
+ base::StringToInt(
+ base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
+ kEnableUiDevTools),
+ &port);
sadrul 2016/10/18 01:09:15 Use some default port if the flag is there, but it
Sarmad Hashmi 2016/10/18 02:40:48 Done.
+ server.reset(new UiDevToolsServer());
+ server->Start("127.0.0.1", port);
+ }
+ return server;
+}
+
+void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) {
+ clients_.push_back(std::move(client));
sadrul 2016/10/18 01:09:15 It looks like we are never removing the client fro
Sarmad Hashmi 2016/10/18 02:40:48 It would be possible if in the future we detach cl
sadrul 2016/10/18 20:29:37 I think this is OK for now.
+}
+
+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::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));
+ }
+}
+
+void UiDevToolsServer::StartServer(const std::string& address_string,
+ uint16_t port) {
+ std::unique_ptr<net::ServerSocket> socket(
+ new net::TCPServerSocket(nullptr, net::NetLogSource()));
+ if (socket->ListenWithAddressAndPort(address_string, port, kBacklog) !=
sadrul 2016/10/18 01:09:15 Define kBacklog just before this line here.
Sarmad Hashmi 2016/10/18 02:40:49 Done.
+ net::OK)
+ return;
+ server_ = base::MakeUnique<net::HttpServer>(std::move(socket), this);
+}
+
+// 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>";
+ net::IPEndPoint ip;
+ server_->GetLocalAddress(&ip);
+ for (ClientsList::size_type i = 0; i != clients_.size(); i++) {
+ clientHTML += base::StringPrintf(
+ "<p><strong>%s</strong> (%s%s/%" PRIuS ")</p>",
+ clients_[i]->name_.c_str(), kChromeDeveloperToolsPrefix,
+ ip.ToString().c_str(), i);
+ }
+ 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) {
+ size_t target_id;
+ if (info.path.empty() ||
+ !base::StringToSizeT(info.path.substr(1), &target_id) ||
+ target_id > clients_.size())
+ return;
+
+ UiDevToolsClient* client = clients_[target_id].get();
+ // Only one user can inspect the client at a time
+ if (client->connected())
+ return;
+ client->connection_id_ = connection_id;
sadrul 2016/10/18 01:09:15 client->set_connection_id(connection_id) instead,
Sarmad Hashmi 2016/10/18 02:40:48 Done.
+ connections_[connection_id] = client;
+ 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);
+ DCHECK(it != connections_.end());
+ UiDevToolsClient* client = it->second;
+ if (!client) {
sadrul 2016/10/18 01:09:15 |client| should never be null here, right?
Sarmad Hashmi 2016/10/18 02:40:49 Can't it be null if the client is destroyed but th
sadrul 2016/10/18 20:29:37 Since UiDevToolsServer owns the client (and also t
Sarmad Hashmi 2016/10/19 23:56:02 Done.
+ // Client shut down, close the connection
+ connections_.erase(it);
+ thread_->task_runner()->PostTask(
+ FROM_HERE, base::Bind(&net::HttpServer::Close,
+ base::Unretained(server_.get()), connection_id));
+ return;
+ }
+
+ thread_->task_runner()->PostTask(
+ FROM_HERE,
+ base::Bind(&UiDevToolsClient::Dispatch, base::Unretained(client), data));
+}
+
+void UiDevToolsServer::OnClose(int connection_id) {
+ ConnectionsMap::iterator it = connections_.find(connection_id);
+ DCHECK(it != connections_.end());
+ UiDevToolsClient* client = it->second;
+ if (client)
sadrul 2016/10/18 01:09:15 ditto re client being non-null.
Sarmad Hashmi 2016/10/18 02:40:48 Replied above.
Sarmad Hashmi 2016/10/19 23:56:02 Done.
+ client->connection_id_ = UiDevToolsClient::kNotConnected;
+ connections_.erase(it);
+}
+
+} // namespace devtools
+} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698