Make the socket manager just manage the server, leave the socket controller in charge...
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Thu, 22 Dec 2022 13:37:30 +0000 (14:37 +0100)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Fri, 12 May 2023 05:21:07 +0000 (13:21 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/CMakeLists.txt
src/gui/macOS/fileprovidersocketcontroller.cpp
src/gui/macOS/fileprovidersocketcontroller.h
src/gui/macOS/fileprovidersocketmanager.cpp [deleted file]
src/gui/macOS/fileprovidersocketmanager.h [deleted file]
src/gui/macOS/fileprovidersocketmanager_mac.mm [deleted file]
src/gui/macOS/fileprovidersocketserver.cpp [new file with mode: 0644]
src/gui/macOS/fileprovidersocketserver.h [new file with mode: 0644]
src/gui/macOS/fileprovidersocketserver_mac.mm [new file with mode: 0644]

index a7816d401ebd40278db905abe6949e48b21b54e4..6450760502116b7ca2fbba1913286f01555c8eda 100644 (file)
@@ -287,9 +287,9 @@ IF( APPLE )
         macOS/fileproviderdomainmanager_mac.mm
         macOS/fileprovidersocketcontroller.h
         macOS/fileprovidersocketcontroller.cpp
-        macOS/fileprovidersocketmanager.h
-        macOS/fileprovidersocketmanager.cpp
-        macOS/fileprovidersocketmanager_mac.mm)
+        macOS/fileprovidersocketserver.h
+        macOS/fileprovidersocketserver.cpp
+        macOS/fileprovidersocketserver_mac.mm)
 
     if(SPARKLE_FOUND AND BUILD_UPDATER)
         # Define this, we need to check in updater.cpp
index ceaa659361acab2b3efb55f18bedf8690aaa89ed..fd1ff53605d55cce89e7e326886ce82b7c3208a1 100644 (file)
 
 #include "fileprovidersocketcontroller.h"
 
-namespace OCC
-{
+#include <QLoggingCategory>
 
-namespace Mac
-{
+namespace OCC {
+
+namespace Mac {
+
+Q_LOGGING_CATEGORY(lcFileProviderSocketController, "nextcloud.gui.macos.fileprovider.socketcontroller", QtInfoMsg)
 
 FileProviderSocketController::FileProviderSocketController(QLocalSocket *socket, QObject *parent)
     : QObject{parent}
     , _socket(socket)
 {
+    connect(socket, &QLocalSocket::disconnected,
+            this, &FileProviderSocketController::slotOnDisconnected);
+    connect(socket, &QLocalSocket::destroyed,
+            this, &FileProviderSocketController::slotSocketDestroyed);
+}
 
+void FileProviderSocketController::slotOnDisconnected()
+{
+    qCInfo(lcFileProviderSocketController) << "File provider socket disconnected";
+    _socket->deleteLater();
+    Q_EMIT socketDestroyed(_socket);
+}
+
+void FileProviderSocketController::slotSocketDestroyed(QObject *object)
+{
+    Q_UNUSED(object)
+    qCInfo(lcFileProviderSocketController) << "File provider socket object has been destroyed, destroying controller";
+    Q_EMIT socketDestroyed(_socket);
 }
 
 }
index 169a622c2c69a35b43fb2fd500c198b131a10ce9..f660a38e61a3ae4bcd2e330df4a167a14d96dd49 100644 (file)
 #include <QPointer>
 #include <QLocalSocket>
 
-namespace OCC
-{
+namespace OCC {
 
-namespace Mac
-{
+namespace Mac {
 
 class FileProviderSocketController : public QObject
 {
@@ -31,6 +29,13 @@ class FileProviderSocketController : public QObject
 public:
     explicit FileProviderSocketController(QLocalSocket *socket, QObject *parent = nullptr);
 
+signals:
+    void socketDestroyed(const QLocalSocket * const socket);
+
+private slots:
+    void slotOnDisconnected();
+    void slotSocketDestroyed(QObject *object);
+
 private:
     QPointer<QLocalSocket> _socket;
 };
diff --git a/src/gui/macOS/fileprovidersocketmanager.cpp b/src/gui/macOS/fileprovidersocketmanager.cpp
deleted file mode 100644 (file)
index 03257a4..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * for more details.
- */
-
-#include "fileprovidersocketmanager.h"
-
-#include <QLocalSocket>
-
-#include "fileprovidersocketcontroller.h"
-
-namespace OCC
-{
-
-namespace Mac
-{
-
-Q_LOGGING_CATEGORY(lcFileProviderSocketManager, "nextcloud.gui.macos.fileprovider.socketmanager", QtInfoMsg)
-
-FileProviderSocketManager::FileProviderSocketManager(QObject *parent)
-    : QObject{parent}
-{
-#ifdef Q_OS_MACOS
-    _socketPath = fileProviderSocketPath();
-#endif
-    startListening();
-}
-
-void FileProviderSocketManager::startListening()
-{
-    QLocalServer::removeServer(_socketPath);
-
-    const auto serverStarted = _socketServer.listen(_socketPath);
-    if (!serverStarted) {
-        qCWarning(lcFileProviderSocketManager) << "Could not start file provider socket server"
-                                               << _socketPath;
-    } else {
-        qCInfo(lcFileProviderSocketManager) << "File provider socket server started, listening"
-                                            << _socketPath;
-    }
-
-    connect(&_socketServer, &QLocalServer::newConnection,
-            this, &FileProviderSocketManager::slotNewConnection);
-}
-
-void FileProviderSocketManager::slotNewConnection()
-{
-    if (!_socketServer.hasPendingConnections()) {
-        return;
-    }
-
-    const auto socket = _socketServer.nextPendingConnection();
-    if (!socket) {
-        return;
-    }
-
-    connect(socket, &QLocalSocket::disconnected,
-            this, &FileProviderSocketManager::slotOnDisconnected);
-    connect(socket, &QLocalSocket::destroyed,
-            this, &FileProviderSocketManager::slotSocketDestroyed);
-
-    const FileProviderSocketControllerPtr controller(new FileProviderSocketController(socket));
-    _socketControllers.insert(socket, controller);
-}
-
-void FileProviderSocketManager::slotOnDisconnected()
-{
-    const auto socket = qobject_cast<QLocalSocket *>(sender());
-    Q_ASSERT(socket);
-    socket->deleteLater();
-}
-
-void FileProviderSocketManager::slotSocketDestroyed(QObject *object)
-{
-    const auto socket = qobject_cast<QLocalSocket *>(object);
-    _socketControllers.remove(socket);
-}
-
-} // namespace Mac
-
-} // namespace OCC
diff --git a/src/gui/macOS/fileprovidersocketmanager.h b/src/gui/macOS/fileprovidersocketmanager.h
deleted file mode 100644 (file)
index 12f3f2d..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * for more details.
- */
-
-#pragma once
-
-#include <QObject>
-#include <QLocalServer>
-#include <QLoggingCategory>
-
-namespace OCC
-{
-
-namespace Mac {
-
-class FileProviderSocketController;
-using FileProviderSocketControllerPtr = QSharedPointer<FileProviderSocketController>;
-
-#ifdef Q_OS_MACOS
-QString fileProviderSocketPath();
-#endif
-
-class FileProviderSocketManager : public QObject
-{
-    Q_OBJECT
-
-public:
-    explicit FileProviderSocketManager(QObject *parent = nullptr);
-
-private slots:
-    void startListening();
-    void slotNewConnection();
-    void slotOnDisconnected();
-    void slotSocketDestroyed(QObject *object);
-
-private:
-    QString _socketPath;
-    QLocalServer _socketServer;
-    QHash<QLocalSocket*, FileProviderSocketControllerPtr> _socketControllers;
-};
-
-} // namespace Mac
-
-} // namespace OCC
diff --git a/src/gui/macOS/fileprovidersocketmanager_mac.mm b/src/gui/macOS/fileprovidersocketmanager_mac.mm
deleted file mode 100644 (file)
index 8377adb..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * for more details.
- */
-
-#import <Cocoa/Cocoa.h>
-#import <QString>
-
-#include "config.h"
-
-namespace OCC
-{
-
-namespace Mac
-{
-
-QString fileProviderSocketPath()
-{
-    // This must match the code signing Team setting of the extension
-    // Example for developer builds (with ad-hoc signing identity): "" "com.owncloud.desktopclient" ".fileprovidersocket"
-    // Example for official signed packages: "9B5WD74GWJ." "com.owncloud.desktopclient" ".fileprovidersocket"
-    NSString *appGroupId = @SOCKETAPI_TEAM_IDENTIFIER_PREFIX APPLICATION_REV_DOMAIN;
-
-    NSURL *container = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:appGroupId];
-    NSURL *socketPath = [container URLByAppendingPathComponent:@".fileprovidersocket" isDirectory:false];
-    return QString::fromNSString(socketPath.path);
-}
-
-} // namespace Mac
-
-} // namespace OCC
diff --git a/src/gui/macOS/fileprovidersocketserver.cpp b/src/gui/macOS/fileprovidersocketserver.cpp
new file mode 100644 (file)
index 0000000..7ebf55e
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include "fileprovidersocketserver.h"
+
+#include <QLocalSocket>
+
+#include "fileprovidersocketcontroller.h"
+
+namespace OCC {
+
+namespace Mac {
+
+Q_LOGGING_CATEGORY(lcFileProviderSocketServer, "nextcloud.gui.macos.fileprovider.socketserver", QtInfoMsg)
+
+FileProviderSocketServer::FileProviderSocketServer(QObject *parent)
+    : QObject{parent}
+{
+#ifdef Q_OS_MACOS
+    _socketPath = fileProviderSocketPath();
+#endif
+    startListening();
+}
+
+void FileProviderSocketServer::startListening()
+{
+    QLocalServer::removeServer(_socketPath);
+
+    const auto serverStarted = _socketServer.listen(_socketPath);
+    if (!serverStarted) {
+        qCWarning(lcFileProviderSocketServer) << "Could not start file provider socket server"
+                                              << _socketPath;
+    } else {
+        qCInfo(lcFileProviderSocketServer) << "File provider socket server started, listening"
+                                           << _socketPath;
+    }
+
+    connect(&_socketServer, &QLocalServer::newConnection,
+            this, &FileProviderSocketServer::slotNewConnection);
+}
+
+void FileProviderSocketServer::slotNewConnection()
+{
+    if (!_socketServer.hasPendingConnections()) {
+        return;
+    }
+
+    qCInfo(lcFileProviderSocketServer) << "New connection in file provider socket server";
+    const auto socket = _socketServer.nextPendingConnection();
+    if (!socket) {
+        return;
+    }
+
+    const FileProviderSocketControllerPtr socketController(new FileProviderSocketController(socket, this));
+    connect(socketController.data(), &FileProviderSocketController::socketDestroyed,
+            this, &FileProviderSocketServer::slotSocketDestroyed);
+    _socketControllers.insert(socket, socketController);
+}
+
+void FileProviderSocketServer::slotSocketDestroyed(const QLocalSocket * const socket)
+{
+    _socketControllers.remove(socket);
+}
+
+} // namespace Mac
+
+} // namespace OCC
diff --git a/src/gui/macOS/fileprovidersocketserver.h b/src/gui/macOS/fileprovidersocketserver.h
new file mode 100644 (file)
index 0000000..8ad2982
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QLocalServer>
+#include <QLoggingCategory>
+
+namespace OCC {
+
+namespace Mac {
+
+class FileProviderSocketController;
+using FileProviderSocketControllerPtr = QSharedPointer<FileProviderSocketController>;
+
+#ifdef Q_OS_MACOS
+QString fileProviderSocketPath();
+#endif
+
+class FileProviderSocketServer : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit FileProviderSocketServer(QObject *parent = nullptr);
+
+private slots:
+    void startListening();
+    void slotNewConnection();
+    void slotSocketDestroyed(const QLocalSocket * const socket);
+
+private:
+    QString _socketPath;
+    QLocalServer _socketServer;
+    QHash<const QLocalSocket*, FileProviderSocketControllerPtr> _socketControllers;
+};
+
+} // namespace Mac
+
+} // namespace OCC
diff --git a/src/gui/macOS/fileprovidersocketserver_mac.mm b/src/gui/macOS/fileprovidersocketserver_mac.mm
new file mode 100644 (file)
index 0000000..8377adb
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#import <Cocoa/Cocoa.h>
+#import <QString>
+
+#include "config.h"
+
+namespace OCC
+{
+
+namespace Mac
+{
+
+QString fileProviderSocketPath()
+{
+    // This must match the code signing Team setting of the extension
+    // Example for developer builds (with ad-hoc signing identity): "" "com.owncloud.desktopclient" ".fileprovidersocket"
+    // Example for official signed packages: "9B5WD74GWJ." "com.owncloud.desktopclient" ".fileprovidersocket"
+    NSString *appGroupId = @SOCKETAPI_TEAM_IDENTIFIER_PREFIX APPLICATION_REV_DOMAIN;
+
+    NSURL *container = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:appGroupId];
+    NSURL *socketPath = [container URLByAppendingPathComponent:@".fileprovidersocket" isDirectory:false];
+    return QString::fromNSString(socketPath.path);
+}
+
+} // namespace Mac
+
+} // namespace OCC