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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_IMPL_H_ 6 #define CONTENT_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_IMPL_H_ 7 8 #include <stdint.h> 9 10 #include <set> 11 12 #include "base/cancelable_callback.h" 13 #include "base/containers/queue.h" 14 #include "base/gtest_prod_util.h" 15 #include "base/macros.h" 16 #include "base/memory/weak_ptr.h" 17 #include "base/observer_list.h" 18 #include "base/supports_user_data.h" 19 #include "base/synchronization/waitable_event_watcher.h" 20 #include "base/time/time.h" 21 #include "build/build_config.h" 22 #include "content/common/content_export.h" 23 #include "content/public/browser/browsing_data_remover.h" 24 #include "url/origin.h" 25 26 namespace content { 27 28 class BrowserContext; 29 class BrowsingDataFilterBuilder; 30 class StoragePartition; 31 32 class CONTENT_EXPORT BrowsingDataRemoverImpl 33 : public BrowsingDataRemover, 34 public base::SupportsUserData::Data { 35 public: 36 explicit BrowsingDataRemoverImpl(BrowserContext* browser_context); 37 ~BrowsingDataRemoverImpl() override; 38 39 // Is the BrowsingDataRemoverImpl currently in the process of removing data? IsRemovingForTesting()40 bool IsRemovingForTesting() { return is_removing_; } 41 42 // BrowsingDataRemover implementation: 43 void SetEmbedderDelegate( 44 BrowsingDataRemoverDelegate* embedder_delegate) override; 45 bool DoesOriginMatchMaskForTesting( 46 int origin_type_mask, 47 const url::Origin& origin, 48 storage::SpecialStoragePolicy* special_storage_policy) override; 49 void Remove(const base::Time& delete_begin, 50 const base::Time& delete_end, 51 int remove_mask, 52 int origin_type_mask) override; 53 void RemoveAndReply(const base::Time& delete_begin, 54 const base::Time& delete_end, 55 int remove_mask, 56 int origin_type_mask, 57 Observer* observer) override; 58 void RemoveWithFilterAndReply( 59 const base::Time& delete_begin, 60 const base::Time& delete_end, 61 int remove_mask, 62 int origin_type_mask, 63 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder, 64 Observer* observer) override; 65 66 void AddObserver(Observer* observer) override; 67 void RemoveObserver(Observer* observer) override; 68 69 void SetWouldCompleteCallbackForTesting( 70 const base::RepeatingCallback< 71 void(base::OnceClosure continue_to_completion)>& callback) override; 72 73 const base::Time& GetLastUsedBeginTimeForTesting() override; 74 int GetLastUsedRemovalMaskForTesting() override; 75 int GetLastUsedOriginTypeMaskForTesting() override; 76 77 // Used for testing. 78 void OverrideStoragePartitionForTesting(StoragePartition* storage_partition); 79 80 protected: 81 // A common reduction of all public Remove[WithFilter][AndReply] methods. 82 virtual void RemoveInternal( 83 const base::Time& delete_begin, 84 const base::Time& delete_end, 85 int remove_mask, 86 int origin_type_mask, 87 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder, 88 Observer* observer); 89 90 private: 91 // Testing the private RemovalTask. 92 FRIEND_TEST_ALL_PREFIXES(BrowsingDataRemoverImplTest, MultipleTasks); 93 FRIEND_TEST_ALL_PREFIXES(BrowsingDataRemoverImplTest, MultipleIdenticalTasks); 94 95 // For debugging purposes. Please add new deletion tasks at the end. 96 // This enum is recorded in a histogram, so don't change or reuse ids. 97 // Entries must also be added to BrowsingDataRemoverTasks in enums.xml. 98 enum class TracingDataType { 99 kSynchronous = 1, 100 kEmbedderData = 2, 101 kStoragePartition = 3, 102 kHttpCache = 4, 103 kHttpAndMediaCaches = 5, 104 kReportingCache = 6, 105 kChannelIds = 7, 106 kNetworkHistory = 8, 107 kAuthCache = 9, 108 kCodeCaches = 10, 109 kNetworkErrorLogging = 11, 110 kMaxValue = kNetworkErrorLogging, 111 }; 112 113 // Represents a single removal task. Contains all parameters needed to execute 114 // it and a pointer to the observer that added it. CONTENT_EXPORTed to be 115 // visible in tests. 116 struct CONTENT_EXPORT RemovalTask { 117 RemovalTask(const base::Time& delete_begin, 118 const base::Time& delete_end, 119 int remove_mask, 120 int origin_type_mask, 121 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder, 122 Observer* observer); 123 RemovalTask(RemovalTask&& other) noexcept; 124 ~RemovalTask(); 125 126 // Returns true if the deletion parameters are equal. 127 // Does not compare |observer| and |task_started|. 128 bool IsSameDeletion(const RemovalTask& other); 129 130 base::Time delete_begin; 131 base::Time delete_end; 132 int remove_mask; 133 int origin_type_mask; 134 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder; 135 std::vector<Observer*> observers; 136 base::Time task_started; 137 }; 138 139 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're 140 // not already removing, and vice-versa. 141 void SetRemoving(bool is_removing); 142 143 // Executes the next removal task. Called after the previous task was finished 144 // or directly from Remove() if the task queue was empty. 145 void RunNextTask(); 146 147 // Removes the specified items related to browsing for a specific host. If the 148 // provided |remove_url| is empty, data is removed for all origins; otherwise, 149 // it is restricted by the origin filter origin (where implemented yet). The 150 // |origin_type_mask| parameter defines the set of origins from which data 151 // should be removed (protected, unprotected, or both). 152 // TODO(ttr314): Remove "(where implemented yet)" constraint above once 153 // crbug.com/113621 is done. 154 // TODO(crbug.com/589586): Support all backends w/ origin filter. 155 void RemoveImpl(const base::Time& delete_begin, 156 const base::Time& delete_end, 157 int remove_mask, 158 BrowsingDataFilterBuilder* filter_builder, 159 int origin_type_mask); 160 161 // Notifies observers and transitions to the idle state. 162 void Notify(); 163 164 // Called by the closures returned by CreateTaskCompletionClosure(). 165 // Checks if all tasks have completed, and if so, calls Notify(). 166 void OnTaskComplete(TracingDataType data_type); 167 168 // Increments the number of pending tasks by one, and returns a OnceClosure 169 // that calls OnTaskComplete(). The Remover is complete once all the closures 170 // created by this method have been invoked. 171 base::OnceClosure CreateTaskCompletionClosure(TracingDataType data_type); 172 173 // Same as CreateTaskCompletionClosure() but guarantees that 174 // OnTaskComplete() is called if the task is dropped. That can typically 175 // happen when the connection is closed while an interface call is made. 176 base::OnceClosure CreateTaskCompletionClosureForMojo( 177 TracingDataType data_type); 178 179 // Records unfinished tasks from |pending_sub_tasks_| after a delay. 180 void RecordUnfinishedSubTasks(); 181 182 // Like GetWeakPtr(), but returns a weak pointer to BrowsingDataRemoverImpl 183 // for internal purposes. 184 base::WeakPtr<BrowsingDataRemoverImpl> GetWeakPtr(); 185 186 // The browser context we're to remove from. 187 BrowserContext* browser_context_; 188 189 // A delegate to delete the embedder-specific data. Owned by the embedder. 190 BrowsingDataRemoverDelegate* embedder_delegate_; 191 192 // Start time to delete from. 193 base::Time delete_begin_; 194 195 // End time to delete to. 196 base::Time delete_end_; 197 198 // The removal mask for the current removal operation. 199 int remove_mask_ = 0; 200 201 // From which types of origins should we remove data? 202 int origin_type_mask_ = 0; 203 204 // True if Remove has been invoked. 205 bool is_removing_; 206 207 // Removal tasks to be processed. 208 std::deque<RemovalTask> task_queue_; 209 210 // If non-null, the |would_complete_callback_| is called each time an instance 211 // is about to complete a browsing data removal process, and has the ability 212 // to artificially delay completion. Used for testing. 213 base::RepeatingCallback<void(base::OnceClosure continue_to_completion)> 214 would_complete_callback_; 215 216 // Records which tasks of a deletion are currently active. 217 std::set<TracingDataType> pending_sub_tasks_; 218 219 // Fires after some time to track slow tasks. Cancelled when all tasks 220 // are finished. 221 base::CancelableClosure slow_pending_tasks_closure_; 222 223 // Observers of the global state and individual tasks. 224 base::ObserverList<Observer, true>::Unchecked observer_list_; 225 226 // We do not own this. 227 StoragePartition* storage_partition_for_testing_; 228 229 base::WeakPtrFactory<BrowsingDataRemoverImpl> weak_ptr_factory_{this}; 230 231 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverImpl); 232 }; 233 234 } // namespace content 235 236 #endif // CONTENT_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_IMPL_H_ 237