| OLD | NEW |
| 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_ | 5 #ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_ |
| 6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_ | 6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <memory> |
| 9 | |
| 10 #include <queue> | |
| 11 #include <set> | |
| 12 | |
| 13 #include "base/callback_forward.h" | 9 #include "base/callback_forward.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/synchronization/waitable_event_watcher.h" | |
| 19 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 20 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 21 #include "chrome/browser/browsing_data/browsing_data_remover_delegate.h" | 12 #include "chrome/browser/browsing_data/browsing_data_remover_delegate.h" |
| 22 #include "chrome/common/features.h" | 13 #include "chrome/common/features.h" |
| 23 #include "components/keyed_service/core/keyed_service.h" | |
| 24 #include "ppapi/features/features.h" | |
| 25 #include "storage/common/quota/quota_types.h" | |
| 26 #include "url/gurl.h" | |
| 27 | |
| 28 class BrowsingDataFilterBuilder; | |
| 29 class BrowsingDataFlashLSOHelper; | |
| 30 class BrowsingDataRemoverFactory; | |
| 31 | |
| 32 namespace content { | |
| 33 class BrowserContext; | |
| 34 class PluginDataRemover; | |
| 35 class StoragePartition; | |
| 36 } | |
| 37 | 14 |
| 38 //////////////////////////////////////////////////////////////////////////////// | 15 //////////////////////////////////////////////////////////////////////////////// |
| 39 // BrowsingDataRemover is responsible for removing data related to browsing: | 16 // BrowsingDataRemover is responsible for removing data related to browsing: |
| 40 // visits in url database, downloads, cookies ... | 17 // visits in url database, downloads, cookies ... |
| 41 // | 18 // |
| 42 // USAGE: | 19 // USAGE: |
| 43 // | 20 // |
| 44 // 0. Instantiation. | 21 // 0. Instantiation. |
| 45 // | 22 // |
| 46 // BrowsingDataRemover remover = | 23 // BrowsingDataRemover* remover = |
| 47 // BrowsingDataRemoverFactory::GetForBrowserContext(browser_context); | 24 // BrowsingDataRemoverFactory::GetForBrowserContext(browser_context); |
| 48 // | 25 // |
| 49 // 1. No observer. | 26 // 1. No observer. |
| 50 // | 27 // |
| 51 // remover->Remove(base::Time(), base::Time::Max(), REMOVE_COOKIES, ALL); | 28 // remover->Remove(base::Time(), base::Time::Max(), REMOVE_COOKIES, ALL); |
| 52 // | 29 // |
| 53 // 2. Using an observer to report when one's own removal task is finished. | 30 // 2. Using an observer to report when one's own removal task is finished. |
| 54 // | 31 // |
| 55 // class CookiesDeleter : public BrowsingDataRemover::Observer { | 32 // class CookiesDeleter : public BrowsingDataRemover::Observer { |
| 56 // CookiesDeleter() { remover->AddObserver(this); } | 33 // CookiesDeleter() { remover->AddObserver(this); } |
| 57 // ~CookiesDeleter() { remover->RemoveObserver(this); } | 34 // ~CookiesDeleter() { remover->RemoveObserver(this); } |
| 58 // | 35 // |
| 59 // void DeleteCookies() { | 36 // void DeleteCookies() { |
| 60 // remover->RemoveAndReply(base::Time(), base::Time::Max(), | 37 // remover->RemoveAndReply(base::Time(), base::Time::Max(), |
| 61 // REMOVE_COOKIES, ALL, this); | 38 // REMOVE_COOKIES, ALL, this); |
| 62 // } | 39 // } |
| 63 // | 40 // |
| 64 // void OnBrowsingDataRemoverDone() { | 41 // void OnBrowsingDataRemoverDone() { |
| 65 // LOG(INFO) << "Cookies were deleted."; | 42 // LOG(INFO) << "Cookies were deleted."; |
| 66 // } | 43 // } |
| 67 // } | 44 // } |
| 68 // | 45 // |
| 69 //////////////////////////////////////////////////////////////////////////////// | 46 //////////////////////////////////////////////////////////////////////////////// |
| 70 | 47 class BrowsingDataRemover { |
| 71 class BrowsingDataRemover : public KeyedService { | |
| 72 public: | 48 public: |
| 73 // Mask used for Remove. | 49 // Mask used for Remove. |
| 74 enum RemoveDataMask { | 50 enum RemoveDataMask { |
| 75 REMOVE_APPCACHE = 1 << 0, | 51 REMOVE_APPCACHE = 1 << 0, |
| 76 REMOVE_CACHE = 1 << 1, | 52 REMOVE_CACHE = 1 << 1, |
| 77 REMOVE_COOKIES = 1 << 2, | 53 REMOVE_COOKIES = 1 << 2, |
| 78 REMOVE_DOWNLOADS = 1 << 3, | 54 REMOVE_DOWNLOADS = 1 << 3, |
| 79 REMOVE_FILE_SYSTEMS = 1 << 4, | 55 REMOVE_FILE_SYSTEMS = 1 << 4, |
| 80 REMOVE_FORM_DATA = 1 << 5, | 56 REMOVE_FORM_DATA = 1 << 5, |
| 81 // In addition to visits, REMOVE_HISTORY removes keywords, last session and | 57 // In addition to visits, REMOVE_HISTORY removes keywords, last session and |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 class Observer { | 142 class Observer { |
| 167 public: | 143 public: |
| 168 // Called when a removal task is finished. Note that every removal task can | 144 // Called when a removal task is finished. Note that every removal task can |
| 169 // only have one observer attached to it, and only that one is called. | 145 // only have one observer attached to it, and only that one is called. |
| 170 virtual void OnBrowsingDataRemoverDone() = 0; | 146 virtual void OnBrowsingDataRemoverDone() = 0; |
| 171 | 147 |
| 172 protected: | 148 protected: |
| 173 virtual ~Observer() {} | 149 virtual ~Observer() {} |
| 174 }; | 150 }; |
| 175 | 151 |
| 176 // The completion inhibitor can artificially delay completion of the browsing | |
| 177 // data removal process. It is used during testing to simulate scenarios in | |
| 178 // which the deletion stalls or takes a very long time. | |
| 179 class CompletionInhibitor { | |
| 180 public: | |
| 181 // Invoked when a |remover| is just about to complete clearing browser data, | |
| 182 // and will be prevented from completing until after the callback | |
| 183 // |continue_to_completion| is run. | |
| 184 virtual void OnBrowsingDataRemoverWouldComplete( | |
| 185 BrowsingDataRemover* remover, | |
| 186 const base::Closure& continue_to_completion) = 0; | |
| 187 | |
| 188 protected: | |
| 189 virtual ~CompletionInhibitor() {} | |
| 190 }; | |
| 191 | |
| 192 // Used to track the deletion of a single data storage backend. | |
| 193 class SubTask { | |
| 194 public: | |
| 195 // Creates a SubTask that calls |forward_callback| when completed. | |
| 196 // |forward_callback| is only kept as a reference and must outlive SubTask. | |
| 197 explicit SubTask(const base::Closure& forward_callback); | |
| 198 ~SubTask(); | |
| 199 | |
| 200 // Indicate that the task is in progress and we're waiting. | |
| 201 void Start(); | |
| 202 | |
| 203 // Returns a callback that should be called to indicate that the task | |
| 204 // has been finished. | |
| 205 base::Closure GetCompletionCallback(); | |
| 206 | |
| 207 // Whether the task is still in progress. | |
| 208 bool is_pending() const { return is_pending_; } | |
| 209 | |
| 210 private: | |
| 211 void CompletionCallback(); | |
| 212 | |
| 213 bool is_pending_; | |
| 214 const base::Closure& forward_callback_; | |
| 215 base::WeakPtrFactory<SubTask> weak_ptr_factory_; | |
| 216 }; | |
| 217 | |
| 218 // Is the BrowsingDataRemover currently in the process of removing data? | |
| 219 bool is_removing() { return is_removing_; } | |
| 220 | |
| 221 // Sets a CompletionInhibitor, which will be notified each time an instance is | |
| 222 // about to complete a browsing data removal process, and will be able to | |
| 223 // artificially delay the completion. | |
| 224 // TODO(crbug.com/483528): Make this non-static. | |
| 225 static void set_completion_inhibitor_for_testing( | |
| 226 CompletionInhibitor* inhibitor) { | |
| 227 completion_inhibitor_ = inhibitor; | |
| 228 } | |
| 229 | |
| 230 // Called by the embedder to provide the delegate that will take care of | 152 // Called by the embedder to provide the delegate that will take care of |
| 231 // deleting embedder-specific data. | 153 // deleting embedder-specific data. |
| 232 void set_embedder_delegate( | 154 virtual void SetEmbedderDelegate( |
| 233 std::unique_ptr<BrowsingDataRemoverDelegate> embedder_delegate) { | 155 std::unique_ptr<BrowsingDataRemoverDelegate> embedder_delegate) = 0; |
| 234 embedder_delegate_ = std::move(embedder_delegate); | 156 virtual BrowsingDataRemoverDelegate* GetEmbedderDelegate() const = 0; |
| 235 } | |
| 236 | |
| 237 BrowsingDataRemoverDelegate* get_embedder_delegate() const { | |
| 238 return embedder_delegate_.get(); | |
| 239 } | |
| 240 | 157 |
| 241 // Removes browsing data within the given |time_range|, with datatypes being | 158 // Removes browsing data within the given |time_range|, with datatypes being |
| 242 // specified by |remove_mask| and origin types by |origin_type_mask|. | 159 // specified by |remove_mask| and origin types by |origin_type_mask|. |
| 243 void Remove(const base::Time& delete_begin, | 160 virtual void Remove(const base::Time& delete_begin, |
| 244 const base::Time& delete_end, | 161 const base::Time& delete_end, |
| 245 int remove_mask, | 162 int remove_mask, |
| 246 int origin_type_mask); | 163 int origin_type_mask) = 0; |
| 247 | 164 |
| 248 // A version of the above that in addition informs the |observer| when the | 165 // A version of the above that in addition informs the |observer| when the |
| 249 // removal task is finished. | 166 // removal task is finished. |
| 250 void RemoveAndReply(const base::Time& delete_begin, | 167 virtual void RemoveAndReply(const base::Time& delete_begin, |
| 251 const base::Time& delete_end, | 168 const base::Time& delete_end, |
| 252 int remove_mask, | 169 int remove_mask, |
| 253 int origin_type_mask, | 170 int origin_type_mask, |
| 254 Observer* observer); | 171 Observer* observer) = 0; |
| 255 | 172 |
| 256 // Like Remove(), but in case of URL-keyed only removes data whose URL match | 173 // Like Remove(), but in case of URL-keyed only removes data whose URL match |
| 257 // |filter_builder| (e.g. are on certain origin or domain). | 174 // |filter_builder| (e.g. are on certain origin or domain). |
| 258 // RemoveWithFilter() currently only works with FILTERABLE_DATATYPES. | 175 // RemoveWithFilter() currently only works with FILTERABLE_DATATYPES. |
| 259 void RemoveWithFilter( | 176 virtual void RemoveWithFilter( |
| 260 const base::Time& delete_begin, | 177 const base::Time& delete_begin, |
| 261 const base::Time& delete_end, | 178 const base::Time& delete_end, |
| 262 int remove_mask, | 179 int remove_mask, |
| 263 int origin_type_mask, | 180 int origin_type_mask, |
| 264 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder); | 181 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder) = 0; |
| 265 | 182 |
| 266 // A version of the above that in addition informs the |observer| when the | 183 // A version of the above that in addition informs the |observer| when the |
| 267 // removal task is finished. | 184 // removal task is finished. |
| 268 void RemoveWithFilterAndReply( | 185 virtual void RemoveWithFilterAndReply( |
| 269 const base::Time& delete_begin, | 186 const base::Time& delete_begin, |
| 270 const base::Time& delete_end, | 187 const base::Time& delete_end, |
| 271 int remove_mask, | 188 int remove_mask, |
| 272 int origin_type_mask, | 189 int origin_type_mask, |
| 273 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder, | 190 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder, |
| 274 Observer* observer); | 191 Observer* observer) = 0; |
| 275 | 192 |
| 276 void AddObserver(Observer* observer); | 193 // Observers. |
| 277 void RemoveObserver(Observer* observer); | 194 virtual void AddObserver(Observer* observer) = 0; |
| 278 | 195 virtual void RemoveObserver(Observer* observer) = 0; |
| 279 // Used for testing. | |
| 280 void OverrideStoragePartitionForTesting( | |
| 281 content::StoragePartition* storage_partition); | |
| 282 | |
| 283 #if BUILDFLAG(ENABLE_PLUGINS) | |
| 284 void OverrideFlashLSOHelperForTesting( | |
| 285 scoped_refptr<BrowsingDataFlashLSOHelper> flash_lso_helper); | |
| 286 #endif | |
| 287 | 196 |
| 288 // Parameters of the last call are exposed to be used by tests. Removal and | 197 // Parameters of the last call are exposed to be used by tests. Removal and |
| 289 // origin type masks equal to -1 mean that no removal has ever been executed. | 198 // origin type masks equal to -1 mean that no removal has ever been executed. |
| 290 // TODO(msramek): If other consumers than tests are interested in this, | 199 // TODO(msramek): If other consumers than tests are interested in this, |
| 291 // consider returning them in OnBrowsingDataRemoverDone() callback. | 200 // consider returning them in OnBrowsingDataRemoverDone() callback. If not, |
| 292 const base::Time& GetLastUsedBeginTime(); | 201 // consider simplifying this interface by removing these methods and changing |
| 293 const base::Time& GetLastUsedEndTime(); | 202 // the tests to record the parameters using GMock instead. |
| 294 int GetLastUsedRemovalMask(); | 203 virtual const base::Time& GetLastUsedBeginTime() = 0; |
| 295 int GetLastUsedOriginTypeMask(); | 204 virtual const base::Time& GetLastUsedEndTime() = 0; |
| 296 | 205 virtual int GetLastUsedRemovalMask() = 0; |
| 297 protected: | 206 virtual int GetLastUsedOriginTypeMask() = 0; |
| 298 // Use BrowsingDataRemoverFactory::GetForBrowserContext to get an instance of | |
| 299 // this class. The constructor is protected so that the class is mockable. | |
| 300 BrowsingDataRemover(content::BrowserContext* browser_context); | |
| 301 ~BrowsingDataRemover() override; | |
| 302 | |
| 303 // A common reduction of all public Remove[WithFilter][AndReply] methods. | |
| 304 virtual void RemoveInternal( | |
| 305 const base::Time& delete_begin, | |
| 306 const base::Time& delete_end, | |
| 307 int remove_mask, | |
| 308 int origin_type_mask, | |
| 309 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder, | |
| 310 Observer* observer); | |
| 311 | |
| 312 private: | |
| 313 // Testing the private RemovalTask. | |
| 314 FRIEND_TEST_ALL_PREFIXES(BrowsingDataRemoverTest, MultipleTasks); | |
| 315 | |
| 316 // The BrowsingDataRemover tests need to be able to access the implementation | |
| 317 // of Remove(), as it exposes details that aren't yet available in the public | |
| 318 // API. As soon as those details are exposed via new methods, this should be | |
| 319 // removed. | |
| 320 // | |
| 321 // TODO(mkwst): See http://crbug.com/113621 | |
| 322 friend class BrowsingDataRemoverTest; | |
| 323 | |
| 324 friend class BrowsingDataRemoverFactory; | |
| 325 | |
| 326 // Represents a single removal task. Contains all parameters needed to execute | |
| 327 // it and a pointer to the observer that added it. | |
| 328 struct RemovalTask { | |
| 329 RemovalTask(const base::Time& delete_begin, | |
| 330 const base::Time& delete_end, | |
| 331 int remove_mask, | |
| 332 int origin_type_mask, | |
| 333 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder, | |
| 334 Observer* observer); | |
| 335 ~RemovalTask(); | |
| 336 | |
| 337 base::Time delete_begin; | |
| 338 base::Time delete_end; | |
| 339 int remove_mask; | |
| 340 int origin_type_mask; | |
| 341 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder; | |
| 342 Observer* observer; | |
| 343 }; | |
| 344 | |
| 345 void Shutdown() override; | |
| 346 | |
| 347 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're | |
| 348 // not already removing, and vice-versa. | |
| 349 void SetRemoving(bool is_removing); | |
| 350 | |
| 351 #if BUILDFLAG(ENABLE_PLUGINS) | |
| 352 // Called when plugin data has been cleared. Invokes NotifyIfDone. | |
| 353 void OnWaitableEventSignaled(base::WaitableEvent* waitable_event); | |
| 354 | |
| 355 // Called when the list of |sites| storing Flash LSO cookies is fetched. | |
| 356 void OnSitesWithFlashDataFetched( | |
| 357 base::Callback<bool(const std::string&)> plugin_filter, | |
| 358 const std::vector<std::string>& sites); | |
| 359 | |
| 360 // Indicates that LSO cookies for one website have been deleted. | |
| 361 void OnFlashDataDeleted(); | |
| 362 #endif | |
| 363 | |
| 364 // Executes the next removal task. Called after the previous task was finished | |
| 365 // or directly from Remove() if the task queue was empty. | |
| 366 void RunNextTask(); | |
| 367 | |
| 368 // Removes the specified items related to browsing for a specific host. If the | |
| 369 // provided |remove_url| is empty, data is removed for all origins; otherwise, | |
| 370 // it is restricted by the origin filter origin (where implemented yet). The | |
| 371 // |origin_type_mask| parameter defines the set of origins from which data | |
| 372 // should be removed (protected, unprotected, or both). | |
| 373 // TODO(ttr314): Remove "(where implemented yet)" constraint above once | |
| 374 // crbug.com/113621 is done. | |
| 375 // TODO(crbug.com/589586): Support all backends w/ origin filter. | |
| 376 void RemoveImpl(const base::Time& delete_begin, | |
| 377 const base::Time& delete_end, | |
| 378 int remove_mask, | |
| 379 const BrowsingDataFilterBuilder& filter_builder, | |
| 380 int origin_type_mask); | |
| 381 | |
| 382 // Notifies observers and transitions to the idle state. | |
| 383 void Notify(); | |
| 384 | |
| 385 // Checks if we are all done, and if so, calls Notify(). | |
| 386 void NotifyIfDone(); | |
| 387 | |
| 388 // Returns true if we're all done. | |
| 389 bool AllDone(); | |
| 390 | |
| 391 // The browser context we're to remove from. | |
| 392 content::BrowserContext* browser_context_; | |
| 393 | |
| 394 // A delegate to delete the embedder-specific data. | |
| 395 std::unique_ptr<BrowsingDataRemoverDelegate> embedder_delegate_; | |
| 396 | |
| 397 // Start time to delete from. | |
| 398 base::Time delete_begin_; | |
| 399 | |
| 400 // End time to delete to. | |
| 401 base::Time delete_end_; | |
| 402 | |
| 403 // The removal mask for the current removal operation. | |
| 404 int remove_mask_ = 0; | |
| 405 | |
| 406 // From which types of origins should we remove data? | |
| 407 int origin_type_mask_ = 0; | |
| 408 | |
| 409 // True if Remove has been invoked. | |
| 410 bool is_removing_; | |
| 411 | |
| 412 // Removal tasks to be processed. | |
| 413 std::queue<RemovalTask> task_queue_; | |
| 414 | |
| 415 // If non-NULL, the |completion_inhibitor_| is notified each time an instance | |
| 416 // is about to complete a browsing data removal process, and has the ability | |
| 417 // to artificially delay completion. Used for testing. | |
| 418 static CompletionInhibitor* completion_inhibitor_; | |
| 419 | |
| 420 #if BUILDFLAG(ENABLE_PLUGINS) | |
| 421 // Used to delete plugin data. | |
| 422 std::unique_ptr<content::PluginDataRemover> plugin_data_remover_; | |
| 423 base::WaitableEventWatcher watcher_; | |
| 424 | |
| 425 // Used for per-site plugin data deletion. | |
| 426 scoped_refptr<BrowsingDataFlashLSOHelper> flash_lso_helper_; | |
| 427 #endif | |
| 428 | |
| 429 // A callback to NotifyIfDone() used by SubTasks instances. | |
| 430 const base::Closure sub_task_forward_callback_; | |
| 431 | |
| 432 // Keeping track of various subtasks to be completed. | |
| 433 // These may only be accessed from UI thread in order to avoid races! | |
| 434 SubTask synchronous_clear_operations_; | |
| 435 SubTask clear_embedder_data_; | |
| 436 SubTask clear_cache_; | |
| 437 SubTask clear_channel_ids_; | |
| 438 SubTask clear_http_auth_cache_; | |
| 439 SubTask clear_storage_partition_data_; | |
| 440 // Counts the number of plugin data tasks. Should be the number of LSO cookies | |
| 441 // to be deleted, or 1 while we're fetching LSO cookies or deleting in bulk. | |
| 442 int clear_plugin_data_count_ = 0; | |
| 443 | |
| 444 // Observers of the global state and individual tasks. | |
| 445 base::ObserverList<Observer, true> observer_list_; | |
| 446 | |
| 447 // We do not own this. | |
| 448 content::StoragePartition* storage_partition_for_testing_ = nullptr; | |
| 449 | |
| 450 base::WeakPtrFactory<BrowsingDataRemover> weak_ptr_factory_; | |
| 451 | |
| 452 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover); | |
| 453 }; | 207 }; |
| 454 | 208 |
| 455 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_ | 209 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_ |
| OLD | NEW |