From 5522e8f59d2a175fb6096f5143d659b742370dc2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 17 Jan 2021 00:14:37 +0100 Subject: [PATCH] Clipboard+LibGUI: Move clipboard service to anonymous files --- Userland/Libraries/LibGUI/Clipboard.cpp | 26 +++++------------- .../Services/Clipboard/ClientConnection.cpp | 27 ++----------------- .../Services/Clipboard/ClientConnection.h | 2 -- .../Services/Clipboard/ClipboardServer.ipc | 4 +-- Userland/Services/Clipboard/Storage.cpp | 10 +++---- Userland/Services/Clipboard/Storage.h | 12 +++++---- 6 files changed, 21 insertions(+), 60 deletions(-) diff --git a/Userland/Libraries/LibGUI/Clipboard.cpp b/Userland/Libraries/LibGUI/Clipboard.cpp index ff2873d5433..c7e7ce4b379 100644 --- a/Userland/Libraries/LibGUI/Clipboard.cpp +++ b/Userland/Libraries/LibGUI/Clipboard.cpp @@ -25,7 +25,6 @@ */ #include -#include #include #include #include @@ -79,18 +78,9 @@ Clipboard::Clipboard() Clipboard::DataAndType Clipboard::data_and_type() const { auto response = connection().send_sync(); - if (response->shbuf_id() < 0) + if (!response->data().is_valid()) return {}; - auto shared_buffer = SharedBuffer::create_from_shbuf_id(response->shbuf_id()); - if (!shared_buffer) { - dbgln("GUI::Clipboard::data() failed to attach to the shared buffer"); - return {}; - } - if (response->data_size() > shared_buffer->size()) { - dbgln("GUI::Clipboard::data() clipping contents size is greater than shared buffer size"); - return {}; - } - auto data = ByteBuffer::copy(shared_buffer->data(), response->data_size()); + auto data = ByteBuffer::copy(response->data().data(), response->data().size()); auto type = response->mime_type(); auto metadata = response->metadata().entries(); return { data, type, metadata }; @@ -98,17 +88,15 @@ Clipboard::DataAndType Clipboard::data_and_type() const void Clipboard::set_data(ReadonlyBytes data, const String& type, const HashMap& metadata) { - auto shared_buffer = SharedBuffer::create_with_size(data.size()); - if (!shared_buffer) { - dbgln("GUI::Clipboard::set_data() failed to create a shared buffer"); + auto buffer = Core::AnonymousBuffer::create_with_size(data.size()); + if (!buffer.is_valid()) { + dbgln("GUI::Clipboard::set_data() failed to create a buffer"); return; } if (!data.is_empty()) - memcpy(shared_buffer->data(), data.data(), data.size()); - shared_buffer->seal(); - shared_buffer->share_with(connection().server_pid()); + memcpy(buffer.data(), data.data(), data.size()); - connection().send_sync(shared_buffer->shbuf_id(), data.size(), type, metadata); + connection().send_sync(move(buffer), type, metadata); } void ClipboardServerConnection::handle(const Messages::ClipboardClient::ClipboardDataChanged& message) diff --git a/Userland/Services/Clipboard/ClientConnection.cpp b/Userland/Services/Clipboard/ClientConnection.cpp index 9518a4f0b65..86ecba3616c 100644 --- a/Userland/Services/Clipboard/ClientConnection.cpp +++ b/Userland/Services/Clipboard/ClientConnection.cpp @@ -25,7 +25,6 @@ */ #include -#include #include #include #include @@ -63,36 +62,14 @@ OwnPtr ClientConnection::handle(const OwnPtr ClientConnection::handle(const Messages::ClipboardServer::SetClipboardData& message) { - auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id()); - if (!shared_buffer) { - did_misbehave("SetClipboardData: Bad shared buffer ID"); - return {}; - } - Storage::the().set_data(*shared_buffer, message.data_size(), message.mime_type(), message.metadata().entries()); + Storage::the().set_data(message.data(), message.mime_type(), message.metadata().entries()); return make(); } OwnPtr ClientConnection::handle(const Messages::ClipboardServer::GetClipboardData&) { auto& storage = Storage::the(); - - i32 shbuf_id = -1; - if (storage.data_size()) { - // FIXME: Optimize case where an app is copy/pasting within itself. - // We can just reuse the SharedBuffer then, since it will have the same peer PID. - // It would be even nicer if a SharedBuffer could have an arbitrary number of clients.. - RefPtr shared_buffer = SharedBuffer::create_with_size(storage.data_size()); - ASSERT(shared_buffer); - memcpy(shared_buffer->data(), storage.data(), storage.data_size()); - shared_buffer->seal(); - shared_buffer->share_with(client_pid()); - shbuf_id = shared_buffer->shbuf_id(); - - // FIXME: This is a workaround for the fact that SharedBuffers will go away if neither side is retaining them. - // After we respond to GetClipboardData, we have to wait for the client to ref the buffer on his side. - m_last_sent_buffer = move(shared_buffer); - } - return make(shbuf_id, storage.data_size(), storage.mime_type(), storage.metadata()); + return make(storage.buffer(), storage.mime_type(), storage.metadata()); } void ClientConnection::notify_about_clipboard_change() diff --git a/Userland/Services/Clipboard/ClientConnection.h b/Userland/Services/Clipboard/ClientConnection.h index 48794f4f889..fa57cc33726 100644 --- a/Userland/Services/Clipboard/ClientConnection.h +++ b/Userland/Services/Clipboard/ClientConnection.h @@ -53,8 +53,6 @@ private: virtual OwnPtr handle(const Messages::ClipboardServer::Greet&) override; virtual OwnPtr handle(const Messages::ClipboardServer::GetClipboardData&) override; virtual OwnPtr handle(const Messages::ClipboardServer::SetClipboardData&) override; - - RefPtr m_last_sent_buffer; }; } diff --git a/Userland/Services/Clipboard/ClipboardServer.ipc b/Userland/Services/Clipboard/ClipboardServer.ipc index c0c1fab8c83..2b38fc16fe8 100644 --- a/Userland/Services/Clipboard/ClipboardServer.ipc +++ b/Userland/Services/Clipboard/ClipboardServer.ipc @@ -2,6 +2,6 @@ endpoint ClipboardServer = 802 { Greet() => (i32 client_id) - GetClipboardData() => (i32 shbuf_id, i32 data_size, [UTF8] String mime_type, IPC::Dictionary metadata) - SetClipboardData(i32 shbuf_id, i32 data_size, [UTF8] String mime_type, IPC::Dictionary metadata) => () + GetClipboardData() => (Core::AnonymousBuffer data, [UTF8] String mime_type, IPC::Dictionary metadata) + SetClipboardData(Core::AnonymousBuffer data, [UTF8] String mime_type, IPC::Dictionary metadata) => () } diff --git a/Userland/Services/Clipboard/Storage.cpp b/Userland/Services/Clipboard/Storage.cpp index 3b745dea166..b26727c1f1d 100644 --- a/Userland/Services/Clipboard/Storage.cpp +++ b/Userland/Services/Clipboard/Storage.cpp @@ -44,14 +44,10 @@ Storage::~Storage() { } -void Storage::set_data(NonnullRefPtr data, size_t data_size, const String& mime_type, const HashMap& metadata) +void Storage::set_data(Core::AnonymousBuffer data, const String& mime_type, const HashMap& metadata) { - dbg() << "Storage::set_data <- [" << mime_type << "] " << data->data() << " (" << data_size << " bytes)"; - for (auto& it : metadata) { - dbg() << " " << it.key << ": " << it.value; - } - m_shared_buffer = move(data); - m_data_size = data_size; + m_buffer = move(data); + m_data_size = data.size(); m_mime_type = mime_type; m_metadata = metadata; diff --git a/Userland/Services/Clipboard/Storage.h b/Userland/Services/Clipboard/Storage.h index ce194c15f49..82f7408fdab 100644 --- a/Userland/Services/Clipboard/Storage.h +++ b/Userland/Services/Clipboard/Storage.h @@ -28,8 +28,8 @@ #include #include -#include #include +#include namespace Clipboard { @@ -38,7 +38,7 @@ public: static Storage& the(); ~Storage(); - bool has_data() const { return m_shared_buffer; } + bool has_data() const { return m_buffer.is_valid(); } const String& mime_type() const { return m_mime_type; } const HashMap& metadata() const { return m_metadata; } @@ -47,7 +47,7 @@ public: { if (!has_data()) return nullptr; - return m_shared_buffer->data(); + return m_buffer.data(); } size_t data_size() const @@ -57,15 +57,17 @@ public: return 0; } - void set_data(NonnullRefPtr, size_t data_size, const String& mime_type, const HashMap& metadata); + void set_data(Core::AnonymousBuffer, const String& mime_type, const HashMap& metadata); Function on_content_change; + const Core::AnonymousBuffer& buffer() const { return m_buffer; } + private: Storage(); String m_mime_type; - RefPtr m_shared_buffer; + Core::AnonymousBuffer m_buffer; size_t m_data_size { 0 }; HashMap m_metadata; };