Skip to content

Truly independent web browser

LibWeb+LibWebView: Add a file extension to extensionless saved images

Andreas Klingb795615e6603
diff --git a/Libraries/LibCore/MimeData.cpp b/Libraries/LibCore/MimeData.cpp
index 1ed1332b287..ef2d206b5be 100644
--- a/Libraries/LibCore/MimeData.cpp
+++ b/Libraries/LibCore/MimeData.cpp
@@ -62,11 +62,11 @@ static auto make_registered_mime_types()
{ .name = "image/gif"sv, .common_extensions = { ".gif"sv } },
{ .name = "image/j2c"sv, .common_extensions = { ".j2c"sv, ".j2k"sv } },
{ .name = "image/jp2"sv, .common_extensions = { ".jp2"sv, ".jpf"sv, ".jpx"sv } },
- { .name = "image/jpeg"sv, .common_extensions = { ".jpg"sv, ".jpeg"sv } },
+ { .name = "image/jpeg"sv, .common_extensions = { ".jpg"sv, ".jpeg"sv, ".jfif"sv } },
{ .name = "image/jxl"sv, .common_extensions = { ".jxl"sv } },
{ .name = "image/png"sv, .common_extensions = { ".png"sv } },
{ .name = "image/svg+xml"sv, .common_extensions = { ".svg"sv } },
- { .name = "image/tiff"sv, .common_extensions = { ".tiff"sv } },
+ { .name = "image/tiff"sv, .common_extensions = { ".tiff"sv, ".tif"sv } },
{ .name = "image/webp"sv, .common_extensions = { ".webp"sv } },
{ .name = "image/x-icon"sv, .common_extensions = { ".ico"sv } },
{ .name = "image/x-ilbm"sv, .common_extensions = { ".iff"sv, ".lbm"sv } },
@@ -110,4 +110,13 @@ StringView guess_mime_type_based_on_filename(StringView path)
return "application/octet-stream"sv;
}
+Optional<StringView> common_extension_for_mime_type(StringView mime_type_name)
+{
+ for (auto const& mime_type : registered_mime_types()) {
+ if (mime_type.name == mime_type_name)
+ return mime_type.common_extensions.first();
+ }
+ return {};
+}
+
}
diff --git a/Libraries/LibCore/MimeData.h b/Libraries/LibCore/MimeData.h
index 4e5737e9bb8..cb811b33cce 100644
--- a/Libraries/LibCore/MimeData.h
+++ b/Libraries/LibCore/MimeData.h
@@ -7,11 +7,13 @@
#pragma once
+#include <AK/Optional.h>
#include <AK/StringView.h>
#include <LibCore/Export.h>
namespace Core {
CORE_API StringView guess_mime_type_based_on_filename(StringView);
+CORE_API Optional<StringView> common_extension_for_mime_type(StringView);
}
diff --git a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp b/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
index 52752d0835e..d88210c634a 100644
--- a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
+++ b/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
@@ -26,6 +26,7 @@
#include <LibWeb/Platform/ImageCodecPlugin.h>
#include <LibWeb/SVG/SVGDecodedImageData.h>
#include <LibWebCommon/Fetch/Infrastructure/HTTP/Statuses.h>
+#include <LibWebCommon/MimeSniff/Resource.h>
namespace Web::HTML {
@@ -191,6 +192,8 @@ void SharedResourceRequest::fetch_resource(NonnullRefPtr<Fetch::Infrastructure::
auto const is_svg_image = extracted_mime_type.has_value()
? extracted_mime_type.value().essence() == "image/svg+xml"sv
: request->url().basename().ends_with(".svg"sv);
+ if (extracted_mime_type.has_value())
+ self->m_image_mime_type = extracted_mime_type->essence();
self->handle_successful_fetch(request->url(), is_svg_image ? IsSVGImage::Yes : IsSVGImage::No, move(data), image_data_is_cors_cross_origin);
});
auto process_body_error = GC::create_function(GC::Heap::the(), [weak_this](JS::Value) {
@@ -322,6 +325,8 @@ void SharedResourceRequest::handle_successful_fetch(URL::URL const& url_string,
// FIXME: Bring this closer to spec.
if (is_svg_image == IsSVGImage::Yes) {
+ m_image_mime_type = "image/svg+xml"_string;
+
// NB: The decode waits for the image's document to load completely, which it may never do, and its promise keeps
// both handlers. So they hold this request weakly: its document keeps it alive for as long as it is needed.
SVG::SVGDecodedImageData::decode(m_page, url_string, data)
@@ -340,6 +345,11 @@ void SharedResourceRequest::handle_successful_fetch(URL::URL const& url_string,
return;
}
+ // NB: The image's own bytes are a better source than the server's claim, which is often missing or wrong. Formats
+ // that sniffing does not know keep the claimed type.
+ if (auto sniffed_type = MimeSniff::Resource::sniff(data, { .sniffing_context = MimeSniff::SniffingContext::Image }); sniffed_type.is_image())
+ m_image_mime_type = sniffed_type.essence();
+
// NB: Keeping the encoded data lets the decoded image data discard its frame while it is not in use. The decode
// request copies the bytes before it returns, so the handler can own the buffer they are read from.
auto encoded_data = make<ByteBuffer>(move(data));
diff --git a/Libraries/LibWeb/HTML/SharedResourceRequest.h b/Libraries/LibWeb/HTML/SharedResourceRequest.h
index b163fbbb2a1..d5e93294125 100644
--- a/Libraries/LibWeb/HTML/SharedResourceRequest.h
+++ b/Libraries/LibWeb/HTML/SharedResourceRequest.h
@@ -46,6 +46,7 @@ public:
virtual ~SharedResourceRequest() override;
URL::URL const& url() const { return m_key.url; }
+ Optional<String> const& image_mime_type() const { return m_image_mime_type; }
[[nodiscard]] bool can_be_shared() const { return m_can_be_shared; }
[[nodiscard]] GC::Ptr<DecodedImageData> image_data() const;
@@ -103,6 +104,7 @@ private:
};
State m_state { State::New };
+ Optional<String> m_image_mime_type;
GC::Ref<Page> m_page;
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 2bd5170aaff..b215349da5f 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -3730,6 +3730,8 @@ GC::Ptr<JS::Object> Internals::take_context_menu_request()
object->define_direct_property("y"_utf16_fly_string, JS::Value(request->position.y().to_double()), JS::default_attributes);
if (request->link_url.has_value())
object->define_direct_property("linkURL"_utf16_fly_string, JS::PrimitiveString::create(vm(), utf16_string_from_url_ascii(request->link_url->serialize())), JS::default_attributes);
+ if (request->image_mime_type.has_value())
+ object->define_direct_property("imageMimeType"_utf16_fly_string, JS::PrimitiveString::create(vm(), Utf16String::from_utf8(*request->image_mime_type)), JS::default_attributes);
if (request->media.has_value()) {
object->define_direct_property("isPictureInPicture"_utf16_fly_string, JS::Value(request->media->is_picture_in_picture), JS::default_attributes);
object->define_direct_property("canEnterPictureInPicture"_utf16_fly_string, JS::Value(request->media->can_enter_picture_in_picture), JS::default_attributes);
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 883203712e2..f39fb14f75c 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -54,12 +54,14 @@
#include <LibWeb/HTML/HTMLSummaryElement.h>
#include <LibWeb/HTML/HTMLTextAreaElement.h>
#include <LibWeb/HTML/HTMLVideoElement.h>
+#include <LibWeb/HTML/ImageRequest.h>
#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/HTML/LocalTraversableNavigable.h>
#include <LibWeb/HTML/NavigableContainer.h>
#include <LibWeb/HTML/Navigator.h>
#include <LibWeb/HTML/PaintConfig.h>
#include <LibWeb/HTML/Scripting/Environments.h>
+#include <LibWeb/HTML/SharedResourceRequest.h>
#include <LibWeb/HTML/TextDirectiveGenerator.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Layout/LayoutRustFFI.h>
@@ -4091,9 +4093,13 @@ void EventHandler::maybe_show_context_menu(GC::Ref<DOM::Node> node, MouseEventCo
if (link_url.has_value())
link_navigation = HTML::prepare_navigation_from_document(*document, *link_url, link->hyperlink_element_utils_element().hyperlink_referrer_policy());
+ Optional<String> image_mime_type;
+ if (auto request = image_element->current_request().shared_resource_request())
+ image_mime_type = request->image_mime_type();
+
auto navigation = HTML::prepare_navigation_from_document(image_element->document(), image_url.release_value(), ReferrerPolicy::ReferrerPolicy::EmptyString);
- m_navigable->page().record_context_menu_request({}, { .kind = Page::ContextMenuRequest::Kind::Image, .target = *image_element, .position = page_viewport_position, .link_url = move(link_url) });
- m_navigable->page().client().page_did_request_image_context_menu(local_root_id, page_viewport_position, move(navigation), "", modifiers, bitmap, move(link_navigation));
+ m_navigable->page().record_context_menu_request({}, { .kind = Page::ContextMenuRequest::Kind::Image, .target = *image_element, .position = page_viewport_position, .link_url = move(link_url), .image_mime_type = image_mime_type });
+ m_navigable->page().client().page_did_request_image_context_menu(local_root_id, page_viewport_position, move(navigation), "", modifiers, bitmap, move(link_navigation), move(image_mime_type));
} else if (link) {
// Right-clicking link text without an existing selection first selects the word under the pointer, as the
// ordinary page context menu does. A selected range is still only offered when the click lands within it.
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index 3ba8a1fb631..02d2000e798 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -439,6 +439,7 @@ public:
CSSPixelPoint position;
Optional<Web::MediaContextMenu> media {};
Optional<URL::URL> link_url {};
+ Optional<String> image_mime_type {};
};
void record_context_menu_request(Badge<EventHandler>, ContextMenuRequest);
void clear_context_menu_request() { m_context_menu_request.clear(); }
@@ -840,7 +841,7 @@ public:
virtual void page_did_change_accessibility_focus(Web::UniqueNodeID) { }
virtual void page_did_request_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, ContextMenuForInputEventsTarget, [[maybe_unused]] Optional<Utf16String> misspelled_word, [[maybe_unused]] GC::Ptr<DOM::Range> selected_text_range) { }
virtual void page_did_request_link_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, HTML::PreparedNavigationDescriptor, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers, [[maybe_unused]] GC::Ptr<DOM::Range> selected_text_range) { }
- virtual void page_did_request_image_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, HTML::PreparedNavigationDescriptor, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers, Optional<Gfx::Bitmap const*>, [[maybe_unused]] Optional<HTML::PreparedNavigationDescriptor> link_navigation) { }
+ virtual void page_did_request_image_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, HTML::PreparedNavigationDescriptor, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers, Optional<Gfx::Bitmap const*>, [[maybe_unused]] Optional<HTML::PreparedNavigationDescriptor> link_navigation, [[maybe_unused]] Optional<String> image_mime_type) { }
virtual void page_did_request_media_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers, Page::MediaContextMenu const&, Optional<HTML::PreparedNavigationDescriptor>) { }
virtual void page_did_click_link(HTML::PreparedNavigationDescriptor, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers) { }
virtual void page_did_middle_click_link(HTML::PreparedNavigationDescriptor, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers) { }
diff --git a/Libraries/LibWebView/URL.cpp b/Libraries/LibWebView/URL.cpp
index 445a66862aa..00317d77102 100644
--- a/Libraries/LibWebView/URL.cpp
+++ b/Libraries/LibWebView/URL.cpp
@@ -11,6 +11,7 @@
#include <AK/IPv6Address.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
+#include <LibCore/MimeData.h>
#include <LibFileSystem/FileSystem.h>
#include <LibURL/Parser.h>
#include <LibURL/PublicSuffixData.h>
@@ -444,4 +445,22 @@ ByteString url_text_to_copy(URL::URL const& url)
return url_text;
}
+ByteString image_filename_with_extension(ByteString filename, StringView image_mime_type)
+{
+ // NB: The type comes from the page's process, so only an image's type may pick an extension here.
+ if (!image_mime_type.starts_with("image/"sv))
+ return filename;
+ auto extension = Core::common_extension_for_mime_type(image_mime_type);
+ if (!extension.has_value())
+ return filename;
+
+ // A name with an extension we know stays as the server gave it.
+ if (Core::guess_mime_type_based_on_filename(filename) != "application/octet-stream"sv)
+ return filename;
+
+ if (filename.is_empty())
+ filename = "download"sv;
+ return ByteString::formatted("{}{}", filename, *extension);
+}
+
}
diff --git a/Libraries/LibWebView/URL.h b/Libraries/LibWebView/URL.h
index fdf767236e5..ff5fc3a4133 100644
--- a/Libraries/LibWebView/URL.h
+++ b/Libraries/LibWebView/URL.h
@@ -54,5 +54,6 @@ enum class URLType {
};
WEBVIEW_API URLType url_type(URL::URL const&);
WEBVIEW_API ByteString url_text_to_copy(URL::URL const&);
+WEBVIEW_API ByteString image_filename_with_extension(ByteString filename, StringView image_mime_type);
}
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index cc45a66749f..b9cd40233dc 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -5556,11 +5556,13 @@ void ViewImplementation::did_request_link_context_menu(Badge<WebContentPage>, Gf
open_context_menu(move(menu), content_position);
}
-void ViewImplementation::download_url(URL::URL const& url, PromptForPath prompt_for_path)
+void ViewImplementation::download_url(URL::URL const& url, PromptForPath prompt_for_path, Optional<String> const& image_mime_type)
{
// URLs with an opaque path, such as data: URLs, do not contain a usable filename, so the default filename is
// used instead.
auto suggested_filename = url.has_an_opaque_path() ? ByteString {} : url.basename();
+ if (image_mime_type.has_value())
+ suggested_filename = image_filename_with_extension(move(suggested_filename), *image_mime_type);
if (prompt_for_path == PromptForPath::No) {
if (auto download_path = Application::the().default_path_for_downloaded_file(suggested_filename); !download_path.is_error())
@@ -5577,7 +5579,7 @@ void ViewImplementation::download_url(URL::URL const& url, PromptForPath prompt_
});
}
-void ViewImplementation::did_request_image_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation)
+void ViewImplementation::did_request_image_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation, Optional<String> image_mime_type)
{
prepare_inspect_element_action(content_position);
++m_context_menu_request_id;
@@ -5607,8 +5609,8 @@ void ViewImplementation::did_request_image_context_menu(Badge<WebContentPage>, G
menu->add_separator();
menu->add_action(move(copy_image_action));
menu->add_action(create_copy_url_action("Copy Image Address"sv, url));
- menu->add_action(Action::create("Save Image As…"sv, ActionID::SaveImage, [this, url]() {
- download_url(url, PromptForPath::Yes);
+ menu->add_action(Action::create("Save Image As…"sv, ActionID::SaveImage, [this, url, image_mime_type = move(image_mime_type)]() {
+ download_url(url, PromptForPath::Yes, image_mime_type);
}));
menu->add_separator();
menu->add_action(*m_inspect_element_action);
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 037d18b5339..b4081425152 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -699,7 +699,7 @@ public:
void reset_spell_checking();
void did_request_link_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor, Optional<u64> text_fragment_generation_id);
void did_generate_text_fragment_url(u64 request_id, Optional<URL::URL>);
- void did_request_image_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation);
+ void did_request_image_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation, Optional<String> image_mime_type);
void did_request_media_context_menu(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, Web::MediaContextMenu, Optional<Web::HTML::PreparedNavigationDescriptor>);
void did_request_color_picker(Badge<WebContentPage>, WebContentPage& requesting_page, Color current_color);
@@ -876,7 +876,7 @@ protected:
No,
Yes,
};
- void download_url(URL::URL const&, PromptForPath);
+ void download_url(URL::URL const&, PromptForPath, Optional<String> const& image_mime_type = {});
struct SharedBitmap {
i32 id { -1 };
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 8b44215499b..6767dfce992 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2996,7 +2996,7 @@ void WebContentPage::did_generate_text_fragment_url(u64 request_id, Optional<URL
view().did_generate_text_fragment_url(request_id, move(url));
}
-void WebContentPage::did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation)
+void WebContentPage::did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation, Optional<String> image_mime_type)
{
auto verified_navigation = navigation_from_page(client(), move(navigation));
if (!verified_navigation.has_value())
@@ -3006,7 +3006,7 @@ void WebContentPage::did_request_image_context_menu(Web::HTML::CrossProcessId lo
if (link_navigation.has_value())
verified_link_navigation = navigation_from_page(client(), link_navigation.release_value());
if (auto target = view_position(local_root_id, content_position); target.has_value())
- target->view.did_request_image_context_menu({}, target->position, verified_navigation.release_value(), move(bitmap), move(verified_link_navigation));
+ target->view.did_request_image_context_menu({}, target->position, verified_navigation.release_value(), move(bitmap), move(verified_link_navigation), move(image_mime_type));
}
void WebContentPage::did_request_media_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, ByteString, unsigned, Web::MediaContextMenu menu, Optional<Web::HTML::PreparedNavigationDescriptor> navigation)
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index 70e003f0d64..03f4e63f998 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -365,7 +365,7 @@ private:
virtual void did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word, Optional<u64> text_fragment_generation_id) override;
virtual void did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned, Optional<u64> text_fragment_generation_id) override;
virtual void did_generate_text_fragment_url(u64 request_id, Optional<URL::URL> url) override;
- virtual void did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation) override;
+ virtual void did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation, Optional<String> image_mime_type) override;
virtual void did_request_media_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, ByteString, unsigned, Web::MediaContextMenu menu, Optional<Web::HTML::PreparedNavigationDescriptor> navigation) override;
virtual void did_get_highlighted_source(String html) override;
virtual void did_get_debugger_environments(u64 request_id, Optional<String> error, Vector<DebuggerEnvironment> environments) override;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 6f82c2ef9d9..96f2b1710d4 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -1304,13 +1304,13 @@ void PageClient::page_did_request_link_context_menu(Web::HTML::CrossProcessId lo
client().async_did_request_link_context_menu(m_id, local_root_id, page().css_to_device_point(content_position).to_type<int>(), move(navigation), target, modifiers, text_fragment_generation_id);
}
-void PageClient::page_did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString const& target, unsigned modifiers, Optional<Gfx::Bitmap const*> bitmap_pointer, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation)
+void PageClient::page_did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString const& target, unsigned modifiers, Optional<Gfx::Bitmap const*> bitmap_pointer, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation, Optional<String> image_mime_type)
{
Optional<Gfx::ShareableBitmap> bitmap;
if (bitmap_pointer.has_value() && bitmap_pointer.value())
bitmap = bitmap_pointer.value()->to_shareable_bitmap();
- client().async_did_request_image_context_menu(m_id, local_root_id, page().css_to_device_point(content_position).to_type<int>(), move(navigation), target, modifiers, bitmap, move(link_navigation));
+ client().async_did_request_image_context_menu(m_id, local_root_id, page().css_to_device_point(content_position).to_type<int>(), move(navigation), target, modifiers, bitmap, move(link_navigation), move(image_mime_type));
}
void PageClient::page_did_request_media_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, ByteString const& target, unsigned modifiers, Web::Page::MediaContextMenu const& menu, Optional<Web::HTML::PreparedNavigationDescriptor> navigation)
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index 30d42034deb..5f91b52c8d6 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -275,7 +275,7 @@ private:
virtual void page_did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, Web::ContextMenuForInputEventsTarget, Optional<Utf16String> misspelled_word, GC::Ptr<Web::DOM::Range> selected_text_range) override;
virtual void page_did_request_spelling_menu_action_for_testing(Web::SpellingMenuActionForTesting, u32 guess_index) override;
virtual void page_did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, Web::HTML::PreparedNavigationDescriptor, ByteString const& target, unsigned modifiers, GC::Ptr<Web::DOM::Range> selected_text_range) override;
- virtual void page_did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, Web::HTML::PreparedNavigationDescriptor, ByteString const& target, unsigned modifiers, Optional<Gfx::Bitmap const*>, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation) override;
+ virtual void page_did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, Web::HTML::PreparedNavigationDescriptor, ByteString const& target, unsigned modifiers, Optional<Gfx::Bitmap const*>, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation, Optional<String> image_mime_type) override;
virtual void page_did_request_media_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, ByteString const& target, unsigned modifiers, Web::Page::MediaContextMenu const&, Optional<Web::HTML::PreparedNavigationDescriptor>) override;
virtual void page_did_change_active_document_in_top_level_browsing_context(Web::DOM::Document&) override;
virtual void page_did_change_active_document_in_child_navigable(Web::DOM::Document&) override;
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index 502512d73e9..13ad38485c1 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -136,7 +136,7 @@ endpoint WebContentClient routed by Web::PageId page_id
did_request_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word, Optional<u64> text_fragment_generation_id) =|
did_request_link_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString target, unsigned modifiers, Optional<u64> text_fragment_generation_id) =|
did_generate_text_fragment_url(Web::PageId page_id, u64 request_id, Optional<URL::URL> url) =|
- did_request_image_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString target, unsigned modifiers, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation) =|
+ did_request_image_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString target, unsigned modifiers, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation, Optional<String> image_mime_type) =|
did_request_media_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, ByteString target, unsigned modifiers, Web::MediaContextMenu menu, Optional<Web::HTML::PreparedNavigationDescriptor> navigation) =|
did_request_alert(Web::PageId page_id, Utf16String message) =|
did_request_before_unload(Web::PageId page_id, String source) =|
diff --git a/Tests/LibWeb/Text/expected/UIEvents/context-menu-image-mime-type.txt b/Tests/LibWeb/Text/expected/UIEvents/context-menu-image-mime-type.txt
new file mode 100644
index 00000000000..824aff4d3f5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/UIEvents/context-menu-image-mime-type.txt
@@ -0,0 +1,4 @@
+right-click #png: image/png
+right-click #png-without-image-type: image/png
+right-click #gif-claiming-png: image/gif
+right-click #svg: image/svg+xml
diff --git a/Tests/LibWeb/Text/input/UIEvents/context-menu-image-mime-type.html b/Tests/LibWeb/Text/input/UIEvents/context-menu-image-mime-type.html
new file mode 100644
index 00000000000..0442285fc4e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/UIEvents/context-menu-image-mime-type.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0 }
+ img { display: block; width: 100px; height: 20px }
+</style>
+<script>
+ const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGM4YWMDAAMQAUEiFmcFAAAAAElFTkSuQmCC";
+ const gif = "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
+ const svg = "PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxIiBoZWlnaHQ9IjEiLz4=";
+ const sources = {
+ "png": `data:image/png;base64,${png}`,
+ "png-without-image-type": `data:application/octet-stream;base64,${png}`,
+ "gif-claiming-png": `data:image/png;base64,${gif}`,
+ "svg": `data:image/svg+xml;base64,${svg}`,
+ };
+
+ asyncTest(async done => {
+ const images = Object.entries(sources).map(([name, src]) => {
+ const image = new Image();
+ image.id = name;
+ image.src = src;
+ document.body.appendChild(image);
+ return image;
+ });
+ await Promise.all(images.map(image => image.decode()));
+
+ // The type comes from the image's bytes where they tell, as a server's claim is often missing or wrong.
+ for (const image of images) {
+ const rect = image.getBoundingClientRect();
+ const x = rect.left + rect.width / 2;
+ const y = rect.top + rect.height / 2;
+ internals.mouseDown(x, y, 1, 2);
+ internals.mouseUp(x, y, 2);
+ println(`right-click #${image.id}: ${internals.takeContextMenuRequest().imageMimeType}`);
+ }
+ done();
+ });
+</script>