[iOS WK2] Rename and refactor DataInteractionTests and DataInteractionSimulator
[WebKit-https.git] / Tools / TestWebKitAPI / ios / DragAndDropSimulatorIOS.mm
1 /*
2  * Copyright (C) 2017 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 #include "config.h"
27 #include "DragAndDropSimulator.h"
28
29 #if ENABLE(DRAG_SUPPORT) && PLATFORM(IOS) && WK_API_ENABLED
30
31 #import "InstanceMethodSwizzler.h"
32 #import "PlatformUtilities.h"
33 #import "UIKitSPI.h"
34
35 #import <UIKit/UIDragInteraction.h>
36 #import <UIKit/UIDragItem.h>
37 #import <UIKit/UIInteraction.h>
38 #import <WebKit/WKWebViewPrivate.h>
39 #import <WebKit/_WKFocusedElementInfo.h>
40 #import <WebKit/_WKFormInputSession.h>
41 #import <wtf/RetainPtr.h>
42 #import <wtf/SoftLinking.h>
43
44 SOFT_LINK_FRAMEWORK(UIKit)
45 SOFT_LINK(UIKit, UIApplicationInstantiateSingleton, void, (Class singletonClass), (singletonClass))
46
47 using namespace TestWebKitAPI;
48
49 @implementation WKWebView (DragAndDropTesting)
50
51 - (UIView *)_dragDropInteractionView
52 {
53     return [self valueForKey:@"_currentContentView"];
54 }
55
56 - (id <UIDropInteractionDelegate>)dropInteractionDelegate
57 {
58     return (id <UIDropInteractionDelegate>)self._dragDropInteractionView;
59 }
60
61 - (id <UIDragInteractionDelegate>)dragInteractionDelegate
62 {
63     return (id <UIDragInteractionDelegate>)self._dragDropInteractionView;
64 }
65
66 - (UIDropInteraction *)dropInteraction
67 {
68     UIView *interactionView = self._dragDropInteractionView;
69     for (id <UIInteraction> interaction in interactionView.interactions) {
70         if ([interaction isKindOfClass:[UIDropInteraction class]])
71             return (UIDropInteraction *)interaction;
72     }
73     return nil;
74 }
75
76 - (UIDragInteraction *)dragInteraction
77 {
78     UIView *interactionView = self._dragDropInteractionView;
79     for (id <UIInteraction> interaction in interactionView.interactions) {
80         if ([interaction isKindOfClass:[UIDragInteraction class]])
81             return (UIDragInteraction *)interaction;
82     }
83     return nil;
84 }
85
86 @end
87
88 @implementation MockDragDropSession
89
90 - (instancetype)initWithItems:(NSArray <UIDragItem *>*)items location:(CGPoint)locationInWindow window:(UIWindow *)window allowMove:(BOOL)allowMove
91 {
92     if (self = [super init]) {
93         _mockItems = items;
94         _mockLocationInWindow = locationInWindow;
95         _window = window;
96         _allowMove = allowMove;
97     }
98     return self;
99 }
100
101 - (BOOL)allowsMoveOperation
102 {
103     return _allowMove;
104 }
105
106 - (BOOL)isRestrictedToDraggingApplication
107 {
108     return NO;
109 }
110
111 - (BOOL)hasItemsConformingToTypeIdentifiers:(NSArray<NSString *> *)typeIdentifiers
112 {
113     for (NSString *typeIdentifier in typeIdentifiers) {
114         BOOL hasItemConformingToType = NO;
115         for (UIDragItem *item in self.items)
116             hasItemConformingToType |= [[item.itemProvider registeredTypeIdentifiers] containsObject:typeIdentifier];
117         if (!hasItemConformingToType)
118             return NO;
119     }
120     return YES;
121 }
122
123 - (BOOL)canLoadObjectsOfClass:(Class<UIItemProviderReading>)aClass
124 {
125     for (UIDragItem *item in self.items) {
126         if ([item.itemProvider canLoadObjectOfClass:aClass])
127             return YES;
128     }
129     return NO;
130 }
131
132 - (BOOL)canLoadObjectsOfClasses:(NSArray<Class<UIItemProviderReading>> *)classes
133 {
134     for (Class<UIItemProviderReading> aClass in classes) {
135         BOOL canLoad = NO;
136         for (UIDragItem *item in self.items)
137             canLoad |= [item.itemProvider canLoadObjectOfClass:aClass];
138         if (!canLoad)
139             return NO;
140     }
141     return YES;
142 }
143
144 - (NSArray<UIDragItem *> *)items
145 {
146     return _mockItems.get();
147 }
148
149 - (void)setItems:(NSArray<UIDragItem *> *)items
150 {
151     _mockItems = items;
152 }
153
154 - (void)addItems:(NSArray<UIDragItem *> *)items
155 {
156     if (![items count])
157         return;
158
159     if (![_mockItems count])
160         _mockItems = items;
161     else
162         _mockItems = [_mockItems arrayByAddingObjectsFromArray:items];
163 }
164
165 - (CGPoint)locationInView:(UIView *)view
166 {
167     return [_window convertPoint:_mockLocationInWindow toView:view];
168 }
169
170 @end
171
172 @implementation MockDropSession
173
174 - (instancetype)initWithProviders:(NSArray<UIItemProvider *> *)providers location:(CGPoint)locationInWindow window:(UIWindow *)window allowMove:(BOOL)allowMove
175 {
176     auto items = adoptNS([[NSMutableArray alloc] init]);
177     for (UIItemProvider *itemProvider in providers)
178         [items addObject:[[[UIDragItem alloc] initWithItemProvider:itemProvider] autorelease]];
179
180     return [super initWithItems:items.get() location:locationInWindow window:window allowMove:allowMove];
181 }
182
183 - (BOOL)isLocal
184 {
185     return YES;
186 }
187
188 - (NSProgress *)progress
189 {
190     return [NSProgress discreteProgressWithTotalUnitCount:100];
191 }
192
193 - (void)setProgressIndicatorStyle:(UIDropSessionProgressIndicatorStyle)progressIndicatorStyle
194 {
195 }
196
197 - (UIDropSessionProgressIndicatorStyle)progressIndicatorStyle
198 {
199     return UIDropSessionProgressIndicatorStyleNone;
200 }
201
202 - (NSUInteger)operationMask
203 {
204     return 0;
205 }
206
207 - (id <UIDragSession>)localDragSession
208 {
209     return nil;
210 }
211
212 - (BOOL)hasItemsConformingToTypeIdentifier:(NSString *)typeIdentifier
213 {
214     ASSERT_NOT_REACHED();
215     return NO;
216 }
217
218 - (BOOL)canCreateItemsOfClass:(Class<UIItemProviderReading>)aClass
219 {
220     ASSERT_NOT_REACHED();
221     return NO;
222 }
223
224 - (NSProgress *)loadObjectsOfClass:(Class<NSItemProviderReading>)aClass completion:(void(^)(NSArray<__kindof id <NSItemProviderReading>> *objects))completion
225 {
226     ASSERT_NOT_REACHED();
227     return nil;
228 }
229
230 @end
231
232 @implementation MockDragSession {
233     RetainPtr<id> _localContext;
234 }
235
236 - (instancetype)initWithWindow:(UIWindow *)window allowMove:(BOOL)allowMove
237 {
238     return [super initWithItems:@[ ] location:CGPointZero window:window allowMove:allowMove];
239 }
240
241 - (NSUInteger)localOperationMask
242 {
243     ASSERT_NOT_REACHED();
244     return 0;
245 }
246
247 - (NSUInteger)externalOperationMask
248 {
249     ASSERT_NOT_REACHED();
250     return 0;
251 }
252
253 - (id)session
254 {
255     return nil;
256 }
257
258 - (id)localContext
259 {
260     return _localContext.get();
261 }
262
263 - (void)setLocalContext:(id)localContext
264 {
265     _localContext = localContext;
266 }
267
268 @end
269
270 static double progressIncrementStep = 0.033;
271 static double progressTimeStep = 0.016;
272 static NSString *TestWebKitAPISimulateCancelAllTouchesNotificationName = @"TestWebKitAPISimulateCancelAllTouchesNotificationName";
273
274 static NSArray *dragAndDropEventNames()
275 {
276     static NSArray *eventNames = nil;
277     static dispatch_once_t onceToken;
278     dispatch_once(&onceToken, ^() {
279         eventNames = @[ @"dragenter", @"dragover", @"drop", @"dragleave", @"dragstart" ];
280     });
281     return eventNames;
282 }
283
284 @interface DragAndDropSimulatorApplication : UIApplication
285 @end
286
287 @implementation DragAndDropSimulatorApplication
288 - (void)_cancelAllTouches
289 {
290     [[NSNotificationCenter defaultCenter] postNotificationName:TestWebKitAPISimulateCancelAllTouchesNotificationName object:nil];
291 }
292 @end
293
294 @implementation DragAndDropSimulator {
295     RetainPtr<TestWKWebView> _webView;
296     RetainPtr<MockDragSession> _dragSession;
297     RetainPtr<MockDropSession> _dropSession;
298     RetainPtr<NSMutableArray> _observedEventNames;
299     RetainPtr<NSArray> _externalItemProviders;
300     RetainPtr<NSArray *> _sourceItemProviders;
301     RetainPtr<NSArray *> _finalSelectionRects;
302     CGPoint _startLocation;
303     CGPoint _endLocation;
304     CGRect _lastKnownDragCaretRect;
305
306     RetainPtr<NSMutableDictionary<NSNumber *, NSValue *>>_remainingAdditionalItemRequestLocationsByProgress;
307     RetainPtr<NSMutableArray<NSValue *>>_queuedAdditionalItemRequestLocations;
308     RetainPtr<NSMutableArray<UITargetedDragPreview *>> _liftPreviews;
309
310     RetainPtr<NSMutableArray<_WKAttachment *>> _insertedAttachments;
311     RetainPtr<NSMutableArray<_WKAttachment *>> _removedAttachments;
312
313     bool _isDoneWaitingForInputSession;
314     double _currentProgress;
315     bool _isDoneWithCurrentRun;
316     DragAndDropPhase _phase;
317
318     RetainPtr<UIDropProposal> _currentDropProposal;
319 }
320
321 - (instancetype)initWithWebView:(TestWKWebView *)webView
322 {
323     if (self = [super init]) {
324         _webView = webView;
325         _shouldEnsureUIApplication = NO;
326         _shouldAllowMoveOperation = YES;
327         _isDoneWaitingForInputSession = true;
328         [_webView setUIDelegate:self];
329         [_webView _setInputDelegate:self];
330     }
331     return self;
332 }
333
334 - (void)dealloc
335 {
336     if ([_webView UIDelegate] == self)
337         [_webView setUIDelegate:nil];
338
339     if ([_webView _inputDelegate] == self)
340         [_webView _setInputDelegate:nil];
341
342     [super dealloc];
343 }
344
345 - (void)_resetSimulatedState
346 {
347     _phase = DragAndDropPhaseBeginning;
348     _currentProgress = 0;
349     _isDoneWithCurrentRun = false;
350     _observedEventNames = adoptNS([[NSMutableArray alloc] init]);
351     _insertedAttachments = adoptNS([[NSMutableArray alloc] init]);
352     _removedAttachments = adoptNS([[NSMutableArray alloc] init]);
353     _finalSelectionRects = @[ ];
354     _dragSession = nil;
355     _dropSession = nil;
356     _currentDropProposal = nil;
357     _lastKnownDragCaretRect = CGRectZero;
358     _remainingAdditionalItemRequestLocationsByProgress = nil;
359     _queuedAdditionalItemRequestLocations = adoptNS([[NSMutableArray alloc] init]);
360     _liftPreviews = adoptNS([[NSMutableArray alloc] init]);
361 }
362
363 - (NSArray *)observedEventNames
364 {
365     return _observedEventNames.get();
366 }
367
368 - (void)simulateAllTouchesCanceled:(NSNotification *)notification
369 {
370     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_advanceProgress) object:nil];
371     _phase = DragAndDropPhaseCancelled;
372     _currentProgress = 1;
373     _isDoneWithCurrentRun = true;
374     if (_dragSession)
375         [[_webView dragInteractionDelegate] dragInteraction:[_webView dragInteraction] session:_dragSession.get() didEndWithOperation:UIDropOperationCancel];
376 }
377
378 - (void)runFrom:(CGPoint)startLocation to:(CGPoint)endLocation
379 {
380     [self runFrom:startLocation to:endLocation additionalItemRequestLocations:nil];
381 }
382
383 - (void)runFrom:(CGPoint)startLocation to:(CGPoint)endLocation additionalItemRequestLocations:(ProgressToCGPointValueMap)additionalItemRequestLocations
384 {
385     NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
386     [defaultCenter addObserver:self selector:@selector(simulateAllTouchesCanceled:) name:TestWebKitAPISimulateCancelAllTouchesNotificationName object:nil];
387
388     if (_shouldEnsureUIApplication)
389         UIApplicationInstantiateSingleton([DragAndDropSimulatorApplication class]);
390
391     [self _resetSimulatedState];
392
393     if (additionalItemRequestLocations)
394         _remainingAdditionalItemRequestLocationsByProgress = adoptNS([additionalItemRequestLocations mutableCopy]);
395
396     for (NSString *eventName in dragAndDropEventNames()) {
397         [_webView performAfterReceivingMessage:eventName action:[strongSelf = retainPtr(self), name = retainPtr(eventName)] {
398             [strongSelf->_observedEventNames addObject:name.get()];
399         }];
400     }
401
402     _startLocation = startLocation;
403     _endLocation = endLocation;
404
405     if (self.externalItemProviders.count) {
406         _dropSession = adoptNS([[MockDropSession alloc] initWithProviders:self.externalItemProviders location:_startLocation window:[_webView window] allowMove:self.shouldAllowMoveOperation]);
407         _phase = DragAndDropPhaseBegan;
408         [self _advanceProgress];
409     } else {
410         _dragSession = adoptNS([[MockDragSession alloc] initWithWindow:[_webView window] allowMove:self.shouldAllowMoveOperation]);
411         [_dragSession setMockLocationInWindow:_startLocation];
412         [(id <UIDragInteractionDelegate_ForWebKitOnly>)[_webView dragInteractionDelegate] _dragInteraction:[_webView dragInteraction] prepareForSession:_dragSession.get() completion:[strongSelf = retainPtr(self)] {
413             if (strongSelf->_phase == DragAndDropPhaseCancelled)
414                 return;
415
416             strongSelf->_phase = DragAndDropPhaseBeginning;
417             [strongSelf _advanceProgress];
418         }];
419     }
420
421     Util::run(&_isDoneWithCurrentRun);
422     [_webView clearMessageHandlers:dragAndDropEventNames()];
423     _finalSelectionRects = [_webView selectionRectsAfterPresentationUpdate];
424
425     [defaultCenter removeObserver:self];
426 }
427
428 - (NSArray *)finalSelectionRects
429 {
430     return _finalSelectionRects.get();
431 }
432
433 - (void)_concludeDropAndPerformOperationIfNecessary
434 {
435     _lastKnownDragCaretRect = [_webView _dragCaretRect];
436     auto operation = [_currentDropProposal operation];
437     if (operation != UIDropOperationCancel && operation != UIDropOperationForbidden) {
438         [[_webView dropInteractionDelegate] dropInteraction:[_webView dropInteraction] performDrop:_dropSession.get()];
439         _phase = DragAndDropPhasePerformingDrop;
440     } else {
441         _isDoneWithCurrentRun = YES;
442         _phase = DragAndDropPhaseCancelled;
443     }
444
445     [[_webView dropInteractionDelegate] dropInteraction:[_webView dropInteraction] sessionDidEnd:_dropSession.get()];
446
447     if (_dragSession)
448         [[_webView dragInteractionDelegate] dragInteraction:[_webView dragInteraction] session:_dragSession.get() didEndWithOperation:operation];
449 }
450
451 - (void)_enqueuePendingAdditionalItemRequestLocations
452 {
453     NSMutableArray *progressValuesToRemove = [NSMutableArray array];
454     for (NSNumber *progressValue in _remainingAdditionalItemRequestLocationsByProgress.get()) {
455         double progress = progressValue.doubleValue;
456         if (progress > _currentProgress)
457             continue;
458         [progressValuesToRemove addObject:progressValue];
459         [_queuedAdditionalItemRequestLocations addObject:[_remainingAdditionalItemRequestLocationsByProgress objectForKey:progressValue]];
460     }
461
462     for (NSNumber *progressToRemove in progressValuesToRemove)
463         [_remainingAdditionalItemRequestLocationsByProgress removeObjectForKey:progressToRemove];
464 }
465
466 - (BOOL)_sendQueuedAdditionalItemRequest
467 {
468     if (![_queuedAdditionalItemRequestLocations count])
469         return NO;
470
471     RetainPtr<NSValue> requestLocationValue = [_queuedAdditionalItemRequestLocations objectAtIndex:0];
472     [_queuedAdditionalItemRequestLocations removeObjectAtIndex:0];
473
474     auto requestLocation = [[_webView window] convertPoint:[requestLocationValue CGPointValue] toView:_webView.get()];
475     [(id <UIDragInteractionDelegate_Proposed_SPI_33146803>)[_webView dragInteractionDelegate] _dragInteraction:[_webView dragInteraction] itemsForAddingToSession:_dragSession.get() withTouchAtPoint:requestLocation completion:[dragSession = _dragSession, dropSession = _dropSession] (NSArray *items) {
476         [dragSession addItems:items];
477         [dropSession addItems:items];
478     }];
479     return YES;
480 }
481
482 - (void)_advanceProgress
483 {
484     [self _enqueuePendingAdditionalItemRequestLocations];
485     if ([self _sendQueuedAdditionalItemRequest]) {
486         [self _scheduleAdvanceProgress];
487         return;
488     }
489
490     _lastKnownDragCaretRect = [_webView _dragCaretRect];
491     _currentProgress += progressIncrementStep;
492     CGPoint locationInWindow = self._currentLocation;
493     [_dragSession setMockLocationInWindow:locationInWindow];
494     [_dropSession setMockLocationInWindow:locationInWindow];
495
496     if (_currentProgress >= 1) {
497         _currentProgress = 1;
498         [self _concludeDropAndPerformOperationIfNecessary];
499         return;
500     }
501
502     switch (_phase) {
503     case DragAndDropPhaseBeginning: {
504         NSMutableArray<UIItemProvider *> *itemProviders = [NSMutableArray array];
505         NSArray *items = [[_webView dragInteractionDelegate] dragInteraction:[_webView dragInteraction] itemsForBeginningSession:_dragSession.get()];
506         if (!items.count) {
507             _phase = DragAndDropPhaseCancelled;
508             _currentProgress = 1;
509             _isDoneWithCurrentRun = true;
510             return;
511         }
512
513         for (UIDragItem *item in items) {
514             [itemProviders addObject:item.itemProvider];
515             UITargetedDragPreview *liftPreview = [[_webView dragInteractionDelegate] dragInteraction:[_webView dragInteraction] previewForLiftingItem:item session:_dragSession.get()];
516             EXPECT_TRUE(!!liftPreview);
517             if (liftPreview)
518                 [_liftPreviews addObject:liftPreview];
519         }
520
521         _dropSession = adoptNS([[MockDropSession alloc] initWithProviders:itemProviders location:self._currentLocation window:[_webView window] allowMove:self.shouldAllowMoveOperation]);
522         [_dragSession setItems:items];
523         _sourceItemProviders = itemProviders;
524         if (self.showCustomActionSheetBlock) {
525             // Defer progress until the custom action sheet is dismissed.
526             auto startLocationInView = [[_webView window] convertPoint:_startLocation toView:_webView.get()];
527             [_webView _simulateLongPressActionAtLocation:startLocationInView];
528             return;
529         }
530
531         [[_webView dragInteractionDelegate] dragInteraction:[_webView dragInteraction] sessionWillBegin:_dragSession.get()];
532
533         RetainPtr<WKWebView> retainedWebView = _webView;
534         dispatch_async(dispatch_get_main_queue(), ^() {
535             [retainedWebView resignFirstResponder];
536         });
537
538         _phase = DragAndDropPhaseBegan;
539         break;
540     }
541     case DragAndDropPhaseBegan:
542         [[_webView dropInteractionDelegate] dropInteraction:[_webView dropInteraction] sessionDidEnter:_dropSession.get()];
543         _phase = DragAndDropPhaseEntered;
544         break;
545     case DragAndDropPhaseEntered: {
546         _currentDropProposal = [[_webView dropInteractionDelegate] dropInteraction:[_webView dropInteraction] sessionDidUpdate:_dropSession.get()];
547         if (![self shouldAllowMoveOperation] && [_currentDropProposal operation] == UIDropOperationMove)
548             _currentDropProposal = adoptNS([[UIDropProposal alloc] initWithDropOperation:UIDropOperationCancel]);
549         break;
550     }
551     default:
552         break;
553     }
554
555     [self _scheduleAdvanceProgress];
556 }
557
558 - (CGPoint)_currentLocation
559 {
560     CGFloat distanceX = _endLocation.x - _startLocation.x;
561     CGFloat distanceY = _endLocation.y - _startLocation.y;
562     return CGPointMake(_startLocation.x + _currentProgress * distanceX, _startLocation.y + _currentProgress * distanceY);
563 }
564
565 - (void)_scheduleAdvanceProgress
566 {
567     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_advanceProgress) object:nil];
568     [self performSelector:@selector(_advanceProgress) withObject:nil afterDelay:progressTimeStep];
569 }
570
571 - (NSArray *)sourceItemProviders
572 {
573     return _sourceItemProviders.get();
574 }
575
576 - (NSArray *)externalItemProviders
577 {
578     return _externalItemProviders.get();
579 }
580
581 - (void)setExternalItemProviders:(NSArray *)externalItemProviders
582 {
583     _externalItemProviders = adoptNS([externalItemProviders copy]);
584 }
585
586 - (DragAndDropPhase)phase
587 {
588     return _phase;
589 }
590
591 - (NSArray<UITargetedDragPreview *> *)liftPreviews
592 {
593     return _liftPreviews.get();
594 }
595
596 - (CGRect)lastKnownDragCaretRect
597 {
598     return _lastKnownDragCaretRect;
599 }
600
601 - (void)waitForInputSession
602 {
603     _isDoneWaitingForInputSession = false;
604
605     // Waiting for an input session implies that we should allow input sessions to begin.
606     self.allowsFocusToStartInputSession = YES;
607
608     Util::run(&_isDoneWaitingForInputSession);
609 }
610
611 - (NSArray<_WKAttachment *> *)insertedAttachments
612 {
613     return _insertedAttachments.get();
614 }
615
616 - (NSArray<_WKAttachment *> *)removedAttachments
617 {
618     return _removedAttachments.get();
619 }
620
621 - (void)endDataTransfer
622 {
623     [[_webView dragInteractionDelegate] dragInteraction:[_webView dragInteraction] sessionDidTransferItems:_dragSession.get()];
624 }
625
626 #pragma mark - WKUIDelegatePrivate
627
628 - (void)_webView:(WKWebView *)webView dataInteractionOperationWasHandled:(BOOL)handled forSession:(id)session itemProviders:(NSArray<UIItemProvider *> *)itemProviders
629 {
630     _isDoneWithCurrentRun = true;
631
632     if (self.dropCompletionBlock)
633         self.dropCompletionBlock(handled, itemProviders);
634 }
635
636 - (NSUInteger)_webView:(WKWebView *)webView willUpdateDataInteractionOperationToOperation:(NSUInteger)operation forSession:(id)session
637 {
638     return self.overrideDragUpdateBlock ? self.overrideDragUpdateBlock(operation, session) : operation;
639 }
640
641 - (NSArray *)_webView:(WKWebView *)webView adjustedDataInteractionItemProvidersForItemProvider:(UIItemProvider *)itemProvider representingObjects:(NSArray *)representingObjects additionalData:(NSDictionary *)additionalData
642 {
643     return self.convertItemProvidersBlock ? self.convertItemProvidersBlock(itemProvider, representingObjects, additionalData) : @[ itemProvider ];
644 }
645
646 - (BOOL)_webView:(WKWebView *)webView showCustomSheetForElement:(_WKActivatedElementInfo *)element
647 {
648     if (!self.showCustomActionSheetBlock)
649         return NO;
650
651     dispatch_async(dispatch_get_main_queue(), [strongSelf = retainPtr(self)] {
652         [[strongSelf->_webView dragInteractionDelegate] dragInteraction:[strongSelf->_webView dragInteraction] sessionWillBegin:strongSelf->_dragSession.get()];
653         strongSelf->_phase = DragAndDropPhaseBegan;
654         [strongSelf _scheduleAdvanceProgress];
655     });
656
657     return self.showCustomActionSheetBlock(element);
658 }
659
660 - (NSArray<UIDragItem *> *)_webView:(WKWebView *)webView willPerformDropWithSession:(id <UIDropSession>)session
661 {
662     return self.overridePerformDropBlock ? self.overridePerformDropBlock(session) : session.items;
663 }
664
665 - (void)_webView:(WKWebView *)webView didInsertAttachment:(_WKAttachment *)attachment
666 {
667     [_insertedAttachments addObject:attachment];
668 }
669
670 - (void)_webView:(WKWebView *)webView didRemoveAttachment:(_WKAttachment *)attachment
671 {
672     [_removedAttachments addObject:attachment];
673 }
674
675 #pragma mark - _WKInputDelegate
676
677 - (BOOL)_webView:(WKWebView *)webView focusShouldStartInputSession:(id <_WKFocusedElementInfo>)info
678 {
679     return _allowsFocusToStartInputSession;
680 }
681
682 - (void)_webView:(WKWebView *)webView didStartInputSession:(id <_WKFormInputSession>)inputSession
683 {
684     _isDoneWaitingForInputSession = true;
685 }
686
687 @end
688
689 #endif // ENABLE(DRAG_SUPPORT) && PLATFORM(IOS) && WK_API_ENABLED