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_indexed_db_helper.h"
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/browsing_data/browsing_data_helper.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/indexed_db_context.h"
19 using content::BrowserThread
;
20 using content::IndexedDBContext
;
21 using content::IndexedDBInfo
;
23 BrowsingDataIndexedDBHelper::BrowsingDataIndexedDBHelper(
24 IndexedDBContext
* indexed_db_context
)
25 : indexed_db_context_(indexed_db_context
),
27 DCHECK(indexed_db_context_
.get());
30 BrowsingDataIndexedDBHelper::~BrowsingDataIndexedDBHelper() {
33 void BrowsingDataIndexedDBHelper::StartFetching(
34 const base::Callback
<void(const std::list
<IndexedDBInfo
>&)>& callback
) {
35 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
36 DCHECK(!is_fetching_
);
37 DCHECK(!callback
.is_null());
40 completion_callback_
= callback
;
41 indexed_db_context_
->TaskRunner()->PostTask(
44 &BrowsingDataIndexedDBHelper::FetchIndexedDBInfoInIndexedDBThread
,
48 void BrowsingDataIndexedDBHelper::DeleteIndexedDB(
50 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
51 indexed_db_context_
->TaskRunner()->PostTask(
54 &BrowsingDataIndexedDBHelper::DeleteIndexedDBInIndexedDBThread
,
59 void BrowsingDataIndexedDBHelper::FetchIndexedDBInfoInIndexedDBThread() {
60 DCHECK(indexed_db_context_
->TaskRunner()->RunsTasksOnCurrentThread());
61 std::vector
<IndexedDBInfo
> origins
= indexed_db_context_
->GetAllOriginsInfo();
62 for (std::vector
<IndexedDBInfo
>::const_iterator iter
= origins
.begin();
63 iter
!= origins
.end(); ++iter
) {
64 const IndexedDBInfo
& origin
= *iter
;
65 if (!BrowsingDataHelper::HasWebScheme(origin
.origin_
))
66 continue; // Non-websafe state is not considered browsing data.
68 indexed_db_info_
.push_back(origin
);
71 BrowserThread::PostTask(
72 BrowserThread::UI
, FROM_HERE
,
73 base::Bind(&BrowsingDataIndexedDBHelper::NotifyInUIThread
, this));
76 void BrowsingDataIndexedDBHelper::NotifyInUIThread() {
77 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
79 completion_callback_
.Run(indexed_db_info_
);
80 completion_callback_
.Reset();
84 void BrowsingDataIndexedDBHelper::DeleteIndexedDBInIndexedDBThread(
86 DCHECK(indexed_db_context_
->TaskRunner()->RunsTasksOnCurrentThread());
87 indexed_db_context_
->DeleteForOrigin(origin
);
90 CannedBrowsingDataIndexedDBHelper::
91 PendingIndexedDBInfo::PendingIndexedDBInfo(const GURL
& origin
,
92 const base::string16
& name
)
97 CannedBrowsingDataIndexedDBHelper::
98 PendingIndexedDBInfo::~PendingIndexedDBInfo() {
101 bool CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo::operator<(
102 const PendingIndexedDBInfo
& other
) const {
103 if (origin
== other
.origin
)
104 return name
< other
.name
;
105 return origin
< other
.origin
;
108 CannedBrowsingDataIndexedDBHelper::CannedBrowsingDataIndexedDBHelper(
109 content::IndexedDBContext
* context
)
110 : BrowsingDataIndexedDBHelper(context
) {
113 CannedBrowsingDataIndexedDBHelper::~CannedBrowsingDataIndexedDBHelper() {}
115 void CannedBrowsingDataIndexedDBHelper::AddIndexedDB(
116 const GURL
& origin
, const base::string16
& name
) {
117 if (!BrowsingDataHelper::HasWebScheme(origin
))
118 return; // Non-websafe state is not considered browsing data.
120 pending_indexed_db_info_
.insert(PendingIndexedDBInfo(origin
, name
));
123 void CannedBrowsingDataIndexedDBHelper::Reset() {
124 indexed_db_info_
.clear();
125 pending_indexed_db_info_
.clear();
128 bool CannedBrowsingDataIndexedDBHelper::empty() const {
129 return indexed_db_info_
.empty() && pending_indexed_db_info_
.empty();
132 size_t CannedBrowsingDataIndexedDBHelper::GetIndexedDBCount() const {
133 return pending_indexed_db_info_
.size();
136 const std::set
<CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo
>&
137 CannedBrowsingDataIndexedDBHelper::GetIndexedDBInfo() const {
138 return pending_indexed_db_info_
;
141 void CannedBrowsingDataIndexedDBHelper::StartFetching(
142 const base::Callback
<void(const std::list
<IndexedDBInfo
>&)>& callback
) {
143 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
144 DCHECK(!callback
.is_null());
146 std::list
<IndexedDBInfo
> result
;
147 for (std::set
<PendingIndexedDBInfo
>::const_iterator
148 pending_info
= pending_indexed_db_info_
.begin();
149 pending_info
!= pending_indexed_db_info_
.end(); ++pending_info
) {
150 IndexedDBInfo
info(pending_info
->origin
, 0, base::Time(), 0);
151 result
.push_back(info
);
154 BrowserThread::PostTask(
155 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
158 void CannedBrowsingDataIndexedDBHelper::DeleteIndexedDB(
159 const GURL
& origin
) {
160 for (std::set
<PendingIndexedDBInfo
>::iterator it
=
161 pending_indexed_db_info_
.begin();
162 it
!= pending_indexed_db_info_
.end(); ) {
163 if (it
->origin
== origin
)
164 pending_indexed_db_info_
.erase(it
++);
168 BrowsingDataIndexedDBHelper::DeleteIndexedDB(origin
);