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

Side by Side Diff: components/ui_devtools/devtools_server.cc

Issue 2502863002: Add UI DevTools under chrome://inspect/#other (Closed)
Patch Set: feedback Created 4 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « components/ui_devtools/devtools_server.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/ui_devtools/devtools_server.h" 5 #include "components/ui_devtools/devtools_server.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "components/ui_devtools/switches.h" 15 #include "components/ui_devtools/switches.h"
16 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
17 #include "net/log/net_log.h" 17 #include "net/log/net_log.h"
18 #include "net/server/http_server_request_info.h" 18 #include "net/server/http_server_request_info.h"
19 #include "net/socket/server_socket.h" 19 #include "net/socket/server_socket.h"
20 #include "net/socket/tcp_server_socket.h" 20 #include "net/socket/tcp_server_socket.h"
21 21
22 namespace ui { 22 namespace ui {
23 namespace devtools { 23 namespace devtools {
24 24
25 namespace { 25 namespace {
26 const char kChromeDeveloperToolsPrefix[] = 26 const char kChromeDeveloperToolsPrefix[] =
27 "chrome-devtools://devtools/bundled/inspector.html?ws="; 27 "chrome-devtools://devtools/bundled/inspector.html?ws=";
28
29 bool IsUiDevToolsEnabled() {
30 return base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableUiDevTools);
31 }
32
33 int GetUiDevToolsPort() {
34 DCHECK(IsUiDevToolsEnabled());
35 constexpr int kDefaultPort = 9223;
36 int port;
37 if (!base::StringToInt(
38 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
39 kEnableUiDevTools),
40 &port))
41 port = kDefaultPort;
42 return port;
43 }
44
28 } // namespace 45 } // namespace
29 46
47 UiDevToolsServer* UiDevToolsServer::devtools_server_ = nullptr;
48
30 UiDevToolsServer::UiDevToolsServer( 49 UiDevToolsServer::UiDevToolsServer(
31 scoped_refptr<base::SingleThreadTaskRunner> task_runner) 50 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
32 : task_runner_(task_runner) { 51 : task_runner_(task_runner) {
52 DCHECK(!devtools_server_);
53 devtools_server_ = this;
33 if (task_runner_) 54 if (task_runner_)
34 return; 55 return;
35 // If task_runner not passed in, create an I/O thread the server can run on 56 // If task_runner not passed in, create an I/O thread the server can run on
36 thread_.reset(new base::Thread("UiDevToolsServerThread")); 57 thread_.reset(new base::Thread("UiDevToolsServerThread"));
37 base::Thread::Options options; 58 base::Thread::Options options;
38 options.message_loop_type = base::MessageLoop::TYPE_IO; 59 options.message_loop_type = base::MessageLoop::TYPE_IO;
39 CHECK(thread_->StartWithOptions(options)); 60 CHECK(thread_->StartWithOptions(options));
40 task_runner_ = thread_->task_runner(); 61 task_runner_ = thread_->task_runner();
41 } 62 }
42 63
43 UiDevToolsServer::~UiDevToolsServer() {} 64 UiDevToolsServer::~UiDevToolsServer() {
65 devtools_server_ = nullptr;
66 }
44 67
45 // static 68 // static
46 std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create( 69 std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create(
47 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 70 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
48 std::unique_ptr<UiDevToolsServer> server; 71 std::unique_ptr<UiDevToolsServer> server;
49 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableUiDevTools)) { 72 if (IsUiDevToolsEnabled() && !devtools_server_) {
50 // TODO(mhashmi): Change port if more than one inspectable clients 73 // TODO(mhashmi): Change port if more than one inspectable clients
51 int port = 9223; // Default port is 9223
52 base::StringToInt(
53 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
54 kEnableUiDevTools),
55 &port);
56 server.reset(new UiDevToolsServer(task_runner)); 74 server.reset(new UiDevToolsServer(task_runner));
57 server->Start("127.0.0.1", port); 75 server->Start("127.0.0.1", GetUiDevToolsPort());
58 } 76 }
59 return server; 77 return server;
60 } 78 }
61 79
80 // static
81 std::vector<UiDevToolsServer::NameUrlPair>
82 UiDevToolsServer::GetClientNamesAndUrls() {
83 std::vector<NameUrlPair> pairs;
84 if (!devtools_server_)
85 return pairs;
86
87 for (ClientsList::size_type i = 0; i != devtools_server_->clients_.size();
88 i++) {
89 pairs.push_back(std::pair<std::string, std::string>(
90 devtools_server_->clients_[i]->name(),
91 base::StringPrintf("%slocalhost:%d/%" PRIuS,
92 kChromeDeveloperToolsPrefix, GetUiDevToolsPort(),
93 i)));
94 }
95 return pairs;
96 }
97
62 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) { 98 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) {
63 clients_.push_back(std::move(client)); 99 clients_.push_back(std::move(client));
64 } 100 }
65 101
66 void UiDevToolsServer::SendOverWebSocket(int connection_id, 102 void UiDevToolsServer::SendOverWebSocket(int connection_id,
67 const String& message) { 103 const String& message) {
68 task_runner_->PostTask( 104 task_runner_->PostTask(
69 FROM_HERE, 105 FROM_HERE,
70 base::Bind(&net::HttpServer::SendOverWebSocket, 106 base::Bind(&net::HttpServer::SendOverWebSocket,
71 base::Unretained(server_.get()), connection_id, message)); 107 base::Unretained(server_.get()), connection_id, message));
(...skipping 17 matching lines...) Expand all
89 server_ = base::MakeUnique<net::HttpServer>(std::move(socket), this); 125 server_ = base::MakeUnique<net::HttpServer>(std::move(socket), this);
90 } 126 }
91 127
92 // HttpServer::Delegate Implementation 128 // HttpServer::Delegate Implementation
93 void UiDevToolsServer::OnConnect(int connection_id) { 129 void UiDevToolsServer::OnConnect(int connection_id) {
94 NOTIMPLEMENTED(); 130 NOTIMPLEMENTED();
95 } 131 }
96 132
97 void UiDevToolsServer::OnHttpRequest(int connection_id, 133 void UiDevToolsServer::OnHttpRequest(int connection_id,
98 const net::HttpServerRequestInfo& info) { 134 const net::HttpServerRequestInfo& info) {
99 // Display a simple html page with all the clients and the corresponding 135 NOTIMPLEMENTED();
100 // devtools links
101 // TODO(mhashmi): Remove and display all clients under chrome://inspect/#other
102 if (info.path.empty() || info.path == "/") {
103 std::string clientHTML = "<html>";
104 clientHTML +=
105 "<h3>Copy paste the corresponding links in your browser to inspect "
106 "them:</h3>";
107 net::IPEndPoint ip;
108 server_->GetLocalAddress(&ip);
109 for (ClientsList::size_type i = 0; i != clients_.size(); i++) {
110 clientHTML += base::StringPrintf(
111 "<p><strong>%s</strong> (%s%s/%" PRIuS ")</p>",
112 clients_[i]->name().c_str(), kChromeDeveloperToolsPrefix,
113 ip.ToString().c_str(), i);
114 }
115 clientHTML += "</html>";
116 task_runner_->PostTask(
117 FROM_HERE,
118 base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()),
119 connection_id, clientHTML, "text/html"));
120 return;
121 }
122 task_runner_->PostTask(
123 FROM_HERE, base::Bind(&net::HttpServer::Send404,
124 base::Unretained(server_.get()), connection_id));
125 } 136 }
126 137
127 void UiDevToolsServer::OnWebSocketRequest( 138 void UiDevToolsServer::OnWebSocketRequest(
128 int connection_id, 139 int connection_id,
129 const net::HttpServerRequestInfo& info) { 140 const net::HttpServerRequestInfo& info) {
130 size_t target_id = 0; 141 size_t target_id = 0;
131 if (info.path.empty() || 142 if (info.path.empty() ||
132 !base::StringToSizeT(info.path.substr(1), &target_id) || 143 !base::StringToSizeT(info.path.substr(1), &target_id) ||
133 target_id > clients_.size()) 144 target_id > clients_.size())
134 return; 145 return;
(...skipping 25 matching lines...) Expand all
160 if (it == connections_.end()) 171 if (it == connections_.end())
161 return; 172 return;
162 UiDevToolsClient* client = it->second; 173 UiDevToolsClient* client = it->second;
163 DCHECK(client); 174 DCHECK(client);
164 client->Disconnect(); 175 client->Disconnect();
165 connections_.erase(it); 176 connections_.erase(it);
166 } 177 }
167 178
168 } // namespace devtools 179 } // namespace devtools
169 } // namespace ui 180 } // namespace ui
OLDNEW
« no previous file with comments | « components/ui_devtools/devtools_server.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698