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 "base/location.h"
11 #include "chrome/browser/browsing_data/browsing_data_helper.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/cache_storage_context.h"
15 using content::BrowserThread
;
16 using content::CacheStorageContext
;
17 using content::CacheStorageUsageInfo
;
21 void GetAllOriginsInfoCallback(
22 const BrowsingDataCacheStorageHelper::FetchCallback
& callback
,
23 const std::vector
<CacheStorageUsageInfo
>& origins
) {
24 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
25 DCHECK(!callback
.is_null());
27 std::list
<content::CacheStorageUsageInfo
> result
;
28 for (const CacheStorageUsageInfo
& origin
: origins
) {
29 if (!BrowsingDataHelper::HasWebScheme(origin
.origin
))
30 continue; // Non-websafe state is not considered browsing data.
31 result
.push_back(origin
);
34 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
35 base::Bind(callback
, result
));
40 BrowsingDataCacheStorageHelper::BrowsingDataCacheStorageHelper(
41 CacheStorageContext
* cache_storage_context
)
42 : cache_storage_context_(cache_storage_context
) {
43 DCHECK(cache_storage_context_
);
46 BrowsingDataCacheStorageHelper::~BrowsingDataCacheStorageHelper() {}
48 void BrowsingDataCacheStorageHelper::StartFetching(
49 const FetchCallback
& callback
) {
50 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
51 DCHECK(!callback
.is_null());
52 BrowserThread::PostTask(
53 BrowserThread::IO
, FROM_HERE
,
55 &BrowsingDataCacheStorageHelper::FetchCacheStorageUsageInfoOnIOThread
,
59 void BrowsingDataCacheStorageHelper::DeleteCacheStorage(const GURL
& origin
) {
60 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
61 BrowserThread::PostTask(
62 BrowserThread::IO
, FROM_HERE
,
63 base::Bind(&BrowsingDataCacheStorageHelper::DeleteCacheStorageOnIOThread
,
67 void BrowsingDataCacheStorageHelper::FetchCacheStorageUsageInfoOnIOThread(
68 const FetchCallback
& callback
) {
69 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
70 DCHECK(!callback
.is_null());
71 cache_storage_context_
->GetAllOriginsInfo(
72 base::Bind(&GetAllOriginsInfoCallback
, callback
));
75 void BrowsingDataCacheStorageHelper::DeleteCacheStorageOnIOThread(
77 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
78 cache_storage_context_
->DeleteForOrigin(origin
);
81 CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo::
82 PendingCacheStorageUsageInfo(const GURL
& origin
,
83 int64 total_size_bytes
,
84 const base::Time
& last_modified
)
86 total_size_bytes(total_size_bytes
),
87 last_modified(last_modified
) {}
89 CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo::
90 ~PendingCacheStorageUsageInfo() {}
92 bool CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo::
93 operator<(const PendingCacheStorageUsageInfo
& other
) const {
94 return origin
< other
.origin
;
97 CannedBrowsingDataCacheStorageHelper::CannedBrowsingDataCacheStorageHelper(
98 content::CacheStorageContext
* context
)
99 : BrowsingDataCacheStorageHelper(context
) {}
101 CannedBrowsingDataCacheStorageHelper::~CannedBrowsingDataCacheStorageHelper() {}
103 void CannedBrowsingDataCacheStorageHelper::AddCacheStorage(const GURL
& origin
) {
104 if (!BrowsingDataHelper::HasWebScheme(origin
))
105 return; // Non-websafe state is not considered browsing data.
107 pending_cache_storage_info_
.insert(
108 PendingCacheStorageUsageInfo(origin
, 0, base::Time()));
111 void CannedBrowsingDataCacheStorageHelper::Reset() {
112 pending_cache_storage_info_
.clear();
115 bool CannedBrowsingDataCacheStorageHelper::empty() const {
116 return pending_cache_storage_info_
.empty();
119 size_t CannedBrowsingDataCacheStorageHelper::GetCacheStorageCount() const {
120 return pending_cache_storage_info_
.size();
124 CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo
>&
125 CannedBrowsingDataCacheStorageHelper::GetCacheStorageUsageInfo() const {
126 return pending_cache_storage_info_
;
129 void CannedBrowsingDataCacheStorageHelper::StartFetching(
130 const FetchCallback
& callback
) {
131 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
132 DCHECK(!callback
.is_null());
134 std::list
<CacheStorageUsageInfo
> result
;
135 for (const PendingCacheStorageUsageInfo
& pending_info
:
136 pending_cache_storage_info_
) {
137 CacheStorageUsageInfo
info(pending_info
.origin
,
138 pending_info
.total_size_bytes
,
139 pending_info
.last_modified
);
140 result
.push_back(info
);
143 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
144 base::Bind(callback
, result
));
147 void CannedBrowsingDataCacheStorageHelper::DeleteCacheStorage(
148 const GURL
& origin
) {
149 for (std::set
<PendingCacheStorageUsageInfo
>::iterator it
=
150 pending_cache_storage_info_
.begin();
151 it
!= pending_cache_storage_info_
.end();) {
152 if (it
->origin
== origin
)
153 pending_cache_storage_info_
.erase(it
++);
157 BrowsingDataCacheStorageHelper::DeleteCacheStorage(origin
);