1 // Copyright (c) 2012 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_local_storage_helper.h"
8 #include "chrome/browser/browsing_data/browsing_data_helper.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/local_storage_usage_info.h"
12 #include "content/public/browser/storage_partition.h"
14 using content::BrowserContext
;
15 using content::BrowserThread
;
16 using content::DOMStorageContext
;
18 BrowsingDataLocalStorageHelper::LocalStorageInfo::LocalStorageInfo(
19 const GURL
& origin_url
, int64 size
, base::Time last_modified
)
20 : origin_url(origin_url
), size(size
), last_modified(last_modified
) {}
22 BrowsingDataLocalStorageHelper::LocalStorageInfo::~LocalStorageInfo() {}
24 BrowsingDataLocalStorageHelper::BrowsingDataLocalStorageHelper(
26 : dom_storage_context_(
27 BrowserContext::GetDefaultStoragePartition(profile
)->
28 GetDOMStorageContext()),
30 DCHECK(dom_storage_context_
);
33 BrowsingDataLocalStorageHelper::~BrowsingDataLocalStorageHelper() {
36 void BrowsingDataLocalStorageHelper::StartFetching(
37 const base::Callback
<void(const std::list
<LocalStorageInfo
>&)>& callback
) {
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
39 DCHECK(!is_fetching_
);
40 DCHECK_EQ(false, callback
.is_null());
43 completion_callback_
= callback
;
44 dom_storage_context_
->GetLocalStorageUsage(
46 &BrowsingDataLocalStorageHelper::GetUsageInfoCallback
, this));
49 void BrowsingDataLocalStorageHelper::DeleteOrigin(const GURL
& origin
) {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
51 dom_storage_context_
->DeleteLocalStorage(origin
);
54 void BrowsingDataLocalStorageHelper::GetUsageInfoCallback(
55 const std::vector
<content::LocalStorageUsageInfo
>& infos
) {
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
58 for (size_t i
= 0; i
< infos
.size(); ++i
) {
59 // Non-websafe state is not considered browsing data.
60 const content::LocalStorageUsageInfo
& info
= infos
[i
];
61 if (BrowsingDataHelper::HasWebScheme(info
.origin
)) {
62 local_storage_info_
.push_back(
63 LocalStorageInfo(info
.origin
, info
.data_size
, info
.last_modified
));
67 BrowserThread::PostTask(
68 BrowserThread::UI
, FROM_HERE
,
69 base::Bind(&BrowsingDataLocalStorageHelper::CallCompletionCallback
,
73 void BrowsingDataLocalStorageHelper::CallCompletionCallback() {
75 completion_callback_
.Run(local_storage_info_
);
76 completion_callback_
.Reset();
80 //---------------------------------------------------------
82 CannedBrowsingDataLocalStorageHelper::CannedBrowsingDataLocalStorageHelper(
84 : BrowsingDataLocalStorageHelper(profile
),
88 CannedBrowsingDataLocalStorageHelper
*
89 CannedBrowsingDataLocalStorageHelper::Clone() {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
91 CannedBrowsingDataLocalStorageHelper
* clone
=
92 new CannedBrowsingDataLocalStorageHelper(profile_
);
94 clone
->pending_local_storage_info_
= pending_local_storage_info_
;
98 void CannedBrowsingDataLocalStorageHelper::AddLocalStorage(
100 if (BrowsingDataHelper::HasWebScheme(origin
))
101 pending_local_storage_info_
.insert(origin
);
104 void CannedBrowsingDataLocalStorageHelper::Reset() {
105 pending_local_storage_info_
.clear();
108 bool CannedBrowsingDataLocalStorageHelper::empty() const {
109 return pending_local_storage_info_
.empty();
112 size_t CannedBrowsingDataLocalStorageHelper::GetLocalStorageCount() const {
113 return pending_local_storage_info_
.size();
116 const std::set
<GURL
>&
117 CannedBrowsingDataLocalStorageHelper::GetLocalStorageInfo() const {
118 return pending_local_storage_info_
;
121 void CannedBrowsingDataLocalStorageHelper::StartFetching(
122 const base::Callback
<void(const std::list
<LocalStorageInfo
>&)>& callback
) {
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
124 DCHECK(!callback
.is_null());
126 std::list
<LocalStorageInfo
> result
;
127 for (std::set
<GURL
>::iterator iter
= pending_local_storage_info_
.begin();
128 iter
!= pending_local_storage_info_
.end(); ++iter
) {
130 LocalStorageInfo(*iter
, 0, base::Time()));
133 BrowserThread::PostTask(
134 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
137 void CannedBrowsingDataLocalStorageHelper::DeleteOrigin(const GURL
& origin
) {
138 pending_local_storage_info_
.erase(origin
);
139 BrowsingDataLocalStorageHelper::DeleteOrigin(origin
);
142 CannedBrowsingDataLocalStorageHelper::~CannedBrowsingDataLocalStorageHelper() {}