-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
BrowserShared.cpp
65 lines (56 loc) · 2.5 KB
/
BrowserShared.cpp
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
61
62
63
64
65
/*
* Copyright (C) 2020 KeePassXC Team <[email protected]>
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BrowserShared.h"
#include "config-keepassx.h"
#include <QStandardPaths>
#include <QDir>
#if defined(KEEPASSXC_DIST_SNAP)
#include <QProcessEnvironment>
#endif
namespace BrowserShared
{
QString localServerPath()
{
const auto serverName = QStringLiteral("/org.keepassxc.KeePassXC.BrowserServer");
#if defined(KEEPASSXC_DIST_SNAP)
return QProcessEnvironment::systemEnvironment().value("SNAP_USER_COMMON") + serverName;
#elif defined(KEEPASSXC_DIST_FLATPAK)
return QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) + "/app/" + "org.keepassxc.KeePassXC"
+ serverName;
#elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
// Use XDG_RUNTIME_DIR if available, else use /tmp.
QString xdgRuntimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
if (xdgRuntimeDir.isEmpty()) {
return QStandardPaths::writableLocation(QStandardPaths::TempLocation) + serverName;
}
QDir dir = QDir(xdgRuntimeDir);
// Put the socket in a dedicated directory.
// This directory will be easily mountable by sandbox containers.
QDir socketDir = dir.filePath("app/org.keepassxc.KeePassXC");
if (!socketDir.exists()) dir.mkpath("app/org.keepassxc.KeePassXC");
QString socketPath = socketDir.path() + "/BrowserServer.socket";
// Create a symlink at the legacy location for backwards compatibility.
QFile::link(socketPath, xdgRuntimeDir + serverName);
return socketPath;
#elif defined(Q_OS_WIN)
// Windows uses named pipes
return serverName + "_" + qgetenv("USERNAME");
#else // Q_OS_MACOS and others
return QStandardPaths::writableLocation(QStandardPaths::TempLocation) + serverName;
#endif
}
} // namespace BrowserShared