1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/input/native_web_keyboard_event.h"
#include "components/web_modal/web_contents_modal_dialog_host.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/keyboard_event_processing_result.h"
#include "content/public/browser/render_process_host.h"
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#include "chrome/browser/printing/print_preview_dialog_controller.h"
#endif
using content::WebContents;
// static
void DevToolsWindow::ToggleDevToolsWindow(BrowserWindowInterface* browser,
const DevToolsToggleAction& action,
DevToolsOpenedByAction opened_by) {
if (action.type() == DevToolsToggleAction::kToggle &&
browser->GetType() == BrowserWindowInterface::Type::TYPE_DEVTOOLS) {
browser->GetTabStripModel()->CloseAllTabs();
return;
}
auto* web_contents = browser->GetTabStripModel()->GetActiveWebContents();
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
auto* print_preview_web_contents =
printing::PrintPreviewDialogController::GetInstance()
->GetPrintPreviewForContents(web_contents);
if (print_preview_web_contents) {
web_contents = print_preview_web_contents;
}
#endif
ToggleDevToolsWindow(web_contents, nullptr,
action.type() == DevToolsToggleAction::kInspect, action,
"", opened_by);
}
// static
bool DevToolsWindow::HasFiredBeforeUnloadEventForDevToolsBrowser(
BrowserWindowInterface* browser) {
DCHECK(browser->GetType() == BrowserWindowInterface::Type::TYPE_DEVTOOLS);
// When FastUnloadController is used, devtools frontend will be detached
// from the browser window at this point which means we've already fired
// beforeunload.
if (browser->GetTabStripModel()->empty()) {
return true;
}
DevToolsWindow* window = AsDevToolsWindow(browser);
if (!window) {
return false;
}
return window->intercepted_page_beforeunload_;
}
// static
DevToolsWindow* DevToolsWindow::AsDevToolsWindow(
BrowserWindowInterface* browser) {
DCHECK(browser->GetType() == BrowserWindowInterface::Type::TYPE_DEVTOOLS);
if (browser->GetTabStripModel()->empty()) {
return nullptr;
}
WebContents* contents = browser->GetTabStripModel()->GetWebContentsAt(0);
return AsDevToolsWindow(contents);
}
content::KeyboardEventProcessingResult DevToolsWindow::PreHandleKeyboardEvent(
WebContents* source,
const input::NativeWebKeyboardEvent& event) {
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window) {
return inspected_window->PreHandleKeyboardEvent(event);
}
return content::KeyboardEventProcessingResult::NOT_HANDLED;
}
bool DevToolsWindow::HandleKeyboardEvent(
WebContents* source,
const input::NativeWebKeyboardEvent& event) {
if (event.windows_key_code == 0x08) {
// Do not navigate back in history on Windows (http://crbug.com/74156).
return true;
}
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
return inspected_window && inspected_window->HandleKeyboardEvent(event);
}
std::unique_ptr<content::EyeDropper> DevToolsWindow::OpenEyeDropper(
content::RenderFrameHost* render_frame_host,
content::EyeDropperListener* listener) {
BrowserWindow* window = GetInspectedBrowserWindow();
if (window) {
return window->OpenEyeDropper(render_frame_host, listener);
}
return nullptr;
}
BrowserWindow* DevToolsWindow::GetInspectedBrowserWindow() {
content::WebContents* inspected_web_contents = GetInspectedWebContents();
if (!inspected_web_contents) {
return nullptr;
}
Browser* browser = chrome::FindBrowserWithTab(inspected_web_contents);
return browser ? browser->window() : nullptr;
}
void DevToolsWindow::UpdateBrowserToolbar() {
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window) {
inspected_window->UpdateToolbar(nullptr);
}
}
void DevToolsWindow::UpdateBrowserWindow() {
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window) {
inspected_window->UpdateDevTools(GetInspectedWebContents());
}
}
void DevToolsWindow::RegisterModalDialogManager(
BrowserWindowInterface* browser) {
web_modal::WebContentsModalDialogManager::CreateForWebContents(
main_web_contents_);
web_modal::WebContentsModalDialogManager::FromWebContents(main_web_contents_)
->SetDelegate(browser->GetBrowserForMigrationOnly());
// Observer `browser` destruction/removal to reset `SetDelegate(nullptr)`
// before the dialog manager's `raw_ptr` becomes dangling.
if (!browser_list_observation_.IsObserving()) {
browser_list_observation_.Observe(BrowserList::GetInstance());
}
}
|