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 "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "chrome/browser/extensions/active_script_controller.h" | 8 #include "chrome/browser/extensions/active_script_controller.h" |
9 #include "chrome/browser/extensions/extension_action.h" | 9 #include "chrome/browser/extensions/extension_action.h" |
10 #include "chrome/browser/extensions/extension_browsertest.h" | 10 #include "chrome/browser/extensions/extension_browsertest.h" |
11 #include "chrome/browser/extensions/extension_test_message_listener.h" | 11 #include "chrome/browser/extensions/extension_test_message_listener.h" |
12 #include "chrome/browser/extensions/location_bar_controller.h" | 12 #include "chrome/browser/extensions/location_bar_controller.h" |
13 #include "chrome/browser/extensions/tab_helper.h" | 13 #include "chrome/browser/extensions/tab_helper.h" |
14 #include "chrome/browser/extensions/test_extension_dir.h" | 14 #include "chrome/browser/extensions/test_extension_dir.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
17 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
18 #include "extensions/common/feature_switch.h" | 18 #include "extensions/common/feature_switch.h" |
19 #include "extensions/common/switches.h" | |
20 #include "net/test/embedded_test_server/embedded_test_server.h" | 19 #include "net/test/embedded_test_server/embedded_test_server.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
22 | 21 |
23 namespace extensions { | 22 namespace extensions { |
24 | 23 |
25 namespace { | 24 namespace { |
26 | 25 |
27 const char kAllHostsScheme[] = "*://*/*"; | 26 const char kAllHostsScheme[] = "*://*/*"; |
28 const char kExplicitHostsScheme[] = "http://127.0.0.1/*"; | 27 const char kExplicitHostsScheme[] = "http://127.0.0.1/*"; |
29 const char kBackgroundScript[] = | 28 const char kBackgroundScript[] = |
(...skipping 25 matching lines...) Expand all Loading... |
55 | 54 |
56 enum RequiresConsent { | 55 enum RequiresConsent { |
57 REQUIRES_CONSENT, | 56 REQUIRES_CONSENT, |
58 DOES_NOT_REQUIRE_CONSENT | 57 DOES_NOT_REQUIRE_CONSENT |
59 }; | 58 }; |
60 | 59 |
61 } // namespace | 60 } // namespace |
62 | 61 |
63 class ActiveScriptControllerBrowserTest : public ExtensionBrowserTest { | 62 class ActiveScriptControllerBrowserTest : public ExtensionBrowserTest { |
64 public: | 63 public: |
65 ActiveScriptControllerBrowserTest() {} | 64 ActiveScriptControllerBrowserTest() |
| 65 : feature_override_(FeatureSwitch::scripts_require_action(), |
| 66 FeatureSwitch::OVERRIDE_ENABLED) {} |
66 | 67 |
67 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE; | |
68 virtual void CleanUpOnMainThread() OVERRIDE; | 68 virtual void CleanUpOnMainThread() OVERRIDE; |
69 | 69 |
70 // Returns an extension with the given |host_type| and |injection_type|. If | 70 // Returns an extension with the given |host_type| and |injection_type|. If |
71 // one already exists, the existing extension will be returned. Othewrwise, | 71 // one already exists, the existing extension will be returned. Othewrwise, |
72 // one will be created. | 72 // one will be created. |
73 // This could potentially return NULL if LoadExtension() fails. | 73 // This could potentially return NULL if LoadExtension() fails. |
74 const Extension* GetOrCreateExtension(HostType host_type, | 74 const Extension* GetOrCreateExtension(HostType host_type, |
75 InjectionType injection_type); | 75 InjectionType injection_type); |
76 | 76 |
77 private: | 77 private: |
| 78 FeatureSwitch::ScopedOverride feature_override_; |
78 ScopedVector<TestExtensionDir> test_extension_dirs_; | 79 ScopedVector<TestExtensionDir> test_extension_dirs_; |
79 std::vector<const Extension*> extensions_; | 80 std::vector<const Extension*> extensions_; |
80 }; | 81 }; |
81 | 82 |
82 void ActiveScriptControllerBrowserTest::SetUpCommandLine( | |
83 base::CommandLine* command_line) { | |
84 ExtensionBrowserTest::SetUpCommandLine(command_line); | |
85 // We append the actual switch to the commandline because it needs to be | |
86 // passed over to the renderer, which a FeatureSwitch::ScopedOverride will | |
87 // not do. | |
88 command_line->AppendSwitch(switches::kEnableScriptsRequireAction); | |
89 } | |
90 | |
91 void ActiveScriptControllerBrowserTest::CleanUpOnMainThread() { | 83 void ActiveScriptControllerBrowserTest::CleanUpOnMainThread() { |
92 test_extension_dirs_.clear(); | 84 test_extension_dirs_.clear(); |
93 } | 85 } |
94 | 86 |
95 const Extension* ActiveScriptControllerBrowserTest::GetOrCreateExtension( | 87 const Extension* ActiveScriptControllerBrowserTest::GetOrCreateExtension( |
96 HostType host_type, InjectionType injection_type) { | 88 HostType host_type, InjectionType injection_type) { |
97 std::string name = | 89 std::string name = |
98 base::StringPrintf( | 90 base::StringPrintf( |
99 "%s %s", | 91 "%s %s", |
100 injection_type == CONTENT_SCRIPT ? | 92 injection_type == CONTENT_SCRIPT ? |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 259 |
268 // If the extension has permission, we should be able to simply wait for it | 260 // If the extension has permission, we should be able to simply wait for it |
269 // to execute. | 261 // to execute. |
270 if (requires_consent_ == DOES_NOT_REQUIRE_CONSENT) { | 262 if (requires_consent_ == DOES_NOT_REQUIRE_CONSENT) { |
271 inject_success_listener_->WaitUntilSatisfied(); | 263 inject_success_listener_->WaitUntilSatisfied(); |
272 return testing::AssertionSuccess(); | 264 return testing::AssertionSuccess(); |
273 } | 265 } |
274 | 266 |
275 // Otherwise, we don't have permission, and have to grant it. Ensure the | 267 // Otherwise, we don't have permission, and have to grant it. Ensure the |
276 // script has *not* already executed. | 268 // script has *not* already executed. |
277 if (inject_success_listener_->was_satisfied()) { | 269 // Currently, it's okay for content scripts to execute, because we don't |
| 270 // block them. |
| 271 // TODO(rdevlin.cronin): Fix this. |
| 272 if (inject_success_listener_->was_satisfied() && type_ != CONTENT_SCRIPT) { |
278 return testing::AssertionFailure() << | 273 return testing::AssertionFailure() << |
279 name_ << "'s script ran without permission."; | 274 name_ << "'s script ran without permission."; |
280 } | 275 } |
281 | 276 |
282 // If we reach this point, we should always have an action. | 277 // If we reach this point, we should always have an action. |
283 DCHECK(action); | 278 DCHECK(action); |
284 | 279 |
285 // Grant permission by clicking on the extension action. | 280 // Grant permission by clicking on the extension action. |
286 location_bar_controller->OnClicked(action); | 281 location_bar_controller->OnClicked(action); |
287 | 282 |
(...skipping 26 matching lines...) Expand all Loading... |
314 return location_bar_controller ? | 309 return location_bar_controller ? |
315 location_bar_controller->active_script_controller() : NULL; | 310 location_bar_controller->active_script_controller() : NULL; |
316 } | 311 } |
317 | 312 |
318 ExtensionAction* ActiveScriptTester::GetAction() { | 313 ExtensionAction* ActiveScriptTester::GetAction() { |
319 ActiveScriptController* controller = GetActiveScriptController(); | 314 ActiveScriptController* controller = GetActiveScriptController(); |
320 return controller ? controller->GetActionForExtension(extension_) : NULL; | 315 return controller ? controller->GetActionForExtension(extension_) : NULL; |
321 } | 316 } |
322 | 317 |
323 IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest, | 318 IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest, |
324 ActiveScriptsAreDisplayedAndDelayExecution) { | 319 ActiveScriptsAreDisplayed) { |
325 base::FilePath active_script_path = | 320 base::FilePath active_script_path = |
326 test_data_dir_.AppendASCII("active_script"); | 321 test_data_dir_.AppendASCII("active_script"); |
327 | 322 |
328 const char* kExtensionNames[] = { | 323 const char* kExtensionNames[] = { |
329 "inject_scripts_all_hosts", | 324 "inject_scripts_all_hosts", |
330 "inject_scripts_explicit_hosts", | 325 "inject_scripts_explicit_hosts", |
331 "content_scripts_all_hosts", | 326 "content_scripts_all_hosts", |
332 "content_scripts_explicit_hosts" | 327 "content_scripts_explicit_hosts" |
333 }; | 328 }; |
334 | 329 |
(...skipping 25 matching lines...) Expand all Loading... |
360 CONTENT_SCRIPT), | 355 CONTENT_SCRIPT), |
361 ActiveScriptTester( | 356 ActiveScriptTester( |
362 kExtensionNames[3], | 357 kExtensionNames[3], |
363 GetOrCreateExtension(EXPLICIT_HOSTS, CONTENT_SCRIPT), | 358 GetOrCreateExtension(EXPLICIT_HOSTS, CONTENT_SCRIPT), |
364 browser(), | 359 browser(), |
365 DOES_NOT_REQUIRE_CONSENT, | 360 DOES_NOT_REQUIRE_CONSENT, |
366 CONTENT_SCRIPT), | 361 CONTENT_SCRIPT), |
367 }; | 362 }; |
368 | 363 |
369 // Navigate to an URL (which matches the explicit host specified in the | 364 // Navigate to an URL (which matches the explicit host specified in the |
370 // extension content_scripts_explicit_hosts). All four extensions should | 365 // extension content_scripts_explicit_hosts). All three extensions should |
371 // inject the script. | 366 // inject the script. |
372 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 367 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
373 ui_test_utils::NavigateToURL( | 368 ui_test_utils::NavigateToURL( |
374 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); | 369 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); |
375 | 370 |
376 for (size_t i = 0u; i < arraysize(testers); ++i) | 371 for (size_t i = 0u; i < arraysize(testers); ++i) |
377 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; | 372 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; |
378 } | 373 } |
379 | 374 |
380 // A version of the test with the flag off, in order to test that everything | |
381 // still works as expected. | |
382 class FlagOffActiveScriptControllerBrowserTest | |
383 : public ActiveScriptControllerBrowserTest { | |
384 private: | |
385 // Simply don't append the flag. | |
386 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { | |
387 ExtensionBrowserTest::SetUpCommandLine(command_line); | |
388 } | |
389 }; | |
390 | |
391 IN_PROC_BROWSER_TEST_F(FlagOffActiveScriptControllerBrowserTest, | |
392 ScriptsExecuteWhenFlagAbsent) { | |
393 const char* kExtensionNames[] = { | |
394 "content_scripts_all_hosts", | |
395 "inject_scripts_all_hosts", | |
396 }; | |
397 ActiveScriptTester testers[] = { | |
398 ActiveScriptTester( | |
399 kExtensionNames[0], | |
400 GetOrCreateExtension(ALL_HOSTS, CONTENT_SCRIPT), | |
401 browser(), | |
402 DOES_NOT_REQUIRE_CONSENT, | |
403 CONTENT_SCRIPT), | |
404 ActiveScriptTester( | |
405 kExtensionNames[1], | |
406 GetOrCreateExtension(ALL_HOSTS, EXECUTE_SCRIPT), | |
407 browser(), | |
408 DOES_NOT_REQUIRE_CONSENT, | |
409 EXECUTE_SCRIPT), | |
410 }; | |
411 | |
412 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
413 ui_test_utils::NavigateToURL( | |
414 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); | |
415 | |
416 for (size_t i = 0u; i < arraysize(testers); ++i) | |
417 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; | |
418 } | |
419 | |
420 } // namespace extensions | 375 } // namespace extensions |
OLD | NEW |