1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/browsing_data_remover.h"
#include <map>
#include <set>
#include "base/callback.h"
#include "chrome/browser/autofill/personal_data_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_special_storage_policy.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/io_thread.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/net/chrome_net_log.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/browser/password_manager/password_store.h"
#include "chrome/browser/plugin_data_remover.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_host/web_cache_manager.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/sessions/session_service.h"
#include "chrome/browser/sessions/tab_restore_service.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/common/url_constants.h"
#include "content/browser/browser_thread.h"
#include "content/browser/in_process_webkit/webkit_context.h"
#include "content/common/notification_source.h"
#include "net/base/cookie_monster.h"
#include "net/base/net_errors.h"
#include "net/base/transport_security_state.h"
#include "net/disk_cache/disk_cache.h"
#include "net/http/http_cache.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "webkit/database/database_tracker.h"
#include "webkit/database/database_util.h"
// Done so that we can use PostTask on BrowsingDataRemovers and not have
// BrowsingDataRemover implement RefCounted.
DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowsingDataRemover);
bool BrowsingDataRemover::removing_ = false;
BrowsingDataRemover::BrowsingDataRemover(Profile* profile,
base::Time delete_begin,
base::Time delete_end)
: profile_(profile),
special_storage_policy_(profile->GetExtensionSpecialStoragePolicy()),
delete_begin_(delete_begin),
delete_end_(delete_end),
ALLOW_THIS_IN_INITIALIZER_LIST(database_cleared_callback_(
this, &BrowsingDataRemover::OnClearedDatabases)),
ALLOW_THIS_IN_INITIALIZER_LIST(cache_callback_(
this, &BrowsingDataRemover::DoClearCache)),
ALLOW_THIS_IN_INITIALIZER_LIST(appcache_got_info_callback_(
this, &BrowsingDataRemover::OnGotAppCacheInfo)),
ALLOW_THIS_IN_INITIALIZER_LIST(appcache_deleted_callback_(
this, &BrowsingDataRemover::OnAppCacheDeleted)),
appcaches_to_be_deleted_count_(0),
next_cache_state_(STATE_NONE),
cache_(NULL),
main_context_getter_(profile->GetRequestContext()),
media_context_getter_(profile->GetRequestContextForMedia()),
waiting_for_clear_databases_(false),
waiting_for_clear_history_(false),
waiting_for_clear_networking_history_(false),
waiting_for_clear_cache_(false),
waiting_for_clear_appcache_(false) {
DCHECK(profile);
}
BrowsingDataRemover::BrowsingDataRemover(Profile* profile,
TimePeriod time_period,
base::Time delete_end)
: profile_(profile),
special_storage_policy_(profile->GetExtensionSpecialStoragePolicy()),
delete_begin_(CalculateBeginDeleteTime(time_period)),
delete_end_(delete_end),
ALLOW_THIS_IN_INITIALIZER_LIST(database_cleared_callback_(
this, &BrowsingDataRemover::OnClearedDatabases)),
ALLOW_THIS_IN_INITIALIZER_LIST(cache_callback_(
this, &BrowsingDataRemover::DoClearCache)),
ALLOW_THIS_IN_INITIALIZER_LIST(appcache_got_info_callback_(
this, &BrowsingDataRemover::OnGotAppCacheInfo)),
ALLOW_THIS_IN_INITIALIZER_LIST(appcache_deleted_callback_(
this, &BrowsingDataRemover::OnAppCacheDeleted)),
appcaches_to_be_deleted_count_(0),
next_cache_state_(STATE_NONE),
cache_(NULL),
main_context_getter_(profile->GetRequestContext()),
media_context_getter_(profile->GetRequestContextForMedia()),
waiting_for_clear_databases_(false),
waiting_for_clear_history_(false),
waiting_for_clear_networking_history_(false),
waiting_for_clear_cache_(false),
waiting_for_clear_appcache_(false),
waiting_for_clear_lso_data_(false) {
DCHECK(profile);
}
BrowsingDataRemover::~BrowsingDataRemover() {
DCHECK(all_done());
}
void BrowsingDataRemover::Remove(int remove_mask) {
DCHECK(!removing_);
removing_ = true;
if (remove_mask & REMOVE_HISTORY) {
HistoryService* history_service =
profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
if (history_service) {
std::set<GURL> restrict_urls;
UserMetrics::RecordAction(UserMetricsAction("ClearBrowsingData_History"),
profile_);
waiting_for_clear_history_ = true;
history_service->ExpireHistoryBetween(restrict_urls,
delete_begin_, delete_end_,
&request_consumer_,
NewCallback(this, &BrowsingDataRemover::OnHistoryDeletionDone));
}
// Need to clear the host cache and accumulated speculative data, as it also
// reveals some history.
waiting_for_clear_networking_history_ = true;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
this,
&BrowsingDataRemover::ClearNetworkingHistory,
g_browser_process->io_thread()));
// As part of history deletion we also delete the auto-generated keywords.
TemplateURLModel* keywords_model = profile_->GetTemplateURLModel();
if (keywords_model && !keywords_model->loaded()) {
registrar_.Add(this, NotificationType::TEMPLATE_URL_MODEL_LOADED,
Source<TemplateURLModel>(keywords_model));
keywords_model->Load();
} else if (keywords_model) {
keywords_model->RemoveAutoGeneratedBetween(delete_begin_, delete_end_);
}
// We also delete the list of recently closed tabs. Since these expire,
// they can't be more than a day old, so we can simply clear them all.
TabRestoreService* tab_service = profile_->GetTabRestoreService();
if (tab_service) {
tab_service->ClearEntries();
tab_service->DeleteLastSession();
}
// We also delete the last session when we delete the history.
SessionService* session_service = profile_->GetSessionService();
if (session_service)
session_service->DeleteLastSession();
}
if (remove_mask & REMOVE_DOWNLOADS) {
UserMetrics::RecordAction(UserMetricsAction("ClearBrowsingData_Downloads"),
profile_);
DownloadManager* download_manager = profile_->GetDownloadManager();
download_manager->RemoveDownloadsBetween(delete_begin_, delete_end_);
download_manager->ClearLastDownloadPath();
}
if (remove_mask & REMOVE_COOKIES) {
UserMetrics::RecordAction(UserMetricsAction("ClearBrowsingData_Cookies"),
profile_);
// Since we are running on the UI thread don't call GetURLRequestContext().
net::CookieMonster* cookie_monster =
profile_->GetRequestContext()->DONTUSEME_GetCookieStore()->
GetCookieMonster();
if (cookie_monster)
cookie_monster->DeleteAllCreatedBetween(delete_begin_, delete_end_, true);
// REMOVE_COOKIES is actually "cookies and other site data" so we make sure
// to remove other data such local databases, STS state, etc.
// We assume the end time is now.
profile_->GetWebKitContext()->DeleteDataModifiedSince(delete_begin_);
database_tracker_ = profile_->GetDatabaseTracker();
if (database_tracker_.get()) {
waiting_for_clear_databases_ = true;
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
NewRunnableMethod(
this,
&BrowsingDataRemover::ClearDatabasesOnFILEThread));
}
waiting_for_clear_appcache_ = true;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
this,
&BrowsingDataRemover::ClearAppCacheOnIOThread));
// TODO(michaeln): delete temporary file system data too
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(
profile_->GetTransportSecurityState(),
&net::TransportSecurityState::DeleteSince,
delete_begin_));
}
if (remove_mask & REMOVE_PASSWORDS) {
UserMetrics::RecordAction(UserMetricsAction("ClearBrowsingData_Passwords"),
profile_);
PasswordStore* password_store =
profile_->GetPasswordStore(Profile::EXPLICIT_ACCESS);
if (password_store)
password_store->RemoveLoginsCreatedBetween(delete_begin_, delete_end_);
}
if (remove_mask & REMOVE_FORM_DATA) {
UserMetrics::RecordAction(UserMetricsAction("ClearBrowsingData_Autofill"),
profile_);
WebDataService* web_data_service =
profile_->GetWebDataService(Profile::EXPLICIT_ACCESS);
if (web_data_service) {
web_data_service->RemoveFormElementsAddedBetween(delete_begin_,
delete_end_);
web_data_service->RemoveAutofillProfilesAndCreditCardsModifiedBetween(
delete_begin_, delete_end_);
PersonalDataManager* data_manager = profile_->GetPersonalDataManager();
if (data_manager) {
data_manager->Refresh();
}
}
}
if (remove_mask & REMOVE_CACHE) {
// Tell the renderers to clear their cache.
WebCacheManager::GetInstance()->ClearCache();
// Invoke DoClearCache on the IO thread.
waiting_for_clear_cache_ = true;
UserMetrics::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"),
profile_);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(this, &BrowsingDataRemover::ClearCacheOnIOThread));
}
if (remove_mask & REMOVE_LSO_DATA) {
UserMetrics::RecordAction(UserMetricsAction("ClearBrowsingData_LSOData"));
waiting_for_clear_lso_data_ = true;
if (!plugin_data_remover_.get())
plugin_data_remover_ = new PluginDataRemover();
base::WaitableEvent* event =
plugin_data_remover_->StartRemoving(delete_begin_);
watcher_.StartWatching(event, this);
}
NotifyAndDeleteIfDone();
}
void BrowsingDataRemover::AddObserver(Observer* observer) {
observer_list_.AddObserver(observer);
}
void BrowsingDataRemover::RemoveObserver(Observer* observer) {
observer_list_.RemoveObserver(observer);
}
void BrowsingDataRemover::OnHistoryDeletionDone() {
waiting_for_clear_history_ = false;
NotifyAndDeleteIfDone();
}
base::Time BrowsingDataRemover::CalculateBeginDeleteTime(
TimePeriod time_period) {
base::TimeDelta diff;
base::Time delete_begin_time = base::Time::Now();
switch (time_period) {
case LAST_HOUR:
diff = base::TimeDelta::FromHours(1);
break;
case LAST_DAY:
diff = base::TimeDelta::FromHours(24);
break;
case LAST_WEEK:
diff = base::TimeDelta::FromHours(7*24);
break;
case FOUR_WEEKS:
diff = base::TimeDelta::FromHours(4*7*24);
break;
case EVERYTHING:
delete_begin_time = base::Time();
break;
default:
NOTREACHED() << L"Missing item";
break;
}
return delete_begin_time - diff;
}
void BrowsingDataRemover::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
// TODO(brettw) bug 1139736: This should also observe session
// clearing (what about other things such as passwords, etc.?) and wait for
// them to complete before continuing.
DCHECK(type == NotificationType::TEMPLATE_URL_MODEL_LOADED);
TemplateURLModel* model = Source<TemplateURLModel>(source).ptr();
if (model->profile() == profile_->GetOriginalProfile()) {
registrar_.RemoveAll();
model->RemoveAutoGeneratedBetween(delete_begin_, delete_end_);
NotifyAndDeleteIfDone();
}
}
void BrowsingDataRemover::NotifyAndDeleteIfDone() {
// TODO(brettw) bug 1139736: see TODO in Observe() above.
if (!all_done())
return;
// The NetLog contains download history, but may also contain form data,
// cookies and passwords. Simplest just to always clear it. Must be cleared
// after the cache, as cleaning up the disk cache exposes some of the history
// in the NetLog.
g_browser_process->net_log()->ClearAllPassivelyCapturedEvents();
removing_ = false;
FOR_EACH_OBSERVER(Observer, observer_list_, OnBrowsingDataRemoverDone());
// History requests aren't happy if you delete yourself from the callback.
// As such, we do a delete later.
MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
void BrowsingDataRemover::ClearedNetworkHistory() {
waiting_for_clear_networking_history_ = false;
NotifyAndDeleteIfDone();
}
void BrowsingDataRemover::ClearNetworkingHistory(IOThread* io_thread) {
// This function should be called on the IO thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
io_thread->ClearNetworkingHistory();
// Notify the UI thread that we are done.
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this, &BrowsingDataRemover::ClearedNetworkHistory));
}
void BrowsingDataRemover::ClearedCache() {
waiting_for_clear_cache_ = false;
NotifyAndDeleteIfDone();
}
void BrowsingDataRemover::ClearCacheOnIOThread() {
// This function should be called on the IO thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK_EQ(STATE_NONE, next_cache_state_);
DCHECK(main_context_getter_);
DCHECK(media_context_getter_);
next_cache_state_ = STATE_CREATE_MAIN;
DoClearCache(net::OK);
}
// The expected state sequence is STATE_NONE --> STATE_CREATE_MAIN -->
// STATE_DELETE_MAIN --> STATE_CREATE_MEDIA --> STATE_DELETE_MEDIA -->
// STATE_DONE, and any errors are ignored.
void BrowsingDataRemover::DoClearCache(int rv) {
DCHECK_NE(STATE_NONE, next_cache_state_);
while (rv != net::ERR_IO_PENDING && next_cache_state_ != STATE_NONE) {
switch (next_cache_state_) {
case STATE_CREATE_MAIN:
case STATE_CREATE_MEDIA: {
// Get a pointer to the cache.
net::URLRequestContextGetter* getter =
(next_cache_state_ == STATE_CREATE_MAIN) ?
main_context_getter_ : media_context_getter_;
net::HttpTransactionFactory* factory =
getter->GetURLRequestContext()->http_transaction_factory();
rv = factory->GetCache()->GetBackend(&cache_, &cache_callback_);
next_cache_state_ = (next_cache_state_ == STATE_CREATE_MAIN) ?
STATE_DELETE_MAIN : STATE_DELETE_MEDIA;
break;
}
case STATE_DELETE_MAIN:
case STATE_DELETE_MEDIA: {
// |cache_| can be null if it cannot be initialized.
if (cache_) {
if (delete_begin_.is_null()) {
rv = cache_->DoomAllEntries(&cache_callback_);
} else {
rv = cache_->DoomEntriesBetween(delete_begin_, delete_end_,
&cache_callback_);
}
cache_ = NULL;
}
next_cache_state_ = (next_cache_state_ == STATE_DELETE_MAIN) ?
STATE_CREATE_MEDIA : STATE_DONE;
break;
}
case STATE_DONE: {
cache_ = NULL;
// Notify the UI thread that we are done.
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this, &BrowsingDataRemover::ClearedCache));
next_cache_state_ = STATE_NONE;
break;
}
default: {
NOTREACHED() << "bad state";
next_cache_state_ = STATE_NONE; // Stop looping.
break;
}
}
}
}
void BrowsingDataRemover::OnClearedDatabases(int rv) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
bool result = BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this, &BrowsingDataRemover::OnClearedDatabases, rv));
DCHECK(result);
return;
}
// Notify the UI thread that we are done.
database_tracker_ = NULL;
waiting_for_clear_databases_ = false;
NotifyAndDeleteIfDone();
}
void BrowsingDataRemover::ClearDatabasesOnFILEThread() {
// This function should be called on the FILE thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
int rv = database_tracker_->DeleteDataModifiedSince(
delete_begin_, &database_cleared_callback_);
if (rv != net::ERR_IO_PENDING)
OnClearedDatabases(rv);
}
void BrowsingDataRemover::OnClearedAppCache() {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
bool result = BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this, &BrowsingDataRemover::OnClearedAppCache));
DCHECK(result);
return;
}
waiting_for_clear_appcache_ = false;
NotifyAndDeleteIfDone();
}
void BrowsingDataRemover::ClearAppCacheOnIOThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(waiting_for_clear_appcache_);
appcache_info_ = new appcache::AppCacheInfoCollection;
GetAppCacheService()->GetAllAppCacheInfo(
appcache_info_, &appcache_got_info_callback_);
// continues in OnGotAppCacheInfo
}
void BrowsingDataRemover::OnGotAppCacheInfo(int rv) {
using appcache::AppCacheInfoVector;
typedef std::map<GURL, AppCacheInfoVector> InfoByOrigin;
for (InfoByOrigin::const_iterator origin =
appcache_info_->infos_by_origin.begin();
origin != appcache_info_->infos_by_origin.end(); ++origin) {
if (special_storage_policy_->IsStorageProtected(origin->first))
continue;
for (AppCacheInfoVector::const_iterator info = origin->second.begin();
info != origin->second.end(); ++info) {
if (info->creation_time > delete_begin_) {
++appcaches_to_be_deleted_count_;
GetAppCacheService()->DeleteAppCacheGroup(
info->manifest_url, &appcache_deleted_callback_);
}
}
}
if (!appcaches_to_be_deleted_count_)
OnClearedAppCache();
// else continues in OnAppCacheDeleted
}
void BrowsingDataRemover::OnAppCacheDeleted(int rv) {
--appcaches_to_be_deleted_count_;
if (!appcaches_to_be_deleted_count_)
OnClearedAppCache();
}
ChromeAppCacheService* BrowsingDataRemover::GetAppCacheService() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
ChromeURLRequestContext* request_context =
reinterpret_cast<ChromeURLRequestContext*>(
main_context_getter_->GetURLRequestContext());
return request_context ? request_context->appcache_service()
: NULL;
}
void BrowsingDataRemover::OnWaitableEventSignaled(
base::WaitableEvent* waitable_event) {
waiting_for_clear_lso_data_ = false;
NotifyAndDeleteIfDone();
}
|