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/location.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/browsing_data/browsing_data_helper.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/indexed_db_context.h"
16 using content::BrowserThread
;
17 using content::IndexedDBContext
;
18 using content::IndexedDBInfo
;
20 BrowsingDataIndexedDBHelper::BrowsingDataIndexedDBHelper(
21 IndexedDBContext
* indexed_db_context
)
22 : indexed_db_context_(indexed_db_context
) {
23 DCHECK(indexed_db_context_
.get());
26 BrowsingDataIndexedDBHelper::~BrowsingDataIndexedDBHelper() {
29 void BrowsingDataIndexedDBHelper::StartFetching(const FetchCallback
& callback
) {
30 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
31 DCHECK(!callback
.is_null());
32 indexed_db_context_
->TaskRunner()->PostTask(
35 &BrowsingDataIndexedDBHelper::FetchIndexedDBInfoInIndexedDBThread
,
39 void BrowsingDataIndexedDBHelper::DeleteIndexedDB(const GURL
& origin
) {
40 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
41 indexed_db_context_
->TaskRunner()->PostTask(
44 &BrowsingDataIndexedDBHelper::DeleteIndexedDBInIndexedDBThread
,
49 void BrowsingDataIndexedDBHelper::FetchIndexedDBInfoInIndexedDBThread(
50 const FetchCallback
& callback
) {
51 DCHECK(indexed_db_context_
->TaskRunner()->RunsTasksOnCurrentThread());
52 DCHECK(!callback
.is_null());
53 std::vector
<IndexedDBInfo
> origins
= indexed_db_context_
->GetAllOriginsInfo();
54 std::list
<content::IndexedDBInfo
> result
;
55 for (const IndexedDBInfo
& origin
: origins
) {
56 if (!BrowsingDataHelper::HasWebScheme(origin
.origin
))
57 continue; // Non-websafe state is not considered browsing data.
58 result
.push_back(origin
);
60 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
61 base::Bind(callback
, result
));
64 void BrowsingDataIndexedDBHelper::DeleteIndexedDBInIndexedDBThread(
66 DCHECK(indexed_db_context_
->TaskRunner()->RunsTasksOnCurrentThread());
67 indexed_db_context_
->DeleteForOrigin(origin
);
70 CannedBrowsingDataIndexedDBHelper::
71 PendingIndexedDBInfo::PendingIndexedDBInfo(const GURL
& origin
,
72 const base::string16
& name
)
77 CannedBrowsingDataIndexedDBHelper::
78 PendingIndexedDBInfo::~PendingIndexedDBInfo() {
81 bool CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo::operator<(
82 const PendingIndexedDBInfo
& other
) const {
83 if (origin
== other
.origin
)
84 return name
< other
.name
;
85 return origin
< other
.origin
;
88 CannedBrowsingDataIndexedDBHelper::CannedBrowsingDataIndexedDBHelper(
89 content::IndexedDBContext
* context
)
90 : BrowsingDataIndexedDBHelper(context
) {
93 CannedBrowsingDataIndexedDBHelper::~CannedBrowsingDataIndexedDBHelper() {}
95 void CannedBrowsingDataIndexedDBHelper::AddIndexedDB(
96 const GURL
& origin
, const base::string16
& name
) {
97 if (!BrowsingDataHelper::HasWebScheme(origin
))
98 return; // Non-websafe state is not considered browsing data.
100 pending_indexed_db_info_
.insert(PendingIndexedDBInfo(origin
, name
));
103 void CannedBrowsingDataIndexedDBHelper::Reset() {
104 pending_indexed_db_info_
.clear();
107 bool CannedBrowsingDataIndexedDBHelper::empty() const {
108 return pending_indexed_db_info_
.empty();
111 size_t CannedBrowsingDataIndexedDBHelper::GetIndexedDBCount() const {
112 return pending_indexed_db_info_
.size();
115 const std::set
<CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo
>&
116 CannedBrowsingDataIndexedDBHelper::GetIndexedDBInfo() const {
117 return pending_indexed_db_info_
;
120 void CannedBrowsingDataIndexedDBHelper::StartFetching(
121 const FetchCallback
& callback
) {
122 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
123 DCHECK(!callback
.is_null());
125 std::list
<IndexedDBInfo
> result
;
126 for (const PendingIndexedDBInfo
& pending_info
: pending_indexed_db_info_
) {
127 IndexedDBInfo
info(pending_info
.origin
, 0, base::Time(), 0);
128 result
.push_back(info
);
131 BrowserThread::PostTask(
132 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
135 void CannedBrowsingDataIndexedDBHelper::DeleteIndexedDB(
136 const GURL
& origin
) {
137 for (std::set
<PendingIndexedDBInfo
>::iterator it
=
138 pending_indexed_db_info_
.begin();
139 it
!= pending_indexed_db_info_
.end(); ) {
140 if (it
->origin
== origin
)
141 pending_indexed_db_info_
.erase(it
++);
145 BrowsingDataIndexedDBHelper::DeleteIndexedDB(origin
);