diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index 1202168ae..533ad5c77 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -143,9 +143,8 @@ struct Application::Private { Settings settings; }; -Application::Application(not_null launcher) +Application::Application() : QObject() -, _launcher(launcher) , _private(std::make_unique()) , _platformIntegration(Platform::Integration::Create()) , _batterySaving(std::make_unique()) @@ -945,11 +944,11 @@ rpl::producer<> Application::materializeLocalDraftsRequests() const { void Application::switchDebugMode() { if (Logs::DebugEnabled()) { Logs::SetDebugEnabled(false); - _launcher->writeDebugModeSetting(); + Launcher::Instance().writeDebugModeSetting(); Restart(); } else { Logs::SetDebugEnabled(true); - _launcher->writeDebugModeSetting(); + Launcher::Instance().writeDebugModeSetting(); DEBUG_LOG(("Debug logs started.")); if (_lastActivePrimaryWindow) { _lastActivePrimaryWindow->hideLayer(); @@ -957,10 +956,6 @@ void Application::switchDebugMode() { } } -void Application::writeInstallBetaVersionsSetting() { - _launcher->writeInstallBetaVersionsSetting(); -} - Main::Account &Application::activeAccount() const { return _domain->active(); } @@ -1770,7 +1765,7 @@ void Application::RegisterUrlScheme() { .executable = (!Platform::IsLinux() || !Core::UpdaterDisabled()) ? (cExeDir() + cExeName()) : cExeName(), - .arguments = Sandbox::Instance().customWorkingDir() + .arguments = Launcher::Instance().customWorkingDir() ? u"-workdir \"%1\""_q.arg(cWorkingDir()) : QString(), .protocol = u"tg"_q, diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h index ef65d170e..f9059da8d 100644 --- a/Telegram/SourceFiles/core/application.h +++ b/Telegram/SourceFiles/core/application.h @@ -103,7 +103,6 @@ class Instance; namespace Core { -class Launcher; struct LocalUrlHandler; class Settings; class Tray; @@ -126,16 +125,13 @@ public: MTP::ProxyData now; }; - Application(not_null launcher); + Application(); Application(const Application &other) = delete; Application &operator=(const Application &other) = delete; ~Application(); void run(); - [[nodiscard]] Launcher &launcher() const { - return *_launcher; - } [[nodiscard]] Platform::Integration &platformIntegration() const { return *_platformIntegration; } @@ -319,7 +315,6 @@ public: [[nodiscard]] rpl::producer<> materializeLocalDraftsRequests() const; void switchDebugMode(); - void writeInstallBetaVersionsSetting(); void preventOrInvoke(Fn &&callback); @@ -381,7 +376,6 @@ private: }; InstanceSetter _setter = { this }; - const not_null _launcher; rpl::event_stream _proxyChanges; // Some fields are just moved from the declaration. diff --git a/Telegram/SourceFiles/core/crash_report_window.cpp b/Telegram/SourceFiles/core/crash_report_window.cpp index e8e866ab2..41bacf4ba 100644 --- a/Telegram/SourceFiles/core/crash_report_window.cpp +++ b/Telegram/SourceFiles/core/crash_report_window.cpp @@ -9,7 +9,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/crash_reports.h" #include "core/application.h" -#include "core/launcher.h" #include "core/sandbox.h" #include "core/update_checker.h" #include "core/ui_integration.h" @@ -250,7 +249,6 @@ LastCrashedWindow::UpdaterData::UpdaterData(QWidget *buttonParent) } LastCrashedWindow::LastCrashedWindow( - not_null launcher, const QByteArray &crashdump, Fn launch) : _dumpraw(crashdump) diff --git a/Telegram/SourceFiles/core/crash_report_window.h b/Telegram/SourceFiles/core/crash_report_window.h index 75e17ba23..5c20c26c8 100644 --- a/Telegram/SourceFiles/core/crash_report_window.h +++ b/Telegram/SourceFiles/core/crash_report_window.h @@ -94,10 +94,7 @@ private: class LastCrashedWindow : public PreLaunchWindow { public: - LastCrashedWindow( - not_null launcher, - const QByteArray &crashdump, - Fn launch); + LastCrashedWindow(const QByteArray &crashdump, Fn launch); rpl::producer proxyChanges() const; diff --git a/Telegram/SourceFiles/core/launcher.cpp b/Telegram/SourceFiles/core/launcher.cpp index e5cc426d0..c6127295b 100644 --- a/Telegram/SourceFiles/core/launcher.cpp +++ b/Telegram/SourceFiles/core/launcher.cpp @@ -281,6 +281,8 @@ base::options::toggle OptionFractionalScalingEnabled({ const char kOptionFractionalScalingEnabled[] = "fractional-scaling-enabled"; const char kOptionFreeType[] = "freetype"; +Launcher *Launcher::InstanceSetter::Instance = nullptr; + std::unique_ptr Launcher::Create(int argc, char *argv[]) { return std::make_unique(argc, argv); } @@ -294,6 +296,10 @@ Launcher::Launcher(int argc, char *argv[]) base::Integration::Set(&_baseIntegration); } +Launcher::~Launcher() { + InstanceSetter::Instance = nullptr; +} + void Launcher::init() { _arguments = readArguments(_argc, _argv); @@ -558,7 +564,7 @@ void Launcher::processArguments() { int Launcher::executeApplication() { FilteredCommandLineArguments arguments(_argc, _argv); - Sandbox sandbox(this, arguments.count(), arguments.values()); + Sandbox sandbox(arguments.count(), arguments.values()); Ui::MainQueueProcessor processor; base::ConcurrentTimerEnvironment environment; return sandbox.start(); diff --git a/Telegram/SourceFiles/core/launcher.h b/Telegram/SourceFiles/core/launcher.h index 047218951..4d1f17edc 100644 --- a/Telegram/SourceFiles/core/launcher.h +++ b/Telegram/SourceFiles/core/launcher.h @@ -20,6 +20,12 @@ public: static std::unique_ptr Create(int argc, char *argv[]); + static Launcher &Instance() { + Expects(InstanceSetter::Instance != nullptr); + + return *InstanceSetter::Instance; + } + virtual int exec(); QString argumentsString() const; @@ -32,7 +38,7 @@ public: void writeDebugModeSetting(); void writeInstallBetaVersionsSetting(); - virtual ~Launcher() = default; + virtual ~Launcher(); protected: enum class UpdaterLaunch { @@ -61,6 +67,17 @@ private: int executeApplication(); + struct InstanceSetter { + InstanceSetter(not_null instance) { + Expects(Instance == nullptr); + + Instance = instance; + } + + static Launcher *Instance; + }; + InstanceSetter _setter = { this }; + int _argc; char **_argv; QStringList _arguments; diff --git a/Telegram/SourceFiles/core/sandbox.cpp b/Telegram/SourceFiles/core/sandbox.cpp index 741f7d072..6c332bbc1 100644 --- a/Telegram/SourceFiles/core/sandbox.cpp +++ b/Telegram/SourceFiles/core/sandbox.cpp @@ -78,13 +78,9 @@ QString _escapeFrom7bit(const QString &str) { bool Sandbox::QuitOnStartRequested = false; -Sandbox::Sandbox( - not_null launcher, - int &argc, - char **argv) +Sandbox::Sandbox(int &argc, char **argv) : QApplication(argc, argv) -, _mainThreadId(QThread::currentThreadId()) -, _launcher(launcher) { +, _mainThreadId(QThread::currentThreadId()) { setQuitOnLastWindowClosed(false); } @@ -107,7 +103,8 @@ int Sandbox::start() { hashMd5Hex(d.constData(), d.size(), h.data()); _lockFile = std::make_unique(QDir::tempPath() + '/' + h + '-' + cGUIDStr()); _lockFile->setStaleLockTime(0); - if (!_lockFile->tryLock() && _launcher->customWorkingDir()) { + if (!_lockFile->tryLock() + && Launcher::Instance().customWorkingDir()) { // On Windows, QLockFile has problems detecting a stale lock // if the machine's hostname contains characters outside the US-ASCII character set. if constexpr (Platform::IsWindows()) { @@ -200,7 +197,7 @@ void Sandbox::launchApplication() { } setupScreenScale(); - _application = std::make_unique(_launcher); + _application = std::make_unique(); // Ideally this should go to constructor. // But we want to catch all native events and Application installs @@ -401,7 +398,6 @@ void Sandbox::singleInstanceChecked() { } _lastCrashDump = crashdump; auto window = new LastCrashedWindow( - _launcher, _lastCrashDump, [=] { launchApplication(); }); window->proxyChanges( @@ -529,14 +525,6 @@ void Sandbox::refreshGlobalProxy() { } } -bool Sandbox::customWorkingDir() const { - return _launcher->customWorkingDir(); -} - -uint64 Sandbox::installationTag() const { - return _launcher->installationTag(); -} - void Sandbox::checkForEmptyLoopNestingLevel() { // _loopNestingLevel == _eventNestingLevel means that we had a // native event in a nesting loop that didn't get a notify() call diff --git a/Telegram/SourceFiles/core/sandbox.h b/Telegram/SourceFiles/core/sandbox.h index bcf83982a..d8f47cd5a 100644 --- a/Telegram/SourceFiles/core/sandbox.h +++ b/Telegram/SourceFiles/core/sandbox.h @@ -19,7 +19,6 @@ class QLockFile; namespace Core { -class Launcher; class UpdateChecker; class Application; @@ -33,7 +32,7 @@ private: } public: - Sandbox(not_null launcher, int &argc, char **argv); + Sandbox(int &argc, char **argv); Sandbox(const Sandbox &other) = delete; Sandbox &operator=(const Sandbox &other) = delete; @@ -41,8 +40,6 @@ public: int start(); void refreshGlobalProxy(); - bool customWorkingDir() const; - uint64 installationTag() const; void postponeCall(FnMut &&callable); bool notify(QObject *receiver, QEvent *e) override; @@ -116,7 +113,6 @@ private: std::vector _previousLoopNestingLevels; std::vector _postponedCalls; - not_null _launcher; std::unique_ptr _application; QString _localServerName, _localSocketReadData; diff --git a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp index a2bc1c237..10f4e82b6 100644 --- a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp @@ -24,8 +24,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Platform { namespace { -Launcher *LauncherInstance = nullptr; - class Arguments { public: void push(QByteArray argument) { @@ -50,15 +48,6 @@ private: Launcher::Launcher(int argc, char *argv[]) : Core::Launcher(argc, argv) , _arguments(argv, argv + argc) { - Expects(LauncherInstance == nullptr); - - LauncherInstance = this; -} - -Launcher &Launcher::Instance() { - Expects(LauncherInstance != nullptr); - - return *LauncherInstance; } int Launcher::exec() { diff --git a/Telegram/SourceFiles/platform/linux/launcher_linux.h b/Telegram/SourceFiles/platform/linux/launcher_linux.h index 99d7259ee..36625a36a 100644 --- a/Telegram/SourceFiles/platform/linux/launcher_linux.h +++ b/Telegram/SourceFiles/platform/linux/launcher_linux.h @@ -15,8 +15,6 @@ class Launcher : public Core::Launcher { public: Launcher(int argc, char *argv[]); - static Launcher &Instance(); - int exec() override; private: diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index 817997802..1ab7a8d54 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -15,10 +15,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/platform/linux/base_linux_xdp_utilities.h" #include "platform/linux/linux_desktop_environment.h" #include "platform/linux/linux_wayland_integration.h" -#include "platform/platform_launcher.h" #include "lang/lang_keys.h" #include "mainwindow.h" #include "storage/localstorage.h" +#include "core/launcher.h" #include "core/sandbox.h" #include "core/application.h" #include "core/local_url_handlers.h" @@ -138,7 +138,7 @@ bool PortalAutostart(bool start, bool silent) { std::vector commandline; commandline.push_back(cExeName().toStdString()); - if (Core::Sandbox::Instance().customWorkingDir()) { + if (Core::Launcher::Instance().customWorkingDir()) { commandline.push_back("-workdir"); commandline.push_back(cWorkingDir().toStdString()); } @@ -403,7 +403,7 @@ bool GenerateDesktopFile( exec.append(!Core::UpdaterDisabled() ? (cExeDir() + cExeName()) : cExeName()); - if (Core::Sandbox::Instance().customWorkingDir()) { + if (Core::Launcher::Instance().customWorkingDir()) { exec.append(u"-workdir"_q); exec.append(cWorkingDir()); } @@ -426,7 +426,7 @@ bool GenerateDesktopFile( exec[0] = !Core::UpdaterDisabled() ? (cExeDir() + cExeName()) : cExeName(); - if (Core::Sandbox::Instance().customWorkingDir()) { + if (Core::Launcher::Instance().customWorkingDir()) { exec.insert(1, u"-workdir"_q); exec.insert(2, cWorkingDir()); } @@ -479,7 +479,7 @@ bool GenerateDesktopFile( const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath()); hashMd5Hex(d.constData(), d.size(), md5Hash); - if (!Core::Sandbox::Instance().customWorkingDir()) { + if (!Core::Launcher::Instance().customWorkingDir()) { const auto exePath = QFile::encodeName( cExeDir() + cExeName()); hashMd5Hex(exePath.constData(), exePath.size(), md5Hash); @@ -676,7 +676,7 @@ void start() { if (!Core::UpdaterDisabled()) { QByteArray md5Hash(h); - if (!Launcher::Instance().customWorkingDir()) { + if (!Core::Launcher::Instance().customWorkingDir()) { const auto exePath = QFile::encodeName( cExeDir() + cExeName()); @@ -726,6 +726,8 @@ void start() { h, cGUIDStr(), u"%1"_q).toStdString()); + + InstallLauncher(); } void finish() { @@ -805,7 +807,6 @@ void start() { "except various functionality to not to work."); } - InstallLauncher(); LaunchGApplication(); } diff --git a/Telegram/SourceFiles/settings/settings_advanced.cpp b/Telegram/SourceFiles/settings/settings_advanced.cpp index 0a105c158..0cd7c485f 100644 --- a/Telegram/SourceFiles/settings/settings_advanced.cpp +++ b/Telegram/SourceFiles/settings/settings_advanced.cpp @@ -32,6 +32,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_session_controller.h" #include "lang/lang_keys.h" #include "core/update_checker.h" +#include "core/launcher.h" #include "core/application.h" #include "tray.h" #include "storage/localstorage.h" @@ -224,7 +225,7 @@ void SetupUpdate( return (toggled != cInstallBetaVersion()); }) | rpl::start_with_next([=](bool toggled) { cSetInstallBetaVersion(toggled); - Core::App().writeInstallBetaVersionsSetting(); + Core::Launcher::Instance().writeInstallBetaVersionsSetting(); Core::UpdateChecker checker; checker.stop(); diff --git a/Telegram/SourceFiles/support/support_helper.cpp b/Telegram/SourceFiles/support/support_helper.cpp index 642f138c2..bb0888d13 100644 --- a/Telegram/SourceFiles/support/support_helper.cpp +++ b/Telegram/SourceFiles/support/support_helper.cpp @@ -28,7 +28,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_session_controller.h" #include "storage/storage_media_prepare.h" #include "storage/localimageloader.h" -#include "core/sandbox.h" +#include "core/launcher.h" #include "core/application.h" #include "core/core_settings.h" #include "main/main_session.h" @@ -138,7 +138,7 @@ void EditInfoBox::setInnerFocus() { } uint32 OccupationTag() { - return uint32(Core::Sandbox::Instance().installationTag() & 0xFFFFFFFF); + return uint32(Core::Launcher::Instance().installationTag() & 0xFFFFFFFF); } QString NormalizeName(QString name) { @@ -268,7 +268,7 @@ Helper::Helper(not_null session) }).fail([=] { setSupportName( u"[rand^"_q - + QString::number(Core::Sandbox::Instance().installationTag()) + + QString::number(Core::Launcher::Instance().installationTag()) + ']'); }).send(); }