From a45f6fc3e1016a223a4b235cc61b033f8665c853 Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@theqtcompany.com>
Date: Fri, 25 Sep 2015 15:39:12 +0200
Subject: [PATCH] Keep order of scripts added to QWebEngineScriptCollection

Use a QList instead of a QSet to store the scripts in the collection.
This avoids situations where two scripts injected depend on each other,
and fail or succeed depending on the semi-random order that QSet imposes.

Change-Id: I44d5d89866ff2431544cc91afb1c102d93daa5da
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
---
 src/core/user_script.cpp                 | 11 ---------
 src/core/user_script.h                   |  4 ---
 src/core/user_script_controller_host.cpp | 31 ++++++++++++++----------
 src/core/user_script_controller_host.h   |  6 ++---
 4 files changed, 21 insertions(+), 31 deletions(-)

diff --git a/src/core/user_script.cpp b/src/core/user_script.cpp
index fb293c56a..179febc48 100644
--- a/src/core/user_script.cpp
+++ b/src/core/user_script.cpp
@@ -168,14 +168,3 @@ UserScriptData &UserScript::data() const
 }
 
 } // namespace QtWebEngineCore
-
-QT_BEGIN_NAMESPACE
-uint qHash(const QtWebEngineCore::UserScript &script, uint seed)
-{
-    if (script.isNull())
-        return 0;
-    return qHash(script.sourceCode(), seed) ^ qHash(script.name(), seed)
-           ^ (script.injectionPoint() | (script.runsOnSubFrames() << 4))
-           ^ script.worldId();
-}
-QT_END_NAMESPACE
diff --git a/src/core/user_script.h b/src/core/user_script.h
index 7aeba9131..69c32c7ba 100644
--- a/src/core/user_script.h
+++ b/src/core/user_script.h
@@ -93,8 +93,4 @@ private:
 
 } // namespace QtWebEngineCore
 
-QT_BEGIN_NAMESPACE
-uint qHash(const QtWebEngineCore::UserScript &, uint seed = 0);
-QT_END_NAMESPACE
-
 #endif // USER_SCRIPT_H
diff --git a/src/core/user_script_controller_host.cpp b/src/core/user_script_controller_host.cpp
index d57518275..a0d3f6fed 100644
--- a/src/core/user_script_controller_host.cpp
+++ b/src/core/user_script_controller_host.cpp
@@ -116,9 +116,11 @@ void UserScriptControllerHost::addUserScript(const UserScript &script, WebConten
         return;
     // Global scripts should be dispatched to all our render processes.
     if (!adapter) {
-        m_profileWideScripts.insert(script);
-        Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses)
-            renderer->Send(new UserScriptController_AddScript(script.data()));
+        if (!m_profileWideScripts.contains(script)) {
+            m_profileWideScripts.append(script);
+            Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses)
+                renderer->Send(new UserScriptController_AddScript(script.data()));
+        }
     } else {
         content::WebContents *contents = adapter->webContents();
         ContentsScriptsMap::iterator it = m_perContentsScripts.find(contents);
@@ -126,11 +128,13 @@ void UserScriptControllerHost::addUserScript(const UserScript &script, WebConten
             // We need to keep track of RenderView/RenderViewHost changes for a given contents
             // in order to make sure the scripts stay in sync
             new WebContentsObserverHelper(this, contents);
-            it = m_perContentsScripts.insert(contents, (QSet<UserScript>() << script));
+            it = m_perContentsScripts.insert(contents, (QList<UserScript>() << script));
         } else {
-            QSet<UserScript> currentScripts = it.value();
-            currentScripts.insert(script);
-            m_perContentsScripts.insert(contents, currentScripts);
+            QList<UserScript> currentScripts = it.value();
+            if (!currentScripts.contains(script)) {
+                currentScripts.append(script);
+                m_perContentsScripts.insert(contents, currentScripts);
+            }
         }
         contents->Send(new RenderViewObserverHelper_AddScript(contents->GetRoutingID(), script.data()));
     }
@@ -151,7 +155,8 @@ bool UserScriptControllerHost::removeUserScript(const UserScript &script, WebCon
     if (script.isNull())
         return false;
     if (!adapter) {
-        QSet<UserScript>::iterator it = m_profileWideScripts.find(script);
+        QList<UserScript>::iterator it
+                = std::find(m_profileWideScripts.begin(), m_profileWideScripts.end(), script);
         if (it == m_profileWideScripts.end())
             return false;
         Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses)
@@ -161,12 +166,12 @@ bool UserScriptControllerHost::removeUserScript(const UserScript &script, WebCon
         content::WebContents *contents = adapter->webContents();
         if (!m_perContentsScripts.contains(contents))
             return false;
-        QSet<UserScript> &set(m_perContentsScripts[contents]);
-        QSet<UserScript>::iterator it = set.find(script);
-        if (it == set.end())
+        QList<UserScript> &list(m_perContentsScripts[contents]);
+        QList<UserScript>::iterator it = std::find(list.begin(), list.end(), script);
+        if (it == list.end())
             return false;
         contents->Send(new RenderViewObserverHelper_RemoveScript(contents->GetRoutingID(), (*it).data()));
-        set.erase(it);
+        list.erase(it);
     }
     return true;
 }
@@ -184,7 +189,7 @@ void UserScriptControllerHost::clearAllScripts(WebContentsAdapter *adapter)
     }
 }
 
-const QSet<UserScript> UserScriptControllerHost::registeredScripts(WebContentsAdapter *adapter) const
+const QList<UserScript> UserScriptControllerHost::registeredScripts(WebContentsAdapter *adapter) const
 {
     if (!adapter)
         return m_profileWideScripts;
diff --git a/src/core/user_script_controller_host.h b/src/core/user_script_controller_host.h
index 49c96b333..3884fb3b9 100644
--- a/src/core/user_script_controller_host.h
+++ b/src/core/user_script_controller_host.h
@@ -64,7 +64,7 @@ public:
     bool removeUserScript(const UserScript &script, WebContentsAdapter *adapter);
     void clearAllScripts(WebContentsAdapter *adapter);
     void reserve(WebContentsAdapter *adapter, int count);
-    const QSet<UserScript> registeredScripts(WebContentsAdapter *adapter) const;
+    const QList<UserScript> registeredScripts(WebContentsAdapter *adapter) const;
 
     void renderProcessStartedWithHost(content::RenderProcessHost *renderer);
 
@@ -75,8 +75,8 @@ private:
 
     void webContentsDestroyed(content::WebContents *);
 
-    QSet<UserScript> m_profileWideScripts;
-    typedef QHash<content::WebContents *, QSet<UserScript>> ContentsScriptsMap;
+    QList<UserScript> m_profileWideScripts;
+    typedef QHash<content::WebContents *, QList<UserScript>> ContentsScriptsMap;
     ContentsScriptsMap m_perContentsScripts;
     QSet<content::RenderProcessHost *> m_observedProcesses;
     QScopedPointer<RenderProcessObserverHelper> m_renderProcessObserver;
-- 
GitLab