cros: Remove default pinned apps trial.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_local_storage_helper.cc
blobb906a79a4e39b9d7602541069ddbcaac4fbc1e13
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 "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(
26 Profile* profile)
27 : dom_storage_context_(
28 BrowserContext::GetDefaultStoragePartition(profile)->
29 GetDOMStorageContext()),
30 is_fetching_(false) {
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());
43 is_fetching_ = true;
44 completion_callback_ = callback;
45 dom_storage_context_->GetLocalStorageUsage(
46 base::Bind(
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,
71 this));
74 void BrowsingDataLocalStorageHelper::CallCompletionCallback() {
75 DCHECK(is_fetching_);
76 completion_callback_.Run(local_storage_info_);
77 completion_callback_.Reset();
78 is_fetching_ = false;
81 //---------------------------------------------------------
83 CannedBrowsingDataLocalStorageHelper::CannedBrowsingDataLocalStorageHelper(
84 Profile* profile)
85 : BrowsingDataLocalStorageHelper(profile),
86 profile_(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_;
96 return clone;
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());
127 is_fetching_ = true;
128 completion_callback_ = callback;
130 // We post a task to emulate async fetching behavior.
131 base::MessageLoop::current()->PostTask(
132 FROM_HERE,
133 base::Bind(&CannedBrowsingDataLocalStorageHelper::ConvertPendingInfo,
134 this));
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(
148 FROM_HERE,
149 base::Bind(&CannedBrowsingDataLocalStorageHelper::CallCompletionCallback,
150 this));