| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/browser/browsing_data/clear_site_data_throttle.h" | 5 #include "content/browser/browsing_data/clear_site_data_throttle.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 // information to the loaded website's title and C++ will wait until that | 57 // information to the loaded website's title and C++ will wait until that |
| 58 // happens. | 58 // happens. |
| 59 void WaitForTitle(const Shell* shell, const char* expected_title) { | 59 void WaitForTitle(const Shell* shell, const char* expected_title) { |
| 60 base::string16 expected_title_16 = base::ASCIIToUTF16(expected_title); | 60 base::string16 expected_title_16 = base::ASCIIToUTF16(expected_title); |
| 61 TitleWatcher title_watcher(shell->web_contents(), expected_title_16); | 61 TitleWatcher title_watcher(shell->web_contents(), expected_title_16); |
| 62 ASSERT_EQ(expected_title_16, title_watcher.WaitAndGetTitle()); | 62 ASSERT_EQ(expected_title_16, title_watcher.WaitAndGetTitle()); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // A value of the Clear-Site-Data header that requests cookie deletion. Reused | 65 // A value of the Clear-Site-Data header that requests cookie deletion. Reused |
| 66 // in tests that need a valid header but do not depend on its value. | 66 // in tests that need a valid header but do not depend on its value. |
| 67 static const char* kClearCookiesHeader = "{ \"types\": [ \"cookies\" ] }"; | 67 static const char* kClearCookiesHeader = "\"cookies\""; |
| 68 | 68 |
| 69 // A helper class to observe BrowsingDataRemover deletion tasks coming from | 69 // A helper class to observe BrowsingDataRemover deletion tasks coming from |
| 70 // ClearSiteData. | 70 // ClearSiteData. |
| 71 class TestBrowsingDataRemoverDelegate : public MockBrowsingDataRemoverDelegate { | 71 class TestBrowsingDataRemoverDelegate : public MockBrowsingDataRemoverDelegate { |
| 72 public: | 72 public: |
| 73 // Sets a test expectation that a Clear-Site-Data header call from |origin|, | 73 // Sets a test expectation that a Clear-Site-Data header call from |origin|, |
| 74 // instructing to delete |cookies|, |storage|, and |cache|, will schedule | 74 // instructing to delete |cookies|, |storage|, and |cache|, will schedule |
| 75 // the corresponding BrowsingDataRemover deletion tasks. | 75 // the corresponding BrowsingDataRemover deletion tasks. |
| 76 void ExpectClearSiteDataCall(const url::Origin& origin, | 76 void ExpectClearSiteDataCall(const url::Origin& origin, |
| 77 bool cookies, | 77 bool cookies, |
| (...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 // Tests that ClearSiteData() is called for the correct data types. | 723 // Tests that ClearSiteData() is called for the correct data types. |
| 724 IN_PROC_BROWSER_TEST_F(ClearSiteDataThrottleBrowserTest, Types) { | 724 IN_PROC_BROWSER_TEST_F(ClearSiteDataThrottleBrowserTest, Types) { |
| 725 GURL base_url = https_server()->GetURL("example.com", "/"); | 725 GURL base_url = https_server()->GetURL("example.com", "/"); |
| 726 | 726 |
| 727 struct TestCase { | 727 struct TestCase { |
| 728 const char* value; | 728 const char* value; |
| 729 bool remove_cookies; | 729 bool remove_cookies; |
| 730 bool remove_storage; | 730 bool remove_storage; |
| 731 bool remove_cache; | 731 bool remove_cache; |
| 732 } test_cases[] = { | 732 } test_cases[] = { |
| 733 {"{ \"types\": [ \"cookies\" ] }", true, false, false}, | 733 {"\"cookies\"", true, false, false}, |
| 734 {"{ \"types\": [ \"storage\" ] }", false, true, false}, | 734 {"\"storage\"", false, true, false}, |
| 735 {"{ \"types\": [ \"cache\" ] }", false, false, true}, | 735 {"\"cache\"", false, false, true}, |
| 736 {"{ \"types\": [ \"cookies\", \"storage\" ] }", true, true, false}, | 736 {"\"cookies\", \"storage\"", true, true, false}, |
| 737 {"{ \"types\": [ \"cookies\", \"cache\" ] }", true, false, true}, | 737 {"\"cookies\", \"cache\"", true, false, true}, |
| 738 {"{ \"types\": [ \"storage\", \"cache\" ] }", false, true, true}, | 738 {"\"storage\", \"cache\"", false, true, true}, |
| 739 {"{ \"types\": [ \"cookies\", \"storage\", \"cache\" ] }", true, true, | 739 {"\"cookies\", \"storage\", \"cache\"", true, true, true}, |
| 740 true}, | |
| 741 }; | 740 }; |
| 742 | 741 |
| 743 for (const TestCase& test_case : test_cases) { | 742 for (const TestCase& test_case : test_cases) { |
| 744 GURL url = base_url; | 743 GURL url = base_url; |
| 745 AddQuery(&url, "header", test_case.value); | 744 AddQuery(&url, "header", test_case.value); |
| 746 | 745 |
| 747 delegate()->ExpectClearSiteDataCall( | 746 delegate()->ExpectClearSiteDataCall( |
| 748 url::Origin(url), test_case.remove_cookies, test_case.remove_storage, | 747 url::Origin(url), test_case.remove_cookies, test_case.remove_storage, |
| 749 test_case.remove_cache); | 748 test_case.remove_cache); |
| 750 | 749 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 786 | 785 |
| 787 // There are two service workers installed on two origins. | 786 // There are two service workers installed on two origins. |
| 788 std::vector<ServiceWorkerUsageInfo> service_workers = GetServiceWorkers(); | 787 std::vector<ServiceWorkerUsageInfo> service_workers = GetServiceWorkers(); |
| 789 EXPECT_EQ(2u, service_workers.size()); | 788 EXPECT_EQ(2u, service_workers.size()); |
| 790 | 789 |
| 791 // Navigate to a URL within the scope of "origin1.com" which responds with | 790 // Navigate to a URL within the scope of "origin1.com" which responds with |
| 792 // a Clear-Site-Data header. Verify that this did NOT remove the service | 791 // a Clear-Site-Data header. Verify that this did NOT remove the service |
| 793 // worker for "origin1.com", as the header would not be respected outside | 792 // worker for "origin1.com", as the header would not be respected outside |
| 794 // of the scope. | 793 // of the scope. |
| 795 GURL url = https_server()->GetURL("origin1.com", "/anything-in-the-scope"); | 794 GURL url = https_server()->GetURL("origin1.com", "/anything-in-the-scope"); |
| 796 AddQuery(&url, "header", "{ \"types\": [ \"storage\" ] }"); | 795 AddQuery(&url, "header", "\"storage\""); |
| 797 NavigateToURL(shell(), url); | 796 NavigateToURL(shell(), url); |
| 798 service_workers = GetServiceWorkers(); | 797 service_workers = GetServiceWorkers(); |
| 799 EXPECT_EQ(2u, service_workers.size()); | 798 EXPECT_EQ(2u, service_workers.size()); |
| 800 | 799 |
| 801 // This time, we will navigate to a URL on "origin1.com" that is not handled | 800 // This time, we will navigate to a URL on "origin1.com" that is not handled |
| 802 // by the serice worker, but results in a network request. One such resource | 801 // by the serice worker, but results in a network request. One such resource |
| 803 // not handled by "worker.js" is the path "resource". | 802 // not handled by "worker.js" is the path "resource". |
| 804 // The header will be respected and the worker deleted. | 803 // The header will be respected and the worker deleted. |
| 805 url = https_server()->GetURL("origin1.com", "/resource"); | 804 url = https_server()->GetURL("origin1.com", "/resource"); |
| 806 AddQuery(&url, "header", "{ \"types\": [ \"storage\" ] }"); | 805 AddQuery(&url, "header", "\"storage\""); |
| 807 NavigateToURL(shell(), url); | 806 NavigateToURL(shell(), url); |
| 808 | 807 |
| 809 // Only "origin2.com" now has a service worker. | 808 // Only "origin2.com" now has a service worker. |
| 810 service_workers = GetServiceWorkers(); | 809 service_workers = GetServiceWorkers(); |
| 811 ASSERT_EQ(1u, service_workers.size()); | 810 ASSERT_EQ(1u, service_workers.size()); |
| 812 EXPECT_EQ(service_workers[0].origin, | 811 EXPECT_EQ(service_workers[0].origin, |
| 813 https_server()->GetURL("origin2.com", "/")); | 812 https_server()->GetURL("origin2.com", "/")); |
| 814 | 813 |
| 815 // TODO(msramek): Test that the service worker update ping also deletes | 814 // TODO(msramek): Test that the service worker update ping also deletes |
| 816 // the service worker. | 815 // the service worker. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 837 std::set<std::string> entries_to_create = {url1, url2, url3, url4}; | 836 std::set<std::string> entries_to_create = {url1, url2, url3, url4}; |
| 838 util.CreateCacheEntries(entries_to_create); | 837 util.CreateCacheEntries(entries_to_create); |
| 839 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | 838 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); |
| 840 | 839 |
| 841 // There are four cache entries on two origins. | 840 // There are four cache entries on two origins. |
| 842 std::vector<std::string> cache_keys = util.GetEntryKeys(); | 841 std::vector<std::string> cache_keys = util.GetEntryKeys(); |
| 843 EXPECT_EQ(4u, cache_keys.size()); | 842 EXPECT_EQ(4u, cache_keys.size()); |
| 844 | 843 |
| 845 // Let Clear-Site-Data delete the "cache" of "origin1.com". | 844 // Let Clear-Site-Data delete the "cache" of "origin1.com". |
| 846 GURL url = https_server()->GetURL("origin1.com", "/clear-site-data"); | 845 GURL url = https_server()->GetURL("origin1.com", "/clear-site-data"); |
| 847 AddQuery(&url, "header", "{ \"types\": [ \"cache\" ] }"); | 846 AddQuery(&url, "header", "\"cache\""); |
| 848 NavigateToURL(shell(), url); | 847 NavigateToURL(shell(), url); |
| 849 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | 848 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); |
| 850 | 849 |
| 851 // Only "origin2.com" now has cache entries. | 850 // Only "origin2.com" now has cache entries. |
| 852 cache_keys = util.GetEntryKeys(); | 851 cache_keys = util.GetEntryKeys(); |
| 853 ASSERT_EQ(2u, cache_keys.size()); | 852 ASSERT_EQ(2u, cache_keys.size()); |
| 854 std::sort(cache_keys.begin(), cache_keys.end()); | 853 std::sort(cache_keys.begin(), cache_keys.end()); |
| 855 EXPECT_EQ(url4, cache_keys[0]); | 854 EXPECT_EQ(url4, cache_keys[0]); |
| 856 EXPECT_EQ(url3, cache_keys[1]); | 855 EXPECT_EQ(url3, cache_keys[1]); |
| 857 } | 856 } |
| 858 | 857 |
| 859 // Tests that closing the tab right after executing Clear-Site-Data does | 858 // Tests that closing the tab right after executing Clear-Site-Data does |
| 860 // not crash. | 859 // not crash. |
| 861 IN_PROC_BROWSER_TEST_F(ClearSiteDataThrottleBrowserTest, ClosedTab) { | 860 IN_PROC_BROWSER_TEST_F(ClearSiteDataThrottleBrowserTest, ClosedTab) { |
| 862 GURL url = https_server()->GetURL("example.com", "/"); | 861 GURL url = https_server()->GetURL("example.com", "/"); |
| 863 AddQuery(&url, "header", kClearCookiesHeader); | 862 AddQuery(&url, "header", kClearCookiesHeader); |
| 864 shell()->LoadURL(url); | 863 shell()->LoadURL(url); |
| 865 shell()->Close(); | 864 shell()->Close(); |
| 866 } | 865 } |
| 867 | 866 |
| 868 } // namespace content | 867 } // namespace content |
| OLD | NEW |