<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[213170] trunk/Source/WebKit2</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/213170">213170</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2017-02-28 13:28:58 -0800 (Tue, 28 Feb 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add delegate method to handle images with alternate data.
https://bugs.webkit.org/show_bug.cgi?id=168785
&lt;rdar://problem/28776219&gt;

Add delegate methods to WKUIDelegatePrivate so that a WebKit client can provide alternate URL or data for an image
before long-press or preview. When an image has alternate URL or data, we can change the actions accordingly.

Patch by Yongjun Zhang &lt;yongjun_zhang@apple.com&gt; on 2017-02-28
Reviewed by Enrica Casucci.

* UIProcess/API/Cocoa/WKUIDelegatePrivate.h: Add two delegate methods _webView:getAlternateURLFromImage:completionHandler:
    and _webView:alternateURLFromImage:userInfo;
* UIProcess/API/Cocoa/_WKActivatedElementInfo.h: Add an readonly property userInfo so that alternate data can be
    carried by _WKActivatedElementInfo and be processed by WebKit client.
* UIProcess/API/Cocoa/_WKActivatedElementInfo.mm:
(-[_WKActivatedElementInfo _initWithType:URL:location:title:ID:rect:image:]):
(-[_WKActivatedElementInfo _initWithType:URL:location:title:ID:rect:image:userInfo:]): A new initializer that also takes userInfo.
(-[_WKActivatedElementInfo userInfo]):
* UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h: A new delegate method for WKActionSheetAssistantDelegate to fetch
    alternate data before showing image sheet.
* UIProcess/ios/WKActionSheetAssistant.h:
* UIProcess/ios/WKActionSheetAssistant.mm:
(-[WKActionSheetAssistant showImageSheet]): Before showing the image sheet, if the image doesn't have linked URL (i.e. not a image
    link), try to fetch its alternate URL and data.
(-[WKActionSheetAssistant defaultActionsForImageSheet:]): Drive-by fix. We always use positionInformation's URL to populate the
    actions, however, we should use elementInfo's URL since the two URLs could mismatch.
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView actionSheetAssistant:getAlternateURLForImage:completion:]):
(-[WKContentView _presentedViewControllerForPreviewItemController:]): Before previewing an image, synchronously get alternate
    data for the image and update the actions accordingly.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPICocoaWKUIDelegatePrivateh">trunk/Source/WebKit2/UIProcess/API/Cocoa/WKUIDelegatePrivate.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPICocoa_WKActivatedElementInfoh">trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPICocoa_WKActivatedElementInfomm">trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.mm</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPICocoa_WKActivatedElementInfoInternalh">trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWKActionSheetAssistanth">trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWKActionSheetAssistantmm">trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.mm</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWKContentViewInteractionmm">trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (213169 => 213170)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2017-02-28 21:26:27 UTC (rev 213169)
+++ trunk/Source/WebKit2/ChangeLog        2017-02-28 21:28:58 UTC (rev 213170)
</span><span class="lines">@@ -1,3 +1,35 @@
</span><ins>+2017-02-28  Yongjun Zhang  &lt;yongjun_zhang@apple.com&gt;
+
+        Add delegate method to handle images with alternate data.
+        https://bugs.webkit.org/show_bug.cgi?id=168785
+        &lt;rdar://problem/28776219&gt;
+
+        Add delegate methods to WKUIDelegatePrivate so that a WebKit client can provide alternate URL or data for an image
+        before long-press or preview. When an image has alternate URL or data, we can change the actions accordingly.
+
+        Reviewed by Enrica Casucci.
+
+        * UIProcess/API/Cocoa/WKUIDelegatePrivate.h: Add two delegate methods _webView:getAlternateURLFromImage:completionHandler:
+            and _webView:alternateURLFromImage:userInfo;
+        * UIProcess/API/Cocoa/_WKActivatedElementInfo.h: Add an readonly property userInfo so that alternate data can be
+            carried by _WKActivatedElementInfo and be processed by WebKit client.
+        * UIProcess/API/Cocoa/_WKActivatedElementInfo.mm:
+        (-[_WKActivatedElementInfo _initWithType:URL:location:title:ID:rect:image:]):
+        (-[_WKActivatedElementInfo _initWithType:URL:location:title:ID:rect:image:userInfo:]): A new initializer that also takes userInfo.
+        (-[_WKActivatedElementInfo userInfo]):
+        * UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h: A new delegate method for WKActionSheetAssistantDelegate to fetch
+            alternate data before showing image sheet.
+        * UIProcess/ios/WKActionSheetAssistant.h:
+        * UIProcess/ios/WKActionSheetAssistant.mm:
+        (-[WKActionSheetAssistant showImageSheet]): Before showing the image sheet, if the image doesn't have linked URL (i.e. not a image
+            link), try to fetch its alternate URL and data.
+        (-[WKActionSheetAssistant defaultActionsForImageSheet:]): Drive-by fix. We always use positionInformation's URL to populate the
+            actions, however, we should use elementInfo's URL since the two URLs could mismatch.
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView actionSheetAssistant:getAlternateURLForImage:completion:]):
+        (-[WKContentView _presentedViewControllerForPreviewItemController:]): Before previewing an image, synchronously get alternate
+            data for the image and update the actions accordingly.
+
</ins><span class="cx"> 2017-02-28  Brady Eidson  &lt;beidson@apple.com&gt;
</span><span class="cx">         
</span><span class="cx">         Refactor WebViewImpl creation in preparation for supporting multiple WebsiteDataStores.
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPICocoaWKUIDelegatePrivateh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/WKUIDelegatePrivate.h (213169 => 213170)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/Cocoa/WKUIDelegatePrivate.h        2017-02-28 21:26:27 UTC (rev 213169)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/WKUIDelegatePrivate.h        2017-02-28 21:28:58 UTC (rev 213170)
</span><span class="lines">@@ -84,6 +84,8 @@
</span><span class="cx"> - (UIViewController *)_webView:(WKWebView *)webView previewViewControllerForURL:(NSURL *)url defaultActions:(NSArray&lt;_WKElementAction *&gt; *)actions elementInfo:(_WKActivatedElementInfo *)elementInfo WK_API_AVAILABLE(ios(9.0));
</span><span class="cx"> - (UIViewController *)_webView:(WKWebView *)webView previewViewControllerForAnimatedImageAtURL:(NSURL *)url defaultActions:(NSArray&lt;_WKElementAction *&gt; *)actions elementInfo:(_WKActivatedElementInfo *)elementInfo imageSize:(CGSize)imageSize WK_API_AVAILABLE(ios(9.0));
</span><span class="cx"> - (UIViewController *)_presentingViewControllerForWebView:(WKWebView *)webView WK_API_AVAILABLE(ios(10.0));
</span><ins>+- (void)_webView:(WKWebView *)webView getAlternateURLFromImage:(UIImage *)image completionHandler:(void (^)(NSURL *alternateURL, NSDictionary *userInfo))completionHandler WK_API_AVAILABLE(ios(WK_IOS_TBA));
+- (NSURL *)_webView:(WKWebView *)webView alternateURLFromImage:(UIImage *)image userInfo:(NSDictionary **)userInfo WK_API_AVAILABLE(ios(WK_IOS_TBA));
</ins><span class="cx"> #else
</span><span class="cx"> - (NSMenu *)_webView:(WKWebView *)webView contextMenu:(NSMenu *)menu forElement:(_WKContextMenuElementInfo *)element WK_API_AVAILABLE(macosx(10.12));
</span><span class="cx"> - (NSMenu *)_webView:(WKWebView *)webView contextMenu:(NSMenu *)menu forElement:(_WKContextMenuElementInfo *)element userInfo:(id &lt;NSSecureCoding&gt;)userInfo WK_API_AVAILABLE(macosx(10.12));
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPICocoa_WKActivatedElementInfoh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.h (213169 => 213170)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.h        2017-02-28 21:26:27 UTC (rev 213169)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.h        2017-02-28 21:28:58 UTC (rev 213170)
</span><span class="lines">@@ -48,6 +48,7 @@
</span><span class="cx"> @property (nonatomic, readonly) CGRect boundingRect;
</span><span class="cx"> @property (nonatomic, readonly) NSString *ID WK_API_AVAILABLE(macosx(10.12), ios(10.0));
</span><span class="cx"> #if TARGET_OS_IPHONE
</span><ins>+@property (nonatomic, readonly) NSDictionary *userInfo;
</ins><span class="cx"> @property (nonatomic, readonly, copy) UIImage *image;
</span><span class="cx"> #else
</span><span class="cx"> @property (nonatomic, readonly, copy) NSImage *image;
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPICocoa_WKActivatedElementInfomm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.mm (213169 => 213170)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.mm        2017-02-28 21:26:27 UTC (rev 213169)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.mm        2017-02-28 21:28:58 UTC (rev 213170)
</span><span class="lines">@@ -47,6 +47,7 @@
</span><span class="cx">     RefPtr&lt;WebKit::ShareableBitmap&gt; _image;
</span><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx">     RetainPtr&lt;UIImage&gt; _uiImage;
</span><ins>+    RetainPtr&lt;NSDictionary&gt; _userInfo;
</ins><span class="cx"> #endif
</span><span class="cx"> #if PLATFORM(MAC)
</span><span class="cx">     RetainPtr&lt;NSImage&gt; _nsImage;
</span><span class="lines">@@ -55,6 +56,11 @@
</span><span class="cx"> 
</span><span class="cx"> - (instancetype)_initWithType:(_WKActivatedElementType)type URL:(NSURL *)url location:(CGPoint)location title:(NSString *)title ID:(NSString *)ID rect:(CGRect)rect image:(WebKit::ShareableBitmap*)image
</span><span class="cx"> {
</span><ins>+    return [self _initWithType:type URL:url location:location title:title ID:ID rect:rect image:image userInfo:nil];
+}
+
+- (instancetype)_initWithType:(_WKActivatedElementType)type URL:(NSURL *)url location:(CGPoint)location title:(NSString *)title ID:(NSString *)ID rect:(CGRect)rect image:(WebKit::ShareableBitmap*)image userInfo:(NSDictionary *)userInfo
+{
</ins><span class="cx">     if (!(self = [super init]))
</span><span class="cx">         return nil;
</span><span class="cx"> 
</span><span class="lines">@@ -65,6 +71,9 @@
</span><span class="cx">     _type = type;
</span><span class="cx">     _image = image;
</span><span class="cx">     _ID = ID;
</span><ins>+#if PLATFORM(IOS)
+    _userInfo = adoptNS([userInfo copy]);
+#endif
</ins><span class="cx"> 
</span><span class="cx">     return self;
</span><span class="cx"> }
</span><span class="lines">@@ -90,6 +99,11 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(IOS)
</span><ins>+- (NSDictionary *)userInfo
+{
+    return _userInfo.get();
+}
+
</ins><span class="cx"> - (UIImage *)image
</span><span class="cx"> {
</span><span class="cx">     if (_uiImage)
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPICocoa_WKActivatedElementInfoInternalh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h (213169 => 213170)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h        2017-02-28 21:26:27 UTC (rev 213169)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h        2017-02-28 21:28:58 UTC (rev 213170)
</span><span class="lines">@@ -34,6 +34,7 @@
</span><span class="cx"> @interface _WKActivatedElementInfo ()
</span><span class="cx"> 
</span><span class="cx"> - (instancetype)_initWithType:(_WKActivatedElementType)type URL:(NSURL *)url location:(CGPoint)location title:(NSString *)title ID:(NSString *)ID rect:(CGRect)rect image:(WebKit::ShareableBitmap*)image;
</span><ins>+- (instancetype)_initWithType:(_WKActivatedElementType)type URL:(NSURL *)url location:(CGPoint)location title:(NSString *)title ID:(NSString *)ID rect:(CGRect)rect image:(WebKit::ShareableBitmap*)image userInfo:(NSDictionary *)userInfo;
</ins><span class="cx"> 
</span><span class="cx"> @property (nonatomic, readonly) CGPoint _interactionLocation;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWKActionSheetAssistanth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.h (213169 => 213170)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.h        2017-02-28 21:26:27 UTC (rev 213169)
+++ trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.h        2017-02-28 21:28:58 UTC (rev 213170)
</span><span class="lines">@@ -58,6 +58,7 @@
</span><span class="cx"> - (void)actionSheetAssistantDidStopInteraction:(WKActionSheetAssistant *)assistant;
</span><span class="cx"> - (NSDictionary *)dataDetectionContextForActionSheetAssistant:(WKActionSheetAssistant *)assistant;
</span><span class="cx"> - (NSString *)selectedTextForActionSheetAssistant:(WKActionSheetAssistant *)assistant;
</span><ins>+- (void)actionSheetAssistant:(WKActionSheetAssistant *)assistant getAlternateURLForImage:(UIImage *)image completion:(void (^)(NSURL *alternateURL, NSDictionary *userInfo))completion;
</ins><span class="cx"> 
</span><span class="cx"> @end
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWKActionSheetAssistantmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.mm (213169 => 213170)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.mm        2017-02-28 21:26:27 UTC (rev 213169)
+++ trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.mm        2017-02-28 21:28:58 UTC (rev 213170)
</span><span class="lines">@@ -334,26 +334,44 @@
</span><span class="cx"> 
</span><span class="cx">     const auto&amp; positionInformation = [delegate positionInformationForActionSheetAssistant:self];
</span><span class="cx"> 
</span><del>-    NSURL *targetURL = [NSURL _web_URLWithWTFString:positionInformation.url];
-    auto elementBounds = positionInformation.bounds;
-    auto elementInfo = adoptNS([[_WKActivatedElementInfo alloc] _initWithType:_WKActivatedElementTypeImage URL:targetURL location:positionInformation.request.point title:positionInformation.title ID:positionInformation.idAttribute rect:elementBounds image:positionInformation.image.get()]);
-    if ([delegate respondsToSelector:@selector(actionSheetAssistant:showCustomSheetForElement:)] &amp;&amp; [delegate actionSheetAssistant:self showCustomSheetForElement:elementInfo.get()])
-        return;
-    auto defaultActions = [self defaultActionsForImageSheet:elementInfo.get()];
</del><ins>+    void (^showImageSheetWithAlternateURLBlock)(NSURL*, NSDictionary *userInfo) = ^(NSURL *alternateURL, NSDictionary *userInfo) {
+        NSURL *targetURL = [NSURL _web_URLWithWTFString:positionInformation.url] ?: alternateURL;
+        auto elementBounds = positionInformation.bounds;
+        auto elementInfo = adoptNS([[_WKActivatedElementInfo alloc] _initWithType:_WKActivatedElementTypeImage URL:targetURL location:positionInformation.request.point title:positionInformation.title ID:positionInformation.idAttribute rect:elementBounds image:positionInformation.image.get() userInfo:userInfo]);
+        if ([delegate respondsToSelector:@selector(actionSheetAssistant:showCustomSheetForElement:)] &amp;&amp; [delegate actionSheetAssistant:self showCustomSheetForElement:elementInfo.get()])
+            return;
+        auto defaultActions = [self defaultActionsForImageSheet:elementInfo.get()];
</ins><span class="cx"> 
</span><del>-    RetainPtr&lt;NSArray&gt; actions = [delegate actionSheetAssistant:self decideActionsForElement:elementInfo.get() defaultActions:WTFMove(defaultActions)];
</del><ins>+        RetainPtr&lt;NSArray&gt; actions = [delegate actionSheetAssistant:self decideActionsForElement:elementInfo.get() defaultActions:WTFMove(defaultActions)];
</ins><span class="cx"> 
</span><del>-    if (![actions count])
-        return;
</del><ins>+        if (![actions count])
+            return;
</ins><span class="cx"> 
</span><del>-    [self _createSheetWithElementActions:actions.get() showLinkTitle:YES];
-    if (!_interactionSheet)
</del><ins>+        if (!alternateURL &amp;&amp; userInfo) {
+            [UIApp _cancelAllTouches];
+            return;
+        }
+
+        [self _createSheetWithElementActions:actions.get() showLinkTitle:YES];
+        if (!_interactionSheet)
+            return;
+
+        _elementInfo = WTFMove(elementInfo);
+
+        if (![_interactionSheet presentSheet:[self _shouldPresentAtTouchLocationForElementRect:elementBounds] ? WKActionSheetPresentAtTouchLocation : WKActionSheetPresentAtElementRect])
+            [self cleanupSheet];
+    };
+
+    if (positionInformation.url.isEmpty() &amp;&amp; positionInformation.image &amp;&amp; [delegate respondsToSelector:@selector(actionSheetAssistant:getAlternateURLForImage:completion:)]) {
+        RetainPtr&lt;UIImage&gt; uiImage = adoptNS([[UIImage alloc] initWithCGImage:positionInformation.image-&gt;makeCGImageCopy().get()]);
+
+        [delegate actionSheetAssistant:self getAlternateURLForImage:uiImage.get() completion:^(NSURL *alternateURL, NSDictionary *userInfo) {
+            showImageSheetWithAlternateURLBlock(alternateURL, userInfo);
+        }];
</ins><span class="cx">         return;
</span><ins>+    }
</ins><span class="cx"> 
</span><del>-    _elementInfo = WTFMove(elementInfo);
-
-    if (![_interactionSheet presentSheet:[self _shouldPresentAtTouchLocationForElementRect:elementBounds] ? WKActionSheetPresentAtTouchLocation : WKActionSheetPresentAtElementRect])
-        [self cleanupSheet];
</del><ins>+    showImageSheetWithAlternateURLBlock(nil, nil);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (BOOL)_shouldPresentAtTouchLocationForElementRect:(CGRect)elementRect
</span><span class="lines">@@ -436,11 +454,10 @@
</span><span class="cx">     if (!delegate)
</span><span class="cx">         return nil;
</span><span class="cx"> 
</span><del>-    const auto&amp; positionInformation = [delegate positionInformationForActionSheetAssistant:self];
-    NSURL *targetURL = [NSURL _web_URLWithWTFString:positionInformation.url];
</del><ins>+    NSURL *targetURL = [elementInfo URL];
</ins><span class="cx"> 
</span><span class="cx">     auto defaultActions = adoptNS([[NSMutableArray alloc] init]);
</span><del>-    if (!positionInformation.url.isEmpty()) {
</del><ins>+    if (targetURL) {
</ins><span class="cx">         [self _appendOpenActionsForURL:targetURL actions:defaultActions.get() elementInfo:elementInfo];
</span><span class="cx">         [defaultActions addObject:[_WKElementAction _elementActionWithType:_WKElementActionTypeShare assistant:self]];
</span><span class="cx">     }
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWKContentViewInteractionmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm (213169 => 213170)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm        2017-02-28 21:26:27 UTC (rev 213169)
+++ trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm        2017-02-28 21:28:58 UTC (rev 213170)
</span><span class="lines">@@ -4012,6 +4012,17 @@
</span><span class="cx">     return [self selectedText];
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+- (void)actionSheetAssistant:(WKActionSheetAssistant *)assistant getAlternateURLForImage:(UIImage *)image completion:(void (^)(NSURL *alternateURL, NSDictionary *userInfo))completion
+{
+    id &lt;WKUIDelegatePrivate&gt; uiDelegate = static_cast&lt;id &lt;WKUIDelegatePrivate&gt;&gt;([_webView UIDelegate]);
+    if ([uiDelegate respondsToSelector:@selector(_webView:getAlternateURLFromImage:completionHandler:)]) {
+        [uiDelegate _webView:_webView getAlternateURLFromImage:image completionHandler:^(NSURL *alternateURL, NSDictionary *userInfo) {
+            completion(alternateURL, userInfo);
+        }];
+    } else
+        completion(nil, nil);
+}
+
</ins><span class="cx"> @end
</span><span class="cx"> 
</span><span class="cx"> @implementation WKContentView (WKTesting)
</span><span class="lines">@@ -4257,12 +4268,27 @@
</span><span class="cx">         if (!isValidURLForImagePreview)
</span><span class="cx">             return nil;
</span><span class="cx"> 
</span><del>-        RetainPtr&lt;_WKActivatedElementInfo&gt; elementInfo = adoptNS([[_WKActivatedElementInfo alloc] _initWithType:_WKActivatedElementTypeImage URL:targetURL location:_positionInformation.request.point title:_positionInformation.title ID:_positionInformation.idAttribute rect:_positionInformation.bounds image:_positionInformation.image.get()]);
</del><ins>+        RetainPtr&lt;NSURL&gt; alternateURL = targetURL;
+        RetainPtr&lt;NSDictionary&gt; imageInfo;
+        RetainPtr&lt;CGImageRef&gt; cgImage = _positionInformation.image-&gt;makeCGImageCopy();
+        RetainPtr&lt;UIImage&gt; uiImage = adoptNS([[UIImage alloc] initWithCGImage:cgImage.get()]);
+        if ([uiDelegate respondsToSelector:@selector(_webView:alternateURLFromImage:userInfo:)]) {
+            NSDictionary *userInfo;
+            alternateURL = [uiDelegate _webView:_webView alternateURLFromImage:uiImage.get() userInfo:&amp;userInfo];
+            imageInfo = userInfo;
+        }
+
+        RetainPtr&lt;_WKActivatedElementInfo&gt; elementInfo = adoptNS([[_WKActivatedElementInfo alloc] _initWithType:_WKActivatedElementTypeImage URL:alternateURL.get() location:_positionInformation.request.point title:_positionInformation.title ID:_positionInformation.idAttribute rect:_positionInformation.bounds image:_positionInformation.image.get() userInfo:imageInfo.get()]);
</ins><span class="cx">         _page-&gt;startInteractionWithElementAtPosition(_positionInformation.request.point);
</span><span class="cx"> 
</span><span class="cx">         if ([uiDelegate respondsToSelector:@selector(_webView:willPreviewImageWithURL:)])
</span><span class="cx">             [uiDelegate _webView:_webView willPreviewImageWithURL:targetURL];
</span><del>-        return [[[WKImagePreviewViewController alloc] initWithCGImage:_positionInformation.image-&gt;makeCGImageCopy() defaultActions:[_actionSheetAssistant defaultActionsForImageSheet:elementInfo.get()] elementInfo:elementInfo] autorelease];
</del><ins>+
+        auto defaultActions = [_actionSheetAssistant defaultActionsForImageSheet:elementInfo.get()];
+        if (imageInfo &amp;&amp; [uiDelegate respondsToSelector:@selector(_webView:actionsForElement:defaultActions:)])
+            defaultActions = [uiDelegate _webView:_webView actionsForElement:elementInfo.get() defaultActions:defaultActions.get()];
+
+        return [[[WKImagePreviewViewController alloc] initWithCGImage:cgImage defaultActions:defaultActions elementInfo:elementInfo] autorelease];
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     return nil;
</span></span></pre>
</div>
</div>

</body>
</html>