Fix some status bar issues

Fix two issues introduced by https://crrev.com/c/1568092
+ Scrim status bar color on pre-L. The old "parity" logic does not
  work now that we calculate the status bar color instead of passing in
  a previously used status bar base color.
+ In tab grid switcher, the url focus percentage can change before
  #onObservingDifferentTab() is notified, so we need to avoid calling
  #updateStatusBarColor() in #onUrlExpansionPercentageChanged() with
  the out-of-date tab.

Bug: 952752, 953665
Change-Id: I175b1e2d3136fd5dc190d4ec8cdba2f88888c23d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1572702
Commit-Queue: Becky Zhou <huayinz@chromium.org>
Reviewed-by: Theresa <twellington@chromium.org>
Reviewed-by: Matthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#652588}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ui/system/StatusBarColorController.java b/chrome/android/java/src/org/chromium/chrome/browser/ui/system/StatusBarColorController.java
index df32bb6..3a5951b 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ui/system/StatusBarColorController.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ui/system/StatusBarColorController.java
@@ -88,6 +88,7 @@
     private float mStatusBarScrimFraction;
 
     private float mToolbarUrlExpansionPercentage;
+    private boolean mShouldUpdateStatusBarColorForNTP;
 
     /**
      * @param chromeActivity The {@link ChromeActivity} that this class is attached to.
@@ -99,7 +100,7 @@
         mStatusBarColorProvider = chromeActivity;
         mStatusBarScrimDelegate = (fraction) -> {
             mStatusBarScrimFraction = fraction;
-            updateStatusBarColor(null);
+            updateStatusBarColor(mCurrentTab);
         };
 
         Resources resources = chromeActivity.getResources();
@@ -121,8 +122,30 @@
             }
 
             @Override
+            public void onContentChanged(Tab tab) {
+                final boolean newShouldUpdateStatusBarColorForNTP = isStandardNTP();
+                // Also update the status bar color if the content was previously an NTP, because an
+                // NTP can use a different status bar color than the default theme color. In this
+                // case, the theme color might not change, and thus #onDidChangeThemeColor might
+                // not get called.
+                if (mShouldUpdateStatusBarColorForNTP || newShouldUpdateStatusBarColorForNTP) {
+                    updateStatusBarColor(tab);
+                }
+                mShouldUpdateStatusBarColorForNTP = newShouldUpdateStatusBarColorForNTP;
+            }
+
+            @Override
+            public void onDestroyed(Tab tab) {
+                // Make sure that #mCurrentTab is cleared because #onObservingDifferentTab() might
+                // not be notified early enough when #onUrlExpansionPercentageChanged() is called.
+                mCurrentTab = null;
+                mShouldUpdateStatusBarColorForNTP = false;
+            }
+
+            @Override
             protected void onObservingDifferentTab(Tab tab) {
                 mCurrentTab = tab;
+                mShouldUpdateStatusBarColorForNTP = isStandardNTP();
 
                 // |tab == null| means we're switching tabs - by the tab switcher or by swiping
                 // on the omnibox. These cases are dealt with differently, elsewhere.
@@ -180,7 +203,7 @@
     @Override
     public void onUrlExpansionPercentageChanged(float percentage) {
         mToolbarUrlExpansionPercentage = percentage;
-        if (isStandardNTP()) updateStatusBarColor(mCurrentTab);
+        if (mShouldUpdateStatusBarColorForNTP) updateStatusBarColor(mCurrentTab);
     }
 
     /**