2014-07-30 03:40:17 +00:00
|
|
|
// Copyright (c) 2014 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-CHROMIUM file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/devtools_manager_delegate.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2018-10-26 05:45:46 +00:00
|
|
|
#include <utility>
|
2014-07-30 03:40:17 +00:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/command_line.h"
|
2015-01-10 01:14:52 +00:00
|
|
|
#include "base/files/file_path.h"
|
2019-04-16 18:22:51 +00:00
|
|
|
#include "base/path_service.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2021-06-15 00:32:56 +00:00
|
|
|
#include "chrome/common/chrome_paths.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "content/public/browser/devtools_agent_host.h"
|
2015-07-23 10:18:03 +00:00
|
|
|
#include "content/public/browser/devtools_frontend_host.h"
|
2017-01-23 05:48:23 +00:00
|
|
|
#include "content/public/browser/devtools_socket_factory.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "content/public/browser/favicon_status.h"
|
|
|
|
#include "content/public/browser/navigation_entry.h"
|
|
|
|
#include "content/public/common/content_switches.h"
|
|
|
|
#include "content/public/common/url_constants.h"
|
|
|
|
#include "content/public/common/user_agent.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "electron/grit/electron_resources.h"
|
2015-07-23 10:18:03 +00:00
|
|
|
#include "net/base/net_errors.h"
|
2015-03-09 02:07:53 +00:00
|
|
|
#include "net/socket/stream_socket.h"
|
2017-05-18 22:29:22 +00:00
|
|
|
#include "net/socket/tcp_server_socket.h"
|
2020-05-08 23:13:32 +00:00
|
|
|
#include "shell/browser/browser.h"
|
2020-05-07 20:31:26 +00:00
|
|
|
#include "shell/common/electron_paths.h"
|
2020-05-08 23:13:32 +00:00
|
|
|
#include "third_party/inspector_protocol/crdtp/dispatch.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2015-01-10 01:14:52 +00:00
|
|
|
|
2014-07-30 03:40:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-01-23 05:48:23 +00:00
|
|
|
class TCPServerSocketFactory : public content::DevToolsSocketFactory {
|
2014-12-05 23:37:11 +00:00
|
|
|
public:
|
2015-04-21 10:54:57 +00:00
|
|
|
TCPServerSocketFactory(const std::string& address, int port)
|
2018-04-18 01:56:12 +00:00
|
|
|
: address_(address), port_(port) {}
|
2014-12-05 23:37:11 +00:00
|
|
|
|
|
|
|
private:
|
2017-01-23 05:48:23 +00:00
|
|
|
// content::ServerSocketFactory.
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
|
2021-06-08 02:00:05 +00:00
|
|
|
auto socket =
|
|
|
|
std::make_unique<net::TCPServerSocket>(nullptr, net::NetLogSource());
|
2015-04-21 10:54:57 +00:00
|
|
|
if (socket->ListenWithAddressAndPort(address_, port_, 10) != net::OK)
|
2016-05-23 01:59:07 +00:00
|
|
|
return std::unique_ptr<net::ServerSocket>();
|
2015-04-21 10:54:57 +00:00
|
|
|
|
|
|
|
return socket;
|
2014-12-05 23:37:11 +00:00
|
|
|
}
|
2017-01-23 06:07:18 +00:00
|
|
|
std::unique_ptr<net::ServerSocket> CreateForTethering(
|
|
|
|
std::string* name) override {
|
|
|
|
return std::unique_ptr<net::ServerSocket>();
|
|
|
|
}
|
2014-12-05 23:37:11 +00:00
|
|
|
|
2015-04-21 10:54:57 +00:00
|
|
|
std::string address_;
|
2016-03-08 06:38:48 +00:00
|
|
|
uint16_t port_;
|
2015-04-21 10:54:57 +00:00
|
|
|
|
2014-12-05 23:37:11 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
|
|
|
|
};
|
|
|
|
|
2018-04-18 01:56:12 +00:00
|
|
|
std::unique_ptr<content::DevToolsSocketFactory> CreateSocketFactory() {
|
2015-03-09 02:07:53 +00:00
|
|
|
auto& command_line = *base::CommandLine::ForCurrentProcess();
|
2014-07-30 03:40:17 +00:00
|
|
|
// See if the user specified a port on the command line (useful for
|
|
|
|
// automation). If not, use an ephemeral port by specifying 0.
|
|
|
|
int port = 0;
|
|
|
|
if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
|
|
|
|
int temp_port;
|
|
|
|
std::string port_str =
|
|
|
|
command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
|
2019-04-16 18:22:51 +00:00
|
|
|
if (base::StringToInt(port_str, &temp_port) && temp_port >= 0 &&
|
2018-04-18 01:56:12 +00:00
|
|
|
temp_port < 65535) {
|
2014-07-30 03:40:17 +00:00
|
|
|
port = temp_port;
|
|
|
|
} else {
|
|
|
|
DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 02:00:05 +00:00
|
|
|
return std::make_unique<TCPServerSocketFactory>("127.0.0.1", port);
|
2015-12-07 11:55:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 23:13:32 +00:00
|
|
|
const char kBrowserCloseMethod[] = "Browser.close";
|
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
} // namespace
|
|
|
|
|
2014-12-05 23:37:11 +00:00
|
|
|
// DevToolsManagerDelegate ---------------------------------------------------
|
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
// static
|
2017-01-23 06:07:18 +00:00
|
|
|
void DevToolsManagerDelegate::StartHttpHandler() {
|
2019-04-16 18:22:51 +00:00
|
|
|
base::FilePath user_dir;
|
2021-06-15 00:32:56 +00:00
|
|
|
base::PathService::Get(chrome::DIR_USER_DATA, &user_dir);
|
2017-01-23 06:07:18 +00:00
|
|
|
content::DevToolsAgentHost::StartRemoteDebuggingServer(
|
2019-04-16 18:22:51 +00:00
|
|
|
CreateSocketFactory(), user_dir, base::FilePath());
|
2015-01-10 01:14:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
DevToolsManagerDelegate::DevToolsManagerDelegate() = default;
|
2014-12-05 23:37:11 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
DevToolsManagerDelegate::~DevToolsManagerDelegate() = default;
|
2014-12-05 23:37:11 +00:00
|
|
|
|
2018-04-18 01:56:12 +00:00
|
|
|
void DevToolsManagerDelegate::Inspect(content::DevToolsAgentHost* agent_host) {}
|
2016-03-13 21:05:22 +00:00
|
|
|
|
2018-10-25 17:18:21 +00:00
|
|
|
void DevToolsManagerDelegate::HandleCommand(
|
2020-01-29 21:45:08 +00:00
|
|
|
content::DevToolsAgentHostClientChannel* channel,
|
2020-01-17 18:41:52 +00:00
|
|
|
base::span<const uint8_t> message,
|
2018-10-25 17:18:21 +00:00
|
|
|
NotHandledCallback callback) {
|
2020-05-08 23:13:32 +00:00
|
|
|
crdtp::Dispatchable dispatchable(crdtp::SpanFrom(message));
|
|
|
|
DCHECK(dispatchable.ok());
|
|
|
|
if (crdtp::SpanEquals(crdtp::SpanFrom(kBrowserCloseMethod),
|
|
|
|
dispatchable.Method())) {
|
|
|
|
// In theory, we should respond over the protocol saying that the
|
|
|
|
// Browser.close was handled. But doing so requires instantiating the
|
|
|
|
// protocol UberDispatcher and generating proper protocol handlers.
|
|
|
|
// Since we only have one method and it is supposed to close Electron,
|
|
|
|
// we don't need to add this complexity. Should we decide to support
|
2020-10-13 17:25:21 +00:00
|
|
|
// methods like Browser.setWindowBounds, we'll need to do it though.
|
2020-05-08 23:13:32 +00:00
|
|
|
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
|
|
|
|
base::BindOnce([]() { Browser::Get()->Quit(); }));
|
|
|
|
return;
|
|
|
|
}
|
2019-02-21 08:16:41 +00:00
|
|
|
std::move(callback).Run(message);
|
2014-12-05 23:37:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 05:48:23 +00:00
|
|
|
scoped_refptr<content::DevToolsAgentHost>
|
2017-01-23 06:07:18 +00:00
|
|
|
DevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
|
2017-01-23 05:48:23 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:07:18 +00:00
|
|
|
std::string DevToolsManagerDelegate::GetDiscoveryPageHTML() {
|
2020-09-23 18:47:44 +00:00
|
|
|
return ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
|
|
|
|
IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE);
|
2017-01-23 06:07:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 13:15:04 +00:00
|
|
|
bool DevToolsManagerDelegate::HasBundledFrontendResources() {
|
|
|
|
return true;
|
2017-01-23 06:07:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|