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
) {
26 DCHECK(indexed_db_context_
.get());
29 BrowsingDataIndexedDBHelper::~BrowsingDataIndexedDBHelper() {
32 void BrowsingDataIndexedDBHelper::StartFetching(
33 const base::Callback
<void(const std::list
<IndexedDBInfo
>&)>& callback
) {
34 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
35 DCHECK(!is_fetching_
);
36 DCHECK(!callback
.is_null());
39 completion_callback_
= callback
;
40 indexed_db_context_
->TaskRunner()->PostTask(
43 &BrowsingDataIndexedDBHelper::FetchIndexedDBInfoInIndexedDBThread
,
47 void BrowsingDataIndexedDBHelper::DeleteIndexedDB(
49 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
50 indexed_db_context_
->TaskRunner()->PostTask(
53 &BrowsingDataIndexedDBHelper::DeleteIndexedDBInIndexedDBThread
,
58 void BrowsingDataIndexedDBHelper::FetchIndexedDBInfoInIndexedDBThread() {
59 DCHECK(indexed_db_context_
->TaskRunner()->RunsTasksOnCurrentThread());
60 std::vector
<IndexedDBInfo
> origins
= indexed_db_context_
->GetAllOriginsInfo();
61 for (const IndexedDBInfo
& origin
: origins
) {
62 if (!BrowsingDataHelper::HasWebScheme(origin
.origin_
))
63 continue; // Non-websafe state is not considered browsing data.
65 indexed_db_info_
.push_back(origin
);
68 BrowserThread::PostTask(
69 BrowserThread::UI
, FROM_HERE
,
70 base::Bind(&BrowsingDataIndexedDBHelper::NotifyInUIThread
, this));
73 void BrowsingDataIndexedDBHelper::NotifyInUIThread() {
74 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
76 completion_callback_
.Run(indexed_db_info_
);
77 completion_callback_
.Reset();
81 void BrowsingDataIndexedDBHelper::DeleteIndexedDBInIndexedDBThread(
83 DCHECK(indexed_db_context_
->TaskRunner()->RunsTasksOnCurrentThread());
84 indexed_db_context_
->DeleteForOrigin(origin
);
87 CannedBrowsingDataIndexedDBHelper::
88 PendingIndexedDBInfo::PendingIndexedDBInfo(const GURL
& origin
,
89 const base::string16
& name
)
94 CannedBrowsingDataIndexedDBHelper::
95 PendingIndexedDBInfo::~PendingIndexedDBInfo() {
98 bool CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo::operator<(
99 const PendingIndexedDBInfo
& other
) const {
100 if (origin
== other
.origin
)
101 return name
< other
.name
;
102 return origin
< other
.origin
;
105 CannedBrowsingDataIndexedDBHelper::CannedBrowsingDataIndexedDBHelper(
106 content::IndexedDBContext
* context
)
107 : BrowsingDataIndexedDBHelper(context
) {
110 CannedBrowsingDataIndexedDBHelper::~CannedBrowsingDataIndexedDBHelper() {}
112 void CannedBrowsingDataIndexedDBHelper::AddIndexedDB(
113 const GURL
& origin
, const base::string16
& name
) {
114 if (!BrowsingDataHelper::HasWebScheme(origin
))
115 return; // Non-websafe state is not considered browsing data.
117 pending_indexed_db_info_
.insert(PendingIndexedDBInfo(origin
, name
));
120 void CannedBrowsingDataIndexedDBHelper::Reset() {
121 indexed_db_info_
.clear();
122 pending_indexed_db_info_
.clear();
125 bool CannedBrowsingDataIndexedDBHelper::empty() const {
126 return indexed_db_info_
.empty() && pending_indexed_db_info_
.empty();
129 size_t CannedBrowsingDataIndexedDBHelper::GetIndexedDBCount() const {
130 return pending_indexed_db_info_
.size();
133 const std::set
<CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo
>&
134 CannedBrowsingDataIndexedDBHelper::GetIndexedDBInfo() const {
135 return pending_indexed_db_info_
;
138 void CannedBrowsingDataIndexedDBHelper::StartFetching(
139 const base::Callback
<void(const std::list
<IndexedDBInfo
>&)>& callback
) {
140 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
141 DCHECK(!callback
.is_null());
143 std::list
<IndexedDBInfo
> result
;
144 for (const PendingIndexedDBInfo
& pending_info
: pending_indexed_db_info_
) {
145 IndexedDBInfo
info(pending_info
.origin
, 0, base::Time(), 0);
146 result
.push_back(info
);
149 BrowserThread::PostTask(
150 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
153 void CannedBrowsingDataIndexedDBHelper::DeleteIndexedDB(
154 const GURL
& origin
) {
155 for (std::set
<PendingIndexedDBInfo
>::iterator it
=
156 pending_indexed_db_info_
.begin();
157 it
!= pending_indexed_db_info_
.end(); ) {
158 if (it
->origin
== origin
)
159 pending_indexed_db_info_
.erase(it
++);
163 BrowsingDataIndexedDBHelper::DeleteIndexedDB(origin
);