1 // Copyright 2015 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.
5 #include "chrome/browser/browsing_data/browsing_data_cache_storage_helper.h"
10 #include "chrome/browser/browsing_data/browsing_data_helper.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/cache_storage_context.h"
14 using content::BrowserThread
;
15 using content::CacheStorageContext
;
16 using content::CacheStorageUsageInfo
;
18 BrowsingDataCacheStorageHelper::BrowsingDataCacheStorageHelper(
19 CacheStorageContext
* cache_storage_context
)
20 : cache_storage_context_(cache_storage_context
) {
21 DCHECK(cache_storage_context_
);
24 BrowsingDataCacheStorageHelper::~BrowsingDataCacheStorageHelper() {}
26 void BrowsingDataCacheStorageHelper::StartFetching(
27 const FetchCallback
& callback
) {
28 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
29 DCHECK(!callback
.is_null());
30 BrowserThread::PostTask(
31 BrowserThread::IO
, FROM_HERE
,
33 &BrowsingDataCacheStorageHelper::FetchCacheStorageUsageInfoOnIOThread
,
37 void BrowsingDataCacheStorageHelper::DeleteCacheStorage(const GURL
& origin
) {
38 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
39 BrowserThread::PostTask(
40 BrowserThread::IO
, FROM_HERE
,
41 base::Bind(&BrowsingDataCacheStorageHelper::DeleteCacheStorageOnIOThread
,
45 void BrowsingDataCacheStorageHelper::FetchCacheStorageUsageInfoOnIOThread(
46 const FetchCallback
& callback
) {
47 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
48 DCHECK(!callback
.is_null());
49 cache_storage_context_
->GetAllOriginsInfo(
50 base::Bind(&BrowsingDataCacheStorageHelper::GetAllOriginsInfoCallback
,
54 void BrowsingDataCacheStorageHelper::GetAllOriginsInfoCallback(
55 const FetchCallback
& callback
,
56 const std::vector
<CacheStorageUsageInfo
>& origins
) {
57 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
59 std::list
<content::CacheStorageUsageInfo
> result
;
60 for (const CacheStorageUsageInfo
& origin
: origins
) {
61 if (!BrowsingDataHelper::HasWebScheme(origin
.origin
))
62 continue; // Non-websafe state is not considered browsing data.
63 result
.push_back(origin
);
66 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
67 base::Bind(callback
, result
));
70 void BrowsingDataCacheStorageHelper::DeleteCacheStorageOnIOThread(
72 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
73 cache_storage_context_
->DeleteForOrigin(origin
);
76 CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo::
77 PendingCacheStorageUsageInfo(const GURL
& origin
,
78 int64 total_size_bytes
,
79 const base::Time
& last_modified
)
81 total_size_bytes(total_size_bytes
),
82 last_modified(last_modified
) {}
84 CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo::
85 ~PendingCacheStorageUsageInfo() {}
87 bool CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo::
88 operator<(const PendingCacheStorageUsageInfo
& other
) const {
89 return origin
< other
.origin
;
92 CannedBrowsingDataCacheStorageHelper::CannedBrowsingDataCacheStorageHelper(
93 content::CacheStorageContext
* context
)
94 : BrowsingDataCacheStorageHelper(context
) {}
96 CannedBrowsingDataCacheStorageHelper::~CannedBrowsingDataCacheStorageHelper() {}
98 void CannedBrowsingDataCacheStorageHelper::AddCacheStorage(const GURL
& origin
) {
99 if (!BrowsingDataHelper::HasWebScheme(origin
))
100 return; // Non-websafe state is not considered browsing data.
102 pending_cache_storage_info_
.insert(
103 PendingCacheStorageUsageInfo(origin
, 0, base::Time()));
106 void CannedBrowsingDataCacheStorageHelper::Reset() {
107 pending_cache_storage_info_
.clear();
110 bool CannedBrowsingDataCacheStorageHelper::empty() const {
111 return pending_cache_storage_info_
.empty();
114 size_t CannedBrowsingDataCacheStorageHelper::GetCacheStorageCount() const {
115 return pending_cache_storage_info_
.size();
119 CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo
>&
120 CannedBrowsingDataCacheStorageHelper::GetCacheStorageUsageInfo() const {
121 return pending_cache_storage_info_
;
124 void CannedBrowsingDataCacheStorageHelper::StartFetching(
125 const FetchCallback
& callback
) {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
127 DCHECK(!callback
.is_null());
129 std::list
<CacheStorageUsageInfo
> result
;
130 for (const PendingCacheStorageUsageInfo
& pending_info
:
131 pending_cache_storage_info_
) {
132 CacheStorageUsageInfo
info(pending_info
.origin
,
133 pending_info
.total_size_bytes
,
134 pending_info
.last_modified
);
135 result
.push_back(info
);
138 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
139 base::Bind(callback
, result
));
142 void CannedBrowsingDataCacheStorageHelper::DeleteCacheStorage(
143 const GURL
& origin
) {
144 for (std::set
<PendingCacheStorageUsageInfo
>::iterator it
=
145 pending_cache_storage_info_
.begin();
146 it
!= pending_cache_storage_info_
.end();) {
147 if (it
->origin
== origin
)
148 pending_cache_storage_info_
.erase(it
++);
152 BrowsingDataCacheStorageHelper::DeleteCacheStorage(origin
);