1// Copyright (c) 2011 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 CHROME_BROWSER_BROWSING_DATA_REMOVER_H_
6#define CHROME_BROWSER_BROWSING_DATA_REMOVER_H_
7#pragma once
8
9#include <vector>
10
11#include "base/memory/ref_counted.h"
12#include "base/observer_list.h"
13#include "base/synchronization/waitable_event_watcher.h"
14#include "base/time.h"
15#include "content/browser/appcache/chrome_appcache_service.h"
16#include "content/browser/cancelable_request.h"
17#include "content/common/notification_registrar.h"
18
19class ExtensionSpecialStoragePolicy;
20class IOThread;
21class PluginDataRemover;
22class Profile;
23
24namespace disk_cache {
25class Backend;
26}
27
28namespace net {
29class URLRequestContextGetter;
30}
31
32namespace webkit_database {
33class DatabaseTracker;
34}
35
36// BrowsingDataRemover is responsible for removing data related to browsing:
37// visits in url database, downloads, cookies ...
38
39class BrowsingDataRemover : public NotificationObserver,
40                            public base::WaitableEventWatcher::Delegate {
41 public:
42  // Time period ranges available when doing browsing data removals.
43  enum TimePeriod {
44    LAST_HOUR = 0,
45    LAST_DAY,
46    LAST_WEEK,
47    FOUR_WEEKS,
48    EVERYTHING
49  };
50
51  // Mask used for Remove.
52
53  // In addition to visits, this removes keywords and the last session.
54  static const int REMOVE_HISTORY = 1 << 0;
55  static const int REMOVE_DOWNLOADS = 1 << 1;
56  static const int REMOVE_COOKIES = 1 << 2;
57  static const int REMOVE_PASSWORDS = 1 << 3;
58  static const int REMOVE_FORM_DATA = 1 << 4;
59  static const int REMOVE_CACHE = 1 << 5;
60  static const int REMOVE_LSO_DATA = 1 << 6;
61
62  // Observer is notified when the removal is done. Done means keywords have
63  // been deleted, cache cleared and all other tasks scheduled.
64  class Observer {
65   public:
66    virtual void OnBrowsingDataRemoverDone() = 0;
67
68   protected:
69    virtual ~Observer() {}
70  };
71
72  // Creates a BrowsingDataRemover to remove browser data from the specified
73  // profile in the specified time range. Use Remove to initiate the removal.
74  BrowsingDataRemover(Profile* profile, base::Time delete_begin,
75                      base::Time delete_end);
76
77  // Creates a BrowsingDataRemover to remove browser data from the specified
78  // profile in the specified time range.
79  BrowsingDataRemover(Profile* profile, TimePeriod time_period,
80                      base::Time delete_end);
81
82  // Removes the specified items related to browsing.
83  void Remove(int remove_mask);
84
85  void AddObserver(Observer* observer);
86  void RemoveObserver(Observer* observer);
87
88  // Called when history deletion is done.
89  void OnHistoryDeletionDone();
90
91  static bool is_removing() { return removing_; }
92
93 private:
94  enum CacheState {
95    STATE_NONE,
96    STATE_CREATE_MAIN,
97    STATE_CREATE_MEDIA,
98    STATE_DELETE_MAIN,
99    STATE_DELETE_MEDIA,
100    STATE_DONE
101  };
102
103  // BrowsingDataRemover deletes itself (using DeleteTask) and is not supposed
104  // to be deleted by other objects so make destructor private and DeleteTask
105  // a friend.
106  friend class DeleteTask<BrowsingDataRemover>;
107  ~BrowsingDataRemover();
108
109  // NotificationObserver method. Callback when TemplateURLModel has finished
110  // loading. Deletes the entries from the model, and if we're not waiting on
111  // anything else notifies observers and deletes this BrowsingDataRemover.
112  virtual void Observe(NotificationType type,
113                       const NotificationSource& source,
114                       const NotificationDetails& details);
115
116  // WaitableEventWatcher implementation.
117  // Called when plug-in data has been cleared. Invokes NotifyAndDeleteIfDone.
118  virtual void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
119
120  // If we're not waiting on anything, notifies observers and deletes this
121  // object.
122  void NotifyAndDeleteIfDone();
123
124  // Callback when the network history has been deleted. Invokes
125  // NotifyAndDeleteIfDone.
126  void ClearedNetworkHistory();
127
128  // Invoked on the IO thread to clear the HostCache, speculative data about
129  // subresources on visited sites, and initial navigation history.
130  void ClearNetworkingHistory(IOThread* io_thread);
131
132  // Callback when the cache has been deleted. Invokes NotifyAndDeleteIfDone.
133  void ClearedCache();
134
135  // Invoked on the IO thread to delete from the cache.
136  void ClearCacheOnIOThread();
137
138  // Performs the actual work to delete the cache.
139  void DoClearCache(int rv);
140
141  // Callback when HTML5 databases have been deleted. Invokes
142  // NotifyAndDeleteIfDone.
143  void OnClearedDatabases(int rv);
144
145  // Invoked on the FILE thread to delete HTML5 databases.
146  void ClearDatabasesOnFILEThread();
147
148  // Callback when the appcache has been cleared. Invokes
149  // NotifyAndDeleteIfDone.
150  void OnClearedAppCache();
151
152  // Invoked on the IO thread to delete from the AppCache.
153  void ClearAppCacheOnIOThread();
154
155  // Lower level helpers.
156  void OnGotAppCacheInfo(int rv);
157  void OnAppCacheDeleted(int rv);
158  ChromeAppCacheService* GetAppCacheService();
159
160  // Calculate the begin time for the deletion range specified by |time_period|.
161  base::Time CalculateBeginDeleteTime(TimePeriod time_period);
162
163  // Returns true if we're all done.
164  bool all_done() {
165    return registrar_.IsEmpty() && !waiting_for_clear_cache_ &&
166           !waiting_for_clear_history_ &&
167           !waiting_for_clear_networking_history_ &&
168           !waiting_for_clear_databases_ && !waiting_for_clear_appcache_ &&
169           !waiting_for_clear_lso_data_;
170  }
171
172  NotificationRegistrar registrar_;
173
174  // Profile we're to remove from.
175  Profile* profile_;
176
177  // 'Protected' origins are not subject to data removal.
178  scoped_refptr<ExtensionSpecialStoragePolicy> special_storage_policy_;
179
180  // Start time to delete from.
181  const base::Time delete_begin_;
182
183  // End time to delete to.
184  const base::Time delete_end_;
185
186  // True if Remove has been invoked.
187  static bool removing_;
188
189  // Reference to database tracker held while deleting databases.
190  scoped_refptr<webkit_database::DatabaseTracker> database_tracker_;
191
192  net::CompletionCallbackImpl<BrowsingDataRemover> database_cleared_callback_;
193  net::CompletionCallbackImpl<BrowsingDataRemover> cache_callback_;
194
195  // Used to clear the appcache.
196  net::CompletionCallbackImpl<BrowsingDataRemover> appcache_got_info_callback_;
197  net::CompletionCallbackImpl<BrowsingDataRemover> appcache_deleted_callback_;
198  scoped_refptr<appcache::AppCacheInfoCollection> appcache_info_;
199  int appcaches_to_be_deleted_count_;
200
201  // Used to delete data from the HTTP caches.
202  CacheState next_cache_state_;
203  disk_cache::Backend* cache_;
204
205  // Used to delete data from HTTP cache and appcache.
206  scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
207  scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
208
209  // Used to delete plugin data.
210  scoped_refptr<PluginDataRemover> plugin_data_remover_;
211  base::WaitableEventWatcher watcher_;
212
213  // True if we're waiting for various data to be deleted.
214  bool waiting_for_clear_databases_;
215  bool waiting_for_clear_history_;
216  bool waiting_for_clear_networking_history_;
217  bool waiting_for_clear_cache_;
218  bool waiting_for_clear_appcache_;
219  bool waiting_for_clear_lso_data_;
220
221  ObserverList<Observer> observer_list_;
222
223  // Used if we need to clear history.
224  CancelableRequestConsumer request_consumer_;
225
226  DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
227};
228
229#endif  // CHROME_BROWSER_BROWSING_DATA_REMOVER_H_
230