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 "chrome/browser/extensions/extension_action_runner.h" | 5 #include "chrome/browser/extensions/extension_action_runner.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 public: | 177 public: |
178 ActiveScriptTester(const std::string& name, | 178 ActiveScriptTester(const std::string& name, |
179 const Extension* extension, | 179 const Extension* extension, |
180 Browser* browser, | 180 Browser* browser, |
181 RequiresConsent requires_consent, | 181 RequiresConsent requires_consent, |
182 InjectionType type); | 182 InjectionType type); |
183 ~ActiveScriptTester(); | 183 ~ActiveScriptTester(); |
184 | 184 |
185 testing::AssertionResult Verify(); | 185 testing::AssertionResult Verify(); |
186 | 186 |
| 187 std::string name() const; |
| 188 |
187 private: | 189 private: |
188 // Returns the ExtensionActionRunner, or null if one does not exist. | 190 // Returns the ExtensionActionRunner, or null if one does not exist. |
189 ExtensionActionRunner* GetExtensionActionRunner(); | 191 ExtensionActionRunner* GetExtensionActionRunner(); |
190 | 192 |
191 // Returns true if the extension has a pending request with the | 193 // Returns true if the extension has a pending request with the |
192 // ExtensionActionRunner. | 194 // ExtensionActionRunner. |
193 bool WantsToRun(); | 195 bool WantsToRun(); |
194 | 196 |
195 // The name of the extension, and also the message it sends. | 197 // The name of the extension, and also the message it sends. |
196 std::string name_; | 198 std::string name_; |
197 | 199 |
198 // The extension associated with this tester. | 200 // The extension associated with this tester. |
199 const Extension* extension_; | 201 const Extension* extension_; |
200 | 202 |
201 // The browser the tester is running in. | 203 // The browser the tester is running in. |
202 Browser* browser_; | 204 Browser* browser_; |
203 | 205 |
204 // Whether or not the extension has permission to run the script without | 206 // Whether or not the extension has permission to run the script without |
205 // asking the user. | 207 // asking the user. |
206 RequiresConsent requires_consent_; | 208 RequiresConsent requires_consent_; |
207 | 209 |
208 // The type of injection this tester uses. | |
209 InjectionType type_; | |
210 | |
211 // All of these extensions should inject a script (either through content | 210 // All of these extensions should inject a script (either through content |
212 // scripts or through chrome.tabs.executeScript()) that sends a message with | 211 // scripts or through chrome.tabs.executeScript()) that sends a message with |
213 // the |kInjectSucceeded| message. | 212 // the |kInjectSucceeded| message. |
214 linked_ptr<ExtensionTestMessageListener> inject_success_listener_; | 213 std::unique_ptr<ExtensionTestMessageListener> inject_success_listener_; |
215 }; | 214 }; |
216 | 215 |
217 ActiveScriptTester::ActiveScriptTester(const std::string& name, | 216 ActiveScriptTester::ActiveScriptTester(const std::string& name, |
218 const Extension* extension, | 217 const Extension* extension, |
219 Browser* browser, | 218 Browser* browser, |
220 RequiresConsent requires_consent, | 219 RequiresConsent requires_consent, |
221 InjectionType type) | 220 InjectionType type) |
222 : name_(name), | 221 : name_(name), |
223 extension_(extension), | 222 extension_(extension), |
224 browser_(browser), | 223 browser_(browser), |
225 requires_consent_(requires_consent), | 224 requires_consent_(requires_consent), |
226 type_(type), | |
227 inject_success_listener_( | 225 inject_success_listener_( |
228 new ExtensionTestMessageListener(kInjectSucceeded, | 226 new ExtensionTestMessageListener(kInjectSucceeded, |
229 false /* won't reply */)) { | 227 false /* won't reply */)) { |
230 inject_success_listener_->set_extension_id(extension->id()); | 228 inject_success_listener_->set_extension_id(extension->id()); |
231 } | 229 } |
232 | 230 |
233 ActiveScriptTester::~ActiveScriptTester() {} | 231 ActiveScriptTester::~ActiveScriptTester() {} |
234 | 232 |
| 233 std::string ActiveScriptTester::name() const { |
| 234 return name_; |
| 235 } |
| 236 |
235 testing::AssertionResult ActiveScriptTester::Verify() { | 237 testing::AssertionResult ActiveScriptTester::Verify() { |
236 if (!extension_) | 238 if (!extension_) |
237 return testing::AssertionFailure() << "Could not load extension: " << name_; | 239 return testing::AssertionFailure() << "Could not load extension: " << name_; |
238 | 240 |
239 content::WebContents* web_contents = | 241 content::WebContents* web_contents = |
240 browser_ ? browser_->tab_strip_model()->GetActiveWebContents() : NULL; | 242 browser_ ? browser_->tab_strip_model()->GetActiveWebContents() : NULL; |
241 if (!web_contents) | 243 if (!web_contents) |
242 return testing::AssertionFailure() << "No web contents."; | 244 return testing::AssertionFailure() << "No web contents."; |
243 | 245 |
244 // Give the extension plenty of time to inject. | 246 // Give the extension plenty of time to inject. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 browser_ ? browser_->tab_strip_model()->GetActiveWebContents() : nullptr); | 303 browser_ ? browser_->tab_strip_model()->GetActiveWebContents() : nullptr); |
302 } | 304 } |
303 | 305 |
304 bool ActiveScriptTester::WantsToRun() { | 306 bool ActiveScriptTester::WantsToRun() { |
305 ExtensionActionRunner* runner = GetExtensionActionRunner(); | 307 ExtensionActionRunner* runner = GetExtensionActionRunner(); |
306 return runner ? runner->WantsToRun(extension_) : false; | 308 return runner ? runner->WantsToRun(extension_) : false; |
307 } | 309 } |
308 | 310 |
309 IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerBrowserTest, | 311 IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerBrowserTest, |
310 ActiveScriptsAreDisplayedAndDelayExecution) { | 312 ActiveScriptsAreDisplayedAndDelayExecution) { |
311 base::FilePath active_script_path = | |
312 test_data_dir_.AppendASCII("active_script"); | |
313 | |
314 const char* const kExtensionNames[] = { | |
315 "inject_scripts_all_hosts", "inject_scripts_explicit_hosts", | |
316 "content_scripts_all_hosts", "content_scripts_explicit_hosts"}; | |
317 | |
318 // First, we load up three extensions: | 313 // First, we load up three extensions: |
319 // - An extension that injects scripts into all hosts, | 314 // - An extension that injects scripts into all hosts, |
320 // - An extension that injects scripts into explicit hosts, | 315 // - An extension that injects scripts into explicit hosts, |
321 // - An extension with a content script that runs on all hosts, | 316 // - An extension with a content script that runs on all hosts, |
322 // - An extension with a content script that runs on explicit hosts. | 317 // - An extension with a content script that runs on explicit hosts. |
323 // The extensions that operate on explicit hosts have permission; the ones | 318 // The extensions that operate on explicit hosts have permission; the ones |
324 // that request all hosts require user consent. | 319 // that request all hosts require user consent. |
325 ActiveScriptTester testers[] = { | 320 std::vector<std::unique_ptr<ActiveScriptTester>> testers; |
326 ActiveScriptTester(kExtensionNames[0], | 321 testers.push_back(base::MakeUnique<ActiveScriptTester>( |
327 CreateExtension(ALL_HOSTS, EXECUTE_SCRIPT), browser(), | 322 "inject_scripts_all_hosts", CreateExtension(ALL_HOSTS, EXECUTE_SCRIPT), |
328 REQUIRES_CONSENT, EXECUTE_SCRIPT), | 323 browser(), REQUIRES_CONSENT, EXECUTE_SCRIPT)); |
329 ActiveScriptTester(kExtensionNames[1], | 324 testers.push_back(base::MakeUnique<ActiveScriptTester>( |
330 CreateExtension(EXPLICIT_HOSTS, EXECUTE_SCRIPT), | 325 "inject_scripts_explicit_hosts", |
331 browser(), DOES_NOT_REQUIRE_CONSENT, EXECUTE_SCRIPT), | 326 CreateExtension(EXPLICIT_HOSTS, EXECUTE_SCRIPT), browser(), |
332 ActiveScriptTester(kExtensionNames[2], | 327 DOES_NOT_REQUIRE_CONSENT, EXECUTE_SCRIPT)); |
333 CreateExtension(ALL_HOSTS, CONTENT_SCRIPT), browser(), | 328 testers.push_back(base::MakeUnique<ActiveScriptTester>( |
334 REQUIRES_CONSENT, CONTENT_SCRIPT), | 329 "content_scripts_all_hosts", CreateExtension(ALL_HOSTS, CONTENT_SCRIPT), |
335 ActiveScriptTester(kExtensionNames[3], | 330 browser(), REQUIRES_CONSENT, CONTENT_SCRIPT)); |
336 CreateExtension(EXPLICIT_HOSTS, CONTENT_SCRIPT), | 331 testers.push_back(base::MakeUnique<ActiveScriptTester>( |
337 browser(), DOES_NOT_REQUIRE_CONSENT, CONTENT_SCRIPT), | 332 "content_scripts_explicit_hosts", |
338 }; | 333 CreateExtension(EXPLICIT_HOSTS, CONTENT_SCRIPT), browser(), |
| 334 DOES_NOT_REQUIRE_CONSENT, CONTENT_SCRIPT)); |
339 | 335 |
340 // Navigate to an URL (which matches the explicit host specified in the | 336 // Navigate to an URL (which matches the explicit host specified in the |
341 // extension content_scripts_explicit_hosts). All four extensions should | 337 // extension content_scripts_explicit_hosts). All four extensions should |
342 // inject the script. | 338 // inject the script. |
343 ASSERT_TRUE(embedded_test_server()->Start()); | 339 ASSERT_TRUE(embedded_test_server()->Start()); |
344 ui_test_utils::NavigateToURL( | 340 ui_test_utils::NavigateToURL( |
345 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); | 341 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); |
346 | 342 |
347 for (size_t i = 0u; i < arraysize(testers); ++i) | 343 for (const auto& tester : testers) |
348 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; | 344 EXPECT_TRUE(tester->Verify()) << tester->name(); |
349 } | 345 } |
350 | 346 |
351 // Test that removing an extension with pending injections a) removes the | 347 // Test that removing an extension with pending injections a) removes the |
352 // pending injections for that extension, and b) does not affect pending | 348 // pending injections for that extension, and b) does not affect pending |
353 // injections for other extensions. | 349 // injections for other extensions. |
354 IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerBrowserTest, | 350 IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerBrowserTest, |
355 RemoveExtensionWithPendingInjections) { | 351 RemoveExtensionWithPendingInjections) { |
356 // Load up two extensions, each with content scripts. | 352 // Load up two extensions, each with content scripts. |
357 const Extension* extension1 = CreateExtension(ALL_HOSTS, CONTENT_SCRIPT); | 353 const Extension* extension1 = CreateExtension(ALL_HOSTS, CONTENT_SCRIPT); |
358 ASSERT_TRUE(extension1); | 354 ASSERT_TRUE(extension1); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 : public ExtensionActionRunnerBrowserTest { | 537 : public ExtensionActionRunnerBrowserTest { |
542 private: | 538 private: |
543 // Simply don't append the flag. | 539 // Simply don't append the flag. |
544 void SetUpCommandLine(base::CommandLine* command_line) override { | 540 void SetUpCommandLine(base::CommandLine* command_line) override { |
545 ExtensionBrowserTest::SetUpCommandLine(command_line); | 541 ExtensionBrowserTest::SetUpCommandLine(command_line); |
546 } | 542 } |
547 }; | 543 }; |
548 | 544 |
549 IN_PROC_BROWSER_TEST_F(FlagOffExtensionActionRunnerBrowserTest, | 545 IN_PROC_BROWSER_TEST_F(FlagOffExtensionActionRunnerBrowserTest, |
550 ScriptsExecuteWhenFlagAbsent) { | 546 ScriptsExecuteWhenFlagAbsent) { |
551 const char* const kExtensionNames[] = { | 547 std::vector<std::unique_ptr<ActiveScriptTester>> testers; |
552 "content_scripts_all_hosts", "inject_scripts_all_hosts", | 548 testers.push_back(base::MakeUnique<ActiveScriptTester>( |
553 }; | 549 "content_scripts_all_hosts", CreateExtension(ALL_HOSTS, CONTENT_SCRIPT), |
554 ActiveScriptTester testers[] = { | 550 browser(), DOES_NOT_REQUIRE_CONSENT, CONTENT_SCRIPT)); |
555 ActiveScriptTester(kExtensionNames[0], | 551 testers.push_back(base::MakeUnique<ActiveScriptTester>( |
556 CreateExtension(ALL_HOSTS, CONTENT_SCRIPT), browser(), | 552 "inject_scripts_all_hosts", CreateExtension(ALL_HOSTS, EXECUTE_SCRIPT), |
557 DOES_NOT_REQUIRE_CONSENT, CONTENT_SCRIPT), | 553 browser(), DOES_NOT_REQUIRE_CONSENT, EXECUTE_SCRIPT)); |
558 ActiveScriptTester(kExtensionNames[1], | |
559 CreateExtension(ALL_HOSTS, EXECUTE_SCRIPT), browser(), | |
560 DOES_NOT_REQUIRE_CONSENT, EXECUTE_SCRIPT), | |
561 }; | |
562 | 554 |
563 ASSERT_TRUE(embedded_test_server()->Start()); | 555 ASSERT_TRUE(embedded_test_server()->Start()); |
564 ui_test_utils::NavigateToURL( | 556 ui_test_utils::NavigateToURL( |
565 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); | 557 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); |
566 | 558 |
567 for (size_t i = 0u; i < arraysize(testers); ++i) | 559 for (const auto& tester : testers) |
568 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; | 560 EXPECT_TRUE(tester->Verify()) << tester->name(); |
569 } | 561 } |
570 | 562 |
571 } // namespace extensions | 563 } // namespace extensions |
OLD | NEW |