[ios] Fixes more issues where the BVC is over-dismissed.

BUG=811671

Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I7a8b9a649db4970b208e18419090dcbaa2eb64db
Reviewed-on: https://chromium-review.googlesource.com/927121
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#538111}(cherry picked from commit f46de8c1c857b9913a9e5673f2f77c96ffdc5ec0)
Reviewed-on: https://chromium-review.googlesource.com/934602
Reviewed-by: Rohit Rao <rohitrao@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#570}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/ios/chrome/browser/ui/browser_view_controller.mm b/ios/chrome/browser/ui/browser_view_controller.mm
index b640c0c..d364312 100644
--- a/ios/chrome/browser/ui/browser_view_controller.mm
+++ b/ios/chrome/browser/ui/browser_view_controller.mm
@@ -613,11 +613,6 @@
   // is used to determine whether the pre-rendering animation should be played
   // or not.
   BOOL _insertedTabWasPrerenderedTab;
-
-  // A pointer to the most recently presented UIDocumentMenuViewController.
-  // Used to work around https://crbug.com/801165.
-  __weak UIDocumentMenuViewController*
-      _lastPresentedUIDocumentMenuViewController;
 }
 
 // The browser's side swipe controller.  Lazily instantiated on the first call.
@@ -1763,19 +1758,22 @@
 
 - (void)dismissViewControllerAnimated:(BOOL)flag
                            completion:(void (^)())completion {
-  if (!base::ios::IsRunningOnIOS11OrLater() &&
-      _lastPresentedUIDocumentMenuViewController &&
-      !self.presentedViewController) {
+  if (TabSwitcherPresentsBVCEnabled() && !self.presentedViewController) {
     // TODO(crbug.com/801165): On iOS10, UIDocumentMenuViewController and
     // WKFileUploadPanel somehow combine to call dismiss twice instead of once.
     // The second call would dismiss the BVC itself, so look for that case and
     // return early.
+    //
+    // TODO(crbug.com/811671): A similar bug exists on all iOS versions with
+    // WKFileUploadPanel and UIDocumentPickerViewController.
+    //
+    // To make M65 as safe as possible, return early whenever this method is
+    // invoked but no VC appears to be presented.  These cases will always end
+    // up dismissing the BVC itself, which would put the app into an
+    // unresponsive state.
     return;
   }
 
-  // It is an error to call this method when no VC is being presented.
-  DCHECK(!TabSwitcherPresentsBVCEnabled() || self.presentedViewController);
-
   // Some calling code invokes |dismissViewControllerAnimated:completion:|
   // multiple times.  When the BVC is displayed using VC containment, multiple
   // calls are effectively idempotent because only the first call has any effect
@@ -1859,20 +1857,6 @@
     [self.sideSwipeController resetContentView];
   }
 
-  // TODO(crbug.com/801165): Workaround for an iOS10 bug.
-  // UIDocumentMenuViewController, when used via WKFileUploadPanel,
-  // over-dismisses itself, which triggers a DCHECK in
-  // dismissViewController:animated:.  If a UIDocumentMenuViewController is
-  // being presented here, save a weak pointer that can be tested later when
-  // dismiss is called.
-  if (!base::ios::IsRunningOnIOS11OrLater() &&
-      [viewControllerToPresent
-          isKindOfClass:[UIDocumentMenuViewController class]]) {
-    _lastPresentedUIDocumentMenuViewController =
-        base::mac::ObjCCastStrict<UIDocumentMenuViewController>(
-            viewControllerToPresent);
-  }
-
   [super presentViewController:viewControllerToPresent
                       animated:flag
                     completion:finalCompletionHandler];