LibWeb: Convert callers of ImageCodecPlugin to the async API

The HTMLLinkElement caller is a bit hairy, so we shove an await() in
there temporarily. This is sure to cause fun times for anyone debugging
task/microtask execution order.
This commit is contained in:
Andrew Kaster 2024-04-19 15:55:54 -06:00 committed by Andrew Kaster
commit 651e78fedb
Notes: sideshowbarker 2024-07-17 05:58:46 +09:00
7 changed files with 78 additions and 71 deletions

View file

@ -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<DecodedImageData> 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<void> {
Vector<AnimatedBitmapDecodedImageData::Frame> 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<int>(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()