| 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 <memory> | 10 #include <memory> |
| (...skipping 1048 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); | 1059 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); |
| 1060 | 1060 |
| 1061 TestStoragePartition storage_partition; | 1061 TestStoragePartition storage_partition; |
| 1062 remover->OverrideStoragePartitionForTesting(&storage_partition); | 1062 remover->OverrideStoragePartitionForTesting(&storage_partition); |
| 1063 | 1063 |
| 1064 int origin_type_mask = BrowsingDataHelper::UNPROTECTED_WEB; | 1064 int origin_type_mask = BrowsingDataHelper::UNPROTECTED_WEB; |
| 1065 if (include_protected_origins) | 1065 if (include_protected_origins) |
| 1066 origin_type_mask |= BrowsingDataHelper::PROTECTED_WEB; | 1066 origin_type_mask |= BrowsingDataHelper::PROTECTED_WEB; |
| 1067 | 1067 |
| 1068 BrowsingDataRemoverCompletionObserver completion_observer(remover); | 1068 BrowsingDataRemoverCompletionObserver completion_observer(remover); |
| 1069 remover->Remove(BrowsingDataRemover::Period(period), remove_mask, | 1069 remover->RemoveAndReply(BrowsingDataRemover::Period(period), remove_mask, |
| 1070 origin_type_mask); | 1070 origin_type_mask, &completion_observer); |
| 1071 completion_observer.BlockUntilCompletion(); | 1071 completion_observer.BlockUntilCompletion(); |
| 1072 | 1072 |
| 1073 // Save so we can verify later. | 1073 // Save so we can verify later. |
| 1074 storage_partition_removal_data_ = | 1074 storage_partition_removal_data_ = |
| 1075 storage_partition.GetStoragePartitionRemovalData(); | 1075 storage_partition.GetStoragePartitionRemovalData(); |
| 1076 } | 1076 } |
| 1077 | 1077 |
| 1078 void BlockUntilOriginDataRemoved( | 1078 void BlockUntilOriginDataRemoved( |
| 1079 browsing_data::TimePeriod period, | 1079 browsing_data::TimePeriod period, |
| 1080 int remove_mask, | 1080 int remove_mask, |
| 1081 const BrowsingDataFilterBuilder& filter_builder) { | 1081 const BrowsingDataFilterBuilder& filter_builder) { |
| 1082 BrowsingDataRemover* remover = | 1082 BrowsingDataRemover* remover = |
| 1083 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); | 1083 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); |
| 1084 TestStoragePartition storage_partition; | 1084 TestStoragePartition storage_partition; |
| 1085 remover->OverrideStoragePartitionForTesting(&storage_partition); | 1085 remover->OverrideStoragePartitionForTesting(&storage_partition); |
| 1086 | 1086 |
| 1087 BrowsingDataRemoverCompletionObserver completion_observer(remover); | 1087 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; |
| 1088 remover->RemoveImpl(BrowsingDataRemover::Period(period), remove_mask, | 1088 remover->RemoveImpl(BrowsingDataRemover::Period(period), remove_mask, |
| 1089 filter_builder, BrowsingDataHelper::UNPROTECTED_WEB); | 1089 filter_builder, BrowsingDataHelper::UNPROTECTED_WEB); |
| 1090 completion_observer.BlockUntilCompletion(); | 1090 completion_inhibitor.BlockUntilNearCompletion(); |
| 1091 completion_inhibitor.ContinueToCompletion(); |
| 1091 | 1092 |
| 1092 // Save so we can verify later. | 1093 // Save so we can verify later. |
| 1093 storage_partition_removal_data_ = | 1094 storage_partition_removal_data_ = |
| 1094 storage_partition.GetStoragePartitionRemovalData(); | 1095 storage_partition.GetStoragePartitionRemovalData(); |
| 1095 } | 1096 } |
| 1096 | 1097 |
| 1097 TestingProfile* GetProfile() { | 1098 TestingProfile* GetProfile() { |
| 1098 return profile_.get(); | 1099 return profile_.get(); |
| 1099 } | 1100 } |
| 1100 | 1101 |
| (...skipping 1071 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2172 }; | 2173 }; |
| 2173 | 2174 |
| 2174 TEST_F(BrowsingDataRemoverTest, CompletionInhibition) { | 2175 TEST_F(BrowsingDataRemoverTest, CompletionInhibition) { |
| 2175 // The |completion_inhibitor| on the stack should prevent removal sessions | 2176 // The |completion_inhibitor| on the stack should prevent removal sessions |
| 2176 // from completing until after ContinueToCompletion() is called. | 2177 // from completing until after ContinueToCompletion() is called. |
| 2177 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; | 2178 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; |
| 2178 | 2179 |
| 2179 BrowsingDataRemover* remover = | 2180 BrowsingDataRemover* remover = |
| 2180 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); | 2181 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); |
| 2181 InspectableCompletionObserver completion_observer(remover); | 2182 InspectableCompletionObserver completion_observer(remover); |
| 2182 remover->Remove(BrowsingDataRemover::Unbounded(), | 2183 remover->RemoveAndReply(BrowsingDataRemover::Unbounded(), |
| 2183 BrowsingDataRemover::REMOVE_HISTORY, | 2184 BrowsingDataRemover::REMOVE_HISTORY, |
| 2184 BrowsingDataHelper::UNPROTECTED_WEB); | 2185 BrowsingDataHelper::UNPROTECTED_WEB, |
| 2186 &completion_observer); |
| 2185 | 2187 |
| 2186 // Process messages until the inhibitor is notified, and then some, to make | 2188 // Process messages until the inhibitor is notified, and then some, to make |
| 2187 // sure we do not complete asynchronously before ContinueToCompletion() is | 2189 // sure we do not complete asynchronously before ContinueToCompletion() is |
| 2188 // called. | 2190 // called. |
| 2189 completion_inhibitor.BlockUntilNearCompletion(); | 2191 completion_inhibitor.BlockUntilNearCompletion(); |
| 2190 base::RunLoop().RunUntilIdle(); | 2192 base::RunLoop().RunUntilIdle(); |
| 2191 | 2193 |
| 2192 // Verify that the removal has not yet been completed and the observer has | 2194 // Verify that the removal has not yet been completed and the observer has |
| 2193 // not been called. | 2195 // not been called. |
| 2194 EXPECT_TRUE(remover->is_removing()); | 2196 EXPECT_TRUE(remover->is_removing()); |
| 2195 EXPECT_FALSE(completion_observer.called()); | 2197 EXPECT_FALSE(completion_observer.called()); |
| 2196 | 2198 |
| 2197 // Now run the removal process until completion, and verify that observers are | 2199 // Now run the removal process until completion, and verify that observers are |
| 2198 // now notified, and the notifications is sent out. | 2200 // now notified, and the notifications is sent out. |
| 2199 completion_inhibitor.ContinueToCompletion(); | 2201 completion_inhibitor.ContinueToCompletion(); |
| 2200 completion_observer.BlockUntilCompletion(); | 2202 completion_observer.BlockUntilCompletion(); |
| 2201 | 2203 |
| 2202 EXPECT_FALSE(remover->is_removing()); | 2204 EXPECT_FALSE(remover->is_removing()); |
| 2203 EXPECT_TRUE(completion_observer.called()); | 2205 EXPECT_TRUE(completion_observer.called()); |
| 2204 } | 2206 } |
| 2205 | 2207 |
| 2206 TEST_F(BrowsingDataRemoverTest, EarlyShutdown) { | 2208 TEST_F(BrowsingDataRemoverTest, EarlyShutdown) { |
| 2207 BrowsingDataRemover* remover = | 2209 BrowsingDataRemover* remover = |
| 2208 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); | 2210 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); |
| 2209 InspectableCompletionObserver completion_observer(remover); | 2211 InspectableCompletionObserver completion_observer(remover); |
| 2210 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; | 2212 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; |
| 2211 remover->Remove(BrowsingDataRemover::Unbounded(), | 2213 remover->RemoveAndReply(BrowsingDataRemover::Unbounded(), |
| 2212 BrowsingDataRemover::REMOVE_HISTORY, | 2214 BrowsingDataRemover::REMOVE_HISTORY, |
| 2213 BrowsingDataHelper::UNPROTECTED_WEB); | 2215 BrowsingDataHelper::UNPROTECTED_WEB, |
| 2216 &completion_observer); |
| 2214 | 2217 |
| 2215 completion_inhibitor.BlockUntilNearCompletion(); | 2218 completion_inhibitor.BlockUntilNearCompletion(); |
| 2216 | 2219 |
| 2217 // Verify that the deletion has not yet been completed and the observer has | 2220 // Verify that the deletion has not yet been completed and the observer has |
| 2218 // not been called. | 2221 // not been called. |
| 2219 EXPECT_TRUE(remover->is_removing()); | 2222 EXPECT_TRUE(remover->is_removing()); |
| 2220 EXPECT_FALSE(completion_observer.called()); | 2223 EXPECT_FALSE(completion_observer.called()); |
| 2221 | 2224 |
| 2222 // Destroying the profile should trigger the notification. | 2225 // Destroying the profile should trigger the notification. |
| 2223 DestroyProfile(); | 2226 DestroyProfile(); |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2585 BrowsingDataRemover::ClearSettingsForOneTypeWithPredicate( | 2588 BrowsingDataRemover::ClearSettingsForOneTypeWithPredicate( |
| 2586 host_content_settings_map, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 2589 host_content_settings_map, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 2587 base::Bind(&MatchPrimaryPattern, http_pattern)); | 2590 base::Bind(&MatchPrimaryPattern, http_pattern)); |
| 2588 // Verify we only have one, and it's url1. | 2591 // Verify we only have one, and it's url1. |
| 2589 host_content_settings_map->GetSettingsForOneType( | 2592 host_content_settings_map->GetSettingsForOneType( |
| 2590 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); | 2593 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); |
| 2591 EXPECT_EQ(1u, host_settings.size()); | 2594 EXPECT_EQ(1u, host_settings.size()); |
| 2592 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), | 2595 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), |
| 2593 host_settings[0].primary_pattern); | 2596 host_settings[0].primary_pattern); |
| 2594 } | 2597 } |
| 2598 |
| 2599 class MultipleTasksObserver { |
| 2600 public: |
| 2601 // A simple implementation of BrowsingDataRemover::Observer. |
| 2602 // MultipleTasksObserver will use several instances of Target to test |
| 2603 // that completion callbacks are returned to the correct one. |
| 2604 class Target : public BrowsingDataRemover::Observer { |
| 2605 public: |
| 2606 Target(MultipleTasksObserver* parent, BrowsingDataRemover* remover) |
| 2607 : parent_(parent), |
| 2608 observer_(this) { |
| 2609 observer_.Add(remover); |
| 2610 } |
| 2611 ~Target() override {} |
| 2612 |
| 2613 void OnBrowsingDataRemoverDone() override { |
| 2614 parent_->SetLastCalledTarget(this); |
| 2615 } |
| 2616 |
| 2617 private: |
| 2618 MultipleTasksObserver* parent_; |
| 2619 ScopedObserver<BrowsingDataRemover, BrowsingDataRemover::Observer> |
| 2620 observer_; |
| 2621 }; |
| 2622 |
| 2623 explicit MultipleTasksObserver(BrowsingDataRemover* remover) |
| 2624 : target_a_(this, remover), |
| 2625 target_b_(this, remover), |
| 2626 last_called_target_(nullptr) {} |
| 2627 ~MultipleTasksObserver() {} |
| 2628 |
| 2629 void ClearLastCalledTarget() { |
| 2630 last_called_target_ = nullptr; |
| 2631 } |
| 2632 |
| 2633 Target* GetLastCalledTarget() { |
| 2634 return last_called_target_; |
| 2635 } |
| 2636 |
| 2637 Target* target_a() { return &target_a_; } |
| 2638 Target* target_b() { return &target_b_; } |
| 2639 |
| 2640 private: |
| 2641 void SetLastCalledTarget(Target* target) { |
| 2642 DCHECK(!last_called_target_) |
| 2643 << "Call ClearLastCalledTarget() before every removal task."; |
| 2644 last_called_target_ = target; |
| 2645 } |
| 2646 |
| 2647 Target target_a_; |
| 2648 Target target_b_; |
| 2649 Target* last_called_target_; |
| 2650 }; |
| 2651 |
| 2652 TEST_F(BrowsingDataRemoverTest, MultipleTasks) { |
| 2653 BrowsingDataRemover* remover = |
| 2654 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); |
| 2655 EXPECT_FALSE(remover->is_removing()); |
| 2656 |
| 2657 std::unique_ptr<RegistrableDomainFilterBuilder> filter_builder_1( |
| 2658 new RegistrableDomainFilterBuilder( |
| 2659 RegistrableDomainFilterBuilder::WHITELIST)); |
| 2660 std::unique_ptr<RegistrableDomainFilterBuilder> filter_builder_2( |
| 2661 new RegistrableDomainFilterBuilder( |
| 2662 RegistrableDomainFilterBuilder::BLACKLIST)); |
| 2663 filter_builder_2->AddRegisterableDomain("example.com"); |
| 2664 |
| 2665 MultipleTasksObserver observer(remover); |
| 2666 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; |
| 2667 |
| 2668 // Test several tasks with various configuration of masks, filters, and target |
| 2669 // observers. |
| 2670 BrowsingDataRemover::RemovalTask tasks[] = { |
| 2671 BrowsingDataRemover::RemovalTask( |
| 2672 BrowsingDataRemover::Unbounded(), |
| 2673 BrowsingDataRemover::REMOVE_HISTORY, |
| 2674 BrowsingDataHelper::UNPROTECTED_WEB, |
| 2675 base::WrapUnique(new RegistrableDomainFilterBuilder( |
| 2676 RegistrableDomainFilterBuilder::BLACKLIST)), |
| 2677 observer.target_a()), |
| 2678 BrowsingDataRemover::RemovalTask( |
| 2679 BrowsingDataRemover::Unbounded(), |
| 2680 BrowsingDataRemover::REMOVE_COOKIES, |
| 2681 BrowsingDataHelper::PROTECTED_WEB, |
| 2682 base::WrapUnique(new RegistrableDomainFilterBuilder( |
| 2683 RegistrableDomainFilterBuilder::BLACKLIST)), |
| 2684 nullptr), |
| 2685 BrowsingDataRemover::RemovalTask( |
| 2686 BrowsingDataRemover::TimeRange(base::Time::Now(), base::Time::Max()), |
| 2687 BrowsingDataRemover::REMOVE_PASSWORDS, |
| 2688 BrowsingDataHelper::ALL, |
| 2689 base::WrapUnique(new RegistrableDomainFilterBuilder( |
| 2690 RegistrableDomainFilterBuilder::BLACKLIST)), |
| 2691 observer.target_b()), |
| 2692 BrowsingDataRemover::RemovalTask( |
| 2693 BrowsingDataRemover::TimeRange(base::Time(), base::Time::UnixEpoch()), |
| 2694 BrowsingDataRemover::REMOVE_PASSWORDS, |
| 2695 BrowsingDataHelper::UNPROTECTED_WEB, |
| 2696 std::move(filter_builder_1), |
| 2697 observer.target_b()), |
| 2698 BrowsingDataRemover::RemovalTask( |
| 2699 BrowsingDataRemover::TimeRange( |
| 2700 base::Time::UnixEpoch(), base::Time::Now()), |
| 2701 BrowsingDataRemover::REMOVE_CHANNEL_IDS, |
| 2702 BrowsingDataHelper::ALL, |
| 2703 std::move(filter_builder_2), |
| 2704 nullptr) |
| 2705 }; |
| 2706 |
| 2707 for (BrowsingDataRemover::RemovalTask& task : tasks) { |
| 2708 // All tasks can be directly translated to a RemoveWithFilterAndReply call. |
| 2709 // However, to additionally test that the four versions of Remove.* methods |
| 2710 // are all correctly reduced to RemoveWithFilterAndReply, we will always |
| 2711 // call the simplest one possible. |
| 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::EVERYTHING, |
| 2790 BrowsingDataRemover::REMOVE_COOKIES, |
| 2791 BrowsingDataHelper::UNPROTECTED_WEB); |
| 2792 |
| 2793 EXPECT_FALSE(remover->is_removing()); |
| 2794 } |
| OLD | NEW |