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 "base/message_loop/message_loop.h"
9 #include "chrome/browser/browsing_data/browsing_data_helper.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/local_storage_usage_info.h"
13 #include "content/public/browser/storage_partition.h"
15 using content::BrowserContext
;
16 using content::BrowserThread
;
17 using content::DOMStorageContext
;
19 BrowsingDataLocalStorageHelper::LocalStorageInfo::LocalStorageInfo(
20 const GURL
& origin_url
, int64 size
, base::Time last_modified
)
21 : origin_url(origin_url
), size(size
), last_modified(last_modified
) {}
23 BrowsingDataLocalStorageHelper::LocalStorageInfo::~LocalStorageInfo() {}
25 BrowsingDataLocalStorageHelper::BrowsingDataLocalStorageHelper(
27 : dom_storage_context_(
28 BrowserContext::GetDefaultStoragePartition(profile
)->
29 GetDOMStorageContext()),
31 DCHECK(dom_storage_context_
);
34 BrowsingDataLocalStorageHelper::~BrowsingDataLocalStorageHelper() {
37 void BrowsingDataLocalStorageHelper::StartFetching(
38 const base::Callback
<void(const std::list
<LocalStorageInfo
>&)>& callback
) {
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
40 DCHECK(!is_fetching_
);
41 DCHECK_EQ(false, callback
.is_null());
44 completion_callback_
= callback
;
45 dom_storage_context_
->GetLocalStorageUsage(
47 &BrowsingDataLocalStorageHelper::GetUsageInfoCallback
, this));
50 void BrowsingDataLocalStorageHelper::DeleteOrigin(const GURL
& origin
) {
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
52 dom_storage_context_
->DeleteLocalStorage(origin
);
55 void BrowsingDataLocalStorageHelper::GetUsageInfoCallback(
56 const std::vector
<content::LocalStorageUsageInfo
>& infos
) {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
59 for (size_t i
= 0; i
< infos
.size(); ++i
) {
60 // Non-websafe state is not considered browsing data.
61 const content::LocalStorageUsageInfo
& info
= infos
[i
];
62 if (BrowsingDataHelper::HasWebScheme(info
.origin
)) {
63 local_storage_info_
.push_back(
64 LocalStorageInfo(info
.origin
, info
.data_size
, info
.last_modified
));
68 BrowserThread::PostTask(
69 BrowserThread::UI
, FROM_HERE
,
70 base::Bind(&BrowsingDataLocalStorageHelper::CallCompletionCallback
,
74 void BrowsingDataLocalStorageHelper::CallCompletionCallback() {
76 completion_callback_
.Run(local_storage_info_
);
77 completion_callback_
.Reset();
81 //---------------------------------------------------------
83 CannedBrowsingDataLocalStorageHelper::CannedBrowsingDataLocalStorageHelper(
85 : BrowsingDataLocalStorageHelper(profile
),
89 CannedBrowsingDataLocalStorageHelper
*
90 CannedBrowsingDataLocalStorageHelper::Clone() {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
92 CannedBrowsingDataLocalStorageHelper
* clone
=
93 new CannedBrowsingDataLocalStorageHelper(profile_
);
95 clone
->pending_local_storage_info_
= pending_local_storage_info_
;
99 void CannedBrowsingDataLocalStorageHelper::AddLocalStorage(
100 const GURL
& origin
) {
101 if (BrowsingDataHelper::HasWebScheme(origin
))
102 pending_local_storage_info_
.insert(origin
);
105 void CannedBrowsingDataLocalStorageHelper::Reset() {
106 pending_local_storage_info_
.clear();
109 bool CannedBrowsingDataLocalStorageHelper::empty() const {
110 return pending_local_storage_info_
.empty();
113 size_t CannedBrowsingDataLocalStorageHelper::GetLocalStorageCount() const {
114 return pending_local_storage_info_
.size();
117 const std::set
<GURL
>&
118 CannedBrowsingDataLocalStorageHelper::GetLocalStorageInfo() const {
119 return pending_local_storage_info_
;
122 void CannedBrowsingDataLocalStorageHelper::StartFetching(
123 const base::Callback
<void(const std::list
<LocalStorageInfo
>&)>& callback
) {
124 DCHECK(!is_fetching_
);
125 DCHECK_EQ(false, callback
.is_null());
128 completion_callback_
= callback
;
130 // We post a task to emulate async fetching behavior.
131 base::MessageLoop::current()->PostTask(
133 base::Bind(&CannedBrowsingDataLocalStorageHelper::ConvertPendingInfo
,
137 CannedBrowsingDataLocalStorageHelper::~CannedBrowsingDataLocalStorageHelper() {}
139 void CannedBrowsingDataLocalStorageHelper::ConvertPendingInfo() {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
141 local_storage_info_
.clear();
142 for (std::set
<GURL
>::iterator iter
= pending_local_storage_info_
.begin();
143 iter
!= pending_local_storage_info_
.end(); ++iter
) {
144 local_storage_info_
.push_back(
145 LocalStorageInfo(*iter
, 0, base::Time()));
147 base::MessageLoop::current()->PostTask(
149 base::Bind(&CannedBrowsingDataLocalStorageHelper::CallCompletionCallback
,