mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-09 17:49:40 +00:00
LibWeb+WebContent: Convert ImageCodecPlugin to use the promise-based API
This commit is contained in:
parent
b871bc082c
commit
39dfb659cf
Notes:
sideshowbarker
2024-07-17 08:59:18 +09:00
Author: https://github.com/ADKaster
Commit: 39dfb659cf
Pull-request: https://github.com/SerenityOS/serenity/pull/24028
Reviewed-by: https://github.com/LucasChollet
Reviewed-by: https://github.com/awesomekling
6 changed files with 60 additions and 27 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue