-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathcommandui.cpp
More file actions
394 lines (328 loc) · 12.6 KB
/
Copy pathcommandui.cpp
File metadata and controls
394 lines (328 loc) · 12.6 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "commandui.h"
#include <lottie.h>
#include <nebula.h>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QWindow>
#include <memory>
#include "accessiblenotification.h"
#include "addons/manager/addonmanager.h"
#include "apppermission.h"
#include "commandlineparser.h"
#include "constants.h"
#include "controller.h"
#include "daemon/mock/mockdaemon.h"
#include "fontloader.h"
#include "i18nstrings.h"
#include "imageproviderfactory.h"
#include "inspector/inspectorhandler.h"
#include "leakdetector.h"
#include "logger.h"
#include "models/servercountrymodel.h"
#include "mozillavpn.h"
#include "networkmanager.h"
#include "notificationhandler.h"
#include "qmlengineholder.h"
#include "settingsholder.h"
#include "temporarydir.h"
#ifdef MZ_DEBUG
# include <QQmlDebuggingEnabler>
#endif
#ifdef MZ_LINUX
# include "eventlistener.h"
#endif
#ifdef MZ_MACOS
# include "platforms/macos/macosmenubar.h"
# include "platforms/macos/macosutils.h"
#endif
#ifdef MZ_ANDROID
# include <QQuickWindow>
# include <QSGRendererInterface>
# include "platforms/android/androidcommons.h"
# include "platforms/android/androidutils.h"
# include "platforms/android/androidvpnactivity.h"
#endif
#ifdef MVPN_WEBEXTENSION
# include "webextension/server.h"
# include "webextensionadapter.h"
#endif
#ifdef MZ_WINDOWS
# include <windows.h>
# include <QQuickWindow>
# include <QSGRendererInterface>
# include "eventlistener.h"
# include "platforms/windows/windowscommons.h"
# include "platforms/windows/windowsutils.h"
# include "theme.h"
#endif
#ifdef MZ_WASM
# include "platforms/wasm/wasmwindowcontroller.h"
#endif
#include <QtQml/qqmlextensionplugin.h>
#include <QApplication>
Q_IMPORT_QML_PLUGIN(Mozilla_VPNPlugin);
namespace {
Logger logger("CommandUI");
}
CommandUI::CommandUI(QObject* parent) : Command(parent, "ui", "Start the UI.") {
MZ_COUNT_CTOR(CommandUI);
}
CommandUI::~CommandUI() { MZ_COUNT_DTOR(CommandUI); }
int CommandUI::run(QStringList& tokens) {
Q_ASSERT(!tokens.isEmpty());
QString appName = tokens[0];
CommandLineParser::Option hOption = CommandLineParser::helpOption();
CommandLineParser::Option minimizedOption("m", "minimized",
"Start minimized.");
CommandLineParser::Option startAtBootOption("s", "start-at-boot",
"Start at boot (if configured).");
CommandLineParser::Option testingOption("t", "testing",
"Enable testing mode.");
CommandLineParser::Option updateOption(
"u", "updated", "This execution completes an update flow.");
QList<CommandLineParser::Option*> options;
options.append(&hOption);
options.append(&minimizedOption);
options.append(&startAtBootOption);
options.append(&testingOption);
options.append(&updateOption);
CommandLineParser clp;
if (clp.parse(tokens, options, false)) {
return 1;
}
if (hOption.m_set) {
clp.showHelp(this, appName, options, false, false);
return 0;
}
// Change the application organization for testing mode.
// This should ensure we wind up with a different settings file.
if (testingOption.m_set) {
QCoreApplication::setOrganizationName("Mozilla Testing");
LogHandler::instance()->setStderr(true);
}
// If there is another instance, the execution terminates here.
#if defined(MZ_WINDOWS) || defined(MZ_LINUX)
if (EventListener::checkForInstances()) {
QTextStream stream(stderr);
stream << "Existing instance found" << Qt::endl;
// If we are given URL parameters, send them to the UI socket and exit.
for (const QString& value : tokens) {
QUrl url(value);
if (!url.isValid() || (url.scheme() != Constants::DEEP_LINK_SCHEME)) {
stream << "Invalid link:" << value << Qt::endl;
} else {
stream << "Sending link" << Qt::endl;
EventListener::sendDeepLink(url);
}
}
return 0;
}
// This class receives communications from other instances.
std::unique_ptr<EventListener> eventListener;
#endif
#ifdef MZ_ANDROID
// Configure graphics rendering for Android
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
Qt::HighDpiScaleFactorRoundingPolicy::Round);
// Skip the workaround on Qt 6.10+
// if the underlying ChromeOS rendering bug
// resurfaces, re-evaluate after upgrading further.
# if QT_VERSION < QT_VERSION_CHECK(6, 10, 0)
if (AndroidUtils::isChromeOSContext()) {
QQuickWindow::setGraphicsApi(QSGRendererInterface::Software);
}
# endif
#endif
#ifdef MZ_WINDOWS
// Configure graphics rendering for Windows
SetProcessDPIAware();
if (WindowsCommons::requireSoftwareRendering()) {
QQuickWindow::setGraphicsApi(QSGRendererInterface::Software);
}
#endif
// Ensure that external styling hints are disabled.
qunsetenv("QT_STYLE_OVERRIDE");
return MozillaVPN::runGuiApp([&]() {
// Already intiziaized: VPN & qAPP.
// Note: this lambda will exit, so don't use it's scope for long-living
// objects.
auto const vpn = MozillaVPN::instance();
Q_ASSERT(vpn);
if (testingOption.m_set) {
Constants::setStaging();
// Provide a mocked AppListProvider for testing.
AppPermission::mock();
// When running automated tests, create a mocked daemon.
MockDaemon* daemon = new MockDaemon(qApp);
qputenv("MVPN_CONTROL_SOCKET", daemon->socketPath().toLocal8Bit());
}
logger.debug() << "UI starting";
if (startAtBootOption.m_set || qgetenv("MVPN_STARTATBOOT") == "1") {
logger.debug() << "Maybe start at boot";
if (!SettingsHolder::instance()->startAtBoot()) {
logger.debug() << "We don't need to start at boot.";
return 0;
}
if (SettingsHolder::instance()->token().isEmpty()) {
logger.debug() << "The user is not logged in.";
return 0;
}
}
#if defined(MZ_WINDOWS) || defined(MZ_LINUX)
eventListener.reset(new EventListener{});
#endif
#ifdef MZ_ANDROID
// https://bugreports.qt.io/browse/QTBUG-82617
// Currently there is a crash happening on exit with Huawei devices.
// Until this is fixed, setting this variable is the "official" workaround.
// We certainly should look at this once 6.11 is out.
# if QT_VERSION >= QT_VERSION_CHECK(6, 11, 0)
# error We have forgotten to remove this Huawei hack!
# endif
if (AndroidCommons::GetManufacturer() == "Huawei") {
qputenv("QT_ANDROID_NO_EXIT_CALL", "1");
}
#endif
auto const engineHolder = QmlEngineHolder::instance();
auto const engine =
static_cast<QQmlApplicationEngine*>(engineHolder->engine());
Q_ASSERT(engine);
Lottie::initialize(engine, QString(NetworkManager::userAgent()));
Nebula::Initialize(engine);
// Cleanup previous temporary files.
TemporaryDir::cleanupAll();
vpn->setStartMinimized(minimizedOption.m_set ||
(qgetenv("MVPN_MINIMIZED") == "1"));
// Font loader
FontLoader::loadFonts();
vpn->initialize();
logger.debug() << "VPN initialized";
QQuickImageProvider* provider = ImageProviderFactory::create(qApp);
if (provider) {
engine->addImageProvider(QString("app"), provider);
}
logger.debug() << "Added QML image provider";
qmlRegisterSingletonInstance("Mozilla.Shared", 1, 0,
"MZAccessibleNotification",
AccessibleNotification::instance());
// TODO: MZI18n should be moved to QmlEngineHolder but it requires extra
// work for the generation of i18nstrings.h/cpp for the unit-test app.
qmlRegisterSingletonInstance("Mozilla.Shared", 1, 0, "MZI18n",
I18nStrings::instance());
logger.debug() << "Registered I18nStrings";
QObject::connect(qApp, &QCoreApplication::aboutToQuit, vpn,
[] { MozillaVPN::instance()->aboutToQuit(); });
QObject::connect(
qApp, &QGuiApplication::commitDataRequest, vpn,
[]() { MozillaVPN::instance()->deactivate(true); },
Qt::DirectConnection);
QObject::connect(vpn->controller(), &Controller::readyToQuit, vpn,
&MozillaVPN::quit, Qt::QueuedConnection);
#ifdef MZ_ANDROID
// On Android we need to make sure when we load a QML application that the
// context is set. Qt ___should___ do this for us, but it seems that in some
// cases it does not. So we chant dark JNI magic to make sure the
// VPNActivity is set as context provider.
logger.debug() << "Forcing activity publish";
AndroidCommons::forcePublishActivity();
// In case we have a pending exception, clear it before loading main.qml
AndroidCommons::clearPendingJavaException("before main.qml load");
#endif
const QUrl url(QStringLiteral("qrc:/qt/qml/Mozilla/VPN/main.qml"));
logger.debug() << "Loading main QML file:" << url.toString();
engine->load(url);
logger.debug() << "Loaded main QML file";
if (!engineHolder->hasWindow()) {
logger.error() << "Failed to load " << url.toString();
}
#ifdef MZ_ANDROID
AndroidCommons::dismissSplashScreen();
#endif
#ifdef MZ_WINDOWS
auto const updateWindowDecoration = [engineHolder]() {
auto const window = engineHolder->window();
WindowsUtils::updateTitleBarColor(window,
Theme::instance()->isThemeDark());
WindowsUtils::setDockIcon(window, QImage(":/ui/resources/logo-dock.png"));
WindowsUtils::setTitleBarIcon(window,
Theme::instance()->getTitleBarIcon());
};
QObject::connect(Theme::instance(), &Theme::changed,
updateWindowDecoration);
updateWindowDecoration();
#endif
#ifdef MZ_MACOS
MacOSMenuBar menuBar;
menuBar.initialize();
QObject::connect(vpn, &MozillaVPN::stateChanged, &menuBar,
&MacOSMenuBar::controllerStateChanged);
QObject::connect(vpn->controller(), &Controller::stateChanged, &menuBar,
&MacOSMenuBar::controllerStateChanged);
#endif
#ifdef MZ_WASM
WasmWindowController wasmWindowController;
QObject::connect(SettingsHolder::instance(),
&SettingsHolder::languageCodeChanged,
&wasmWindowController, &WasmWindowController::retranslate);
#endif
NotificationHandler* notificationHandler =
NotificationHandler::create(qApp);
QObject::connect(vpn->controller(), &Controller::stateChanged,
notificationHandler,
&NotificationHandler::showNotification);
QObject::connect(
SettingsHolder::instance(), &SettingsHolder::languageCodeChanged, []() {
logger.debug() << "Retranslating";
QmlEngineHolder::instance()->engine()->retranslate();
NotificationHandler::instance()->retranslate();
I18nStrings::instance()->retranslate();
AddonManager::instance()->retranslate();
#ifdef MZ_MACOS
if (auto* menuBar = MacOSMenuBar::instance()) {
menuBar->retranslate();
}
#endif
MozillaVPN::instance()->serverCountryModel()->retranslate();
MozillaVPN::instance()->serverData()->retranslate();
});
InspectorHandler::initialize();
logger.debug() << "Inspector Handler initialized";
#ifdef MVPN_WEBEXTENSION
QPointer webExtensionServer =
new WebExtension::Server{new WebExtensionAdapter(qApp)};
QObject::connect(vpn->controller(), &Controller::readyToQuit,
webExtensionServer.get(), &WebExtension::Server::close);
webExtensionServer.clear();
#endif
#ifdef MZ_ANDROID
// If we are created with an url intent, auto pass that.
QUrl maybeURL = AndroidVPNActivity::getOpenerURL();
if (!maybeURL.isValid()) {
logger.error() << "Error in deep-link:" << maybeURL.toString();
} else {
vpn->handleDeepLink(maybeURL);
}
// Whenever the client is re-opened with a new url pass it to the handler.
connect(AndroidVPNActivity::instance(),
&AndroidVPNActivity::onOpenedWithUrl, vpn,
&MozillaVPN::handleDeepLink);
#else
// If there happen to be navigation URLs, handle them.
for (const QString& value : tokens) {
QUrl url(value);
if (!url.isValid() || (url.scheme() != Constants::DEEP_LINK_SCHEME)) {
logger.error() << "Invalid link:" << value;
} else {
vpn->handleDeepLink(url);
}
}
#endif
// We're ready to continue!
logger.debug() << "CommandUI finished";
return 0;
});
}
static Command::RegistrationProxy<CommandUI> s_commandUI;