| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| 11 #include "chrome/browser/extensions/active_tab_permission_granter.h" | 11 #include "chrome/browser/extensions/active_tab_permission_granter.h" |
| 12 #include "chrome/browser/extensions/extension_action_runner.h" | 12 #include "chrome/browser/extensions/extension_action_runner.h" |
| 13 #include "chrome/browser/extensions/extension_apitest.h" | 13 #include "chrome/browser/extensions/extension_apitest.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/extensions/tab_helper.h" | 15 #include "chrome/browser/extensions/tab_helper.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/search_engines/template_url_service_factory.h" | 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 18 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_navigator_params.h" | 19 #include "chrome/browser/ui/browser_navigator_params.h" |
| 20 #include "chrome/browser/ui/login/login_handler.h" | 20 #include "chrome/browser/ui/login/login_handler.h" |
| 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 22 #include "chrome/common/extensions/extension_process_policy.h" | 22 #include "chrome/common/extensions/extension_process_policy.h" |
| 23 #include "chrome/test/base/search_test_utils.h" | 23 #include "chrome/test/base/search_test_utils.h" |
| 24 #include "chrome/test/base/ui_test_utils.h" | 24 #include "chrome/test/base/ui_test_utils.h" |
| 25 #include "content/public/browser/notification_registrar.h" | 25 #include "content/public/browser/notification_registrar.h" |
| 26 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
| 27 #include "content/public/browser/render_frame_host.h" |
| 27 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 28 #include "content/public/browser/render_widget_host.h" | 29 #include "content/public/browser/render_widget_host.h" |
| 29 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 30 #include "content/public/test/browser_test_utils.h" | 31 #include "content/public/test/browser_test_utils.h" |
| 31 #include "extensions/browser/api/web_request/web_request_api.h" | 32 #include "extensions/browser/api/web_request/web_request_api.h" |
| 32 #include "extensions/browser/blocked_action_type.h" | 33 #include "extensions/browser/blocked_action_type.h" |
| 33 #include "extensions/browser/extension_system.h" | 34 #include "extensions/browser/extension_system.h" |
| 34 #include "extensions/common/extension_builder.h" | 35 #include "extensions/common/extension_builder.h" |
| 35 #include "extensions/common/features/feature.h" | 36 #include "extensions/common/features/feature.h" |
| 36 #include "extensions/test/extension_test_message_listener.h" | 37 #include "extensions/test/extension_test_message_listener.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 "var xhr = new XMLHttpRequest();\n" | 77 "var xhr = new XMLHttpRequest();\n" |
| 77 "xhr.open('GET', url);\n" | 78 "xhr.open('GET', url);\n" |
| 78 "xhr.onload = function() {\n" | 79 "xhr.onload = function() {\n" |
| 79 " window.domAutomationController.send(true);\n" | 80 " window.domAutomationController.send(true);\n" |
| 80 "};\n" | 81 "};\n" |
| 81 "xhr.onerror = function() {\n" | 82 "xhr.onerror = function() {\n" |
| 82 " window.domAutomationController.send(false);\n" | 83 " window.domAutomationController.send(false);\n" |
| 83 "};\n" | 84 "};\n" |
| 84 "xhr.send();\n"; | 85 "xhr.send();\n"; |
| 85 | 86 |
| 86 // Performs an XHR in the given |web_contents|, replying when complete. | 87 // Performs an XHR in the given |frame|, replying when complete. |
| 87 void PerformXhrInPage(content::WebContents* web_contents, | 88 void PerformXhrInFrame(content::RenderFrameHost* frame, |
| 88 const std::string& host, | 89 const std::string& host, |
| 89 int port, | 90 int port, |
| 90 const std::string& page) { | 91 const std::string& page) { |
| 91 bool success = false; | 92 bool success = false; |
| 92 EXPECT_TRUE(ExecuteScriptAndExtractBool( | 93 EXPECT_TRUE(ExecuteScriptAndExtractBool( |
| 93 web_contents, | 94 frame, |
| 94 base::StringPrintf(kPerformXhrJs, host.c_str(), port, page.c_str()), | 95 base::StringPrintf(kPerformXhrJs, host.c_str(), port, page.c_str()), |
| 95 &success)); | 96 &success)); |
| 96 EXPECT_TRUE(success); | 97 EXPECT_TRUE(success); |
| 97 } | 98 } |
| 98 | 99 |
| 99 // Returns the current count of webRequests received by the |extension| in | 100 // Returns the current count of webRequests received by the |extension| in |
| 100 // the background page (assumes the extension stores a value on the window | 101 // the background page (assumes the extension stores a value on the window |
| 101 // object). Returns -1 if something goes awry. | 102 // object). Returns -1 if something goes awry. |
| 102 int GetWebRequestCountFromBackgroundPage(const Extension* extension, | 103 int GetWebRequestCountFromBackgroundPage(const Extension* extension, |
| 103 content::BrowserContext* context) { | 104 content::BrowserContext* context) { |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 | 523 |
| 523 EXPECT_TRUE(listener1.WaitUntilSatisfied()); | 524 EXPECT_TRUE(listener1.WaitUntilSatisfied()); |
| 524 EXPECT_TRUE(listener2.WaitUntilSatisfied()); | 525 EXPECT_TRUE(listener2.WaitUntilSatisfied()); |
| 525 } | 526 } |
| 526 | 527 |
| 527 // Tests that webRequest works with the --scripts-require-action feature. | 528 // Tests that webRequest works with the --scripts-require-action feature. |
| 528 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, | 529 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, |
| 529 WebRequestWithWithheldPermissions) { | 530 WebRequestWithWithheldPermissions) { |
| 530 FeatureSwitch::ScopedOverride enable_scripts_require_action( | 531 FeatureSwitch::ScopedOverride enable_scripts_require_action( |
| 531 FeatureSwitch::scripts_require_action(), true); | 532 FeatureSwitch::scripts_require_action(), true); |
| 532 ASSERT_TRUE(StartEmbeddedTestServer()); | 533 |
| 534 host_resolver()->AddRule("*", "127.0.0.1"); |
| 535 ASSERT_TRUE(embedded_test_server()->Start()); |
| 536 content::SetupCrossSiteRedirector(embedded_test_server()); |
| 537 |
| 533 // Load an extension that registers a listener for webRequest events, and | 538 // Load an extension that registers a listener for webRequest events, and |
| 534 // wait 'til it's initialized. | 539 // wait 'til it's initialized. |
| 535 ExtensionTestMessageListener listener("ready", false); | 540 ExtensionTestMessageListener listener("ready", false); |
| 536 const Extension* extension = | 541 const Extension* extension = |
| 537 LoadExtension(test_data_dir_.AppendASCII("webrequest_activetab")); | 542 LoadExtension(test_data_dir_.AppendASCII("webrequest_activetab")); |
| 538 ASSERT_TRUE(extension) << message_; | 543 ASSERT_TRUE(extension) << message_; |
| 539 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 544 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 540 | 545 |
| 541 // Navigate the browser to a page in a new tab. | 546 // Navigate the browser to a page in a new tab. |
| 542 const std::string kHost = "example.com"; | 547 GURL url = embedded_test_server()->GetURL( |
| 543 GURL url = embedded_test_server()->GetURL(kHost, "/empty.html"); | 548 "/cross-site/a.com/iframe_cross_site.html"); |
| 549 const std::string kHost = "a.com"; |
| 544 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK); | 550 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK); |
| 545 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; | 551 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
| 546 ui_test_utils::NavigateToURL(¶ms); | 552 ui_test_utils::NavigateToURL(¶ms); |
| 547 | 553 |
| 548 content::WebContents* web_contents = | 554 content::WebContents* web_contents = |
| 549 browser()->tab_strip_model()->GetActiveWebContents(); | 555 browser()->tab_strip_model()->GetActiveWebContents(); |
| 550 ASSERT_TRUE(web_contents); | 556 ASSERT_TRUE(web_contents); |
| 551 ExtensionActionRunner* runner = | 557 ExtensionActionRunner* runner = |
| 552 ExtensionActionRunner::GetForWebContents(web_contents); | 558 ExtensionActionRunner::GetForWebContents(web_contents); |
| 553 ASSERT_TRUE(runner); | 559 ASSERT_TRUE(runner); |
| 554 | 560 |
| 555 int port = embedded_test_server()->port(); | 561 int port = embedded_test_server()->port(); |
| 556 const std::string kXhrPath = "simple.html"; | 562 const std::string kXhrPath = "simple.html"; |
| 557 | 563 |
| 558 // The extension shouldn't have currently received any webRequest events, | 564 // The extension shouldn't have currently received any webRequest events, |
| 559 // since it doesn't have permission (and shouldn't receive any from an XHR). | 565 // since it doesn't have permission (and shouldn't receive any from an XHR). |
| 560 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile())); | 566 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 561 PerformXhrInPage(web_contents, kHost, port, kXhrPath); | 567 |
| 568 content::RenderFrameHost* main_frame = nullptr; |
| 569 content::RenderFrameHost* child_frame = nullptr; |
| 570 auto get_main_and_child_frame = [](content::WebContents* web_contents, |
| 571 content::RenderFrameHost** main_frame, |
| 572 content::RenderFrameHost** child_frame) { |
| 573 *child_frame = nullptr; |
| 574 *main_frame = web_contents->GetMainFrame(); |
| 575 std::vector<content::RenderFrameHost*> all_frames = |
| 576 web_contents->GetAllFrames(); |
| 577 ASSERT_EQ(3u, all_frames.size()); |
| 578 *child_frame = all_frames[0] == *main_frame ? all_frames[1] : all_frames[0]; |
| 579 ASSERT_TRUE(*child_frame); |
| 580 }; |
| 581 |
| 582 get_main_and_child_frame(web_contents, &main_frame, &child_frame); |
| 583 const std::string kMainHost = main_frame->GetLastCommittedURL().host(); |
| 584 const std::string kChildHost = child_frame->GetLastCommittedURL().host(); |
| 585 |
| 586 PerformXhrInFrame(main_frame, kHost, port, kXhrPath); |
| 587 PerformXhrInFrame(child_frame, kChildHost, port, kXhrPath); |
| 562 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile())); | 588 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 589 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner->GetBlockedActions(extension)); |
| 563 | 590 |
| 564 // Grant activeTab permission, and perform another XHR. The extension should | 591 // Grant activeTab permission, and perform another XHR. The extension should |
| 565 // receive the event. | 592 // receive the event. |
| 566 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner->GetBlockedActions(extension)); | |
| 567 runner->set_default_bubble_close_action_for_testing( | 593 runner->set_default_bubble_close_action_for_testing( |
| 568 base::WrapUnique(new ToolbarActionsBarBubbleDelegate::CloseAction( | 594 base::WrapUnique(new ToolbarActionsBarBubbleDelegate::CloseAction( |
| 569 ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE))); | 595 ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE))); |
| 570 runner->RunAction(extension, true); | 596 runner->RunAction(extension, true); |
| 571 base::RunLoop().RunUntilIdle(); | 597 base::RunLoop().RunUntilIdle(); |
| 572 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); | 598 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); |
| 573 // The runner will have refreshed the page... | 599 // The runner will have refreshed the page... |
| 600 get_main_and_child_frame(web_contents, &main_frame, &child_frame); |
| 574 EXPECT_EQ(BLOCKED_ACTION_NONE, runner->GetBlockedActions(extension)); | 601 EXPECT_EQ(BLOCKED_ACTION_NONE, runner->GetBlockedActions(extension)); |
| 602 |
| 575 int xhr_count = GetWebRequestCountFromBackgroundPage(extension, profile()); | 603 int xhr_count = GetWebRequestCountFromBackgroundPage(extension, profile()); |
| 576 // ... which means that we should have a non-zero xhr count. | 604 // ... which means that we should have a non-zero xhr count... |
| 577 EXPECT_GT(xhr_count, 0); | 605 EXPECT_GT(xhr_count, 0); |
| 578 // And the extension should receive future events. | 606 // ... and the extension should receive future events. |
| 579 PerformXhrInPage(web_contents, kHost, port, kXhrPath); | 607 PerformXhrInFrame(main_frame, kHost, port, kXhrPath); |
| 580 ++xhr_count; | 608 ++xhr_count; |
| 581 EXPECT_EQ(xhr_count, | 609 EXPECT_EQ(xhr_count, |
| 582 GetWebRequestCountFromBackgroundPage(extension, profile())); | 610 GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 583 | 611 |
| 612 // However, activeTab only grants access to the main frame, not to child |
| 613 // frames. As such, trying to XHR in the child frame should still fail. |
| 614 PerformXhrInFrame(child_frame, kChildHost, port, kXhrPath); |
| 615 EXPECT_EQ(xhr_count, |
| 616 GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 617 // But since there's no way for the user to currently grant access to child |
| 618 // frames, this shouldn't show up as a blocked action. |
| 619 EXPECT_EQ(BLOCKED_ACTION_NONE, runner->GetBlockedActions(extension)); |
| 620 |
| 584 // If we revoke the extension's tab permissions, it should no longer receive | 621 // If we revoke the extension's tab permissions, it should no longer receive |
| 585 // webRequest events. | 622 // webRequest events. |
| 586 ActiveTabPermissionGranter* granter = | 623 ActiveTabPermissionGranter* granter = |
| 587 TabHelper::FromWebContents(web_contents)->active_tab_permission_granter(); | 624 TabHelper::FromWebContents(web_contents)->active_tab_permission_granter(); |
| 588 ASSERT_TRUE(granter); | 625 ASSERT_TRUE(granter); |
| 589 granter->RevokeForTesting(); | 626 granter->RevokeForTesting(); |
| 590 base::RunLoop().RunUntilIdle(); | 627 base::RunLoop().RunUntilIdle(); |
| 591 PerformXhrInPage(web_contents, kHost, port, kXhrPath); | 628 PerformXhrInFrame(main_frame, kHost, port, kXhrPath); |
| 592 EXPECT_EQ(xhr_count, | 629 EXPECT_EQ(xhr_count, |
| 593 GetWebRequestCountFromBackgroundPage(extension, profile())); | 630 GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 594 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner->GetBlockedActions(extension)); | 631 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner->GetBlockedActions(extension)); |
| 595 } | 632 } |
| 596 | 633 |
| 597 } // namespace extensions | 634 } // namespace extensions |
| OLD | NEW |