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