[iOS DnD] [WK2] Add SPI to enable, disable, or follow default behavior for DnD on...
[WebKit.git] / Source / WebKit2 / UIProcess / ios / WKContentViewInteraction.h
1 /*
2  * Copyright (C) 2012-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 #if PLATFORM(IOS)
27
28 #import "WKContentView.h"
29
30 #import "AssistedNodeInformation.h"
31 #import "EditorState.h"
32 #import "GestureTypes.h"
33 #import "InteractionInformationAtPosition.h"
34 #import "UIKitSPI.h"
35 #import "WKActionSheetAssistant.h"
36 #import "WKAirPlayRoutePicker.h"
37 #import "WKFileUploadPanel.h"
38 #import "WKFormPeripheral.h"
39 #import "WKSyntheticClickTapGestureRecognizer.h"
40 #import <UIKit/UIView.h>
41 #import <WebCore/Color.h>
42 #import <WebCore/DragActions.h>
43 #import <WebCore/FloatQuad.h>
44 #import <wtf/BlockPtr.h>
45 #import <wtf/Forward.h>
46 #import <wtf/Vector.h>
47 #import <wtf/text/WTFString.h>
48
49 namespace API {
50 class OpenPanelParameters;
51 }
52
53 namespace WebCore {
54 class Color;
55 class FloatQuad;
56 class IntSize;
57 class TextStream;
58 }
59
60 #if ENABLE(DRAG_SUPPORT)
61 namespace WebCore {
62 struct DragItem;
63 }
64 #endif
65
66 namespace WebKit {
67 class InputViewUpdateDeferrer;
68 class NativeWebTouchEvent;
69 class SmartMagnificationController;
70 class WebOpenPanelResultListenerProxy;
71 class WebPageProxy;
72 }
73
74 @class WKActionSheetAssistant;
75 @class WKFormInputSession;
76 @class WKInspectorNodeSearchGestureRecognizer;
77 @class WebEvent;
78 @class _UIHighlightView;
79 @class _UIWebHighlightLongPressGestureRecognizer;
80
81 #if ENABLE(DATA_INTERACTION)
82 @class _UITextDragCaretView;
83 #endif
84
85 typedef void (^UIWKAutocorrectionCompletionHandler)(UIWKAutocorrectionRects *rectsForInput);
86 typedef void (^UIWKAutocorrectionContextHandler)(UIWKAutocorrectionContext *autocorrectionContext);
87 typedef void (^UIWKDictationContextHandler)(NSString *selectedText, NSString *beforeText, NSString *afterText);
88 typedef void (^UIWKSelectionCompletionHandler)(void);
89 typedef void (^UIWKSelectionWithDirectionCompletionHandler)(BOOL selectionEndIsMoving);
90 typedef void (^UIWKKeyWebEventCompletionHandler)(::WebEvent *theEvent, BOOL wasHandled);
91
92 typedef BlockPtr<void(WebKit::InteractionInformationAtPosition)> InteractionInformationCallback;
93 typedef std::pair<WebKit::InteractionInformationRequest, InteractionInformationCallback> InteractionInformationRequestAndCallback;
94
95 #define FOR_EACH_WKCONTENTVIEW_ACTION(M) \
96     M(_addShortcut) \
97     M(_arrowKey) \
98     M(_define) \
99     M(_lookup) \
100     M(_promptForReplace) \
101     M(_reanalyze) \
102     M(_share) \
103     M(_showTextStyleOptions) \
104     M(_transliterateChinese) \
105     M(copy) \
106     M(cut) \
107     M(paste) \
108     M(replace) \
109     M(select) \
110     M(selectAll) \
111     M(toggleBoldface) \
112     M(toggleItalics) \
113     M(toggleUnderline)
114
115 namespace WebKit {
116
117 #if ENABLE(DRAG_SUPPORT)
118
119 struct WKDataInteractionState {
120     RetainPtr<UIImage> image;
121     std::optional<WebCore::TextIndicatorData> indicatorData;
122     CGPoint adjustedOrigin { CGPointZero };
123     CGPoint lastGlobalPosition { CGPointZero };
124     CGRect elementBounds { CGRectZero };
125     BOOL didBeginDragging { NO };
126     BOOL isPerformingOperation { NO };
127     BOOL isAnimatingConcludeEditDrag { NO };
128     BOOL shouldRestoreCalloutBar { NO };
129     RetainPtr<id <UIDragSession>> dragSession;
130     RetainPtr<id <UIDropSession>> dropSession;
131     BlockPtr<void()> dragStartCompletionBlock;
132     BlockPtr<void()> dragCancelSetDownBlock;
133     WebCore::DragSourceAction sourceAction { WebCore::DragSourceActionNone };
134
135     String linkTitle;
136     WebCore::URL linkURL;
137
138     RetainPtr<UIView> visibleContentViewSnapshot;
139     RetainPtr<_UITextDragCaretView> caretView;
140 };
141
142 #endif // ENABLE(DRAG_SUPPORT)
143
144 struct WKSelectionDrawingInfo {
145     enum class SelectionType { None, Plugin, Range };
146     WKSelectionDrawingInfo();
147     explicit WKSelectionDrawingInfo(const EditorState&);
148     SelectionType type;
149     WebCore::IntRect caretRect;
150     Vector<WebCore::SelectionRect> selectionRects;
151 };
152
153 WebCore::TextStream& operator<<(WebCore::TextStream&, const WKSelectionDrawingInfo&);
154
155 struct WKAutoCorrectionData {
156     String fontName;
157     CGFloat fontSize;
158     uint64_t fontTraits;
159     CGRect textFirstRect;
160     CGRect textLastRect;
161     UIWKAutocorrectionCompletionHandler autocorrectionHandler;
162     UIWKAutocorrectionContextHandler autocorrectionContextHandler;
163 };
164
165 }
166
167 @interface WKContentView () {
168     RetainPtr<UIWebTouchEventsGestureRecognizer> _touchEventGestureRecognizer;
169
170     BOOL _canSendTouchEventsAsynchronously;
171
172     RetainPtr<WKSyntheticClickTapGestureRecognizer> _singleTapGestureRecognizer;
173     RetainPtr<_UIWebHighlightLongPressGestureRecognizer> _highlightLongPressGestureRecognizer;
174     RetainPtr<UILongPressGestureRecognizer> _longPressGestureRecognizer;
175     RetainPtr<UITapGestureRecognizer> _doubleTapGestureRecognizer;
176     RetainPtr<UITapGestureRecognizer> _nonBlockingDoubleTapGestureRecognizer;
177     RetainPtr<UITapGestureRecognizer> _twoFingerDoubleTapGestureRecognizer;
178     RetainPtr<UITapGestureRecognizer> _twoFingerSingleTapGestureRecognizer;
179     RetainPtr<WKInspectorNodeSearchGestureRecognizer> _inspectorNodeSearchGestureRecognizer;
180
181     RetainPtr<UIWKTextInteractionAssistant> _textSelectionAssistant;
182     RetainPtr<UIWKSelectionAssistant> _webSelectionAssistant;
183     BOOL _suppressAssistantSelectionView;
184
185     RetainPtr<UITextInputTraits> _traits;
186     RetainPtr<UIWebFormAccessory> _formAccessoryView;
187     RetainPtr<_UIHighlightView> _highlightView;
188     RetainPtr<UIView> _interactionViewsContainerView;
189     RetainPtr<NSString> _markedText;
190     RetainPtr<WKActionSheetAssistant> _actionSheetAssistant;
191     RetainPtr<WKAirPlayRoutePicker> _airPlayRoutePicker;
192     RetainPtr<WKFormInputSession> _formInputSession;
193     RetainPtr<WKFileUploadPanel> _fileUploadPanel;
194     RetainPtr<UIGestureRecognizer> _previewGestureRecognizer;
195     RetainPtr<UIGestureRecognizer> _previewSecondaryGestureRecognizer;
196 #if HAVE(LINK_PREVIEW)
197     RetainPtr<UIPreviewItemController> _previewItemController;
198 #endif
199
200     std::unique_ptr<WebKit::SmartMagnificationController> _smartMagnificationController;
201
202     id <UITextInputDelegate> _inputDelegate;
203
204     uint64_t _latestTapID;
205     struct TapHighlightInformation {
206         WebCore::Color color;
207         Vector<WebCore::FloatQuad> quads;
208         WebCore::IntSize topLeftRadius;
209         WebCore::IntSize topRightRadius;
210         WebCore::IntSize bottomLeftRadius;
211         WebCore::IntSize bottomRightRadius;
212     };
213     TapHighlightInformation _tapHighlightInformation;
214
215     WebKit::WKAutoCorrectionData _autocorrectionData;
216     WebKit::InteractionInformationAtPosition _positionInformation;
217     WebKit::AssistedNodeInformation _assistedNodeInformation;
218     RetainPtr<NSObject<WKFormPeripheral>> _inputPeripheral;
219     RetainPtr<UIEvent> _uiEventBeingResent;
220     UIWKKeyWebEventCompletionHandler _keyWebEventHandler;
221
222     CGPoint _lastInteractionLocation;
223     uint64_t _layerTreeTransactionIdAtLastTouchStart;
224
225     WebKit::WKSelectionDrawingInfo _lastSelectionDrawingInfo;
226
227     std::optional<WebKit::InteractionInformationRequest> _outstandingPositionInformationRequest;
228
229     uint64_t _positionInformationCallbackDepth;
230     Vector<std::optional<InteractionInformationRequestAndCallback>> _pendingPositionInformationHandlers;
231     
232     std::unique_ptr<WebKit::InputViewUpdateDeferrer> _inputViewUpdateDeferrer;
233
234     BOOL _isEditable;
235     BOOL _showingTextStyleOptions;
236     BOOL _hasValidPositionInformation;
237     BOOL _isTapHighlightIDValid;
238     BOOL _potentialTapInProgress;
239     BOOL _isDoubleTapPending;
240     BOOL _highlightLongPressCanClick;
241     BOOL _hasTapHighlightForPotentialTap;
242     BOOL _selectionNeedsUpdate;
243     BOOL _shouldRestoreSelection;
244     BOOL _usingGestureForSelection;
245     BOOL _inspectorNodeSearchEnabled;
246     BOOL _didAccessoryTabInitiateFocus;
247     BOOL _isExpectingFastSingleTapCommit;
248     BOOL _showDebugTapHighlightsForFastClicking;
249
250     BOOL _becomingFirstResponder;
251     BOOL _resigningFirstResponder;
252     BOOL _needsDeferredEndScrollingSelectionUpdate;
253
254 #if ENABLE(DATA_INTERACTION)
255     WebKit::WKDataInteractionState _dataInteractionState;
256     RetainPtr<UIDragInteraction> _dataInteraction;
257     RetainPtr<UIDropInteraction> _dataOperation;
258 #endif
259 }
260
261 @end
262
263 @interface WKContentView (WKInteraction) <UIGestureRecognizerDelegate, UIWebTouchEventsGestureRecognizerDelegate, UITextInputPrivate, UIWebFormAccessoryDelegate, UIWKInteractionViewProtocol, WKFileUploadPanelDelegate, WKActionSheetAssistantDelegate
264 #if ENABLE(DATA_INTERACTION)
265     , UIDragInteractionDelegate, UIDropInteractionDelegate
266 #endif
267 >
268
269 @property (nonatomic, readonly) CGPoint lastInteractionLocation;
270 @property (nonatomic, readonly) BOOL isEditable;
271 @property (nonatomic, readonly) BOOL shouldHideSelectionWhenScrolling;
272 @property (nonatomic, readonly) const WebKit::InteractionInformationAtPosition& positionInformation;
273 @property (nonatomic, readonly) const WebKit::WKAutoCorrectionData& autocorrectionData;
274 @property (nonatomic, readonly) const WebKit::AssistedNodeInformation& assistedNodeInformation;
275 @property (nonatomic, readonly) UIWebFormAccessory *formAccessoryView;
276 @property (nonatomic) BOOL suppressAssistantSelectionView;
277
278 - (void)setupInteraction;
279 - (void)cleanupInteraction;
280
281 - (void)scrollViewWillStartPanOrPinchGesture;
282
283 - (BOOL)canBecomeFirstResponderForWebView;
284 - (BOOL)becomeFirstResponderForWebView;
285 - (BOOL)resignFirstResponderForWebView;
286 - (BOOL)canPerformActionForWebView:(SEL)action withSender:(id)sender;
287
288 #define DECLARE_WKCONTENTVIEW_ACTION_FOR_WEB_VIEW(_action) \
289     - (void)_action ## ForWebView:(id)sender;
290 FOR_EACH_WKCONTENTVIEW_ACTION(DECLARE_WKCONTENTVIEW_ACTION_FOR_WEB_VIEW)
291 #undef DECLARE_WKCONTENTVIEW_ACTION_FOR_WEB_VIEW
292
293 #if ENABLE(TOUCH_EVENTS)
294 - (void)_webTouchEvent:(const WebKit::NativeWebTouchEvent&)touchEvent preventsNativeGestures:(BOOL)preventsDefault;
295 #endif
296 - (void)_commitPotentialTapFailed;
297 - (void)_didNotHandleTapAsClick:(const WebCore::IntPoint&)point;
298 - (void)_didCompleteSyntheticClick;
299 - (void)_didGetTapHighlightForRequest:(uint64_t)requestID color:(const WebCore::Color&)color quads:(const Vector<WebCore::FloatQuad>&)highlightedQuads topLeftRadius:(const WebCore::IntSize&)topLeftRadius topRightRadius:(const WebCore::IntSize&)topRightRadius bottomLeftRadius:(const WebCore::IntSize&)bottomLeftRadius bottomRightRadius:(const WebCore::IntSize&)bottomRightRadius;
300
301 - (BOOL)_mayDisableDoubleTapGesturesDuringSingleTap;
302 - (void)_disableDoubleTapGesturesDuringTapIfNecessary:(uint64_t)requestID;
303 - (void)_startAssistingNode:(const WebKit::AssistedNodeInformation&)information userIsInteracting:(BOOL)userIsInteracting blurPreviousNode:(BOOL)blurPreviousNode userObject:(NSObject <NSSecureCoding> *)userObject;
304 - (void)_stopAssistingNode;
305 - (void)_selectionChanged;
306 - (void)_updateChangedSelection;
307 - (BOOL)_interpretKeyEvent:(::WebEvent *)theEvent isCharEvent:(BOOL)isCharEvent;
308 - (void)_positionInformationDidChange:(const WebKit::InteractionInformationAtPosition&)info;
309 - (void)_attemptClickAtLocation:(CGPoint)location;
310 - (void)_willStartScrollingOrZooming;
311 - (void)_didScroll;
312 - (void)_didEndScrollingOrZooming;
313 - (void)_overflowScrollingWillBegin;
314 - (void)_overflowScrollingDidEnd;
315 - (void)_didUpdateBlockSelectionWithTouch:(WebKit::SelectionTouch)touch withFlags:(WebKit::SelectionFlags)flags growThreshold:(CGFloat)growThreshold shrinkThreshold:(CGFloat)shrinkThreshold;
316 - (void)_showPlaybackTargetPicker:(BOOL)hasVideo fromRect:(const WebCore::IntRect&)elementRect;
317 - (void)_showRunOpenPanel:(API::OpenPanelParameters*)parameters resultListener:(WebKit::WebOpenPanelResultListenerProxy*)listener;
318 - (void)accessoryDone;
319 - (void)_didHandleKeyEvent:(::WebEvent *)event eventWasHandled:(BOOL)eventWasHandled;
320 - (Vector<WebKit::OptionItem>&) assistedNodeSelectOptions;
321 - (void)_enableInspectorNodeSearch;
322 - (void)_disableInspectorNodeSearch;
323 - (void)_becomeFirstResponderWithSelectionMovingForward:(BOOL)selectingForward completionHandler:(void (^)(BOOL didBecomeFirstResponder))completionHandler;
324 - (void)_setDoubleTapGesturesEnabled:(BOOL)enabled;
325 #if ENABLE(DATA_DETECTION)
326 - (NSArray *)_dataDetectionResults;
327 #endif
328 - (NSArray<NSValue *> *)_uiTextSelectionRects;
329 - (void)accessibilityRetrieveSpeakSelectionContent;
330 - (void)_accessibilityRetrieveRectsEnclosingSelectionOffset:(NSInteger)offset withGranularity:(UITextGranularity)granularity;
331 - (void)_accessibilityRetrieveRectsAtSelectionOffset:(NSInteger)offset withText:(NSString *)text;
332
333 @property (nonatomic, readonly) WebKit::InteractionInformationAtPosition currentPositionInformation;
334 - (void)doAfterPositionInformationUpdate:(void (^)(WebKit::InteractionInformationAtPosition))action forRequest:(WebKit::InteractionInformationRequest)request;
335 - (BOOL)ensurePositionInformationIsUpToDate:(WebKit::InteractionInformationRequest)request;
336
337 #if ENABLE(DATA_INTERACTION)
338 - (void)_didChangeDragInteractionPolicy;
339 - (void)_didPerformDataInteractionControllerOperation:(BOOL)handled;
340 - (void)_didHandleStartDataInteractionRequest:(BOOL)started;
341 - (void)_startDrag:(RetainPtr<CGImageRef>)image item:(const WebCore::DragItem&)item;
342 - (void)_didConcludeEditDataInteraction:(std::optional<WebCore::TextIndicatorData>)data;
343 - (void)_didChangeDataInteractionCaretRect:(CGRect)previousRect currentRect:(CGRect)rect;
344
345 - (void)_simulateDataInteractionEntered:(id)info;
346 - (NSUInteger)_simulateDataInteractionUpdated:(id)info;
347 - (void)_simulateDataInteractionPerformOperation:(id)info;
348 - (void)_simulateDataInteractionEnded:(id)info;
349 - (void)_simulateDataInteractionSessionDidEnd:(id)session;
350 - (void)_simulateWillBeginDataInteractionWithSession:(id)session;
351 - (NSArray *)_simulatedItemsForSession:(id)session;
352 - (void)_simulatePrepareForDataInteractionSession:(id)session completion:(dispatch_block_t)completion;
353 #endif
354
355 - (void)_simulateLongPressActionAtLocation:(CGPoint)location;
356
357 @end
358
359 @interface WKContentView (WKTesting)
360
361 - (void)selectFormAccessoryPickerRow:(NSInteger)rowIndex;
362 - (NSDictionary *)_contentsOfUserInterfaceItem:(NSString *)userInterfaceItem;
363
364 @end
365
366 #if HAVE(LINK_PREVIEW)
367 @interface WKContentView (WKInteractionPreview) <UIPreviewItemDelegate>
368
369 - (void)_registerPreview;
370 - (void)_unregisterPreview;
371 @end
372 #endif
373
374 #endif // PLATFORM(IOS)