From 15da77f4c4c1754e2b0af298e4b1f98d7fb906ad Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 23 Aug 2023 06:54:22 -0400 Subject: [PATCH] Ladybird+LibWebView: Migrate file APIs to LibWebView callbacks This also sets the default callback to do what every non-Serenity browser is doing, rather than copy-pasting this callback into every implementation. The callback is still available for any platform which might want to override the default behavior. For example, OOPWV now overrides this callback to use FileSystemAccessClient. --- Ladybird/AppKit/UI/LadybirdWebViewBridge.cpp | 10 ---------- Ladybird/AppKit/UI/LadybirdWebViewBridge.h | 1 - Ladybird/Qt/WebContentView.cpp | 9 --------- Ladybird/Qt/WebContentView.h | 1 - .../LibWebView/OutOfProcessWebView.cpp | 18 +++++++++--------- .../Libraries/LibWebView/OutOfProcessWebView.h | 1 - .../LibWebView/ViewImplementation.cpp | 9 +++++++++ .../Libraries/LibWebView/ViewImplementation.h | 2 +- .../Libraries/LibWebView/WebContentClient.cpp | 3 ++- Userland/Utilities/headless-browser.cpp | 11 ----------- 10 files changed, 21 insertions(+), 44 deletions(-) diff --git a/Ladybird/AppKit/UI/LadybirdWebViewBridge.cpp b/Ladybird/AppKit/UI/LadybirdWebViewBridge.cpp index fb0307fc36..25966f9750 100644 --- a/Ladybird/AppKit/UI/LadybirdWebViewBridge.cpp +++ b/Ladybird/AppKit/UI/LadybirdWebViewBridge.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -197,15 +196,6 @@ void WebViewBridge::notify_server_did_leave_tooltip_area(Badge, DeprecatedString const& path, i32 request_id) -{ - auto file = Core::File::open(path, Core::File::OpenMode::Read); - - if (file.is_error()) - client().async_handle_file_return(file.error().code(), {}, request_id); - else - client().async_handle_file_return(0, IPC::File(*file.value()), request_id); -} void WebViewBridge::notify_server_did_finish_handling_input_event(bool) { diff --git a/Ladybird/AppKit/UI/LadybirdWebViewBridge.h b/Ladybird/AppKit/UI/LadybirdWebViewBridge.h index b45d7c7264..799af0df07 100644 --- a/Ladybird/AppKit/UI/LadybirdWebViewBridge.h +++ b/Ladybird/AppKit/UI/LadybirdWebViewBridge.h @@ -72,7 +72,6 @@ private: virtual void notify_server_did_request_scroll_into_view(Badge, Gfx::IntRect const&) override; virtual void notify_server_did_enter_tooltip_area(Badge, Gfx::IntPoint, DeprecatedString const&) override; virtual void notify_server_did_leave_tooltip_area(Badge) override; - virtual void notify_server_did_request_file(Badge, DeprecatedString const& path, i32) override; virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) override; virtual void update_zoom() override; diff --git a/Ladybird/Qt/WebContentView.cpp b/Ladybird/Qt/WebContentView.cpp index 2f13485779..41eb0cccad 100644 --- a/Ladybird/Qt/WebContentView.cpp +++ b/Ladybird/Qt/WebContentView.cpp @@ -725,15 +725,6 @@ void WebContentView::notify_server_did_leave_tooltip_area(Badge, DeprecatedString const& path, i32 request_id) -{ - auto file = Core::File::open(path, Core::File::OpenMode::Read); - if (file.is_error()) - client().async_handle_file_return(file.error().code(), {}, request_id); - else - client().async_handle_file_return(0, IPC::File(*file.value()), request_id); -} - Gfx::IntRect WebContentView::viewport_rect() const { return m_viewport_rect; diff --git a/Ladybird/Qt/WebContentView.h b/Ladybird/Qt/WebContentView.h index a6af261644..7b737d9f5d 100644 --- a/Ladybird/Qt/WebContentView.h +++ b/Ladybird/Qt/WebContentView.h @@ -86,7 +86,6 @@ public: virtual void notify_server_did_request_scroll_into_view(Badge, Gfx::IntRect const&) override; virtual void notify_server_did_enter_tooltip_area(Badge, Gfx::IntPoint, DeprecatedString const&) override; virtual void notify_server_did_leave_tooltip_area(Badge) override; - virtual void notify_server_did_request_file(Badge, DeprecatedString const& path, i32) override; virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) override; signals: diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp index 45c1708876..167a465a5c 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp @@ -28,6 +28,15 @@ OutOfProcessWebView::OutOfProcessWebView() set_focus_policy(GUI::FocusPolicy::StrongFocus); create_client(); + + on_request_file = [this](auto const& path, auto request_id) { + auto file = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), path); + + if (file.is_error()) + client().async_handle_file_return(file.error().code(), {}, request_id); + else + client().async_handle_file_return(0, IPC::File(file.value().stream()), request_id); + }; } OutOfProcessWebView::~OutOfProcessWebView() = default; @@ -231,15 +240,6 @@ void OutOfProcessWebView::notify_server_did_leave_tooltip_area(Badgehide_tooltip(); } -void OutOfProcessWebView::notify_server_did_request_file(Badge, DeprecatedString const& path, i32 request_id) -{ - auto file = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), path); - if (file.is_error()) - client().async_handle_file_return(file.error().code(), {}, request_id); - else - client().async_handle_file_return(0, IPC::File(file.value().stream()), request_id); -} - void OutOfProcessWebView::did_scroll() { client().async_set_viewport_rect(visible_content_rect()); diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.h b/Userland/Libraries/LibWebView/OutOfProcessWebView.h index 454f760880..b6b7a46dee 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.h @@ -92,7 +92,6 @@ private: virtual void notify_server_did_request_scroll_into_view(Badge, Gfx::IntRect const&) override; virtual void notify_server_did_enter_tooltip_area(Badge, Gfx::IntPoint, DeprecatedString const&) override; virtual void notify_server_did_leave_tooltip_area(Badge) override; - virtual void notify_server_did_request_file(Badge, DeprecatedString const& path, i32) override; virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) override; virtual Gfx::IntRect viewport_rect() const override; diff --git a/Userland/Libraries/LibWebView/ViewImplementation.cpp b/Userland/Libraries/LibWebView/ViewImplementation.cpp index 7bcca7e89e..a5fd85804f 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.cpp +++ b/Userland/Libraries/LibWebView/ViewImplementation.cpp @@ -25,6 +25,15 @@ ViewImplementation::ViewImplementation() // happen to be visiting crashy websites a lot. this->m_crash_count = 0; }).release_value_but_fixme_should_propagate_errors(); + + on_request_file = [this](auto const& path, auto request_id) { + auto file = Core::File::open(path, Core::File::OpenMode::Read); + + if (file.is_error()) + client().async_handle_file_return(file.error().code(), {}, request_id); + else + client().async_handle_file_return(0, IPC::File(*file.value()), request_id); + }; } WebContentClient& ViewImplementation::client() diff --git a/Userland/Libraries/LibWebView/ViewImplementation.h b/Userland/Libraries/LibWebView/ViewImplementation.h index 97327c1c38..fd529be845 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.h +++ b/Userland/Libraries/LibWebView/ViewImplementation.h @@ -104,6 +104,7 @@ public: Function on_title_change; Function on_load_start; Function on_load_finish; + Function on_request_file; Function on_navigate_back; Function on_navigate_forward; Function on_refresh; @@ -143,7 +144,6 @@ public: virtual void notify_server_did_request_scroll_into_view(Badge, Gfx::IntRect const&) = 0; virtual void notify_server_did_enter_tooltip_area(Badge, Gfx::IntPoint, DeprecatedString const&) = 0; virtual void notify_server_did_leave_tooltip_area(Badge) = 0; - virtual void notify_server_did_request_file(Badge, DeprecatedString const& path, i32) = 0; virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) = 0; virtual Gfx::IntRect viewport_rect() const = 0; diff --git a/Userland/Libraries/LibWebView/WebContentClient.cpp b/Userland/Libraries/LibWebView/WebContentClient.cpp index aac70309ad..9bef4e8690 100644 --- a/Userland/Libraries/LibWebView/WebContentClient.cpp +++ b/Userland/Libraries/LibWebView/WebContentClient.cpp @@ -366,7 +366,8 @@ Messages::WebContentClient::DidRequestFullscreenWindowResponse WebContentClient: void WebContentClient::did_request_file(DeprecatedString const& path, i32 request_id) { - m_view.notify_server_did_request_file({}, path, request_id); + if (m_view.on_request_file) + m_view.on_request_file(path, request_id); } void WebContentClient::did_finish_handling_input_event(bool event_was_accepted) diff --git a/Userland/Utilities/headless-browser.cpp b/Userland/Utilities/headless-browser.cpp index e03a6fec06..a1b0ad0f32 100644 --- a/Userland/Utilities/headless-browser.cpp +++ b/Userland/Utilities/headless-browser.cpp @@ -112,17 +112,6 @@ private: void notify_server_did_request_scroll_into_view(Badge, Gfx::IntRect const&) override { } void notify_server_did_enter_tooltip_area(Badge, Gfx::IntPoint, DeprecatedString const&) override { } void notify_server_did_leave_tooltip_area(Badge) override { } - - void notify_server_did_request_file(Badge, DeprecatedString const& path, i32 request_id) override - { - auto file = Core::File::open(path, Core::File::OpenMode::Read); - - if (file.is_error()) - client().async_handle_file_return(file.error().code(), {}, request_id); - else - client().async_handle_file_return(0, IPC::File(*file.value()), request_id); - } - void notify_server_did_finish_handling_input_event(bool) override { } void update_zoom() override { } void create_client(WebView::EnableCallgrindProfiling) override { }