Skip to content

Commit

Permalink
Fix white flash in navigation initial
Browse files Browse the repository at this point in the history
The page show white color in navigation initial. For dark mode, this excepted a dark color.
If developer don't set WebView background color, just show a black background in dark mode.

Bug: 1098235
Change-Id: I9b221d0a899d228c71f6ce3b9a46c9dda45c4536
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2256921
Commit-Queue: Richard Coles <torne@chromium.org>
Reviewed-by: Richard Coles <torne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#788780}
  • Loading branch information
XiangYang authored and Commit Bot committed Jul 15, 2020
1 parent 1166357 commit 136eaf2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -1182,4 +1182,5 @@ Venture 3 Systems LLC <*@venture3systems.com>
Vewd Software AS <*@vewd.com>
Vivaldi Technologies AS <*@vivaldi.com>
Yandex LLC <*@yandex-team.ru>
XiangYang <yangxiang12@huawei.com>
# END organizations section.
17 changes: 11 additions & 6 deletions android_webview/browser/aw_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ AwSettings::AwSettings(JNIEnv* env,
javascript_can_open_windows_automatically_(false),
allow_third_party_cookies_(false),
allow_file_access_(false),
is_dark_mode_(false),
aw_settings_(env, obj) {
web_contents->SetUserData(kAwSettingsUserDataKey,
std::make_unique<AwSettingsUserData>(this));
Expand Down Expand Up @@ -515,24 +516,23 @@ void AwSettings::PopulateWebPreferencesLocked(JNIEnv* env,
web_prefs->allow_mixed_content_upgrades =
Java_AwSettings_getAllowMixedContentAutoupgradesLocked(env, obj);

bool is_dark_mode;
switch (Java_AwSettings_getForceDarkModeLocked(env, obj)) {
case ForceDarkMode::FORCE_DARK_OFF:
is_dark_mode = false;
is_dark_mode_ = false;
break;
case ForceDarkMode::FORCE_DARK_ON:
is_dark_mode = true;
is_dark_mode_ = true;
break;
case ForceDarkMode::FORCE_DARK_AUTO: {
AwContents* contents = AwContents::FromWebContents(web_contents());
is_dark_mode = contents && contents->GetViewTreeForceDarkState();
is_dark_mode_ = contents && contents->GetViewTreeForceDarkState();
break;
}
}
web_prefs->preferred_color_scheme = is_dark_mode
web_prefs->preferred_color_scheme = is_dark_mode_
? blink::PreferredColorScheme::kDark
: blink::PreferredColorScheme::kLight;
if (is_dark_mode) {
if (is_dark_mode_) {
switch (Java_AwSettings_getForceDarkBehaviorLocked(env, obj)) {
case ForceDarkBehavior::FORCE_DARK_ONLY: {
web_prefs->preferred_color_scheme = blink::PreferredColorScheme::kLight;
Expand Down Expand Up @@ -562,6 +562,11 @@ void AwSettings::PopulateWebPreferencesLocked(JNIEnv* env,
}
}

bool AwSettings::IsDarkMode(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
return is_dark_mode_;
}

bool AwSettings::GetAllowFileAccess() {
return allow_file_access_;
}
Expand Down
3 changes: 3 additions & 0 deletions android_webview/browser/aw_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class AwSettings : public content::WebContentsObserver {

void PopulateWebPreferences(content::WebPreferences* web_prefs);
bool GetAllowFileAccess();
bool IsDarkMode(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj);

private:
AwRenderViewHostExt* GetAwRenderViewHostExt();
Expand All @@ -99,6 +101,7 @@ class AwSettings : public content::WebContentsObserver {
bool javascript_can_open_windows_automatically_;
bool allow_third_party_cookies_;
bool allow_file_access_;
bool is_dark_mode_;

JavaObjectWeakGlobalRef aw_settings_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ public abstract static class VisualStateCallback {
// The base background color, i.e. not accounting for any CSS body from the current page.
private int mBaseBackgroundColor = Color.WHITE;

// Did background set by developer, now used for dark mode.
private boolean mDidInitBackground;

// Must call AwContentsJni.get().updateLastHitTestData first to update this before use.
private final HitTestData mPossiblyStaleHitTestData = new HitTestData();

Expand Down Expand Up @@ -2034,6 +2037,7 @@ public void requestFocus() {

public void setBackgroundColor(int color) {
mBaseBackgroundColor = color;
mDidInitBackground = true;
if (!isDestroyed(WARN)) {
AwContentsJni.get().setBackgroundColor(mNativeAwContents, AwContents.this, color);
}
Expand All @@ -2046,11 +2050,23 @@ public void setLayerType(int layerType, Paint paint) {
mAwViewMethods.setLayerType(layerType, paint);
}

@VisibleForTesting
public int getEffectiveBackgroundColorForTesting() {
return getEffectiveBackgroundColor();
}

int getEffectiveBackgroundColor() {
// Do not ask the WebContents for the background color, as it will always
// report white prior to initial navigation or post destruction, whereas we want
// to use the client supplied base value in those cases.
if (isDestroyed(NO_WARN) || !mContentsClient.isCachedRendererBackgroundColorValid()) {
if (isDestroyed(NO_WARN)) {
return mBaseBackgroundColor;
} else if (!mContentsClient.isCachedRendererBackgroundColorValid()) {
// In force dark mode, if background color not set, this cause a white flash,
// just show black background.
if (mSettings.isDarkMode() && !mDidInitBackground) {
return Color.BLACK;
}
return mBaseBackgroundColor;
}
return mContentsClient.getCachedRendererBackgroundColor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,13 @@ public void setForceDarkMode(@ForceDarkMode int forceDarkMode) {
}
}

public boolean isDarkMode() {
synchronized (mAwSettingsLock) {
assert mNativeAwSettings != 0;
return AwSettingsJni.get().isDarkMode(mNativeAwSettings, AwSettings.this);
}
}

@ForceDarkBehavior
public int getForceDarkBehavior() {
synchronized (mAwSettingsLock) {
Expand Down Expand Up @@ -1929,5 +1936,6 @@ void populateWebPreferencesLocked(
void updateWillSuppressErrorStateLocked(long nativeAwSettings, AwSettings caller);
void updateCookiePolicyLocked(long nativeAwSettings, AwSettings caller);
void updateAllowFileAccessLocked(long nativeAwSettings, AwSettings caller);
boolean isDarkMode(long nativeAwSettings, AwSettings caller);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,29 @@ public void testUseAwSettingsAfterDestroy() throws Throwable {
Assert.assertEquals(newBlockNetworkLoads, awSettings.getBlockNetworkLoads());
}

@Test
@SmallTest
@Feature({"AndroidWebView"})
public void testBackgroundColorInDarkMode() throws Throwable {
mActivityTestRule.runOnUiThread(() -> {
AwContents awContents =
mActivityTestRule.createAwTestContainerView(mContentsClient).getAwContents();
AwSettings awSettings = awContents.getSettings();

Assert.assertEquals(awContents.getEffectiveBackgroundColorForTesting(), Color.WHITE);

awSettings.setForceDarkMode(AwSettings.FORCE_DARK_ON);
Assert.assertTrue(awSettings.isDarkMode());
Assert.assertEquals(awContents.getEffectiveBackgroundColorForTesting(), Color.BLACK);

awContents.setBackgroundColor(Color.RED);
Assert.assertEquals(awContents.getEffectiveBackgroundColorForTesting(), Color.RED);

awContents.destroy();
Assert.assertEquals(awContents.getEffectiveBackgroundColorForTesting(), Color.RED);
});
}

private int callDocumentHasImagesSync(final AwContents awContents)
throws Throwable, InterruptedException {
// Set up a container to hold the result object and a semaphore to
Expand Down

0 comments on commit 136eaf2

Please sign in to comment.