Guard long press link preview with a USE macro rather than complicated includes
https://bugs.webkit.org/show_bug.cgi?id=197728
<rdar://problem/50604700>

Reviewed by Wenson Hsieh.

It was getting complicated to manage the coordination between WebKitAdditions
and WebKit without having to split up the includes into multiple files
or have lots of duplication.

Instead, WebKitAdditions will now define a USE macro which can be used
in various places through WebKit.

* UIProcess/ios/WKContentViewInteraction.h:
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView dragInteraction:itemsForBeginningSession:]):
(-[WKContentView _contentsOfUserInterfaceItem:]):
(previewIdentifierForElementAction):
(-[WKContentView shouldUsePreviewForLongPress]):
(-[WKContentView _registerPreview]):
(-[WKContentView _unregisterPreview]):
(shouldUsePreviewForLongPress): Deleted.
(-[WKContentView _registerPreviewLongPress]): Deleted.
(-[WKContentView _unregisterPreviewLongPress]): Deleted.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@245153 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebKit/ChangeLog b/Source/WebKit/ChangeLog
index 0ff593e..7811c89 100644
--- a/Source/WebKit/ChangeLog
+++ b/Source/WebKit/ChangeLog
@@ -1,3 +1,30 @@
+2019-05-08  Dean Jackson  <dino@apple.com>
+
+        Guard long press link preview with a USE macro rather than complicated includes
+        https://bugs.webkit.org/show_bug.cgi?id=197728
+        <rdar://problem/50604700>
+
+        Reviewed by Wenson Hsieh.
+
+        It was getting complicated to manage the coordination between WebKitAdditions
+        and WebKit without having to split up the includes into multiple files
+        or have lots of duplication.
+
+        Instead, WebKitAdditions will now define a USE macro which can be used
+        in various places through WebKit.
+
+        * UIProcess/ios/WKContentViewInteraction.h:
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView dragInteraction:itemsForBeginningSession:]):
+        (-[WKContentView _contentsOfUserInterfaceItem:]):
+        (previewIdentifierForElementAction):
+        (-[WKContentView shouldUsePreviewForLongPress]):
+        (-[WKContentView _registerPreview]):
+        (-[WKContentView _unregisterPreview]):
+        (shouldUsePreviewForLongPress): Deleted.
+        (-[WKContentView _registerPreviewLongPress]): Deleted.
+        (-[WKContentView _unregisterPreviewLongPress]): Deleted.
+
 2019-05-09  Alex Christensen  <achristensen@webkit.org>
 
         Remove now-unnecessary Connection::sendMessageWithReply
diff --git a/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h b/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h
index 9736c2b..e7ecbcb 100644
--- a/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h
+++ b/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h
@@ -61,13 +61,6 @@
 
 #if USE(APPLE_INTERNAL_SDK) && __has_include(<WebKitAdditions/WKInteractionPreviewAdditions.h>)
 #import <WebKitAdditions/WKInteractionPreviewAdditions.h>
-#else
-#ifndef ADDITIONAL_LINK_PREVIEW_MEMBERS
-#define ADDITIONAL_LINK_PREVIEW_MEMBERS
-#endif
-#ifndef ADDITIONAL_LINK_PREVIEW_PROTOCOLS
-#define ADDITIONAL_LINK_PREVIEW_PROTOCOLS
-#endif
 #endif
 
 namespace API {
@@ -255,8 +248,11 @@
     RetainPtr<UIGestureRecognizer> _previewSecondaryGestureRecognizer;
     Vector<bool> _focusStateStack;
 #if HAVE(LINK_PREVIEW)
+#if USE(LONG_PRESS_FOR_LINK_PREVIEW)
+    LONG_PRESS_LINK_PREVIEW_MEMBERS
+#else
     RetainPtr<UIPreviewItemController> _previewItemController;
-    ADDITIONAL_LINK_PREVIEW_MEMBERS
+#endif
 #endif
 
     std::unique_ptr<WebKit::SmartMagnificationController> _smartMagnificationController;
@@ -527,7 +523,11 @@
 @end
 
 #if HAVE(LINK_PREVIEW)
-@interface WKContentView (WKInteractionPreview) <UIPreviewItemDelegate ADDITIONAL_LINK_PREVIEW_PROTOCOLS>
+#if USE(LONG_PRESS_FOR_LINK_PREVIEW)
+@interface WKContentView (WKInteractionPreview) <LONG_PRESS_LINK_PREVIEW_PROTOCOL>
+#else
+@interface WKContentView (WKInteractionPreview) <UIPreviewItemDelegate>
+#endif
 
 @property (nonatomic, readonly) BOOL shouldUsePreviewForLongPress;
 
diff --git a/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm b/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
index 819c6eb..aea73e7 100644
--- a/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
+++ b/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
@@ -7215,8 +7215,12 @@
 
 #if HAVE(LINK_PREVIEW)
     if ([userInterfaceItem isEqualToString:@"linkPreviewPopoverContents"]) {
+#if USE(LONG_PRESS_FOR_LINK_PREVIEW)
+        return @{ userInterfaceItem: @{ @"pageURL": WTF::userVisibleString(_positionInformation.url) } };
+#else
         NSString *url = [_previewItemController previewData][UIPreviewDataLink];
         return @{ userInterfaceItem: @{ @"pageURL": url } };
+#endif
     }
 #endif
 
@@ -7233,28 +7237,35 @@
 
 #if HAVE(LINK_PREVIEW)
 
-@implementation WKContentView (WKInteractionPreview)
+static NSString *previewIdentifierForElementAction(_WKElementAction *action)
+{
+    switch (action.type) {
+    case _WKElementActionTypeOpen:
+        return WKPreviewActionItemIdentifierOpen;
+    case _WKElementActionTypeCopy:
+        return WKPreviewActionItemIdentifierCopy;
+#if !defined(TARGET_OS_IOS) || TARGET_OS_IOS
+    case _WKElementActionTypeAddToReadingList:
+        return WKPreviewActionItemIdentifierAddToReadingList;
+#endif
+    case _WKElementActionTypeShare:
+        return WKPreviewActionItemIdentifierShare;
+    default:
+        return nil;
+    }
+    ASSERT_NOT_REACHED();
+    return nil;
+}
 
-#if USE(APPLE_INTERNAL_SDK) && __has_include(<WebKitAdditions/WKInteractionPreviewAdditions.mm>)
+#if USE(LONG_PRESS_FOR_LINK_PREVIEW)
 #include <WebKitAdditions/WKInteractionPreviewAdditions.mm>
 #else
-static BOOL shouldUsePreviewForLongPress()
-{
-    return NO;
-}
 
-- (void)_registerPreviewLongPress
-{
-}
-
-- (void)_unregisterPreviewLongPress
-{
-}
-#endif
+@implementation WKContentView (WKInteractionPreview)
 
 - (BOOL)shouldUsePreviewForLongPress
 {
-    return shouldUsePreviewForLongPress();
+    return NO;
 }
 
 - (void)_registerPreview
@@ -7262,27 +7273,19 @@
     if (!_webView.allowsLinkPreview)
         return;
 
-    if (shouldUsePreviewForLongPress())
-        [self _registerPreviewLongPress];
-    else {
-        _previewItemController = adoptNS([[UIPreviewItemController alloc] initWithView:self]);
-        [_previewItemController setDelegate:self];
-        _previewGestureRecognizer = _previewItemController.get().presentationGestureRecognizer;
-        if ([_previewItemController respondsToSelector:@selector(presentationSecondaryGestureRecognizer)])
-            _previewSecondaryGestureRecognizer = _previewItemController.get().presentationSecondaryGestureRecognizer;
-    }
+    _previewItemController = adoptNS([[UIPreviewItemController alloc] initWithView:self]);
+    [_previewItemController setDelegate:self];
+    _previewGestureRecognizer = _previewItemController.get().presentationGestureRecognizer;
+    if ([_previewItemController respondsToSelector:@selector(presentationSecondaryGestureRecognizer)])
+        _previewSecondaryGestureRecognizer = _previewItemController.get().presentationSecondaryGestureRecognizer;
 }
 
 - (void)_unregisterPreview
 {
-    if (shouldUsePreviewForLongPress())
-        [self _unregisterPreviewLongPress];
-    else {
-        [_previewItemController setDelegate:nil];
-        _previewGestureRecognizer = nil;
-        _previewSecondaryGestureRecognizer = nil;
-        _previewItemController = nil;
-    }
+    [_previewItemController setDelegate:nil];
+    _previewGestureRecognizer = nil;
+    _previewSecondaryGestureRecognizer = nil;
+    _previewItemController = nil;
 }
 
 - (BOOL)_interactionShouldBeginFromPreviewItemController:(UIPreviewItemController *)controller forPosition:(CGPoint)position
@@ -7416,26 +7419,6 @@
     return _positionInformation.bounds;
 }
 
-static NSString *previewIdentifierForElementAction(_WKElementAction *action)
-{
-    switch (action.type) {
-    case _WKElementActionTypeOpen:
-        return WKPreviewActionItemIdentifierOpen;
-    case _WKElementActionTypeCopy:
-        return WKPreviewActionItemIdentifierCopy;
-#if !defined(TARGET_OS_IOS) || TARGET_OS_IOS
-    case _WKElementActionTypeAddToReadingList:
-        return WKPreviewActionItemIdentifierAddToReadingList;
-#endif
-    case _WKElementActionTypeShare:
-        return WKPreviewActionItemIdentifierShare;
-    default:
-        return nil;
-    }
-    ASSERT_NOT_REACHED();
-    return nil;
-}
-
 - (UIViewController *)_presentedViewControllerForPreviewItemController:(UIPreviewItemController *)controller
 {
     id <WKUIDelegatePrivate> uiDelegate = static_cast<id <WKUIDelegatePrivate>>([_webView UIDelegate]);
@@ -7606,6 +7589,8 @@
 
 @end
 
+#endif // USE(LONG_PRESS_FOR_LINK_PREVIEW)
+
 #endif // HAVE(LINK_PREVIEW)
 
 // UITextRange, UITextPosition and UITextSelectionRect implementations for WK2