Truly independent web browser
/*
* Copyright (c) 2020, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/HashTable.h>
#include <ImageDecoder/ImageDecoderClientEndpoint.h>
#include <ImageDecoder/ImageDecoderServerEndpoint.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Promise.h>
#include <LibGfx/ColorSpace.h>
#include <LibIPC/ConnectionToServer.h>
namespace ImageDecoderClient {
struct Frame {
NonnullRefPtr<Gfx::Bitmap> bitmap;
u32 duration { 0 };
};
struct DecodedImage {
bool is_animated { false };
Gfx::FloatPoint scale { 1, 1 };
u32 loop_count { 0 };
u32 frame_count { 0 };
Vector<Frame> frames;
Vector<u32> all_durations;
Gfx::ColorSpace color_space;
i64 session_id { 0 };
};
struct DecodedFirstFrame {
NonnullRefPtr<Gfx::Bitmap> bitmap;
Gfx::ColorSpace color_space;
};
class Client final
: public IPC::ConnectionToServer<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>
, public ImageDecoderClientEndpoint {
C_OBJECT_ABSTRACT(Client);
public:
Client(NonnullOwnPtr<IPC::Transport>);
NonnullRefPtr<Core::Promise<DecodedImage>> decode_image(ReadonlyBytes, Function<ErrorOr<void>(DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected, Optional<Gfx::IntSize> ideal_size = {}, Optional<ByteString> mime_type = {});
ErrorOr<DecodedFirstFrame> decode_first_frame_synchronously(ReadonlyBytes, Optional<ByteString> mime_type = {});
void request_animation_frames(i64 session_id, u32 start_frame_index, u32 count);
void stop_animation_decode(i64 session_id);
// Whether nothing depends on this connection: no decode waits on it, and no animation it opened is still playing.
bool is_idle() const { return m_token_promises.is_empty() && m_animation_sessions.is_empty(); }
Function<void()> on_death;
Function<void(i64 session_id, Vector<NonnullRefPtr<Gfx::Bitmap>>)> on_animation_frames_decoded;
Function<void(i64 session_id, String error_message)> on_animation_decode_failed;
private:
void verify_event_loop() const;
virtual void die() override;
virtual void did_decode_image(i64 request_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmap_sequence, Vector<u32> durations, Gfx::FloatPoint scale, Gfx::ColorSpace color_space, i64 session_id) override;
virtual void did_fail_to_decode_image(i64 request_id, String error_message) override;
virtual void did_decode_animation_frames(i64 session_id, Gfx::BitmapSequence bitmaps) override;
virtual void did_fail_animation_decode(i64 session_id, String error_message) override;
Core::EventLoop* m_creation_event_loop { &Core::EventLoop::current() };
i64 m_next_request_id { 0 };
HashMap<i64, NonnullRefPtr<Core::Promise<DecodedImage>>> m_token_promises;
HashTable<i64> m_animation_sessions;
};
}