Chromium Code Reviews| Index: content/browser/appcache/chrome_appcache_service.cc |
| diff --git a/content/browser/appcache/chrome_appcache_service.cc b/content/browser/appcache/chrome_appcache_service.cc |
| index 169f59eaa919f7aa9ce22a1fc497c8dade11b063..3b70c90da4b3637264f7b8dc8ab00bbbfa03c495 100644 |
| --- a/content/browser/appcache/chrome_appcache_service.cc |
| +++ b/content/browser/appcache/chrome_appcache_service.cc |
| @@ -14,33 +14,24 @@ |
| static bool has_initialized_thread_ids; |
| -namespace { |
| - |
| -// Used to defer deleting of local storage until the destructor has finished. |
| -void DeleteLocalStateOnIOThread(FilePath cache_path) { |
| - // Post the actual deletion to the DB thread to ensure it happens after the |
| - // database file has been closed. |
| - BrowserThread::PostTask( |
| - BrowserThread::DB, FROM_HERE, |
| - NewRunnableFunction<bool(*)(const FilePath&, bool), FilePath, bool>( |
| - &file_util::Delete, cache_path, true)); |
| -} |
| - |
| -} // namespace |
| - |
| // ---------------------------------------------------------------------------- |
| ChromeAppCacheService::ChromeAppCacheService( |
| quota::QuotaManagerProxy* quota_manager_proxy) |
| : AppCacheService(quota_manager_proxy), |
| - resource_context_(NULL), clear_local_state_on_exit_(false) { |
| + resource_context_(NULL), clear_appcache_helper_(NULL), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(appcache_got_info_callback_( |
| + this, &ChromeAppCacheService::OnGotAppCacheInfo)), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(appcache_deleted_callback_( |
| + this, &ChromeAppCacheService::OnAppCacheDeleted)), |
| + appcaches_to_be_deleted_count_(0), |
| + appcaches_cleared_event_(new base::WaitableEvent(true, false)) { |
| } |
| void ChromeAppCacheService::InitializeOnIOThread( |
| const FilePath& cache_path, |
| const content::ResourceContext* resource_context, |
| - scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy, |
| - bool clear_local_state_on_exit) { |
| + scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| if (!has_initialized_thread_ids) { |
| @@ -52,7 +43,6 @@ void ChromeAppCacheService::InitializeOnIOThread( |
| resource_context_ = resource_context; |
| registrar_.Add( |
| this, NotificationType::PURGE_MEMORY, NotificationService::AllSources()); |
| - SetClearLocalStateOnExit(clear_local_state_on_exit); |
| // Init our base class. |
| Initialize(cache_path_, |
| @@ -62,26 +52,62 @@ void ChromeAppCacheService::InitializeOnIOThread( |
| } |
| ChromeAppCacheService::~ChromeAppCacheService() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| +} |
| - if (clear_local_state_on_exit_ && !cache_path_.empty()) { |
| +base::WaitableEvent* ChromeAppCacheService::ClearAppCache( |
| + net::CompletionCallback* callback) |
| +{ |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| BrowserThread::PostTask( |
| - BrowserThread::IO, FROM_HERE, |
| - NewRunnableFunction(DeleteLocalStateOnIOThread, cache_path_)); |
| + BrowserThread::IO, FROM_HERE, |
| + NewRunnableMethod(this, |
| + &ChromeAppCacheService::ClearAppCache, |
| + callback)); |
| + return appcaches_cleared_event_.get(); |
| } |
| + |
| + // FIXME: should we handle multiple simultaneous deletions better? |
| + if (clear_appcache_helper_) |
| + return appcaches_cleared_event_.get(); |
| + |
| + appcaches_cleared_event_->Reset(); |
| + // ClearAppCacheHelper keeps this ChromeAppCacheService alive until the |
| + // deletion is done, even if other objects drop their references. |
| + clear_appcache_helper_ = new ClearAppcacheHelper(this, callback); |
|
jochen (gone - plz use gerrit)
2011/07/06 09:41:45
you could just this->Ref(); and Deref() later, no
marja(google)
2011/07/06 14:34:09
Done.
marja(google)
2011/07/13 11:06:59
Done.
|
| + |
| + appcache_info_ = new appcache::AppCacheInfoCollection; |
| + GetAllAppCacheInfo( |
| + appcache_info_, &appcache_got_info_callback_); |
| + // continues in OnGotAppCacheInfo |
| + |
| + return appcaches_cleared_event_.get(); |
| } |
| -void ChromeAppCacheService::SetClearLocalStateOnExit(bool clear_local_state) { |
| - // TODO(michaeln): How is 'protected' status granted to apps in this case? |
| - if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| - BrowserThread::PostTask( |
| - BrowserThread::IO, FROM_HERE, |
| - NewRunnableMethod(this, |
| - &ChromeAppCacheService::SetClearLocalStateOnExit, |
| - clear_local_state)); |
| - return; |
| +void ChromeAppCacheService::OnGotAppCacheInfo(int rv) { |
| + using appcache::AppCacheInfoVector; |
| + typedef std::map<GURL, AppCacheInfoVector> InfoByOrigin; |
| + |
| + appcaches_to_be_deleted_count_ = 0; |
| + 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; |
| + |
| + ++appcaches_to_be_deleted_count_; |
|
michaeln
2011/07/02 00:29:09
maybe origins_to_be_deleted_count_
marja(google)
2011/07/06 14:34:09
Done.
marja(google)
2011/07/13 11:06:59
I had changed this, but changed it back because of
|
| + DeleteAppCachesForOrigin(origin->first, &appcache_deleted_callback_); |
|
michaeln
2011/07/02 00:29:09
maybe origin_deleted_callback_
marja(google)
2011/07/06 14:34:09
Done.
marja(google)
2011/07/13 11:06:59
Same here.
|
| } |
| - clear_local_state_on_exit_ = clear_local_state; |
| + |
| + if (appcaches_to_be_deleted_count_ == 0) |
| + clear_appcache_helper_->OnClearedAppCache(); |
| + // else continues in OnAppCacheDeleted |
| +} |
| + |
| +void ChromeAppCacheService::OnAppCacheDeleted(int rv) { |
| + --appcaches_to_be_deleted_count_; |
| + if (appcaches_to_be_deleted_count_ == 0) |
| + clear_appcache_helper_->OnClearedAppCache(); |
| } |
| bool ChromeAppCacheService::CanLoadAppCache(const GURL& manifest_url) { |
| @@ -108,6 +134,29 @@ void ChromeAppCacheService::Observe(NotificationType type, |
| // ---------------------------------------------------------------------------- |
| +ChromeAppCacheService::ClearAppcacheHelper::ClearAppcacheHelper( |
| + ChromeAppCacheService* parent, net::CompletionCallback* callback) |
| + : parent_(parent), callback_(callback) |
| +{ |
|
michaeln
2011/07/02 00:29:09
style: bracket belongs at the end of the previous
marja(google)
2011/07/06 14:34:09
Done.
|
| +} |
| + |
| +ChromeAppCacheService::ClearAppcacheHelper::~ClearAppcacheHelper() |
| +{ |
|
michaeln
2011/07/02 00:29:09
style: ditto
marja(google)
2011/07/06 14:34:09
Done.
|
| +} |
| + |
| +void ChromeAppCacheService::ClearAppcacheHelper::OnClearedAppCache() |
|
michaeln
2011/07/02 00:29:09
This class seems heavier weight than it needs to b
marja(google)
2011/07/06 14:34:09
Done.
|
| +{ |
| + parent_->appcaches_cleared_event_->Signal(); |
| + parent_->clear_appcache_helper_ = NULL; |
| + if (callback_) { |
| + callback_->Run(0); |
| + } |
| + MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| +} |
| + |
| + |
| +// ---------------------------------------------------------------------------- |
| + |
| static BrowserThread::ID ToBrowserThreadID(int id) { |
| DCHECK(has_initialized_thread_ids); |
| DCHECK(id == BrowserThread::DB || id == BrowserThread::IO); |