Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: chrome/browser/extensions/active_script_controller_browsertest.cc

Issue 288053002: Block content scripts from executing until user grants permission (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
19 #include "net/test/embedded_test_server/embedded_test_server.h" 20 #include "net/test/embedded_test_server/embedded_test_server.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
22 namespace extensions { 23 namespace extensions {
23 24
24 namespace { 25 namespace {
25 26
26 const char kAllHostsScheme[] = "*://*/*"; 27 const char kAllHostsScheme[] = "*://*/*";
27 const char kExplicitHostsScheme[] = "http://127.0.0.1/*"; 28 const char kExplicitHostsScheme[] = "http://127.0.0.1/*";
28 const char kBackgroundScript[] = 29 const char kBackgroundScript[] =
(...skipping 25 matching lines...) Expand all
54 55
55 enum RequiresConsent { 56 enum RequiresConsent {
56 REQUIRES_CONSENT, 57 REQUIRES_CONSENT,
57 DOES_NOT_REQUIRE_CONSENT 58 DOES_NOT_REQUIRE_CONSENT
58 }; 59 };
59 60
60 } // namespace 61 } // namespace
61 62
62 class ActiveScriptControllerBrowserTest : public ExtensionBrowserTest { 63 class ActiveScriptControllerBrowserTest : public ExtensionBrowserTest {
63 public: 64 public:
64 ActiveScriptControllerBrowserTest() 65 ActiveScriptControllerBrowserTest() {}
65 : feature_override_(FeatureSwitch::scripts_require_action(),
66 FeatureSwitch::OVERRIDE_ENABLED) {}
67 66
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_;
79 ScopedVector<TestExtensionDir> test_extension_dirs_; 78 ScopedVector<TestExtensionDir> test_extension_dirs_;
80 std::vector<const Extension*> extensions_; 79 std::vector<const Extension*> extensions_;
81 }; 80 };
82 81
82 void ActiveScriptControllerBrowserTest::SetUpCommandLine(
83 base::CommandLine* command_line) {
84 // We append the actual switch to the commandline because it needs to be
85 // passed over to the renderer, which a FeatureSwitch::ScopedOverride will
86 // not do.
87 command_line->AppendSwitch(switches::kEnableScriptsRequireAction);
88 }
89
83 void ActiveScriptControllerBrowserTest::CleanUpOnMainThread() { 90 void ActiveScriptControllerBrowserTest::CleanUpOnMainThread() {
84 test_extension_dirs_.clear(); 91 test_extension_dirs_.clear();
85 } 92 }
86 93
87 const Extension* ActiveScriptControllerBrowserTest::GetOrCreateExtension( 94 const Extension* ActiveScriptControllerBrowserTest::GetOrCreateExtension(
88 HostType host_type, InjectionType injection_type) { 95 HostType host_type, InjectionType injection_type) {
89 std::string name = 96 std::string name =
90 base::StringPrintf( 97 base::StringPrintf(
91 "%s %s", 98 "%s %s",
92 injection_type == CONTENT_SCRIPT ? 99 injection_type == CONTENT_SCRIPT ?
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 266
260 // If the extension has permission, we should be able to simply wait for it 267 // If the extension has permission, we should be able to simply wait for it
261 // to execute. 268 // to execute.
262 if (requires_consent_ == DOES_NOT_REQUIRE_CONSENT) { 269 if (requires_consent_ == DOES_NOT_REQUIRE_CONSENT) {
263 inject_success_listener_->WaitUntilSatisfied(); 270 inject_success_listener_->WaitUntilSatisfied();
264 return testing::AssertionSuccess(); 271 return testing::AssertionSuccess();
265 } 272 }
266 273
267 // Otherwise, we don't have permission, and have to grant it. Ensure the 274 // Otherwise, we don't have permission, and have to grant it. Ensure the
268 // script has *not* already executed. 275 // script has *not* already executed.
269 // Currently, it's okay for content scripts to execute, because we don't 276 if (inject_success_listener_->was_satisfied()) {
270 // block them.
271 // TODO(rdevlin.cronin): Fix this.
272 if (inject_success_listener_->was_satisfied() && type_ != CONTENT_SCRIPT) {
273 return testing::AssertionFailure() << 277 return testing::AssertionFailure() <<
274 name_ << "'s script ran without permission."; 278 name_ << "'s script ran without permission.";
275 } 279 }
276 280
277 // If we reach this point, we should always have an action. 281 // If we reach this point, we should always have an action.
278 DCHECK(action); 282 DCHECK(action);
279 283
280 // Grant permission by clicking on the extension action. 284 // Grant permission by clicking on the extension action.
281 location_bar_controller->OnClicked(action); 285 location_bar_controller->OnClicked(action);
282 286
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 // inject the script. 370 // inject the script.
367 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 371 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
368 ui_test_utils::NavigateToURL( 372 ui_test_utils::NavigateToURL(
369 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); 373 browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
370 374
371 for (size_t i = 0u; i < arraysize(testers); ++i) 375 for (size_t i = 0u; i < arraysize(testers); ++i)
372 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; 376 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i];
373 } 377 }
374 378
375 } // namespace extensions 379 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698