nextcloud-desktop/src/gui/socketapi.cpp

564 lines
20 KiB
C++
Raw Normal View History

/*
* Copyright (C) by Dominik Schmidt <dev@dominik-schmidt.de>
2014-07-14 17:28:26 +04:00
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
* Copyright (C) by Roeland Jago Douma <roeland@famdouma.nl>
*
* 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 "socketapi.h"
#include "config.h"
2014-11-10 01:25:57 +03:00
#include "configfile.h"
#include "folderman.h"
#include "folder.h"
#include "theme.h"
2017-09-01 19:11:43 +03:00
#include "common/syncjournalfilerecord.h"
#include "syncengine.h"
#include "syncfileitem.h"
#include "filesystem.h"
2014-08-27 14:02:47 +04:00
#include "version.h"
#include "account.h"
#include "accountstate.h"
#include "account.h"
#include "capabilities.h"
2017-09-01 19:11:43 +03:00
#include "common/asserts.h"
#include "guiutility.h"
#include <array>
#include <QBitArray>
#include <QUrl>
#include <QMetaMethod>
#include <QMetaObject>
#include <QStringList>
2014-06-02 14:08:06 +04:00
#include <QScopedPointer>
#include <QFile>
#include <QDir>
#include <QApplication>
#include <QLocalSocket>
#include <QStringBuilder>
#include <QClipboard>
2014-10-13 16:14:43 +04:00
#include <sqlite3.h>
#include <QStandardPaths>
2014-08-27 14:02:47 +04:00
// This is the version that is returned when the client asks for the VERSION.
// The first number should be changed if there is an incompatible change that breaks old clients.
// The second number should be changed when there are new features.
#define MIRALL_SOCKET_API_VERSION "1.0"
static inline QString removeTrailingSlash(QString path)
{
Q_ASSERT(path.endsWith(QLatin1Char('/')));
2017-05-17 11:55:42 +03:00
path.truncate(path.length() - 1);
return path;
}
2017-05-17 11:55:42 +03:00
static QString buildMessage(const QString &verb, const QString &path, const QString &status = QString::null)
{
QString msg(verb);
2017-05-17 11:55:42 +03:00
if (!status.isEmpty()) {
msg.append(QLatin1Char(':'));
msg.append(status);
}
2017-05-17 11:55:42 +03:00
if (!path.isEmpty()) {
msg.append(QLatin1Char(':'));
QFileInfo fi(path);
msg.append(QDir::toNativeSeparators(fi.absoluteFilePath()));
}
return msg;
}
2014-11-10 00:34:07 +03:00
namespace OCC {
Q_LOGGING_CATEGORY(lcSocketApi, "gui.socketapi", QtInfoMsg)
2017-05-17 11:55:42 +03:00
class BloomFilter
{
// Initialize with m=1024 bits and k=2 (high and low 16 bits of a qHash).
// For a client navigating in less than 100 directories, this gives us a probability less than (1-e^(-2*100/1024))^2 = 0.03147872136 false positives.
const static int NumBits = 1024;
public:
2017-05-17 11:55:42 +03:00
BloomFilter()
: hashBits(NumBits)
{
}
2017-05-17 11:55:42 +03:00
void storeHash(uint hash)
{
hashBits.setBit((hash & 0xFFFF) % NumBits);
hashBits.setBit((hash >> 16) % NumBits);
}
2017-05-17 11:55:42 +03:00
bool isHashMaybeStored(uint hash) const
{
return hashBits.testBit((hash & 0xFFFF) % NumBits)
&& hashBits.testBit((hash >> 16) % NumBits);
}
private:
QBitArray hashBits;
};
2017-05-17 11:55:42 +03:00
class SocketListener
{
public:
2017-05-17 11:55:42 +03:00
QIODevice *socket;
2017-05-17 11:55:42 +03:00
SocketListener(QIODevice *socket = 0)
: socket(socket)
{
}
2017-05-17 11:55:42 +03:00
void sendMessage(const QString &message, bool doWait = false) const
{
2017-07-04 17:41:40 +03:00
qCInfo(lcSocketApi) << "Sending SocketAPI message -->" << message << "to" << socket;
QString localMessage = message;
2017-05-17 11:55:42 +03:00
if (!localMessage.endsWith(QLatin1Char('\n'))) {
localMessage.append(QLatin1Char('\n'));
}
QByteArray bytesToSend = localMessage.toUtf8();
qint64 sent = socket->write(bytesToSend);
2017-05-17 11:55:42 +03:00
if (doWait) {
socket->waitForBytesWritten(1000);
}
2017-05-17 11:55:42 +03:00
if (sent != bytesToSend.length()) {
qCWarning(lcSocketApi) << "Could not send all data on socket for " << localMessage;
}
}
2017-05-17 11:55:42 +03:00
void sendMessageIfDirectoryMonitored(const QString &message, uint systemDirectoryHash) const
{
if (_monitoredDirectoriesBloomFilter.isHashMaybeStored(systemDirectoryHash))
sendMessage(message, false);
}
void registerMonitoredDirectory(uint systemDirectoryHash)
{
_monitoredDirectoriesBloomFilter.storeHash(systemDirectoryHash);
}
2017-05-17 11:55:42 +03:00
private:
BloomFilter _monitoredDirectoriesBloomFilter;
};
2017-05-17 11:55:42 +03:00
struct ListenerHasSocketPred
{
QIODevice *socket;
2017-05-17 11:55:42 +03:00
ListenerHasSocketPred(QIODevice *socket)
: socket(socket)
{
}
bool operator()(const SocketListener &listener) const { return listener.socket == socket; }
};
2017-05-17 11:55:42 +03:00
SocketApi::SocketApi(QObject *parent)
: QObject(parent)
{
QString socketPath;
if (Utility::isWindows()) {
socketPath = QLatin1String("\\\\.\\pipe\\")
2017-05-17 11:55:42 +03:00
+ QLatin1String("ownCloud-")
+ QString::fromLocal8Bit(qgetenv("USERNAME"));
// TODO: once the windows extension supports multiple
// client connections, switch back to the theme name
// See issue #2388
// + Theme::instance()->appName();
} else if (Utility::isMac()) {
// This must match the code signing Team setting of the extension
// Example for developer builds (with ad-hoc signing identity): "" "com.owncloud.desktopclient" ".socketApi"
// Example for official signed packages: "9B5WD74GWJ." "com.owncloud.desktopclient" ".socketApi"
socketPath = SOCKETAPI_TEAM_IDENTIFIER_PREFIX APPLICATION_REV_DOMAIN ".socketApi";
2017-05-17 11:55:42 +03:00
} else if (Utility::isLinux() || Utility::isBSD()) {
QString runtimeDir;
runtimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
socketPath = runtimeDir + "/" + Theme::instance()->appName() + "/socket";
} else {
2017-05-17 11:55:42 +03:00
qCWarning(lcSocketApi) << "An unexpected system detected, this probably won't work.";
}
SocketApiServer::removeServer(socketPath);
QFileInfo info(socketPath);
if (!info.dir().exists()) {
bool result = info.dir().mkpath(".");
qCDebug(lcSocketApi) << "creating" << info.dir().path() << result;
2017-05-17 11:55:42 +03:00
if (result) {
QFile::setPermissions(socketPath,
2017-05-17 11:55:42 +03:00
QFile::Permissions(QFile::ReadOwner + QFile::WriteOwner + QFile::ExeOwner));
}
}
2017-05-17 11:55:42 +03:00
if (!_localServer.listen(socketPath)) {
qCWarning(lcSocketApi) << "can't start server" << socketPath;
} else {
qCInfo(lcSocketApi) << "server started, listening at " << socketPath;
}
2017-09-23 14:42:39 +03:00
connect(&_localServer, &SocketApiServer::newConnection, this, &SocketApi::slotNewConnection);
// folder watcher
connect(FolderMan::instance(), &FolderMan::folderSyncStateChange, this, &SocketApi::slotUpdateFolderView);
}
SocketApi::~SocketApi()
{
qCDebug(lcSocketApi) << "dtor";
_localServer.close();
// All remaining sockets will be destroyed with _localServer, their parent
ASSERT(_listeners.isEmpty() || _listeners.first().socket->parent() == &_localServer);
_listeners.clear();
}
2014-06-02 14:08:06 +04:00
void SocketApi::slotNewConnection()
{
2017-05-17 11:55:42 +03:00
QIODevice *socket = _localServer.nextPendingConnection();
2017-05-17 11:55:42 +03:00
if (!socket) {
2014-06-02 14:08:06 +04:00
return;
}
qCInfo(lcSocketApi) << "New connection" << socket;
connect(socket, &QIODevice::readyRead, this, &SocketApi::slotReadSocket);
connect(socket, SIGNAL(disconnected()), this, SLOT(onLostConnection()));
connect(socket, &QObject::destroyed, this, &SocketApi::slotSocketDestroyed);
ASSERT(socket->readAll().isEmpty());
_listeners.append(SocketListener(socket));
SocketListener &listener = _listeners.last();
2017-05-17 11:55:42 +03:00
foreach (Folder *f, FolderMan::instance()->map()) {
if (f->canSync()) {
QString message = buildRegisterPathMessage(removeTrailingSlash(f->path()));
listener.sendMessage(message);
}
}
}
void SocketApi::onLostConnection()
{
qCInfo(lcSocketApi) << "Lost connection " << sender();
sender()->deleteLater();
}
2017-05-17 11:55:42 +03:00
void SocketApi::slotSocketDestroyed(QObject *obj)
{
2017-05-17 11:55:42 +03:00
QIODevice *socket = static_cast<QIODevice *>(obj);
_listeners.erase(std::remove_if(_listeners.begin(), _listeners.end(), ListenerHasSocketPred(socket)), _listeners.end());
}
2014-06-02 14:08:06 +04:00
void SocketApi::slotReadSocket()
{
2017-05-17 11:55:42 +03:00
QIODevice *socket = qobject_cast<QIODevice *>(sender());
ASSERT(socket);
SocketListener *listener = &*std::find_if(_listeners.begin(), _listeners.end(), ListenerHasSocketPred(socket));
2017-05-17 11:55:42 +03:00
while (socket->canReadLine()) {
// Make sure to normalize the input from the socket to
// make sure that the path will match, especially on OS X.
QString line = QString::fromUtf8(socket->readLine()).normalized(QString::NormalizationForm_C);
line.chop(1); // remove the '\n'
2017-07-04 17:41:40 +03:00
qCInfo(lcSocketApi) << "Received SocketAPI message <--" << line << "from" << socket;
QByteArray command = line.split(":").value(0).toAscii();
QByteArray functionWithArguments = "command_" + command + "(QString,SocketListener*)";
int indexOfMethod = staticMetaObject.indexOfMethod(functionWithArguments);
2017-05-17 11:55:42 +03:00
QString argument = line.remove(0, command.length() + 1);
if (indexOfMethod != -1) {
staticMetaObject.method(indexOfMethod).invoke(this, Q_ARG(QString, argument), Q_ARG(SocketListener *, listener));
2014-06-02 14:08:06 +04:00
} else {
qCWarning(lcSocketApi) << "The command is not supported by this version of the client:" << command << "with argument:" << argument;
}
}
}
2017-05-17 11:55:42 +03:00
void SocketApi::slotRegisterPath(const QString &alias)
{
// Make sure not to register twice to each connected client
if (_registeredAliases.contains(alias))
return;
Folder *f = FolderMan::instance()->folder(alias);
if (f) {
QString message = buildRegisterPathMessage(removeTrailingSlash(f->path()));
foreach (auto &listener, _listeners) {
listener.sendMessage(message);
}
}
_registeredAliases.insert(alias);
}
2017-05-17 11:55:42 +03:00
void SocketApi::slotUnregisterPath(const QString &alias)
{
if (!_registeredAliases.contains(alias))
return;
Folder *f = FolderMan::instance()->folder(alias);
if (f)
broadcastMessage(buildMessage(QLatin1String("UNREGISTER_PATH"), removeTrailingSlash(f->path()), QString::null), true);
_registeredAliases.remove(alias);
}
void SocketApi::slotUpdateFolderView(Folder *f)
{
if (_listeners.isEmpty()) {
return;
}
if (f) {
// do only send UPDATE_VIEW for a couple of status
2017-05-17 11:55:42 +03:00
if (f->syncResult().status() == SyncResult::SyncPrepare
|| f->syncResult().status() == SyncResult::Success
|| f->syncResult().status() == SyncResult::Paused
|| f->syncResult().status() == SyncResult::Problem
|| f->syncResult().status() == SyncResult::Error
|| f->syncResult().status() == SyncResult::SetupError) {
QString rootPath = removeTrailingSlash(f->path());
broadcastStatusPushMessage(rootPath, f->syncEngine().syncFileStatusTracker().fileStatus(""));
broadcastMessage(buildMessage(QLatin1String("UPDATE_VIEW"), rootPath));
} else {
qCDebug(lcSocketApi) << "Not sending UPDATE_VIEW for" << f->alias() << "because status() is" << f->syncResult().status();
}
}
}
2017-05-17 11:55:42 +03:00
void SocketApi::broadcastMessage(const QString &msg, bool doWait)
{
foreach (auto &listener, _listeners) {
listener.sendMessage(msg, doWait);
}
}
2017-05-17 11:55:42 +03:00
void SocketApi::broadcastStatusPushMessage(const QString &systemPath, SyncFileStatus fileStatus)
{
QString msg = buildMessage(QLatin1String("STATUS"), systemPath, fileStatus.toSocketAPIString());
Q_ASSERT(!systemPath.endsWith('/'));
uint directoryHash = qHash(systemPath.left(systemPath.lastIndexOf('/')));
foreach (auto &listener, _listeners) {
listener.sendMessageIfDirectoryMonitored(msg, directoryHash);
}
}
2017-05-17 11:55:42 +03:00
void SocketApi::command_RETRIEVE_FOLDER_STATUS(const QString &argument, SocketListener *listener)
{
// This command is the same as RETRIEVE_FILE_STATUS
command_RETRIEVE_FILE_STATUS(argument, listener);
}
2017-05-17 11:55:42 +03:00
void SocketApi::command_RETRIEVE_FILE_STATUS(const QString &argument, SocketListener *listener)
{
QString statusString;
2017-05-17 11:55:42 +03:00
Folder *syncFolder = FolderMan::instance()->folderForPath(argument);
if (!syncFolder) {
// this can happen in offline mode e.g.: nothing to worry about
statusString = QLatin1String("NOP");
} else {
QString systemPath = QDir::cleanPath(argument);
2017-05-17 11:55:42 +03:00
if (systemPath.endsWith(QLatin1Char('/'))) {
systemPath.truncate(systemPath.length() - 1);
qCWarning(lcSocketApi) << "Removed trailing slash for directory: " << systemPath << "Status pushes won't have one.";
}
// The user probably visited this directory in the file shell.
// Let the listener know that it should now send status pushes for sibblings of this file.
QString directory = systemPath.left(systemPath.lastIndexOf('/'));
listener->registerMonitoredDirectory(qHash(directory));
2017-05-17 11:55:42 +03:00
QString relativePath = systemPath.mid(syncFolder->cleanPath().length() + 1);
SyncFileStatus fileStatus = syncFolder->syncEngine().syncFileStatusTracker().fileStatus(relativePath);
statusString = fileStatus.toSocketAPIString();
}
2017-05-17 11:55:42 +03:00
const QString message = QLatin1String("STATUS:") % statusString % QLatin1Char(':') % QDir::toNativeSeparators(argument);
listener->sendMessage(message);
}
2017-05-17 11:55:42 +03:00
void SocketApi::command_SHARE(const QString &localFile, SocketListener *listener)
{
auto theme = Theme::instance();
Folder *shareFolder = FolderMan::instance()->folderForPath(localFile);
if (!shareFolder) {
2017-05-17 11:55:42 +03:00
const QString message = QLatin1String("SHARE:NOP:") + QDir::toNativeSeparators(localFile);
// files that are not within a sync folder are not synced.
listener->sendMessage(message);
} else if (!shareFolder->accountState()->isConnected()) {
2017-05-17 11:55:42 +03:00
const QString message = QLatin1String("SHARE:NOTCONNECTED:") + QDir::toNativeSeparators(localFile);
// if the folder isn't connected, don't open the share dialog
listener->sendMessage(message);
2017-05-17 11:55:42 +03:00
} else if (!theme->linkSharing() && (!theme->userGroupSharing() || shareFolder->accountState()->account()->serverVersionInt() < Account::makeServerVersion(8, 2, 0))) {
const QString message = QLatin1String("SHARE:NOP:") + QDir::toNativeSeparators(localFile);
listener->sendMessage(message);
} else {
const QString localFileClean = QDir::cleanPath(localFile);
2017-05-17 11:55:42 +03:00
const QString file = localFileClean.mid(shareFolder->cleanPath().length() + 1);
SyncFileStatus fileStatus = shareFolder->syncEngine().syncFileStatusTracker().fileStatus(file);
// Verify the file is on the server (to our knowledge of course)
if (fileStatus.tag() != SyncFileStatus::StatusUpToDate) {
2017-05-17 11:55:42 +03:00
const QString message = QLatin1String("SHARE:NOTSYNCED:") + QDir::toNativeSeparators(localFile);
listener->sendMessage(message);
return;
}
const QString remotePath = QDir(shareFolder->remotePath()).filePath(file);
// Can't share root folder
if (remotePath == "/") {
2017-05-17 11:55:42 +03:00
const QString message = QLatin1String("SHARE:CANNOTSHAREROOT:") + QDir::toNativeSeparators(localFile);
listener->sendMessage(message);
return;
}
2017-05-17 11:55:42 +03:00
const QString message = QLatin1String("SHARE:OK:") + QDir::toNativeSeparators(localFile);
listener->sendMessage(message);
emit shareCommandReceived(remotePath, localFileClean);
}
}
2017-05-17 11:55:42 +03:00
void SocketApi::command_VERSION(const QString &, SocketListener *listener)
2014-08-27 14:02:47 +04:00
{
listener->sendMessage(QLatin1String("VERSION:" MIRALL_VERSION_STRING ":" MIRALL_SOCKET_API_VERSION));
2014-08-27 14:02:47 +04:00
}
2017-05-17 11:55:42 +03:00
void SocketApi::command_SHARE_STATUS(const QString &localFile, SocketListener *listener)
{
Folder *shareFolder = FolderMan::instance()->folderForPath(localFile);
if (!shareFolder) {
2017-05-17 11:55:42 +03:00
const QString message = QLatin1String("SHARE_STATUS:NOP:") + QDir::toNativeSeparators(localFile);
listener->sendMessage(message);
} else {
2017-05-17 11:55:42 +03:00
const QString file = QDir::cleanPath(localFile).mid(shareFolder->cleanPath().length() + 1);
SyncFileStatus fileStatus = shareFolder->syncEngine().syncFileStatusTracker().fileStatus(file);
// Verify the file is on the server (to our knowledge of course)
if (fileStatus.tag() != SyncFileStatus::StatusUpToDate) {
2017-05-17 11:55:42 +03:00
const QString message = QLatin1String("SHARE_STATUS:NOTSYNCED:") + QDir::toNativeSeparators(localFile);
listener->sendMessage(message);
return;
}
const Capabilities capabilities = shareFolder->accountState()->account()->capabilities();
if (!capabilities.shareAPI()) {
2017-05-17 11:55:42 +03:00
const QString message = QLatin1String("SHARE_STATUS:DISABLED:") + QDir::toNativeSeparators(localFile);
listener->sendMessage(message);
} else {
auto theme = Theme::instance();
QString available;
if (theme->userGroupSharing()) {
available = "USER,GROUP";
}
if (theme->linkSharing() && capabilities.sharePublicLink()) {
if (available.isEmpty()) {
available = "LINK";
} else {
available += ",LINK";
}
}
if (available.isEmpty()) {
const QString message = QLatin1String("SHARE_STATUS:DISABLED") + ":" + QDir::toNativeSeparators(localFile);
listener->sendMessage(message);
} else {
const QString message = QLatin1String("SHARE_STATUS:") + available + ":" + QDir::toNativeSeparators(localFile);
listener->sendMessage(message);
}
}
}
}
2017-05-17 11:55:42 +03:00
void SocketApi::command_SHARE_MENU_TITLE(const QString &, SocketListener *listener)
{
listener->sendMessage(QLatin1String("SHARE_MENU_TITLE:") + tr("Share with %1", "parameter is ownCloud").arg(Theme::instance()->appNameGUI()));
}
// Fetches the private link url asynchronously and then calls the target slot
static void fetchPrivateLinkUrlHelper(const QString &localFile, SocketApi *target, void (SocketApi::*targetFun)(const QString &url) const)
{
Folder *shareFolder = FolderMan::instance()->folderForPath(localFile);
if (!shareFolder) {
qCWarning(lcSocketApi) << "Unknown path" << localFile;
return;
}
const QString localFileClean = QDir::cleanPath(localFile);
const QString file = localFileClean.mid(shareFolder->cleanPath().length() + 1);
AccountPtr account = shareFolder->accountState()->account();
SyncJournalFileRecord rec;
if (!shareFolder->journalDb()->getFileRecord(file, &rec) || !rec.isValid())
return;
fetchPrivateLinkUrl(account, file, rec.numericFileId(), target, [=](const QString &url) {
(target->*targetFun)(url);
});
}
void SocketApi::command_COPY_PRIVATE_LINK(const QString &localFile, SocketListener *)
{
fetchPrivateLinkUrlHelper(localFile, this, &SocketApi::copyPrivateLinkToClipboard);
}
void SocketApi::command_EMAIL_PRIVATE_LINK(const QString &localFile, SocketListener *)
{
fetchPrivateLinkUrlHelper(localFile, this, &SocketApi::emailPrivateLink);
}
void SocketApi::copyPrivateLinkToClipboard(const QString &link) const
{
QApplication::clipboard()->setText(link);
}
void SocketApi::emailPrivateLink(const QString &link) const
{
Utility::openEmailComposer(
tr("I shared something with you"),
link,
0);
}
void SocketApi::command_GET_STRINGS(const QString &, SocketListener *listener)
{
static std::array<std::pair<const char *, QString>, 5> strings { {
{ "SHARE_MENU_TITLE", tr("Share...") },
{ "CONTEXT_MENU_TITLE", Theme::instance()->appNameGUI() },
{ "COPY_PRIVATE_LINK_MENU_TITLE", tr("Copy private link to clipboard") },
{ "EMAIL_PRIVATE_LINK_MENU_TITLE", tr("Send private link by email...") },
} };
listener->sendMessage(QString("GET_STRINGS:BEGIN"));
for (auto key_value : strings) {
listener->sendMessage(QString("STRING:%1:%2").arg(key_value.first, key_value.second));
}
listener->sendMessage(QString("GET_STRINGS:END"));
}
2017-05-17 11:55:42 +03:00
QString SocketApi::buildRegisterPathMessage(const QString &path)
{
QFileInfo fi(path);
QString message = QLatin1String("REGISTER_PATH:");
message.append(QDir::toNativeSeparators(fi.absoluteFilePath()));
return message;
}
2014-11-10 00:34:07 +03:00
} // namespace OCC