diff --git a/Tests/LibWeb/Text/input/createBitmap.html b/Tests/LibWeb/Text/input/createBitmap.html index bd7ccd297c0..2026687b00f 100644 --- a/Tests/LibWeb/Text/input/createBitmap.html +++ b/Tests/LibWeb/Text/input/createBitmap.html @@ -1,7 +1,7 @@ diff --git a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp index 14bedaeeb7b..5c8d4dd8162 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp @@ -463,21 +463,25 @@ void HTMLLinkElement::resource_did_load_favicon() document().check_favicon_after_loading_link_resource(); } -static bool decode_favicon(ReadonlyBytes favicon_data, URL::URL const& favicon_url, JS::GCPtr navigable) +static NonnullRefPtr> decode_favicon(ReadonlyBytes favicon_data, URL::URL const& favicon_url, JS::GCPtr navigable) { - auto decoded_image = Platform::ImageCodecPlugin::the().decode_image(favicon_data); - if (!decoded_image.has_value() || decoded_image->frames.is_empty()) { - dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode favicon {}", favicon_url); - return false; - } + auto on_failed_decode = [favicon_url]([[maybe_unused]] Error& error) { + dbgln_if(IMAGE_DECODER_DEBUG, "Failed to decode favicon {}: {}", favicon_url, error); + }; - auto favicon_bitmap = decoded_image->frames[0].bitmap; - dbgln_if(IMAGE_DECODER_DEBUG, "Decoded favicon, {}", favicon_bitmap->size()); + auto on_successful_decode = [navigable = JS::Handle(*navigable)](Web::Platform::DecodedImage& decoded_image) -> ErrorOr { + auto favicon_bitmap = decoded_image.frames[0].bitmap; + dbgln_if(IMAGE_DECODER_DEBUG, "Decoded favicon, {}", favicon_bitmap->size()); - if (navigable && navigable->is_traversable()) - navigable->traversable_navigable()->page().client().page_did_change_favicon(*favicon_bitmap); + if (navigable && navigable->is_traversable()) + navigable->traversable_navigable()->page().client().page_did_change_favicon(*favicon_bitmap); - return favicon_bitmap; + return {}; + }; + + auto promise = Platform::ImageCodecPlugin::the().decode_image(favicon_data, move(on_successful_decode), move(on_failed_decode)); + + return promise; } bool HTMLLinkElement::load_favicon_and_use_if_window_is_active() @@ -485,7 +489,10 @@ bool HTMLLinkElement::load_favicon_and_use_if_window_is_active() if (!has_loaded_icon()) return false; - return decode_favicon(resource()->encoded_data(), resource()->url(), navigable()); + // FIXME: Refactor the caller(s) to handle the async nature of image loading + auto promise = decode_favicon(resource()->encoded_data(), resource()->url(), navigable()); + auto result = promise->await(); + return !result.is_error(); } // https://html.spec.whatwg.org/multipage/links.html#rel-icon:the-link-element-3 @@ -519,7 +526,7 @@ WebIDL::ExceptionOr HTMLLinkElement::load_fallback_favicon_if_needed(JS::N auto global = JS::NonnullGCPtr { realm.global_object() }; auto process_body = JS::create_heap_function(realm.heap(), [document, request](ByteBuffer body) { - decode_favicon(body, request->url(), document->navigable()); + (void)decode_favicon(body, request->url(), document->navigable()); }); auto process_body_error = JS::create_heap_function(realm.heap(), [](JS::GCPtr) { }); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLVideoElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLVideoElement.cpp index c2e4d41246a..ae3d4da3cb7 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLVideoElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLVideoElement.cpp @@ -182,11 +182,14 @@ WebIDL::ExceptionOr HTMLVideoElement::determine_element_poster_frame(Optio m_fetch_controller = nullptr; // 6. If an image is thus obtained, the poster frame is that image. Otherwise, there is no poster frame. - auto image = Platform::ImageCodecPlugin::the().decode_image(image_data); - if (!image.has_value() || image->frames.is_empty()) - return; - - m_poster_frame = move(image.release_value().frames[0].bitmap); + (void)Platform::ImageCodecPlugin::the().decode_image( + image_data, + [strong_this = JS::Handle(*this)](Web::Platform::DecodedImage& image) -> ErrorOr { + if (!image.frames.is_empty()) + strong_this->m_poster_frame = move(image.frames[0].bitmap); + return {}; + }, + [](auto&) {}); }); VERIFY(response->body()); diff --git a/Userland/Libraries/LibWeb/HTML/SharedImageRequest.cpp b/Userland/Libraries/LibWeb/HTML/SharedImageRequest.cpp index 0a47f69336c..f6308fa1d09 100644 --- a/Userland/Libraries/LibWeb/HTML/SharedImageRequest.cpp +++ b/Userland/Libraries/LibWeb/HTML/SharedImageRequest.cpp @@ -141,46 +141,48 @@ void SharedImageRequest::handle_successful_fetch(URL::URL const& url_string, Str bool const is_svg_image = mime_type == "image/svg+xml"sv || url_string.basename().ends_with(".svg"sv); - JS::GCPtr image_data; - - auto handle_failed_decode = [&] { - m_state = State::Failed; - for (auto& callback : m_callbacks) { + auto handle_failed_decode = [strong_this = JS::Handle(*this)](Error&) -> void { + strong_this->m_state = State::Failed; + for (auto& callback : strong_this->m_callbacks) { if (callback.on_fail) callback.on_fail->function()(); } }; + auto handle_successful_decode = [](SharedImageRequest& self) { + self.m_state = State::Finished; + for (auto& callback : self.m_callbacks) { + if (callback.on_finish) + callback.on_finish->function()(); + } + self.m_callbacks.clear(); + }; + if (is_svg_image) { auto result = SVG::SVGDecodedImageData::create(m_document->realm(), m_page, url_string, data); - if (result.is_error()) - return handle_failed_decode(); - - image_data = result.release_value(); - } else { - auto result = Web::Platform::ImageCodecPlugin::the().decode_image(data.bytes()); - if (!result.has_value()) - return handle_failed_decode(); + if (result.is_error()) { + handle_failed_decode(result.error()); + } else { + m_image_data = result.release_value(); + handle_successful_decode(*this); + } + return; + } + auto handle_successful_bitmap_decode = [strong_this = JS::Handle(*this), handle_successful_decode = move(handle_successful_decode)](Web::Platform::DecodedImage& result) -> ErrorOr { Vector frames; - for (auto& frame : result.value().frames) { + for (auto& frame : result.frames) { frames.append(AnimatedBitmapDecodedImageData::Frame { .bitmap = Gfx::ImmutableBitmap::create(*frame.bitmap), .duration = static_cast(frame.duration), }); } - image_data = AnimatedBitmapDecodedImageData::create(m_document->realm(), move(frames), result.value().loop_count, result.value().is_animated).release_value_but_fixme_should_propagate_errors(); - } + strong_this->m_image_data = AnimatedBitmapDecodedImageData::create(strong_this->m_document->realm(), move(frames), result.loop_count, result.is_animated).release_value_but_fixme_should_propagate_errors(); + handle_successful_decode(*strong_this); + return {}; + }; - m_image_data = image_data; - - m_state = State::Finished; - - for (auto& callback : m_callbacks) { - if (callback.on_finish) - callback.on_finish->function()(); - } - m_callbacks.clear(); + (void)Web::Platform::ImageCodecPlugin::the().decode_image(data.bytes(), move(handle_successful_bitmap_decode), move(handle_failed_decode)); } void SharedImageRequest::handle_failed_fetch() diff --git a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp index b527ad9b86d..417366c6832 100644 --- a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp +++ b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp @@ -203,7 +203,7 @@ JS::NonnullGCPtr WindowOrWorkerGlobalScopeMixin::create_image_bitma image.visit( [&](JS::Handle& blob) { // Run these step in parallel: - Platform::EventLoopPlugin::the().deferred_invoke([=, this]() { + Platform::EventLoopPlugin::the().deferred_invoke([=]() { // 1. Let imageData be the result of reading image's data. If an error occurs during reading of the // object, then reject p with an "InvalidStateError" DOMException and abort these steps. // FIXME: I guess this is always fine for us as the data is already read. @@ -213,24 +213,27 @@ JS::NonnullGCPtr WindowOrWorkerGlobalScopeMixin::create_image_bitma // 2. Apply the image sniffing rules to determine the file format of imageData, with MIME type of // image (as given by image's type attribute) giving the official type. - // 3. If imageData is not in a supported image file format (e.g., it's not an image at all), or if - // imageData is corrupted in some fatal way such that the image dimensions cannot be obtained - // (e.g., a vector graphic with no natural size), then reject p with an "InvalidStateError" DOMException - // and abort these steps. - auto result = Web::Platform::ImageCodecPlugin::the().decode_image(image_data); - if (!result.has_value()) { - p->reject(WebIDL::InvalidStateError::create(this_impl().realm(), "image does not contain a supported image format"_string)); - return; - } + auto on_failed_decode = [p = JS::Handle(*p)](Error&) { + // 3. If imageData is not in a supported image file format (e.g., it's not an image at all), or if + // imageData is corrupted in some fatal way such that the image dimensions cannot be obtained + // (e.g., a vector graphic with no natural size), then reject p with an "InvalidStateError" DOMException + // and abort these steps. + p->reject(WebIDL::InvalidStateError::create(relevant_realm(*p), "image does not contain a supported image format"_string)); + }; - // 4. Set imageBitmap's bitmap data to imageData, cropped to the source rectangle with formatting. - // If this is an animated image, imageBitmap's bitmap data must only be taken from the default image - // of the animation (the one that the format defines is to be used when animation is not supported - // or is disabled), or, if there is no such image, the first frame of the animation. - image_bitmap->set_bitmap(result.value().frames.take_first().bitmap); + auto on_successful_decode = [image_bitmap = JS::Handle(*image_bitmap), p = JS::Handle(*p)](Web::Platform::DecodedImage& result) -> ErrorOr { + // 4. Set imageBitmap's bitmap data to imageData, cropped to the source rectangle with formatting. + // If this is an animated image, imageBitmap's bitmap data must only be taken from the default image + // of the animation (the one that the format defines is to be used when animation is not supported + // or is disabled), or, if there is no such image, the first frame of the animation. + image_bitmap->set_bitmap(result.frames.take_first().bitmap); - // 5. Resolve p with imageBitmap. - p->fulfill(image_bitmap); + // 5. Resolve p with imageBitmap. + p->fulfill(image_bitmap); + return {}; + }; + + (void)Web::Platform::ImageCodecPlugin::the().decode_image(image_data, move(on_successful_decode), move(on_failed_decode)); }); }, [&](auto&) { diff --git a/Userland/Libraries/LibWeb/Platform/ImageCodecPlugin.cpp b/Userland/Libraries/LibWeb/Platform/ImageCodecPlugin.cpp index 58ae72dcc20..54057349610 100644 --- a/Userland/Libraries/LibWeb/Platform/ImageCodecPlugin.cpp +++ b/Userland/Libraries/LibWeb/Platform/ImageCodecPlugin.cpp @@ -25,13 +25,4 @@ void ImageCodecPlugin::install(ImageCodecPlugin& plugin) s_the = &plugin; } -Optional ImageCodecPlugin::decode_image(ReadonlyBytes encoded_data) -{ - auto promise = decode_image(encoded_data, {}, {}); - auto result = promise->await(); - if (result.is_error()) - return {}; - return result.release_value(); -} - } diff --git a/Userland/Libraries/LibWeb/Platform/ImageCodecPlugin.h b/Userland/Libraries/LibWeb/Platform/ImageCodecPlugin.h index 85c94841a6d..477915d486c 100644 --- a/Userland/Libraries/LibWeb/Platform/ImageCodecPlugin.h +++ b/Userland/Libraries/LibWeb/Platform/ImageCodecPlugin.h @@ -33,7 +33,6 @@ public: virtual ~ImageCodecPlugin(); virtual NonnullRefPtr> decode_image(ReadonlyBytes, Function(DecodedImage&)> on_resolved, Function on_rejected) = 0; - Optional decode_image(ReadonlyBytes); }; }