Title: [265695] branches/safari-610.1-branch
Revision
265695
Author
[email protected]
Date
2020-08-14 12:48:37 -0700 (Fri, 14 Aug 2020)

Log Message

Cherry-pick r265510. rdar://problem/67084518

    Deferred WKUserScripts are exponentially injected on preloaded pages with frames.
    https://bugs.webkit.org/show_bug.cgi?id=215382
    rdar://problem/66837802

    Reviewed by Sam Weinig.

    Source/WebCore:

    When defering a script in a frame it was previously added to a vector per-page.
    Later when notified to inject the defered scripts, the page would iterate over all
    the frames and evaluate the scripts on each frame. Since this vector had all the
    frame's scripts the evaluations would be multiplied by the number of frames.

    Now the defered scripts are stored per-frame and the page asks each frame to
    inject the defered scripts.

    * page/Frame.cpp:
    (WebCore::Frame::injectUserScripts):
    (WebCore::Frame::addUserScriptAwaitingNotification):
    (WebCore::Frame::injectUserScriptsAwaitingNotification):
    * page/Frame.h:
    * page/Page.cpp:
    (WebCore::Page::notifyToInjectUserScripts):
    (WebCore::Page::addUserScriptAwaitingNotification): Deleted.
    * page/Page.h:
    * page/Quirks.cpp:
    (WebCore::Quirks::triggerOptionalStorageAccessQuirk const):

    Tools:

    * TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm:
    (TEST):

    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@265510 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Modified Paths

Diff

Modified: branches/safari-610.1-branch/Source/WebCore/ChangeLog (265694 => 265695)


--- branches/safari-610.1-branch/Source/WebCore/ChangeLog	2020-08-14 19:48:33 UTC (rev 265694)
+++ branches/safari-610.1-branch/Source/WebCore/ChangeLog	2020-08-14 19:48:37 UTC (rev 265695)
@@ -1,5 +1,73 @@
 2020-08-14  Alan Coon  <[email protected]>
 
+        Cherry-pick r265510. rdar://problem/67084518
+
+    Deferred WKUserScripts are exponentially injected on preloaded pages with frames.
+    https://bugs.webkit.org/show_bug.cgi?id=215382
+    rdar://problem/66837802
+    
+    Reviewed by Sam Weinig.
+    
+    Source/WebCore:
+    
+    When defering a script in a frame it was previously added to a vector per-page.
+    Later when notified to inject the defered scripts, the page would iterate over all
+    the frames and evaluate the scripts on each frame. Since this vector had all the
+    frame's scripts the evaluations would be multiplied by the number of frames.
+    
+    Now the defered scripts are stored per-frame and the page asks each frame to
+    inject the defered scripts.
+    
+    * page/Frame.cpp:
+    (WebCore::Frame::injectUserScripts):
+    (WebCore::Frame::addUserScriptAwaitingNotification):
+    (WebCore::Frame::injectUserScriptsAwaitingNotification):
+    * page/Frame.h:
+    * page/Page.cpp:
+    (WebCore::Page::notifyToInjectUserScripts):
+    (WebCore::Page::addUserScriptAwaitingNotification): Deleted.
+    * page/Page.h:
+    * page/Quirks.cpp:
+    (WebCore::Quirks::triggerOptionalStorageAccessQuirk const):
+    
+    Tools:
+    
+    * TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm:
+    (TEST):
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@265510 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2020-08-11  Timothy Hatcher  <[email protected]>
+
+            Deferred WKUserScripts are exponentially injected on preloaded pages with frames.
+            https://bugs.webkit.org/show_bug.cgi?id=215382
+            rdar://problem/66837802
+
+            Reviewed by Sam Weinig.
+
+            When defering a script in a frame it was previously added to a vector per-page.
+            Later when notified to inject the defered scripts, the page would iterate over all
+            the frames and evaluate the scripts on each frame. Since this vector had all the
+            frame's scripts the evaluations would be multiplied by the number of frames.
+
+            Now the defered scripts are stored per-frame and the page asks each frame to
+            inject the defered scripts.
+
+            * page/Frame.cpp:
+            (WebCore::Frame::injectUserScripts):
+            (WebCore::Frame::addUserScriptAwaitingNotification):
+            (WebCore::Frame::injectUserScriptsAwaitingNotification):
+            * page/Frame.h:
+            * page/Page.cpp:
+            (WebCore::Page::notifyToInjectUserScripts):
+            (WebCore::Page::addUserScriptAwaitingNotification): Deleted.
+            * page/Page.h:
+            * page/Quirks.cpp:
+            (WebCore::Quirks::triggerOptionalStorageAccessQuirk const):
+
+2020-08-14  Alan Coon  <[email protected]>
+
         Cherry-pick r265499. rdar://problem/67084446
 
     [AutoTableLayout] REGRESSION(r263855) Paypal email is rendered right aligned on Safari

Modified: branches/safari-610.1-branch/Source/WebCore/page/Frame.cpp (265694 => 265695)


--- branches/safari-610.1-branch/Source/WebCore/page/Frame.cpp	2020-08-14 19:48:33 UTC (rev 265694)
+++ branches/safari-610.1-branch/Source/WebCore/page/Frame.cpp	2020-08-14 19:48:37 UTC (rev 265695)
@@ -648,7 +648,7 @@
     m_page->userContentProvider().forEachUserScript([this, protectedThis = makeRef(*this), injectionTime, pageWasNotified] (DOMWrapperWorld& world, const UserScript& script) {
         if (script.injectionTime() == injectionTime) {
             if (script.waitForNotificationBeforeInjecting() == WaitForNotificationBeforeInjecting::Yes && !pageWasNotified)
-                m_page->addUserScriptAwaitingNotification(world, script);
+                addUserScriptAwaitingNotification(world, script);
             else
                 injectUserScriptImmediately(world, script);
         }
@@ -680,6 +680,17 @@
     m_script->evaluateInWorldIgnoringException(ScriptSourceCode(script.source(), URL(script.url())), world);
 }
 
+void Frame::addUserScriptAwaitingNotification(DOMWrapperWorld& world, const UserScript& script)
+{
+    m_userScriptsAwaitingNotification.append({ makeRef(world), makeUniqueRef<UserScript>(script) });
+}
+
+void Frame::injectUserScriptsAwaitingNotification()
+{
+    for (const auto& [world, script] : std::exchange(m_userScriptsAwaitingNotification, { }))
+        injectUserScriptImmediately(world, script.get());
+}
+
 Optional<PageIdentifier> Frame::pageID() const
 {
     return loader().pageID();

Modified: branches/safari-610.1-branch/Source/WebCore/page/Frame.h (265694 => 265695)


--- branches/safari-610.1-branch/Source/WebCore/page/Frame.h	2020-08-14 19:48:33 UTC (rev 265694)
+++ branches/safari-610.1-branch/Source/WebCore/page/Frame.h	2020-08-14 19:48:37 UTC (rev 265695)
@@ -198,7 +198,10 @@
 
     WEBCORE_EXPORT void injectUserScripts(UserScriptInjectionTime);
     WEBCORE_EXPORT void injectUserScriptImmediately(DOMWrapperWorld&, const UserScript&);
-    
+
+    void injectUserScriptsAwaitingNotification();
+    void addUserScriptAwaitingNotification(DOMWrapperWorld&, const UserScript&);
+
     WEBCORE_EXPORT String layerTreeAsText(LayerTreeFlags = 0) const;
     WEBCORE_EXPORT String trackedRepaintRectsAsText() const;
 
@@ -328,6 +331,8 @@
 
     HashSet<FrameDestructionObserver*> m_destructionObservers;
 
+    Vector<std::pair<Ref<DOMWrapperWorld>, UniqueRef<UserScript>>> m_userScriptsAwaitingNotification;
+
     Frame& m_mainFrame;
     Page* m_page;
     const RefPtr<Settings> m_settings;

Modified: branches/safari-610.1-branch/Source/WebCore/page/Page.cpp (265694 => 265695)


--- branches/safari-610.1-branch/Source/WebCore/page/Page.cpp	2020-08-14 19:48:33 UTC (rev 265694)
+++ branches/safari-610.1-branch/Source/WebCore/page/Page.cpp	2020-08-14 19:48:37 UTC (rev 265695)
@@ -2664,19 +2664,10 @@
 {
     m_hasBeenNotifiedToInjectUserScripts = true;
 
-    for (auto* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
-        for (const auto& pair : m_userScriptsAwaitingNotification)
-            frame->injectUserScriptImmediately(pair.first, pair.second.get());
-    }
-
-    m_userScriptsAwaitingNotification.clear();
+    for (auto* frame = &mainFrame(); frame; frame = frame->tree().traverseNext())
+        frame->injectUserScriptsAwaitingNotification();
 }
 
-void Page::addUserScriptAwaitingNotification(DOMWrapperWorld& world, const UserScript& script)
-{
-    m_userScriptsAwaitingNotification.append({ makeRef(world), makeUniqueRef<UserScript>(script) });
-}
-
 void Page::setUserContentProvider(Ref<UserContentProvider>&& userContentProvider)
 {
     m_userContentProvider->removePage(*this);

Modified: branches/safari-610.1-branch/Source/WebCore/page/Page.h (265694 => 265695)


--- branches/safari-610.1-branch/Source/WebCore/page/Page.h	2020-08-14 19:48:33 UTC (rev 265694)
+++ branches/safari-610.1-branch/Source/WebCore/page/Page.h	2020-08-14 19:48:37 UTC (rev 265695)
@@ -774,7 +774,6 @@
 
     bool hasBeenNotifiedToInjectUserScripts() const { return m_hasBeenNotifiedToInjectUserScripts; }
     WEBCORE_EXPORT void notifyToInjectUserScripts();
-    void addUserScriptAwaitingNotification(DOMWrapperWorld&, const UserScript&);
 
 private:
     struct Navigation {
@@ -1069,7 +1068,6 @@
     bool m_loadsFromNetwork { true };
     ShouldRelaxThirdPartyCookieBlocking m_shouldRelaxThirdPartyCookieBlocking { ShouldRelaxThirdPartyCookieBlocking::No };
     bool m_hasBeenNotifiedToInjectUserScripts { false };
-    Vector<std::pair<Ref<DOMWrapperWorld>, UniqueRef<UserScript>>> m_userScriptsAwaitingNotification;
 };
 
 inline PageGroup& Page::group()

Modified: branches/safari-610.1-branch/Source/WebCore/page/Quirks.cpp (265694 => 265695)


--- branches/safari-610.1-branch/Source/WebCore/page/Quirks.cpp	2020-08-14 19:48:33 UTC (rev 265694)
+++ branches/safari-610.1-branch/Source/WebCore/page/Quirks.cpp	2020-08-14 19:48:37 UTC (rev 265695)
@@ -947,11 +947,9 @@
             auto* abstractFrame = proxy->frame();
             if (abstractFrame && is<Frame>(*abstractFrame)) {
                 auto& frame = downcast<Frame>(*abstractFrame);
-                if (auto* page = frame.page()) {
-                    auto world = ScriptController::createWorld("kinjaComQuirkWorld", ScriptController::WorldType::User);
-                    page->addUserScriptAwaitingNotification(world.get(), kinjaLoginUserScript);
-                    return Quirks::StorageAccessResult::ShouldCancelEvent;
-                }
+                auto world = ScriptController::createWorld("kinjaComQuirkWorld", ScriptController::WorldType::User);
+                frame.addUserScriptAwaitingNotification(world.get(), kinjaLoginUserScript);
+                return Quirks::StorageAccessResult::ShouldCancelEvent;
             }
         }
     }

Modified: branches/safari-610.1-branch/Tools/ChangeLog (265694 => 265695)


--- branches/safari-610.1-branch/Tools/ChangeLog	2020-08-14 19:48:33 UTC (rev 265694)
+++ branches/safari-610.1-branch/Tools/ChangeLog	2020-08-14 19:48:37 UTC (rev 265695)
@@ -1,5 +1,56 @@
 2020-08-14  Alan Coon  <[email protected]>
 
+        Cherry-pick r265510. rdar://problem/67084518
+
+    Deferred WKUserScripts are exponentially injected on preloaded pages with frames.
+    https://bugs.webkit.org/show_bug.cgi?id=215382
+    rdar://problem/66837802
+    
+    Reviewed by Sam Weinig.
+    
+    Source/WebCore:
+    
+    When defering a script in a frame it was previously added to a vector per-page.
+    Later when notified to inject the defered scripts, the page would iterate over all
+    the frames and evaluate the scripts on each frame. Since this vector had all the
+    frame's scripts the evaluations would be multiplied by the number of frames.
+    
+    Now the defered scripts are stored per-frame and the page asks each frame to
+    inject the defered scripts.
+    
+    * page/Frame.cpp:
+    (WebCore::Frame::injectUserScripts):
+    (WebCore::Frame::addUserScriptAwaitingNotification):
+    (WebCore::Frame::injectUserScriptsAwaitingNotification):
+    * page/Frame.h:
+    * page/Page.cpp:
+    (WebCore::Page::notifyToInjectUserScripts):
+    (WebCore::Page::addUserScriptAwaitingNotification): Deleted.
+    * page/Page.h:
+    * page/Quirks.cpp:
+    (WebCore::Quirks::triggerOptionalStorageAccessQuirk const):
+    
+    Tools:
+    
+    * TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm:
+    (TEST):
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@265510 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2020-08-11  Timothy Hatcher  <[email protected]>
+
+            Deferred WKUserScripts are exponentially injected on preloaded pages with frames.
+            https://bugs.webkit.org/show_bug.cgi?id=215382
+            rdar://problem/66837802
+
+            Reviewed by Sam Weinig.
+
+            * TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm:
+            (TEST):
+
+2020-08-14  Alan Coon  <[email protected]>
+
         Cherry-pick r265420. rdar://problem/67083903
 
     REGRESSION (r260831): Web process crashes under Editor::setComposition() after navigating with marked text

Modified: branches/safari-610.1-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm (265694 => 265695)


--- branches/safari-610.1-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm	2020-08-14 19:48:33 UTC (rev 265694)
+++ branches/safari-610.1-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm	2020-08-14 19:48:37 UTC (rev 265695)
@@ -945,6 +945,18 @@
     EXPECT_FALSE(webView3._deferrableUserScriptsNeedNotification);
     EXPECT_WK_STREQ([delegate waitForAlert], "waited for notification");
     EXPECT_WK_STREQ([delegate waitForAlert], "document parsing ended");
+
+    TestWKWebView *webView4 = [[TestWKWebView new] autorelease];
+    EXPECT_TRUE(webView4._deferrableUserScriptsNeedNotification);
+    [webView4.configuration.userContentController addUserScript:waitsForNotification];
+    [webView4.configuration.userContentController addUserScript:documentEnd];
+    webView4.UIDelegate = delegate;
+    [webView4 loadTestPageNamed:@"simple-iframe"];
+    [webView4 _notifyUserScripts];
+
+    // If this is broken, two alerts would appear back-to-back with the same text due to the frame.
+    EXPECT_WK_STREQ([delegate waitForAlert], "waited for notification");
+    EXPECT_WK_STREQ([delegate waitForAlert], "document parsing ended");
 }
 
 @interface AsyncScriptMessageHandler : NSObject <WKScriptMessageHandlerWithReply>
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to