| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "chrome/browser/download/download_shelf.h" |
| 13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 14 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 14 #include "chrome/browser/extensions/browser_action_test_util.h" | 15 #include "chrome/browser/extensions/browser_action_test_util.h" |
| 15 #include "chrome/browser/extensions/extension_action.h" | 16 #include "chrome/browser/extensions/extension_action.h" |
| 16 #include "chrome/browser/extensions/extension_action_icon_factory.h" | 17 #include "chrome/browser/extensions/extension_action_icon_factory.h" |
| 17 #include "chrome/browser/extensions/extension_action_manager.h" | 18 #include "chrome/browser/extensions/extension_action_manager.h" |
| 18 #include "chrome/browser/extensions/extension_action_runner.h" | 19 #include "chrome/browser/extensions/extension_action_runner.h" |
| 19 #include "chrome/browser/extensions/extension_apitest.h" | 20 #include "chrome/browser/extensions/extension_apitest.h" |
| 20 #include "chrome/browser/extensions/extension_tab_util.h" | 21 #include "chrome/browser/extensions/extension_tab_util.h" |
| 21 #include "chrome/browser/extensions/extension_util.h" | 22 #include "chrome/browser/extensions/extension_util.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/ui/browser.h" | 24 #include "chrome/browser/ui/browser.h" |
| 24 #include "chrome/browser/ui/browser_commands.h" | 25 #include "chrome/browser/ui/browser_commands.h" |
| 25 #include "chrome/browser/ui/browser_finder.h" | 26 #include "chrome/browser/ui/browser_finder.h" |
| 26 #include "chrome/browser/ui/browser_navigator_params.h" | 27 #include "chrome/browser/ui/browser_navigator_params.h" |
| 27 #include "chrome/browser/ui/browser_window.h" | 28 #include "chrome/browser/ui/browser_window.h" |
| 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 29 #include "chrome/common/extensions/extension_process_policy.h" | |
| 30 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
| 31 #include "chrome/common/url_constants.h" | 31 #include "chrome/common/url_constants.h" |
| 32 #include "chrome/test/base/ui_test_utils.h" | 32 #include "chrome/test/base/ui_test_utils.h" |
| 33 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
| 34 #include "content/public/browser/browser_context.h" | 34 #include "content/public/browser/browser_context.h" |
| 35 #include "content/public/browser/notification_service.h" | 35 #include "content/public/browser/notification_service.h" |
| 36 #include "content/public/browser/render_frame_host.h" | 36 #include "content/public/browser/render_frame_host.h" |
| 37 #include "content/public/browser/web_contents.h" | 37 #include "content/public/browser/web_contents.h" |
| 38 #include "content/public/test/browser_test_utils.h" | 38 #include "content/public/test/browser_test_utils.h" |
| 39 #include "content/public/test/content_browser_test_utils.h" | 39 #include "content/public/test/content_browser_test_utils.h" |
| (...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 } | 849 } |
| 850 | 850 |
| 851 enum ExpectedNavigationStatus { | 851 enum ExpectedNavigationStatus { |
| 852 EXPECTING_NAVIGATION_SUCCESS, | 852 EXPECTING_NAVIGATION_SUCCESS, |
| 853 EXPECTING_NAVIGATION_FAILURE, | 853 EXPECTING_NAVIGATION_FAILURE, |
| 854 }; | 854 }; |
| 855 | 855 |
| 856 void TestPopupNavigationViaGet( | 856 void TestPopupNavigationViaGet( |
| 857 const GURL& target_url, | 857 const GURL& target_url, |
| 858 ExpectedNavigationStatus expected_navigation_status) { | 858 ExpectedNavigationStatus expected_navigation_status) { |
| 859 std::string navigation_starting_script = | |
| 860 "window.location = '" + target_url.spec() + "';\n"; | |
| 861 TestPopupNavigation(target_url, expected_navigation_status, | 859 TestPopupNavigation(target_url, expected_navigation_status, |
| 862 navigation_starting_script); | 860 CreateNavigationScriptForGet(target_url)); |
| 863 } | 861 } |
| 864 | 862 |
| 865 void TestPopupNavigationViaPost( | 863 void TestPopupNavigationViaPost( |
| 866 const GURL& target_url, | 864 const GURL& target_url, |
| 867 ExpectedNavigationStatus expected_navigation_status) { | 865 ExpectedNavigationStatus expected_navigation_status) { |
| 868 std::string navigation_starting_script = | |
| 869 "var form = document.getElementById('form');\n" | |
| 870 "form.action = '" + target_url.spec() + "';\n" | |
| 871 "form.submit();\n"; | |
| 872 TestPopupNavigation(target_url, expected_navigation_status, | 866 TestPopupNavigation(target_url, expected_navigation_status, |
| 873 navigation_starting_script); | 867 CreateNavigationScriptForPost(target_url)); |
| 868 } |
| 869 |
| 870 void TestPopupDownloadViaGet(const GURL& target_url) { |
| 871 TestPopupDownload(target_url, CreateNavigationScriptForGet(target_url)); |
| 872 } |
| 873 |
| 874 void TestPopupDownloadViaPost(const GURL& target_url) { |
| 875 TestPopupDownload(target_url, CreateNavigationScriptForPost(target_url)); |
| 874 } | 876 } |
| 875 | 877 |
| 876 private: | 878 private: |
| 879 std::string CreateNavigationScriptForGet(const GURL& target_url) { |
| 880 return "window.location = '" + target_url.spec() + "';\n"; |
| 881 } |
| 882 |
| 883 std::string CreateNavigationScriptForPost(const GURL& target_url) { |
| 884 return "var form = document.getElementById('form');\n" |
| 885 "form.action = '" + |
| 886 target_url.spec() + |
| 887 "';\n" |
| 888 "form.submit();\n"; |
| 889 } |
| 890 |
| 877 void TestPopupNavigation(const GURL& target_url, | 891 void TestPopupNavigation(const GURL& target_url, |
| 878 ExpectedNavigationStatus expected_navigation_status, | 892 ExpectedNavigationStatus expected_navigation_status, |
| 879 std::string navigation_starting_script) { | 893 std::string navigation_starting_script) { |
| 880 // Were there any failures so far (e.g. in SetUpOnMainThread)? | 894 // Were there any failures so far (e.g. in SetUpOnMainThread)? |
| 881 ASSERT_FALSE(HasFailure()); | 895 ASSERT_FALSE(HasFailure()); |
| 882 | 896 |
| 883 // Simulate a click on the browser action to open the popup. | 897 // Simulate a click on the browser action to open the popup. |
| 884 WebContents* popup = OpenPopup(0); | 898 WebContents* popup = OpenPopup(0); |
| 885 ASSERT_TRUE(popup); | 899 ASSERT_TRUE(popup); |
| 886 GURL popup_url = popup_extension().GetResourceURL("popup.html"); | 900 GURL popup_url = popup_extension().GetResourceURL("popup.html"); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 938 // source WebContents]). | 952 // source WebContents]). |
| 939 TabStripModel* tabs = browser()->tab_strip_model(); | 953 TabStripModel* tabs = browser()->tab_strip_model(); |
| 940 for (int i = 0; i < tabs->count(); i++) { | 954 for (int i = 0; i < tabs->count(); i++) { |
| 941 WebContents* tab_contents = tabs->GetWebContentsAt(i); | 955 WebContents* tab_contents = tabs->GetWebContentsAt(i); |
| 942 WaitForLoadStop(tab_contents); | 956 WaitForLoadStop(tab_contents); |
| 943 EXPECT_NE(target_url, tab_contents->GetLastCommittedURL()) | 957 EXPECT_NE(target_url, tab_contents->GetLastCommittedURL()) |
| 944 << "Navigating an extension pop-up should not affect tabs."; | 958 << "Navigating an extension pop-up should not affect tabs."; |
| 945 } | 959 } |
| 946 } | 960 } |
| 947 | 961 |
| 962 void TestPopupDownload(const GURL& target_url, |
| 963 std::string navigation_starting_script) { |
| 964 content::DownloadTestObserverTerminal downloads_observer( |
| 965 content::BrowserContext::GetDownloadManager(browser()->profile()), |
| 966 1, // == wait_count (only waiting for a single file). |
| 967 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); |
| 968 |
| 969 // Override the default downloads directory, so that the test can cleanup |
| 970 // after itself. This section is based on CreateAndSetDownloadsDirectory |
| 971 // method defined in a few other source files with tests. |
| 972 std::unique_ptr<base::ScopedTempDir> downloads_directory = |
| 973 CreateAndSetDownloadsDirectory(browser()->profile()->GetPrefs()); |
| 974 ASSERT_TRUE(downloads_directory); |
| 975 |
| 976 // Navigate to a URL that will trigger a download (i.e. by replying with |
| 977 // Content-Disposition: attachment; filename=... |
| 978 // header). |
| 979 TestPopupNavigation(target_url, EXPECTING_NAVIGATION_FAILURE, |
| 980 navigation_starting_script); |
| 981 |
| 982 // Verify that a single file got downloaded. |
| 983 downloads_observer.WaitForFinished(); |
| 984 EXPECT_EQ(0u, downloads_observer.NumDangerousDownloadsSeen()); |
| 985 EXPECT_EQ(1u, downloads_observer.NumDownloadsSeenInState( |
| 986 content::DownloadItem::COMPLETE)); |
| 987 EXPECT_TRUE(base::PathExists(downloads_directory->GetPath().AppendASCII( |
| 988 "download-test3-attachment.gif"))); |
| 989 |
| 990 #if !defined(OS_CHROMEOS) |
| 991 // The test verification below is applicable only to scenarios where the |
| 992 // download shelf is supported - on ChromeOS, instead of the download shelf, |
| 993 // there is a download notification in the right-bottom corner of the |
| 994 // screen. |
| 995 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| 996 browser()->window()->GetDownloadShelf()->Hide(); |
| 997 #endif |
| 998 } |
| 999 |
| 948 const Extension* popup_extension_; | 1000 const Extension* popup_extension_; |
| 949 const Extension* other_extension_; | 1001 const Extension* other_extension_; |
| 950 }; | 1002 }; |
| 951 | 1003 |
| 952 // Tests that an extension pop-up cannot be navigated to a web page. | 1004 // Tests that an extension pop-up cannot be navigated to a web page. |
| 953 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest, Webpage) { | 1005 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest, Webpage) { |
| 954 GURL web_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); | 1006 GURL web_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 955 | 1007 |
| 956 // The GET request will be blocked in ExtensionViewHost::OpenURLFromTab | 1008 // The GET request will be blocked in ExtensionViewHost::OpenURLFromTab |
| 957 // (which silently drops navigations with CURRENT_TAB disposition). | 1009 // (which silently drops navigations with CURRENT_TAB disposition). |
| (...skipping 20 matching lines...) Expand all Loading... |
| 978 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest, | 1030 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest, |
| 979 PageInOtherExtension) { | 1031 PageInOtherExtension) { |
| 980 GURL other_extension_url = other_extension().GetResourceURL("other.html"); | 1032 GURL other_extension_url = other_extension().GetResourceURL("other.html"); |
| 981 TestPopupNavigationViaGet(other_extension_url, EXPECTING_NAVIGATION_FAILURE); | 1033 TestPopupNavigationViaGet(other_extension_url, EXPECTING_NAVIGATION_FAILURE); |
| 982 TestPopupNavigationViaPost(other_extension_url, EXPECTING_NAVIGATION_FAILURE); | 1034 TestPopupNavigationViaPost(other_extension_url, EXPECTING_NAVIGATION_FAILURE); |
| 983 } | 1035 } |
| 984 | 1036 |
| 985 // Tests that navigating an extension pop-up to a http URI that returns | 1037 // Tests that navigating an extension pop-up to a http URI that returns |
| 986 // Content-Disposition: attachment; filename=... | 1038 // Content-Disposition: attachment; filename=... |
| 987 // works: No navigation, but download shelf visible + download goes through. | 1039 // works: No navigation, but download shelf visible + download goes through. |
| 988 // | 1040 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest, Download) { |
| 989 // Note - there is no "...ViaGet" flavour of this test, because we don't care | |
| 990 // (yet) if GET succeeds with the download or not (it probably should succeed | |
| 991 // for consistency with POST, but it always failed in M54 and before). After | |
| 992 // abandoing ShouldFork/OpenURL for all methods (not just for POST) [see comment | |
| 993 // about https://crbug.com/646261 in ChromeContentRendererClient::ShouldFork] | |
| 994 // GET should automagically start working for downloads. | |
| 995 // TODO(lukasza): https://crbug.com/650694: Add a "Get" flavour of the test once | |
| 996 // the download works both for GET and POST requests. | |
| 997 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest, DownloadViaPost) { | |
| 998 // Override the default downloads directory, so that the test can cleanup | |
| 999 // after itself. This section is based on CreateAndSetDownloadsDirectory | |
| 1000 // method defined in a few other source files with tests. | |
| 1001 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 1002 std::unique_ptr<base::ScopedTempDir> downloads_directory = | |
| 1003 CreateAndSetDownloadsDirectory(browser()->profile()->GetPrefs()); | |
| 1004 ASSERT_TRUE(downloads_directory); | |
| 1005 | |
| 1006 // Setup monitoring of the downloads. | |
| 1007 content::DownloadTestObserverTerminal downloads_observer( | |
| 1008 content::BrowserContext::GetDownloadManager(browser()->profile()), | |
| 1009 1, // == wait_count (only waiting for "download-test3.gif"). | |
| 1010 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); | |
| 1011 | |
| 1012 // Navigate to a URL that replies with | 1041 // Navigate to a URL that replies with |
| 1013 // Content-Disposition: attachment; filename=... | 1042 // Content-Disposition: attachment; filename=... |
| 1014 // header. | 1043 // header. |
| 1015 GURL download_url( | 1044 GURL download_url( |
| 1016 embedded_test_server()->GetURL("foo.com", "/download-test3.gif")); | 1045 embedded_test_server()->GetURL("foo.com", "/download-test3.gif")); |
| 1017 TestPopupNavigationViaPost(download_url, EXPECTING_NAVIGATION_FAILURE); | 1046 TestPopupDownloadViaGet(download_url); |
| 1018 | 1047 TestPopupDownloadViaPost(download_url); |
| 1019 // Verify that "download-test3.gif got downloaded. | |
| 1020 downloads_observer.WaitForFinished(); | |
| 1021 EXPECT_EQ(0u, downloads_observer.NumDangerousDownloadsSeen()); | |
| 1022 EXPECT_EQ(1u, downloads_observer.NumDownloadsSeenInState( | |
| 1023 content::DownloadItem::COMPLETE)); | |
| 1024 EXPECT_TRUE(base::PathExists(downloads_directory->GetPath().AppendASCII( | |
| 1025 "download-test3-attachment.gif"))); | |
| 1026 | |
| 1027 // The test verification below is applicable only to scenarios where the | |
| 1028 // download shelf is supported - on ChromeOS, instead of the download shelf, | |
| 1029 // there is a download notification in the right-bottom corner of the screen. | |
| 1030 #if !defined(OS_CHROMEOS) | |
| 1031 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | |
| 1032 #endif | |
| 1033 } | 1048 } |
| 1034 | 1049 |
| 1035 } // namespace | 1050 } // namespace |
| 1036 } // namespace extensions | 1051 } // namespace extensions |
| OLD | NEW |