Add WKUIDelegatePrivate callbacks corresponding to WKPageUIClient's takeFocus, focus...
authorachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 23 Aug 2017 20:35:49 +0000 (20:35 +0000)
committerachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 23 Aug 2017 20:35:49 +0000 (20:35 +0000)
https://bugs.webkit.org/show_bug.cgi?id=175896

Reviewed by Tim Horton.
Source/WebKit:

I also renamed _webViewShow to _showWebView based on feedback from https://bugs.webkit.org/show_bug.cgi?id=175797

Added an API test.

* UIProcess/API/Cocoa/WKUIDelegatePrivate.h:
* UIProcess/Cocoa/UIDelegate.h:
* UIProcess/Cocoa/UIDelegate.mm:
(WebKit::UIDelegate::setDelegate):
(WebKit::toWKFocusDirection):
(WebKit::UIDelegate::UIClient::takeFocus):
(WebKit::UIDelegate::UIClient::focus):
(WebKit::UIDelegate::UIClient::unfocus):
(WebKit::UIDelegate::UIClient::showPage):

Tools:

* TestWebKitAPI/Tests/WebKit2Cocoa/UIDelegate.mm:
(-[UITestDelegate _showWebView:]):
(TEST):
(tabEvent):
(synthesizeTab):
(-[FocusDelegate _webView:takeFocus:]):
(-[FocusDelegate webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:]):
(-[UITestDelegate _webViewShow:]): Deleted.

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

Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h
Source/WebKit/UIProcess/Cocoa/UIDelegate.h
Source/WebKit/UIProcess/Cocoa/UIDelegate.mm
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKit2Cocoa/UIDelegate.mm

index 86f9e6f..d93f87c 100644 (file)
@@ -1,3 +1,24 @@
+2017-08-23  Alex Christensen  <achristensen@webkit.org>
+
+        Add WKUIDelegatePrivate callbacks corresponding to WKPageUIClient's takeFocus, focus, and unfocus
+        https://bugs.webkit.org/show_bug.cgi?id=175896
+
+        Reviewed by Tim Horton.
+        
+        I also renamed _webViewShow to _showWebView based on feedback from https://bugs.webkit.org/show_bug.cgi?id=175797
+
+        Added an API test.
+
+        * UIProcess/API/Cocoa/WKUIDelegatePrivate.h:
+        * UIProcess/Cocoa/UIDelegate.h:
+        * UIProcess/Cocoa/UIDelegate.mm:
+        (WebKit::UIDelegate::setDelegate):
+        (WebKit::toWKFocusDirection):
+        (WebKit::UIDelegate::UIClient::takeFocus):
+        (WebKit::UIDelegate::UIClient::focus):
+        (WebKit::UIDelegate::UIClient::unfocus):
+        (WebKit::UIDelegate::UIClient::showPage):
+
 2017-08-23  Youenn Fablet  <youenn@apple.com>
 
         [CacheAPI] Introduce a WebKit::CacheStorage namespace
index 63fed87..5dce93d 100644 (file)
 @class UITargetedDragPreview;
 @protocol UIDragSession;
 @protocol UIDropSession;
+#else
+typedef NS_ENUM(NSInteger, _WKFocusDirection) {
+    WKFocusDirectionBackward,
+    WKFocusDirectionForward,
+} WK_API_AVAILABLE(macosx(WK_MAC_TBA));
 #endif
 
 @protocol WKUIDelegatePrivate <WKUIDelegate>
@@ -120,7 +125,10 @@ struct UIEdgeInsets;
 #endif
 - (void)_webView:(WKWebView *)webView didChangeSafeAreaShouldAffectObscuredInsets:(BOOL)safeAreaShouldAffectObscuredInsets WK_API_AVAILABLE(ios(WK_IOS_TBA));
 #else
-- (void)_webViewShow:(WKWebView *)webView WK_API_AVAILABLE(macosx(WK_MAC_TBA));
+- (void)_showWebView:(WKWebView *)webView WK_API_AVAILABLE(macosx(WK_MAC_TBA));
+- (void)_focusWebView:(WKWebView *)webView WK_API_AVAILABLE(macosx(WK_MAC_TBA));
+- (void)_unfocusWebView:(WKWebView *)webView WK_API_AVAILABLE(macosx(WK_MAC_TBA));
+- (void)_webView:(WKWebView *)webView takeFocus:(_WKFocusDirection)direction WK_API_AVAILABLE(macosx(WK_MAC_TBA));
 - (NSMenu *)_webView:(WKWebView *)webView contextMenu:(NSMenu *)menu forElement:(_WKContextMenuElementInfo *)element WK_API_AVAILABLE(macosx(10.12));
 - (NSMenu *)_webView:(WKWebView *)webView contextMenu:(NSMenu *)menu forElement:(_WKContextMenuElementInfo *)element userInfo:(id <NSSecureCoding>)userInfo WK_API_AVAILABLE(macosx(10.12));
 #endif
index eea7d6d..7122f3b 100644 (file)
@@ -97,6 +97,9 @@ private:
         void reachedApplicationCacheOriginQuota(WebPageProxy*, const WebCore::SecurityOrigin&, uint64_t currentQuota, uint64_t totalBytesNeeded, Function<void (unsigned long long)>&& completionHandler) override;
 #if PLATFORM(MAC)
         void showPage(WebPageProxy*) final;
+        void takeFocus(WebKit::WebPageProxy*, WKFocusDirection) final;
+        void focus(WebKit::WebPageProxy*) final;
+        void unfocus(WebKit::WebPageProxy*) final;
         bool runOpenPanel(WebPageProxy*, WebFrameProxy*, const WebCore::SecurityOriginData&, API::OpenPanelParameters*, WebOpenPanelResultListenerProxy*) override;
 #endif
         bool decidePolicyForUserMediaPermissionRequest(WebKit::WebPageProxy&, WebKit::WebFrameProxy&, API::SecurityOrigin&, API::SecurityOrigin&, WebKit::UserMediaPermissionRequestProxy&) override;
@@ -132,12 +135,15 @@ private:
     struct {
         bool webViewCreateWebViewWithConfigurationForNavigationActionWindowFeatures : 1;
         bool webViewCreateWebViewWithConfigurationForNavigationActionWindowFeaturesAsync : 1;
-        bool webViewShow : 1;
         bool webViewRunJavaScriptAlertPanelWithMessageInitiatedByFrameCompletionHandler : 1;
         bool webViewRunJavaScriptConfirmPanelWithMessageInitiatedByFrameCompletionHandler : 1;
         bool webViewRunJavaScriptTextInputPanelWithPromptDefaultTextInitiatedByFrameCompletionHandler : 1;
         bool webViewRunBeforeUnloadConfirmPanelWithMessageInitiatedByFrameCompletionHandler : 1;
 #if PLATFORM(MAC)
+        bool showWebView : 1;
+        bool focusWebView : 1;
+        bool unfocusWebView : 1;
+        bool webViewTakeFocus : 1;
         bool webViewRunOpenPanelWithParametersInitiatedByFrameCompletionHandler : 1;
 #endif
         bool webViewDecideDatabaseQuotaForSecurityOriginCurrentQuotaCurrentOriginUsageCurrentDatabaseUsageExpectedUsageDecisionHandler : 1;
index 2eb7f0e..279a259 100644 (file)
@@ -100,7 +100,10 @@ void UIDelegate::setDelegate(id <WKUIDelegate> delegate)
     m_delegateMethods.webViewRunBeforeUnloadConfirmPanelWithMessageInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(_webView:runBeforeUnloadConfirmPanelWithMessage:initiatedByFrame:completionHandler:)];
 
 #if PLATFORM(MAC)
-    m_delegateMethods.webViewShow = [delegate respondsToSelector:@selector(_webViewShow:)];
+    m_delegateMethods.showWebView = [delegate respondsToSelector:@selector(_showWebView:)];
+    m_delegateMethods.focusWebView = [delegate respondsToSelector:@selector(_focusWebView:)];
+    m_delegateMethods.unfocusWebView = [delegate respondsToSelector:@selector(_unfocusWebView:)];
+    m_delegateMethods.webViewTakeFocus = [delegate respondsToSelector:@selector(_webView:takeFocus:)];
     m_delegateMethods.webViewRunOpenPanelWithParametersInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:)];
 #endif
 
@@ -372,16 +375,64 @@ void UIDelegate::UIClient::exceededDatabaseQuota(WebPageProxy*, WebFrameProxy*,
 }
 
 #if PLATFORM(MAC)
+static inline _WKFocusDirection toWKFocusDirection(WKFocusDirection direction)
+{
+    switch (direction) {
+    case kWKFocusDirectionBackward:
+        return WKFocusDirectionBackward;
+    case kWKFocusDirectionForward:
+        return WKFocusDirectionForward;
+    }
+    ASSERT_NOT_REACHED();
+    return WKFocusDirectionForward;
+}
+
+void UIDelegate::UIClient::takeFocus(WebKit::WebPageProxy*, WKFocusDirection direction)
+{
+    if (!m_uiDelegate.m_delegateMethods.webViewTakeFocus)
+        return;
+    
+    auto delegate = m_uiDelegate.m_delegate.get();
+    if (!delegate)
+        return;
+    
+    [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView takeFocus:toWKFocusDirection(direction)];
+}
+
+void UIDelegate::UIClient::focus(WebKit::WebPageProxy*)
+{
+    if (!m_uiDelegate.m_delegateMethods.focusWebView)
+        return;
+    
+    auto delegate = m_uiDelegate.m_delegate.get();
+    if (!delegate)
+        return;
+    
+    [(id <WKUIDelegatePrivate>)delegate _focusWebView:m_uiDelegate.m_webView];
+}
+
+void UIDelegate::UIClient::unfocus(WebKit::WebPageProxy*)
+{
+    if (!m_uiDelegate.m_delegateMethods.unfocusWebView)
+        return;
+    
+    auto delegate = m_uiDelegate.m_delegate.get();
+    if (!delegate)
+        return;
+    
+    [(id <WKUIDelegatePrivate>)delegate _unfocusWebView:m_uiDelegate.m_webView];
+}
+
 void UIDelegate::UIClient::showPage(WebPageProxy*)
 {
-    if (!m_uiDelegate.m_delegateMethods.webViewShow)
+    if (!m_uiDelegate.m_delegateMethods.showWebView)
         return;
 
     auto delegate = m_uiDelegate.m_delegate.get();
     if (!delegate)
         return;
     
-    [(id <WKUIDelegatePrivate>)delegate _webViewShow:m_uiDelegate.m_webView];
+    [(id <WKUIDelegatePrivate>)delegate _showWebView:m_uiDelegate.m_webView];
 }
 
 bool UIDelegate::UIClient::runOpenPanel(WebPageProxy*, WebFrameProxy* webFrameProxy, const WebCore::SecurityOriginData& securityOriginData, API::OpenPanelParameters* openPanelParameters, WebOpenPanelResultListenerProxy* listener)
index e995b9e..1ec5607 100644 (file)
@@ -1,3 +1,19 @@
+2017-08-23  Alex Christensen  <achristensen@webkit.org>
+
+        Add WKUIDelegatePrivate callbacks corresponding to WKPageUIClient's takeFocus, focus, and unfocus
+        https://bugs.webkit.org/show_bug.cgi?id=175896
+
+        Reviewed by Tim Horton.
+
+        * TestWebKitAPI/Tests/WebKit2Cocoa/UIDelegate.mm:
+        (-[UITestDelegate _showWebView:]):
+        (TEST):
+        (tabEvent):
+        (synthesizeTab):
+        (-[FocusDelegate _webView:takeFocus:]):
+        (-[FocusDelegate webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:]):
+        (-[UITestDelegate _webViewShow:]): Deleted.
+
 2017-08-23  Eric Carlson  <eric.carlson@apple.com>
 
         Platform code should be able to safely log in release builds
index 68a3b22..3a5c3b1 100644 (file)
 
 #import "TestWKWebView.h"
 #import "Utilities.h"
+#import <Carbon/Carbon.h>
 #import <WebKit/WKUIDelegatePrivate.h>
 #import <WebKit/WKWebView.h>
 #import <wtf/RetainPtr.h>
+#import <wtf/mac/AppKitCompatibilityDeclarations.h>
 
 @class UITestDelegate;
 
@@ -54,7 +56,7 @@ static bool done;
     return createdWebView.get();
 }
 
-- (void)_webViewShow:(WKWebView *)webView
+- (void)_showWebView:(WKWebView *)webView
 {
     webViewFromDelegateCallback = webView;
     done = true;
@@ -74,7 +76,7 @@ static bool done;
 
 @end
 
-TEST(WebKit2, ShowPage)
+TEST(WebKit2, ShowWebView)
 {
     delegate = adoptNS([[UITestDelegate alloc] init]);
     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
@@ -87,6 +89,53 @@ TEST(WebKit2, ShowPage)
     ASSERT_EQ(webViewFromDelegateCallback, createdWebView);
 }
 
+static NSEvent *tabEvent(NSWindow *window, NSEventType type, NSEventModifierFlags flags)
+{
+    return [NSEvent keyEventWithType:type location:NSMakePoint(5, 5) modifierFlags:flags timestamp:GetCurrentEventTime() windowNumber:[window windowNumber] context:[NSGraphicsContext currentContext] characters:@"\t" charactersIgnoringModifiers:@"\t" isARepeat:NO keyCode:0];
+}
+
+static void synthesizeTab(NSWindow *window, NSView *view, bool withShiftDown)
+{
+    [view keyDown:tabEvent(window, NSEventTypeKeyDown, withShiftDown ? NSEventModifierFlagShift : 0)];
+    [view keyUp:tabEvent(window, NSEventTypeKeyUp, withShiftDown ? NSEventModifierFlagShift : 0)];
+}
+
+static RetainPtr<NSWindow> window;
+static _WKFocusDirection takenDirection;
+
+@interface FocusDelegate : NSObject <WKUIDelegatePrivate>
+@end
+
+@implementation FocusDelegate
+
+- (void)_webView:(WKWebView *)webView takeFocus:(_WKFocusDirection)direction
+{
+    takenDirection = direction;
+    done = true;
+}
+
+- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
+{
+    completionHandler();
+    synthesizeTab(window.get(), webView, true);
+}
+
+@end
+
+TEST(WebKit2, Focus)
+{
+    window = adoptNS([[NSWindow alloc] initWithContentRect:CGRectMake(0, 0, 800, 600) styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:YES]);
+    auto webView = adoptNS([[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600)]);
+    [[window contentView] addSubview:webView.get()];
+    auto delegate = adoptNS([[FocusDelegate alloc] init]);
+    [webView setUIDelegate:delegate.get()];
+    NSString *html = @"<script>function loaded() { document.getElementById('in').focus(); alert('ready'); }</script>"
+    "<body onload='loaded()'><input type='text' id='in'></body>";
+    [webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://example.com/"]];
+    TestWebKitAPI::Util::run(&done);
+    ASSERT_EQ(takenDirection, WKFocusDirectionBackward);
+}
+
 #endif // PLATFORM(MAC)
 
 #endif // WK_API_ENABLED