c63c0a8ecf446057d52e640536fb18be824cdb2a
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKitCocoa / BundleEditingDelegatePlugIn.mm
1 /*
2  * Copyright (C) 2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27
28 #if WK_API_ENABLED
29
30 #import "BundleEditingDelegateProtocol.h"
31 #import "PlatformUtilities.h"
32 #import <WebKit/WKWebProcessPlugIn.h>
33 #import <WebKit/WKWebProcessPlugInBrowserContextControllerPrivate.h>
34 #import <WebKit/WKWebProcessPlugInEditingDelegate.h>
35 #import <WebKit/WKWebProcessPlugInFrame.h>
36 #import <WebKit/WKWebProcessPlugInScriptWorld.h>
37 #import <WebKit/_WKRemoteObjectInterface.h>
38 #import <WebKit/_WKRemoteObjectRegistry.h>
39 #import <wtf/RetainPtr.h>
40
41 @interface BundleEditingDelegatePlugIn : NSObject <WKWebProcessPlugIn, WKWebProcessPlugInEditingDelegate>
42 @end
43
44 @implementation BundleEditingDelegatePlugIn {
45     RetainPtr<WKWebProcessPlugInBrowserContextController> _browserContextController;
46     RetainPtr<WKWebProcessPlugInController> _plugInController;
47     RetainPtr<id <BundleEditingDelegateProtocol>> _remoteObject;
48     BOOL _editingDelegateShouldInsertText;
49     BOOL _shouldOverridePerformTwoStepDrop;
50 }
51
52 - (void)webProcessPlugIn:(WKWebProcessPlugInController *)plugInController didCreateBrowserContextController:(WKWebProcessPlugInBrowserContextController *)browserContextController
53 {
54     ASSERT(!_browserContextController);
55     ASSERT(!_plugInController);
56     _browserContextController = browserContextController;
57     _plugInController = plugInController;
58
59     if (id shouldInsertText = [plugInController.parameters valueForKey:@"EditingDelegateShouldInsertText"]) {
60         ASSERT([shouldInsertText isKindOfClass:[NSNumber class]]);
61         _editingDelegateShouldInsertText = [(NSNumber *)shouldInsertText boolValue];
62     } else
63         _editingDelegateShouldInsertText = YES;
64
65     _shouldOverridePerformTwoStepDrop = [[plugInController.parameters valueForKey:@"BundleOverridePerformTwoStepDrop"] boolValue];
66
67     _WKRemoteObjectInterface *interface = [_WKRemoteObjectInterface remoteObjectInterfaceWithProtocol:@protocol(BundleEditingDelegateProtocol)];
68     _remoteObject = [browserContextController._remoteObjectRegistry remoteObjectProxyWithInterface:interface];
69
70     [_browserContextController _setEditingDelegate:self];
71 }
72
73 - (BOOL)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller shouldInsertText:(nonnull NSString *)text replacingRange:(nonnull WKWebProcessPlugInRangeHandle *)range givenAction:(WKEditorInsertAction)action
74 {
75     JSValue *jsRange = [range.frame jsRangeForRangeHandle:range inWorld:[WKWebProcessPlugInScriptWorld normalWorld]];
76     [_remoteObject shouldInsertText:text replacingRange:[jsRange toString] givenAction:action];
77     return _editingDelegateShouldInsertText;
78 }
79
80 - (void)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller willWriteRangeToPasteboard:(WKWebProcessPlugInRangeHandle *)range
81 {
82     JSValue *jsRange = [range.frame jsRangeForRangeHandle:range inWorld:[WKWebProcessPlugInScriptWorld normalWorld]];
83     [_remoteObject willWriteToPasteboard:[jsRange toString]];
84 }
85
86 - (NSDictionary<NSString *, NSData *> *)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller pasteboardDataForRange:(WKWebProcessPlugInRangeHandle *)range
87 {
88     return @{ @"org.webkit.data" : [NSData dataWithBytesNoCopy:(void*)"hello" length:5 freeWhenDone:NO] };
89 }
90
91 - (void)_webProcessPlugInBrowserContextControllerDidWriteToPasteboard:(WKWebProcessPlugInBrowserContextController *)controller
92 {
93     [_remoteObject didWriteToPasteboard];
94 }
95
96 - (BOOL)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller performTwoStepDrop:(WKWebProcessPlugInNodeHandle *)fragment atDestination:(WKWebProcessPlugInRangeHandle *)destination isMove:(BOOL)isMove
97 {
98     return _shouldOverridePerformTwoStepDrop;
99 }
100
101 @end
102
103 #endif // WK_API_ENABLED