2014-07-30 11:40:17 +08: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 13:46:59 -07:00
|
|
|
#include "shell/browser/ui/devtools_manager_delegate.h"
|
2014-07-30 11:40:17 +08:00
|
|
|
|
2018-09-12 19:25:56 -05:00
|
|
|
#include <memory>
|
2018-10-26 16:45:46 +11:00
|
|
|
#include <utility>
|
2014-07-30 11:40:17 +08:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/command_line.h"
|
2015-01-09 17:14:52 -08:00
|
|
|
#include "base/files/file_path.h"
|
2019-04-16 14:22:51 -04:00
|
|
|
#include "base/path_service.h"
|
2014-07-30 11:40:17 +08:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "content/public/browser/devtools_agent_host.h"
|
2015-07-23 18:18:03 +08:00
|
|
|
#include "content/public/browser/devtools_frontend_host.h"
|
2017-01-23 14:48:23 +09:00
|
|
|
#include "content/public/browser/devtools_socket_factory.h"
|
2014-07-30 11:40:17 +08: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 06:30:43 +05:30
|
|
|
#include "electron/grit/electron_resources.h"
|
2015-07-23 18:18:03 +08:00
|
|
|
#include "net/base/net_errors.h"
|
2015-03-08 19:07:53 -07:00
|
|
|
#include "net/socket/stream_socket.h"
|
2017-05-18 15:29:22 -07:00
|
|
|
#include "net/socket/tcp_server_socket.h"
|
2020-05-08 16:13:32 -07:00
|
|
|
#include "shell/browser/browser.h"
|
2020-05-07 13:31:26 -07:00
|
|
|
#include "shell/common/electron_paths.h"
|
2020-05-08 16:13:32 -07:00
|
|
|
#include "third_party/inspector_protocol/crdtp/dispatch.h"
|
2014-07-30 11:40:17 +08:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
|
2019-06-19 14:23:04 -07:00
|
|
|
namespace electron {
|
2015-01-09 17:14:52 -08:00
|
|
|
|
2014-07-30 11:40:17 +08:00
|
|
|
namespace {
|
|
|
|
|
2017-01-23 14:48:23 +09:00
|
|
|
class TCPServerSocketFactory : public content::DevToolsSocketFactory {
|
2014-12-05 15:37:11 -08:00
|
|
|
public:
|
2015-04-21 18:54:57 +08:00
|
|
|
TCPServerSocketFactory(const std::string& address, int port)
|
2018-04-17 21:56:12 -04:00
|
|
|
: address_(address), port_(port) {}
|
2014-12-05 15:37:11 -08:00
|
|
|
|
|
|
|
private:
|
2017-01-23 14:48:23 +09:00
|
|
|
// content::ServerSocketFactory.
|
2016-05-23 10:59:07 +09:00
|
|
|
std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
|
2021-06-07 19:00:05 -07:00
|
|
|
auto socket =
|
|
|
|
std::make_unique<net::TCPServerSocket>(nullptr, net::NetLogSource());
|
2015-04-21 18:54:57 +08:00
|
|
|
if (socket->ListenWithAddressAndPort(address_, port_, 10) != net::OK)
|
2016-05-23 10:59:07 +09:00
|
|
|
return std::unique_ptr<net::ServerSocket>();
|
2015-04-21 18:54:57 +08:00
|
|
|
|
|
|
|
return socket;
|
2014-12-05 15:37:11 -08:00
|
|
|
}
|
2017-01-23 15:07:18 +09:00
|
|
|
std::unique_ptr<net::ServerSocket> CreateForTethering(
|
|
|
|
std::string* name) override {
|
|
|
|
return std::unique_ptr<net::ServerSocket>();
|
|
|
|
}
|
2014-12-05 15:37:11 -08:00
|
|
|
|
2015-04-21 18:54:57 +08:00
|
|
|
std::string address_;
|
2016-03-07 22:38:48 -08:00
|
|
|
uint16_t port_;
|
2015-04-21 18:54:57 +08:00
|
|
|
|
2014-12-05 15:37:11 -08:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
|
|
|
|
};
|
|
|
|
|
2018-04-17 21:56:12 -04:00
|
|
|
std::unique_ptr<content::DevToolsSocketFactory> CreateSocketFactory() {
|
2015-03-08 19:07:53 -07:00
|
|
|
auto& command_line = *base::CommandLine::ForCurrentProcess();
|
2014-07-30 11:40:17 +08: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 14:22:51 -04:00
|
|
|
if (base::StringToInt(port_str, &temp_port) && temp_port >= 0 &&
|
2018-04-17 21:56:12 -04:00
|
|
|
temp_port < 65535) {
|
2014-07-30 11:40:17 +08:00
|
|
|
port = temp_port;
|
|
|
|
} else {
|
|
|
|
DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 19:00:05 -07:00
|
|
|
return std::make_unique<TCPServerSocketFactory>("127.0.0.1", port);
|
2015-12-07 19:55:01 +08:00
|
|
|
}
|
|
|
|
|
2020-05-08 16:13:32 -07:00
|
|
|
const char kBrowserCloseMethod[] = "Browser.close";
|
|
|
|
|
2015-01-09 17:14:52 -08:00
|
|
|
} // namespace
|
|
|
|
|
2014-12-05 15:37:11 -08:00
|
|
|
// DevToolsManagerDelegate ---------------------------------------------------
|
|
|
|
|
2015-01-09 17:14:52 -08:00
|
|
|
// static
|
2017-01-23 15:07:18 +09:00
|
|
|
void DevToolsManagerDelegate::StartHttpHandler() {
|
2019-04-16 14:22:51 -04:00
|
|
|
base::FilePath user_dir;
|
|
|
|
base::PathService::Get(DIR_USER_DATA, &user_dir);
|
2017-01-23 15:07:18 +09:00
|
|
|
content::DevToolsAgentHost::StartRemoteDebuggingServer(
|
2019-04-16 14:22:51 -04:00
|
|
|
CreateSocketFactory(), user_dir, base::FilePath());
|
2015-01-09 17:14:52 -08:00
|
|
|
}
|
|
|
|
|
2019-09-16 18:12:00 -04:00
|
|
|
DevToolsManagerDelegate::DevToolsManagerDelegate() = default;
|
2014-12-05 15:37:11 -08:00
|
|
|
|
2019-09-16 18:12:00 -04:00
|
|
|
DevToolsManagerDelegate::~DevToolsManagerDelegate() = default;
|
2014-12-05 15:37:11 -08:00
|
|
|
|
2018-04-17 21:56:12 -04:00
|
|
|
void DevToolsManagerDelegate::Inspect(content::DevToolsAgentHost* agent_host) {}
|
2016-03-14 02:35:22 +05:30
|
|
|
|
2018-10-25 22:48:21 +05:30
|
|
|
void DevToolsManagerDelegate::HandleCommand(
|
2020-01-29 13:45:08 -08:00
|
|
|
content::DevToolsAgentHostClientChannel* channel,
|
2020-01-17 10:41:52 -08:00
|
|
|
base::span<const uint8_t> message,
|
2018-10-25 22:48:21 +05:30
|
|
|
NotHandledCallback callback) {
|
2020-05-08 16:13:32 -07: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 10:25:21 -07:00
|
|
|
// methods like Browser.setWindowBounds, we'll need to do it though.
|
2020-05-08 16:13:32 -07:00
|
|
|
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
|
|
|
|
base::BindOnce([]() { Browser::Get()->Quit(); }));
|
|
|
|
return;
|
|
|
|
}
|
2019-02-21 13:46:41 +05:30
|
|
|
std::move(callback).Run(message);
|
2014-12-05 15:37:11 -08:00
|
|
|
}
|
|
|
|
|
2017-01-23 14:48:23 +09:00
|
|
|
scoped_refptr<content::DevToolsAgentHost>
|
2017-01-23 15:07:18 +09:00
|
|
|
DevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
|
2017-01-23 14:48:23 +09:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-23 15:07:18 +09:00
|
|
|
std::string DevToolsManagerDelegate::GetDiscoveryPageHTML() {
|
2020-09-23 11:47:44 -07:00
|
|
|
return ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
|
|
|
|
IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE);
|
2017-01-23 15:07:18 +09:00
|
|
|
}
|
|
|
|
|
2018-04-04 15:15:04 +02:00
|
|
|
bool DevToolsManagerDelegate::HasBundledFrontendResources() {
|
|
|
|
return true;
|
2017-01-23 15:07:18 +09:00
|
|
|
}
|
|
|
|
|
2019-06-19 14:23:04 -07:00
|
|
|
} // namespace electron
|