Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / browsing_data / browsing_data_remover.h
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 CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_
7
8 #include <set>
9
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/observer_list.h"
13 #include "base/prefs/pref_member.h"
14 #include "base/sequenced_task_runner_helpers.h"
15 #include "base/synchronization/waitable_event_watcher.h"
16 #include "base/task/cancelable_task_tracker.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/pepper_flash_settings_manager.h"
19 #include "chrome/browser/search_engines/template_url_service.h"
20 #if defined(OS_CHROMEOS)
21 #include "chromeos/dbus/dbus_method_call_status.h"
22 #endif
23 #include "url/gurl.h"
24 #include "webkit/common/quota/quota_types.h"
25
26 class ExtensionSpecialStoragePolicy;
27 class IOThread;
28 class Profile;
29
30 namespace content {
31 class PluginDataRemover;
32 class StoragePartition;
33 }
34
35 namespace disk_cache {
36 class Backend;
37 }
38
39 namespace net {
40 class URLRequestContextGetter;
41 }
42
43 namespace quota {
44 class QuotaManager;
45 }
46
47 namespace content {
48 class DOMStorageContext;
49 struct LocalStorageUsageInfo;
50 struct SessionStorageUsageInfo;
51 }
52
53 // BrowsingDataRemover is responsible for removing data related to browsing:
54 // visits in url database, downloads, cookies ...
55
56 class BrowsingDataRemover
57 #if defined(ENABLE_PLUGINS)
58     : public PepperFlashSettingsManager::Client
59 #endif
60     {
61  public:
62   // Time period ranges available when doing browsing data removals.
63   enum TimePeriod {
64     LAST_HOUR = 0,
65     LAST_DAY,
66     LAST_WEEK,
67     FOUR_WEEKS,
68     EVERYTHING
69   };
70
71   // Mask used for Remove.
72   enum RemoveDataMask {
73     REMOVE_APPCACHE = 1 << 0,
74     REMOVE_CACHE = 1 << 1,
75     REMOVE_COOKIES = 1 << 2,
76     REMOVE_DOWNLOADS = 1 << 3,
77     REMOVE_FILE_SYSTEMS = 1 << 4,
78     REMOVE_FORM_DATA = 1 << 5,
79     // In addition to visits, REMOVE_HISTORY removes keywords and last session.
80     REMOVE_HISTORY = 1 << 6,
81     REMOVE_INDEXEDDB = 1 << 7,
82     REMOVE_LOCAL_STORAGE = 1 << 8,
83     REMOVE_PLUGIN_DATA = 1 << 9,
84     REMOVE_PASSWORDS = 1 << 10,
85     REMOVE_WEBSQL = 1 << 11,
86     REMOVE_SERVER_BOUND_CERTS = 1 << 12,
87     REMOVE_CONTENT_LICENSES = 1 << 13,
88 #if defined(OS_ANDROID)
89     REMOVE_APP_BANNER_DATA = 1 << 14,
90 #endif
91     // The following flag is used only in tests. In normal usage, hosted app
92     // data is controlled by the REMOVE_COOKIES flag, applied to the
93     // protected-web origin.
94     REMOVE_HOSTED_APP_DATA_TESTONLY = 1 << 31,
95
96     // "Site data" includes cookies, appcache, file systems, indexedDBs, local
97     // storage, webSQL, and plugin data.
98     REMOVE_SITE_DATA = REMOVE_APPCACHE |
99                        REMOVE_COOKIES |
100                        REMOVE_FILE_SYSTEMS |
101                        REMOVE_INDEXEDDB |
102                        REMOVE_LOCAL_STORAGE |
103                        REMOVE_PLUGIN_DATA |
104                        REMOVE_WEBSQL |
105 #if defined(OS_ANDROID)
106                        REMOVE_APP_BANNER_DATA |
107 #endif
108                        REMOVE_SERVER_BOUND_CERTS,
109
110     // Includes all the available remove options. Meant to be used by clients
111     // that wish to wipe as much data as possible from a Profile, to make it
112     // look like a new Profile.
113     REMOVE_ALL = REMOVE_SITE_DATA |
114                  REMOVE_CACHE |
115                  REMOVE_DOWNLOADS |
116                  REMOVE_FORM_DATA |
117                  REMOVE_HISTORY |
118                  REMOVE_PASSWORDS |
119                  REMOVE_CONTENT_LICENSES,
120   };
121
122   // When BrowsingDataRemover successfully removes data, a notification of type
123   // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
124   // this type.
125   struct NotificationDetails {
126     NotificationDetails();
127     NotificationDetails(const NotificationDetails& details);
128     NotificationDetails(base::Time removal_begin,
129                        int removal_mask,
130                        int origin_set_mask);
131     ~NotificationDetails();
132
133     // The beginning of the removal time range.
134     base::Time removal_begin;
135
136     // The removal mask (see the RemoveDataMask enum for details).
137     int removal_mask;
138
139     // The origin set mask (see BrowsingDataHelper::OriginSetMask for details).
140     int origin_set_mask;
141   };
142
143   // Observer is notified when the removal is done. Done means keywords have
144   // been deleted, cache cleared and all other tasks scheduled.
145   class Observer {
146    public:
147     virtual void OnBrowsingDataRemoverDone() = 0;
148
149    protected:
150     virtual ~Observer() {}
151   };
152
153   // Creates a BrowsingDataRemover object that removes data regardless of the
154   // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
155   // retains ownership of itself, and deletes itself once finished.
156   static BrowsingDataRemover* CreateForUnboundedRange(Profile* profile);
157
158   // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
159   // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
160   // deletes itself once finished.
161   static BrowsingDataRemover* CreateForRange(Profile* profile,
162                                              base::Time delete_begin,
163                                              base::Time delete_end);
164
165   // Creates a BrowsingDataRemover bound to a specific period of time (as
166   // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
167   // retains ownership of itself, and deletes itself once finished.
168   static BrowsingDataRemover* CreateForPeriod(Profile* profile,
169                                               TimePeriod period);
170
171   // Calculate the begin time for the deletion range specified by |time_period|.
172   static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
173
174   // Is the BrowsingDataRemover currently in the process of removing data?
175   static bool is_removing() { return is_removing_; }
176
177   // Removes the specified items related to browsing for all origins that match
178   // the provided |origin_set_mask| (see BrowsingDataHelper::OriginSetMask).
179   void Remove(int remove_mask, int origin_set_mask);
180
181   void AddObserver(Observer* observer);
182   void RemoveObserver(Observer* observer);
183
184   // Called when history deletion is done.
185   void OnHistoryDeletionDone();
186
187   // Used for testing.
188   void OverrideStoragePartitionForTesting(
189       content::StoragePartition* storage_partition);
190
191  private:
192   // The clear API needs to be able to toggle removing_ in order to test that
193   // only one BrowsingDataRemover instance can be called at a time.
194   FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest, OneAtATime);
195
196   // The BrowsingDataRemover tests need to be able to access the implementation
197   // of Remove(), as it exposes details that aren't yet available in the public
198   // API. As soon as those details are exposed via new methods, this should be
199   // removed.
200   //
201   // TODO(mkwst): See http://crbug.com/113621
202   friend class BrowsingDataRemoverTest;
203
204   enum CacheState {
205     STATE_NONE,
206     STATE_CREATE_MAIN,
207     STATE_CREATE_MEDIA,
208     STATE_DELETE_MAIN,
209     STATE_DELETE_MEDIA,
210     STATE_DONE
211   };
212
213   // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
214   // not already removing, and vice-versa.
215   static void set_removing(bool is_removing);
216
217   // Creates a BrowsingDataRemover to remove browser data from the specified
218   // profile in the specified time range. Use Remove to initiate the removal.
219   BrowsingDataRemover(Profile* profile,
220                       base::Time delete_begin,
221                       base::Time delete_end);
222
223   // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
224   // to be deleted by other objects so make destructor private and DeleteHelper
225   // a friend.
226   friend class base::DeleteHelper<BrowsingDataRemover>;
227   virtual ~BrowsingDataRemover();
228
229   // Callback for when TemplateURLService has finished loading. Clears the data,
230   // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
231   void OnKeywordsLoaded();
232
233   // Called when plug-in data has been cleared. Invokes NotifyAndDeleteIfDone.
234   void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
235
236 #if defined(ENABLE_PLUGINS)
237   // PepperFlashSettingsManager::Client implementation.
238   virtual void OnDeauthorizeContentLicensesCompleted(uint32 request_id,
239                                                      bool success) OVERRIDE;
240 #endif
241
242 #if defined (OS_CHROMEOS)
243   void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status,
244                            bool result);
245 #endif
246
247   // Removes the specified items related to browsing for a specific host. If the
248   // provided |origin| is empty, data is removed for all origins. The
249   // |origin_set_mask| parameter defines the set of origins from which data
250   // should be removed (protected, unprotected, or both).
251   void RemoveImpl(int remove_mask,
252                   const GURL& origin,
253                   int origin_set_mask);
254
255   // If we're not waiting on anything, notifies observers and deletes this
256   // object.
257   void NotifyAndDeleteIfDone();
258
259   // Callback for when the hostname resolution cache has been cleared.
260   // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
261   void OnClearedHostnameResolutionCache();
262
263   // Invoked on the IO thread to clear the hostname resolution cache.
264   void ClearHostnameResolutionCacheOnIOThread(IOThread* io_thread);
265
266   // Callback for when the LoggedIn Predictor has been cleared.
267   // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
268   void OnClearedLoggedInPredictor();
269
270   // Clears the LoggedIn Predictor.
271   void ClearLoggedInPredictor();
272
273   // Callback for when speculative data in the network Predictor has been
274   // cleared. Clears the respective waiting flag and invokes
275   // NotifyAndDeleteIfDone.
276   void OnClearedNetworkPredictor();
277
278   // Invoked on the IO thread to clear speculative data related to hostname
279   // pre-resolution from the network Predictor.
280   void ClearNetworkPredictorOnIOThread();
281
282   // Callback for when network related data in ProfileIOData has been cleared.
283   // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
284   void OnClearedNetworkingHistory();
285
286   // Callback for when the cache has been deleted. Invokes
287   // NotifyAndDeleteIfDone.
288   void ClearedCache();
289
290   // Invoked on the IO thread to delete from the cache.
291   void ClearCacheOnIOThread();
292
293   // Performs the actual work to delete the cache.
294   void DoClearCache(int rv);
295
296 #if !defined(DISABLE_NACL)
297   // Callback for when the NaCl cache has been deleted. Invokes
298   // NotifyAndDeleteIfDone.
299   void ClearedNaClCache();
300
301   // Invokes the ClearedNaClCache on the UI thread.
302   void ClearedNaClCacheOnIOThread();
303
304   // Invoked on the IO thread to delete the NaCl cache.
305   void ClearNaClCacheOnIOThread();
306
307   // Callback for when the PNaCl translation cache has been deleted. Invokes
308   // NotifyAndDeleteIfDone.
309   void ClearedPnaclCache();
310
311   // Invokes ClearedPnaclCacheOn on the UI thread.
312   void ClearedPnaclCacheOnIOThread();
313
314   // Invoked on the IO thread to delete entries in the PNaCl translation cache.
315   void ClearPnaclCacheOnIOThread(base::Time begin, base::Time end);
316 #endif
317
318   // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
319   void OnClearedCookies(int num_deleted);
320
321   // Invoked on the IO thread to delete cookies.
322   void ClearCookiesOnIOThread(net::URLRequestContextGetter* rq_context);
323
324   // Invoked on the IO thread to delete server bound certs.
325   void ClearServerBoundCertsOnIOThread(
326       net::URLRequestContextGetter* rq_context);
327
328   // Callback on IO Thread when server bound certs have been deleted. Clears SSL
329   // connection pool and posts to UI thread to run OnClearedServerBoundCerts.
330   void OnClearedServerBoundCertsOnIOThread(
331       net::URLRequestContextGetter* rq_context);
332
333   // Callback for when server bound certs have been deleted. Invokes
334   // NotifyAndDeleteIfDone.
335   void OnClearedServerBoundCerts();
336
337   // Callback from the above method.
338   void OnClearedFormData();
339
340   // Callback for when the Autofill profile and credit card origin URLs have
341   // been deleted.
342   void OnClearedAutofillOriginURLs();
343
344
345   // Callback on UI thread when the storage partition related data are cleared.
346   void OnClearedStoragePartitionData();
347
348   // Returns true if we're all done.
349   bool AllDone();
350
351   // Profile we're to remove from.
352   Profile* profile_;
353
354   // 'Protected' origins are not subject to data removal.
355   scoped_refptr<ExtensionSpecialStoragePolicy> special_storage_policy_;
356
357   // Start time to delete from.
358   const base::Time delete_begin_;
359
360   // End time to delete to.
361   base::Time delete_end_;
362
363   // True if Remove has been invoked.
364   static bool is_removing_;
365
366   CacheState next_cache_state_;
367   disk_cache::Backend* cache_;
368
369   // Used to delete data from HTTP cache.
370   scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
371   scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
372
373 #if defined(ENABLE_PLUGINS)
374   // Used to delete plugin data.
375   scoped_ptr<content::PluginDataRemover> plugin_data_remover_;
376   base::WaitableEventWatcher watcher_;
377
378   // Used to deauthorize content licenses for Pepper Flash.
379   scoped_ptr<PepperFlashSettingsManager> pepper_flash_settings_manager_;
380 #endif
381
382   uint32 deauthorize_content_licenses_request_id_;
383   // True if we're waiting for various data to be deleted.
384   // These may only be accessed from UI thread in order to avoid races!
385   bool waiting_for_clear_autofill_origin_urls_;
386   bool waiting_for_clear_cache_;
387   bool waiting_for_clear_content_licenses_;
388   // Non-zero if waiting for cookies to be cleared.
389   int waiting_for_clear_cookies_count_;
390   bool waiting_for_clear_form_;
391   bool waiting_for_clear_history_;
392   bool waiting_for_clear_hostname_resolution_cache_;
393   bool waiting_for_clear_keyword_data_;
394   bool waiting_for_clear_logged_in_predictor_;
395   bool waiting_for_clear_nacl_cache_;
396   bool waiting_for_clear_network_predictor_;
397   bool waiting_for_clear_networking_history_;
398   bool waiting_for_clear_platform_keys_;
399   bool waiting_for_clear_plugin_data_;
400   bool waiting_for_clear_pnacl_cache_;
401   bool waiting_for_clear_server_bound_certs_;
402   bool waiting_for_clear_storage_partition_data_;
403
404   // The removal mask for the current removal operation.
405   int remove_mask_;
406
407   // The origin for the current removal operation.
408   GURL remove_origin_;
409
410   // From which types of origins should we remove data?
411   int origin_set_mask_;
412
413   ObserverList<Observer> observer_list_;
414
415   // Used if we need to clear history.
416   base::CancelableTaskTracker history_task_tracker_;
417
418   scoped_ptr<TemplateURLService::Subscription> template_url_sub_;
419
420   // We do not own this.
421   content::StoragePartition* storage_partition_for_testing_;
422
423   DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
424 };
425
426 #endif  // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_