Title: [252634] trunk
Revision
252634
Author
[email protected]
Date
2019-11-19 09:11:49 -0800 (Tue, 19 Nov 2019)

Log Message

[Cocoa] Add WKUIDelegate SPI to inform clients when a _WKInspector attaches to a WKWebView
https://bugs.webkit.org/show_bug.cgi?id=204300
<rdar://problem/57136993>

Reviewed by Devin Rousso.

Source/WebKit:

Add a new UI delegate method to notify clients when local Web Inspector is about to be loaded.
This can be triggered by -[_WKInspector show] in the Cocoa API, or via the WebCore-controlled
Inspect context menu item.

The client can then configure Web Inspector using delegates or by setting _WKInspector properties.

Covered by new API test WebKit.DidNotifyWhenInspectorAttached.

* UIProcess/API/APIUIClient.h:
(API::UIClient::didAttachInspector):
* UIProcess/API/Cocoa/WKUIDelegatePrivate.h:
* UIProcess/Cocoa/UIDelegate.h:
* UIProcess/Cocoa/UIDelegate.mm:
(WebKit::UIDelegate::setDelegate):
(WebKit::UIDelegate::UIClient::didAttachInspector):
* UIProcess/WebInspectorProxy.cpp:
(WebKit::WebInspectorProxy::openLocalInspectorFrontend):

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/UIDelegate.mm:
(-[InspectorDelegate _webView:didAttachInspector:]):
(TEST): Add new test to ensure the delegate is called as expected.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (252633 => 252634)


--- trunk/Source/WebKit/ChangeLog	2019-11-19 16:51:33 UTC (rev 252633)
+++ trunk/Source/WebKit/ChangeLog	2019-11-19 17:11:49 UTC (rev 252634)
@@ -1,3 +1,29 @@
+2019-11-19  Brian Burg  <[email protected]>
+
+        [Cocoa] Add WKUIDelegate SPI to inform clients when a _WKInspector attaches to a WKWebView
+        https://bugs.webkit.org/show_bug.cgi?id=204300
+        <rdar://problem/57136993>
+
+        Reviewed by Devin Rousso.
+
+        Add a new UI delegate method to notify clients when local Web Inspector is about to be loaded.
+        This can be triggered by -[_WKInspector show] in the Cocoa API, or via the WebCore-controlled 
+        Inspect context menu item.
+
+        The client can then configure Web Inspector using delegates or by setting _WKInspector properties.
+
+        Covered by new API test WebKit.DidNotifyWhenInspectorAttached.
+
+        * UIProcess/API/APIUIClient.h:
+        (API::UIClient::didAttachInspector):
+        * UIProcess/API/Cocoa/WKUIDelegatePrivate.h:
+        * UIProcess/Cocoa/UIDelegate.h:
+        * UIProcess/Cocoa/UIDelegate.mm:
+        (WebKit::UIDelegate::setDelegate):
+        (WebKit::UIDelegate::UIClient::didAttachInspector):
+        * UIProcess/WebInspectorProxy.cpp:
+        (WebKit::WebInspectorProxy::openLocalInspectorFrontend):
+
 2019-11-18  John Wilander  <[email protected]>
 
         Check if ITP is on before applying third-party cookie blocking

Modified: trunk/Source/WebKit/UIProcess/API/APIUIClient.h (252633 => 252634)


--- trunk/Source/WebKit/UIProcess/API/APIUIClient.h	2019-11-19 16:51:33 UTC (rev 252633)
+++ trunk/Source/WebKit/UIProcess/API/APIUIClient.h	2019-11-19 17:11:49 UTC (rev 252634)
@@ -199,6 +199,8 @@
 #if ENABLE(WEB_AUTHN)
     virtual void runWebAuthenticationPanel(WebKit::WebPageProxy&, WebAuthenticationPanel&, WebKit::WebFrameProxy&, WebCore::SecurityOriginData&&, CompletionHandler<void(WebKit::WebAuthenticationPanelResult)>&& completionHandler) { completionHandler(WebKit::WebAuthenticationPanelResult::Unavailable); }
 #endif
+
+    virtual void didAttachInspector(WebKit::WebPageProxy&, WebKit::WebInspectorProxy&) { }
 };
 
 } // namespace API

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h (252633 => 252634)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h	2019-11-19 16:51:33 UTC (rev 252633)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h	2019-11-19 17:11:49 UTC (rev 252634)
@@ -203,7 +203,7 @@
  */
 - (void)_webView:(WKWebView *)webView shouldAllowDeviceOrientationAndMotionAccessRequestedByFrame:(WKFrameInfo *)requestingFrame decisionHandler:(void (^)(BOOL))decisionHandler WK_API_AVAILABLE(ios(13.0));
 
-#else // TARGET_OS_IPHONE
+#else // !TARGET_OS_IPHONE
 - (void)_prepareForImmediateActionAnimationForWebView:(WKWebView *)webView WK_API_AVAILABLE(macos(10.13.4));
 - (void)_cancelImmediateActionAnimationForWebView:(WKWebView *)webView WK_API_AVAILABLE(macos(10.13.4));
 - (void)_completeImmediateActionAnimationForWebView:(WKWebView *)webView WK_API_AVAILABLE(macos(10.13.4));
@@ -232,6 +232,11 @@
 - (NSMenu *)_webView:(WKWebView *)webView contextMenu:(NSMenu *)menu forElement:(_WKContextMenuElementInfo *)element userInfo:(id <NSSecureCoding>)userInfo WK_API_DEPRECATED_WITH_REPLACEMENT("_webView:getContextMenuFromProposedMenu:forElement:userInfo:completionHandler:", macos(10.12, 10.14.4));
 - (void)_webView:(WKWebView *)webView getContextMenuFromProposedMenu:(NSMenu *)menu forElement:(_WKContextMenuElementInfo *)element userInfo:(id <NSSecureCoding>)userInfo completionHandler:(void (^)(NSMenu *))completionHandler WK_API_AVAILABLE(macos(10.14));
 - (void)_webView:(WKWebView *)webView didPerformDragOperation:(BOOL)handled WK_API_AVAILABLE(macos(10.14.4));
-#endif // TARGET_OS_IPHONE
 
+/*! @abstract Called when a Web Inspector instance is attached to this WKWebView. This is not called in the case of remote inspection.
+    @param inspector The Web Inspector instance attached to this WKWebView.
+*/
+- (void)_webView:(WKWebView *)webView didAttachInspector:(_WKInspector *)inspector WK_API_AVAILABLE(macos(WK_MAC_TBA));
+#endif // !TARGET_OS_IPHONE
+
 @end

Modified: trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.h (252633 => 252634)


--- trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.h	2019-11-19 16:51:33 UTC (rev 252633)
+++ trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.h	2019-11-19 17:11:49 UTC (rev 252634)
@@ -124,6 +124,7 @@
         bool runOpenPanel(WebPageProxy&, WebFrameProxy*, WebCore::SecurityOriginData&&, API::OpenPanelParameters*, WebOpenPanelResultListenerProxy*) final;
         void didExceedBackgroundResourceLimitWhileInForeground(WebPageProxy&, WKResourceLimit) final;
         void saveDataToFileInDownloadsFolder(WebPageProxy*, const WTF::String&, const WTF::String&, const URL&, API::Data&) final;
+        void didAttachInspector(WebPageProxy&, WebInspectorProxy&) final;
 #endif
 #if ENABLE(DEVICE_ORIENTATION)
         void shouldAllowDeviceOrientationAndMotionAccess(WebKit::WebPageProxy&, WebFrameProxy&, WebCore::SecurityOriginData&&, CompletionHandler<void(bool)>&&) final;
@@ -198,6 +199,7 @@
         bool webViewSaveDataToFileSuggestedFilenameMimeTypeOriginatingURL : 1;
         bool webViewRunOpenPanelWithParametersInitiatedByFrameCompletionHandler : 1;
         bool webViewRequestNotificationPermissionForSecurityOriginDecisionHandler : 1;
+        bool webViewDidAttachInspector : 1;
 #endif
 #if ENABLE(DEVICE_ORIENTATION)
         bool webViewShouldAllowDeviceOrientationAndMotionAccessRequestedByFrameDecisionHandler : 1;

Modified: trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.mm (252633 => 252634)


--- trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.mm	2019-11-19 16:51:33 UTC (rev 252633)
+++ trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.mm	2019-11-19 17:11:49 UTC (rev 252634)
@@ -47,11 +47,13 @@
 #import "WKWebViewInternal.h"
 #import "WKWindowFeaturesInternal.h"
 #import "WebEventFactory.h"
+#import "WebInspectorProxy.h"
 #import "WebOpenPanelResultListenerProxy.h"
 #import "WebProcessProxy.h"
 #import "_WKContextMenuElementInfo.h"
 #import "_WKFrameHandleInternal.h"
 #import "_WKHitTestResultInternal.h"
+#import "_WKInspectorInternal.h"
 #import "_WKWebAuthenticationPanelInternal.h"
 #import <WebCore/FontAttributes.h>
 #import <WebCore/SecurityOriginData.h>
@@ -131,6 +133,7 @@
     m_delegateMethods.webViewSaveDataToFileSuggestedFilenameMimeTypeOriginatingURL = [delegate respondsToSelector:@selector(_webView:saveDataToFile:suggestedFilename:mimeType:originatingURL:)];
     m_delegateMethods.webViewRunOpenPanelWithParametersInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:)];
     m_delegateMethods.webViewRequestNotificationPermissionForSecurityOriginDecisionHandler = [delegate respondsToSelector:@selector(_webView:requestNotificationPermissionForSecurityOrigin:decisionHandler:)];
+    m_delegateMethods.webViewDidAttachInspector = [delegate respondsToSelector:@selector(_webView:didAttachInspector:)];
 #endif
 #if ENABLE(DEVICE_ORIENTATION)
     m_delegateMethods.webViewShouldAllowDeviceOrientationAndMotionAccessRequestedByFrameDecisionHandler = [delegate respondsToSelector:@selector(_webView:shouldAllowDeviceOrientationAndMotionAccessRequestedByFrame:decisionHandler:)];
@@ -845,6 +848,18 @@
 
     return true;
 }
+
+void UIDelegate::UIClient::didAttachInspector(WebKit::WebPageProxy&, WebKit::WebInspectorProxy& inspector)
+{
+    if (!m_uiDelegate.m_delegateMethods.webViewDidAttachInspector)
+        return;
+
+    auto delegate = m_uiDelegate.m_delegate.get();
+    if (!delegate)
+        return;
+
+    [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView didAttachInspector:wrapper(inspector)];
+}
 #endif
 
 #if ENABLE(DEVICE_ORIENTATION)

Modified: trunk/Source/WebKit/UIProcess/WebInspectorProxy.cpp (252633 => 252634)


--- trunk/Source/WebKit/UIProcess/WebInspectorProxy.cpp	2019-11-19 16:51:33 UTC (rev 252633)
+++ trunk/Source/WebKit/UIProcess/WebInspectorProxy.cpp	2019-11-19 17:11:49 UTC (rev 252634)
@@ -29,6 +29,7 @@
 
 #include "APINavigation.h"
 #include "APIProcessPoolConfiguration.h"
+#include "APIUIClient.h"
 #include "WebAutomationSession.h"
 #include "WebFrameProxy.h"
 #include "WebInspectorInterruptDispatcherMessages.h"
@@ -444,6 +445,13 @@
         m_inspectorPage->process().send(Messages::WebInspectorUI::SetDockingUnavailable(!m_canAttach), m_inspectorPage->webPageID());
     }
 
+    // Notify WebKit client when a local inspector attaches so that it may install delegates prior to the _WKInspector loading its frontend.
+    m_inspectedPage->uiClient().didAttachInspector(*m_inspectedPage, *this);
+
+    // Bail out if the client closed the inspector from the delegate method.
+    if (!m_inspectorPage)
+        return;
+
     m_inspectorPage->loadRequest(URL(URL(), m_underTest ? WebInspectorProxy::inspectorTestPageURL() : WebInspectorProxy::inspectorPageURL()));
 }
 

Modified: trunk/Tools/ChangeLog (252633 => 252634)


--- trunk/Tools/ChangeLog	2019-11-19 16:51:33 UTC (rev 252633)
+++ trunk/Tools/ChangeLog	2019-11-19 17:11:49 UTC (rev 252634)
@@ -1,3 +1,15 @@
+2019-11-19  Brian Burg  <[email protected]>
+
+        [Cocoa] Add WKUIDelegate SPI to inform clients when a _WKInspector attaches to a WKWebView
+        https://bugs.webkit.org/show_bug.cgi?id=204300
+        <rdar://problem/57136993>
+
+        Reviewed by Devin Rousso.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/UIDelegate.mm:
+        (-[InspectorDelegate _webView:didAttachInspector:]):
+        (TEST): Add new test to ensure the delegate is called as expected.
+
 2019-11-19  Sihui Liu  <[email protected]>
 
         Update expectations for bufferedAmount-unchanged-by-sync-xhr.any.worker.html

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/UIDelegate.mm (252633 => 252634)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/UIDelegate.mm	2019-11-19 16:51:33 UTC (rev 252633)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/UIDelegate.mm	2019-11-19 17:11:49 UTC (rev 252634)
@@ -38,6 +38,7 @@
 #import <WebKit/WKUIDelegatePrivate.h>
 #import <WebKit/WKWebViewPrivate.h>
 #import <WebKit/_WKHitTestResult.h>
+#import <WebKit/_WKInspector.h>
 #import <wtf/RetainPtr.h>
 #import <wtf/Vector.h>
 
@@ -899,4 +900,34 @@
 
 #endif // RELIABLE_DID_NOT_HANDLE_WHEEL_EVENT
 
+@interface InspectorDelegate : NSObject <WKUIDelegatePrivate>
+@end
+
+@implementation InspectorDelegate
+
+- (void)_webView:(WKWebView *)webView didAttachInspector:(_WKInspector *)inspector
+{
+    EXPECT_EQ(webView._inspector, inspector);
+    EXPECT_TRUE(webView._hasInspectorFrontend);
+    [inspector close];
+    done = true;
+}
+
+@end
+
+TEST(WebKit, DidNotifyWhenInspectorAttached)
+{
+    auto webViewConfiguration = adoptNS([WKWebViewConfiguration new]);
+    webViewConfiguration.get().preferences._developerExtrasEnabled = YES;
+    auto webView = adoptNS([[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:webViewConfiguration.get()]);
+    auto delegate = adoptNS([InspectorDelegate new]);
+    [webView setUIDelegate:delegate.get()];
+    [webView loadHTMLString:@"<head><title>Test page to be inspected</title></head><body><p>Filler content</p></body>" baseURL:[NSURL URLWithString:@"http://example.com/"]];
+
+    EXPECT_FALSE(webView.get()._hasInspectorFrontend);
+
+    [[webView _inspector] show];
+    TestWebKitAPI::Util::run(&done);
+}
+
 #endif // PLATFORM(MAC)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to