diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-21 18:29:45 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-21 18:29:45 +0000 |
commit | d885bfe7aba1fdd42cbd86f4ba5f43416987c8ae (patch) | |
tree | aa74738e48c4b572ede6038720eb7db2e2a47ad0 | |
parent | 772602e4b632cb947b616c4bf48265dd8790ee2e (diff) | |
download | chromium_src-d885bfe7aba1fdd42cbd86f4ba5f43416987c8ae.zip chromium_src-d885bfe7aba1fdd42cbd86f4ba5f43416987c8ae.tar.gz chromium_src-d885bfe7aba1fdd42cbd86f4ba5f43416987c8ae.tar.bz2 |
Update BrowsingDataRemover to use only the asynchronous
interface of the http/disk cache.
BUG=26729
TEST=none
Review URL: http://codereview.chromium.org/2063017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47936 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/browsing_data_remover.cc | 127 | ||||
-rw-r--r-- | chrome/browser/browsing_data_remover.h | 30 |
2 files changed, 100 insertions, 57 deletions
diff --git a/chrome/browser/browsing_data_remover.cc b/chrome/browser/browsing_data_remover.cc index 6263d6d..dac2404 100644 --- a/chrome/browser/browsing_data_remover.cc +++ b/chrome/browser/browsing_data_remover.cc @@ -47,12 +47,16 @@ BrowsingDataRemover::BrowsingDataRemover(Profile* profile, 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)), request_context_getter_(profile->GetRequestContext()), appcaches_to_be_deleted_count_(0), + next_cache_state_(STATE_NONE), + cache_(NULL), waiting_for_clear_databases_(false), waiting_for_clear_history_(false), waiting_for_clear_cache_(false), @@ -68,12 +72,16 @@ BrowsingDataRemover::BrowsingDataRemover(Profile* profile, 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)), request_context_getter_(profile->GetRequestContext()), appcaches_to_be_deleted_count_(0), + next_cache_state_(STATE_NONE), + cache_(NULL), waiting_for_clear_databases_(false), waiting_for_clear_history_(false), waiting_for_clear_cache_(false), @@ -201,24 +209,12 @@ void BrowsingDataRemover::Remove(int remove_mask) { UserMetrics::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"), profile_); - URLRequestContextGetter* main_context_getter = - profile_->GetRequestContext(); - URLRequestContextGetter* media_context_getter = - profile_->GetRequestContextForMedia(); - - // Balanced in ClearCacheOnIOThread(). - main_context_getter->AddRef(); - media_context_getter->AddRef(); + main_context_getter_ = profile_->GetRequestContext(); + media_context_getter_ = profile_->GetRequestContextForMedia(); ChromeThread::PostTask( ChromeThread::IO, FROM_HERE, - NewRunnableMethod( - this, - &BrowsingDataRemover::ClearCacheOnIOThread, - main_context_getter, - media_context_getter, - delete_begin_, - delete_end_)); + NewRunnableMethod(this, &BrowsingDataRemover::ClearCacheOnIOThread)); } NotifyAndDeleteIfDone(); @@ -298,48 +294,75 @@ void BrowsingDataRemover::ClearedCache() { NotifyAndDeleteIfDone(); } -void BrowsingDataRemover::ClearCacheOnIOThread( - URLRequestContextGetter* main_context_getter, - URLRequestContextGetter* media_context_getter, - base::Time delete_begin, - base::Time delete_end) { +void BrowsingDataRemover::ClearCacheOnIOThread() { // This function should be called on the IO thread. DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); + DCHECK_EQ(STATE_NONE, next_cache_state_); + DCHECK(main_context_getter_); + DCHECK(media_context_getter_); - // Get a pointer to the cache. - net::HttpTransactionFactory* factory = - main_context_getter->GetURLRequestContext()->http_transaction_factory(); - disk_cache::Backend* cache = factory->GetCache()->GetBackend(); - - // |cache| can be null if it cannot be initialized. - if (cache) { - if (delete_begin.is_null()) - cache->DoomAllEntries(); - else - cache->DoomEntriesBetween(delete_begin, delete_end); - } + next_cache_state_ = STATE_CREATE_MAIN; + DoClearCache(net::OK); +} - // Get a pointer to the media cache. - factory = media_context_getter->GetURLRequestContext()-> - http_transaction_factory(); - cache = factory->GetCache()->GetBackend(); - - // |cache| can be null if it cannot be initialized. - if (cache) { - if (delete_begin.is_null()) - cache->DoomAllEntries(); - else - cache->DoomEntriesBetween(delete_begin, delete_end); +// 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. + 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: { + main_context_getter_ = NULL; + media_context_getter_ = NULL; + cache_ = NULL; + + // Notify the UI thread that we are done. + ChromeThread::PostTask( + ChromeThread::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; + } + } } - - // Balance the AddRef()s done on the UI thread by Remove(). - main_context_getter->Release(); - media_context_getter->Release(); - - // Notify the UI thread that we are done. - ChromeThread::PostTask( - ChromeThread::UI, FROM_HERE, - NewRunnableMethod(this, &BrowsingDataRemover::ClearedCache)); } void BrowsingDataRemover::OnClearedDatabases(int rv) { diff --git a/chrome/browser/browsing_data_remover.h b/chrome/browser/browsing_data_remover.h index 591427c..5034e88 100644 --- a/chrome/browser/browsing_data_remover.h +++ b/chrome/browser/browsing_data_remover.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -16,6 +16,10 @@ class Profile; class URLRequestContextGetter; +namespace disk_cache { +class Backend; +} + // BrowsingDataRemover is responsible for removing data related to browsing: // visits in url database, downloads, cookies ... @@ -72,6 +76,15 @@ class BrowsingDataRemover : public NotificationObserver { static bool is_removing() { return removing_; } private: + enum CacheState { + STATE_NONE, + STATE_CREATE_MAIN, + STATE_CREATE_MEDIA, + STATE_DELETE_MAIN, + STATE_DELETE_MEDIA, + STATE_DONE + }; + // BrowsingDataRemover deletes itself (using DeleteTask) and is not supposed // to be deleted by other objects so make destructor private and DeleteTask // a friend. @@ -93,10 +106,10 @@ class BrowsingDataRemover : public NotificationObserver { void ClearedCache(); // Invoked on the IO thread to delete from the cache. - void ClearCacheOnIOThread(URLRequestContextGetter* main_context_getter, - URLRequestContextGetter* media_context_getter, - base::Time delete_begin, - base::Time delete_end); + void ClearCacheOnIOThread(); + + // Performs the actual work to delete the cache. + void DoClearCache(int rv); // Callback when HTML5 databases have been deleted. Invokes // NotifyAndDeleteIfDone. @@ -145,6 +158,7 @@ class BrowsingDataRemover : public NotificationObserver { scoped_refptr<webkit_database::DatabaseTracker> database_tracker_; net::CompletionCallbackImpl<BrowsingDataRemover> database_cleared_callback_; + net::CompletionCallbackImpl<BrowsingDataRemover> cache_callback_; // Used to clear the appcache. net::CompletionCallbackImpl<BrowsingDataRemover> appcache_got_info_callback_; @@ -153,6 +167,12 @@ class BrowsingDataRemover : public NotificationObserver { scoped_refptr<URLRequestContextGetter> request_context_getter_; int appcaches_to_be_deleted_count_; + // Used to delete data from the HTTP caches. + CacheState next_cache_state_; + disk_cache::Backend* cache_; + scoped_refptr<URLRequestContextGetter> main_context_getter_; + scoped_refptr<URLRequestContextGetter> media_context_getter_; + // True if we're waiting for various data to be deleted. bool waiting_for_clear_databases_; bool waiting_for_clear_history_; |