OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/browsing_data_local_storage_helper.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "chrome/browser/browsing_data_helper.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "content/public/browser/dom_storage_context.h" | |
13 | |
14 using content::BrowserContext; | |
15 using content::BrowserThread; | |
16 using content::DOMStorageContext; | |
17 using dom_storage::DomStorageContext; | |
18 | |
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) {} | |
22 | |
23 BrowsingDataLocalStorageHelper::LocalStorageInfo::~LocalStorageInfo() {} | |
24 | |
25 BrowsingDataLocalStorageHelper::BrowsingDataLocalStorageHelper( | |
26 Profile* profile) | |
27 : dom_storage_context_( | |
28 BrowserContext::GetDefaultDOMStorageContext(profile)), | |
29 is_fetching_(false) { | |
30 DCHECK(dom_storage_context_); | |
31 } | |
32 | |
33 BrowsingDataLocalStorageHelper::~BrowsingDataLocalStorageHelper() { | |
34 } | |
35 | |
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()); | |
41 | |
42 is_fetching_ = true; | |
43 completion_callback_ = callback; | |
44 dom_storage_context_->GetUsageInfo( | |
45 base::Bind( | |
46 &BrowsingDataLocalStorageHelper::GetUsageInfoCallback, this)); | |
47 } | |
48 | |
49 void BrowsingDataLocalStorageHelper::DeleteOrigin(const GURL& origin) { | |
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
51 dom_storage_context_->DeleteOrigin(origin); | |
52 } | |
53 | |
54 void BrowsingDataLocalStorageHelper::GetUsageInfoCallback( | |
55 const std::vector<DomStorageContext::UsageInfo>& infos) { | |
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
57 | |
58 for (size_t i = 0; i < infos.size(); ++i) { | |
59 // Non-websafe state is not considered browsing data. | |
60 const DomStorageContext::UsageInfo& 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)); | |
64 } | |
65 } | |
66 | |
67 BrowserThread::PostTask( | |
68 BrowserThread::UI, FROM_HERE, | |
69 base::Bind(&BrowsingDataLocalStorageHelper::CallCompletionCallback, | |
70 this)); | |
71 } | |
72 | |
73 void BrowsingDataLocalStorageHelper::CallCompletionCallback() { | |
74 DCHECK(is_fetching_); | |
75 completion_callback_.Run(local_storage_info_); | |
76 completion_callback_.Reset(); | |
77 is_fetching_ = false; | |
78 } | |
79 | |
80 //--------------------------------------------------------- | |
81 | |
82 CannedBrowsingDataLocalStorageHelper::CannedBrowsingDataLocalStorageHelper( | |
83 Profile* profile) | |
84 : BrowsingDataLocalStorageHelper(profile), | |
85 profile_(profile) { | |
86 } | |
87 | |
88 CannedBrowsingDataLocalStorageHelper* | |
89 CannedBrowsingDataLocalStorageHelper::Clone() { | |
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
91 CannedBrowsingDataLocalStorageHelper* clone = | |
92 new CannedBrowsingDataLocalStorageHelper(profile_); | |
93 | |
94 clone->pending_local_storage_info_ = pending_local_storage_info_; | |
95 return clone; | |
96 } | |
97 | |
98 void CannedBrowsingDataLocalStorageHelper::AddLocalStorage( | |
99 const GURL& origin) { | |
100 if (BrowsingDataHelper::HasWebScheme(origin)) | |
101 pending_local_storage_info_.insert(origin); | |
102 } | |
103 | |
104 void CannedBrowsingDataLocalStorageHelper::Reset() { | |
105 pending_local_storage_info_.clear(); | |
106 } | |
107 | |
108 bool CannedBrowsingDataLocalStorageHelper::empty() const { | |
109 return pending_local_storage_info_.empty(); | |
110 } | |
111 | |
112 size_t CannedBrowsingDataLocalStorageHelper::GetLocalStorageCount() const { | |
113 return pending_local_storage_info_.size(); | |
114 } | |
115 | |
116 const std::set<GURL>& | |
117 CannedBrowsingDataLocalStorageHelper::GetLocalStorageInfo() const { | |
118 return pending_local_storage_info_; | |
119 } | |
120 | |
121 void CannedBrowsingDataLocalStorageHelper::StartFetching( | |
122 const base::Callback<void(const std::list<LocalStorageInfo>&)>& callback) { | |
123 DCHECK(!is_fetching_); | |
124 DCHECK_EQ(false, callback.is_null()); | |
125 | |
126 is_fetching_ = true; | |
127 completion_callback_ = callback; | |
128 | |
129 // We post a task to emulate async fetching behavior. | |
130 MessageLoop::current()->PostTask( | |
131 FROM_HERE, | |
132 base::Bind(&CannedBrowsingDataLocalStorageHelper:: | |
133 ConvertPendingInfo, this)); | |
134 } | |
135 | |
136 CannedBrowsingDataLocalStorageHelper::~CannedBrowsingDataLocalStorageHelper() {} | |
137 | |
138 void CannedBrowsingDataLocalStorageHelper::ConvertPendingInfo() { | |
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
140 local_storage_info_.clear(); | |
141 for (std::set<GURL>::iterator iter = pending_local_storage_info_.begin(); | |
142 iter != pending_local_storage_info_.end(); ++iter) { | |
143 local_storage_info_.push_back( | |
144 LocalStorageInfo(*iter, 0, base::Time())); | |
145 } | |
146 MessageLoop::current()->PostTask( | |
147 FROM_HERE, | |
148 base::Bind(&CannedBrowsingDataLocalStorageHelper::CallCompletionCallback, | |
149 this)); | |
150 } | |
OLD | NEW |