| 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" |
| 6 |
| 5 #include <stddef.h> | 7 #include <stddef.h> |
| 6 | 8 |
| 9 #include <memory> |
| 7 #include <utility> | 10 #include <utility> |
| 8 #include <vector> | 11 #include <vector> |
| 9 | 12 |
| 10 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 11 #include "base/macros.h" | 14 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 14 #include "chrome/browser/extensions/extension_action.h" | 17 #include "chrome/browser/extensions/extension_action.h" |
| 15 #include "chrome/browser/extensions/extension_action_runner.h" | |
| 16 #include "chrome/browser/extensions/extension_browsertest.h" | 18 #include "chrome/browser/extensions/extension_browsertest.h" |
| 17 #include "chrome/browser/extensions/extension_util.h" | 19 #include "chrome/browser/extensions/extension_util.h" |
| 18 #include "chrome/browser/extensions/tab_helper.h" | 20 #include "chrome/browser/extensions/tab_helper.h" |
| 19 #include "chrome/browser/extensions/test_extension_dir.h" | 21 #include "chrome/browser/extensions/test_extension_dir.h" |
| 20 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 22 #include "chrome/test/base/ui_test_utils.h" | 24 #include "chrome/test/base/ui_test_utils.h" |
| 23 #include "content/public/browser/navigation_entry.h" | 25 #include "content/public/browser/navigation_entry.h" |
| 24 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/test/browser_test_utils.h" | 27 #include "content/public/test/browser_test_utils.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 void TearDownOnMainThread() override; | 93 void TearDownOnMainThread() override; |
| 92 | 94 |
| 93 // Returns an extension with the given |host_type| and |injection_type|. If | 95 // Returns an extension with the given |host_type| and |injection_type|. If |
| 94 // one already exists, the existing extension will be returned. Othewrwise, | 96 // one already exists, the existing extension will be returned. Othewrwise, |
| 95 // one will be created. | 97 // one will be created. |
| 96 // This could potentially return NULL if LoadExtension() fails. | 98 // This could potentially return NULL if LoadExtension() fails. |
| 97 const Extension* CreateExtension(HostType host_type, | 99 const Extension* CreateExtension(HostType host_type, |
| 98 InjectionType injection_type); | 100 InjectionType injection_type); |
| 99 | 101 |
| 100 private: | 102 private: |
| 101 std::vector<scoped_ptr<TestExtensionDir>> test_extension_dirs_; | 103 std::vector<std::unique_ptr<TestExtensionDir>> test_extension_dirs_; |
| 102 std::vector<const Extension*> extensions_; | 104 std::vector<const Extension*> extensions_; |
| 103 }; | 105 }; |
| 104 | 106 |
| 105 void ExtensionActionRunnerBrowserTest::SetUpCommandLine( | 107 void ExtensionActionRunnerBrowserTest::SetUpCommandLine( |
| 106 base::CommandLine* command_line) { | 108 base::CommandLine* command_line) { |
| 107 ExtensionBrowserTest::SetUpCommandLine(command_line); | 109 ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 108 // We append the actual switch to the commandline because it needs to be | 110 // We append the actual switch to the commandline because it needs to be |
| 109 // passed over to the renderer, which a FeatureSwitch::ScopedOverride will | 111 // passed over to the renderer, which a FeatureSwitch::ScopedOverride will |
| 110 // not do. | 112 // not do. |
| 111 command_line->AppendSwitch(switches::kEnableScriptsRequireAction); | 113 command_line->AppendSwitch(switches::kEnableScriptsRequireAction); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 std::string manifest = base::StringPrintf( | 150 std::string manifest = base::StringPrintf( |
| 149 "{" | 151 "{" |
| 150 " \"name\": \"%s\"," | 152 " \"name\": \"%s\"," |
| 151 " \"version\": \"1.0\"," | 153 " \"version\": \"1.0\"," |
| 152 " \"manifest_version\": 2," | 154 " \"manifest_version\": 2," |
| 153 " %s," | 155 " %s," |
| 154 " %s" | 156 " %s" |
| 155 "}", | 157 "}", |
| 156 name.c_str(), permissions.c_str(), scripts.c_str()); | 158 name.c_str(), permissions.c_str(), scripts.c_str()); |
| 157 | 159 |
| 158 scoped_ptr<TestExtensionDir> dir(new TestExtensionDir); | 160 std::unique_ptr<TestExtensionDir> dir(new TestExtensionDir); |
| 159 dir->WriteManifest(manifest); | 161 dir->WriteManifest(manifest); |
| 160 dir->WriteFile(FILE_PATH_LITERAL("script.js"), | 162 dir->WriteFile(FILE_PATH_LITERAL("script.js"), |
| 161 injection_type == CONTENT_SCRIPT ? kContentScriptSource | 163 injection_type == CONTENT_SCRIPT ? kContentScriptSource |
| 162 : kBackgroundScriptSource); | 164 : kBackgroundScriptSource); |
| 163 | 165 |
| 164 const Extension* extension = LoadExtension(dir->unpacked_path()); | 166 const Extension* extension = LoadExtension(dir->unpacked_path()); |
| 165 if (extension) { | 167 if (extension) { |
| 166 test_extension_dirs_.push_back(std::move(dir)); | 168 test_extension_dirs_.push_back(std::move(dir)); |
| 167 extensions_.push_back(extension); | 169 extensions_.push_back(extension); |
| 168 } | 170 } |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 // injected. | 465 // injected. |
| 464 ExtensionActionRunner* runner = | 466 ExtensionActionRunner* runner = |
| 465 ExtensionActionRunner::GetForWebContents(web_contents); | 467 ExtensionActionRunner::GetForWebContents(web_contents); |
| 466 ASSERT_TRUE(runner); | 468 ASSERT_TRUE(runner); |
| 467 EXPECT_TRUE(runner->WantsToRun(extension)); | 469 EXPECT_TRUE(runner->WantsToRun(extension)); |
| 468 EXPECT_EQ("undefined", GetValue(web_contents)); | 470 EXPECT_EQ("undefined", GetValue(web_contents)); |
| 469 | 471 |
| 470 // Wire up the runner to automatically accept the bubble to prompt for page | 472 // Wire up the runner to automatically accept the bubble to prompt for page |
| 471 // refresh. | 473 // refresh. |
| 472 runner->set_default_bubble_close_action_for_testing( | 474 runner->set_default_bubble_close_action_for_testing( |
| 473 make_scoped_ptr(new ToolbarActionsBarBubbleDelegate::CloseAction( | 475 base::WrapUnique(new ToolbarActionsBarBubbleDelegate::CloseAction( |
| 474 ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE))); | 476 ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE))); |
| 475 | 477 |
| 476 content::NavigationEntry* entry = | 478 content::NavigationEntry* entry = |
| 477 web_contents->GetController().GetLastCommittedEntry(); | 479 web_contents->GetController().GetLastCommittedEntry(); |
| 478 ASSERT_TRUE(entry); | 480 ASSERT_TRUE(entry); |
| 479 const int first_nav_id = entry->GetUniqueID(); | 481 const int first_nav_id = entry->GetUniqueID(); |
| 480 | 482 |
| 481 // Run the extension action, which should cause a page refresh (since we | 483 // Run the extension action, which should cause a page refresh (since we |
| 482 // automatically accepted the bubble prompting us), and the extension should | 484 // automatically accepted the bubble prompting us), and the extension should |
| 483 // have injected at document start. | 485 // have injected at document start. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 499 web_contents->GetController().Reload(true); | 501 web_contents->GetController().Reload(true); |
| 500 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); | 502 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); |
| 501 | 503 |
| 502 // The extension should again want to run. Automatically dismiss the bubble | 504 // The extension should again want to run. Automatically dismiss the bubble |
| 503 // that pops up prompting for page refresh. | 505 // that pops up prompting for page refresh. |
| 504 EXPECT_TRUE(runner->WantsToRun(extension)); | 506 EXPECT_TRUE(runner->WantsToRun(extension)); |
| 505 EXPECT_EQ("undefined", GetValue(web_contents)); | 507 EXPECT_EQ("undefined", GetValue(web_contents)); |
| 506 const int next_nav_id = | 508 const int next_nav_id = |
| 507 web_contents->GetController().GetLastCommittedEntry()->GetUniqueID(); | 509 web_contents->GetController().GetLastCommittedEntry()->GetUniqueID(); |
| 508 runner->set_default_bubble_close_action_for_testing( | 510 runner->set_default_bubble_close_action_for_testing( |
| 509 make_scoped_ptr(new ToolbarActionsBarBubbleDelegate::CloseAction( | 511 base::WrapUnique(new ToolbarActionsBarBubbleDelegate::CloseAction( |
| 510 ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS_USER_ACTION))); | 512 ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS_USER_ACTION))); |
| 511 | 513 |
| 512 // Try running the extension. Nothing should happen, because the user | 514 // Try running the extension. Nothing should happen, because the user |
| 513 // didn't agree to refresh the page. The extension should still want to run. | 515 // didn't agree to refresh the page. The extension should still want to run. |
| 514 runner->RunAction(extension, true); | 516 runner->RunAction(extension, true); |
| 515 base::RunLoop().RunUntilIdle(); | 517 base::RunLoop().RunUntilIdle(); |
| 516 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); | 518 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); |
| 517 EXPECT_EQ("undefined", GetValue(web_contents)); | 519 EXPECT_EQ("undefined", GetValue(web_contents)); |
| 518 EXPECT_EQ( | 520 EXPECT_EQ( |
| 519 next_nav_id, | 521 next_nav_id, |
| 520 web_contents->GetController().GetLastCommittedEntry()->GetUniqueID()); | 522 web_contents->GetController().GetLastCommittedEntry()->GetUniqueID()); |
| 521 | 523 |
| 522 // Repeat with a dismissal from bubble deactivation - same story. | 524 // Repeat with a dismissal from bubble deactivation - same story. |
| 523 runner->set_default_bubble_close_action_for_testing( | 525 runner->set_default_bubble_close_action_for_testing( |
| 524 make_scoped_ptr(new ToolbarActionsBarBubbleDelegate::CloseAction( | 526 base::WrapUnique(new ToolbarActionsBarBubbleDelegate::CloseAction( |
| 525 ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS_DEACTIVATION))); | 527 ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS_DEACTIVATION))); |
| 526 runner->RunAction(extension, true); | 528 runner->RunAction(extension, true); |
| 527 base::RunLoop().RunUntilIdle(); | 529 base::RunLoop().RunUntilIdle(); |
| 528 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); | 530 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); |
| 529 EXPECT_EQ("undefined", GetValue(web_contents)); | 531 EXPECT_EQ("undefined", GetValue(web_contents)); |
| 530 EXPECT_EQ( | 532 EXPECT_EQ( |
| 531 next_nav_id, | 533 next_nav_id, |
| 532 web_contents->GetController().GetLastCommittedEntry()->GetUniqueID()); | 534 web_contents->GetController().GetLastCommittedEntry()->GetUniqueID()); |
| 533 } | 535 } |
| 534 | 536 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 559 | 561 |
| 560 ASSERT_TRUE(embedded_test_server()->Start()); | 562 ASSERT_TRUE(embedded_test_server()->Start()); |
| 561 ui_test_utils::NavigateToURL( | 563 ui_test_utils::NavigateToURL( |
| 562 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); | 564 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); |
| 563 | 565 |
| 564 for (size_t i = 0u; i < arraysize(testers); ++i) | 566 for (size_t i = 0u; i < arraysize(testers); ++i) |
| 565 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; | 567 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; |
| 566 } | 568 } |
| 567 | 569 |
| 568 } // namespace extensions | 570 } // namespace extensions |
| OLD | NEW |