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 "chrome/browser/browsing_data/browsing_data_remover.h" | 5 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <list> |
10 #include <memory> | 11 #include <memory> |
11 #include <set> | 12 #include <set> |
12 #include <string> | 13 #include <string> |
13 #include <vector> | 14 #include <vector> |
14 | 15 |
15 #include "base/bind.h" | 16 #include "base/bind.h" |
16 #include "base/bind_helpers.h" | 17 #include "base/bind_helpers.h" |
17 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
18 #include "base/files/file_util.h" | 19 #include "base/files/file_util.h" |
19 #include "base/guid.h" | 20 #include "base/guid.h" |
(...skipping 1039 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1059 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); | 1060 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); |
1060 | 1061 |
1061 TestStoragePartition storage_partition; | 1062 TestStoragePartition storage_partition; |
1062 remover->OverrideStoragePartitionForTesting(&storage_partition); | 1063 remover->OverrideStoragePartitionForTesting(&storage_partition); |
1063 | 1064 |
1064 int origin_type_mask = BrowsingDataHelper::UNPROTECTED_WEB; | 1065 int origin_type_mask = BrowsingDataHelper::UNPROTECTED_WEB; |
1065 if (include_protected_origins) | 1066 if (include_protected_origins) |
1066 origin_type_mask |= BrowsingDataHelper::PROTECTED_WEB; | 1067 origin_type_mask |= BrowsingDataHelper::PROTECTED_WEB; |
1067 | 1068 |
1068 BrowsingDataRemoverCompletionObserver completion_observer(remover); | 1069 BrowsingDataRemoverCompletionObserver completion_observer(remover); |
1069 remover->Remove(BrowsingDataRemover::Period(period), remove_mask, | 1070 remover->RemoveAndReply(BrowsingDataRemover::Period(period), remove_mask, |
1070 origin_type_mask); | 1071 origin_type_mask, &completion_observer); |
1071 completion_observer.BlockUntilCompletion(); | 1072 completion_observer.BlockUntilCompletion(); |
1072 | 1073 |
1073 // Save so we can verify later. | 1074 // Save so we can verify later. |
1074 storage_partition_removal_data_ = | 1075 storage_partition_removal_data_ = |
1075 storage_partition.GetStoragePartitionRemovalData(); | 1076 storage_partition.GetStoragePartitionRemovalData(); |
1076 } | 1077 } |
1077 | 1078 |
1078 void BlockUntilOriginDataRemoved( | 1079 void BlockUntilOriginDataRemoved( |
1079 browsing_data::TimePeriod period, | 1080 browsing_data::TimePeriod period, |
1080 int remove_mask, | 1081 int remove_mask, |
1081 const BrowsingDataFilterBuilder& filter_builder) { | 1082 const BrowsingDataFilterBuilder& filter_builder) { |
1082 BrowsingDataRemover* remover = | 1083 BrowsingDataRemover* remover = |
1083 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); | 1084 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); |
1084 TestStoragePartition storage_partition; | 1085 TestStoragePartition storage_partition; |
1085 remover->OverrideStoragePartitionForTesting(&storage_partition); | 1086 remover->OverrideStoragePartitionForTesting(&storage_partition); |
1086 | 1087 |
1087 BrowsingDataRemoverCompletionObserver completion_observer(remover); | 1088 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; |
1088 remover->RemoveImpl(BrowsingDataRemover::Period(period), remove_mask, | 1089 remover->RemoveImpl(BrowsingDataRemover::Period(period), remove_mask, |
1089 filter_builder, BrowsingDataHelper::UNPROTECTED_WEB); | 1090 filter_builder, BrowsingDataHelper::UNPROTECTED_WEB); |
1090 completion_observer.BlockUntilCompletion(); | 1091 completion_inhibitor.BlockUntilNearCompletion(); |
| 1092 completion_inhibitor.ContinueToCompletion(); |
1091 | 1093 |
1092 // Save so we can verify later. | 1094 // Save so we can verify later. |
1093 storage_partition_removal_data_ = | 1095 storage_partition_removal_data_ = |
1094 storage_partition.GetStoragePartitionRemovalData(); | 1096 storage_partition.GetStoragePartitionRemovalData(); |
1095 } | 1097 } |
1096 | 1098 |
1097 TestingProfile* GetProfile() { | 1099 TestingProfile* GetProfile() { |
1098 return profile_.get(); | 1100 return profile_.get(); |
1099 } | 1101 } |
1100 | 1102 |
(...skipping 1071 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2172 }; | 2174 }; |
2173 | 2175 |
2174 TEST_F(BrowsingDataRemoverTest, CompletionInhibition) { | 2176 TEST_F(BrowsingDataRemoverTest, CompletionInhibition) { |
2175 // The |completion_inhibitor| on the stack should prevent removal sessions | 2177 // The |completion_inhibitor| on the stack should prevent removal sessions |
2176 // from completing until after ContinueToCompletion() is called. | 2178 // from completing until after ContinueToCompletion() is called. |
2177 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; | 2179 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; |
2178 | 2180 |
2179 BrowsingDataRemover* remover = | 2181 BrowsingDataRemover* remover = |
2180 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); | 2182 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); |
2181 InspectableCompletionObserver completion_observer(remover); | 2183 InspectableCompletionObserver completion_observer(remover); |
2182 remover->Remove(BrowsingDataRemover::Unbounded(), | 2184 remover->RemoveAndReply(BrowsingDataRemover::Unbounded(), |
2183 BrowsingDataRemover::REMOVE_HISTORY, | 2185 BrowsingDataRemover::REMOVE_HISTORY, |
2184 BrowsingDataHelper::UNPROTECTED_WEB); | 2186 BrowsingDataHelper::UNPROTECTED_WEB, |
| 2187 &completion_observer); |
2185 | 2188 |
2186 // Process messages until the inhibitor is notified, and then some, to make | 2189 // Process messages until the inhibitor is notified, and then some, to make |
2187 // sure we do not complete asynchronously before ContinueToCompletion() is | 2190 // sure we do not complete asynchronously before ContinueToCompletion() is |
2188 // called. | 2191 // called. |
2189 completion_inhibitor.BlockUntilNearCompletion(); | 2192 completion_inhibitor.BlockUntilNearCompletion(); |
2190 base::RunLoop().RunUntilIdle(); | 2193 base::RunLoop().RunUntilIdle(); |
2191 | 2194 |
2192 // Verify that the removal has not yet been completed and the observer has | 2195 // Verify that the removal has not yet been completed and the observer has |
2193 // not been called. | 2196 // not been called. |
2194 EXPECT_TRUE(remover->is_removing()); | 2197 EXPECT_TRUE(remover->is_removing()); |
2195 EXPECT_FALSE(completion_observer.called()); | 2198 EXPECT_FALSE(completion_observer.called()); |
2196 | 2199 |
2197 // Now run the removal process until completion, and verify that observers are | 2200 // Now run the removal process until completion, and verify that observers are |
2198 // now notified, and the notifications is sent out. | 2201 // now notified, and the notifications is sent out. |
2199 completion_inhibitor.ContinueToCompletion(); | 2202 completion_inhibitor.ContinueToCompletion(); |
2200 completion_observer.BlockUntilCompletion(); | 2203 completion_observer.BlockUntilCompletion(); |
2201 | 2204 |
2202 EXPECT_FALSE(remover->is_removing()); | 2205 EXPECT_FALSE(remover->is_removing()); |
2203 EXPECT_TRUE(completion_observer.called()); | 2206 EXPECT_TRUE(completion_observer.called()); |
2204 } | 2207 } |
2205 | 2208 |
2206 TEST_F(BrowsingDataRemoverTest, EarlyShutdown) { | 2209 TEST_F(BrowsingDataRemoverTest, EarlyShutdown) { |
2207 BrowsingDataRemover* remover = | 2210 BrowsingDataRemover* remover = |
2208 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); | 2211 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); |
2209 InspectableCompletionObserver completion_observer(remover); | 2212 InspectableCompletionObserver completion_observer(remover); |
2210 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; | 2213 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; |
2211 remover->Remove(BrowsingDataRemover::Unbounded(), | 2214 remover->RemoveAndReply(BrowsingDataRemover::Unbounded(), |
2212 BrowsingDataRemover::REMOVE_HISTORY, | 2215 BrowsingDataRemover::REMOVE_HISTORY, |
2213 BrowsingDataHelper::UNPROTECTED_WEB); | 2216 BrowsingDataHelper::UNPROTECTED_WEB, |
| 2217 &completion_observer); |
2214 | 2218 |
2215 completion_inhibitor.BlockUntilNearCompletion(); | 2219 completion_inhibitor.BlockUntilNearCompletion(); |
2216 | 2220 |
2217 // Verify that the deletion has not yet been completed and the observer has | 2221 // Verify that the deletion has not yet been completed and the observer has |
2218 // not been called. | 2222 // not been called. |
2219 EXPECT_TRUE(remover->is_removing()); | 2223 EXPECT_TRUE(remover->is_removing()); |
2220 EXPECT_FALSE(completion_observer.called()); | 2224 EXPECT_FALSE(completion_observer.called()); |
2221 | 2225 |
2222 // Destroying the profile should trigger the notification. | 2226 // Destroying the profile should trigger the notification. |
2223 DestroyProfile(); | 2227 DestroyProfile(); |
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2585 BrowsingDataRemover::ClearSettingsForOneTypeWithPredicate( | 2589 BrowsingDataRemover::ClearSettingsForOneTypeWithPredicate( |
2586 host_content_settings_map, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 2590 host_content_settings_map, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
2587 base::Bind(&MatchPrimaryPattern, http_pattern)); | 2591 base::Bind(&MatchPrimaryPattern, http_pattern)); |
2588 // Verify we only have one, and it's url1. | 2592 // Verify we only have one, and it's url1. |
2589 host_content_settings_map->GetSettingsForOneType( | 2593 host_content_settings_map->GetSettingsForOneType( |
2590 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); | 2594 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); |
2591 EXPECT_EQ(1u, host_settings.size()); | 2595 EXPECT_EQ(1u, host_settings.size()); |
2592 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), | 2596 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), |
2593 host_settings[0].primary_pattern); | 2597 host_settings[0].primary_pattern); |
2594 } | 2598 } |
| 2599 |
| 2600 class MultipleTasksObserver { |
| 2601 public: |
| 2602 // A simple implementation of BrowsingDataRemover::Observer. |
| 2603 // MultipleTasksObserver will use several instances of Target to test |
| 2604 // that completion callbacks are returned to the correct one. |
| 2605 class Target : public BrowsingDataRemover::Observer { |
| 2606 public: |
| 2607 Target(MultipleTasksObserver* parent, BrowsingDataRemover* remover) |
| 2608 : parent_(parent), |
| 2609 observer_(this) { |
| 2610 observer_.Add(remover); |
| 2611 } |
| 2612 ~Target() override {} |
| 2613 |
| 2614 void OnBrowsingDataRemoverDone() override { |
| 2615 parent_->SetLastCalledTarget(this); |
| 2616 } |
| 2617 |
| 2618 private: |
| 2619 MultipleTasksObserver* parent_; |
| 2620 ScopedObserver<BrowsingDataRemover, BrowsingDataRemover::Observer> |
| 2621 observer_; |
| 2622 }; |
| 2623 |
| 2624 explicit MultipleTasksObserver(BrowsingDataRemover* remover) |
| 2625 : target_a_(this, remover), |
| 2626 target_b_(this, remover), |
| 2627 last_called_target_(nullptr) {} |
| 2628 ~MultipleTasksObserver() {} |
| 2629 |
| 2630 void ClearLastCalledTarget() { |
| 2631 last_called_target_ = nullptr; |
| 2632 } |
| 2633 |
| 2634 Target* GetLastCalledTarget() { |
| 2635 return last_called_target_; |
| 2636 } |
| 2637 |
| 2638 Target* target_a() { return &target_a_; } |
| 2639 Target* target_b() { return &target_b_; } |
| 2640 |
| 2641 private: |
| 2642 void SetLastCalledTarget(Target* target) { |
| 2643 DCHECK(!last_called_target_) |
| 2644 << "Call ClearLastCalledTarget() before every removal task."; |
| 2645 last_called_target_ = target; |
| 2646 } |
| 2647 |
| 2648 Target target_a_; |
| 2649 Target target_b_; |
| 2650 Target* last_called_target_; |
| 2651 }; |
| 2652 |
| 2653 TEST_F(BrowsingDataRemoverTest, MultipleTasks) { |
| 2654 BrowsingDataRemover* remover = |
| 2655 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); |
| 2656 EXPECT_FALSE(remover->is_removing()); |
| 2657 |
| 2658 std::unique_ptr<RegistrableDomainFilterBuilder> filter_builder_1( |
| 2659 new RegistrableDomainFilterBuilder( |
| 2660 RegistrableDomainFilterBuilder::WHITELIST)); |
| 2661 std::unique_ptr<RegistrableDomainFilterBuilder> filter_builder_2( |
| 2662 new RegistrableDomainFilterBuilder( |
| 2663 RegistrableDomainFilterBuilder::BLACKLIST)); |
| 2664 filter_builder_2->AddRegisterableDomain("example.com"); |
| 2665 |
| 2666 MultipleTasksObserver observer(remover); |
| 2667 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; |
| 2668 |
| 2669 // Test several tasks with various configuration of masks, filters, and target |
| 2670 // observers. |
| 2671 std::list<BrowsingDataRemover::RemovalTask> tasks; |
| 2672 tasks.emplace_back( |
| 2673 BrowsingDataRemover::Unbounded(), |
| 2674 BrowsingDataRemover::REMOVE_HISTORY, |
| 2675 BrowsingDataHelper::UNPROTECTED_WEB, |
| 2676 base::WrapUnique(new RegistrableDomainFilterBuilder( |
| 2677 RegistrableDomainFilterBuilder::BLACKLIST)), |
| 2678 observer.target_a()); |
| 2679 tasks.emplace_back( |
| 2680 BrowsingDataRemover::Unbounded(), |
| 2681 BrowsingDataRemover::REMOVE_COOKIES, |
| 2682 BrowsingDataHelper::PROTECTED_WEB, |
| 2683 base::WrapUnique(new RegistrableDomainFilterBuilder( |
| 2684 RegistrableDomainFilterBuilder::BLACKLIST)), |
| 2685 nullptr); |
| 2686 tasks.emplace_back( |
| 2687 BrowsingDataRemover::TimeRange(base::Time::Now(), base::Time::Max()), |
| 2688 BrowsingDataRemover::REMOVE_PASSWORDS, |
| 2689 BrowsingDataHelper::ALL, |
| 2690 base::WrapUnique(new RegistrableDomainFilterBuilder( |
| 2691 RegistrableDomainFilterBuilder::BLACKLIST)), |
| 2692 observer.target_b()); |
| 2693 tasks.emplace_back( |
| 2694 BrowsingDataRemover::TimeRange(base::Time(), base::Time::UnixEpoch()), |
| 2695 BrowsingDataRemover::REMOVE_PASSWORDS, |
| 2696 BrowsingDataHelper::UNPROTECTED_WEB, |
| 2697 std::move(filter_builder_1), |
| 2698 observer.target_b()); |
| 2699 tasks.emplace_back( |
| 2700 BrowsingDataRemover::TimeRange( |
| 2701 base::Time::UnixEpoch(), base::Time::Now()), |
| 2702 BrowsingDataRemover::REMOVE_CHANNEL_IDS, |
| 2703 BrowsingDataHelper::ALL, |
| 2704 std::move(filter_builder_2), |
| 2705 nullptr); |
| 2706 |
| 2707 for (BrowsingDataRemover::RemovalTask& task : tasks) { |
| 2708 // All tasks can be directly translated to a RemoveInternal() call. Since |
| 2709 // that is a private method, we must call the four public versions of |
| 2710 // Remove.* instead. This also serves as a test that those methods are all |
| 2711 // correctly reduced to RemoveInternal(). |
| 2712 if (!task.observer && task.filter_builder->IsEmptyBlacklist()) { |
| 2713 remover->Remove(task.time_range, task.remove_mask, task.origin_type_mask); |
| 2714 } else if (task.filter_builder->IsEmptyBlacklist()) { |
| 2715 remover->RemoveAndReply(task.time_range, task.remove_mask, |
| 2716 task.origin_type_mask, task.observer); |
| 2717 } else if (!task.observer) { |
| 2718 remover->RemoveWithFilter(task.time_range, task.remove_mask, |
| 2719 task.origin_type_mask, |
| 2720 std::move(task.filter_builder)); |
| 2721 } else { |
| 2722 remover->RemoveWithFilterAndReply(task.time_range, task.remove_mask, |
| 2723 task.origin_type_mask, |
| 2724 std::move(task.filter_builder), |
| 2725 task.observer); |
| 2726 } |
| 2727 } |
| 2728 |
| 2729 // Use the inhibitor to stop after every task and check the results. |
| 2730 for (BrowsingDataRemover::RemovalTask& task : tasks) { |
| 2731 EXPECT_TRUE(remover->is_removing()); |
| 2732 observer.ClearLastCalledTarget(); |
| 2733 |
| 2734 // Finish the task execution synchronously. |
| 2735 completion_inhibitor.BlockUntilNearCompletion(); |
| 2736 completion_inhibitor.ContinueToCompletion(); |
| 2737 |
| 2738 // Observers, if any, should have been called by now (since we call |
| 2739 // observers on the same thread). |
| 2740 EXPECT_EQ(task.observer, observer.GetLastCalledTarget()); |
| 2741 |
| 2742 // TODO(msramek): If BrowsingDataRemover took ownership of the last used |
| 2743 // filter builder and exposed it, we could also test it here. Make it so. |
| 2744 EXPECT_EQ(task.remove_mask, GetRemovalMask()); |
| 2745 EXPECT_EQ(task.origin_type_mask, GetOriginTypeMask()); |
| 2746 EXPECT_EQ(task.time_range.begin, GetBeginTime()); |
| 2747 } |
| 2748 |
| 2749 EXPECT_FALSE(remover->is_removing()); |
| 2750 } |
| 2751 |
| 2752 // The previous test, BrowsingDataRemoverTest.MultipleTasks, tests that the |
| 2753 // tasks are not mixed up and they are executed in a correct order. However, |
| 2754 // the completion inhibitor kept synchronizing the execution in order to verify |
| 2755 // the parameters. This test demonstrates that even running the tasks without |
| 2756 // inhibition is executed correctly and doesn't crash. |
| 2757 TEST_F(BrowsingDataRemoverTest, MultipleTasksInQuickSuccession) { |
| 2758 BrowsingDataRemover* remover = |
| 2759 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); |
| 2760 EXPECT_FALSE(remover->is_removing()); |
| 2761 |
| 2762 int test_removal_masks[] = { |
| 2763 BrowsingDataRemover::REMOVE_COOKIES, |
| 2764 BrowsingDataRemover::REMOVE_PASSWORDS, |
| 2765 BrowsingDataRemover::REMOVE_COOKIES, |
| 2766 BrowsingDataRemover::REMOVE_COOKIES, |
| 2767 BrowsingDataRemover::REMOVE_COOKIES, |
| 2768 BrowsingDataRemover::REMOVE_HISTORY, |
| 2769 BrowsingDataRemover::REMOVE_HISTORY, |
| 2770 BrowsingDataRemover::REMOVE_HISTORY, |
| 2771 BrowsingDataRemover::REMOVE_COOKIES | BrowsingDataRemover::REMOVE_HISTORY, |
| 2772 BrowsingDataRemover::REMOVE_COOKIES | BrowsingDataRemover::REMOVE_HISTORY, |
| 2773 BrowsingDataRemover::REMOVE_COOKIES | |
| 2774 BrowsingDataRemover::REMOVE_HISTORY | |
| 2775 BrowsingDataRemover::REMOVE_PASSWORDS, |
| 2776 BrowsingDataRemover::REMOVE_PASSWORDS, |
| 2777 BrowsingDataRemover::REMOVE_PASSWORDS, |
| 2778 }; |
| 2779 |
| 2780 for (int removal_mask : test_removal_masks) { |
| 2781 remover->Remove(BrowsingDataRemover::Unbounded(), removal_mask, |
| 2782 BrowsingDataHelper::UNPROTECTED_WEB); |
| 2783 } |
| 2784 |
| 2785 EXPECT_TRUE(remover->is_removing()); |
| 2786 |
| 2787 // Add one more deletion and wait for it. |
| 2788 BlockUntilBrowsingDataRemoved( |
| 2789 browsing_data::ALL_TIME, |
| 2790 BrowsingDataRemover::REMOVE_COOKIES, |
| 2791 BrowsingDataHelper::UNPROTECTED_WEB); |
| 2792 |
| 2793 EXPECT_FALSE(remover->is_removing()); |
| 2794 } |
OLD | NEW |