1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 09:57:36 +00:00

Browser+Ladybird+LibWebView: Move some common functions to LibWebView

The implementations of handle_web_content_process_crash and
take_screenshot are exactly the same across Browser and Ladybird. Let's
reduce some code duplication and move them to LibWebView.
This commit is contained in:
Timothy Flynn 2023-05-17 09:53:13 -04:00 committed by Andreas Kling
parent 5312a140fe
commit d8b14da380
12 changed files with 90 additions and 188 deletions

View file

@ -14,10 +14,8 @@
#include "CookieJar.h"
#include "InspectorWidget.h"
#include "Tab.h"
#include <AK/LexicalPath.h>
#include <Applications/Browser/BrowserWindowGML.h>
#include <LibConfig/Client.h>
#include <LibCore/DateTime.h>
#include <LibCore/StandardPaths.h>
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
@ -32,7 +30,6 @@
#include <LibGUI/TabWidget.h>
#include <LibGUI/ToolbarContainer.h>
#include <LibGUI/Widget.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWeb/Dump.h>
@ -259,22 +256,6 @@ void BrowserWindow::build_menus()
this);
m_inspect_dom_node_action->set_status_tip("Open inspector for this element");
m_take_visible_screenshot_action = GUI::Action::create(
"Take &Visible Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) {
if (auto result = take_screenshot(ScreenshotType::Visible); result.is_error())
GUI::MessageBox::show_error(this, DeprecatedString::formatted("{}", result.error()));
},
this);
m_take_visible_screenshot_action->set_status_tip("Save a screenshot of the visible portion of the current tab to the Downloads directory"sv);
m_take_full_screenshot_action = GUI::Action::create(
"Take &Full Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) {
if (auto result = take_screenshot(ScreenshotType::Full); result.is_error())
GUI::MessageBox::show_error(this, DeprecatedString::formatted("{}", result.error()));
},
this);
m_take_full_screenshot_action->set_status_tip("Save a screenshot of the entirety of the current tab to the Downloads directory"sv);
auto& inspect_menu = add_menu("&Inspect"_string.release_value_but_fixme_should_propagate_errors());
inspect_menu.add_action(*m_view_source_action);
inspect_menu.add_action(*m_inspect_dom_tree_action);
@ -655,10 +636,6 @@ Tab& BrowserWindow::create_new_tab(URL url, Web::HTML::ActivateTab activate)
return active_tab().view().get_session_storage_entries();
};
new_tab.on_take_screenshot = [this]() {
return active_tab().view().take_screenshot();
};
new_tab.load(url);
dbgln_if(SPAM_DEBUG, "Added new tab {:p}, loading {}", &new_tab, url);
@ -776,36 +753,6 @@ void BrowserWindow::event(Core::Event& event)
Window::event(event);
}
ErrorOr<void> BrowserWindow::take_screenshot(ScreenshotType type)
{
if (!active_tab().on_take_screenshot)
return {};
Gfx::ShareableBitmap bitmap;
switch (type) {
case ScreenshotType::Visible:
bitmap = active_tab().on_take_screenshot();
break;
case ScreenshotType::Full:
bitmap = active_tab().view().take_document_screenshot();
break;
}
if (!bitmap.is_valid())
return Error::from_string_view("Failed to take a screenshot of the current tab"sv);
LexicalPath path { Core::StandardPaths::downloads_directory() };
path = path.append(Core::DateTime::now().to_deprecated_string("screenshot-%Y-%m-%d-%H-%M-%S.png"sv));
auto encoded = TRY(Gfx::PNGWriter::encode(*bitmap.bitmap()));
auto screenshot_file = TRY(Core::File::open(path.string(), Core::File::OpenMode::Write));
TRY(screenshot_file->write_until_depleted(encoded));
return {};
}
void BrowserWindow::update_displayed_zoom_level()
{
VERIFY(m_zoom_menu);

View file

@ -41,8 +41,6 @@ public:
GUI::Action& view_source_action() { return *m_view_source_action; }
GUI::Action& inspect_dom_tree_action() { return *m_inspect_dom_tree_action; }
GUI::Action& inspect_dom_node_action() { return *m_inspect_dom_node_action; }
GUI::Action& take_visible_screenshot_action() { return *m_take_visible_screenshot_action; }
GUI::Action& take_full_screenshot_action() { return *m_take_full_screenshot_action; }
void content_filters_changed();
void autoplay_allowlist_changed();
@ -65,12 +63,6 @@ private:
void update_displayed_zoom_level();
enum class ScreenshotType {
Visible,
Full,
};
ErrorOr<void> take_screenshot(ScreenshotType);
RefPtr<GUI::Action> m_go_back_action;
RefPtr<GUI::Action> m_go_forward_action;
RefPtr<GUI::Action> m_go_home_action;
@ -80,8 +72,6 @@ private:
RefPtr<GUI::Action> m_view_source_action;
RefPtr<GUI::Action> m_inspect_dom_tree_action;
RefPtr<GUI::Action> m_inspect_dom_node_action;
RefPtr<GUI::Action> m_take_visible_screenshot_action;
RefPtr<GUI::Action> m_take_full_screenshot_action;
RefPtr<GUI::Menu> m_zoom_menu;

View file

@ -554,6 +554,26 @@ Tab::Tab(BrowserWindow& window)
on_tab_close_other_request(*this);
}));
auto take_visible_screenshot_action = GUI::Action::create(
"Take &Visible Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) {
if (auto result = view().take_screenshot(WebView::ViewImplementation::ScreenshotType::Visible); result.is_error()) {
auto error = String::formatted("{}", result.error()).release_value_but_fixme_should_propagate_errors();
GUI::MessageBox::show_error(&this->window(), error);
}
},
this);
take_visible_screenshot_action->set_status_tip("Save a screenshot of the visible portion of the current tab to the Downloads directory"sv);
auto take_full_screenshot_action = GUI::Action::create(
"Take &Full Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) {
if (auto result = view().take_screenshot(WebView::ViewImplementation::ScreenshotType::Full); result.is_error()) {
auto error = String::formatted("{}", result.error()).release_value_but_fixme_should_propagate_errors();
GUI::MessageBox::show_error(&this->window(), error);
}
},
this);
take_full_screenshot_action->set_status_tip("Save a screenshot of the entirety of the current tab to the Downloads directory"sv);
m_page_context_menu = GUI::Menu::construct();
m_page_context_menu->add_action(window.go_back_action());
m_page_context_menu->add_action(window.go_forward_action());
@ -562,8 +582,8 @@ Tab::Tab(BrowserWindow& window)
m_page_context_menu->add_action(window.copy_selection_action());
m_page_context_menu->add_action(window.select_all_action());
m_page_context_menu->add_separator();
m_page_context_menu->add_action(window.take_visible_screenshot_action());
m_page_context_menu->add_action(window.take_full_screenshot_action());
m_page_context_menu->add_action(move(take_visible_screenshot_action));
m_page_context_menu->add_action(move(take_full_screenshot_action));
m_page_context_menu->add_separator();
m_page_context_menu->add_action(window.view_source_action());
m_page_context_menu->add_action(window.inspect_dom_tree_action());

View file

@ -77,7 +77,6 @@ public:
Function<Vector<Web::Cookie::Cookie>()> on_get_cookies_entries;
Function<OrderedHashMap<DeprecatedString, DeprecatedString>()> on_get_local_storage_entries;
Function<OrderedHashMap<DeprecatedString, DeprecatedString>()> on_get_session_storage_entries;
Function<Gfx::ShareableBitmap()> on_take_screenshot;
void enable_webdriver_mode();