Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: chrome/browser/browsing_data/browsing_data_remover.h

Issue 2175703002: Implement a task scheduler for BrowsingDataRemover (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bdr-race-condition
Patch Set: Formatting. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #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 <stdint.h>
9 9
10 #include <queue>
10 #include <set> 11 #include <set>
11 12
12 #include "base/callback_forward.h" 13 #include "base/callback_forward.h"
13 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
14 #include "base/macros.h" 15 #include "base/macros.h"
15 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h" 17 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h" 18 #include "base/observer_list.h"
18 #include "base/sequenced_task_runner_helpers.h" 19 #include "base/sequenced_task_runner_helpers.h"
19 #include "base/synchronization/waitable_event_watcher.h" 20 #include "base/synchronization/waitable_event_watcher.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 } 57 }
57 58
58 namespace net { 59 namespace net {
59 class URLRequestContextGetter; 60 class URLRequestContextGetter;
60 } 61 }
61 62
62 #if BUILDFLAG(ANDROID_JAVA_UI) 63 #if BUILDFLAG(ANDROID_JAVA_UI)
63 class WebappRegistry; 64 class WebappRegistry;
64 #endif 65 #endif
65 66
67 ////////////////////////////////////////////////////////////////////////////////
66 // BrowsingDataRemover is responsible for removing data related to browsing: 68 // BrowsingDataRemover is responsible for removing data related to browsing:
67 // visits in url database, downloads, cookies ... 69 // visits in url database, downloads, cookies ...
70 //
71 // USAGE:
72 //
73 // 0. Instantiation.
74 //
75 // BrowsingDataRemover remover =
76 // BrowsingDataRemoverFactory::GetForBrowserContext(profile);
77 //
78 // 1. No observer.
79 //
80 // remover->Remove(Unbounded(), REMOVE_COOKIES, ALL);
81 //
82 // 2. Using an observer to report when one's own removal task is finished.
83 //
84 // class CookiesDeleter : public BrowsingDataRemover::Observer {
85 // CookiesDeleter() { remover->AddObserver(this); }
86 // ~CookiesDeleter() { remover->RemoveObserver(this); }
87 //
88 // void DeleteCookies() {
89 // remover->RemoveAndReply(Unbounded(), REMOVE_COOKIES, ALL, this);
90 // }
91 //
92 // void OnBrowsingDataRemoverDone() {
93 // LOG(INFO) << "Cookies were deleted.";
94 // }
95 // }
96 //
97 // 3. Using an observer to report when any removal task started or all removal
98 // tasks finished (i.e. when BrowsingDataRemover changes state from idle
99 // to busy and vice versa).
100 //
101 // class BrowsingDataRemoverStatusObsever {
102 // BrowsingDataRemoverStatusObserver() { remover->AddObserver(this); }
103 // ~BrowsingDataRemoverStatusObserver() {
104 // remover->RemoveObserver(this);
105 // }
106 //
107 // void OnBrowsingDataRemoving(bool removing) {
108 // LOG(INFO) << "Remover is now " << (removing ? "busy" : "idle");
109 // }
110 // }
111 //
112 ////////////////////////////////////////////////////////////////////////////////
113
68 class BrowsingDataRemover : public KeyedService 114 class BrowsingDataRemover : public KeyedService
69 #if defined(ENABLE_PLUGINS) 115 #if defined(ENABLE_PLUGINS)
70 , public PepperFlashSettingsManager::Client 116 , public PepperFlashSettingsManager::Client
71 #endif 117 #endif
72 { 118 {
73 public: 119 public:
74 // Mask used for Remove. 120 // Mask used for Remove.
75 enum RemoveDataMask { 121 enum RemoveDataMask {
76 REMOVE_APPCACHE = 1 << 0, 122 REMOVE_APPCACHE = 1 << 0,
77 REMOVE_CACHE = 1 << 1, 123 REMOVE_CACHE = 1 << 1,
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 192
147 struct TimeRange { 193 struct TimeRange {
148 TimeRange(base::Time begin, base::Time end) : begin(begin), end(end) {} 194 TimeRange(base::Time begin, base::Time end) : begin(begin), end(end) {}
149 bool operator==(const TimeRange& other) const; 195 bool operator==(const TimeRange& other) const;
150 196
151 base::Time begin; 197 base::Time begin;
152 base::Time end; 198 base::Time end;
153 }; 199 };
154 200
155 // Observer is notified when the removal is active and when it's done. 201 // Observer is notified when the removal is active and when it's done.
202 // TODO(msramek): The semantics of the two methods are slightly different;
203 // one is called for every observer at the same time, while the other only
204 // for one observer at a time. Split the interface or deprecate
205 // OnBrowsingDataRemoving().
156 class Observer { 206 class Observer {
157 public: 207 public:
158 // NOTE: DEPRECATED; talk to dbeam/msramek before using this. 208 // NOTE: DEPRECATED; talk to dbeam/msramek before using this.
159 // 209 //
160 // Whether removal is active. Note that not having an active removal is not 210 // Whether removal is active. Note that not having an active removal is not
161 // same as completing a removal. That is why the removing status is separate 211 // same as completing a removal. That is why the removing status is separate
162 // from the done message. 212 // from the done message.
163 virtual void OnBrowsingDataRemoving(bool is_removing) {} 213 virtual void OnBrowsingDataRemoving(bool is_removing) {}
164 214
165 // Done means keywords have been deleted, cache cleared and all other 215 // Called when a removal task is finished. Note that every removal task can
166 // removal tasks are scheduled. 216 // only have one observer attached to it, and only that one is called.
167 virtual void OnBrowsingDataRemoverDone() {} 217 virtual void OnBrowsingDataRemoverDone() {}
168 218
169 protected: 219 protected:
170 virtual ~Observer() {} 220 virtual ~Observer() {}
171 }; 221 };
172 222
173 // The completion inhibitor can artificially delay completion of the browsing 223 // The completion inhibitor can artificially delay completion of the browsing
174 // data removal process. It is used during testing to simulate scenarios in 224 // data removal process. It is used during testing to simulate scenarios in
175 // which the deletion stalls or takes a very long time. 225 // which the deletion stalls or takes a very long time.
176 class CompletionInhibitor { 226 class CompletionInhibitor {
(...skipping 18 matching lines...) Expand all
195 245
196 // Sets a CompletionInhibitor, which will be notified each time an instance is 246 // Sets a CompletionInhibitor, which will be notified each time an instance is
197 // about to complete a browsing data removal process, and will be able to 247 // about to complete a browsing data removal process, and will be able to
198 // artificially delay the completion. 248 // artificially delay the completion.
199 // TODO(crbug.com/483528): Make this non-static. 249 // TODO(crbug.com/483528): Make this non-static.
200 static void set_completion_inhibitor_for_testing( 250 static void set_completion_inhibitor_for_testing(
201 CompletionInhibitor* inhibitor) { 251 CompletionInhibitor* inhibitor) {
202 completion_inhibitor_ = inhibitor; 252 completion_inhibitor_ = inhibitor;
203 } 253 }
204 254
205 // Removes the specified items related to browsing for all origins that match 255 // Removes browsing data within the given |time_range|, with datatypes being
206 // the provided |origin_type_mask| (see BrowsingDataHelper::OriginTypeMask). 256 // specified by |remove_mask| and origin types by |origin_type_mask|.
207 void Remove(const TimeRange& time_range, 257 void Remove(const TimeRange& time_range,
208 int remove_mask, 258 int remove_mask,
209 int origin_type_mask); 259 int origin_type_mask);
210 260
211 // Removes the specified items related to browsing for all origins that match 261 // A version of the above that in addition informs the |observer| when the
212 // the provided |origin_type_mask| (see BrowsingDataHelper::OriginTypeMask). 262 // removal task is finished.
213 // The |origin_filter| is used as a final filter for clearing operations. 263 void RemoveAndReply(const TimeRange& time_range,
264 int remove_mask,
265 int origin_type_mask,
266 Observer* observer);
267
268 // Like Remove(), but in case of URL-keyed only removes data whose URL match
269 // |filter_builder| (e.g. are on certain origin or domain).
214 // TODO(dmurph): Support all backends with filter (crbug.com/113621). 270 // TODO(dmurph): Support all backends with filter (crbug.com/113621).
215 // DO NOT USE THIS METHOD UNLESS CALLER KNOWS WHAT THEY'RE DOING. NOT ALL 271 // DO NOT USE THIS METHOD UNLESS CALLER KNOWS WHAT THEY'RE DOING. NOT ALL
216 // BACKENDS ARE SUPPORTED YET, AND MORE DATA THAN EXPECTED COULD BE DELETED. 272 // BACKENDS ARE SUPPORTED YET, AND MORE DATA THAN EXPECTED COULD BE DELETED.
217 virtual void RemoveWithFilter(const TimeRange& time_range, 273 void RemoveWithFilter(
218 int remove_mask, 274 const TimeRange& time_range,
219 int origin_type_mask, 275 int remove_mask,
220 const BrowsingDataFilterBuilder& origin_filter); 276 int origin_type_mask,
277 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder);
278
279 // A version of the above that in addition informs the |observer| when the
280 // removal task is finished.
281 void RemoveWithFilterAndReply(
282 const TimeRange& time_range,
283 int remove_mask,
284 int origin_type_mask,
285 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder,
286 Observer* observer);
221 287
222 void AddObserver(Observer* observer); 288 void AddObserver(Observer* observer);
223 void RemoveObserver(Observer* observer); 289 void RemoveObserver(Observer* observer);
224 290
225 // Used for testing. 291 // Used for testing.
226 void OverrideStoragePartitionForTesting( 292 void OverrideStoragePartitionForTesting(
227 content::StoragePartition* storage_partition); 293 content::StoragePartition* storage_partition);
228 294
229 #if BUILDFLAG(ANDROID_JAVA_UI) 295 #if BUILDFLAG(ANDROID_JAVA_UI)
230 void OverrideWebappRegistryForTesting( 296 void OverrideWebappRegistryForTesting(
231 std::unique_ptr<WebappRegistry> webapp_registry); 297 std::unique_ptr<WebappRegistry> webapp_registry);
232 #endif 298 #endif
233 299
234 // Parameters of the last call are exposed to be used by tests. Removal and 300 // Parameters of the last call are exposed to be used by tests. Removal and
235 // origin type masks equal to -1 mean that no removal has ever been executed. 301 // origin type masks equal to -1 mean that no removal has ever been executed.
236 // TODO(msramek): If other consumers than tests are interested in this, 302 // TODO(msramek): If other consumers than tests are interested in this,
237 // consider returning them in OnBrowsingDataRemoverDone() callback. 303 // consider returning them in OnBrowsingDataRemoverDone() callback.
238 const base::Time& GetLastUsedBeginTime(); 304 const base::Time& GetLastUsedBeginTime();
239 const base::Time& GetLastUsedEndTime(); 305 const base::Time& GetLastUsedEndTime();
240 int GetLastUsedRemovalMask(); 306 int GetLastUsedRemovalMask();
241 int GetLastUsedOriginTypeMask(); 307 int GetLastUsedOriginTypeMask();
242 308
243 protected: 309 protected:
244 // Use BrowsingDataRemoverFactory::GetForBrowserContext to get an instance of 310 // Use BrowsingDataRemoverFactory::GetForBrowserContext to get an instance of
245 // this class. The constructor is protected so that the class is mockable. 311 // this class. The constructor is protected so that the class is mockable.
246 BrowsingDataRemover(content::BrowserContext* browser_context); 312 BrowsingDataRemover(content::BrowserContext* browser_context);
247 ~BrowsingDataRemover() override; 313 ~BrowsingDataRemover() override;
248 314
315 // A common reduction of all public Remove[WithFilter][AndReply] methods.
316 virtual void RemoveInternal(
317 const TimeRange& time_range,
318 int remove_mask,
319 int origin_type_mask,
320 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder,
321 Observer* observer);
322
249 private: 323 private:
250 // The clear API needs to be able to toggle removing_ in order to test that 324 // The clear API needs to be able to toggle removing_ in order to test that
251 // only one BrowsingDataRemover instance can be called at a time. 325 // only one BrowsingDataRemover instance can be called at a time.
252 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest, OneAtATime); 326 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest, OneAtATime);
253 // Testing our static method, ClearSettingsForOneTypeWithPredicate. 327 // Testing our static method, ClearSettingsForOneTypeWithPredicate.
254 FRIEND_TEST_ALL_PREFIXES(BrowsingDataRemoverTest, ClearWithPredicate); 328 FRIEND_TEST_ALL_PREFIXES(BrowsingDataRemoverTest, ClearWithPredicate);
329 // Testing the private RemovalTask.
330 FRIEND_TEST_ALL_PREFIXES(BrowsingDataRemoverTest, MultipleTasks);
255 331
256 // The BrowsingDataRemover tests need to be able to access the implementation 332 // The BrowsingDataRemover tests need to be able to access the implementation
257 // of Remove(), as it exposes details that aren't yet available in the public 333 // of Remove(), as it exposes details that aren't yet available in the public
258 // API. As soon as those details are exposed via new methods, this should be 334 // API. As soon as those details are exposed via new methods, this should be
259 // removed. 335 // removed.
260 // 336 //
261 // TODO(mkwst): See http://crbug.com/113621 337 // TODO(mkwst): See http://crbug.com/113621
262 friend class BrowsingDataRemoverTest; 338 friend class BrowsingDataRemoverTest;
263 339
264 friend class BrowsingDataRemoverFactory; 340 friend class BrowsingDataRemoverFactory;
265 341
342 // Represents a single removal task. Contains all parameters needed to execute
343 // it and a pointer to the observer that added it.
344 struct RemovalTask {
345 RemovalTask(const TimeRange& time_range,
346 int remove_mask,
347 int origin_type_mask,
348 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder,
349 Observer* observer);
350 ~RemovalTask();
351
352 TimeRange time_range;
353 int remove_mask;
354 int origin_type_mask;
355 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder;
356 Observer* observer;
357 };
358
266 // Clears all host-specific settings for one content type that satisfy the 359 // Clears all host-specific settings for one content type that satisfy the
267 // given predicate. 360 // given predicate.
268 // 361 //
269 // This should only be called on the UI thread. 362 // This should only be called on the UI thread.
270 static void ClearSettingsForOneTypeWithPredicate( 363 static void ClearSettingsForOneTypeWithPredicate(
271 HostContentSettingsMap* content_settings_map, 364 HostContentSettingsMap* content_settings_map,
272 ContentSettingsType content_type, 365 ContentSettingsType content_type,
273 const base::Callback< 366 const base::Callback<
274 bool(const ContentSettingsPattern& primary_pattern, 367 bool(const ContentSettingsPattern& primary_pattern,
275 const ContentSettingsPattern& secondary_pattern)>& predicate); 368 const ContentSettingsPattern& secondary_pattern)>& predicate);
(...skipping 15 matching lines...) Expand all
291 // PepperFlashSettingsManager::Client implementation. 384 // PepperFlashSettingsManager::Client implementation.
292 void OnDeauthorizeFlashContentLicensesCompleted(uint32_t request_id, 385 void OnDeauthorizeFlashContentLicensesCompleted(uint32_t request_id,
293 bool success) override; 386 bool success) override;
294 #endif 387 #endif
295 388
296 #if defined (OS_CHROMEOS) 389 #if defined (OS_CHROMEOS)
297 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status, 390 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status,
298 bool result); 391 bool result);
299 #endif 392 #endif
300 393
394 // Executes the next removal task. Called after the previous task was finished
395 // or directly from Remove() if the task queue was empty.
396 void RunNextTask();
397
301 // Removes the specified items related to browsing for a specific host. If the 398 // Removes the specified items related to browsing for a specific host. If the
302 // provided |remove_url| is empty, data is removed for all origins; otherwise, 399 // provided |remove_url| is empty, data is removed for all origins; otherwise,
303 // it is restricted by the origin filter origin (where implemented yet). The 400 // it is restricted by the origin filter origin (where implemented yet). The
304 // |origin_type_mask| parameter defines the set of origins from which data 401 // |origin_type_mask| parameter defines the set of origins from which data
305 // should be removed (protected, unprotected, or both). 402 // should be removed (protected, unprotected, or both).
306 // TODO(ttr314): Remove "(where implemented yet)" constraint above once 403 // TODO(ttr314): Remove "(where implemented yet)" constraint above once
307 // crbug.com/113621 is done. 404 // crbug.com/113621 is done.
308 // TODO(crbug.com/589586): Support all backends w/ origin filter. 405 // TODO(crbug.com/589586): Support all backends w/ origin filter.
309 void RemoveImpl(const TimeRange& time_range, 406 void RemoveImpl(const TimeRange& time_range,
310 int remove_mask, 407 int remove_mask,
311 const BrowsingDataFilterBuilder& origin_filter, 408 const BrowsingDataFilterBuilder& filter_builder,
312 int origin_type_mask); 409 int origin_type_mask);
313 410
314 // Notifies observers and transitions to the idle state. 411 // Notifies observers and transitions to the idle state.
315 void Notify(); 412 void Notify();
316 413
317 // Checks if we are all done, and if so, calls Notify(). 414 // Checks if we are all done, and if so, calls Notify().
318 void NotifyIfDone(); 415 void NotifyIfDone();
319 416
320 // Called when history deletion is done. 417 // Called when history deletion is done.
321 void OnHistoryDeletionDone(); 418 void OnHistoryDeletionDone();
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 501
405 // Start time to delete from. 502 // Start time to delete from.
406 base::Time delete_begin_; 503 base::Time delete_begin_;
407 504
408 // End time to delete to. 505 // End time to delete to.
409 base::Time delete_end_; 506 base::Time delete_end_;
410 507
411 // True if Remove has been invoked. 508 // True if Remove has been invoked.
412 bool is_removing_; 509 bool is_removing_;
413 510
511 // Removal tasks to be processed.
512 std::queue<RemovalTask> task_queue_;
513
414 // If non-NULL, the |completion_inhibitor_| is notified each time an instance 514 // If non-NULL, the |completion_inhibitor_| is notified each time an instance
415 // is about to complete a browsing data removal process, and has the ability 515 // is about to complete a browsing data removal process, and has the ability
416 // to artificially delay completion. Used for testing. 516 // to artificially delay completion. Used for testing.
417 static CompletionInhibitor* completion_inhibitor_; 517 static CompletionInhibitor* completion_inhibitor_;
418 518
419 #if defined(ENABLE_PLUGINS) 519 #if defined(ENABLE_PLUGINS)
420 // Used to delete plugin data. 520 // Used to delete plugin data.
421 std::unique_ptr<content::PluginDataRemover> plugin_data_remover_; 521 std::unique_ptr<content::PluginDataRemover> plugin_data_remover_;
422 base::WaitableEventWatcher watcher_; 522 base::WaitableEventWatcher watcher_;
423 523
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 bool waiting_for_clear_webrtc_logs_ = false; 559 bool waiting_for_clear_webrtc_logs_ = false;
460 #endif 560 #endif
461 bool waiting_for_clear_auto_sign_in_ = false; 561 bool waiting_for_clear_auto_sign_in_ = false;
462 562
463 // The removal mask for the current removal operation. 563 // The removal mask for the current removal operation.
464 int remove_mask_ = 0; 564 int remove_mask_ = 0;
465 565
466 // From which types of origins should we remove data? 566 // From which types of origins should we remove data?
467 int origin_type_mask_ = 0; 567 int origin_type_mask_ = 0;
468 568
569 // Observers of the global state and individual tasks.
469 base::ObserverList<Observer, true> observer_list_; 570 base::ObserverList<Observer, true> observer_list_;
470 571
471 // Used if we need to clear history. 572 // Used if we need to clear history.
472 base::CancelableTaskTracker history_task_tracker_; 573 base::CancelableTaskTracker history_task_tracker_;
473 574
474 std::unique_ptr<TemplateURLService::Subscription> template_url_sub_; 575 std::unique_ptr<TemplateURLService::Subscription> template_url_sub_;
475 576
476 // We do not own this. 577 // We do not own this.
477 content::StoragePartition* storage_partition_for_testing_ = nullptr; 578 content::StoragePartition* storage_partition_for_testing_ = nullptr;
478 579
479 #if BUILDFLAG(ANDROID_JAVA_UI) 580 #if BUILDFLAG(ANDROID_JAVA_UI)
480 // WebappRegistry makes calls across the JNI. In unit tests, the Java side is 581 // WebappRegistry makes calls across the JNI. In unit tests, the Java side is
481 // not initialised, so the registry must be mocked out. 582 // not initialised, so the registry must be mocked out.
482 std::unique_ptr<WebappRegistry> webapp_registry_; 583 std::unique_ptr<WebappRegistry> webapp_registry_;
483 #endif 584 #endif
484 585
485 base::WeakPtrFactory<BrowsingDataRemover> weak_ptr_factory_; 586 base::WeakPtrFactory<BrowsingDataRemover> weak_ptr_factory_;
486 587
487 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover); 588 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
488 }; 589 };
489 590
490 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_ 591 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_
OLDNEW
« no previous file with comments | « chrome/browser/android/signin/signin_manager_android.cc ('k') | chrome/browser/browsing_data/browsing_data_remover.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698