2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/sandbox.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-09-17 16:13:12 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "mainwidget.h"
|
2017-02-23 10:59:19 +00:00
|
|
|
#include "mainwindow.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-10-02 13:54:27 +00:00
|
|
|
#include "window/notifications_manager.h"
|
2017-12-11 14:45:29 +00:00
|
|
|
#include "core/crash_reports.h"
|
2019-02-04 13:34:50 +00:00
|
|
|
#include "core/crash_report_window.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/launcher.h"
|
|
|
|
#include "core/local_url_handlers.h"
|
2019-02-04 13:34:50 +00:00
|
|
|
#include "core/update_checker.h"
|
2017-04-07 12:10:10 +00:00
|
|
|
#include "base/timer.h"
|
2018-08-09 20:39:22 +00:00
|
|
|
#include "base/concurrent_timer.h"
|
2019-07-10 15:03:15 +00:00
|
|
|
#include "base/invoke_queued.h"
|
2018-04-09 17:48:29 +00:00
|
|
|
#include "base/qthelp_url.h"
|
|
|
|
#include "base/qthelp_regex.h"
|
2019-02-04 13:34:50 +00:00
|
|
|
#include "ui/effects/animations.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
|
|
|
#include "app.h"
|
2015-06-03 18:13:01 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtGui/QScreen>
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
namespace Core {
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-04-13 05:11:53 +00:00
|
|
|
constexpr auto kEmptyPidForCommandResponse = 0ULL;
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
using ErrorSignal = void(QLocalSocket::*)(QLocalSocket::LocalSocketError);
|
|
|
|
const auto QLocalSocket_error = ErrorSignal(&QLocalSocket::error);
|
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
QChar _toHex(ushort v) {
|
|
|
|
v = v & 0x000F;
|
|
|
|
return QChar::fromLatin1((v >= 10) ? ('a' + (v - 10)) : ('0' + v));
|
|
|
|
}
|
|
|
|
ushort _fromHex(QChar c) {
|
|
|
|
return ((c.unicode() >= uchar('a')) ? (c.unicode() - uchar('a') + 10) : (c.unicode() - uchar('0'))) & 0x000F;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString _escapeTo7bit(const QString &str) {
|
|
|
|
QString result;
|
|
|
|
result.reserve(str.size() * 2);
|
|
|
|
for (int i = 0, l = str.size(); i != l; ++i) {
|
|
|
|
QChar ch(str.at(i));
|
|
|
|
ushort uch(ch.unicode());
|
|
|
|
if (uch < 32 || uch > 127 || uch == ushort(uchar('%'))) {
|
|
|
|
result.append('%').append(_toHex(uch >> 12)).append(_toHex(uch >> 8)).append(_toHex(uch >> 4)).append(_toHex(uch));
|
|
|
|
} else {
|
|
|
|
result.append(ch);
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 13:03:51 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
QString _escapeFrom7bit(const QString &str) {
|
|
|
|
QString result;
|
|
|
|
result.reserve(str.size());
|
|
|
|
for (int i = 0, l = str.size(); i != l; ++i) {
|
|
|
|
QChar ch(str.at(i));
|
|
|
|
if (ch == QChar::fromLatin1('%') && i + 4 < l) {
|
|
|
|
result.append(QChar(ushort((_fromHex(str.at(i + 1)) << 12) | (_fromHex(str.at(i + 2)) << 8) | (_fromHex(str.at(i + 3)) << 4) | _fromHex(str.at(i + 4)))));
|
|
|
|
i += 4;
|
|
|
|
} else {
|
|
|
|
result.append(ch);
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 13:03:51 +00:00
|
|
|
return result;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
Sandbox::Sandbox(
|
|
|
|
not_null<Core::Launcher*> launcher,
|
|
|
|
int &argc,
|
|
|
|
char **argv)
|
2019-05-06 10:40:04 +00:00
|
|
|
: QApplication(argc, argv)
|
|
|
|
, _mainThreadId(QThread::currentThreadId())
|
2019-09-17 07:51:02 +00:00
|
|
|
, _handleObservables([=] {
|
|
|
|
Expects(_application != nullptr);
|
|
|
|
|
|
|
|
_application->call_handleObservables();
|
|
|
|
})
|
2019-05-06 10:40:04 +00:00
|
|
|
, _launcher(launcher) {
|
2019-01-21 13:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Sandbox::start() {
|
2018-12-26 09:02:43 +00:00
|
|
|
if (!Core::UpdaterDisabled()) {
|
|
|
|
_updateChecker = std::make_unique<Core::UpdateChecker>();
|
|
|
|
}
|
2017-12-11 14:45:29 +00:00
|
|
|
const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath());
|
2014-07-18 10:37:34 +00:00
|
|
|
char h[33] = { 0 };
|
|
|
|
hashMd5Hex(d.constData(), d.size(), h);
|
2020-01-22 03:12:07 +00:00
|
|
|
_localServerName = Platform::SingleInstanceLocalServerName(h);
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
connect(
|
|
|
|
&_localSocket,
|
|
|
|
&QLocalSocket::connected,
|
|
|
|
[=] { socketConnected(); });
|
|
|
|
connect(
|
|
|
|
&_localSocket,
|
|
|
|
&QLocalSocket::disconnected,
|
|
|
|
[=] { socketDisconnected(); });
|
|
|
|
connect(
|
|
|
|
&_localSocket,
|
|
|
|
QLocalSocket_error,
|
|
|
|
[=](QLocalSocket::LocalSocketError error) { socketError(error); });
|
|
|
|
connect(
|
|
|
|
&_localSocket,
|
|
|
|
&QLocalSocket::bytesWritten,
|
|
|
|
[=](qint64 bytes) { socketWritten(bytes); });
|
|
|
|
connect(
|
|
|
|
&_localSocket,
|
|
|
|
&QLocalSocket::readyRead,
|
|
|
|
[=] { socketReading(); });
|
|
|
|
connect(
|
|
|
|
&_localServer,
|
|
|
|
&QLocalServer::newConnection,
|
|
|
|
[=] { newInstanceConnected(); });
|
|
|
|
|
|
|
|
crl::on_main(this, [=] { checkForQuit(); });
|
2019-04-06 08:12:24 +00:00
|
|
|
connect(this, &QCoreApplication::aboutToQuit, [=] {
|
|
|
|
customEnterFromEventLoop([&] {
|
|
|
|
closeApplication();
|
|
|
|
});
|
|
|
|
});
|
2016-01-30 18:24:18 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
if (cManyInstance()) {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Many instance allowed, starting..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
singleInstanceChecked();
|
|
|
|
} else {
|
2019-01-21 13:42:21 +00:00
|
|
|
LOG(("Connecting local socket to %1...").arg(_localServerName));
|
2016-01-11 15:43:29 +00:00
|
|
|
_localSocket.connectToServer(_localServerName);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-12-26 09:02:43 +00:00
|
|
|
|
|
|
|
return exec();
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::launchApplication() {
|
2019-02-10 16:29:55 +00:00
|
|
|
InvokeQueued(this, [=] {
|
|
|
|
if (App::quitting()) {
|
|
|
|
quit();
|
|
|
|
} else if (_application) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setupScreenScale();
|
|
|
|
|
2019-09-17 07:51:02 +00:00
|
|
|
base::InitObservables([] {
|
|
|
|
Instance()._handleObservables.call();
|
|
|
|
});
|
|
|
|
|
2019-02-10 16:29:55 +00:00
|
|
|
_application = std::make_unique<Application>(_launcher);
|
|
|
|
|
|
|
|
// Ideally this should go to constructor.
|
|
|
|
// But we want to catch all native events and Application installs
|
|
|
|
// its own filter that can filter out some of them. So we install
|
|
|
|
// our filter after the Application constructor installs his.
|
|
|
|
installNativeEventFilter(this);
|
|
|
|
|
|
|
|
_application->run();
|
|
|
|
});
|
|
|
|
}
|
2019-01-21 13:42:21 +00:00
|
|
|
|
2019-02-10 16:29:55 +00:00
|
|
|
void Sandbox::setupScreenScale() {
|
2019-01-21 13:42:21 +00:00
|
|
|
const auto dpi = Sandbox::primaryScreen()->logicalDotsPerInch();
|
|
|
|
LOG(("Primary screen DPI: %1").arg(dpi));
|
|
|
|
if (dpi <= 108) {
|
|
|
|
cSetScreenScale(100); // 100%: 96 DPI (0-108)
|
|
|
|
} else if (dpi <= 132) {
|
|
|
|
cSetScreenScale(125); // 125%: 120 DPI (108-132)
|
|
|
|
} else if (dpi <= 168) {
|
|
|
|
cSetScreenScale(150); // 150%: 144 DPI (132-168)
|
|
|
|
} else if (dpi <= 216) {
|
|
|
|
cSetScreenScale(200); // 200%: 192 DPI (168-216)
|
|
|
|
} else if (dpi <= 264) {
|
|
|
|
cSetScreenScale(250); // 250%: 240 DPI (216-264)
|
|
|
|
} else {
|
|
|
|
cSetScreenScale(300); // 300%: 288 DPI (264-inf)
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto ratio = devicePixelRatio();
|
|
|
|
if (ratio > 1.) {
|
2019-06-03 14:41:23 +00:00
|
|
|
if (!Platform::IsMac() || (ratio != 2.)) {
|
2019-01-21 13:42:21 +00:00
|
|
|
LOG(("Found non-trivial Device Pixel Ratio: %1").arg(ratio));
|
|
|
|
LOG(("Environmental variables: QT_DEVICE_PIXEL_RATIO='%1'").arg(QString::fromLatin1(qgetenv("QT_DEVICE_PIXEL_RATIO"))));
|
|
|
|
LOG(("Environmental variables: QT_SCALE_FACTOR='%1'").arg(QString::fromLatin1(qgetenv("QT_SCALE_FACTOR"))));
|
|
|
|
LOG(("Environmental variables: QT_AUTO_SCREEN_SCALE_FACTOR='%1'").arg(QString::fromLatin1(qgetenv("QT_AUTO_SCREEN_SCALE_FACTOR"))));
|
|
|
|
LOG(("Environmental variables: QT_SCREEN_SCALE_FACTORS='%1'").arg(QString::fromLatin1(qgetenv("QT_SCREEN_SCALE_FACTORS"))));
|
|
|
|
}
|
2019-09-13 10:24:06 +00:00
|
|
|
style::SetDevicePixelRatio(int(ratio));
|
|
|
|
cSetScreenScale(style::kScaleDefault);
|
2019-01-21 13:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Sandbox::~Sandbox() = default;
|
2017-02-23 10:59:19 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
bool Sandbox::event(QEvent *e) {
|
2016-09-15 10:50:43 +00:00
|
|
|
if (e->type() == QEvent::Close) {
|
|
|
|
App::quit();
|
|
|
|
}
|
|
|
|
return QApplication::event(e);
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::socketConnected() {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Socket connected, this is not the first application instance, sending show command..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
_secondInstance = true;
|
|
|
|
|
|
|
|
QString commands;
|
|
|
|
const QStringList &lst(cSendPaths());
|
|
|
|
for (QStringList::const_iterator i = lst.cbegin(), e = lst.cend(); i != e; ++i) {
|
|
|
|
commands += qsl("SEND:") + _escapeTo7bit(*i) + ';';
|
|
|
|
}
|
|
|
|
if (!cStartUrl().isEmpty()) {
|
|
|
|
commands += qsl("OPEN:") + _escapeTo7bit(cStartUrl()) + ';';
|
2018-06-06 09:06:07 +00:00
|
|
|
} else {
|
|
|
|
commands += qsl("CMD:show;");
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
DEBUG_LOG(("Sandbox Info: writing commands %1").arg(commands));
|
2016-01-11 15:43:29 +00:00
|
|
|
_localSocket.write(commands.toLatin1());
|
|
|
|
}
|
2014-06-15 12:31:03 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::socketWritten(qint64/* bytes*/) {
|
2016-01-11 15:43:29 +00:00
|
|
|
if (_localSocket.state() != QLocalSocket::ConnectedState) {
|
2016-01-17 05:01:14 +00:00
|
|
|
LOG(("Socket is not connected %1").arg(_localSocket.state()));
|
2016-01-11 15:43:29 +00:00
|
|
|
return;
|
2014-12-19 21:20:30 +00:00
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
if (_localSocket.bytesToWrite()) {
|
|
|
|
return;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Show command written, waiting response..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::socketReading() {
|
2016-01-11 15:43:29 +00:00
|
|
|
if (_localSocket.state() != QLocalSocket::ConnectedState) {
|
2016-01-17 05:01:14 +00:00
|
|
|
LOG(("Socket is not connected %1").arg(_localSocket.state()));
|
2016-01-11 15:43:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_localSocketReadData.append(_localSocket.readAll());
|
|
|
|
if (QRegularExpression("RES:(\\d+);").match(_localSocketReadData).hasMatch()) {
|
|
|
|
uint64 pid = _localSocketReadData.mid(4, _localSocketReadData.length() - 5).toULongLong();
|
2018-03-31 17:07:29 +00:00
|
|
|
if (pid != kEmptyPidForCommandResponse) {
|
|
|
|
psActivateProcess(pid);
|
|
|
|
}
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Show command response received, pid = %1, activating and quitting...").arg(pid));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 20:19:05 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::socketError(QLocalSocket::LocalSocketError e) {
|
2016-03-02 18:34:42 +00:00
|
|
|
if (App::quitting()) return;
|
2016-01-21 06:58:58 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
if (_secondInstance) {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Could not write show command, error %1, quitting...").arg(e));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
if (e == QLocalSocket::ServerNotFoundError) {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("This is the only instance of Telegram, starting server and app..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
} else {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Socket connect error %1, starting server and app...").arg(e));
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
_localSocket.close();
|
2014-11-26 16:45:52 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
// Local server does not work in WinRT build.
|
2016-04-26 13:00:23 +00:00
|
|
|
#ifndef Q_OS_WINRT
|
2016-01-11 15:43:29 +00:00
|
|
|
psCheckLocalSocket(_localServerName);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
if (!_localServer.listen(_localServerName)) {
|
2020-03-04 18:21:09 +00:00
|
|
|
LOG(("Failed to start listening to %1 server: %2").arg(_localServerName).arg(_localServer.errorString()));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
2016-04-26 13:00:23 +00:00
|
|
|
#endif // !Q_OS_WINRT
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-07-11 22:14:44 +00:00
|
|
|
if (!Core::UpdaterDisabled()
|
|
|
|
&& !cNoStartUpdate()
|
|
|
|
&& Core::checkReadyUpdate()) {
|
2016-01-11 15:43:29 +00:00
|
|
|
cSetRestartingUpdate(true);
|
2019-01-21 13:42:21 +00:00
|
|
|
DEBUG_LOG(("Sandbox Info: installing update instead of starting app..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
2015-10-15 11:51:10 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
singleInstanceChecked();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::singleInstanceChecked() {
|
2016-01-17 05:01:14 +00:00
|
|
|
if (cManyInstance()) {
|
|
|
|
Logs::multipleInstances();
|
|
|
|
}
|
|
|
|
|
2018-04-23 10:24:03 +00:00
|
|
|
refreshGlobalProxy();
|
2016-01-21 06:58:58 +00:00
|
|
|
if (!Logs::started() || (!cManyInstance() && !Logs::instanceChecked())) {
|
2016-01-25 10:22:58 +00:00
|
|
|
new NotStartedWindow();
|
2019-01-21 13:42:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto result = CrashReports::Start();
|
|
|
|
result.match([&](CrashReports::Status status) {
|
2017-12-11 14:45:29 +00:00
|
|
|
if (status == CrashReports::CantOpen) {
|
2016-01-25 10:22:58 +00:00
|
|
|
new NotStartedWindow();
|
2019-01-21 13:42:21 +00:00
|
|
|
} else {
|
|
|
|
launchApplication();
|
|
|
|
}
|
|
|
|
}, [&](const QByteArray &crashdump) {
|
|
|
|
// If crash dump is empty with that status it means that we
|
|
|
|
// didn't close the application properly. Just ignore for now.
|
|
|
|
if (crashdump.isEmpty()) {
|
|
|
|
if (CrashReports::Restart() == CrashReports::CantOpen) {
|
|
|
|
new NotStartedWindow();
|
2016-02-07 15:38:49 +00:00
|
|
|
} else {
|
2019-01-21 13:42:21 +00:00
|
|
|
launchApplication();
|
2016-02-07 15:38:49 +00:00
|
|
|
}
|
2019-01-21 13:42:21 +00:00
|
|
|
return;
|
2016-01-21 06:58:58 +00:00
|
|
|
}
|
2019-01-21 13:42:21 +00:00
|
|
|
_lastCrashDump = crashdump;
|
|
|
|
auto window = new LastCrashedWindow(
|
|
|
|
_launcher,
|
|
|
|
_lastCrashDump,
|
|
|
|
[=] { launchApplication(); });
|
|
|
|
window->proxyChanges(
|
2019-11-13 14:12:04 +00:00
|
|
|
) | rpl::start_with_next([=](MTP::ProxyData &&proxy) {
|
2019-01-21 13:42:21 +00:00
|
|
|
_sandboxProxy = std::move(proxy);
|
|
|
|
refreshGlobalProxy();
|
|
|
|
}, window->lifetime());
|
|
|
|
});
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::socketDisconnected() {
|
2016-01-11 15:43:29 +00:00
|
|
|
if (_secondInstance) {
|
2019-01-21 13:42:21 +00:00
|
|
|
DEBUG_LOG(("Sandbox Error: socket disconnected before command response received, quitting..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::newInstanceConnected() {
|
|
|
|
DEBUG_LOG(("Sandbox Info: new local socket connected"));
|
|
|
|
for (auto client = _localServer.nextPendingConnection(); client; client = _localServer.nextPendingConnection()) {
|
2016-01-11 15:43:29 +00:00
|
|
|
_localClients.push_back(LocalClient(client, QByteArray()));
|
2019-01-21 13:42:21 +00:00
|
|
|
connect(
|
|
|
|
client,
|
|
|
|
&QLocalSocket::readyRead,
|
|
|
|
[=] { readClients(); });
|
|
|
|
connect(
|
|
|
|
client,
|
|
|
|
&QLocalSocket::disconnected,
|
|
|
|
[=] { removeClients(); });
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::readClients() {
|
|
|
|
// This method can be called before Application is constructed.
|
2016-01-11 15:43:29 +00:00
|
|
|
QString startUrl;
|
|
|
|
QStringList toSend;
|
|
|
|
for (LocalClients::iterator i = _localClients.begin(), e = _localClients.end(); i != e; ++i) {
|
|
|
|
i->second.append(i->first->readAll());
|
|
|
|
if (i->second.size()) {
|
|
|
|
QString cmds(QString::fromLatin1(i->second));
|
|
|
|
int32 from = 0, l = cmds.length();
|
|
|
|
for (int32 to = cmds.indexOf(QChar(';'), from); to >= from; to = (from < l) ? cmds.indexOf(QChar(';'), from) : -1) {
|
|
|
|
QStringRef cmd(&cmds, from, to - from);
|
|
|
|
if (cmd.startsWith(qsl("CMD:"))) {
|
2019-01-21 13:42:21 +00:00
|
|
|
execExternal(cmds.mid(from + 4, to - from - 4));
|
|
|
|
const auto response = qsl("RES:%1;").arg(QApplication::applicationPid()).toLatin1();
|
2016-01-11 15:43:29 +00:00
|
|
|
i->first->write(response.data(), response.size());
|
|
|
|
} else if (cmd.startsWith(qsl("SEND:"))) {
|
|
|
|
if (cSendPaths().isEmpty()) {
|
|
|
|
toSend.append(_escapeFrom7bit(cmds.mid(from + 5, to - from - 5)));
|
|
|
|
}
|
|
|
|
} else if (cmd.startsWith(qsl("OPEN:"))) {
|
2018-03-31 17:07:29 +00:00
|
|
|
auto activateRequired = true;
|
2016-01-11 15:43:29 +00:00
|
|
|
if (cStartUrl().isEmpty()) {
|
2016-07-28 17:01:08 +00:00
|
|
|
startUrl = _escapeFrom7bit(cmds.mid(from + 5, to - from - 5)).mid(0, 8192);
|
2018-03-31 17:07:29 +00:00
|
|
|
activateRequired = StartUrlRequiresActivate(startUrl);
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
2018-03-31 17:07:29 +00:00
|
|
|
if (activateRequired) {
|
2019-01-21 13:42:21 +00:00
|
|
|
execExternal("show");
|
2018-03-31 17:07:29 +00:00
|
|
|
}
|
|
|
|
const auto responsePid = activateRequired
|
2019-01-21 13:42:21 +00:00
|
|
|
? QApplication::applicationPid()
|
2018-03-31 17:07:29 +00:00
|
|
|
: kEmptyPidForCommandResponse;
|
|
|
|
const auto response = qsl("RES:%1;").arg(responsePid).toLatin1();
|
|
|
|
i->first->write(response.data(), response.size());
|
2016-01-11 15:43:29 +00:00
|
|
|
} else {
|
2019-01-21 13:42:21 +00:00
|
|
|
LOG(("Sandbox Error: unknown command %1 passed in local socket").arg(QString(cmd.constData(), cmd.length())));
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
from = to + 1;
|
|
|
|
}
|
|
|
|
if (from > 0) {
|
|
|
|
i->second = i->second.mid(from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!toSend.isEmpty()) {
|
|
|
|
QStringList paths(cSendPaths());
|
|
|
|
paths.append(toSend);
|
|
|
|
cSetSendPaths(paths);
|
|
|
|
}
|
|
|
|
if (!cSendPaths().isEmpty()) {
|
|
|
|
if (App::wnd()) {
|
|
|
|
App::wnd()->sendPaths();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!startUrl.isEmpty()) {
|
|
|
|
cSetStartUrl(startUrl);
|
|
|
|
}
|
2019-01-21 13:42:21 +00:00
|
|
|
if (_application) {
|
|
|
|
_application->checkStartUrl();
|
2017-07-05 18:45:46 +00:00
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::removeClients() {
|
|
|
|
DEBUG_LOG(("Sandbox Info: remove clients slot called, clients %1"
|
|
|
|
).arg(_localClients.size()));
|
|
|
|
for (auto i = _localClients.begin(), e = _localClients.end(); i != e;) {
|
2016-01-11 15:43:29 +00:00
|
|
|
if (i->first->state() != QLocalSocket::ConnectedState) {
|
2019-01-21 13:42:21 +00:00
|
|
|
DEBUG_LOG(("Sandbox Info: removing client"));
|
2016-01-11 15:43:29 +00:00
|
|
|
i = _localClients.erase(i);
|
|
|
|
e = _localClients.end();
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::checkForQuit() {
|
2016-03-02 18:34:42 +00:00
|
|
|
if (App::quitting()) {
|
2016-02-29 16:53:26 +00:00
|
|
|
quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::refreshGlobalProxy() {
|
2018-04-23 10:24:03 +00:00
|
|
|
#ifndef TDESKTOP_DISABLE_NETWORK_PROXY
|
2019-01-21 13:42:21 +00:00
|
|
|
const auto proxy = !Global::started()
|
|
|
|
? _sandboxProxy
|
2019-11-13 14:12:04 +00:00
|
|
|
: (Global::ProxySettings() == MTP::ProxyData::Settings::Enabled)
|
2019-01-21 13:42:21 +00:00
|
|
|
? Global::SelectedProxy()
|
2019-11-13 14:12:04 +00:00
|
|
|
: MTP::ProxyData();
|
|
|
|
if (proxy.type == MTP::ProxyData::Type::Socks5
|
|
|
|
|| proxy.type == MTP::ProxyData::Type::Http) {
|
2018-05-17 19:58:00 +00:00
|
|
|
QNetworkProxy::setApplicationProxy(
|
2019-11-13 14:12:04 +00:00
|
|
|
MTP::ToNetworkProxy(MTP::ToDirectIpProxy(proxy)));
|
2018-11-05 13:58:24 +00:00
|
|
|
} else if (!Global::started()
|
2019-11-13 14:12:04 +00:00
|
|
|
|| Global::ProxySettings() == MTP::ProxyData::Settings::System) {
|
2018-04-23 10:24:03 +00:00
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(true);
|
2018-11-05 13:58:24 +00:00
|
|
|
} else {
|
|
|
|
QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);
|
2018-04-23 10:24:03 +00:00
|
|
|
}
|
|
|
|
#endif // TDESKTOP_DISABLE_NETWORK_PROXY
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
uint64 Sandbox::installationTag() const {
|
|
|
|
return _launcher->installationTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sandbox::postponeCall(FnMut<void()> &&callable) {
|
2018-12-12 08:59:51 +00:00
|
|
|
Expects(callable != nullptr);
|
2018-12-25 10:11:10 +00:00
|
|
|
Expects(_eventNestingLevel >= _loopNestingLevel);
|
|
|
|
|
|
|
|
// _loopNestingLevel == _eventNestingLevel means that we had a
|
|
|
|
// native event in a nesting loop that didn't get a notify() call
|
|
|
|
// after. That means we already have exited the nesting loop and
|
|
|
|
// there must not be any postponed calls with that nesting level.
|
|
|
|
if (_loopNestingLevel == _eventNestingLevel) {
|
|
|
|
Assert(_postponedCalls.empty()
|
|
|
|
|| _postponedCalls.back().loopNestingLevel < _loopNestingLevel);
|
|
|
|
Assert(!_previousLoopNestingLevels.empty());
|
|
|
|
|
|
|
|
_loopNestingLevel = _previousLoopNestingLevels.back();
|
|
|
|
_previousLoopNestingLevels.pop_back();
|
|
|
|
}
|
2018-12-12 08:59:51 +00:00
|
|
|
|
|
|
|
_postponedCalls.push_back({
|
|
|
|
_loopNestingLevel,
|
|
|
|
std::move(callable)
|
2019-01-21 13:42:21 +00:00
|
|
|
});
|
2018-12-12 08:59:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::incrementEventNestingLevel() {
|
2018-12-12 08:59:51 +00:00
|
|
|
++_eventNestingLevel;
|
2018-12-27 09:34:12 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::decrementEventNestingLevel() {
|
2018-12-12 08:59:51 +00:00
|
|
|
if (_eventNestingLevel == _loopNestingLevel) {
|
|
|
|
_loopNestingLevel = _previousLoopNestingLevels.back();
|
|
|
|
_previousLoopNestingLevels.pop_back();
|
|
|
|
}
|
2018-12-20 16:02:44 +00:00
|
|
|
const auto processTillLevel = _eventNestingLevel - 1;
|
|
|
|
processPostponedCalls(processTillLevel);
|
|
|
|
_eventNestingLevel = processTillLevel;
|
2018-12-27 09:34:12 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::registerEnterFromEventLoop() {
|
2018-12-27 09:34:12 +00:00
|
|
|
if (_eventNestingLevel > _loopNestingLevel) {
|
|
|
|
_previousLoopNestingLevels.push_back(_loopNestingLevel);
|
|
|
|
_loopNestingLevel = _eventNestingLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:03:15 +00:00
|
|
|
bool Sandbox::notifyOrInvoke(QObject *receiver, QEvent *e) {
|
|
|
|
if (e->type() == base::InvokeQueuedEvent::kType) {
|
|
|
|
static_cast<base::InvokeQueuedEvent*>(e)->invoke();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return QApplication::notify(receiver, e);
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
bool Sandbox::notify(QObject *receiver, QEvent *e) {
|
2018-12-27 09:34:12 +00:00
|
|
|
if (QThread::currentThreadId() != _mainThreadId) {
|
2019-07-10 15:03:15 +00:00
|
|
|
return notifyOrInvoke(receiver, e);
|
2018-12-27 09:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto wrap = createEventNestingLevel();
|
2019-07-10 15:03:15 +00:00
|
|
|
if (e->type() == QEvent::UpdateRequest) {
|
2019-09-13 12:22:54 +00:00
|
|
|
const auto weak = QPointer<QObject>(receiver);
|
2019-03-07 13:23:19 +00:00
|
|
|
_widgetUpdateRequests.fire({});
|
2019-07-05 14:29:02 +00:00
|
|
|
if (!weak) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-04 13:34:50 +00:00
|
|
|
}
|
2019-07-10 15:03:15 +00:00
|
|
|
return notifyOrInvoke(receiver, e);
|
2018-12-12 08:59:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::processPostponedCalls(int level) {
|
2018-12-12 08:59:51 +00:00
|
|
|
while (!_postponedCalls.empty()) {
|
|
|
|
auto &last = _postponedCalls.back();
|
|
|
|
if (last.loopNestingLevel != level) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto taken = std::move(last);
|
|
|
|
_postponedCalls.pop_back();
|
|
|
|
taken.callable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
bool Sandbox::nativeEventFilter(
|
2019-09-09 07:37:22 +00:00
|
|
|
const QByteArray &eventType,
|
|
|
|
void *message,
|
|
|
|
long *result) {
|
2018-12-27 09:34:12 +00:00
|
|
|
registerEnterFromEventLoop();
|
2018-12-12 08:59:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:23:19 +00:00
|
|
|
rpl::producer<> Sandbox::widgetUpdateRequests() const {
|
|
|
|
return _widgetUpdateRequests.events();
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
MTP::ProxyData Sandbox::sandboxProxy() const {
|
2019-01-21 13:42:21 +00:00
|
|
|
return _sandboxProxy;
|
|
|
|
}
|
2016-03-02 20:50:58 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::closeApplication() {
|
|
|
|
if (App::launchState() == App::QuitProcessed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
App::setLaunchState(App::QuitProcessed);
|
2016-02-29 16:53:26 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
_application = nullptr;
|
2016-02-29 16:53:26 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
_localServer.close();
|
2019-01-21 13:42:21 +00:00
|
|
|
for (const auto &localClient : base::take(_localClients)) {
|
|
|
|
localClient.first->close();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
_localClients.clear();
|
2016-02-29 16:53:26 +00:00
|
|
|
|
|
|
|
_localSocket.close();
|
|
|
|
|
2018-04-26 16:14:21 +00:00
|
|
|
_updateChecker = nullptr;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void Sandbox::execExternal(const QString &cmd) {
|
|
|
|
DEBUG_LOG(("Sandbox Info: executing external command '%1'").arg(cmd));
|
2017-02-23 10:59:19 +00:00
|
|
|
if (cmd == "show") {
|
|
|
|
if (App::wnd()) {
|
|
|
|
App::wnd()->activate();
|
|
|
|
} else if (PreLaunchWindow::instance()) {
|
|
|
|
PreLaunchWindow::instance()->activate();
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
} // namespace Core
|
2019-05-28 11:39:38 +00:00
|
|
|
|
|
|
|
namespace crl {
|
|
|
|
|
|
|
|
rpl::producer<> on_main_update_requests() {
|
|
|
|
return Core::Sandbox::Instance().widgetUpdateRequests();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace crl
|
2019-09-13 16:45:48 +00:00
|
|
|
|
|
|
|
namespace base {
|
|
|
|
|
|
|
|
void EnterFromEventLoop(FnMut<void()> &&method) {
|
|
|
|
Core::Sandbox::Instance().customEnterFromEventLoop(std::move(method));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace base
|