2020-06-13 02:54:08 +02:00
|
|
|
// Copyright (c) 2020 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
#include "libcef/browser/devtools/devtools_protocol_manager.h"
|
2024-01-20 23:48:57 +01:00
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
#include "libcef/browser/browser_host_base.h"
|
2020-06-13 02:54:08 +02:00
|
|
|
#include "libcef/browser/devtools/devtools_controller.h"
|
2024-04-17 18:01:26 +02:00
|
|
|
#include "libcef/browser/thread_util.h"
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// May be created on any thread but will be destroyed on the UI thread.
|
|
|
|
class CefDevToolsRegistrationImpl : public CefRegistration,
|
|
|
|
public CefDevToolsController::Observer {
|
|
|
|
public:
|
|
|
|
explicit CefDevToolsRegistrationImpl(
|
|
|
|
CefRefPtr<CefDevToolsMessageObserver> observer)
|
|
|
|
: observer_(observer) {
|
|
|
|
DCHECK(observer_);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefDevToolsRegistrationImpl(const CefDevToolsRegistrationImpl&) = delete;
|
|
|
|
CefDevToolsRegistrationImpl& operator=(const CefDevToolsRegistrationImpl&) =
|
|
|
|
delete;
|
|
|
|
|
2020-06-13 02:54:08 +02:00
|
|
|
~CefDevToolsRegistrationImpl() override {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
// May be null if OnDevToolsControllerDestroyed was called.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!controller_) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
controller_->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
2021-03-18 18:17:33 +01:00
|
|
|
void Initialize(CefBrowserHostBase* browser,
|
2020-06-13 02:54:08 +02:00
|
|
|
base::WeakPtr<CefDevToolsController> controller) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
DCHECK(browser && controller);
|
|
|
|
DCHECK(!browser_ && !controller_);
|
|
|
|
browser_ = browser;
|
|
|
|
controller_ = controller;
|
|
|
|
|
|
|
|
controller_->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// CefDevToolsController::Observer methods:
|
2024-04-23 22:06:00 +02:00
|
|
|
bool OnDevToolsMessage(const std::string_view& message) override {
|
2020-06-13 02:54:08 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
return observer_->OnDevToolsMessage(browser_, message.data(),
|
|
|
|
message.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDevToolsMethodResult(int message_id,
|
|
|
|
bool success,
|
2024-04-23 22:06:00 +02:00
|
|
|
const std::string_view& result) override {
|
2020-06-13 02:54:08 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
observer_->OnDevToolsMethodResult(browser_, message_id, success,
|
|
|
|
result.data(), result.size());
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:06:00 +02:00
|
|
|
void OnDevToolsEvent(const std::string_view& method,
|
|
|
|
const std::string_view& params) override {
|
2020-06-13 02:54:08 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2021-06-04 03:34:56 +02:00
|
|
|
observer_->OnDevToolsEvent(browser_, std::string(method), params.data(),
|
2020-06-13 02:54:08 +02:00
|
|
|
params.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDevToolsAgentAttached() override {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
observer_->OnDevToolsAgentAttached(browser_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDevToolsAgentDetached() override {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
observer_->OnDevToolsAgentDetached(browser_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDevToolsControllerDestroyed() override {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
browser_ = nullptr;
|
|
|
|
controller_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDevToolsMessageObserver> observer_;
|
|
|
|
|
2021-03-18 18:17:33 +01:00
|
|
|
CefBrowserHostBase* browser_ = nullptr;
|
2020-06-13 02:54:08 +02:00
|
|
|
base::WeakPtr<CefDevToolsController> controller_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING_DELETE_ON_UIT(CefDevToolsRegistrationImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
CefDevToolsProtocolManager::CefDevToolsProtocolManager(
|
|
|
|
CefBrowserHostBase* inspected_browser)
|
|
|
|
: inspected_browser_(inspected_browser) {
|
2020-06-13 02:54:08 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
CefDevToolsProtocolManager::~CefDevToolsProtocolManager() {
|
2020-06-13 02:54:08 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
bool CefDevToolsProtocolManager::SendDevToolsMessage(const void* message,
|
|
|
|
size_t message_size) {
|
2020-06-13 02:54:08 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!message || message_size == 0) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!EnsureController()) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
return devtools_controller_->SendDevToolsMessage(
|
2024-04-23 22:06:00 +02:00
|
|
|
std::string_view(static_cast<const char*>(message), message_size));
|
2020-06-13 02:54:08 +02:00
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
int CefDevToolsProtocolManager::ExecuteDevToolsMethod(
|
2020-06-13 02:54:08 +02:00
|
|
|
int message_id,
|
|
|
|
const CefString& method,
|
|
|
|
CefRefPtr<CefDictionaryValue> params) {
|
|
|
|
CEF_REQUIRE_UIT();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (method.empty()) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!EnsureController()) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
if (params && params->IsValid()) {
|
|
|
|
CefDictionaryValueImpl* impl =
|
|
|
|
static_cast<CefDictionaryValueImpl*>(params.get());
|
|
|
|
CefValueController::AutoLock lock_scope(impl->controller());
|
2023-01-30 22:42:40 +01:00
|
|
|
return devtools_controller_->ExecuteDevToolsMethod(
|
|
|
|
message_id, method, impl->GetValueUnsafe()->GetIfDict());
|
2020-06-13 02:54:08 +02:00
|
|
|
} else {
|
|
|
|
return devtools_controller_->ExecuteDevToolsMethod(message_id, method,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2024-04-17 18:01:26 +02:00
|
|
|
CefRefPtr<CefRegistration> CefDevToolsProtocolManager::CreateRegistration(
|
2020-06-13 02:54:08 +02:00
|
|
|
CefRefPtr<CefDevToolsMessageObserver> observer) {
|
|
|
|
DCHECK(observer);
|
|
|
|
return new CefDevToolsRegistrationImpl(observer);
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
void CefDevToolsProtocolManager::InitializeRegistrationOnUIThread(
|
2020-06-13 02:54:08 +02:00
|
|
|
CefRefPtr<CefRegistration> registration) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!EnsureController()) {
|
2020-06-13 02:54:08 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
static_cast<CefDevToolsRegistrationImpl*>(registration.get())
|
|
|
|
->Initialize(inspected_browser_, devtools_controller_->GetWeakPtr());
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
bool CefDevToolsProtocolManager::EnsureController() {
|
2020-06-13 02:54:08 +02:00
|
|
|
if (!devtools_controller_) {
|
2024-01-20 23:48:57 +01:00
|
|
|
devtools_controller_ = std::make_unique<CefDevToolsController>(
|
|
|
|
inspected_browser_->contents_delegate()->web_contents());
|
2020-06-13 02:54:08 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|