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"
10 #include "base/location.h"
11 #include "chrome/browser/browsing_data/browsing_data_helper.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/local_storage_usage_info.h"
15 #include "content/public/browser/storage_partition.h"
17 using content::BrowserContext
;
18 using content::BrowserThread
;
19 using content::DOMStorageContext
;
23 void GetUsageInfoCallback(
24 const BrowsingDataLocalStorageHelper::FetchCallback
& callback
,
25 const std::vector
<content::LocalStorageUsageInfo
>& infos
) {
26 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
27 DCHECK(!callback
.is_null());
29 std::list
<BrowsingDataLocalStorageHelper::LocalStorageInfo
> result
;
30 for (const content::LocalStorageUsageInfo
& info
: infos
) {
31 if (!BrowsingDataHelper::HasWebScheme(info
.origin
))
32 continue; // Non-websafe state is not considered browsing data.
33 result
.push_back(BrowsingDataLocalStorageHelper::LocalStorageInfo(
34 info
.origin
, info
.data_size
, info
.last_modified
));
37 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
38 base::Bind(callback
, result
));
43 BrowsingDataLocalStorageHelper::LocalStorageInfo::LocalStorageInfo(
44 const GURL
& origin_url
,
46 base::Time last_modified
)
47 : origin_url(origin_url
), size(size
), last_modified(last_modified
) {}
49 BrowsingDataLocalStorageHelper::LocalStorageInfo::~LocalStorageInfo() {}
51 BrowsingDataLocalStorageHelper::BrowsingDataLocalStorageHelper(Profile
* profile
)
52 : dom_storage_context_(BrowserContext::GetDefaultStoragePartition(profile
)
53 ->GetDOMStorageContext()) {
54 DCHECK(dom_storage_context_
);
57 BrowsingDataLocalStorageHelper::~BrowsingDataLocalStorageHelper() {
60 void BrowsingDataLocalStorageHelper::StartFetching(
61 const FetchCallback
& callback
) {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
63 DCHECK(!callback
.is_null());
64 dom_storage_context_
->GetLocalStorageUsage(
65 base::Bind(&GetUsageInfoCallback
, callback
));
68 void BrowsingDataLocalStorageHelper::DeleteOrigin(const GURL
& origin
) {
69 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
70 dom_storage_context_
->DeleteLocalStorage(origin
);
73 //---------------------------------------------------------
75 CannedBrowsingDataLocalStorageHelper::CannedBrowsingDataLocalStorageHelper(
77 : BrowsingDataLocalStorageHelper(profile
) {
80 void CannedBrowsingDataLocalStorageHelper::AddLocalStorage(
82 if (!BrowsingDataHelper::HasWebScheme(origin
))
83 return; // Non-websafe state is not considered browsing data.
84 pending_local_storage_info_
.insert(origin
);
87 void CannedBrowsingDataLocalStorageHelper::Reset() {
88 pending_local_storage_info_
.clear();
91 bool CannedBrowsingDataLocalStorageHelper::empty() const {
92 return pending_local_storage_info_
.empty();
95 size_t CannedBrowsingDataLocalStorageHelper::GetLocalStorageCount() const {
96 return pending_local_storage_info_
.size();
100 CannedBrowsingDataLocalStorageHelper::GetLocalStorageInfo() const {
101 return pending_local_storage_info_
;
104 void CannedBrowsingDataLocalStorageHelper::StartFetching(
105 const FetchCallback
& callback
) {
106 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
107 DCHECK(!callback
.is_null());
109 std::list
<LocalStorageInfo
> result
;
110 for (const GURL
& url
: pending_local_storage_info_
)
111 result
.push_back(LocalStorageInfo(url
, 0, base::Time()));
113 BrowserThread::PostTask(
114 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
117 void CannedBrowsingDataLocalStorageHelper::DeleteOrigin(const GURL
& origin
) {
118 pending_local_storage_info_
.erase(origin
);
119 BrowsingDataLocalStorageHelper::DeleteOrigin(origin
);
122 CannedBrowsingDataLocalStorageHelper::~CannedBrowsingDataLocalStorageHelper() {}