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
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
#include "mtproto/mtproto_proxy_data.h"
|
2022-01-24 16:33:30 +00:00
|
|
|
#include "base/qt/qt_common_adapters.h"
|
2019-11-13 14:12:04 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
#include <QtNetwork/QLocalServer>
|
|
|
|
#include <QtNetwork/QLocalSocket>
|
|
|
|
#include <QtCore/QAbstractNativeEventFilter>
|
|
|
|
|
2021-11-25 01:32:31 +00:00
|
|
|
class QLockFile;
|
|
|
|
|
2017-12-11 14:45:29 +00:00
|
|
|
namespace Core {
|
2019-01-21 13:42:21 +00:00
|
|
|
|
2018-04-26 16:14:21 +00:00
|
|
|
class UpdateChecker;
|
2019-01-21 13:42:21 +00:00
|
|
|
class Application;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
class Sandbox final
|
|
|
|
: public QApplication
|
|
|
|
, private QAbstractNativeEventFilter {
|
2019-04-05 10:13:54 +00:00
|
|
|
private:
|
|
|
|
auto createEventNestingLevel() {
|
|
|
|
incrementEventNestingLevel();
|
|
|
|
return gsl::finally([=] { decrementEventNestingLevel(); });
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
public:
|
2023-06-13 00:34:27 +00:00
|
|
|
Sandbox(int &argc, char **argv);
|
2019-01-21 13:42:21 +00:00
|
|
|
|
|
|
|
Sandbox(const Sandbox &other) = delete;
|
|
|
|
Sandbox &operator=(const Sandbox &other) = delete;
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
int start();
|
2016-09-15 10:50:43 +00:00
|
|
|
|
2018-04-23 10:24:03 +00:00
|
|
|
void refreshGlobalProxy();
|
2017-02-23 10:59:19 +00:00
|
|
|
|
2018-12-12 08:59:51 +00:00
|
|
|
void postponeCall(FnMut<void()> &&callable);
|
|
|
|
bool notify(QObject *receiver, QEvent *e) override;
|
2019-04-05 10:13:54 +00:00
|
|
|
|
|
|
|
template <typename Callable>
|
|
|
|
auto customEnterFromEventLoop(Callable &&callable) {
|
|
|
|
registerEnterFromEventLoop();
|
|
|
|
const auto wrap = createEventNestingLevel();
|
|
|
|
return callable();
|
2018-12-27 09:34:12 +00:00
|
|
|
}
|
2018-12-12 08:59:51 +00:00
|
|
|
|
2019-03-07 13:23:19 +00:00
|
|
|
rpl::producer<> widgetUpdateRequests() const;
|
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
MTP::ProxyData sandboxProxy() const;
|
2017-02-23 10:59:19 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
static Sandbox &Instance() {
|
2019-09-04 07:19:15 +00:00
|
|
|
Expects(QCoreApplication::instance() != nullptr);
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
return *static_cast<Sandbox*>(QCoreApplication::instance());
|
2019-01-21 13:42:21 +00:00
|
|
|
}
|
2021-11-08 13:33:18 +00:00
|
|
|
static void QuitWhenStarted();
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
~Sandbox();
|
2016-02-29 16:53:26 +00:00
|
|
|
|
2018-12-26 09:02:43 +00:00
|
|
|
protected:
|
|
|
|
bool event(QEvent *e) override;
|
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
private:
|
|
|
|
typedef QPair<QLocalSocket*, QByteArray> LocalClient;
|
|
|
|
typedef QList<LocalClient> LocalClients;
|
|
|
|
|
2018-12-12 08:59:51 +00:00
|
|
|
struct PostponedCall {
|
|
|
|
int loopNestingLevel = 0;
|
|
|
|
FnMut<void()> callable;
|
|
|
|
};
|
|
|
|
|
2019-07-10 15:03:15 +00:00
|
|
|
bool notifyOrInvoke(QObject *receiver, QEvent *e);
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
void closeApplication(); // will be done in aboutToQuit()
|
|
|
|
void checkForQuit(); // will be done in exec()
|
2020-06-29 17:37:35 +00:00
|
|
|
void checkForEmptyLoopNestingLevel();
|
2019-04-05 10:13:54 +00:00
|
|
|
void registerEnterFromEventLoop();
|
2018-12-27 09:34:12 +00:00
|
|
|
void incrementEventNestingLevel();
|
|
|
|
void decrementEventNestingLevel();
|
2018-12-12 08:59:51 +00:00
|
|
|
bool nativeEventFilter(
|
|
|
|
const QByteArray &eventType,
|
|
|
|
void *message,
|
2021-10-19 13:00:21 +00:00
|
|
|
base::NativeEventResult *result) override;
|
2018-12-12 08:59:51 +00:00
|
|
|
void processPostponedCalls(int level);
|
2019-01-21 13:42:21 +00:00
|
|
|
void singleInstanceChecked();
|
|
|
|
void launchApplication();
|
2019-02-10 16:29:55 +00:00
|
|
|
void setupScreenScale();
|
2023-01-09 12:08:34 +00:00
|
|
|
|
|
|
|
// Return window id for activation.
|
|
|
|
uint64 execExternal(const QString &cmd);
|
2019-01-21 13:42:21 +00:00
|
|
|
|
|
|
|
// Single instance application
|
|
|
|
void socketConnected();
|
|
|
|
void socketError(QLocalSocket::LocalSocketError e);
|
|
|
|
void socketDisconnected();
|
|
|
|
void socketWritten(qint64 bytes);
|
|
|
|
void socketReading();
|
|
|
|
void newInstanceConnected();
|
|
|
|
|
|
|
|
void readClients();
|
|
|
|
void removeClients();
|
2018-12-12 08:59:51 +00:00
|
|
|
|
|
|
|
const Qt::HANDLE _mainThreadId = nullptr;
|
|
|
|
int _eventNestingLevel = 0;
|
|
|
|
int _loopNestingLevel = 0;
|
|
|
|
std::vector<int> _previousLoopNestingLevels;
|
|
|
|
std::vector<PostponedCall> _postponedCalls;
|
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
std::unique_ptr<Application> _application;
|
2017-02-23 10:59:19 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
QString _localServerName, _localSocketReadData;
|
|
|
|
QLocalServer _localServer;
|
|
|
|
QLocalSocket _localSocket;
|
|
|
|
LocalClients _localClients;
|
2021-11-25 01:32:31 +00:00
|
|
|
std::unique_ptr<QLockFile> _lockFile;
|
2016-04-14 19:24:42 +00:00
|
|
|
bool _secondInstance = false;
|
2021-11-08 13:33:18 +00:00
|
|
|
bool _started = false;
|
|
|
|
static bool QuitOnStartRequested;
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
std::unique_ptr<UpdateChecker> _updateChecker;
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
QByteArray _lastCrashDump;
|
2019-11-13 14:12:04 +00:00
|
|
|
MTP::ProxyData _sandboxProxy;
|
2018-04-26 16:14:21 +00:00
|
|
|
|
2019-03-07 13:23:19 +00:00
|
|
|
rpl::event_stream<> _widgetUpdateRequests;
|
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
};
|
|
|
|
|
2018-12-26 09:54:49 +00:00
|
|
|
} // namespace Core
|