Просмотр исходного кода

LibIPC: Add an expressive way to close an IPC::File after sending it

If you don't need a file descriptor after sending it to someone over
IPC, construct it with IPC::File(fd, IPC::File::CloseAfterSending)
and LibIPC will take care of it for you. :^)
Andreas Kling 4 лет назад
Родитель
Сommit
f1f7bf567b

+ 2 - 0
Userland/Libraries/LibIPC/File.h

@@ -48,8 +48,10 @@ public:
     }
 
     // Tagged constructor for fd's that should be closed on destruction unless take_fd() is called.
+    // Note that the tags are the same, this is intentional to allow expressive invocation.
     enum Tag {
         ConstructWithReceivedFileDescriptor = 1,
+        CloseAfterSending = 1,
     };
     File(int fd, Tag)
         : m_fd(fd)

+ 0 - 2
Userland/Libraries/LibIPC/Message.cpp

@@ -34,8 +34,6 @@ Message::Message()
 
 Message::~Message()
 {
-    if (on_destruction)
-        on_destruction();
 }
 
 }

+ 0 - 2
Userland/Libraries/LibIPC/Message.h

@@ -45,8 +45,6 @@ public:
     virtual const char* message_name() const = 0;
     virtual MessageBuffer encode() const = 0;
 
-    Function<void()> on_destruction;
-
 protected:
     Message();
 };

+ 1 - 3
Userland/Services/ProtocolServer/ClientConnection.cpp

@@ -78,9 +78,7 @@ OwnPtr<Messages::ProtocolServer::StartDownloadResponse> ClientConnection::handle
     auto id = download->id();
     auto fd = download->download_fd();
     m_downloads.set(id, move(download));
-    auto response = make<Messages::ProtocolServer::StartDownloadResponse>(id, fd);
-    response->on_destruction = [fd] { close(fd); };
-    return response;
+    return make<Messages::ProtocolServer::StartDownloadResponse>(id, IPC::File(fd, IPC::File::CloseAfterSending));
 }
 
 OwnPtr<Messages::ProtocolServer::StopDownloadResponse> ClientConnection::handle(const Messages::ProtocolServer::StopDownload& message)