Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 int port = 9223; // Default port is 9223 | |
| 36 base::StringToInt(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
|
sadrul
2016/11/17 18:40:57
StringToInt is expected to leave |port| unchanged
Sarmad Hashmi
2016/11/21 17:18:24
Just checked, it doesn't. Sets it to various value
sadrul
2016/11/21 18:26:39
I think we should set to default if StringToInt()
Sarmad Hashmi
2016/11/22 04:39:32
Done.
| |
| 37 kEnableUiDevTools), | |
| 38 &port); | |
| 39 return port; | |
| 40 } | |
| 41 | |
| 28 } // namespace | 42 } // namespace |
| 29 | 43 |
| 44 UiDevToolsServer* UiDevToolsServer::devtools_server_ = nullptr; | |
| 45 | |
| 30 UiDevToolsServer::UiDevToolsServer( | 46 UiDevToolsServer::UiDevToolsServer( |
| 31 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 47 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 32 : task_runner_(task_runner) { | 48 : task_runner_(task_runner) { |
| 33 if (task_runner_) | 49 if (task_runner_) |
|
sadrul
2016/11/17 18:40:57
You need to set |devtools_server_| before the earl
Sarmad Hashmi
2016/11/21 17:18:24
Done.
| |
| 34 return; | 50 return; |
| 35 // If task_runner not passed in, create an I/O thread the server can run on | 51 // If task_runner not passed in, create an I/O thread the server can run on |
| 36 thread_.reset(new base::Thread("UiDevToolsServerThread")); | 52 thread_.reset(new base::Thread("UiDevToolsServerThread")); |
| 37 base::Thread::Options options; | 53 base::Thread::Options options; |
| 38 options.message_loop_type = base::MessageLoop::TYPE_IO; | 54 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 39 CHECK(thread_->StartWithOptions(options)); | 55 CHECK(thread_->StartWithOptions(options)); |
| 40 task_runner_ = thread_->task_runner(); | 56 task_runner_ = thread_->task_runner(); |
| 57 devtools_server_ = this; | |
| 41 } | 58 } |
| 42 | 59 |
| 43 UiDevToolsServer::~UiDevToolsServer() {} | 60 UiDevToolsServer::~UiDevToolsServer() { |
| 61 devtools_server_ = nullptr; | |
| 62 } | |
| 44 | 63 |
| 45 // static | 64 // static |
| 46 std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create( | 65 std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create( |
| 47 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 66 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 48 std::unique_ptr<UiDevToolsServer> server; | 67 std::unique_ptr<UiDevToolsServer> server; |
| 49 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableUiDevTools)) { | 68 if (IsUiDevToolsEnabled() && !devtools_server_) { |
| 50 // TODO(mhashmi): Change port if more than one inspectable clients | 69 // 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)); | 70 server.reset(new UiDevToolsServer(task_runner)); |
| 57 server->Start("127.0.0.1", port); | 71 server->Start("127.0.0.1", GetUiDevToolsPort()); |
| 58 } | 72 } |
| 59 return server; | 73 return server; |
| 60 } | 74 } |
| 61 | 75 |
| 76 // static | |
| 77 std::vector<UiDevToolsServer::NameUrlPair> | |
| 78 UiDevToolsServer::GetClientNamesAndUrls() { | |
| 79 std::vector<NameUrlPair> pairs; | |
| 80 if (!devtools_server_) | |
| 81 return pairs; | |
| 82 | |
| 83 for (ClientsList::size_type i = 0; i != devtools_server_->clients_.size(); | |
| 84 i++) { | |
| 85 pairs.push_back(std::pair<std::string, std::string>( | |
| 86 devtools_server_->clients_[i]->name(), | |
| 87 base::StringPrintf("%slocalhost:%d/%" PRIuS, | |
| 88 kChromeDeveloperToolsPrefix, GetUiDevToolsPort(), | |
| 89 i))); | |
| 90 } | |
| 91 return pairs; | |
| 92 } | |
| 93 | |
| 62 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) { | 94 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) { |
| 63 clients_.push_back(std::move(client)); | 95 clients_.push_back(std::move(client)); |
| 64 } | 96 } |
| 65 | 97 |
| 66 void UiDevToolsServer::SendOverWebSocket(int connection_id, | 98 void UiDevToolsServer::SendOverWebSocket(int connection_id, |
| 67 const String& message) { | 99 const String& message) { |
| 68 task_runner_->PostTask( | 100 task_runner_->PostTask( |
| 69 FROM_HERE, | 101 FROM_HERE, |
| 70 base::Bind(&net::HttpServer::SendOverWebSocket, | 102 base::Bind(&net::HttpServer::SendOverWebSocket, |
| 71 base::Unretained(server_.get()), connection_id, message)); | 103 base::Unretained(server_.get()), connection_id, message)); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 89 server_ = base::MakeUnique<net::HttpServer>(std::move(socket), this); | 121 server_ = base::MakeUnique<net::HttpServer>(std::move(socket), this); |
| 90 } | 122 } |
| 91 | 123 |
| 92 // HttpServer::Delegate Implementation | 124 // HttpServer::Delegate Implementation |
| 93 void UiDevToolsServer::OnConnect(int connection_id) { | 125 void UiDevToolsServer::OnConnect(int connection_id) { |
| 94 NOTIMPLEMENTED(); | 126 NOTIMPLEMENTED(); |
| 95 } | 127 } |
| 96 | 128 |
| 97 void UiDevToolsServer::OnHttpRequest(int connection_id, | 129 void UiDevToolsServer::OnHttpRequest(int connection_id, |
| 98 const net::HttpServerRequestInfo& info) { | 130 const net::HttpServerRequestInfo& info) { |
| 99 // Display a simple html page with all the clients and the corresponding | 131 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 } | 132 } |
| 126 | 133 |
| 127 void UiDevToolsServer::OnWebSocketRequest( | 134 void UiDevToolsServer::OnWebSocketRequest( |
| 128 int connection_id, | 135 int connection_id, |
| 129 const net::HttpServerRequestInfo& info) { | 136 const net::HttpServerRequestInfo& info) { |
| 130 size_t target_id = 0; | 137 size_t target_id = 0; |
| 131 if (info.path.empty() || | 138 if (info.path.empty() || |
| 132 !base::StringToSizeT(info.path.substr(1), &target_id) || | 139 !base::StringToSizeT(info.path.substr(1), &target_id) || |
| 133 target_id > clients_.size()) | 140 target_id > clients_.size()) |
| 134 return; | 141 return; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 160 if (it == connections_.end()) | 167 if (it == connections_.end()) |
| 161 return; | 168 return; |
| 162 UiDevToolsClient* client = it->second; | 169 UiDevToolsClient* client = it->second; |
| 163 DCHECK(client); | 170 DCHECK(client); |
| 164 client->Disconnect(); | 171 client->Disconnect(); |
| 165 connections_.erase(it); | 172 connections_.erase(it); |
| 166 } | 173 } |
| 167 | 174 |
| 168 } // namespace devtools | 175 } // namespace devtools |
| 169 } // namespace ui | 176 } // namespace ui |
| OLD | NEW |