Chromium Code Reviews| Index: chrome/browser/browsing_data/browsing_data_cache_storage_helper.cc |
| diff --git a/chrome/browser/browsing_data/browsing_data_cache_storage_helper.cc b/chrome/browser/browsing_data/browsing_data_cache_storage_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..95a4bb78aafb2bcba36a9f9081c89390aed15429 |
| --- /dev/null |
| +++ b/chrome/browser/browsing_data/browsing_data_cache_storage_helper.cc |
| @@ -0,0 +1,168 @@ |
| +// Copyright 2015 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/browsing_data_cache_storage_helper.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/browsing_data/browsing_data_helper.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/cache_storage_context.h" |
| + |
| +using content::BrowserThread; |
| +using content::CacheStorageContext; |
| +using content::CacheStorageUsageInfo; |
| + |
| +BrowsingDataCacheStorageHelper::BrowsingDataCacheStorageHelper( |
| + CacheStorageContext* cache_storage_context) |
| + : cache_storage_context_(cache_storage_context) { |
| + DCHECK(cache_storage_context_); |
| +} |
| + |
| +BrowsingDataCacheStorageHelper::~BrowsingDataCacheStorageHelper() {} |
| + |
| +void BrowsingDataCacheStorageHelper::StartFetching( |
| + const base::Callback<void(const std::list<CacheStorageUsageInfo>&)>& |
| + callback) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + DCHECK(!is_fetching_); |
| + DCHECK(!callback.is_null()); |
| + |
| + is_fetching_ = true; |
| + completion_callback_ = callback; |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind( |
| + &BrowsingDataCacheStorageHelper::FetchCacheStorageUsageInfoOnIOThread, |
| + this)); |
| +} |
| + |
| +void BrowsingDataCacheStorageHelper::DeleteCacheStorage(const GURL& origin) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&BrowsingDataCacheStorageHelper::DeleteCacheStorageOnIOThread, |
| + this, origin)); |
| +} |
| + |
| +void BrowsingDataCacheStorageHelper::FetchCacheStorageUsageInfoOnIOThread() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + cache_storage_context_->GetAllOriginsInfo(base::Bind( |
| + &BrowsingDataCacheStorageHelper::GetAllOriginsInfoCallback, this)); |
| +} |
| + |
| +void BrowsingDataCacheStorageHelper::GetAllOriginsInfoCallback( |
| + const std::vector<CacheStorageUsageInfo>& origins) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + for (const CacheStorageUsageInfo& origin : origins) { |
| + if (!BrowsingDataHelper::HasWebScheme(origin.origin)) |
| + continue; // Non-websafe state is not considered browsing data. |
| + cache_storage_info_.push_back(origin); |
|
michaeln
2015/08/18 22:25:51
is the cache_storagse_info_ data member really nee
jsbell
2015/08/19 01:06:19
Done. Excellent suggestion, definitely simplifies
|
| + } |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&BrowsingDataCacheStorageHelper::NotifyOnUIThread, this)); |
| +} |
| + |
| +void BrowsingDataCacheStorageHelper::NotifyOnUIThread() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + DCHECK(is_fetching_); |
| + completion_callback_.Run(cache_storage_info_); |
| + completion_callback_.Reset(); |
| + is_fetching_ = false; |
| +} |
| + |
| +void BrowsingDataCacheStorageHelper::DeleteCacheStorageOnIOThread( |
| + const GURL& origin) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + cache_storage_context_->DeleteForOrigin(origin); |
| +} |
| + |
| +CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo:: |
|
michaeln
2015/08/18 22:25:51
i hope this canned stuff is dead stripped, i don't
|
| + PendingCacheStorageUsageInfo(const GURL& origin, |
| + int64 total_size_bytes, |
| + const base::Time& last_modified) |
| + : origin(origin), |
| + total_size_bytes(total_size_bytes), |
| + last_modified(last_modified) {} |
| + |
| +CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo:: |
| + ~PendingCacheStorageUsageInfo() {} |
| + |
| +bool CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo:: |
| +operator<(const PendingCacheStorageUsageInfo& other) const { |
| + return origin < other.origin; |
| +} |
| + |
| +CannedBrowsingDataCacheStorageHelper::CannedBrowsingDataCacheStorageHelper( |
| + content::CacheStorageContext* context) |
| + : BrowsingDataCacheStorageHelper(context) {} |
| + |
| +CannedBrowsingDataCacheStorageHelper::~CannedBrowsingDataCacheStorageHelper() {} |
| + |
| +void CannedBrowsingDataCacheStorageHelper::AddCacheStorage(const GURL& origin) { |
| + if (!BrowsingDataHelper::HasWebScheme(origin)) |
| + return; // Non-websafe state is not considered browsing data. |
| + |
| + pending_cache_storage_info_.insert( |
| + PendingCacheStorageUsageInfo(origin, 0, base::Time())); |
| +} |
| + |
| +void CannedBrowsingDataCacheStorageHelper::Reset() { |
| + cache_storage_info_.clear(); |
| + pending_cache_storage_info_.clear(); |
| +} |
| + |
| +bool CannedBrowsingDataCacheStorageHelper::empty() const { |
| + return cache_storage_info_.empty() && pending_cache_storage_info_.empty(); |
| +} |
| + |
| +size_t CannedBrowsingDataCacheStorageHelper::GetCacheStorageCount() const { |
| + return pending_cache_storage_info_.size(); |
| +} |
| + |
| +const std::set< |
| + CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo>& |
| +CannedBrowsingDataCacheStorageHelper::GetCacheStorageUsageInfo() const { |
| + return pending_cache_storage_info_; |
| +} |
| + |
| +void CannedBrowsingDataCacheStorageHelper::StartFetching( |
| + const base::Callback<void(const std::list<CacheStorageUsageInfo>&)>& |
| + callback) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + DCHECK(!callback.is_null()); |
| + |
| + std::list<CacheStorageUsageInfo> result; |
| + for (const PendingCacheStorageUsageInfo& pending_info : |
| + pending_cache_storage_info_) { |
| + CacheStorageUsageInfo info(pending_info.origin, |
| + pending_info.total_size_bytes, |
| + pending_info.last_modified); |
| + result.push_back(info); |
| + } |
| + |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + base::Bind(callback, result)); |
| +} |
| + |
| +void CannedBrowsingDataCacheStorageHelper::DeleteCacheStorage( |
| + const GURL& origin) { |
| + for (std::set<PendingCacheStorageUsageInfo>::iterator it = |
| + pending_cache_storage_info_.begin(); |
| + it != pending_cache_storage_info_.end();) { |
| + if (it->origin == origin) |
| + pending_cache_storage_info_.erase(it++); |
| + else |
| + ++it; |
| + } |
| + BrowsingDataCacheStorageHelper::DeleteCacheStorage(origin); |
| +} |