Repository: LadybirdBrowser/ladybird
File: Libraries/LibImageDecoderClient/Client.cpp (C++)

Code before:
/*
 * Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/AnonymousBuffer.h>
#include <LibImageDecoderClient/Client.h>

namespace ImageDecoderClient {

Client::Client(NonnullOwnPtr<IPC::Transport> transport)
    : IPC::ConnectionToServer<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(transport))
{
}

void Client::die()
{
    verify_thread_affinity();
    Vector<NonnullRefPtr<Core::Promise<DecodedImage>>> pending_promises;
    pending_promises.ensure_capacity(m_token_promises.size());
    for (auto& [_, promise] : m_token_promises)
        pending_promises.unchecked_append(promise);
    m_token_promises.clear();

    for (auto& promise : pending_promises)
        promise->reject(Error::from_string_literal("ImageDecoder disconnected"));
}

NonnullRefPtr<Core::Promise<DecodedImage>> Client::decode_image(ReadonlyBytes encoded_data, Function<ErrorOr<void>(DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type)
{
    verify_thread_affinity();
    auto promise = Core::Promise<DecodedImage>::construct();
    if (on_resolved)
        promise->on_resolution = move(on_resolved);
    if (on_rejected)
        promise->on_rejection = move(on_rejected);

    if (encoded_data.is_empty()) {
        promise->reject(Error::from_string_literal("No encoded data"));
        return promise;
    }

    auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
    if (encoded_buffer_or_error.is_error()) {
        dbgln("Could not allocate encoded buffer: {}", encoded_buffer_or_error.error());
        promise->reject(encoded_buffer_or_error.release_error());
        return promise;
    }

Review comment (suggestion):
```suggestion
    verify_thread_affinity();
    auto pending_promises = move(m_token_promises);
```

Code after:
/*
 * Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/AnonymousBuffer.h>
#include <LibImageDecoderClient/Client.h>

namespace ImageDecoderClient {

Client::Client(NonnullOwnPtr<IPC::Transport> transport)
    : IPC::ConnectionToServer<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(transport))
{
}

void Client::die()
{
    verify_event_loop();
    auto pending_promises = move(m_token_promises);

    for (auto& promise : pending_promises)
        promise.value->reject(Error::from_string_literal("ImageDecoder disconnected"));
}

NonnullRefPtr<Core::Promise<DecodedImage>> Client::decode_image(ReadonlyBytes encoded_data, Function<ErrorOr<void>(DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type)
{
    verify_event_loop();
    auto promise = Core::Promise<DecodedImage>::construct();
    if (on_resolved)
        promise->on_resolution = move(on_resolved);
    if (on_rejected)
        promise->on_rejection = move(on_rejected);

    if (encoded_data.is_empty()) {
        promise->reject(Error::from_string_literal("No encoded data"));
        return promise;
    }

    auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
    if (encoded_buffer_or_error.is_error()) {
        dbgln("Could not allocate encoded buffer: {}", encoded_buffer_or_error.error());
        promise->reject(encoded_buffer_or_error.release_error());
        return promise;
    }
    auto encoded_buffer = encoded_buffer_or_error.release_value();

    memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());