Turns off previews on android if DisplayMode not Browser
Previews in android should not be presented without the full chrome
browser user treatment (eg, PWA launched from homescreen in standalone
mode). This CL blocks previews for navigations that do not originate
from an android Tab in kWebDisplayModeBrowser.
Bug: 818031
Change-Id: I88bbae7954daf503e49720adce094a9111790003
Reviewed-on: https://chromium-review.googlesource.com/953882
Reviewed-by: Yaron Friedman <[email protected]>
Reviewed-by: Kinuko Yasuda <[email protected]>
Reviewed-by: Peter Williamson <[email protected]>
Commit-Queue: Doug Arnett <[email protected]>
Cr-Commit-Position: refs/heads/master@{#548662}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappActivity.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappActivity.java
index a7518086..2c85df5 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappActivity.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappActivity.java
@@ -251,6 +251,10 @@
}
protected void initializeUI(Bundle savedInstanceState) {
+ // Make display mode available before page load.
+ getActivityTab().getTabWebContentsDelegateAndroid().setDisplayMode(
+ mWebappInfo.displayMode());
+
// We do not load URL when restoring from saved instance states.
if (savedInstanceState == null) {
getActivityTab().loadUrl(
@@ -260,8 +264,6 @@
}
getActivityTab().addObserver(createTabObserver());
- getActivityTab().getTabWebContentsDelegateAndroid().setDisplayMode(
- mWebappInfo.displayMode());
}
@Override
diff --git a/chrome/browser/android/tab_web_contents_delegate_android.cc b/chrome/browser/android/tab_web_contents_delegate_android.cc
index acd62999..a3b34ef 100644
--- a/chrome/browser/android/tab_web_contents_delegate_android.cc
+++ b/chrome/browser/android/tab_web_contents_delegate_android.cc
@@ -286,6 +286,14 @@
return app_modal::JavaScriptDialogManager::GetInstance();
}
+void TabWebContentsDelegateAndroid::AdjustPreviewsStateForNavigation(
+ content::WebContents* web_contents,
+ content::PreviewsState* previews_state) {
+ if (GetDisplayMode(web_contents) != blink::kWebDisplayModeBrowser) {
+ *previews_state = content::PREVIEWS_OFF;
+ }
+}
+
void TabWebContentsDelegateAndroid::RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
diff --git a/chrome/browser/android/tab_web_contents_delegate_android.h b/chrome/browser/android/tab_web_contents_delegate_android.h
index ce1f5c95..0e47c46 100644
--- a/chrome/browser/android/tab_web_contents_delegate_android.h
+++ b/chrome/browser/android/tab_web_contents_delegate_android.h
@@ -56,6 +56,9 @@
const gfx::RectF& active_rect) override;
content::JavaScriptDialogManager* GetJavaScriptDialogManager(
content::WebContents* source) override;
+ void AdjustPreviewsStateForNavigation(
+ content::WebContents* web_contents,
+ content::PreviewsState* previews_state) override;
void RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
diff --git a/chrome/browser/android/tab_web_contents_delegate_android_unittest.cc b/chrome/browser/android/tab_web_contents_delegate_android_unittest.cc
new file mode 100644
index 0000000..3b10d06
--- /dev/null
+++ b/chrome/browser/android/tab_web_contents_delegate_android_unittest.cc
@@ -0,0 +1,80 @@
+// Copyright 2018 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/android/tab_web_contents_delegate_android.h"
+
+#include "base/android/jni_android.h"
+#include "components/previews/core/previews_experiments.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+class TestTabWebContentsDelegateAndroid
+ : public android::TabWebContentsDelegateAndroid {
+ public:
+ explicit TestTabWebContentsDelegateAndroid(blink::WebDisplayMode display_mode)
+ : TabWebContentsDelegateAndroid(base::android::AttachCurrentThread(),
+ nullptr) {
+ display_mode_ = display_mode;
+ }
+
+ blink::WebDisplayMode GetDisplayMode(
+ const content::WebContents* web_contents) const override {
+ return display_mode_;
+ }
+
+ private:
+ blink::WebDisplayMode display_mode_ = blink::kWebDisplayModeBrowser;
+};
+
+} // namespace
+
+namespace android {
+
+TEST(TabWebContentsDelegateAndroidTest,
+ AdjustPreviewsStateForNavigationAllowsPreviews) {
+ TestTabWebContentsDelegateAndroid browser_display_delegate(
+ blink::kWebDisplayModeBrowser);
+ content::PreviewsState noscript_previews_state = content::NOSCRIPT_ON;
+ browser_display_delegate.AdjustPreviewsStateForNavigation(
+ nullptr, &noscript_previews_state);
+ EXPECT_EQ(content::NOSCRIPT_ON, noscript_previews_state);
+ content::PreviewsState lofi_previews_state = content::CLIENT_LOFI_ON;
+ browser_display_delegate.AdjustPreviewsStateForNavigation(
+ nullptr, &lofi_previews_state);
+ EXPECT_EQ(content::CLIENT_LOFI_ON, lofi_previews_state);
+}
+
+TEST(TabWebContentsDelegateAndroidTest,
+ AdjustPreviewsStateForNavigationBlocksPreviews) {
+ TestTabWebContentsDelegateAndroid standalone_display_delegate(
+ blink::kWebDisplayModeStandalone);
+ content::PreviewsState noscript_previews_state = content::NOSCRIPT_ON;
+ standalone_display_delegate.AdjustPreviewsStateForNavigation(
+ nullptr, &noscript_previews_state);
+ EXPECT_EQ(content::PREVIEWS_OFF, noscript_previews_state);
+
+ TestTabWebContentsDelegateAndroid fullscreen_display_delegate(
+ blink::kWebDisplayModeFullscreen);
+ content::PreviewsState lofi_previews_state = content::CLIENT_LOFI_ON;
+ fullscreen_display_delegate.AdjustPreviewsStateForNavigation(
+ nullptr, &lofi_previews_state);
+ EXPECT_EQ(content::PREVIEWS_OFF, lofi_previews_state);
+
+ TestTabWebContentsDelegateAndroid minimal_ui_display_delegate(
+ blink::kWebDisplayModeMinimalUi);
+ content::PreviewsState litepage_previews_state = content::SERVER_LITE_PAGE_ON;
+ minimal_ui_display_delegate.AdjustPreviewsStateForNavigation(
+ nullptr, &litepage_previews_state);
+ EXPECT_EQ(content::PREVIEWS_OFF, litepage_previews_state);
+
+ TestTabWebContentsDelegateAndroid undefined_display_delegate(
+ blink::kWebDisplayModeUndefined);
+ content::PreviewsState server_lofi_previews_state = content::SERVER_LOFI_ON;
+ undefined_display_delegate.AdjustPreviewsStateForNavigation(
+ nullptr, &server_lofi_previews_state);
+ EXPECT_EQ(content::PREVIEWS_OFF, server_lofi_previews_state);
+}
+
+} // namespace android
diff --git a/chrome/test/BUILD.gn b/chrome/test/BUILD.gn
index f486e52..becd4003 100644
--- a/chrome/test/BUILD.gn
+++ b/chrome/test/BUILD.gn
@@ -2216,6 +2216,7 @@
"../browser/android/history_report/delta_file_commons_unittest.cc",
"../browser/android/history_report/usage_reports_buffer_backend_unittest.cc",
"../browser/android/locale/special_locale_handler_unittest.cc",
+ "../browser/android/tab_web_contents_delegate_android_unittest.cc",
# TODO(newt): move this to test_support_unit?
"../browser/android/chrome_backup_agent_unittest.cc",
diff --git a/components/offline_pages/content/background_loader/background_loader_contents.cc b/components/offline_pages/content/background_loader/background_loader_contents.cc
index 17f1cdd..6f8f80c 100644
--- a/components/offline_pages/content/background_loader/background_loader_contents.cc
+++ b/components/offline_pages/content/background_loader/background_loader_contents.cc
@@ -125,6 +125,7 @@
}
void BackgroundLoaderContents::AdjustPreviewsStateForNavigation(
+ content::WebContents* web_contents,
content::PreviewsState* previews_state) {
DCHECK(previews_state);
diff --git a/components/offline_pages/content/background_loader/background_loader_contents.h b/components/offline_pages/content/background_loader/background_loader_contents.h
index 20828b438..53863a7 100644
--- a/components/offline_pages/content/background_loader/background_loader_contents.h
+++ b/components/offline_pages/content/background_loader/background_loader_contents.h
@@ -90,6 +90,7 @@
content::MediaStreamType type) override;
void AdjustPreviewsStateForNavigation(
+ content::WebContents* web_contents,
content::PreviewsState* previews_state) override;
private:
diff --git a/components/offline_pages/content/background_loader/background_loader_contents_unittest.cc b/components/offline_pages/content/background_loader/background_loader_contents_unittest.cc
index 7f45500..645ecd19 100644
--- a/components/offline_pages/content/background_loader/background_loader_contents_unittest.cc
+++ b/components/offline_pages/content/background_loader/background_loader_contents_unittest.cc
@@ -182,28 +182,28 @@
// If the state starts out as off or disabled, it should stay that way.
previews_state = content::PREVIEWS_OFF;
- contents()->AdjustPreviewsStateForNavigation(&previews_state);
+ contents()->AdjustPreviewsStateForNavigation(nullptr, &previews_state);
EXPECT_EQ(previews_state, content::PREVIEWS_OFF);
previews_state = content::PREVIEWS_NO_TRANSFORM;
- contents()->AdjustPreviewsStateForNavigation(&previews_state);
+ contents()->AdjustPreviewsStateForNavigation(nullptr, &previews_state);
EXPECT_EQ(previews_state, content::PREVIEWS_NO_TRANSFORM);
// If the state starts out as a state unfriendly to offlining, we should
// and out the unfriendly previews.
previews_state = content::SERVER_LOFI_ON | content::CLIENT_LOFI_ON;
- contents()->AdjustPreviewsStateForNavigation(&previews_state);
+ contents()->AdjustPreviewsStateForNavigation(nullptr, &previews_state);
EXPECT_EQ(previews_state, content::SERVER_LOFI_ON);
// If the state starts out as offlining friendly previews, we should preserve
// them.
previews_state = content::PARTIAL_CONTENT_SAFE_PREVIEWS;
- contents()->AdjustPreviewsStateForNavigation(&previews_state);
+ contents()->AdjustPreviewsStateForNavigation(nullptr, &previews_state);
EXPECT_EQ(previews_state, content::PARTIAL_CONTENT_SAFE_PREVIEWS);
// If there are only offlining unfriendly previews, they should all get turned
// off.
previews_state = content::CLIENT_LOFI_ON;
- contents()->AdjustPreviewsStateForNavigation(&previews_state);
+ contents()->AdjustPreviewsStateForNavigation(nullptr, &previews_state);
EXPECT_EQ(previews_state, content::PREVIEWS_OFF);
}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index 3d744549b..82876285 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -3320,7 +3320,7 @@
void WebContentsImpl::AdjustPreviewsStateForNavigation(
PreviewsState* previews_state) {
if (delegate_)
- delegate_->AdjustPreviewsStateForNavigation(previews_state);
+ delegate_->AdjustPreviewsStateForNavigation(this, previews_state);
}
InterstitialPageImpl* WebContentsImpl::GetInterstitialPage() const {
diff --git a/content/public/browser/web_contents_delegate.h b/content/public/browser/web_contents_delegate.h
index e78e4fa..cfb5ee45 100644
--- a/content/public/browser/web_contents_delegate.h
+++ b/content/public/browser/web_contents_delegate.h
@@ -556,8 +556,9 @@
virtual bool DoBrowserControlsShrinkBlinkSize() const;
// Give WebContentsDelegates the opportunity to adjust the previews state.
- virtual void AdjustPreviewsStateForNavigation(PreviewsState* previews_state) {
- }
+ virtual void AdjustPreviewsStateForNavigation(
+ content::WebContents* web_contents,
+ PreviewsState* previews_state) {}
// Requests to print an out-of-process subframe for the specified WebContents.
// |rect| is the rectangular area where its content resides in its parent