Process Swap on Navigation causes many webpages to hang due to attempted process...
[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     RetainPtr<NSDictionary<NSString *, NSString *>> _mimeTypeToReplacementURLMap;
51 }
52
53 - (void)webProcessPlugIn:(WKWebProcessPlugInController *)plugInController didCreateBrowserContextController:(WKWebProcessPlugInBrowserContextController *)browserContextController
54 {
55     ASSERT(!_browserContextController);
56     ASSERT(!_plugInController);
57     _browserContextController = browserContextController;
58     _plugInController = plugInController;
59
60     if (id shouldInsertText = [plugInController.parameters valueForKey:@"EditingDelegateShouldInsertText"]) {
61         ASSERT([shouldInsertText isKindOfClass:[NSNumber class]]);
62         _editingDelegateShouldInsertText = [(NSNumber *)shouldInsertText boolValue];
63     } else
64         _editingDelegateShouldInsertText = YES;
65
66     _shouldOverridePerformTwoStepDrop = [[plugInController.parameters valueForKey:@"BundleOverridePerformTwoStepDrop"] boolValue];
67     _mimeTypeToReplacementURLMap = [plugInController.parameters valueForKey:@"MIMETypeToReplacementURLMap"];
68
69     _WKRemoteObjectInterface *interface = [_WKRemoteObjectInterface remoteObjectInterfaceWithProtocol:@protocol(BundleEditingDelegateProtocol)];
70     _remoteObject = [browserContextController._remoteObjectRegistry remoteObjectProxyWithInterface:interface];
71
72     [_browserContextController _setEditingDelegate:self];
73 }
74
75 - (BOOL)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller shouldInsertText:(nonnull NSString *)text replacingRange:(nonnull WKWebProcessPlugInRangeHandle *)range givenAction:(WKEditorInsertAction)action
76 {
77     JSValue *jsRange = [range.frame jsRangeForRangeHandle:range inWorld:[WKWebProcessPlugInScriptWorld normalWorld]];
78     [_remoteObject shouldInsertText:text replacingRange:[jsRange toString] givenAction:action];
79     return _editingDelegateShouldInsertText;
80 }
81
82 - (void)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller willWriteRangeToPasteboard:(WKWebProcessPlugInRangeHandle *)range
83 {
84     JSValue *jsRange = [range.frame jsRangeForRangeHandle:range inWorld:[WKWebProcessPlugInScriptWorld normalWorld]];
85     [_remoteObject willWriteToPasteboard:[jsRange toString]];
86 }
87
88 - (NSDictionary<NSString *, NSData *> *)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller pasteboardDataForRange:(WKWebProcessPlugInRangeHandle *)range
89 {
90     return @{ @"org.webkit.data" : [NSData dataWithBytesNoCopy:(void*)"hello" length:5 freeWhenDone:NO] };
91 }
92
93 - (void)_webProcessPlugInBrowserContextControllerDidWriteToPasteboard:(WKWebProcessPlugInBrowserContextController *)controller
94 {
95     [_remoteObject didWriteToPasteboard];
96 }
97
98 - (BOOL)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller performTwoStepDrop:(WKWebProcessPlugInNodeHandle *)fragment atDestination:(WKWebProcessPlugInRangeHandle *)destination isMove:(BOOL)isMove
99 {
100     return _shouldOverridePerformTwoStepDrop;
101 }
102
103 - (NSString *)_webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller replacementURLForResource:(NSData *)resourceData mimeType:(NSString *)mimeType
104 {
105     assert(!!resourceData);
106     return [_mimeTypeToReplacementURLMap objectForKey:mimeType];
107 }
108
109 @end
110
111 #endif // WK_API_ENABLED