1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:27:43 +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

@ -35,30 +35,6 @@ OutOfProcessWebView::OutOfProcessWebView()
OutOfProcessWebView::~OutOfProcessWebView() = default;
void OutOfProcessWebView::handle_web_content_process_crash()
{
create_client();
VERIFY(m_client_state.client);
// Don't keep a stale backup bitmap around.
m_backup_bitmap = nullptr;
handle_resize();
StringBuilder builder;
builder.append("<html><head><title>Crashed: "sv);
builder.append(escape_html_entities(m_url.to_deprecated_string()));
builder.append("</title></head><body>"sv);
builder.append("<h1>Web page crashed"sv);
if (!m_url.host().is_empty()) {
builder.appendff(" on {}", escape_html_entities(m_url.host()));
}
builder.append("</h1>"sv);
auto escaped_url = escape_html_entities(m_url.to_deprecated_string());
builder.appendff("The web page <a href=\"{}\">{}</a> has crashed.<br><br>You can reload the page to try again.", escaped_url, escaped_url);
builder.append("</body></html>"sv);
load_html(builder.to_deprecated_string(), m_url);
}
void OutOfProcessWebView::create_client(EnableCallgrindProfiling)
{
m_client_state = {};
@ -591,18 +567,6 @@ void OutOfProcessWebView::set_window_size(Gfx::IntSize size)
client().async_set_window_size(size);
}
Gfx::ShareableBitmap OutOfProcessWebView::take_screenshot() const
{
if (auto* bitmap = m_client_state.has_usable_bitmap ? m_client_state.front_bitmap.bitmap.ptr() : m_backup_bitmap.ptr())
return bitmap->to_shareable_bitmap();
return {};
}
Gfx::ShareableBitmap OutOfProcessWebView::take_document_screenshot()
{
return client().take_document_screenshot();
}
void OutOfProcessWebView::focusin_event(GUI::FocusEvent&)
{
client().async_set_has_focus(true);

View file

@ -53,9 +53,6 @@ public:
void set_system_visibility_state(bool visible);
Gfx::ShareableBitmap take_screenshot() const;
Gfx::ShareableBitmap take_document_screenshot();
// This is a hint that tells OOPWV that the content will scale to the viewport size.
// In practice, this means that OOPWV may render scaled stale versions of the content while resizing.
void set_content_scales_to_viewport(bool);
@ -183,8 +180,6 @@ private:
virtual Gfx::IntRect viewport_rect() const override;
void handle_web_content_process_crash();
using InputEvent = Variant<GUI::KeyEvent, GUI::MouseEvent>;
void enqueue_input_event(InputEvent const&);
void process_next_input_event();

View file

@ -5,7 +5,11 @@
*/
#include <AK/Error.h>
#include <AK/LexicalPath.h>
#include <AK/String.h>
#include <LibCore/DateTime.h>
#include <LibCore/StandardPaths.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibWebView/ViewImplementation.h>
namespace WebView {
@ -286,4 +290,58 @@ void ViewImplementation::request_repaint()
client().async_paint(viewport_rect(), m_client_state.back_bitmap.id);
}
void ViewImplementation::handle_web_content_process_crash()
{
dbgln("WebContent process crashed!");
create_client();
VERIFY(m_client_state.client);
// Don't keep a stale backup bitmap around.
m_backup_bitmap = nullptr;
handle_resize();
StringBuilder builder;
builder.append("<html><head><title>Crashed: "sv);
builder.append(escape_html_entities(m_url.to_deprecated_string()));
builder.append("</title></head><body>"sv);
builder.append("<h1>Web page crashed"sv);
if (!m_url.host().is_empty()) {
builder.appendff(" on {}", escape_html_entities(m_url.host()));
}
builder.append("</h1>"sv);
auto escaped_url = escape_html_entities(m_url.to_deprecated_string());
builder.appendff("The web page <a href=\"{}\">{}</a> has crashed.<br><br>You can reload the page to try again.", escaped_url, escaped_url);
builder.append("</body></html>"sv);
load_html(builder.to_deprecated_string(), m_url);
}
ErrorOr<void> ViewImplementation::take_screenshot(ScreenshotType type)
{
Gfx::ShareableBitmap bitmap;
switch (type) {
case ScreenshotType::Visible:
if (auto* visible_bitmap = m_client_state.has_usable_bitmap ? m_client_state.front_bitmap.bitmap.ptr() : m_backup_bitmap.ptr())
bitmap = visible_bitmap->to_shareable_bitmap();
break;
case ScreenshotType::Full:
bitmap = client().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(TRY(Core::DateTime::now().to_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 {};
}
}

View file

@ -74,6 +74,12 @@ public:
void toggle_video_loop_state();
void toggle_video_controls_state();
enum class ScreenshotType {
Visible,
Full,
};
ErrorOr<void> take_screenshot(ScreenshotType);
virtual void notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize content_size) = 0;
virtual void notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id, Gfx::IntSize) = 0;
virtual void notify_server_did_invalidate_content_rect(Badge<WebContentClient>, Gfx::IntRect const&) = 0;
@ -157,6 +163,8 @@ protected:
ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(ReadonlySpan<String> candidate_web_content_paths, EnableCallgrindProfiling = EnableCallgrindProfiling::No, IsLayoutTestMode = IsLayoutTestMode::No);
#endif
void handle_web_content_process_crash();
struct SharedBitmap {
i32 id { -1 };
i32 pending_paints { 0 };