forked from chromium/chromium
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If an extension does a content injection disable bf cache.
This code tracks whether a content injection (insertCSS, contentScript, executeScript) has occurred for a WebFrame. If so then turn off BFCache for the frame. BUG=1192785 Change-Id: I682a9efb247aae358023e3a591368c84d47001ce Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2787195 Reviewed-by: Reilly Grant <reillyg@chromium.org> Reviewed-by: Takashi Toyoshima <toyoshim@chromium.org> Reviewed-by: Dave Tapuska <dtapuska@chromium.org> Reviewed-by: Avi Drissman <avi@chromium.org> Reviewed-by: Kentaro Hara <haraken@chromium.org> Reviewed-by: Kouhei Ueno <kouhei@chromium.org> Commit-Queue: Dave Tapuska <dtapuska@chromium.org> Cr-Commit-Position: refs/heads/master@{#870582}
- Loading branch information
Showing
23 changed files
with
306 additions
and
48 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
chrome/browser/extensions/back_forward_cache_browsertest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright 2021 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/extensions/extension_browsertest.h" | ||
#include "chrome/test/base/ui_test_utils.h" | ||
#include "content/public/common/content_features.h" | ||
#include "content/public/test/browser_test.h" | ||
#include "extensions/common/extension.h" | ||
#include "net/dns/mock_host_resolver.h" | ||
|
||
namespace extensions { | ||
|
||
class ExtensionBackForwardCacheBrowserTest : public ExtensionBrowserTest { | ||
public: | ||
explicit ExtensionBackForwardCacheBrowserTest( | ||
bool allow_content_scripts = true) { | ||
feature_list_.InitWithFeaturesAndParameters( | ||
{{features::kBackForwardCache, | ||
{{"content_injection_supported", | ||
allow_content_scripts ? "true" : "false"}}}}, | ||
{features::kBackForwardCacheMemoryControls}); | ||
} | ||
|
||
void SetUpOnMainThread() override { | ||
host_resolver()->AddRule("*", "127.0.0.1"); | ||
ExtensionBrowserTest::SetUpOnMainThread(); | ||
} | ||
|
||
private: | ||
base::test::ScopedFeatureList feature_list_; | ||
}; | ||
|
||
class ExtensionBackForwardCacheContentScriptDisabledBrowserTest | ||
: public ExtensionBackForwardCacheBrowserTest { | ||
public: | ||
ExtensionBackForwardCacheContentScriptDisabledBrowserTest() | ||
: ExtensionBackForwardCacheBrowserTest(/*allow_content_scripts=*/false) {} | ||
}; | ||
|
||
IN_PROC_BROWSER_TEST_F( | ||
ExtensionBackForwardCacheContentScriptDisabledBrowserTest, | ||
ScriptDisallowed) { | ||
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("back_forward_cache") | ||
.AppendASCII("content_script"))); | ||
|
||
ASSERT_TRUE(embedded_test_server()->Start()); | ||
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html")); | ||
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html")); | ||
|
||
// 1) Navigate to A. | ||
content::RenderFrameHost* rfh_a = | ||
ui_test_utils::NavigateToURL(browser(), url_a); | ||
content::RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a); | ||
|
||
// 2) Navigate to B. | ||
content::RenderFrameHost* rfh_b = | ||
ui_test_utils::NavigateToURL(browser(), url_b); | ||
content::RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b); | ||
|
||
// Expect that |rfh_a| is destroyed as it wouldn't be placed in the cache. | ||
EXPECT_TRUE(delete_observer_rfh_a.deleted()); | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(ExtensionBackForwardCacheBrowserTest, ScriptAllowed) { | ||
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("back_forward_cache") | ||
.AppendASCII("content_script"))); | ||
|
||
ASSERT_TRUE(embedded_test_server()->Start()); | ||
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html")); | ||
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html")); | ||
|
||
// 1) Navigate to A. | ||
content::RenderFrameHost* rfh_a = | ||
ui_test_utils::NavigateToURL(browser(), url_a); | ||
content::RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a); | ||
|
||
// 2) Navigate to B. | ||
content::RenderFrameHost* rfh_b = | ||
ui_test_utils::NavigateToURL(browser(), url_b); | ||
content::RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b); | ||
|
||
// Ensure that |rfh_a| is in the cache. | ||
EXPECT_FALSE(delete_observer_rfh_a.deleted()); | ||
EXPECT_NE(rfh_a, rfh_b); | ||
EXPECT_TRUE(rfh_a->IsInBackForwardCache()); | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F( | ||
ExtensionBackForwardCacheContentScriptDisabledBrowserTest, | ||
CSSDisallowed) { | ||
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("back_forward_cache") | ||
.AppendASCII("content_css"))); | ||
|
||
ASSERT_TRUE(embedded_test_server()->Start()); | ||
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html")); | ||
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html")); | ||
|
||
// 1) Navigate to A. | ||
content::RenderFrameHost* rfh_a = | ||
ui_test_utils::NavigateToURL(browser(), url_a); | ||
content::RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a); | ||
|
||
// 2) Navigate to B. | ||
content::RenderFrameHost* rfh_b = | ||
ui_test_utils::NavigateToURL(browser(), url_b); | ||
content::RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b); | ||
|
||
// Expect that |rfh_a| is destroyed as it wouldn't be placed in the cache. | ||
EXPECT_TRUE(delete_observer_rfh_a.deleted()); | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(ExtensionBackForwardCacheBrowserTest, CSSAllowed) { | ||
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("back_forward_cache") | ||
.AppendASCII("content_css"))); | ||
|
||
ASSERT_TRUE(embedded_test_server()->Start()); | ||
GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html")); | ||
GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html")); | ||
|
||
// 1) Navigate to A. | ||
content::RenderFrameHost* rfh_a = | ||
ui_test_utils::NavigateToURL(browser(), url_a); | ||
content::RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a); | ||
|
||
// 2) Navigate to B. | ||
content::RenderFrameHost* rfh_b = | ||
ui_test_utils::NavigateToURL(browser(), url_b); | ||
content::RenderFrameDeletedObserver delete_observer_rfh_b(rfh_b); | ||
|
||
// Ensure that |rfh_a| is in the cache. | ||
EXPECT_FALSE(delete_observer_rfh_a.deleted()); | ||
EXPECT_NE(rfh_a, rfh_b); | ||
EXPECT_TRUE(rfh_a->IsInBackForwardCache()); | ||
} | ||
|
||
} // namespace extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
chrome/test/data/extensions/back_forward_cache/content_css/color.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2021 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. | ||
|
||
body {background-color: cyan;} |
14 changes: 14 additions & 0 deletions
14
chrome/test/data/extensions/back_forward_cache/content_css/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "no caching", | ||
"version": "0.1", | ||
"manifest_version": 2, | ||
"description": "Checks that injected CSS do prevent back forward cache.", | ||
"permissions": ["http://*/*", "https://*/*"], | ||
"content_scripts": [ | ||
{ | ||
"matches": ["http://*/*", "https://*/*"], | ||
"css": ["color.css"], | ||
"run_at": "document_start" | ||
} | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
chrome/test/data/extensions/back_forward_cache/content_script/change_page_title.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2021 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. | ||
|
||
document.title = "modified"; |
14 changes: 14 additions & 0 deletions
14
chrome/test/data/extensions/back_forward_cache/content_script/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "no caching", | ||
"version": "0.1", | ||
"manifest_version": 2, | ||
"description": "Checks that content scripts do prevent back forward cache.", | ||
"permissions": ["http://*/*", "https://*/*"], | ||
"content_scripts": [ | ||
{ | ||
"matches": ["http://*/*", "https://*/*"], | ||
"js": ["change_page_title.js"], | ||
"run_at": "document_end" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.