summaryrefslogtreecommitdiff
path: root/Userland/Services/Clipboard/ClientConnection.cpp
blob: 4f8130fac2c18baea587e323622bb0ff2e22a8d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Badge.h>
#include <Clipboard/ClientConnection.h>
#include <Clipboard/ClipboardClientEndpoint.h>
#include <Clipboard/Storage.h>

namespace Clipboard {

static HashMap<int, RefPtr<ClientConnection>> s_connections;

void ClientConnection::for_each_client(Function<void(ClientConnection&)> callback)
{
    for (auto& it : s_connections) {
        callback(*it.value);
    }
}

ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
    : IPC::ClientConnection<ClipboardClientEndpoint, ClipboardServerEndpoint>(*this, move(socket), client_id)
{
    s_connections.set(client_id, *this);
}

ClientConnection::~ClientConnection()
{
}

void ClientConnection::die()
{
    s_connections.remove(client_id());
}

OwnPtr<Messages::ClipboardServer::GreetResponse> ClientConnection::handle(const Messages::ClipboardServer::Greet&)
{
    return make<Messages::ClipboardServer::GreetResponse>();
}

OwnPtr<Messages::ClipboardServer::SetClipboardDataResponse> ClientConnection::handle(const Messages::ClipboardServer::SetClipboardData& message)
{
    Storage::the().set_data(message.data(), message.mime_type(), message.metadata().entries());
    return make<Messages::ClipboardServer::SetClipboardDataResponse>();
}

OwnPtr<Messages::ClipboardServer::GetClipboardDataResponse> ClientConnection::handle(const Messages::ClipboardServer::GetClipboardData&)
{
    auto& storage = Storage::the();
    return make<Messages::ClipboardServer::GetClipboardDataResponse>(storage.buffer(), storage.mime_type(), storage.metadata());
}

void ClientConnection::notify_about_clipboard_change()
{
    post_message(Messages::ClipboardClient::ClipboardDataChanged(Storage::the().mime_type()));
}

}