mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 09:21:57 -05:00
LibWeb+WebContent: Convert ImageCodecPlugin to use the promise-based API
This commit is contained in:
parent
b871bc082c
commit
39dfb659cf
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ public:
|
|||
ImageCodecPlugin() = default;
|
||||
virtual ~ImageCodecPlugin() override;
|
||||
|
||||
virtual Optional<Web::Platform::DecodedImage> decode_image(ReadonlyBytes data) override;
|
||||
virtual NonnullRefPtr<Core::Promise<Web::Platform::DecodedImage>> decode_image(ReadonlyBytes, Function<ErrorOr<void>(Web::Platform::DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected) override;
|
||||
|
||||
private:
|
||||
RefPtr<ImageDecoderClient::Client> m_client;
|
||||
|
|
|
@ -25,4 +25,13 @@ void ImageCodecPlugin::install(ImageCodecPlugin& plugin)
|
|||
s_the = &plugin;
|
||||
}
|
||||
|
||||
Optional<DecodedImage> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/Promise.h>
|
||||
#include <LibGfx/Forward.h>
|
||||
|
||||
namespace Web::Platform {
|
||||
|
@ -31,7 +32,8 @@ public:
|
|||
|
||||
virtual ~ImageCodecPlugin();
|
||||
|
||||
virtual Optional<DecodedImage> decode_image(ReadonlyBytes) = 0;
|
||||
virtual NonnullRefPtr<Core::Promise<DecodedImage>> decode_image(ReadonlyBytes, Function<ErrorOr<void>(DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected) = 0;
|
||||
Optional<DecodedImage> decode_image(ReadonlyBytes);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace WebContent {
|
|||
ImageCodecPluginSerenity::ImageCodecPluginSerenity() = default;
|
||||
ImageCodecPluginSerenity::~ImageCodecPluginSerenity() = default;
|
||||
|
||||
Optional<Web::Platform::DecodedImage> ImageCodecPluginSerenity::decode_image(ReadonlyBytes bytes)
|
||||
NonnullRefPtr<Core::Promise<Web::Platform::DecodedImage>> ImageCodecPluginSerenity::decode_image(ReadonlyBytes bytes, Function<ErrorOr<void>(Web::Platform::DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected)
|
||||
{
|
||||
if (!m_client) {
|
||||
m_client = ImageDecoderClient::Client::try_create().release_value_but_fixme_should_propagate_errors();
|
||||
|
@ -22,19 +22,30 @@ Optional<Web::Platform::DecodedImage> ImageCodecPluginSerenity::decode_image(Rea
|
|||
};
|
||||
}
|
||||
|
||||
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(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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ public:
|
|||
ImageCodecPluginSerenity();
|
||||
virtual ~ImageCodecPluginSerenity() override;
|
||||
|
||||
virtual Optional<Web::Platform::DecodedImage> decode_image(ReadonlyBytes) override;
|
||||
virtual NonnullRefPtr<Core::Promise<Web::Platform::DecodedImage>> decode_image(ReadonlyBytes, Function<ErrorOr<void>(Web::Platform::DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected) override;
|
||||
|
||||
private:
|
||||
RefPtr<ImageDecoderClient::Client> m_client;
|
||||
|
|
Loading…
Reference in a new issue