Title: [240325] trunk/Source/WebKit
Revision
240325
Author
cdu...@apple.com
Date
2019-01-22 21:30:09 -0800 (Tue, 22 Jan 2019)

Log Message

Regression(r240178) Some API tests are crashing
https://bugs.webkit.org/show_bug.cgi?id=193680

Reviewed by Alex Christensen.

r240178 made sure that userScripts / scriptMessageHandlers / contentExtensions are always
properly populated in the WebPageCreationParameters. This was needed in case we need to
reconstruct the WebUserContentController on the WebProcess side. However, this caused a
regression in the case we reuse a process where the WebUserContentController still exists
(because it was kept alive, e.g. by the WebPageGroup). In that case, we would add duplicate
entries to the existing WebUserContentController instance because its "add" methods did not
have duplicate checks. To address the issue, this patch adds duplicate checks to the
WebUserContentController "add" methods.

* WebProcess/UserContent/WebUserContentController.cpp:
(WebKit::WebUserContentController::addUserScriptMessageHandlerInternal):
(WebKit::WebUserContentController::removeUserScriptMessageHandlerInternal):
(WebKit::WebUserContentController::addUserScriptInternal):
(WebKit::WebUserContentController::removeUserScriptInternal):
(WebKit::WebUserContentController::addUserStyleSheetInternal):
(WebKit::WebUserContentController::removeUserStyleSheetInternal):
(WebKit::WebUserContentController::forEachUserMessageHandler const):
* WebProcess/UserContent/WebUserContentController.h:

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (240324 => 240325)


--- trunk/Source/WebKit/ChangeLog	2019-01-23 05:20:36 UTC (rev 240324)
+++ trunk/Source/WebKit/ChangeLog	2019-01-23 05:30:09 UTC (rev 240325)
@@ -1,3 +1,29 @@
+2019-01-22  Chris Dumez  <cdu...@apple.com>
+
+        Regression(r240178) Some API tests are crashing
+        https://bugs.webkit.org/show_bug.cgi?id=193680
+
+        Reviewed by Alex Christensen.
+
+        r240178 made sure that userScripts / scriptMessageHandlers / contentExtensions are always
+        properly populated in the WebPageCreationParameters. This was needed in case we need to
+        reconstruct the WebUserContentController on the WebProcess side. However, this caused a
+        regression in the case we reuse a process where the WebUserContentController still exists
+        (because it was kept alive, e.g. by the WebPageGroup). In that case, we would add duplicate
+        entries to the existing WebUserContentController instance because its "add" methods did not
+        have duplicate checks. To address the issue, this patch adds duplicate checks to the
+        WebUserContentController "add" methods.
+
+        * WebProcess/UserContent/WebUserContentController.cpp:
+        (WebKit::WebUserContentController::addUserScriptMessageHandlerInternal):
+        (WebKit::WebUserContentController::removeUserScriptMessageHandlerInternal):
+        (WebKit::WebUserContentController::addUserScriptInternal):
+        (WebKit::WebUserContentController::removeUserScriptInternal):
+        (WebKit::WebUserContentController::addUserStyleSheetInternal):
+        (WebKit::WebUserContentController::removeUserStyleSheetInternal):
+        (WebKit::WebUserContentController::forEachUserMessageHandler const):
+        * WebProcess/UserContent/WebUserContentController.h:
+
 2019-01-22  Michael Catanzaro  <mcatanz...@igalia.com>
 
         Unreviewed attempt to fix GTK/WPE bots

Modified: trunk/Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp (240324 => 240325)


--- trunk/Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp	2019-01-23 05:20:36 UTC (rev 240324)
+++ trunk/Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp	2019-01-23 05:30:09 UTC (rev 240325)
@@ -318,8 +318,10 @@
 #if ENABLE(USER_MESSAGE_HANDLERS)
 void WebUserContentController::addUserScriptMessageHandlerInternal(InjectedBundleScriptWorld& world, uint64_t userScriptMessageHandlerIdentifier, const String& name)
 {
-    auto& messageHandlersInWorld = m_userMessageHandlers.ensure(&world, [] { return Vector<RefPtr<WebUserMessageHandlerDescriptorProxy>>(); }).iterator->value;
-    messageHandlersInWorld.append(WebUserMessageHandlerDescriptorProxy::create(this, name, world, userScriptMessageHandlerIdentifier));
+    auto& messageHandlersInWorld = m_userMessageHandlers.ensure(&world, [] { return Vector<std::pair<uint64_t, RefPtr<WebUserMessageHandlerDescriptorProxy>>> { }; }).iterator->value;
+    if (messageHandlersInWorld.findMatching([&](auto& pair) { return pair.first ==  userScriptMessageHandlerIdentifier; }) != notFound)
+        return;
+    messageHandlersInWorld.append(std::make_pair(userScriptMessageHandlerIdentifier, WebUserMessageHandlerDescriptorProxy::create(this, name, world, userScriptMessageHandlerIdentifier)));
 }
 
 void WebUserContentController::removeUserScriptMessageHandlerInternal(InjectedBundleScriptWorld& world, uint64_t userScriptMessageHandlerIdentifier)
@@ -329,15 +331,10 @@
         return;
 
     auto& userMessageHandlers = it->value;
+    bool userMessageHandlersChanged = userMessageHandlers.removeFirstMatching([userScriptMessageHandlerIdentifier](auto& pair) {
+        return pair.first ==  userScriptMessageHandlerIdentifier;
+    });
 
-    bool userMessageHandlersChanged = false;
-    for (int i = userMessageHandlers.size() - 1; i >= 0; --i) {
-        if (userMessageHandlers[i]->identifier() == userScriptMessageHandlerIdentifier) {
-            userMessageHandlers.remove(i);
-            userMessageHandlersChanged = true;
-        }
-    }
-
     if (!userMessageHandlersChanged)
         return;
 
@@ -370,7 +367,7 @@
 }
 #endif
 
-void WebUserContentController::addUserScriptInternal(InjectedBundleScriptWorld& world, uint64_t userScriptIdentifier, UserScript&& userScript, InjectUserScriptImmediately immediately)
+void WebUserContentController::addUserScriptInternal(InjectedBundleScriptWorld& world, const Optional<uint64_t>& userScriptIdentifier, UserScript&& userScript, InjectUserScriptImmediately immediately)
 {
     if (immediately == InjectUserScriptImmediately::Yes) {
         Page::forEachPage([&] (auto& page) {
@@ -388,13 +385,16 @@
         });
     }
 
-    auto& scriptsInWorld = m_userScripts.ensure(&world, [] { return Vector<std::pair<uint64_t, WebCore::UserScript>>(); }).iterator->value;
+    auto& scriptsInWorld = m_userScripts.ensure(&world, [] { return Vector<std::pair<Optional<uint64_t>, WebCore::UserScript>>(); }).iterator->value;
+    if (userScriptIdentifier && scriptsInWorld.findMatching([&](auto& pair) { return pair.first == userScriptIdentifier; }) != notFound)
+        return;
+
     scriptsInWorld.append(std::make_pair(userScriptIdentifier, WTFMove(userScript)));
 }
 
 void WebUserContentController::addUserScript(InjectedBundleScriptWorld& world, UserScript&& userScript)
 {
-    addUserScriptInternal(world, 0, WTFMove(userScript), InjectUserScriptImmediately::No);
+    addUserScriptInternal(world, WTF::nullopt, WTFMove(userScript), InjectUserScriptImmediately::No);
 }
 
 void WebUserContentController::removeUserScriptWithURL(InjectedBundleScriptWorld& world, const URL& url)
@@ -404,10 +404,9 @@
         return;
 
     auto& scripts = it->value;
-    for (int i = scripts.size() - 1; i >= 0; --i) {
-        if (scripts[i].second.url() == url)
-            scripts.remove(i);
-    }
+    scripts.removeAllMatching([&](auto& pair) {
+        return pair.second.url() == url;
+    });
 
     if (scripts.isEmpty())
         m_userScripts.remove(it);
@@ -420,10 +419,9 @@
         return;
 
     auto& scripts = it->value;
-    for (int i = scripts.size() - 1; i >= 0; --i) {
-        if (scripts[i].first == userScriptIdentifier)
-            scripts.remove(i);
-    }
+    scripts.removeFirstMatching([userScriptIdentifier](auto& pair) {
+        return pair.first == userScriptIdentifier;
+    });
 
     if (scripts.isEmpty())
         m_userScripts.remove(it);
@@ -434,15 +432,18 @@
     m_userScripts.remove(&world);
 }
 
-void WebUserContentController::addUserStyleSheetInternal(InjectedBundleScriptWorld& world, uint64_t userStyleSheetIdentifier, UserStyleSheet&& userStyleSheet)
+void WebUserContentController::addUserStyleSheetInternal(InjectedBundleScriptWorld& world, const Optional<uint64_t>& userStyleSheetIdentifier, UserStyleSheet&& userStyleSheet)
 {
-    auto& styleSheetsInWorld = m_userStyleSheets.ensure(&world, [] { return Vector<std::pair<uint64_t, WebCore::UserStyleSheet>>(); }).iterator->value;
+    auto& styleSheetsInWorld = m_userStyleSheets.ensure(&world, [] { return Vector<std::pair<Optional<uint64_t>, WebCore::UserStyleSheet>>(); }).iterator->value;
+    if (userStyleSheetIdentifier && styleSheetsInWorld.findMatching([&](auto& pair) { return pair.first == userStyleSheetIdentifier; }) != notFound)
+        return;
+
     styleSheetsInWorld.append(std::make_pair(userStyleSheetIdentifier, WTFMove(userStyleSheet)));
 }
 
 void WebUserContentController::addUserStyleSheet(InjectedBundleScriptWorld& world, UserStyleSheet&& userStyleSheet)
 {
-    addUserStyleSheetInternal(world, 0, WTFMove(userStyleSheet));
+    addUserStyleSheetInternal(world, WTF::nullopt, WTFMove(userStyleSheet));
     invalidateInjectedStyleSheetCacheInAllFramesInAllPages();
 }
 
@@ -453,15 +454,10 @@
         return;
 
     auto& stylesheets = it->value;
+    bool sheetsChanged = stylesheets.removeAllMatching([&](auto& pair) {
+        return pair.second.url() == url;
+    });
 
-    bool sheetsChanged = false;
-    for (int i = stylesheets.size() - 1; i >= 0; --i) {
-        if (stylesheets[i].second.url() == url) {
-            stylesheets.remove(i);
-            sheetsChanged = true;
-        }
-    }
-
     if (!sheetsChanged)
         return;
 
@@ -478,15 +474,10 @@
         return;
 
     auto& stylesheets = it->value;
+    bool sheetsChanged = stylesheets.removeFirstMatching([userStyleSheetIdentifier](auto& pair) {
+        return pair.first == userStyleSheetIdentifier;
+    });
 
-    bool sheetsChanged = false;
-    for (int i = stylesheets.size() - 1; i >= 0; --i) {
-        if (stylesheets[i].first == userStyleSheetIdentifier) {
-            stylesheets.remove(i);
-            sheetsChanged = true;
-        }
-    }
-
     if (!sheetsChanged)
         return;
 
@@ -534,9 +525,9 @@
 #if ENABLE(USER_MESSAGE_HANDLERS)
 void WebUserContentController::forEachUserMessageHandler(Function<void(const WebCore::UserMessageHandlerDescriptor&)>&& functor) const
 {
-    for (const auto& userMessageHandlerVector : m_userMessageHandlers.values()) {
-        for (const auto& userMessageHandler : userMessageHandlerVector)
-            functor(*userMessageHandler.get());
+    for (auto& userMessageHandlerVector : m_userMessageHandlers.values()) {
+        for (auto& pair : userMessageHandlerVector)
+            functor(*pair.second.get());
     }
 }
 #endif

Modified: trunk/Source/WebKit/WebProcess/UserContent/WebUserContentController.h (240324 => 240325)


--- trunk/Source/WebKit/WebProcess/UserContent/WebUserContentController.h	2019-01-23 05:20:36 UTC (rev 240324)
+++ trunk/Source/WebKit/WebProcess/UserContent/WebUserContentController.h	2019-01-23 05:30:09 UTC (rev 240325)
@@ -104,9 +104,9 @@
     void removeAllContentRuleLists();
 #endif
 
-    void addUserScriptInternal(InjectedBundleScriptWorld&, uint64_t userScriptIdentifier, WebCore::UserScript&&, InjectUserScriptImmediately);
+    void addUserScriptInternal(InjectedBundleScriptWorld&, const Optional<uint64_t>& userScriptIdentifier, WebCore::UserScript&&, InjectUserScriptImmediately);
     void removeUserScriptInternal(InjectedBundleScriptWorld&, uint64_t userScriptIdentifier);
-    void addUserStyleSheetInternal(InjectedBundleScriptWorld&, uint64_t userStyleSheetIdentifier, WebCore::UserStyleSheet&&);
+    void addUserStyleSheetInternal(InjectedBundleScriptWorld&, const Optional<uint64_t>& userStyleSheetIdentifier, WebCore::UserStyleSheet&&);
     void removeUserStyleSheetInternal(InjectedBundleScriptWorld&, uint64_t userStyleSheetIdentifier);
 #if ENABLE(USER_MESSAGE_HANDLERS)
     void addUserScriptMessageHandlerInternal(InjectedBundleScriptWorld&, uint64_t userScriptMessageHandlerIdentifier, const String& name);
@@ -115,14 +115,14 @@
 
     UserContentControllerIdentifier m_identifier;
 
-    typedef HashMap<RefPtr<InjectedBundleScriptWorld>, Vector<std::pair<uint64_t, WebCore::UserScript>>> WorldToUserScriptMap;
+    typedef HashMap<RefPtr<InjectedBundleScriptWorld>, Vector<std::pair<Optional<uint64_t>, WebCore::UserScript>>> WorldToUserScriptMap;
     WorldToUserScriptMap m_userScripts;
 
-    typedef HashMap<RefPtr<InjectedBundleScriptWorld>, Vector<std::pair<uint64_t, WebCore::UserStyleSheet>>> WorldToUserStyleSheetMap;
+    typedef HashMap<RefPtr<InjectedBundleScriptWorld>, Vector<std::pair<Optional<uint64_t>, WebCore::UserStyleSheet>>> WorldToUserStyleSheetMap;
     WorldToUserStyleSheetMap m_userStyleSheets;
 
 #if ENABLE(USER_MESSAGE_HANDLERS)
-    typedef HashMap<RefPtr<InjectedBundleScriptWorld>, Vector<RefPtr<WebUserMessageHandlerDescriptorProxy>>> WorldToUserMessageHandlerVectorMap;
+    typedef HashMap<RefPtr<InjectedBundleScriptWorld>, Vector<std::pair<uint64_t, RefPtr<WebUserMessageHandlerDescriptorProxy>>>> WorldToUserMessageHandlerVectorMap;
     WorldToUserMessageHandlerVectorMap m_userMessageHandlers;
 #endif
 #if ENABLE(CONTENT_EXTENSIONS)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to