From e165855e95c5ba6a0fa89864766d95445b071029 Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Wed, 23 Jun 2021 15:38:21 -0400
Subject: [PATCH] misc: use single process mode for qt webEngine

1. Add --single-process
2. Rename the httpUserAgent to jami-qt

Change-Id: I6de29ee89176b453f12ce225acf926dd1b819c18
---
 src/constant/JamiStrings.qml               |  4 ++-
 src/main.cpp                               | 29 ++++++++++++++++------
 src/mainapplication.cpp                    |  8 +++++-
 src/mainview/components/MessageWebView.qml | 21 ++++++----------
 4 files changed, 39 insertions(+), 23 deletions(-)

diff --git a/src/constant/JamiStrings.qml b/src/constant/JamiStrings.qml
index 5a98bf3a3..0c9acd039 100644
--- a/src/constant/JamiStrings.qml
+++ b/src/constant/JamiStrings.qml
@@ -24,7 +24,9 @@ import QtQuick 2.14
 import net.jami.Helpers 1.0
 
 Item {
-    readonly property string appTitle: "Jami" + (UpdateManager.isCurrentVersionBeta() ? " (BETA)" : "")
+    readonly property string appTitle: "Jami" + (UpdateManager.isCurrentVersionBeta()
+                                                 ? " (BETA)" : "")
+    readonly property string httpUserAgentName: "jami-qt"
 
     // Misc
     property string accept: qsTr("Accept")
diff --git a/src/main.cpp b/src/main.cpp
index f0e3cd973..c08f72b85 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -31,26 +31,36 @@
 #ifndef ENABLE_TESTS
 
 static char**
-parseInputArgument(int& argc, char* argv[], char* argToParse)
+parseInputArgument(int& argc, char* argv[], QList<char*> argsToParse)
 {
     /*
-     * Forcefully append argToParse.
+     * Forcefully append argsToParse.
      */
     int oldArgc = argc;
-    argc = argc + 1 + 1;
+    argc += argsToParse.size();
     char** newArgv = new char*[argc];
     for (int i = 0; i < oldArgc; i++) {
         newArgv[i] = argv[i];
     }
-    newArgv[oldArgc] = argToParse;
-    newArgv[oldArgc + 1] = nullptr;
+
+    for (int i = oldArgc; i < argc; i++) {
+        newArgv[i] = argsToParse.at(i - oldArgc);
+    }
     return newArgv;
 }
 
+// Qt WebEngine Chromium Flags
+static char noSandbox[] {"--no-sandbox"};
+static char disableWebSecurity[] {"--disable-web-security"};
+static char singleProcess[] {"--single-process"};
+
 int
 main(int argc, char* argv[])
 {
     setlocale(LC_ALL, "en_US.utf8");
+
+    QList<char*> qtWebEngineChromiumFlags;
+
 #ifdef Q_OS_LINUX
     setenv("QT_QPA_PLATFORMTHEME", "gtk3", true);
 #ifdef __GLIBC__
@@ -59,9 +69,12 @@ main(int argc, char* argv[])
     // As I prefer to not use custom patched Qt, just wait for a
     // new version with this bug fixed
     if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 33))
-        setenv("QTWEBENGINE_CHROMIUM_FLAGS", "--no-sandbox", true);
+        qtWebEngineChromiumFlags << noSandbox;
 #endif
 #endif
+    qtWebEngineChromiumFlags << disableWebSecurity;
+    qtWebEngineChromiumFlags << singleProcess;
+
     QApplication::setApplicationName("Jami");
     QApplication::setOrganizationDomain("jami.net");
     QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
@@ -75,8 +88,8 @@ main(int argc, char* argv[])
 #endif
     QtWebEngine::initialize();
 
-    char ARG_DISABLE_WEB_SECURITY[] = "--disable-web-security";
-    auto newArgv = parseInputArgument(argc, argv, ARG_DISABLE_WEB_SECURITY);
+    auto newArgv = parseInputArgument(argc, argv, qtWebEngineChromiumFlags);
+
     MainApplication app(argc, newArgv);
 
     /*
diff --git a/src/mainapplication.cpp b/src/mainapplication.cpp
index a2049b12a..7426fc4e9 100644
--- a/src/mainapplication.cpp
+++ b/src/mainapplication.cpp
@@ -365,10 +365,16 @@ MainApplication::parseArguments()
     parser.addHelpOption();
     parser.addVersionOption();
 
-    // This option is forced into the arg list.
+    // These options are potentially forced into the arg list.
     QCommandLineOption webSecurityDisableOption(QStringList() << "disable-web-security");
     parser.addOption(webSecurityDisableOption);
 
+    QCommandLineOption noSandboxOption(QStringList() << "no-sandbox");
+    parser.addOption(noSandboxOption);
+
+    QCommandLineOption singleProcessOption(QStringList() << "single-process");
+    parser.addOption(singleProcessOption);
+
     QCommandLineOption webDebugOption(QStringList() << "remote-debugging-port",
                                       "Web debugging port.",
                                       "port");
diff --git a/src/mainview/components/MessageWebView.qml b/src/mainview/components/MessageWebView.qml
index 767710b09..ab653969a 100644
--- a/src/mainview/components/MessageWebView.qml
+++ b/src/mainview/components/MessageWebView.qml
@@ -287,7 +287,6 @@ Rectangle {
         settings.localStorageEnabled: true
 
         webChannel: messageWebViewChannel
-        profile: messageWebViewProfile
 
         DropArea{
             anchors.fill: parent
@@ -319,6 +318,8 @@ Rectangle {
                                                  ":/linkify-string.js"))
                 messageWebView.runJavaScript(UtilsAdapter.qStringFromFile(
                                                  ":/qwebchannel.js"))
+                messageWebView.runJavaScript(UtilsAdapter.qStringFromFile(
+                                                 ":/jed.js"))
                 messageWebView.runJavaScript(UtilsAdapter.qStringFromFile(
                                                  ":/emoji.js"))
                 messageWebView.runJavaScript(UtilsAdapter.qStringFromFile(
@@ -342,24 +343,18 @@ Rectangle {
         }
 
         Component.onCompleted: {
+            profile.cachePath = UtilsAdapter.getCachePath()
+            profile.persistentStoragePath = UtilsAdapter.getCachePath()
+            profile.persistentCookiesPolicy = WebEngineProfile.NoPersistentCookies
+            profile.httpCacheType = WebEngineProfile.NoCache
+            profile.httpUserAgent = JamiStrings.httpUserAgentName
+
             messageWebView.loadHtml(UtilsAdapter.qStringFromFile(
                                         ":/chatview.html"), ":/chatview.html")
             messageWebView.url = "qrc:/chatview.html"
         }
     }
 
-    // Provide WebEngineProfile.
-    WebEngineProfile {
-        id: messageWebViewProfile
-
-        cachePath: UtilsAdapter.getCachePath()
-        persistentStoragePath: UtilsAdapter.getCachePath()
-        persistentCookiesPolicy: WebEngineProfile.NoPersistentCookies
-        httpCacheType: WebEngineProfile.NoCache
-        httpUserAgent: "jami-windows"
-    }
-
-
     // Provide WebChannel by registering jsBridgeObject.
     WebChannel {
         id: messageWebViewChannel
-- 
GitLab