OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/active_script_controller.h" | 8 #include "chrome/browser/extensions/active_script_controller.h" |
9 #include "chrome/browser/extensions/active_tab_permission_granter.h" | 9 #include "chrome/browser/extensions/active_tab_permission_granter.h" |
10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 // ActiveScriptController correctly interfaces in the system) is done in the | 38 // ActiveScriptController correctly interfaces in the system) is done in the |
39 // ActiveScriptControllerBrowserTests. | 39 // ActiveScriptControllerBrowserTests. |
40 class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness { | 40 class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness { |
41 protected: | 41 protected: |
42 ActiveScriptControllerUnitTest(); | 42 ActiveScriptControllerUnitTest(); |
43 virtual ~ActiveScriptControllerUnitTest(); | 43 virtual ~ActiveScriptControllerUnitTest(); |
44 | 44 |
45 // Creates an extension with all hosts permission and adds it to the registry. | 45 // Creates an extension with all hosts permission and adds it to the registry. |
46 const Extension* AddExtension(); | 46 const Extension* AddExtension(); |
47 | 47 |
| 48 // Reloads |extension_| by removing it from the registry and recreating it. |
| 49 const Extension* ReloadExtension(); |
| 50 |
48 // Returns true if the |extension| requires user consent before injecting | 51 // Returns true if the |extension| requires user consent before injecting |
49 // a script. | 52 // a script. |
50 bool RequiresUserConsent(const Extension* extension) const; | 53 bool RequiresUserConsent(const Extension* extension) const; |
51 | 54 |
52 // Request an injection for the given |extension|. | 55 // Request an injection for the given |extension|. |
53 void RequestInjection(const Extension* extension); | 56 void RequestInjection(const Extension* extension); |
54 | 57 |
55 // Returns the number of times a given extension has had a script execute. | 58 // Returns the number of times a given extension has had a script execute. |
56 size_t GetExecutionCountForExtension(const std::string& extension_id) const; | 59 size_t GetExecutionCountForExtension(const std::string& extension_id) const; |
57 | 60 |
(...skipping 13 matching lines...) Expand all Loading... |
71 | 74 |
72 // Since ActiveScriptController's behavior is behind a flag, override the | 75 // Since ActiveScriptController's behavior is behind a flag, override the |
73 // feature switch. | 76 // feature switch. |
74 FeatureSwitch::ScopedOverride feature_override_; | 77 FeatureSwitch::ScopedOverride feature_override_; |
75 | 78 |
76 // The associated ActiveScriptController. | 79 // The associated ActiveScriptController. |
77 ActiveScriptController* active_script_controller_; | 80 ActiveScriptController* active_script_controller_; |
78 | 81 |
79 // The map of observed executions, keyed by extension id. | 82 // The map of observed executions, keyed by extension id. |
80 std::map<std::string, int> extension_executions_; | 83 std::map<std::string, int> extension_executions_; |
| 84 |
| 85 scoped_refptr<const Extension> extension_; |
81 }; | 86 }; |
82 | 87 |
83 ActiveScriptControllerUnitTest::ActiveScriptControllerUnitTest() | 88 ActiveScriptControllerUnitTest::ActiveScriptControllerUnitTest() |
84 : feature_override_(FeatureSwitch::scripts_require_action(), | 89 : feature_override_(FeatureSwitch::scripts_require_action(), |
85 FeatureSwitch::OVERRIDE_ENABLED), | 90 FeatureSwitch::OVERRIDE_ENABLED), |
86 active_script_controller_(NULL) { | 91 active_script_controller_(NULL) { |
87 } | 92 } |
88 | 93 |
89 ActiveScriptControllerUnitTest::~ActiveScriptControllerUnitTest() { | 94 ActiveScriptControllerUnitTest::~ActiveScriptControllerUnitTest() { |
90 } | 95 } |
91 | 96 |
92 const Extension* ActiveScriptControllerUnitTest::AddExtension() { | 97 const Extension* ActiveScriptControllerUnitTest::AddExtension() { |
93 const std::string kId = id_util::GenerateId("all_hosts_extension"); | 98 const std::string kId = id_util::GenerateId("all_hosts_extension"); |
94 scoped_refptr<const Extension> extension = | 99 extension_ = ExtensionBuilder() |
95 ExtensionBuilder() | 100 .SetManifest( |
96 .SetManifest( | 101 DictionaryBuilder() |
97 DictionaryBuilder() | 102 .Set("name", "all_hosts_extension") |
98 .Set("name", "all_hosts_extension") | 103 .Set("description", "an extension") |
99 .Set("description", "an extension") | 104 .Set("manifest_version", 2) |
100 .Set("manifest_version", 2) | 105 .Set("version", "1.0.0") |
101 .Set("version", "1.0.0") | 106 .Set("permissions", |
102 .Set("permissions", | 107 ListBuilder().Append(kAllHostsPermission))) |
103 ListBuilder().Append(kAllHostsPermission))) | 108 .SetLocation(Manifest::INTERNAL) |
104 .SetLocation(Manifest::INTERNAL) | 109 .SetID(kId) |
105 .SetID(kId) | 110 .Build(); |
106 .Build(); | |
107 | 111 |
108 ExtensionRegistry::Get(profile())->AddEnabled(extension); | 112 ExtensionRegistry::Get(profile())->AddEnabled(extension_); |
109 PermissionsUpdater(profile()).InitializePermissions(extension); | 113 PermissionsUpdater(profile()).InitializePermissions(extension_); |
110 return extension; | 114 return extension_; |
| 115 } |
| 116 |
| 117 const Extension* ActiveScriptControllerUnitTest::ReloadExtension() { |
| 118 ExtensionRegistry::Get(profile())->RemoveEnabled(extension_->id()); |
| 119 return AddExtension(); |
111 } | 120 } |
112 | 121 |
113 bool ActiveScriptControllerUnitTest::RequiresUserConsent( | 122 bool ActiveScriptControllerUnitTest::RequiresUserConsent( |
114 const Extension* extension) const { | 123 const Extension* extension) const { |
115 PermissionsData::AccessType access_type = | 124 PermissionsData::AccessType access_type = |
116 controller()->RequiresUserConsentForScriptInjectionForTesting( | 125 controller()->RequiresUserConsentForScriptInjectionForTesting( |
117 extension, UserScript::PROGRAMMATIC_SCRIPT); | 126 extension, UserScript::PROGRAMMATIC_SCRIPT); |
118 // We should never downright refuse access in these tests. | 127 // We should never downright refuse access in these tests. |
119 DCHECK_NE(PermissionsData::ACCESS_DENIED, access_type); | 128 DCHECK_NE(PermissionsData::ACCESS_DENIED, access_type); |
120 return access_type == PermissionsData::ACCESS_WITHHELD; | 129 return access_type == PermissionsData::ACCESS_WITHHELD; |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 | 324 |
316 // Turning off the preference should have instant effect. | 325 // Turning off the preference should have instant effect. |
317 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), false); | 326 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), false); |
318 EXPECT_TRUE(RequiresUserConsent(extension)); | 327 EXPECT_TRUE(RequiresUserConsent(extension)); |
319 | 328 |
320 // And should also persist across navigations and websites. | 329 // And should also persist across navigations and websites. |
321 NavigateAndCommit(GURL("http://www.bar.com")); | 330 NavigateAndCommit(GURL("http://www.bar.com")); |
322 EXPECT_TRUE(RequiresUserConsent(extension)); | 331 EXPECT_TRUE(RequiresUserConsent(extension)); |
323 } | 332 } |
324 | 333 |
| 334 TEST_F(ActiveScriptControllerUnitTest, TestAlwaysRun) { |
| 335 const Extension* extension = AddExtension(); |
| 336 ASSERT_TRUE(extension); |
| 337 |
| 338 NavigateAndCommit(GURL("https://www.google.com/?gws_rd=ssl")); |
| 339 |
| 340 // Ensure that there aren't any executions pending. |
| 341 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 342 ASSERT_FALSE(controller()->GetActionForExtension(extension)); |
| 343 |
| 344 // Since the extension requests all_hosts, we should require user consent. |
| 345 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 346 |
| 347 // Request an injection. There should be an action visible, but no executions. |
| 348 RequestInjection(extension); |
| 349 EXPECT_TRUE(controller()->GetActionForExtension(extension)); |
| 350 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 351 |
| 352 // Allow the extension to always run on this origin. |
| 353 controller()->AlwaysRunOnVisibleOrigin(extension); |
| 354 |
| 355 // The extension should execute, and the action should go away. |
| 356 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
| 357 EXPECT_FALSE(controller()->GetActionForExtension(extension)); |
| 358 |
| 359 // Since we already executed on the given page, we shouldn't need permission |
| 360 // for a second time. |
| 361 EXPECT_FALSE(RequiresUserConsent(extension)); |
| 362 |
| 363 // Navigating to another site that hasn't been granted a persisted permission |
| 364 // should necessitate user consent. |
| 365 NavigateAndCommit(GURL("https://www.foo.com/bar")); |
| 366 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 367 |
| 368 // We shouldn't need user permission upon returning to the original origin. |
| 369 NavigateAndCommit(GURL("https://www.google.com/foo/bar")); |
| 370 EXPECT_FALSE(RequiresUserConsent(extension)); |
| 371 |
| 372 // Reloading the extension should not clear any granted host permissions. |
| 373 extension = ReloadExtension(); |
| 374 Reload(); |
| 375 EXPECT_FALSE(RequiresUserConsent(extension)); |
| 376 |
| 377 // Different host... |
| 378 NavigateAndCommit(GURL("https://www.foo.com/bar")); |
| 379 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 380 // Different scheme... |
| 381 NavigateAndCommit(GURL("http://www.google.com/foo/bar")); |
| 382 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 383 // Different subdomain... |
| 384 NavigateAndCommit(GURL("https://en.google.com/foo/bar")); |
| 385 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 386 // Only the "always run" origin should be allowed to run without user consent. |
| 387 NavigateAndCommit(GURL("https://www.google.com/foo/bar")); |
| 388 EXPECT_FALSE(RequiresUserConsent(extension)); |
| 389 } |
| 390 |
325 } // namespace extensions | 391 } // namespace extensions |
OLD | NEW |