Have a global Core::Launcher accessor

This allows to use Launcher in code executing before Sandbox is created
This commit is contained in:
Ilya Fedin 2023-06-13 04:34:27 +04:00 committed by John Preston
parent 63ccf1bed4
commit a7bd097b2f
13 changed files with 50 additions and 70 deletions

View file

@ -143,9 +143,8 @@ struct Application::Private {
Settings settings; Settings settings;
}; };
Application::Application(not_null<Launcher*> launcher) Application::Application()
: QObject() : QObject()
, _launcher(launcher)
, _private(std::make_unique<Private>()) , _private(std::make_unique<Private>())
, _platformIntegration(Platform::Integration::Create()) , _platformIntegration(Platform::Integration::Create())
, _batterySaving(std::make_unique<base::BatterySaving>()) , _batterySaving(std::make_unique<base::BatterySaving>())
@ -945,11 +944,11 @@ rpl::producer<> Application::materializeLocalDraftsRequests() const {
void Application::switchDebugMode() { void Application::switchDebugMode() {
if (Logs::DebugEnabled()) { if (Logs::DebugEnabled()) {
Logs::SetDebugEnabled(false); Logs::SetDebugEnabled(false);
_launcher->writeDebugModeSetting(); Launcher::Instance().writeDebugModeSetting();
Restart(); Restart();
} else { } else {
Logs::SetDebugEnabled(true); Logs::SetDebugEnabled(true);
_launcher->writeDebugModeSetting(); Launcher::Instance().writeDebugModeSetting();
DEBUG_LOG(("Debug logs started.")); DEBUG_LOG(("Debug logs started."));
if (_lastActivePrimaryWindow) { if (_lastActivePrimaryWindow) {
_lastActivePrimaryWindow->hideLayer(); _lastActivePrimaryWindow->hideLayer();
@ -957,10 +956,6 @@ void Application::switchDebugMode() {
} }
} }
void Application::writeInstallBetaVersionsSetting() {
_launcher->writeInstallBetaVersionsSetting();
}
Main::Account &Application::activeAccount() const { Main::Account &Application::activeAccount() const {
return _domain->active(); return _domain->active();
} }
@ -1770,7 +1765,7 @@ void Application::RegisterUrlScheme() {
.executable = (!Platform::IsLinux() || !Core::UpdaterDisabled()) .executable = (!Platform::IsLinux() || !Core::UpdaterDisabled())
? (cExeDir() + cExeName()) ? (cExeDir() + cExeName())
: cExeName(), : cExeName(),
.arguments = Sandbox::Instance().customWorkingDir() .arguments = Launcher::Instance().customWorkingDir()
? u"-workdir \"%1\""_q.arg(cWorkingDir()) ? u"-workdir \"%1\""_q.arg(cWorkingDir())
: QString(), : QString(),
.protocol = u"tg"_q, .protocol = u"tg"_q,

View file

@ -103,7 +103,6 @@ class Instance;
namespace Core { namespace Core {
class Launcher;
struct LocalUrlHandler; struct LocalUrlHandler;
class Settings; class Settings;
class Tray; class Tray;
@ -126,16 +125,13 @@ public:
MTP::ProxyData now; MTP::ProxyData now;
}; };
Application(not_null<Launcher*> launcher); Application();
Application(const Application &other) = delete; Application(const Application &other) = delete;
Application &operator=(const Application &other) = delete; Application &operator=(const Application &other) = delete;
~Application(); ~Application();
void run(); void run();
[[nodiscard]] Launcher &launcher() const {
return *_launcher;
}
[[nodiscard]] Platform::Integration &platformIntegration() const { [[nodiscard]] Platform::Integration &platformIntegration() const {
return *_platformIntegration; return *_platformIntegration;
} }
@ -319,7 +315,6 @@ public:
[[nodiscard]] rpl::producer<> materializeLocalDraftsRequests() const; [[nodiscard]] rpl::producer<> materializeLocalDraftsRequests() const;
void switchDebugMode(); void switchDebugMode();
void writeInstallBetaVersionsSetting();
void preventOrInvoke(Fn<void()> &&callback); void preventOrInvoke(Fn<void()> &&callback);
@ -381,7 +376,6 @@ private:
}; };
InstanceSetter _setter = { this }; InstanceSetter _setter = { this };
const not_null<Launcher*> _launcher;
rpl::event_stream<ProxyChange> _proxyChanges; rpl::event_stream<ProxyChange> _proxyChanges;
// Some fields are just moved from the declaration. // Some fields are just moved from the declaration.

View file

@ -9,7 +9,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "core/crash_reports.h" #include "core/crash_reports.h"
#include "core/application.h" #include "core/application.h"
#include "core/launcher.h"
#include "core/sandbox.h" #include "core/sandbox.h"
#include "core/update_checker.h" #include "core/update_checker.h"
#include "core/ui_integration.h" #include "core/ui_integration.h"
@ -250,7 +249,6 @@ LastCrashedWindow::UpdaterData::UpdaterData(QWidget *buttonParent)
} }
LastCrashedWindow::LastCrashedWindow( LastCrashedWindow::LastCrashedWindow(
not_null<Core::Launcher*> launcher,
const QByteArray &crashdump, const QByteArray &crashdump,
Fn<void()> launch) Fn<void()> launch)
: _dumpraw(crashdump) : _dumpraw(crashdump)

View file

@ -94,10 +94,7 @@ private:
class LastCrashedWindow : public PreLaunchWindow { class LastCrashedWindow : public PreLaunchWindow {
public: public:
LastCrashedWindow( LastCrashedWindow(const QByteArray &crashdump, Fn<void()> launch);
not_null<Core::Launcher*> launcher,
const QByteArray &crashdump,
Fn<void()> launch);
rpl::producer<MTP::ProxyData> proxyChanges() const; rpl::producer<MTP::ProxyData> proxyChanges() const;

View file

@ -281,6 +281,8 @@ base::options::toggle OptionFractionalScalingEnabled({
const char kOptionFractionalScalingEnabled[] = "fractional-scaling-enabled"; const char kOptionFractionalScalingEnabled[] = "fractional-scaling-enabled";
const char kOptionFreeType[] = "freetype"; const char kOptionFreeType[] = "freetype";
Launcher *Launcher::InstanceSetter::Instance = nullptr;
std::unique_ptr<Launcher> Launcher::Create(int argc, char *argv[]) { std::unique_ptr<Launcher> Launcher::Create(int argc, char *argv[]) {
return std::make_unique<Platform::Launcher>(argc, argv); return std::make_unique<Platform::Launcher>(argc, argv);
} }
@ -294,6 +296,10 @@ Launcher::Launcher(int argc, char *argv[])
base::Integration::Set(&_baseIntegration); base::Integration::Set(&_baseIntegration);
} }
Launcher::~Launcher() {
InstanceSetter::Instance = nullptr;
}
void Launcher::init() { void Launcher::init() {
_arguments = readArguments(_argc, _argv); _arguments = readArguments(_argc, _argv);
@ -558,7 +564,7 @@ void Launcher::processArguments() {
int Launcher::executeApplication() { int Launcher::executeApplication() {
FilteredCommandLineArguments arguments(_argc, _argv); FilteredCommandLineArguments arguments(_argc, _argv);
Sandbox sandbox(this, arguments.count(), arguments.values()); Sandbox sandbox(arguments.count(), arguments.values());
Ui::MainQueueProcessor processor; Ui::MainQueueProcessor processor;
base::ConcurrentTimerEnvironment environment; base::ConcurrentTimerEnvironment environment;
return sandbox.start(); return sandbox.start();

View file

@ -20,6 +20,12 @@ public:
static std::unique_ptr<Launcher> Create(int argc, char *argv[]); static std::unique_ptr<Launcher> Create(int argc, char *argv[]);
static Launcher &Instance() {
Expects(InstanceSetter::Instance != nullptr);
return *InstanceSetter::Instance;
}
virtual int exec(); virtual int exec();
QString argumentsString() const; QString argumentsString() const;
@ -32,7 +38,7 @@ public:
void writeDebugModeSetting(); void writeDebugModeSetting();
void writeInstallBetaVersionsSetting(); void writeInstallBetaVersionsSetting();
virtual ~Launcher() = default; virtual ~Launcher();
protected: protected:
enum class UpdaterLaunch { enum class UpdaterLaunch {
@ -61,6 +67,17 @@ private:
int executeApplication(); int executeApplication();
struct InstanceSetter {
InstanceSetter(not_null<Launcher*> instance) {
Expects(Instance == nullptr);
Instance = instance;
}
static Launcher *Instance;
};
InstanceSetter _setter = { this };
int _argc; int _argc;
char **_argv; char **_argv;
QStringList _arguments; QStringList _arguments;

View file

@ -78,13 +78,9 @@ QString _escapeFrom7bit(const QString &str) {
bool Sandbox::QuitOnStartRequested = false; bool Sandbox::QuitOnStartRequested = false;
Sandbox::Sandbox( Sandbox::Sandbox(int &argc, char **argv)
not_null<Core::Launcher*> launcher,
int &argc,
char **argv)
: QApplication(argc, argv) : QApplication(argc, argv)
, _mainThreadId(QThread::currentThreadId()) , _mainThreadId(QThread::currentThreadId()) {
, _launcher(launcher) {
setQuitOnLastWindowClosed(false); setQuitOnLastWindowClosed(false);
} }
@ -107,7 +103,8 @@ int Sandbox::start() {
hashMd5Hex(d.constData(), d.size(), h.data()); hashMd5Hex(d.constData(), d.size(), h.data());
_lockFile = std::make_unique<QLockFile>(QDir::tempPath() + '/' + h + '-' + cGUIDStr()); _lockFile = std::make_unique<QLockFile>(QDir::tempPath() + '/' + h + '-' + cGUIDStr());
_lockFile->setStaleLockTime(0); _lockFile->setStaleLockTime(0);
if (!_lockFile->tryLock() && _launcher->customWorkingDir()) { if (!_lockFile->tryLock()
&& Launcher::Instance().customWorkingDir()) {
// On Windows, QLockFile has problems detecting a stale lock // On Windows, QLockFile has problems detecting a stale lock
// if the machine's hostname contains characters outside the US-ASCII character set. // if the machine's hostname contains characters outside the US-ASCII character set.
if constexpr (Platform::IsWindows()) { if constexpr (Platform::IsWindows()) {
@ -200,7 +197,7 @@ void Sandbox::launchApplication() {
} }
setupScreenScale(); setupScreenScale();
_application = std::make_unique<Application>(_launcher); _application = std::make_unique<Application>();
// Ideally this should go to constructor. // Ideally this should go to constructor.
// But we want to catch all native events and Application installs // But we want to catch all native events and Application installs
@ -401,7 +398,6 @@ void Sandbox::singleInstanceChecked() {
} }
_lastCrashDump = crashdump; _lastCrashDump = crashdump;
auto window = new LastCrashedWindow( auto window = new LastCrashedWindow(
_launcher,
_lastCrashDump, _lastCrashDump,
[=] { launchApplication(); }); [=] { launchApplication(); });
window->proxyChanges( 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() { void Sandbox::checkForEmptyLoopNestingLevel() {
// _loopNestingLevel == _eventNestingLevel means that we had a // _loopNestingLevel == _eventNestingLevel means that we had a
// native event in a nesting loop that didn't get a notify() call // native event in a nesting loop that didn't get a notify() call

View file

@ -19,7 +19,6 @@ class QLockFile;
namespace Core { namespace Core {
class Launcher;
class UpdateChecker; class UpdateChecker;
class Application; class Application;
@ -33,7 +32,7 @@ private:
} }
public: public:
Sandbox(not_null<Launcher*> launcher, int &argc, char **argv); Sandbox(int &argc, char **argv);
Sandbox(const Sandbox &other) = delete; Sandbox(const Sandbox &other) = delete;
Sandbox &operator=(const Sandbox &other) = delete; Sandbox &operator=(const Sandbox &other) = delete;
@ -41,8 +40,6 @@ public:
int start(); int start();
void refreshGlobalProxy(); void refreshGlobalProxy();
bool customWorkingDir() const;
uint64 installationTag() const;
void postponeCall(FnMut<void()> &&callable); void postponeCall(FnMut<void()> &&callable);
bool notify(QObject *receiver, QEvent *e) override; bool notify(QObject *receiver, QEvent *e) override;
@ -116,7 +113,6 @@ private:
std::vector<int> _previousLoopNestingLevels; std::vector<int> _previousLoopNestingLevels;
std::vector<PostponedCall> _postponedCalls; std::vector<PostponedCall> _postponedCalls;
not_null<Launcher*> _launcher;
std::unique_ptr<Application> _application; std::unique_ptr<Application> _application;
QString _localServerName, _localSocketReadData; QString _localServerName, _localSocketReadData;

View file

@ -24,8 +24,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Platform { namespace Platform {
namespace { namespace {
Launcher *LauncherInstance = nullptr;
class Arguments { class Arguments {
public: public:
void push(QByteArray argument) { void push(QByteArray argument) {
@ -50,15 +48,6 @@ private:
Launcher::Launcher(int argc, char *argv[]) Launcher::Launcher(int argc, char *argv[])
: Core::Launcher(argc, argv) : Core::Launcher(argc, argv)
, _arguments(argv, argv + argc) { , _arguments(argv, argv + argc) {
Expects(LauncherInstance == nullptr);
LauncherInstance = this;
}
Launcher &Launcher::Instance() {
Expects(LauncherInstance != nullptr);
return *LauncherInstance;
} }
int Launcher::exec() { int Launcher::exec() {

View file

@ -15,8 +15,6 @@ class Launcher : public Core::Launcher {
public: public:
Launcher(int argc, char *argv[]); Launcher(int argc, char *argv[]);
static Launcher &Instance();
int exec() override; int exec() override;
private: private:

View file

@ -15,10 +15,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/platform/linux/base_linux_xdp_utilities.h" #include "base/platform/linux/base_linux_xdp_utilities.h"
#include "platform/linux/linux_desktop_environment.h" #include "platform/linux/linux_desktop_environment.h"
#include "platform/linux/linux_wayland_integration.h" #include "platform/linux/linux_wayland_integration.h"
#include "platform/platform_launcher.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "storage/localstorage.h" #include "storage/localstorage.h"
#include "core/launcher.h"
#include "core/sandbox.h" #include "core/sandbox.h"
#include "core/application.h" #include "core/application.h"
#include "core/local_url_handlers.h" #include "core/local_url_handlers.h"
@ -138,7 +138,7 @@ bool PortalAutostart(bool start, bool silent) {
std::vector<Glib::ustring> commandline; std::vector<Glib::ustring> commandline;
commandline.push_back(cExeName().toStdString()); commandline.push_back(cExeName().toStdString());
if (Core::Sandbox::Instance().customWorkingDir()) { if (Core::Launcher::Instance().customWorkingDir()) {
commandline.push_back("-workdir"); commandline.push_back("-workdir");
commandline.push_back(cWorkingDir().toStdString()); commandline.push_back(cWorkingDir().toStdString());
} }
@ -403,7 +403,7 @@ bool GenerateDesktopFile(
exec.append(!Core::UpdaterDisabled() exec.append(!Core::UpdaterDisabled()
? (cExeDir() + cExeName()) ? (cExeDir() + cExeName())
: cExeName()); : cExeName());
if (Core::Sandbox::Instance().customWorkingDir()) { if (Core::Launcher::Instance().customWorkingDir()) {
exec.append(u"-workdir"_q); exec.append(u"-workdir"_q);
exec.append(cWorkingDir()); exec.append(cWorkingDir());
} }
@ -426,7 +426,7 @@ bool GenerateDesktopFile(
exec[0] = !Core::UpdaterDisabled() exec[0] = !Core::UpdaterDisabled()
? (cExeDir() + cExeName()) ? (cExeDir() + cExeName())
: cExeName(); : cExeName();
if (Core::Sandbox::Instance().customWorkingDir()) { if (Core::Launcher::Instance().customWorkingDir()) {
exec.insert(1, u"-workdir"_q); exec.insert(1, u"-workdir"_q);
exec.insert(2, cWorkingDir()); exec.insert(2, cWorkingDir());
} }
@ -479,7 +479,7 @@ bool GenerateDesktopFile(
const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath()); const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath());
hashMd5Hex(d.constData(), d.size(), md5Hash); hashMd5Hex(d.constData(), d.size(), md5Hash);
if (!Core::Sandbox::Instance().customWorkingDir()) { if (!Core::Launcher::Instance().customWorkingDir()) {
const auto exePath = QFile::encodeName( const auto exePath = QFile::encodeName(
cExeDir() + cExeName()); cExeDir() + cExeName());
hashMd5Hex(exePath.constData(), exePath.size(), md5Hash); hashMd5Hex(exePath.constData(), exePath.size(), md5Hash);
@ -676,7 +676,7 @@ void start() {
if (!Core::UpdaterDisabled()) { if (!Core::UpdaterDisabled()) {
QByteArray md5Hash(h); QByteArray md5Hash(h);
if (!Launcher::Instance().customWorkingDir()) { if (!Core::Launcher::Instance().customWorkingDir()) {
const auto exePath = QFile::encodeName( const auto exePath = QFile::encodeName(
cExeDir() + cExeName()); cExeDir() + cExeName());
@ -726,6 +726,8 @@ void start() {
h, h,
cGUIDStr(), cGUIDStr(),
u"%1"_q).toStdString()); u"%1"_q).toStdString());
InstallLauncher();
} }
void finish() { void finish() {
@ -805,7 +807,6 @@ void start() {
"except various functionality to not to work."); "except various functionality to not to work.");
} }
InstallLauncher();
LaunchGApplication(); LaunchGApplication();
} }

View file

@ -32,6 +32,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "window/window_session_controller.h" #include "window/window_session_controller.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "core/update_checker.h" #include "core/update_checker.h"
#include "core/launcher.h"
#include "core/application.h" #include "core/application.h"
#include "tray.h" #include "tray.h"
#include "storage/localstorage.h" #include "storage/localstorage.h"
@ -224,7 +225,7 @@ void SetupUpdate(
return (toggled != cInstallBetaVersion()); return (toggled != cInstallBetaVersion());
}) | rpl::start_with_next([=](bool toggled) { }) | rpl::start_with_next([=](bool toggled) {
cSetInstallBetaVersion(toggled); cSetInstallBetaVersion(toggled);
Core::App().writeInstallBetaVersionsSetting(); Core::Launcher::Instance().writeInstallBetaVersionsSetting();
Core::UpdateChecker checker; Core::UpdateChecker checker;
checker.stop(); checker.stop();

View file

@ -28,7 +28,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "window/window_session_controller.h" #include "window/window_session_controller.h"
#include "storage/storage_media_prepare.h" #include "storage/storage_media_prepare.h"
#include "storage/localimageloader.h" #include "storage/localimageloader.h"
#include "core/sandbox.h" #include "core/launcher.h"
#include "core/application.h" #include "core/application.h"
#include "core/core_settings.h" #include "core/core_settings.h"
#include "main/main_session.h" #include "main/main_session.h"
@ -138,7 +138,7 @@ void EditInfoBox::setInnerFocus() {
} }
uint32 OccupationTag() { uint32 OccupationTag() {
return uint32(Core::Sandbox::Instance().installationTag() & 0xFFFFFFFF); return uint32(Core::Launcher::Instance().installationTag() & 0xFFFFFFFF);
} }
QString NormalizeName(QString name) { QString NormalizeName(QString name) {
@ -268,7 +268,7 @@ Helper::Helper(not_null<Main::Session*> session)
}).fail([=] { }).fail([=] {
setSupportName( setSupportName(
u"[rand^"_q u"[rand^"_q
+ QString::number(Core::Sandbox::Instance().installationTag()) + QString::number(Core::Launcher::Instance().installationTag())
+ ']'); + ']');
}).send(); }).send();
} }