[iOS] Fix scroll end adjustment for hiding toolbar.

This CL updates BVC's fullscreen scroll end adjustment animation to
only update the top content padding early for header-hiding animations
that reveal beyond the top of the rendered content.  All other
scroll end adjustments can update the top content padding in the
animation's completion block.

Bug: 800697
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I3c53e39e6aec1f3ddad2c83aed9c109221780c49
Reviewed-on: https://chromium-review.googlesource.com/922269
Reviewed-by: Justin Cohen <justincohen@chromium.org>
Commit-Queue: Kurt Horimoto <kkhorimoto@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#538542}(cherry picked from commit 458bc43f9505182db53ad3e7368fc73b35ba3fde)
Reviewed-on: https://chromium-review.googlesource.com/935221
Reviewed-by: Kurt Horimoto <kkhorimoto@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#574}
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 71d65b2..a2253bc 100644
--- a/ios/chrome/browser/ui/browser_view_controller.mm
+++ b/ios/chrome/browser/ui/browser_view_controller.mm
@@ -3947,29 +3947,39 @@
 
 - (void)finishFullscreenScrollWithAnimator:
     (FullscreenScrollEndAnimator*)animator {
-  BOOL showingToolbar = animator.finalProgress > animator.startProgress;
+  // If the headers are being hidden, it's possible that this will reveal a
+  // portion of the webview beyond the top of the page's rendered content.  In
+  // order to prevent that, update the top padding and content before the
+  // animation begins.
   CGFloat finalProgress = animator.finalProgress;
-  // WKWebView does not re-render its content until its model layer's bounds
-  // have been updated at the end of the animation.  If the animator is going
-  // to hide the toolbar, update the content view's top padding early so that
-  // content is correctly rendered behind the toolbar that's being animated
-  // away.
-  if (!showingToolbar)
-    [self updateContentViewTopPaddingForFullscreenProgress:finalProgress];
-  [animator addAnimations:^{
-    [self updateHeadersForFullscreenProgress:finalProgress];
-    [self updateFootersForFullscreenProgress:finalProgress];
-  }];
-  // If the toolbar is being animated to become visible, update the content view
-  // top padding in the completion block so that fixed-position elements can be
-  // properly laid out in the new viewport.
-  if (showingToolbar) {
-    __weak FullscreenScrollEndAnimator* weakAnimator = animator;
-    [animator addCompletion:^(UIViewAnimatingPosition finalPosition) {
-      [self updateContentViewTopPaddingForFullscreenProgress:
-                [weakAnimator progressForAnimatingPosition:finalPosition]];
-    }];
+  BOOL hidingHeaders = animator.finalProgress < animator.startProgress;
+  if (hidingHeaders) {
+    id<CRWWebViewProxy> webProxy = self.currentWebState->GetWebViewProxy();
+    CRWWebViewScrollViewProxy* scrollProxy = webProxy.scrollViewProxy;
+    CGPoint contentOffset = scrollProxy.contentOffset;
+    if (contentOffset.y - scrollProxy.contentInset.top <
+        webProxy.topContentPadding) {
+      [self updateContentViewTopPaddingForFullscreenProgress:finalProgress];
+      contentOffset.y = -scrollProxy.contentInset.top;
+      scrollProxy.contentOffset = contentOffset;
+    }
   }
+
+  // Add animations to update the headers and footers.
+  __weak BrowserViewController* weakSelf = self;
+  [animator addAnimations:^{
+    [weakSelf updateHeadersForFullscreenProgress:finalProgress];
+    [weakSelf updateFootersForFullscreenProgress:finalProgress];
+  }];
+
+  // Animating layout changes of the rendered content in the WKWebView is not
+  // supported, so update the content padding in the completion block of the
+  // animator to trigger a rerender in the page's new viewport.
+  __weak FullscreenScrollEndAnimator* weakAnimator = animator;
+  [animator addCompletion:^(UIViewAnimatingPosition finalPosition) {
+    [weakSelf updateContentViewTopPaddingForFullscreenProgress:
+                  [weakAnimator progressForAnimatingPosition:finalPosition]];
+  }];
 }
 
 - (void)scrollFullscreenToTopWithAnimator: