Index: chrome/browser/extensions/active_script_controller_unittest.cc |
diff --git a/chrome/browser/extensions/active_script_controller_unittest.cc b/chrome/browser/extensions/active_script_controller_unittest.cc |
index 01fe26ba0cf72bcc319482f4dd6684c120a1d75c..15c860783e02abc7c316fb0b35588925ca06d185 100644 |
--- a/chrome/browser/extensions/active_script_controller_unittest.cc |
+++ b/chrome/browser/extensions/active_script_controller_unittest.cc |
@@ -42,9 +42,17 @@ class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness { |
ActiveScriptControllerUnitTest(); |
virtual ~ActiveScriptControllerUnitTest(); |
- // Creates an extension with all hosts permission and adds it to the registry. |
+ // Creates an extension with a generated id and adds it to the registry. |
const Extension* AddExtension(); |
+ // Creates an extension with |id| and all host permission and adds it to the |
+ // registry. |
+ const Extension* AddExtension(const std::string& id); |
+ |
+ // Reloads the extension with |id| by removing it from the registry and |
+ // recreating a new extension with the same |id|. |
+ const Extension* ReloadExtension(const std::string& id); |
+ |
// Returns true if the |extension| requires user consent before injecting |
// a script. |
bool RequiresUserConsent(const Extension* extension) const; |
@@ -52,6 +60,9 @@ class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness { |
// Request an injection for the given |extension|. |
void RequestInjection(const Extension* extension); |
+ // Simulate clicking "always run" menu item for |extension|. |
+ void AlwaysRun(const Extension* extension); |
+ |
// Returns the number of times a given extension has had a script execute. |
size_t GetExecutionCountForExtension(const std::string& extension_id) const; |
@@ -90,7 +101,11 @@ ActiveScriptControllerUnitTest::~ActiveScriptControllerUnitTest() { |
} |
const Extension* ActiveScriptControllerUnitTest::AddExtension() { |
- const std::string kId = id_util::GenerateId("all_hosts_extension"); |
+ return AddExtension(id_util::GenerateId("all_hosts_extension")); |
+} |
+ |
+const Extension* ActiveScriptControllerUnitTest::AddExtension( |
+ const std::string& id) { |
scoped_refptr<const Extension> extension = |
ExtensionBuilder() |
.SetManifest( |
@@ -102,7 +117,7 @@ const Extension* ActiveScriptControllerUnitTest::AddExtension() { |
.Set("permissions", |
ListBuilder().Append(kAllHostsPermission))) |
.SetLocation(Manifest::INTERNAL) |
- .SetID(kId) |
+ .SetID(id) |
.Build(); |
ExtensionRegistry::Get(profile())->AddEnabled(extension); |
@@ -110,6 +125,13 @@ const Extension* ActiveScriptControllerUnitTest::AddExtension() { |
return extension; |
} |
+const Extension* ActiveScriptControllerUnitTest::ReloadExtension( |
+ const std::string& id) { |
+ std::string extension_id = id; |
+ ExtensionRegistry::Get(profile())->RemoveEnabled(extension_id); |
+ return AddExtension(extension_id); |
+} |
+ |
bool ActiveScriptControllerUnitTest::RequiresUserConsent( |
const Extension* extension) const { |
PermissionsData::AccessType access_type = |
@@ -127,6 +149,10 @@ void ActiveScriptControllerUnitTest::RequestInjection( |
GetExecutionCallbackForExtension(extension->id())); |
} |
+void ActiveScriptControllerUnitTest::AlwaysRun(const Extension* extension) { |
+ controller()->AlwaysRunOnVisibleHost(extension); |
+} |
+ |
size_t ActiveScriptControllerUnitTest::GetExecutionCountForExtension( |
const std::string& extension_id) const { |
std::map<std::string, int>::const_iterator iter = |
@@ -322,4 +348,61 @@ TEST_F(ActiveScriptControllerUnitTest, ActiveScriptsCanHaveAllUrlsPref) { |
EXPECT_TRUE(RequiresUserConsent(extension)); |
} |
+TEST_F(ActiveScriptControllerUnitTest, TestAlwaysRun) { |
+ const Extension* extension = AddExtension(); |
+ ASSERT_TRUE(extension); |
+ |
+ NavigateAndCommit(GURL("https://www.google.com/?gws_rd=ssl")); |
+ |
+ // Ensure that there aren't any executions pending. |
+ ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
+ ASSERT_FALSE(controller()->GetActionForExtension(extension)); |
+ |
+ // Since the extension requests all_hosts, we should require user consent. |
+ EXPECT_TRUE(RequiresUserConsent(extension)); |
+ |
+ // Request an injection. There should be an action visible, but no executions. |
+ RequestInjection(extension); |
+ EXPECT_TRUE(controller()->GetActionForExtension(extension)); |
+ EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
+ |
+ AlwaysRun(extension); |
+ |
+ // The extension should execute, and the action should go away. |
+ EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
+ EXPECT_FALSE(controller()->GetActionForExtension(extension)); |
+ |
+ // Since we already executed on the given page, we shouldn't need permission |
+ // for a second time. |
+ EXPECT_FALSE(RequiresUserConsent(extension)); |
+ |
+ // Navigating to another site that hasn't been granted a persisted permission |
+ // should necessitate user consent. |
+ NavigateAndCommit(GURL("https://www.foo.com/bar")); |
+ EXPECT_TRUE(RequiresUserConsent(extension)); |
+ |
+ // We shouldn't need user permission upon returning to the original origin. |
+ NavigateAndCommit(GURL("https://www.google.com/foo/bar")); |
+ EXPECT_FALSE(RequiresUserConsent(extension)); |
+ |
+ // Reloading the extension should clear active permissions, but not persisted |
+ // permissions. |
+ extension = ReloadExtension(extension->id()); |
+ Reload(); |
+ EXPECT_FALSE(RequiresUserConsent(extension)); |
+ |
+ // Different host... |
+ NavigateAndCommit(GURL("https://www.foo.com/bar")); |
+ EXPECT_TRUE(RequiresUserConsent(extension)); |
+ // Different scheme... |
+ NavigateAndCommit(GURL("http://www.google.com/foo/bar")); |
+ EXPECT_TRUE(RequiresUserConsent(extension)); |
+ // Different subdomain... |
+ NavigateAndCommit(GURL("https://en.google.com/foo/bar")); |
+ EXPECT_TRUE(RequiresUserConsent(extension)); |
+ // Only the "always run" origin should be allowed to run without user consent. |
+ NavigateAndCommit(GURL("https://www.google.com/foo/bar")); |
+ EXPECT_FALSE(RequiresUserConsent(extension)); |
+} |
+ |
} // namespace extensions |