LibWeb+WebContent: Convert ImageCodecPlugin to use the promise-based API

This commit is contained in:
Andrew Kaster 2024-04-19 15:02:12 -06:00 committed by Andrew Kaster
commit 39dfb659cf
Notes: sideshowbarker 2024-07-17 08:59:18 +09:00
6 changed files with 60 additions and 27 deletions

View file

@ -20,7 +20,7 @@ namespace Ladybird {
ImageCodecPlugin::~ImageCodecPlugin() = default;
Optional<Web::Platform::DecodedImage> ImageCodecPlugin::decode_image(ReadonlyBytes bytes)
NonnullRefPtr<Core::Promise<Web::Platform::DecodedImage>> ImageCodecPlugin::decode_image(ReadonlyBytes bytes, Function<ErrorOr<void>(Web::Platform::DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected)
{
if (!m_client) {
#ifdef AK_OS_ANDROID
@ -34,19 +34,30 @@ Optional<Web::Platform::DecodedImage> ImageCodecPlugin::decode_image(ReadonlyByt
};
}
auto result_or_empty = m_client->decode_image(bytes);
if (!result_or_empty.has_value())
return {};
auto result = result_or_empty.release_value();
auto promise = Core::Promise<Web::Platform::DecodedImage>::construct();
if (on_resolved)
promise->on_resolution = move(on_resolved);
if (on_rejected)
promise->on_rejection = move(on_rejected);
Web::Platform::DecodedImage decoded_image;
decoded_image.is_animated = result.is_animated;
decoded_image.loop_count = result.loop_count;
for (auto const& frame : result.frames) {
decoded_image.frames.empend(move(frame.bitmap), frame.duration);
}
auto image_decoder_promise = m_client->decode_image(
bytes,
[promise](ImageDecoderClient::DecodedImage& result) -> ErrorOr<void> {
// FIXME: Remove this codec plugin and just use the ImageDecoderClient directly to avoid these copies
Web::Platform::DecodedImage decoded_image;
decoded_image.is_animated = result.is_animated;
decoded_image.loop_count = result.loop_count;
for (auto const& frame : result.frames) {
decoded_image.frames.empend(move(frame.bitmap), frame.duration);
}
promise->resolve(move(decoded_image));
return {};
},
[promise](auto& error) {
promise->reject(Error::copy(error));
});
return decoded_image;
return promise;
}
}