mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03: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: https://github.com/SerenityOS/serenity/commit/39dfb659cf Pull-request: https://github.com/SerenityOS/serenity/pull/24028 Reviewed-by: https://github.com/LucasChollet Reviewed-by: https://github.com/awesomekling
@ -20,7 +20,7 @@ namespace Ladybird {
|
|||||||
|
|
||||||
ImageCodecPlugin::~ImageCodecPlugin() = default;
|
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) {
|
if (!m_client) {
|
||||||
#ifdef AK_OS_ANDROID
|
#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);
|
auto promise = Core::Promise<Web::Platform::DecodedImage>::construct();
|
||||||
if (!result_or_empty.has_value())
|
if (on_resolved)
|
||||||
return {};
|
promise->on_resolution = move(on_resolved);
|
||||||
auto result = result_or_empty.release_value();
|
if (on_rejected)
|
||||||
|
promise->on_rejection = move(on_rejected);
|
||||||
|
|
||||||
Web::Platform::DecodedImage decoded_image;
|
auto image_decoder_promise = m_client->decode_image(
|
||||||
decoded_image.is_animated = result.is_animated;
|
bytes,
|
||||||
decoded_image.loop_count = result.loop_count;
|
[promise](ImageDecoderClient::DecodedImage& result) -> ErrorOr<void> {
|
||||||
for (auto const& frame : result.frames) {
|
// FIXME: Remove this codec plugin and just use the ImageDecoderClient directly to avoid these copies
|
||||||
decoded_image.frames.empend(move(frame.bitmap), frame.duration);
|
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;
|
ImageCodecPlugin() = default;
|
||||||
virtual ~ImageCodecPlugin() override;
|
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:
|
private:
|
||||||
RefPtr<ImageDecoderClient::Client> m_client;
|
RefPtr<ImageDecoderClient::Client> m_client;
|
||||||
|
@ -25,4 +25,13 @@ void ImageCodecPlugin::install(ImageCodecPlugin& plugin)
|
|||||||
s_the = &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/RefPtr.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <LibCore/Promise.h>
|
||||||
#include <LibGfx/Forward.h>
|
#include <LibGfx/Forward.h>
|
||||||
|
|
||||||
namespace Web::Platform {
|
namespace Web::Platform {
|
||||||
@ -31,7 +32,8 @@ public:
|
|||||||
|
|
||||||
virtual ~ImageCodecPlugin();
|
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;
|
||||||
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) {
|
if (!m_client) {
|
||||||
m_client = ImageDecoderClient::Client::try_create().release_value_but_fixme_should_propagate_errors();
|
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);
|
auto promise = Core::Promise<Web::Platform::DecodedImage>::construct();
|
||||||
if (!result_or_empty.has_value())
|
if (on_resolved)
|
||||||
return {};
|
promise->on_resolution = move(on_resolved);
|
||||||
auto result = result_or_empty.release_value();
|
if (on_rejected)
|
||||||
|
promise->on_rejection = move(on_rejected);
|
||||||
|
|
||||||
Web::Platform::DecodedImage decoded_image;
|
auto image_decoder_promise = m_client->decode_image(
|
||||||
decoded_image.is_animated = result.is_animated;
|
bytes,
|
||||||
decoded_image.loop_count = result.loop_count;
|
[promise](ImageDecoderClient::DecodedImage& result) -> ErrorOr<void> {
|
||||||
for (auto const& frame : result.frames) {
|
// FIXME: Remove this codec plugin and just use the ImageDecoderClient directly to avoid these copies
|
||||||
decoded_image.frames.empend(frame.bitmap, frame.duration);
|
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();
|
ImageCodecPluginSerenity();
|
||||||
virtual ~ImageCodecPluginSerenity() override;
|
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:
|
private:
|
||||||
RefPtr<ImageDecoderClient::Client> m_client;
|
RefPtr<ImageDecoderClient::Client> m_client;
|
||||||
|
Loading…
Reference in New Issue
Block a user