Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_local_storage_helper.cc
blob21316c3c706708d88dc20d29819b91e8f7e57503
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"
7 #include "base/bind.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(
25 Profile* profile)
26 : dom_storage_context_(
27 BrowserContext::GetDefaultStoragePartition(profile)->
28 GetDOMStorageContext()),
29 is_fetching_(false) {
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());
42 is_fetching_ = true;
43 completion_callback_ = callback;
44 dom_storage_context_->GetLocalStorageUsage(
45 base::Bind(
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,
70 this));
73 void BrowsingDataLocalStorageHelper::CallCompletionCallback() {
74 DCHECK(is_fetching_);
75 completion_callback_.Run(local_storage_info_);
76 completion_callback_.Reset();
77 is_fetching_ = false;
80 //---------------------------------------------------------
82 CannedBrowsingDataLocalStorageHelper::CannedBrowsingDataLocalStorageHelper(
83 Profile* profile)
84 : BrowsingDataLocalStorageHelper(profile),
85 profile_(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_;
95 return clone;
98 void CannedBrowsingDataLocalStorageHelper::AddLocalStorage(
99 const GURL& origin) {
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) {
129 result.push_back(
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() {}