[iOS] Disable fullscreen for splash screen reloads.

Some splash screens that are designed to fit the viewport of the webview
have their content size increase by the safe area insets upon reloading.
In order to continue disabling fullscreen for these pages, this CL
updates the disabling threshold by the top and bottom safe area insets.
While this strategy disables fullscreen more aggressively, it is better
to mistakenly disable fullscreen for short pages (that are slightly
longer than the scroll view) than it is to allow the animation issues
that sometimes occur when these pages are scrolled.

Bug: 924807
Change-Id: I5070b26931fdd6103f21f475a794ecb52b9e5360
Reviewed-on: https://chromium-review.googlesource.com/c/1433141
Commit-Queue: Kurt Horimoto <kkhorimoto@chromium.org>
Reviewed-by: Gauthier Ambard <gambard@chromium.org>
Auto-Submit: Kurt Horimoto <kkhorimoto@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#627777}(cherry picked from commit 4fe064ae2732d1f6986ebe01116da27a27878268)
Reviewed-on: https://chromium-review.googlesource.com/c/1450024
Reviewed-by: Kurt Horimoto <kkhorimoto@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#133}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/ios/chrome/browser/ui/fullscreen/fullscreen_model.h b/ios/chrome/browser/ui/fullscreen/fullscreen_model.h
index 4cd9cb8..2861938 100644
--- a/ios/chrome/browser/ui/fullscreen/fullscreen_model.h
+++ b/ios/chrome/browser/ui/fullscreen/fullscreen_model.h
@@ -147,6 +147,10 @@
   void SetResizesScrollView(bool resizes_scroll_view);
   bool ResizesScrollView() const;
 
+  // Setter for the safe area insets for the current WebState's view.
+  void SetWebViewSafeAreaInsets(UIEdgeInsets safe_area_insets);
+  UIEdgeInsets GetWebViewSafeAreaInsets() const;
+
  private:
   // Returns how a scroll to the current |y_content_offset_| from |from_offset|
   // should be handled.
@@ -220,6 +224,8 @@
   bool ignoring_current_scroll_ = false;
   // Whether the scroll view is resized for fullscreen events.
   bool resizes_scroll_view_ = false;
+  // The WebState view's safe area insets.
+  UIEdgeInsets safe_area_insets_ = UIEdgeInsetsZero;
   // The number of FullscreenModelObserver callbacks currently being executed.
   size_t observer_callback_count_ = 0;
 
diff --git a/ios/chrome/browser/ui/fullscreen/fullscreen_model.mm b/ios/chrome/browser/ui/fullscreen/fullscreen_model.mm
index daa1aad..b639cb6 100644
--- a/ios/chrome/browser/ui/fullscreen/fullscreen_model.mm
+++ b/ios/chrome/browser/ui/fullscreen/fullscreen_model.mm
@@ -228,6 +228,17 @@
   return resizes_scroll_view_;
 }
 
+void FullscreenModel::SetWebViewSafeAreaInsets(UIEdgeInsets safe_area_insets) {
+  if (UIEdgeInsetsEqualToEdgeInsets(safe_area_insets_, safe_area_insets))
+    return;
+  safe_area_insets_ = safe_area_insets;
+  UpdateDisabledCounterForContentHeight();
+}
+
+UIEdgeInsets FullscreenModel::GetWebViewSafeAreaInsets() const {
+  return safe_area_insets_;
+}
+
 FullscreenModel::ScrollAction FullscreenModel::ActionForScrollFromOffset(
     CGFloat from_offset) const {
   // Update the base offset but don't recalculate progress if:
@@ -279,6 +290,14 @@
     // resized to account for the viewport insets after the page has been
     // rendered, so account for the maximum toolbar insets in the threshold.
     disabling_threshold += expanded_toolbar_height_ + bottom_toolbar_height_;
+  } else {
+    // After reloads, pages whose viewports fit the screen are sometimes resized
+    // to account for the safe area insets.  Adding these to the threshold helps
+    // prevent fullscreen from beeing re-enabled in this case.
+    // TODO(crbug.com/924807): This logic can potentially disable fullscreen for
+    // short pages in which this bug does not occur.  It should be removed once
+    // the page can be reloaded without resizing.
+    disabling_threshold += safe_area_insets_.top + safe_area_insets_.bottom;
   }
 
   // Don't disable fullscreen if both heights have not been received.
diff --git a/ios/chrome/browser/ui/fullscreen/fullscreen_web_view_resizer.mm b/ios/chrome/browser/ui/fullscreen/fullscreen_web_view_resizer.mm
index 41a8611..e083962 100644
--- a/ios/chrome/browser/ui/fullscreen/fullscreen_web_view_resizer.mm
+++ b/ios/chrome/browser/ui/fullscreen/fullscreen_web_view_resizer.mm
@@ -93,6 +93,7 @@
     return;
 
   [self updateForInsets:self.model->GetToolbarInsetsAtProgress(progress)];
+  self.model->SetWebViewSafeAreaInsets(self.webState->GetView().safeAreaInsets);
 }
 
 // Updates the WebState view, resizing it such as |insets| is the insets between