OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/browsing_data/browsing_data_cache_storage_helper.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/callback.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "chrome/browser/browsing_data/browsing_data_helper.h" | |
16 #include "content/public/browser/browser_thread.h" | |
17 #include "content/public/browser/cache_storage_context.h" | |
18 | |
19 using content::BrowserThread; | |
20 using content::CacheStorageContext; | |
21 using content::CacheStorageUsageInfo; | |
22 | |
23 BrowsingDataCacheStorageHelper::BrowsingDataCacheStorageHelper( | |
24 CacheStorageContext* cache_storage_context) | |
25 : cache_storage_context_(cache_storage_context) { | |
26 DCHECK(cache_storage_context_); | |
27 } | |
28 | |
29 BrowsingDataCacheStorageHelper::~BrowsingDataCacheStorageHelper() {} | |
30 | |
31 void BrowsingDataCacheStorageHelper::StartFetching( | |
32 const base::Callback<void(const std::list<CacheStorageUsageInfo>&)>& | |
33 callback) { | |
34 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
35 DCHECK(!is_fetching_); | |
36 DCHECK(!callback.is_null()); | |
37 | |
38 is_fetching_ = true; | |
39 completion_callback_ = callback; | |
40 BrowserThread::PostTask( | |
41 BrowserThread::IO, FROM_HERE, | |
42 base::Bind( | |
43 &BrowsingDataCacheStorageHelper::FetchCacheStorageUsageInfoOnIOThread, | |
44 this)); | |
45 } | |
46 | |
47 void BrowsingDataCacheStorageHelper::DeleteCacheStorage(const GURL& origin) { | |
48 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
49 BrowserThread::PostTask( | |
50 BrowserThread::IO, FROM_HERE, | |
51 base::Bind(&BrowsingDataCacheStorageHelper::DeleteCacheStorageOnIOThread, | |
52 this, origin)); | |
53 } | |
54 | |
55 void BrowsingDataCacheStorageHelper::FetchCacheStorageUsageInfoOnIOThread() { | |
56 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
57 cache_storage_context_->GetAllOriginsInfo(base::Bind( | |
58 &BrowsingDataCacheStorageHelper::GetAllOriginsInfoCallback, this)); | |
59 } | |
60 | |
61 void BrowsingDataCacheStorageHelper::GetAllOriginsInfoCallback( | |
62 const std::vector<CacheStorageUsageInfo>& origins) { | |
63 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
64 for (const CacheStorageUsageInfo& origin : origins) { | |
65 if (!BrowsingDataHelper::HasWebScheme(origin.origin)) | |
66 continue; // Non-websafe state is not considered browsing data. | |
67 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
| |
68 } | |
69 | |
70 BrowserThread::PostTask( | |
71 BrowserThread::UI, FROM_HERE, | |
72 base::Bind(&BrowsingDataCacheStorageHelper::NotifyOnUIThread, this)); | |
73 } | |
74 | |
75 void BrowsingDataCacheStorageHelper::NotifyOnUIThread() { | |
76 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
77 DCHECK(is_fetching_); | |
78 completion_callback_.Run(cache_storage_info_); | |
79 completion_callback_.Reset(); | |
80 is_fetching_ = false; | |
81 } | |
82 | |
83 void BrowsingDataCacheStorageHelper::DeleteCacheStorageOnIOThread( | |
84 const GURL& origin) { | |
85 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
86 cache_storage_context_->DeleteForOrigin(origin); | |
87 } | |
88 | |
89 CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo:: | |
michaeln
2015/08/18 22:25:51
i hope this canned stuff is dead stripped, i don't
| |
90 PendingCacheStorageUsageInfo(const GURL& origin, | |
91 int64 total_size_bytes, | |
92 const base::Time& last_modified) | |
93 : origin(origin), | |
94 total_size_bytes(total_size_bytes), | |
95 last_modified(last_modified) {} | |
96 | |
97 CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo:: | |
98 ~PendingCacheStorageUsageInfo() {} | |
99 | |
100 bool CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo:: | |
101 operator<(const PendingCacheStorageUsageInfo& other) const { | |
102 return origin < other.origin; | |
103 } | |
104 | |
105 CannedBrowsingDataCacheStorageHelper::CannedBrowsingDataCacheStorageHelper( | |
106 content::CacheStorageContext* context) | |
107 : BrowsingDataCacheStorageHelper(context) {} | |
108 | |
109 CannedBrowsingDataCacheStorageHelper::~CannedBrowsingDataCacheStorageHelper() {} | |
110 | |
111 void CannedBrowsingDataCacheStorageHelper::AddCacheStorage(const GURL& origin) { | |
112 if (!BrowsingDataHelper::HasWebScheme(origin)) | |
113 return; // Non-websafe state is not considered browsing data. | |
114 | |
115 pending_cache_storage_info_.insert( | |
116 PendingCacheStorageUsageInfo(origin, 0, base::Time())); | |
117 } | |
118 | |
119 void CannedBrowsingDataCacheStorageHelper::Reset() { | |
120 cache_storage_info_.clear(); | |
121 pending_cache_storage_info_.clear(); | |
122 } | |
123 | |
124 bool CannedBrowsingDataCacheStorageHelper::empty() const { | |
125 return cache_storage_info_.empty() && pending_cache_storage_info_.empty(); | |
126 } | |
127 | |
128 size_t CannedBrowsingDataCacheStorageHelper::GetCacheStorageCount() const { | |
129 return pending_cache_storage_info_.size(); | |
130 } | |
131 | |
132 const std::set< | |
133 CannedBrowsingDataCacheStorageHelper::PendingCacheStorageUsageInfo>& | |
134 CannedBrowsingDataCacheStorageHelper::GetCacheStorageUsageInfo() const { | |
135 return pending_cache_storage_info_; | |
136 } | |
137 | |
138 void CannedBrowsingDataCacheStorageHelper::StartFetching( | |
139 const base::Callback<void(const std::list<CacheStorageUsageInfo>&)>& | |
140 callback) { | |
141 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
142 DCHECK(!callback.is_null()); | |
143 | |
144 std::list<CacheStorageUsageInfo> result; | |
145 for (const PendingCacheStorageUsageInfo& pending_info : | |
146 pending_cache_storage_info_) { | |
147 CacheStorageUsageInfo info(pending_info.origin, | |
148 pending_info.total_size_bytes, | |
149 pending_info.last_modified); | |
150 result.push_back(info); | |
151 } | |
152 | |
153 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
154 base::Bind(callback, result)); | |
155 } | |
156 | |
157 void CannedBrowsingDataCacheStorageHelper::DeleteCacheStorage( | |
158 const GURL& origin) { | |
159 for (std::set<PendingCacheStorageUsageInfo>::iterator it = | |
160 pending_cache_storage_info_.begin(); | |
161 it != pending_cache_storage_info_.end();) { | |
162 if (it->origin == origin) | |
163 pending_cache_storage_info_.erase(it++); | |
164 else | |
165 ++it; | |
166 } | |
167 BrowsingDataCacheStorageHelper::DeleteCacheStorage(origin); | |
168 } | |
OLD | NEW |