2016-10-17 20:14:44 +02:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
2019-02-12 19:43:44 +01:00
|
|
|
#include "libcef/browser/devtools/devtools_manager_delegate.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/atomicops.h"
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/files/file_path.h"
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/bind.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
#include "base/memory/ptr_util.h"
|
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "build/build_config.h"
|
|
|
|
#include "cef/grit/cef_resources.h"
|
|
|
|
#include "content/public/browser/browser_context.h"
|
|
|
|
#include "content/public/browser/devtools_agent_host.h"
|
|
|
|
#include "content/public/browser/devtools_frontend_host.h"
|
|
|
|
#include "content/public/browser/devtools_socket_factory.h"
|
|
|
|
#include "content/public/browser/favicon_status.h"
|
|
|
|
#include "content/public/browser/navigation_entry.h"
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
#include "content/public/common/content_switches.h"
|
|
|
|
#include "content/public/common/url_constants.h"
|
|
|
|
#include "content/public/common/user_agent.h"
|
|
|
|
#include "net/base/net_errors.h"
|
2016-10-21 21:52:29 +02:00
|
|
|
#include "net/log/net_log_source.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
#include "net/socket/tcp_server_socket.h"
|
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int kBackLog = 10;
|
|
|
|
|
|
|
|
class TCPServerSocketFactory : public content::DevToolsSocketFactory {
|
|
|
|
public:
|
|
|
|
TCPServerSocketFactory(const std::string& address, uint16_t port)
|
|
|
|
: address_(address), port_(port) {}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
TCPServerSocketFactory(const TCPServerSocketFactory&) = delete;
|
|
|
|
TCPServerSocketFactory& operator=(const TCPServerSocketFactory&) = delete;
|
|
|
|
|
2016-10-17 20:14:44 +02:00
|
|
|
private:
|
|
|
|
// content::DevToolsSocketFactory.
|
|
|
|
std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
|
|
|
|
std::unique_ptr<net::ServerSocket> socket(
|
2016-10-21 21:52:29 +02:00
|
|
|
new net::TCPServerSocket(nullptr, net::NetLogSource()));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) !=
|
|
|
|
net::OK) {
|
2016-10-17 20:14:44 +02:00
|
|
|
return std::unique_ptr<net::ServerSocket>();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-10-17 20:14:44 +02:00
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<net::ServerSocket> CreateForTethering(
|
|
|
|
std::string* out_name) override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string address_;
|
|
|
|
uint16_t port_;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<content::DevToolsSocketFactory> CreateSocketFactory() {
|
|
|
|
const base::CommandLine& command_line =
|
|
|
|
*base::CommandLine::ForCurrentProcess();
|
|
|
|
// See if the user specified a port on the command line. Specifying 0 would
|
|
|
|
// result in the selection of an ephemeral port but that doesn't make sense
|
|
|
|
// for CEF where the URL is otherwise undiscoverable. Also, don't allow
|
|
|
|
// binding of ports between 0 and 1024 exclusive because they're normally
|
|
|
|
// restricted to root on Posix-based systems.
|
|
|
|
uint16_t port = 0;
|
|
|
|
if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
|
|
|
|
int temp_port;
|
|
|
|
std::string port_str =
|
|
|
|
command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
|
2017-05-17 11:29:28 +02:00
|
|
|
if (base::StringToInt(port_str, &temp_port) && temp_port >= 1024 &&
|
|
|
|
temp_port < 65535) {
|
2016-10-17 20:14:44 +02:00
|
|
|
port = static_cast<uint16_t>(temp_port);
|
|
|
|
} else {
|
|
|
|
DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
|
|
|
|
}
|
|
|
|
}
|
2023-01-02 23:59:03 +01:00
|
|
|
if (port == 0) {
|
2016-10-17 20:14:44 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-10-17 20:14:44 +02:00
|
|
|
return std::unique_ptr<content::DevToolsSocketFactory>(
|
|
|
|
new TCPServerSocketFactory("127.0.0.1", port));
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
} // namespace
|
2016-10-17 20:14:44 +02:00
|
|
|
|
|
|
|
// CefDevToolsManagerDelegate ----------------------------------------------
|
|
|
|
|
|
|
|
// static
|
|
|
|
void CefDevToolsManagerDelegate::StartHttpHandler(
|
|
|
|
content::BrowserContext* browser_context) {
|
|
|
|
std::unique_ptr<content::DevToolsSocketFactory> socket_factory =
|
|
|
|
CreateSocketFactory();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!socket_factory) {
|
2016-10-17 20:14:44 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-10-17 20:14:44 +02:00
|
|
|
content::DevToolsAgentHost::StartRemoteDebuggingServer(
|
2018-03-20 21:15:08 +01:00
|
|
|
std::move(socket_factory), browser_context->GetPath(), base::FilePath());
|
2017-12-07 22:44:24 +01:00
|
|
|
|
|
|
|
const base::CommandLine& command_line =
|
|
|
|
*base::CommandLine::ForCurrentProcess();
|
2021-01-28 00:13:12 +01:00
|
|
|
if (command_line.HasSwitch(switches::kRemoteDebuggingPipe)) {
|
|
|
|
content::DevToolsAgentHost::StartRemoteDebuggingPipeHandler(
|
|
|
|
base::OnceClosure());
|
|
|
|
}
|
2016-10-17 20:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void CefDevToolsManagerDelegate::StopHttpHandler() {
|
|
|
|
// This is a no-op if the server was never started.
|
|
|
|
content::DevToolsAgentHost::StopRemoteDebuggingServer();
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefDevToolsManagerDelegate::CefDevToolsManagerDelegate() {}
|
2016-10-17 20:14:44 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefDevToolsManagerDelegate::~CefDevToolsManagerDelegate() {}
|
2016-10-17 20:14:44 +02:00
|
|
|
|
|
|
|
scoped_refptr<content::DevToolsAgentHost>
|
2023-08-09 23:17:17 +02:00
|
|
|
CefDevToolsManagerDelegate::CreateNewTarget(
|
|
|
|
const GURL& url,
|
|
|
|
content::DevToolsManagerDelegate::TargetType target_type) {
|
2016-10-17 20:14:44 +02:00
|
|
|
// This is reached when the user selects "Open link in new tab" from the
|
|
|
|
// DevTools interface.
|
|
|
|
// TODO(cef): Consider exposing new API to support this.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CefDevToolsManagerDelegate::GetDiscoveryPageHTML() {
|
2020-06-30 16:43:48 +02:00
|
|
|
return ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
|
|
|
|
IDR_CEF_DEVTOOLS_DISCOVERY_PAGE);
|
2016-10-17 20:14:44 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
bool CefDevToolsManagerDelegate::HasBundledFrontendResources() {
|
|
|
|
return true;
|
2016-10-17 20:14:44 +02:00
|
|
|
}
|