Title: [233782] trunk
Revision
233782
Author
[email protected]
Date
2018-07-12 14:34:35 -0700 (Thu, 12 Jul 2018)

Log Message

Add a FrameLoaderClient willInjectUserScriptForFrame callback
https://bugs.webkit.org/show_bug.cgi?id=187565

Reviewed by Alex Christensen.

Source/WebCore:

Test: http/tests/contentextensions/injected-script-callback.html.

* loader/FrameLoaderClient.h:
* page/Frame.cpp:
(WebCore::Frame::injectUserScriptImmediately):
Calling the new callback whenever being about to inject a new script.

Source/WebKit:

Introduce a new WKBundlePageLoaderClient callback that is called everytime a user script is injected.
Implement WebFrameLoaderClient::willInjectUserScript by calling this new callback.

* WebProcess/InjectedBundle/API/APIInjectedBundlePageLoaderClient.h:
(API::InjectedBundle::PageLoaderClient::willInjectUserScriptForFrame):
* WebProcess/InjectedBundle/API/c/WKBundlePageLoaderClient.h:
* WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm:
(setUpPageLoaderClient):
* WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.cpp:
(WebKit::InjectedBundlePageLoaderClient::globalObjectIsAvailableForFrame):
(WebKit::InjectedBundlePageLoaderClient::willInjectUserScriptForFrame):
* WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.h:
* WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
(WebKit::WebFrameLoaderClient::willInjectUserScript):
* WebProcess/WebCoreSupport/WebFrameLoaderClient.h:

Tools:

Added new test runner API to check for willInjectUserScriptForFrame callback.

* WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl:
* WebKitTestRunner/InjectedBundle/InjectedBundle.h:
(WTR::InjectedBundle::resetUserScriptInjectedCount):
(WTR::InjectedBundle::increaseUserScriptInjectedCount):
(WTR::InjectedBundle::userScriptInjectedCount const):
* WebKitTestRunner/InjectedBundle/InjectedBundlePage.cpp:
(WTR::InjectedBundlePage::InjectedBundlePage):
(WTR::InjectedBundlePage::resetAfterTest):
(WTR::InjectedBundlePage::willInjectUserScriptForFrame):
* WebKitTestRunner/InjectedBundle/InjectedBundlePage.h:
* WebKitTestRunner/InjectedBundle/TestRunner.cpp:
(WTR::TestRunner::userScriptInjectedCount const):
(WTR::TestRunner::injectUserScript):
* WebKitTestRunner/InjectedBundle/TestRunner.h:
* WebKitTestRunner/TestController.cpp:
(WTR::TestController::injectUserScript):
* WebKitTestRunner/TestController.h:
* WebKitTestRunner/TestInvocation.cpp:
(WTR::TestInvocation::didReceiveSynchronousMessageFromInjectedBundle):
* WebKitTestRunner/cocoa/TestControllerCocoa.mm:
(WTR::TestController::injectUserScript):

LayoutTests:

* http/tests/contentextensions/injected-script-callback-expected.txt: Added.
* http/tests/contentextensions/injected-script-callback.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (233781 => 233782)


--- trunk/LayoutTests/ChangeLog	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/LayoutTests/ChangeLog	2018-07-12 21:34:35 UTC (rev 233782)
@@ -1,3 +1,13 @@
+2018-07-12  Youenn Fablet  <[email protected]>
+
+        Add a FrameLoaderClient willInjectUserScriptForFrame callback
+        https://bugs.webkit.org/show_bug.cgi?id=187565
+
+        Reviewed by Alex Christensen.
+
+        * http/tests/contentextensions/injected-script-callback-expected.txt: Added.
+        * http/tests/contentextensions/injected-script-callback.html: Added.
+
 2018-07-11  Ross Kirsling  <[email protected]>
 
         UTF-16 XHTML files need svn:mime-type to be handled correctly by Windows SVN

Added: trunk/LayoutTests/http/tests/contentextensions/injected-script-callback-expected.txt (0 => 233782)


--- trunk/LayoutTests/http/tests/contentextensions/injected-script-callback-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/contentextensions/injected-script-callback-expected.txt	2018-07-12 21:34:35 UTC (rev 233782)
@@ -0,0 +1,4 @@
+  
+
+PASS Test willInjectUserScript callback being called 
+

Added: trunk/LayoutTests/http/tests/contentextensions/injected-script-callback.html (0 => 233782)


--- trunk/LayoutTests/http/tests/contentextensions/injected-script-callback.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/contentextensions/injected-script-callback.html	2018-07-12 21:34:35 UTC (rev 233782)
@@ -0,0 +1,53 @@
+<!doctype html>
+<html>
+    <head>
+        <meta charset="utf-8">
+        <title>Testing new callback</title>
+        <script src=""
+        <script src=""
+    </head>
+    <body>
+        <script>
+function waitFor(duration)
+{
+    return new Promise((resolve) => setTimeout(resolve, duration));
+}
+
+function withFrame(url)
+{
+    return new Promise((resolve) => {
+        let frame = document.createElement('iframe');
+        frame.src = ""
+        frame._onload_ = function() { resolve(frame); };
+        document.body.appendChild(frame);
+    });
+}
+
+async function verifyUserScriptInjectedCount(expected)
+{
+    let counter = 0;
+    while (++counter < 100) {
+        if (testRunner.userScriptInjectedCount === expected)
+            return;
+        await waitFor(10);
+    }
+    return Promise.reject("Did not get expected value " + expected + " but " + testRunner.userScriptInjectedCount);
+}
+
+promise_test(async () => {
+    if (!window.testRunner) {
+        reject("Test requires internals API");
+        return;
+    }
+    assert_equals(testRunner.userScriptInjectedCount, 0);
+    testRunner.injectUserScript("function a() { }");
+
+    await withFrame(".");
+    await verifyUserScriptInjectedCount(1);
+
+    await withFrame(".");
+    await verifyUserScriptInjectedCount(2);
+}, 'Test willInjectUserScript callback being called');
+        </script>
+    </body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (233781 => 233782)


--- trunk/Source/WebCore/ChangeLog	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebCore/ChangeLog	2018-07-12 21:34:35 UTC (rev 233782)
@@ -1,3 +1,17 @@
+2018-07-12  Youenn Fablet  <[email protected]>
+
+        Add a FrameLoaderClient willInjectUserScriptForFrame callback
+        https://bugs.webkit.org/show_bug.cgi?id=187565
+
+        Reviewed by Alex Christensen.
+
+        Test: http/tests/contentextensions/injected-script-callback.html.
+
+        * loader/FrameLoaderClient.h:
+        * page/Frame.cpp:
+        (WebCore::Frame::injectUserScriptImmediately):
+        Calling the new callback whenever being about to inject a new script.
+
 2018-07-12  Megan Gardner  <[email protected]>
 
         Keep Selections within Shadow DOM boundaries

Modified: trunk/Source/WebCore/loader/FrameLoaderClient.h (233781 => 233782)


--- trunk/Source/WebCore/loader/FrameLoaderClient.h	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebCore/loader/FrameLoaderClient.h	2018-07-12 21:34:35 UTC (rev 233782)
@@ -332,6 +332,8 @@
     virtual void dispatchDidReconnectDOMWindowExtensionToGlobalObject(DOMWindowExtension*) { }
     virtual void dispatchWillDestroyGlobalObjectForDOMWindowExtension(DOMWindowExtension*) { }
 
+    virtual void willInjectUserScript(DOMWrapperWorld&) { }
+
 #if ENABLE(WEB_RTC)
     virtual void dispatchWillStartUsingPeerConnectionHandler(RTCPeerConnectionHandler*) { }
 #endif

Modified: trunk/Source/WebCore/page/Frame.cpp (233781 => 233782)


--- trunk/Source/WebCore/page/Frame.cpp	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebCore/page/Frame.cpp	2018-07-12 21:34:35 UTC (rev 233782)
@@ -751,6 +751,7 @@
         return;
     if (m_page)
         m_page->setAsRunningUserScripts();
+    loader().client().willInjectUserScript(world);
     m_script->evaluateInWorld(ScriptSourceCode(script.source(), script.url()), world);
 }
 

Modified: trunk/Source/WebKit/ChangeLog (233781 => 233782)


--- trunk/Source/WebKit/ChangeLog	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebKit/ChangeLog	2018-07-12 21:34:35 UTC (rev 233782)
@@ -1,3 +1,26 @@
+2018-07-12  Youenn Fablet  <[email protected]>
+
+        Add a FrameLoaderClient willInjectUserScriptForFrame callback
+        https://bugs.webkit.org/show_bug.cgi?id=187565
+
+        Reviewed by Alex Christensen.
+
+        Introduce a new WKBundlePageLoaderClient callback that is called everytime a user script is injected.
+        Implement WebFrameLoaderClient::willInjectUserScript by calling this new callback.
+
+        * WebProcess/InjectedBundle/API/APIInjectedBundlePageLoaderClient.h:
+        (API::InjectedBundle::PageLoaderClient::willInjectUserScriptForFrame):
+        * WebProcess/InjectedBundle/API/c/WKBundlePageLoaderClient.h:
+        * WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm:
+        (setUpPageLoaderClient):
+        * WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.cpp:
+        (WebKit::InjectedBundlePageLoaderClient::globalObjectIsAvailableForFrame):
+        (WebKit::InjectedBundlePageLoaderClient::willInjectUserScriptForFrame):
+        * WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.h:
+        * WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
+        (WebKit::WebFrameLoaderClient::willInjectUserScript):
+        * WebProcess/WebCoreSupport/WebFrameLoaderClient.h:
+
 2018-07-12  Said Abou-Hallawa  <[email protected]>
 
         [iOS] When bringing MobileSafari to the foreground, images are drawn asynchronously after removing a snapshot that included them

Modified: trunk/Source/WebKit/WebProcess/InjectedBundle/API/APIInjectedBundlePageLoaderClient.h (233781 => 233782)


--- trunk/Source/WebKit/WebProcess/InjectedBundle/API/APIInjectedBundlePageLoaderClient.h	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebKit/WebProcess/InjectedBundle/API/APIInjectedBundlePageLoaderClient.h	2018-07-12 21:34:35 UTC (rev 233782)
@@ -89,6 +89,8 @@
     virtual void didReconnectDOMWindowExtensionToGlobalObject(WebKit::WebPage&, WebCore::DOMWindowExtension*) { }
     virtual void willDestroyGlobalObjectForDOMWindowExtension(WebKit::WebPage&, WebCore::DOMWindowExtension*) { }
 
+    virtual void willInjectUserScriptForFrame(WebKit::WebPage&, WebKit::WebFrame&, WebCore::DOMWrapperWorld&) { }
+
     virtual bool shouldForceUniversalAccessFromLocalURL(WebKit::WebPage&, const WTF::String&) { return false; }
 
     virtual void featuresUsedInPage(WebKit::WebPage&, const Vector<WTF::String>&) { }

Modified: trunk/Source/WebKit/WebProcess/InjectedBundle/API/c/WKBundlePageLoaderClient.h (233781 => 233782)


--- trunk/Source/WebKit/WebProcess/InjectedBundle/API/c/WKBundlePageLoaderClient.h	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebKit/WebProcess/InjectedBundle/API/c/WKBundlePageLoaderClient.h	2018-07-12 21:34:35 UTC (rev 233782)
@@ -53,6 +53,7 @@
 typedef void (*WKBundlePageDidHandleOnloadEventsForFrameCallback)(WKBundlePageRef page, WKBundleFrameRef frame, const void *clientInfo);
 typedef bool (*WKBundlePageShouldGoToBackForwardListItemCallback)(WKBundlePageRef page, WKBundleBackForwardListItemRef item, WKTypeRef* userData, const void *clientInfo);
 typedef void (*WKBundlePageGlobalObjectIsAvailableForFrameCallback)(WKBundlePageRef page, WKBundleFrameRef, WKBundleScriptWorldRef, const void* clientInfo);
+typedef void (*WKBundlePageWillInjectUserScriptForFrameCallback)(WKBundlePageRef page, WKBundleFrameRef, WKBundleScriptWorldRef, const void* clientInfo);
 typedef void (*WKBundlePageWillDisconnectDOMWindowExtensionFromGlobalObjectCallback)(WKBundlePageRef page, WKBundleDOMWindowExtensionRef, const void* clientInfo);
 typedef void (*WKBundlePageDidReconnectDOMWindowExtensionToGlobalObjectCallback)(WKBundlePageRef page, WKBundleDOMWindowExtensionRef, const void* clientInfo);
 typedef void (*WKBundlePageWillDestroyGlobalObjectForDOMWindowExtensionCallback)(WKBundlePageRef page, WKBundleDOMWindowExtensionRef, const void* clientInfo);
@@ -464,4 +465,65 @@
     WKBundlePageUserAgentForURLCallback                                     userAgentForURL;
 } WKBundlePageLoaderClientV8;
 
+typedef struct WKBundlePageLoaderClientV9 {
+    WKBundlePageLoaderClientBase                                            base;
+
+    // Version 0.
+    WKBundlePageDidStartProvisionalLoadForFrameCallback                     didStartProvisionalLoadForFrame;
+    WKBundlePageDidReceiveServerRedirectForProvisionalLoadForFrameCallback  didReceiveServerRedirectForProvisionalLoadForFrame;
+    WKBundlePageDidFailProvisionalLoadWithErrorForFrameCallback             didFailProvisionalLoadWithErrorForFrame;
+    WKBundlePageDidCommitLoadForFrameCallback                               didCommitLoadForFrame;
+    WKBundlePageDidFinishDocumentLoadForFrameCallback                       didFinishDocumentLoadForFrame;
+    WKBundlePageDidFinishLoadForFrameCallback                               didFinishLoadForFrame;
+    WKBundlePageDidFailLoadWithErrorForFrameCallback                        didFailLoadWithErrorForFrame;
+    WKBundlePageDidSameDocumentNavigationForFrameCallback                   didSameDocumentNavigationForFrame;
+    WKBundlePageDidReceiveTitleForFrameCallback                             didReceiveTitleForFrame;
+    WKBundlePageDidFirstLayoutForFrameCallback                              didFirstLayoutForFrame;
+    WKBundlePageDidFirstVisuallyNonEmptyLayoutForFrameCallback              didFirstVisuallyNonEmptyLayoutForFrame;
+    WKBundlePageDidRemoveFrameFromHierarchyCallback                         didRemoveFrameFromHierarchy;
+    WKBundlePageDidDisplayInsecureContentForFrameCallback                   didDisplayInsecureContentForFrame;
+    WKBundlePageDidRunInsecureContentForFrameCallback                       didRunInsecureContentForFrame;
+    WKBundlePageDidClearWindowObjectForFrameCallback                        didClearWindowObjectForFrame;
+    WKBundlePageDidCancelClientRedirectForFrameCallback                     didCancelClientRedirectForFrame;
+    WKBundlePageWillPerformClientRedirectForFrameCallback                   willPerformClientRedirectForFrame;
+    WKBundlePageDidHandleOnloadEventsForFrameCallback                       didHandleOnloadEventsForFrame;
+
+    // Version 1.
+    WKBundlePageDidLayoutForFrameCallback                                   didLayoutForFrame;
+    void *                                                                  didNewFirstVisuallyNonEmptyLayout_unavailable;
+    WKBundlePageDidDetectXSSForFrameCallback                                didDetectXSSForFrame;
+    WKBundlePageShouldGoToBackForwardListItemCallback                       shouldGoToBackForwardListItem;
+    WKBundlePageGlobalObjectIsAvailableForFrameCallback                     globalObjectIsAvailableForFrame;
+    WKBundlePageWillDisconnectDOMWindowExtensionFromGlobalObjectCallback    willDisconnectDOMWindowExtensionFromGlobalObject;
+    WKBundlePageDidReconnectDOMWindowExtensionToGlobalObjectCallback        didReconnectDOMWindowExtensionToGlobalObject;
+    WKBundlePageWillDestroyGlobalObjectForDOMWindowExtensionCallback        willDestroyGlobalObjectForDOMWindowExtension;
+
+    // Version 2
+    WKBundlePageDidFinishProgressCallback                                   didFinishProgress;
+    WKBundlePageShouldForceUniversalAccessFromLocalURLCallback              shouldForceUniversalAccessFromLocalURL;
+
+    // Version 3
+    void *                                                                  didReceiveIntentForFrame_unavailable;
+    void *                                                                  registerIntentServiceForFrame_unavailable;
+
+    // Version 4
+    WKBundlePageDidLayoutCallback                                           didLayout;
+
+    // Version 5
+    WKBundlePageFeaturesUsedInPageCallback                                  featuresUsedInPage;
+
+    // Version 6
+    WKBundlePageWillLoadURLRequestCallback                                  willLoadURLRequest;
+    WKBundlePageWillLoadDataRequestCallback                                 willLoadDataRequest;
+
+    // Version 7
+    void *                                                                  willDestroyFrame_unavailable;
+
+    // Version 8
+    WKBundlePageUserAgentForURLCallback                                     userAgentForURL;
+
+    // Version 9
+    WKBundlePageWillInjectUserScriptForFrameCallback                        willInjectUserScriptForFrame;
+} WKBundlePageLoaderClientV9;
+
 #endif // WKBundlePageLoaderClient_h

Modified: trunk/Source/WebKit/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm (233781 => 233782)


--- trunk/Source/WebKit/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebKit/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm	2018-07-12 21:34:35 UTC (rev 233782)
@@ -232,7 +232,7 @@
 
 static void setUpPageLoaderClient(WKWebProcessPlugInBrowserContextController *contextController, WebPage& page)
 {
-    WKBundlePageLoaderClientV8 client;
+    WKBundlePageLoaderClientV9 client;
     memset(&client, 0, sizeof(client));
 
     client.base.version = 8;

Modified: trunk/Source/WebKit/WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.cpp (233781 => 233782)


--- trunk/Source/WebKit/WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.cpp	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebKit/WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.cpp	2018-07-12 21:34:35 UTC (rev 233782)
@@ -303,11 +303,20 @@
 {
     if (!m_client.globalObjectIsAvailableForFrame)
         return;
-    
+
     RefPtr<InjectedBundleScriptWorld> injectedWorld = InjectedBundleScriptWorld::getOrCreate(world);
     m_client.globalObjectIsAvailableForFrame(toAPI(&page), toAPI(&frame), toAPI(injectedWorld.get()), m_client.base.clientInfo);
 }
 
+void InjectedBundlePageLoaderClient::willInjectUserScriptForFrame(WebPage& page, WebFrame& frame, DOMWrapperWorld& world)
+{
+    if (!m_client.willInjectUserScriptForFrame)
+        return;
+
+    RefPtr<InjectedBundleScriptWorld> injectedWorld = InjectedBundleScriptWorld::getOrCreate(world);
+    m_client.willInjectUserScriptForFrame(toAPI(&page), toAPI(&frame), toAPI(injectedWorld.get()), m_client.base.clientInfo);
+}
+
 void InjectedBundlePageLoaderClient::willDisconnectDOMWindowExtensionFromGlobalObject(WebPage& page, DOMWindowExtension* coreExtension)
 {
     if (!m_client.willDisconnectDOMWindowExtensionFromGlobalObject)

Modified: trunk/Source/WebKit/WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.h (233781 => 233782)


--- trunk/Source/WebKit/WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.h	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebKit/WebProcess/InjectedBundle/InjectedBundlePageLoaderClient.h	2018-07-12 21:34:35 UTC (rev 233782)
@@ -37,7 +37,7 @@
 class URL;
 
 template<> struct ClientTraits<WKBundlePageLoaderClientBase> {
-    typedef std::tuple<WKBundlePageLoaderClientV0, WKBundlePageLoaderClientV1, WKBundlePageLoaderClientV2, WKBundlePageLoaderClientV3, WKBundlePageLoaderClientV4, WKBundlePageLoaderClientV5, WKBundlePageLoaderClientV6, WKBundlePageLoaderClientV7, WKBundlePageLoaderClientV8> Versions;
+    typedef std::tuple<WKBundlePageLoaderClientV0, WKBundlePageLoaderClientV1, WKBundlePageLoaderClientV2, WKBundlePageLoaderClientV3, WKBundlePageLoaderClientV4, WKBundlePageLoaderClientV5, WKBundlePageLoaderClientV6, WKBundlePageLoaderClientV7, WKBundlePageLoaderClientV8, WKBundlePageLoaderClientV9> Versions;
 };
 }
 
@@ -81,6 +81,8 @@
     void didReconnectDOMWindowExtensionToGlobalObject(WebPage&, WebCore::DOMWindowExtension*) override;
     void willDestroyGlobalObjectForDOMWindowExtension(WebPage&, WebCore::DOMWindowExtension*) override;
 
+    void willInjectUserScriptForFrame(WebKit::WebPage&, WebKit::WebFrame&, WebCore::DOMWrapperWorld&) final;
+
     bool shouldForceUniversalAccessFromLocalURL(WebPage&, const WTF::String&) override;
 
     void featuresUsedInPage(WebPage&, const Vector<WTF::String>&) override;

Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp (233781 => 233782)


--- trunk/Source/WebKit/WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp	2018-07-12 21:34:35 UTC (rev 233782)
@@ -1691,7 +1691,6 @@
 #endif
 }
 
-
 void WebFrameLoaderClient::dispatchGlobalObjectAvailable(DOMWrapperWorld& world)
 {
     WebPage* webPage = m_frame->page();
@@ -1701,6 +1700,15 @@
     webPage->injectedBundleLoaderClient().globalObjectIsAvailableForFrame(*webPage, *m_frame, world);
 }
 
+void WebFrameLoaderClient::willInjectUserScript(DOMWrapperWorld& world)
+{
+    WebPage* webPage = m_frame->page();
+    if (!webPage)
+        return;
+
+    webPage->injectedBundleLoaderClient().willInjectUserScriptForFrame(*webPage, *m_frame, world);
+}
+
 void WebFrameLoaderClient::dispatchWillDisconnectDOMWindowExtensionFromGlobalObject(WebCore::DOMWindowExtension* extension)
 {
     WebPage* webPage = m_frame->page();

Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/WebFrameLoaderClient.h (233781 => 233782)


--- trunk/Source/WebKit/WebProcess/WebCoreSupport/WebFrameLoaderClient.h	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/WebFrameLoaderClient.h	2018-07-12 21:34:35 UTC (rev 233782)
@@ -232,7 +232,9 @@
     void dispatchWillDisconnectDOMWindowExtensionFromGlobalObject(WebCore::DOMWindowExtension*) final;
     void dispatchDidReconnectDOMWindowExtensionToGlobalObject(WebCore::DOMWindowExtension*) final;
     void dispatchWillDestroyGlobalObjectForDOMWindowExtension(WebCore::DOMWindowExtension*) final;
-    
+
+    void willInjectUserScript(WebCore::DOMWrapperWorld&) final;
+
 #if PLATFORM(COCOA)
     RemoteAXObjectRef accessibilityRemoteObject() final;
     

Modified: trunk/Tools/ChangeLog (233781 => 233782)


--- trunk/Tools/ChangeLog	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/ChangeLog	2018-07-12 21:34:35 UTC (rev 233782)
@@ -1,3 +1,34 @@
+2018-07-12  Youenn Fablet  <[email protected]>
+
+        Add a FrameLoaderClient willInjectUserScriptForFrame callback
+        https://bugs.webkit.org/show_bug.cgi?id=187565
+
+        Reviewed by Alex Christensen.
+
+        Added new test runner API to check for willInjectUserScriptForFrame callback.
+
+        * WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl:
+        * WebKitTestRunner/InjectedBundle/InjectedBundle.h:
+        (WTR::InjectedBundle::resetUserScriptInjectedCount):
+        (WTR::InjectedBundle::increaseUserScriptInjectedCount):
+        (WTR::InjectedBundle::userScriptInjectedCount const):
+        * WebKitTestRunner/InjectedBundle/InjectedBundlePage.cpp:
+        (WTR::InjectedBundlePage::InjectedBundlePage):
+        (WTR::InjectedBundlePage::resetAfterTest):
+        (WTR::InjectedBundlePage::willInjectUserScriptForFrame):
+        * WebKitTestRunner/InjectedBundle/InjectedBundlePage.h:
+        * WebKitTestRunner/InjectedBundle/TestRunner.cpp:
+        (WTR::TestRunner::userScriptInjectedCount const):
+        (WTR::TestRunner::injectUserScript):
+        * WebKitTestRunner/InjectedBundle/TestRunner.h:
+        * WebKitTestRunner/TestController.cpp:
+        (WTR::TestController::injectUserScript):
+        * WebKitTestRunner/TestController.h:
+        * WebKitTestRunner/TestInvocation.cpp:
+        (WTR::TestInvocation::didReceiveSynchronousMessageFromInjectedBundle):
+        * WebKitTestRunner/cocoa/TestControllerCocoa.mm:
+        (WTR::TestController::injectUserScript):
+
 2018-07-12  Sihui Liu  <[email protected]>
 
         IndexedDB: database file of subframe cannot be removed

Modified: trunk/Tools/WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl	2018-07-12 21:34:35 UTC (rev 233782)
@@ -343,4 +343,7 @@
     void clearMockMediaDevices();
     void removeMockMediaDevice(DOMString persistentId);
     void resetMockMediaDevices();
+
+    void injectUserScript(DOMString string);
+    readonly attribute unsigned long userScriptInjectedCount;
 };

Modified: trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundle.h (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundle.h	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundle.h	2018-07-12 21:34:35 UTC (rev 233782)
@@ -140,6 +140,10 @@
     void textFieldDidBeginEditing();
     void textFieldDidEndEditing();
 
+    void resetUserScriptInjectedCount() { m_userScriptInjectedCount = 0; }
+    void increaseUserScriptInjectedCount() { ++m_userScriptInjectedCount; }
+    size_t userScriptInjectedCount() const { return m_userScriptInjectedCount; }
+
 private:
     InjectedBundle();
     ~InjectedBundle();
@@ -199,6 +203,8 @@
     WKRetainPtr<WKArrayRef> m_repaintRects;
 
     Vector<String> m_allowedHosts;
+
+    size_t m_userScriptInjectedCount { 0 };
 };
 
 } // namespace WTR

Modified: trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundlePage.cpp (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundlePage.cpp	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundlePage.cpp	2018-07-12 21:34:35 UTC (rev 233782)
@@ -274,8 +274,8 @@
     : m_page(page)
     , m_world(AdoptWK, WKBundleScriptWorldCreateWorld())
 {
-    WKBundlePageLoaderClientV8 loaderClient = {
-        { 8, this },
+    WKBundlePageLoaderClientV9 loaderClient = {
+        { 9, this },
         didStartProvisionalLoadForFrame,
         didReceiveServerRedirectForProvisionalLoadForFrame,
         didFailProvisionalLoadWithErrorForFrame,
@@ -312,6 +312,7 @@
         0, // willLoadDataRequest
         0, // willDestroyFrame_unavailable
         0, // userAgentForURL
+        willInjectUserScriptForFrame
     };
     WKBundlePageSetPageLoaderClient(m_page, &loaderClient.base);
 
@@ -443,6 +444,8 @@
     WKBundlePageRemoveAllUserContent(m_page);
 
     uninstallFakeHelvetica();
+
+    InjectedBundle::singleton().resetUserScriptInjectedCount();
 }
 
 // Loader Client Callbacks
@@ -592,6 +595,11 @@
     static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didFinishProgress();
 }
 
+void InjectedBundlePage::willInjectUserScriptForFrame(WKBundlePageRef, WKBundleFrameRef, WKBundleScriptWorldRef, const void* clientInfo)
+{
+    static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->willInjectUserScriptForFrame();
+}
+
 void InjectedBundlePage::didFinishDocumentLoadForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKTypeRef*, const void* clientInfo)
 {
     static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didFinishDocumentLoadForFrame(frame);
@@ -759,6 +767,11 @@
     injectedBundle.outputText("postProgressFinishedNotification\n");
 }
 
+void InjectedBundlePage::willInjectUserScriptForFrame()
+{
+    InjectedBundle::singleton().increaseUserScriptInjectedCount();
+}
+
 enum FrameNamePolicy { ShouldNotIncludeFrameName, ShouldIncludeFrameName };
 
 static void dumpFrameScrollPosition(WKBundleFrameRef frame, StringBuilder& stringBuilder, FrameNamePolicy shouldIncludeFrameName = ShouldNotIncludeFrameName)

Modified: trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundlePage.h (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundlePage.h	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundlePage.h	2018-07-12 21:34:35 UTC (rev 233782)
@@ -77,6 +77,7 @@
     static void didFinishLoadForResource(WKBundlePageRef, WKBundleFrameRef, uint64_t identifier, const void*);
     static void didFailLoadForResource(WKBundlePageRef, WKBundleFrameRef, uint64_t identifier, WKErrorRef, const void*);
     static bool shouldCacheResponse(WKBundlePageRef, WKBundleFrameRef, uint64_t identifier, const void*);
+    static void willInjectUserScriptForFrame(WKBundlePageRef, WKBundleFrameRef, WKBundleScriptWorldRef, const void*);
 
     void didStartProvisionalLoadForFrame(WKBundleFrameRef);
     void didReceiveServerRedirectForProvisionalLoadForFrame(WKBundleFrameRef);
@@ -95,6 +96,7 @@
     void didDisplayInsecureContentForFrame(WKBundleFrameRef);
     void didRunInsecureContentForFrame(WKBundleFrameRef);
     void didDetectXSSForFrame(WKBundleFrameRef);
+    void willInjectUserScriptForFrame();
 
     // Resource Load Client
     void didInitiateLoadForResource(WKBundlePageRef, WKBundleFrameRef, uint64_t identifier, WKURLRequestRef, bool pageLoadIsProvisional);

Modified: trunk/Tools/WebKitTestRunner/InjectedBundle/TestRunner.cpp (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/InjectedBundle/TestRunner.cpp	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/InjectedBundle/TestRunner.cpp	2018-07-12 21:34:35 UTC (rev 233782)
@@ -2267,4 +2267,17 @@
     callTestRunnerCallback(GetApplicationManifestCallbackID);
 }
 
+size_t TestRunner::userScriptInjectedCount() const
+{
+    return InjectedBundle::singleton().userScriptInjectedCount();
+}
+
+void TestRunner::injectUserScript(JSStringRef script)
+{
+    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("InjectUserScript"));
+    WKRetainPtr<WKStringRef> messageBody(AdoptWK, WKStringCreateWithJSString(script));
+    WKTypeRef returnData = 0;
+    WKBundlePagePostSynchronousMessageForTesting(InjectedBundle::singleton().page()->page(), messageName.get(), messageBody.get(), &returnData);
+}
+
 } // namespace WTR

Modified: trunk/Tools/WebKitTestRunner/InjectedBundle/TestRunner.h (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/InjectedBundle/TestRunner.h	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/InjectedBundle/TestRunner.h	2018-07-12 21:34:35 UTC (rev 233782)
@@ -454,6 +454,9 @@
     void removeMockMediaDevice(JSStringRef persistentId);
     void resetMockMediaDevices();
 
+    size_t userScriptInjectedCount() const;
+    void injectUserScript(JSStringRef);
+
 private:
     TestRunner();
 

Modified: trunk/Tools/WebKitTestRunner/TestController.cpp (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/TestController.cpp	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/TestController.cpp	2018-07-12 21:34:35 UTC (rev 233782)
@@ -2912,6 +2912,11 @@
 void TestController::platformAddTestOptions(TestOptions&) const
 {
 }
+
+void TestController::injectUserScript(WKStringRef)
+{
+}
+
 #endif
 
 } // namespace WTR

Modified: trunk/Tools/WebKitTestRunner/TestController.h (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/TestController.h	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/TestController.h	2018-07-12 21:34:35 UTC (rev 233782)
@@ -218,6 +218,8 @@
     void removeMockMediaDevice(WKStringRef persistentID);
     void resetMockMediaDevices();
 
+    void injectUserScript(WKStringRef);
+
 private:
     WKRetainPtr<WKPageConfigurationRef> generatePageConfiguration(WKContextConfigurationRef);
     WKRetainPtr<WKContextConfigurationRef> generateContextConfiguration() const;

Modified: trunk/Tools/WebKitTestRunner/TestInvocation.cpp (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/TestInvocation.cpp	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/TestInvocation.cpp	2018-07-12 21:34:35 UTC (rev 233782)
@@ -1414,6 +1414,13 @@
         return result;
     }
 
+    if (WKStringIsEqualToUTF8CString(messageName, "InjectUserScript")) {
+        ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
+        WKStringRef script = static_cast<WKStringRef>(messageBody);
+
+        TestController::singleton().injectUserScript(script);
+    }
+
     if (WKStringIsEqualToUTF8CString(messageName, "GetApplicationManifest")) {
 #ifdef __BLOCKS__
         WKPageGetApplicationManifest_b(TestController::singleton().mainWebView()->page(), ^{

Modified: trunk/Tools/WebKitTestRunner/cocoa/TestControllerCocoa.mm (233781 => 233782)


--- trunk/Tools/WebKitTestRunner/cocoa/TestControllerCocoa.mm	2018-07-12 21:24:38 UTC (rev 233781)
+++ trunk/Tools/WebKitTestRunner/cocoa/TestControllerCocoa.mm	2018-07-12 21:34:35 UTC (rev 233782)
@@ -280,4 +280,13 @@
 #endif
 }
 
+void TestController::injectUserScript(WKStringRef script)
+{
+#if WK_API_ENABLED
+    auto userScript = adoptNS([[WKUserScript alloc] initWithSource: toWTFString(script) injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]);
+
+    [[globalWebViewConfiguration userContentController] addUserScript: userScript.get()];
+#endif
+}
+
 } // namespace WTR
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to