Title: [209775] trunk
Revision
209775
Author
[email protected]
Date
2016-12-13 14:08:49 -0800 (Tue, 13 Dec 2016)

Log Message

[Cocoa] Implement -shouldInsertText: on WKWebProcessPlugInEditingDelegate
https://bugs.webkit.org/show_bug.cgi?id=165785
<rdar://problem/26929398>

Reviewed by Darin Adler.

Source/WebKit2:

* WebProcess/InjectedBundle/API/Cocoa/WKWebProcessPlugInEditingDelegate.h: Defined
WKEditorInsertAction and declared
-_webProcessPlugInBrowserContextController:shouldInsertText:replacingRange:givenAction:.
* WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm:
(toWK): Added. Mapped a WebCore::EditorInsertAction to a WKEditorInsertAction.
(-[WKWebProcessPlugInBrowserContextController _setEditingDelegate:]): Implemented
EditorClient::shouldInsertText() by calling
-_webProcessPlugInBrowserContextController:shouldInsertText:replacingRange:givenAction: with
the text, wrapped range handle, and action. Added shouldInsertText to DelegateMethods and
initialized it according to whether the delegate responds to shouldInsertText:.

Tools:

* TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegate.mm:
(-[BundleEditingDelegateRemoteObject shouldInsertText:replacingRange:givenAction:]): Added
expectations for the text, range, and action.
(TEST): Set the "EditingDelegateShouldInsertText" bundle parameter to NO so that the plug-in
will return NO in -_webProcessPlugInBrowserContextController:willInsertText:. Added a
plain-text string to the pasteboard, executed a paste action, and verified that the selected
text in the web view did not change.
* TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegatePlugIn.mm:
(-[BundleEditingDelegatePlugIn webProcessPlugIn:didCreateBrowserContextController:]): Set
_editingDelegateShouldInsertText according to the "EditingDelegateShouldInsertText" bundle
parameter.
(-[BundleEditingDelegatePlugIn _webProcessPlugInBrowserContextController:shouldInsertText:replacingRange:givenAction:]):
Added. Called -shouldInsertText:replacingRange:givenAction: on the remote object proxy
and returned the value of _editingDelegateShouldInsertText.
* TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegateProtocol.h: Declared
-shouldInsertText:replacingRange:givenAction:.

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (209774 => 209775)


--- trunk/Source/WebKit2/ChangeLog	2016-12-13 22:05:00 UTC (rev 209774)
+++ trunk/Source/WebKit2/ChangeLog	2016-12-13 22:08:49 UTC (rev 209775)
@@ -1,3 +1,22 @@
+2016-12-13  Andy Estes  <[email protected]>
+
+        [Cocoa] Implement -shouldInsertText: on WKWebProcessPlugInEditingDelegate
+        https://bugs.webkit.org/show_bug.cgi?id=165785
+        <rdar://problem/26929398>
+
+        Reviewed by Darin Adler.
+
+        * WebProcess/InjectedBundle/API/Cocoa/WKWebProcessPlugInEditingDelegate.h: Defined
+        WKEditorInsertAction and declared
+        -_webProcessPlugInBrowserContextController:shouldInsertText:replacingRange:givenAction:.
+        * WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm:
+        (toWK): Added. Mapped a WebCore::EditorInsertAction to a WKEditorInsertAction.
+        (-[WKWebProcessPlugInBrowserContextController _setEditingDelegate:]): Implemented
+        EditorClient::shouldInsertText() by calling
+        -_webProcessPlugInBrowserContextController:shouldInsertText:replacingRange:givenAction: with
+        the text, wrapped range handle, and action. Added shouldInsertText to DelegateMethods and
+        initialized it according to whether the delegate responds to shouldInsertText:.
+
 2016-12-13  Joseph Pecoraro  <[email protected]>
 
         NSArray leaks seen in Safari, allocated under WKIconDatabaseTryCopyCGImageArrayForURL

Modified: trunk/Source/WebKit2/WebProcess/InjectedBundle/API/Cocoa/WKWebProcessPlugInEditingDelegate.h (209774 => 209775)


--- trunk/Source/WebKit2/WebProcess/InjectedBundle/API/Cocoa/WKWebProcessPlugInEditingDelegate.h	2016-12-13 22:05:00 UTC (rev 209774)
+++ trunk/Source/WebKit2/WebProcess/InjectedBundle/API/Cocoa/WKWebProcessPlugInEditingDelegate.h	2016-12-13 22:08:49 UTC (rev 209775)
@@ -32,11 +32,18 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
+typedef NS_ENUM(NSInteger, WKEditorInsertAction) {
+    WKEditorInsertActionTyped,
+    WKEditorInsertActionPasted,
+    WKEditorInsertActionDropped,
+} WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
+
 WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA))
 @protocol WKWebProcessPlugInEditingDelegate <NSObject>
 
 @optional
 
+- (BOOL)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller shouldInsertText:(NSString *)text replacingRange:(WKWebProcessPlugInRangeHandle *)range givenAction:(WKEditorInsertAction)action;
 - (void)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller willWriteRangeToPasteboard:(WKWebProcessPlugInRangeHandle *)range;
 - (NSDictionary<NSString *, NSData *> *)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller pasteboardDataForRange:(WKWebProcessPlugInRangeHandle *)range;
 - (void)_webProcessPlugInBrowserContextControllerDidWriteToPasteboard:(WKWebProcessPlugInBrowserContextController *)controller;

Modified: trunk/Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm (209774 => 209775)


--- trunk/Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm	2016-12-13 22:05:00 UTC (rev 209774)
+++ trunk/Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm	2016-12-13 22:08:49 UTC (rev 209775)
@@ -573,6 +573,18 @@
     return _editingDelegate.getAutoreleased();
 }
 
+static inline WKEditorInsertAction toWK(EditorInsertAction action)
+{
+    switch (action) {
+    case EditorInsertActionTyped:
+        return WKEditorInsertActionTyped;
+    case EditorInsertActionPasted:
+        return WKEditorInsertActionPasted;
+    case EditorInsertActionDropped:
+        return WKEditorInsertActionDropped;
+    }
+}
+
 - (void)_setEditingDelegate:(id <WKWebProcessPlugInEditingDelegate>)editingDelegate
 {
     _editingDelegate = editingDelegate;
@@ -586,6 +598,14 @@
         }
 
     private:
+        bool shouldInsertText(WebPage&, StringImpl* text, Range* rangeToReplace, EditorInsertAction action) final
+        {
+            if (!m_delegateMethods.shouldInsertText)
+                return true;
+
+            return [m_controller->_editingDelegate.get() _webProcessPlugInBrowserContextController:m_controller shouldInsertText:String(text) replacingRange:wrapper(*InjectedBundleRangeHandle::getOrCreate(rangeToReplace)) givenAction:toWK(action)];
+        }
+
         void willWriteToPasteboard(WebKit::WebPage&, WebCore::Range* range) final
         {
             if (!m_delegateMethods.willWriteToPasteboard)
@@ -617,12 +637,14 @@
         WKWebProcessPlugInBrowserContextController *m_controller;
         const struct DelegateMethods {
             DelegateMethods(RetainPtr<id <WKWebProcessPlugInEditingDelegate>> delegate)
-                : willWriteToPasteboard([delegate respondsToSelector:@selector(_webProcessPlugInBrowserContextController:willWriteRangeToPasteboard:)])
+                : shouldInsertText([delegate respondsToSelector:@selector(_webProcessPlugInBrowserContextController:shouldInsertText:replacingRange:givenAction:)])
+                , willWriteToPasteboard([delegate respondsToSelector:@selector(_webProcessPlugInBrowserContextController:willWriteRangeToPasteboard:)])
                 , getPasteboardDataForRange([delegate respondsToSelector:@selector(_webProcessPlugInBrowserContextController:pasteboardDataForRange:)])
                 , didWriteToPasteboard([delegate respondsToSelector:@selector(_webProcessPlugInBrowserContextControllerDidWriteToPasteboard:)])
             {
             }
 
+            bool shouldInsertText;
             bool willWriteToPasteboard;
             bool getPasteboardDataForRange;
             bool didWriteToPasteboard;

Modified: trunk/Tools/ChangeLog (209774 => 209775)


--- trunk/Tools/ChangeLog	2016-12-13 22:05:00 UTC (rev 209774)
+++ trunk/Tools/ChangeLog	2016-12-13 22:08:49 UTC (rev 209775)
@@ -1,3 +1,28 @@
+2016-12-13  Andy Estes  <[email protected]>
+
+        [Cocoa] Implement -shouldInsertText: on WKWebProcessPlugInEditingDelegate
+        https://bugs.webkit.org/show_bug.cgi?id=165785
+        <rdar://problem/26929398>
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegate.mm:
+        (-[BundleEditingDelegateRemoteObject shouldInsertText:replacingRange:givenAction:]): Added
+        expectations for the text, range, and action.
+        (TEST): Set the "EditingDelegateShouldInsertText" bundle parameter to NO so that the plug-in
+        will return NO in -_webProcessPlugInBrowserContextController:willInsertText:. Added a
+        plain-text string to the pasteboard, executed a paste action, and verified that the selected
+        text in the web view did not change.
+        * TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegatePlugIn.mm:
+        (-[BundleEditingDelegatePlugIn webProcessPlugIn:didCreateBrowserContextController:]): Set
+        _editingDelegateShouldInsertText according to the "EditingDelegateShouldInsertText" bundle
+        parameter.
+        (-[BundleEditingDelegatePlugIn _webProcessPlugInBrowserContextController:shouldInsertText:replacingRange:givenAction:]):
+        Added. Called -shouldInsertText:replacingRange:givenAction: on the remote object proxy
+        and returned the value of _editingDelegateShouldInsertText.
+        * TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegateProtocol.h: Declared
+        -shouldInsertText:replacingRange:givenAction:.
+
 2016-12-13  Chris Dumez  <[email protected]>
 
         Unreviewed, rolling out r209544.

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegate.mm (209774 => 209775)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegate.mm	2016-12-13 22:05:00 UTC (rev 209774)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegate.mm	2016-12-13 22:08:49 UTC (rev 209775)
@@ -35,11 +35,13 @@
 #import "Test.h"
 #import "TestNavigationDelegate.h"
 #import "WKWebViewConfigurationExtras.h"
+#import <WebKit/WKProcessPoolPrivate.h>
 #import <WebKit/WKWebViewPrivate.h>
 #import <WebKit/_WKRemoteObjectInterface.h>
 #import <WebKit/_WKRemoteObjectRegistry.h>
 #import <wtf/RetainPtr.h>
 
+static bool shouldInsertTextCalled;
 static bool willWriteToPasteboard;
 static bool didWriteToPasteboard;
 
@@ -48,6 +50,14 @@
 
 @implementation BundleEditingDelegateRemoteObject
 
+- (void)shouldInsertText:(NSString *)text replacingRange:(NSString *)rangeAsString givenAction:(WKEditorInsertAction)action
+{
+    EXPECT_WK_STREQ("hello", text);
+    EXPECT_WK_STREQ("something", rangeAsString);
+    EXPECT_EQ(WKEditorInsertActionPasted, action);
+    shouldInsertTextCalled = true;
+}
+
 - (void)willWriteToPasteboard:(NSString *)rangeAsString
 {
     willWriteToPasteboard = true;
@@ -64,10 +74,11 @@
 TEST(WebKit2, WKWebProcessPlugInEditingDelegate)
 {
     RetainPtr<WKWebViewConfiguration> configuration = retainPtr([WKWebViewConfiguration testwebkitapi_configurationWithTestPlugInClassName:@"BundleEditingDelegatePlugIn"]);
+    [[configuration processPool] _setObject:[NSNumber numberWithBool:NO] forBundleParameter:@"EditingDelegateShouldInsertText"];
     
     RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
 
-    [webView loadHTMLString:@"Just something to copy <script> var textNode = document.body.firstChild; document.getSelection().setBaseAndExtent(textNode, 5, textNode, 14) </script>" baseURL:nil];
+    [webView loadHTMLString:@"<body style='-webkit-user-modify: read-write-plaintext-only'>Just something to copy <script> var textNode = document.body.firstChild; document.getSelection().setBaseAndExtent(textNode, 5, textNode, 14) </script>" baseURL:nil];
     [webView _test_waitForDidFinishNavigation];
 
     RetainPtr<BundleEditingDelegateRemoteObject> object = adoptNS([[BundleEditingDelegateRemoteObject alloc] init]);
@@ -77,11 +88,33 @@
     [webView performSelector:@selector(copy:) withObject:nil];
 
     TestWebKitAPI::Util::run(&didWriteToPasteboard);
+
+    NSString *copiedString = nil;
 #if PLATFORM(MAC)
-    EXPECT_STREQ("hello", [[NSPasteboard generalPasteboard] stringForType:@"org.webkit.data"].UTF8String);
+    copiedString = [[NSPasteboard generalPasteboard] stringForType:@"org.webkit.data"];
 #elif PLATFORM(IOS)
-    EXPECT_TRUE([[[UIPasteboard generalPasteboard] dataForPasteboardType:@"org.webkit.data"] isEqual:[@"hello" dataUsingEncoding:NSUTF8StringEncoding]]);
+    copiedString = [[[NSString alloc] initWithData:[[UIPasteboard generalPasteboard] dataForPasteboardType:@"org.webkit.data"] encoding:NSUTF8StringEncoding] autorelease];
 #endif
+    EXPECT_WK_STREQ("hello", copiedString);
+
+#if PLATFORM(MAC)
+    [[NSPasteboard generalPasteboard] setString:copiedString forType:@"public.utf8-plain-text"];
+#elif PLATFORM(IOS)
+    [[UIPasteboard generalPasteboard] setValue:copiedString forPasteboardType:@"public.utf8-plain-text"];
+#endif
+    [webView performSelector:@selector(paste:) withObject:nil];
+
+    TestWebKitAPI::Util::run(&shouldInsertTextCalled);
+
+    __block bool doneEvaluatingJavaScript = false;
+    [webView evaluateJavaScript:@"document.body.firstChild.textContent" completionHandler:^(id _Nullable value, NSError * _Nullable error) {
+        EXPECT_NULL(error);
+        EXPECT_TRUE([value isKindOfClass:[NSString class]]);
+        EXPECT_WK_STREQ("Just something to copy ", (NSString *)value);
+        doneEvaluatingJavaScript = true;
+    }];
+
+    TestWebKitAPI::Util::run(&doneEvaluatingJavaScript);
 }
 
 #endif

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegatePlugIn.mm (209774 => 209775)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegatePlugIn.mm	2016-12-13 22:05:00 UTC (rev 209774)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegatePlugIn.mm	2016-12-13 22:08:49 UTC (rev 209775)
@@ -45,6 +45,7 @@
     RetainPtr<WKWebProcessPlugInBrowserContextController> _browserContextController;
     RetainPtr<WKWebProcessPlugInController> _plugInController;
     RetainPtr<id <BundleEditingDelegateProtocol>> _remoteObject;
+    BOOL _editingDelegateShouldInsertText;
 }
 
 - (void)webProcessPlugIn:(WKWebProcessPlugInController *)plugInController didCreateBrowserContextController:(WKWebProcessPlugInBrowserContextController *)browserContextController
@@ -54,6 +55,12 @@
     _browserContextController = browserContextController;
     _plugInController = plugInController;
 
+    if (id shouldInsertText = [plugInController.parameters valueForKey:@"EditingDelegateShouldInsertText"]) {
+        ASSERT([shouldInsertText isKindOfClass:[NSNumber class]]);
+        _editingDelegateShouldInsertText = [(NSNumber *)shouldInsertText boolValue];
+    } else
+        _editingDelegateShouldInsertText = YES;
+
     _WKRemoteObjectInterface *interface = [_WKRemoteObjectInterface remoteObjectInterfaceWithProtocol:@protocol(BundleEditingDelegateProtocol)];
     _remoteObject = [browserContextController._remoteObjectRegistry remoteObjectProxyWithInterface:interface];
 
@@ -60,6 +67,13 @@
     [_browserContextController _setEditingDelegate:self];
 }
 
+- (BOOL)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller shouldInsertText:(nonnull NSString *)text replacingRange:(nonnull WKWebProcessPlugInRangeHandle *)range givenAction:(WKEditorInsertAction)action
+{
+    JSValue *jsRange = [range.frame jsRangeForRangeHandle:range inWorld:[WKWebProcessPlugInScriptWorld normalWorld]];
+    [_remoteObject shouldInsertText:text replacingRange:[jsRange toString] givenAction:action];
+    return _editingDelegateShouldInsertText;
+}
+
 - (void)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller willWriteRangeToPasteboard:(WKWebProcessPlugInRangeHandle *)range
 {
     JSValue *jsRange = [range.frame jsRangeForRangeHandle:range inWorld:[WKWebProcessPlugInScriptWorld normalWorld]];

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegateProtocol.h (209774 => 209775)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegateProtocol.h	2016-12-13 22:05:00 UTC (rev 209774)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/BundleEditingDelegateProtocol.h	2016-12-13 22:08:49 UTC (rev 209775)
@@ -27,8 +27,11 @@
 
 #if WK_API_ENABLED
 
+#import <WebKit/WKWebProcessPlugInEditingDelegate.h>
+
 @protocol BundleEditingDelegateProtocol <NSObject>
 
+- (void)shouldInsertText:(NSString *)text replacingRange:(NSString *)rangeAsString givenAction:(WKEditorInsertAction)action;
 - (void)willWriteToPasteboard:(NSString *)rangeAsString;
 - (void)didWriteToPasteboard;
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to