From 31616ac94abad3e3d57b4b2d51858106655a85dd Mon Sep 17 00:00:00 2001 From: Peter Varga Date: Mon, 15 Feb 2021 11:09:23 +0100 Subject: Add basic USB device logging The log is available on the chrome://device-log WebUI. The logging depends on the WebUSB feature and it is disabled by default. Use --enable-features=WebUSB to enable it. Change-Id: Idcbdfa42db3b5a5851abbd3e8d877d69c2d9796a Reviewed-by: Allan Sandfeld Jensen --- src/core/CMakeLists.txt | 1 + src/core/browser_main_parts_qt.cpp | 22 +++++ src/core/browser_main_parts_qt.h | 4 + src/core/configure/BUILD.root.gn.in | 5 ++ src/core/net/webui_controller_factory_qt.cpp | 4 + src/core/web_usb_detector_qt.cpp | 94 ++++++++++++++++++++++ src/core/web_usb_detector_qt.h | 75 +++++++++++++++++ .../widgets/qwebengineview/tst_qwebengineview.cpp | 2 +- 8 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 src/core/web_usb_detector_qt.cpp create mode 100644 src/core/web_usb_detector_qt.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c41b44510..ab5143e21 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -173,6 +173,7 @@ foreach(config ${configs}) web_engine_library_info.cpp web_engine_library_info.h web_engine_settings.cpp web_engine_settings.h web_event_factory.cpp web_event_factory.h + web_usb_detector_qt.cpp web_usb_detector_qt.h ) extend_gn_target(${buildGn} CONDITION QT_FEATURE_webengine_ozone_x11 diff --git a/src/core/browser_main_parts_qt.cpp b/src/core/browser_main_parts_qt.cpp index 864e96c89..4d89a5947 100644 --- a/src/core/browser_main_parts_qt.cpp +++ b/src/core/browser_main_parts_qt.cpp @@ -46,16 +46,21 @@ #include "base/task/current_thread.h" #include "base/task/sequence_manager/sequence_manager_impl.h" #include "base/task/sequence_manager/thread_controller_with_message_pump_impl.h" +#include "base/task/task_traits.h" +#include "base/task/thread_pool.h" #include "base/threading/thread_restrictions.h" #include "chrome/browser/tab_contents/form_interaction_tab_helper.h" +#include "components/device_event_log/device_event_log.h" #include "components/performance_manager/embedder/performance_manager_lifetime.h" #include "components/performance_manager/embedder/performance_manager_registry.h" #include "components/performance_manager/public/graph/graph.h" #include "components/performance_manager/public/performance_manager.h" #include "content/public/browser/browser_main_parts.h" +#include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/child_process_security_policy.h" #include "content/public/browser/system_connector.h" +#include "content/public/common/content_features.h" #include "content/public/common/service_manager_connection.h" #include "extensions/buildflags/buildflags.h" #if BUILDFLAG(ENABLE_EXTENSIONS) @@ -73,6 +78,7 @@ #include "ui/display/screen.h" #include "web_engine_context.h" +#include "web_usb_detector_qt.h" #include @@ -247,6 +253,12 @@ void BrowserMainPartsQt::PreMainMessageLoopStart() { } +void BrowserMainPartsQt::PostMainMessageLoopStart() +{ + if (!device_event_log::IsInitialized()) + device_event_log::Initialize(0 /* default max entries */); +} + void BrowserMainPartsQt::PreMainMessageLoopRun() { ui::SelectFileDialog::SetFactory(new SelectFileDialogFactoryQt()); @@ -259,6 +271,14 @@ void BrowserMainPartsQt::PreMainMessageLoopRun() content::PluginService *plugin_service = content::PluginService::GetInstance(); plugin_service->SetFilter(extensions::PluginServiceFilterQt::GetInstance()); #endif //ENABLE_EXTENSIONS + + if (base::FeatureList::IsEnabled(features::kWebUsb)) { + m_webUsbDetector.reset(new WebUsbDetectorQt()); + content::GetUIThreadTaskRunner({ base::TaskPriority::BEST_EFFORT }) + ->PostTask(FROM_HERE, + base::BindOnce(&WebUsbDetectorQt::Initialize, + base::Unretained(m_webUsbDetector.get()))); + } } void BrowserMainPartsQt::PostMainMessageLoopRun() @@ -267,6 +287,8 @@ void BrowserMainPartsQt::PostMainMessageLoopRun() performance_manager_registry_.reset(); performance_manager::DestroyPerformanceManager(std::move(performance_manager_)); + m_webUsbDetector.reset(); + // The ProfileQt's destructor uses the MessageLoop so it should be deleted // right before the RenderProcessHostImpl's destructor destroys it. WebEngineContext::current()->destroyProfileAdapter(); diff --git a/src/core/browser_main_parts_qt.h b/src/core/browser_main_parts_qt.h index 9d0967612..07e8ffe98 100644 --- a/src/core/browser_main_parts_qt.h +++ b/src/core/browser_main_parts_qt.h @@ -42,6 +42,8 @@ #include "content/public/browser/browser_main_parts.h" +#include "web_usb_detector_qt.h" + namespace base { class MessagePump; } @@ -67,6 +69,7 @@ public: int PreEarlyInitialization() override; void PreMainMessageLoopStart() override; + void PostMainMessageLoopStart() override; void PreMainMessageLoopRun() override; void PostMainMessageLoopRun() override; int PreCreateThreads() override; @@ -76,6 +79,7 @@ private: DISALLOW_COPY_AND_ASSIGN(BrowserMainPartsQt); std::unique_ptr performance_manager_; std::unique_ptr performance_manager_registry_; + std::unique_ptr m_webUsbDetector; }; } // namespace QtWebEngineCore diff --git a/src/core/configure/BUILD.root.gn.in b/src/core/configure/BUILD.root.gn.in index b0e52511c..769b4ba79 100644 --- a/src/core/configure/BUILD.root.gn.in +++ b/src/core/configure/BUILD.root.gn.in @@ -144,6 +144,7 @@ source_set("qtwebengine_sources") { ] deps = [ "//build:branding_buildflags", + "//chrome/browser:dev_ui_browser_resources_grit", "//chrome/browser/resources/quota_internals:quota_internals_resources", "//chrome/common:buildflags", "//components/nacl/common:buildflags", @@ -172,6 +173,8 @@ source_set("qtwebengine_sources") { "//chrome/browser/profiles/profile.h", "//chrome/browser/tab_contents/form_interaction_tab_helper.cc", "//chrome/browser/tab_contents/form_interaction_tab_helper.h", + "//chrome/browser/ui/webui/device_log_ui.cc", + "//chrome/browser/ui/webui/device_log_ui.h", "//chrome/browser/ui/webui/devtools_ui.cc", "//chrome/browser/ui/webui/devtools_ui.h", "//chrome/browser/ui/webui/devtools_ui_data_source.cc", @@ -375,6 +378,7 @@ repack("qtwebengine_repack_resources") { sources = [ "$root_gen_dir/qtwebengine/qt_webengine_resources.pak", "$root_gen_dir/chrome/common_resources.pak", + "$root_gen_dir/chrome/dev_ui_browser_resources.pak", "$root_gen_dir/chrome/net_internals_resources.pak", "$root_gen_dir/chrome/quota_internals_resources.pak", "$root_gen_dir/components/components_resources.pak", @@ -393,6 +397,7 @@ repack("qtwebengine_repack_resources") { deps = [ "//qtwebengine/browser:qt_webengine_resources", "//chrome/browser/resources/net_internals:net_internals_resources", + "//chrome/browser:dev_ui_browser_resources_grit", "//chrome/browser/resources/quota_internals:quota_internals_resources", "//chrome/common:resources_grit", "//components/resources:components_resources_grit", diff --git a/src/core/net/webui_controller_factory_qt.cpp b/src/core/net/webui_controller_factory_qt.cpp index b645a6d72..507a4c1b2 100644 --- a/src/core/net/webui_controller_factory_qt.cpp +++ b/src/core/net/webui_controller_factory_qt.cpp @@ -50,6 +50,7 @@ #include "build/build_config.h" #include "chrome/browser/accessibility/accessibility_ui.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/webui/device_log_ui.h" #include "chrome/browser/ui/webui/devtools_ui.h" #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" #include "chrome/browser/ui/webui/quota_internals/quota_internals_ui.h" @@ -143,6 +144,9 @@ WebUIFactoryFunction GetWebUIFactoryFunction(WebUI *web_ui, Profile *profile, co if (url.host_piece() == chrome::kChromeUIUserActionsHost) return &NewWebUI; + if (url.host_piece() == chrome::kChromeUIDeviceLogHost) + return &NewWebUI; + // if (url.host_piece() == chrome::kChromeUIInspectHost) // return &NewWebUI; // diff --git a/src/core/web_usb_detector_qt.cpp b/src/core/web_usb_detector_qt.cpp new file mode 100644 index 000000000..0fd898333 --- /dev/null +++ b/src/core/web_usb_detector_qt.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "web_usb_detector_qt.h" + +#include "qtwebenginecoreglobal_p.h" + +#include "content/public/browser/device_service.h" +#include "device/base/features.h" + +WebUsbDetectorQt::WebUsbDetectorQt() = default; + +WebUsbDetectorQt::~WebUsbDetectorQt() = default; + +void WebUsbDetectorQt::Initialize() +{ +#if defined(OS_WIN) + // The WebUSB device detector is disabled on Windows due to jank and hangs + // caused by enumerating devices. The new USB backend is designed to resolve + // these issues so enable it for testing. https://crbug.com/656702 + if (!base::FeatureList::IsEnabled(device::kNewUsbBackend)) + return; +#endif // defined(OS_WIN) + + if (!m_deviceManager) { + // Receive mojo::Remote from DeviceService. + content::GetDeviceService().BindUsbDeviceManager( + m_deviceManager.BindNewPipeAndPassReceiver()); + } + DCHECK(m_deviceManager); + m_deviceManager.set_disconnect_handler(base::BindOnce( + &WebUsbDetectorQt::OnDeviceManagerConnectionError, base::Unretained(this))); + + // Listen for added/removed device events. + DCHECK(!m_clientReceiver.is_bound()); + m_deviceManager->SetClient(m_clientReceiver.BindNewEndpointAndPassRemote()); +} + +void WebUsbDetectorQt::OnDeviceAdded(device::mojom::UsbDeviceInfoPtr device_info) +{ + Q_UNUSED(device_info); + QT_NOT_YET_IMPLEMENTED +} + +void WebUsbDetectorQt::OnDeviceRemoved(device::mojom::UsbDeviceInfoPtr device_info) +{ + Q_UNUSED(device_info); + QT_NOT_YET_IMPLEMENTED +} + +void WebUsbDetectorQt::OnDeviceManagerConnectionError() +{ + m_deviceManager.reset(); + m_clientReceiver.reset(); + + // Try to reconnect the device manager. + Initialize(); +} diff --git a/src/core/web_usb_detector_qt.h b/src/core/web_usb_detector_qt.h new file mode 100644 index 000000000..b1d7279bf --- /dev/null +++ b/src/core/web_usb_detector_qt.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WEB_USB_DETECTOR_QT_H +#define WEB_USB_DETECTOR_QT_H + +#include "base/macros.h" +#include "mojo/public/cpp/bindings/associated_receiver.h" +#include "mojo/public/cpp/bindings/pending_remote.h" +#include "mojo/public/cpp/bindings/remote.h" +#include "services/device/public/mojom/usb_manager.mojom.h" +#include "services/device/public/mojom/usb_manager_client.mojom.h" +#include "url/gurl.h" + +class WebUsbDetectorQt : public device::mojom::UsbDeviceManagerClient +{ +public: + WebUsbDetectorQt(); + ~WebUsbDetectorQt() override; + + void Initialize(); + +private: + // device::mojom::UsbDeviceManagerClient implementation. + void OnDeviceAdded(device::mojom::UsbDeviceInfoPtr device_info) override; + void OnDeviceRemoved(device::mojom::UsbDeviceInfoPtr device_info) override; + + void OnDeviceManagerConnectionError(); + + // Connection to |device_manager_instance_|. + mojo::Remote m_deviceManager; + mojo::AssociatedReceiver m_clientReceiver { this }; + + base::WeakPtrFactory m_weakFactory { this }; + + DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorQt); +}; + +#endif // WEB_USB_DETECTOR_QT_H diff --git a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp index dc0efcfb6..c996507cc 100644 --- a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp +++ b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp @@ -3132,7 +3132,7 @@ void tst_QWebEngineView::webUIURLs_data() QTest::newRow("conversion-internals") << QUrl("chrome://conversion-internals") << true; QTest::newRow("crashes") << QUrl("chrome://crashes") << false; QTest::newRow("credits") << QUrl("chrome://credits") << false; - QTest::newRow("device-log") << QUrl("chrome://device-log") << false; + QTest::newRow("device-log") << QUrl("chrome://device-log") << true; QTest::newRow("devices") << QUrl("chrome://devices") << false; QTest::newRow("dino") << QUrl("chrome://dino") << false; // It works but this is an error page QTest::newRow("discards") << QUrl("chrome://discards") << false; -- cgit v1.2.3