[iPadOS] Fix another crash in -[UIPreviewTarget initWithContainer:center:transform:] when generating a fallback targeted preview
https://bugs.webkit.org/show_bug.cgi?id=199243
<rdar://problem/51554509>

Reviewed by Tim Horton.

Additionally make createFallbackTargetedPreview robust in the case where the content view is unparented, and
rename _ensureTargetedPreview to _createTargetedPreviewIfPossible, to reflect the fact that it may now return
nil.

* UIProcess/ios/WKContentViewInteraction.mm:
(createTargetedPreview):
(createFallbackTargetedPreview):
(-[WKContentView _createTargetedPreviewIfPossible]):
(-[WKContentView contextMenuInteraction:previewForHighlightingMenuWithConfiguration:]):
(-[WKContentView contextMenuInteraction:previewForDismissingMenuWithConfiguration:]):
(-[WKContentView _ensureTargetedPreview]): Deleted.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@246859 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebKit/ChangeLog b/Source/WebKit/ChangeLog
index 8e7722a..a7082a9 100644
--- a/Source/WebKit/ChangeLog
+++ b/Source/WebKit/ChangeLog
@@ -1,3 +1,23 @@
+2019-06-26  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        [iPadOS] Fix another crash in -[UIPreviewTarget initWithContainer:center:transform:] when generating a fallback targeted preview
+        https://bugs.webkit.org/show_bug.cgi?id=199243
+        <rdar://problem/51554509>
+
+        Reviewed by Tim Horton.
+
+        Additionally make createFallbackTargetedPreview robust in the case where the content view is unparented, and
+        rename _ensureTargetedPreview to _createTargetedPreviewIfPossible, to reflect the fact that it may now return
+        nil.
+
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (createTargetedPreview):
+        (createFallbackTargetedPreview):
+        (-[WKContentView _createTargetedPreviewIfPossible]):
+        (-[WKContentView contextMenuInteraction:previewForHighlightingMenuWithConfiguration:]):
+        (-[WKContentView contextMenuInteraction:previewForDismissingMenuWithConfiguration:]):
+        (-[WKContentView _ensureTargetedPreview]): Deleted.
+
 2019-06-26  Alex Christensen  <achristensen@webkit.org>
 
         testRunner.setAlwaysAcceptCookies should wait for cookie accept policy to be set
diff --git a/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm b/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
index 06cce9c..1779bb1 100644
--- a/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
+++ b/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
@@ -7921,6 +7921,9 @@
 
 static RetainPtr<UITargetedPreview> createFallbackTargetedPreview(UIView *rootView, UIView *containerView, const WebCore::FloatRect& frameInRootViewCoordinates)
 {
+    if (!containerView.window)
+        return nil;
+
     auto parameters = adoptNS([[UIPreviewParameters alloc] init]);
     UIView *snapshotView = [rootView resizableSnapshotViewFromRect:frameInRootViewCoordinates afterScreenUpdates:NO withCapInsets:UIEdgeInsetsZero];
 
@@ -7933,7 +7936,7 @@
     return adoptNS([[UITargetedPreview alloc] initWithView:snapshotView parameters:parameters.get() target:target.get()]);
 }
 
-- (UITargetedPreview *)_ensureTargetedPreview
+- (UITargetedPreview *)_createTargetedPreviewIfPossible
 {
     if (_contextMenuInteractionTargetedPreview)
         return _contextMenuInteractionTargetedPreview.get();
@@ -7960,7 +7963,7 @@
 - (UITargetedPreview *)contextMenuInteraction:(UIContextMenuInteraction *)interaction previewForHighlightingMenuWithConfiguration:(UIContextMenuConfiguration *)configuration
 {
     [self _startSuppressingSelectionAssistantForReason:WebKit::InteractionIsHappening];
-    return [self _ensureTargetedPreview];
+    return [self _createTargetedPreviewIfPossible];
 }
 
 - (void)contextMenuInteractionWillPresent:(UIContextMenuInteraction *)interaction
@@ -7981,7 +7984,7 @@
 
 - (UITargetedPreview *)contextMenuInteraction:(UIContextMenuInteraction *)interaction previewForDismissingMenuWithConfiguration:(UIContextMenuConfiguration *)configuration
 {
-    return [self _ensureTargetedPreview];
+    return [self _createTargetedPreviewIfPossible];
 }
 
 - (void)contextMenuInteraction:(UIContextMenuInteraction *)interaction willCommitWithAnimator:(id<UIContextMenuInteractionCommitAnimating>)animator