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_database_helper.h"
10 #include "base/callback.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/location.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/browsing_data/browsing_data_helper.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/storage_partition.h"
19 #include "net/base/completion_callback.h"
20 #include "net/base/net_errors.h"
21 #include "storage/common/database/database_identifier.h"
23 using content::BrowserContext
;
24 using content::BrowserThread
;
25 using storage::DatabaseIdentifier
;
27 BrowsingDataDatabaseHelper::DatabaseInfo::DatabaseInfo(
28 const DatabaseIdentifier
& identifier
,
29 const std::string
& database_name
,
30 const std::string
& description
,
32 base::Time last_modified
)
33 : identifier(identifier
),
34 database_name(database_name
),
35 description(description
),
37 last_modified(last_modified
) {
40 BrowsingDataDatabaseHelper::DatabaseInfo::~DatabaseInfo() {}
42 BrowsingDataDatabaseHelper::BrowsingDataDatabaseHelper(Profile
* profile
)
43 : tracker_(BrowserContext::GetDefaultStoragePartition(profile
)
44 ->GetDatabaseTracker()) {}
46 BrowsingDataDatabaseHelper::~BrowsingDataDatabaseHelper() {
49 void BrowsingDataDatabaseHelper::StartFetching(const FetchCallback
& callback
) {
50 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
51 DCHECK(!callback
.is_null());
53 BrowserThread::PostTask(
54 BrowserThread::FILE, FROM_HERE
,
55 base::Bind(&BrowsingDataDatabaseHelper::FetchDatabaseInfoOnFileThread
,
59 void BrowsingDataDatabaseHelper::DeleteDatabase(const std::string
& origin
,
60 const std::string
& name
) {
61 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
62 BrowserThread::PostTask(
63 BrowserThread::FILE, FROM_HERE
,
64 base::Bind(&BrowsingDataDatabaseHelper::DeleteDatabaseOnFileThread
, this,
68 void BrowsingDataDatabaseHelper::FetchDatabaseInfoOnFileThread(
69 const FetchCallback
& callback
) {
70 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
71 DCHECK(!callback
.is_null());
73 std::list
<DatabaseInfo
> result
;
74 std::vector
<storage::OriginInfo
> origins_info
;
75 if (tracker_
.get() && tracker_
->GetAllOriginsInfo(&origins_info
)) {
76 for (const storage::OriginInfo
& origin
: origins_info
) {
77 DatabaseIdentifier identifier
=
78 DatabaseIdentifier::Parse(origin
.GetOriginIdentifier());
79 if (!BrowsingDataHelper::HasWebScheme(identifier
.ToOrigin()))
80 continue; // Non-websafe state is not considered browsing data.
81 std::vector
<base::string16
> databases
;
82 origin
.GetAllDatabaseNames(&databases
);
83 for (const base::string16
& db
: databases
) {
84 base::FilePath file_path
=
85 tracker_
->GetFullDBFilePath(origin
.GetOriginIdentifier(), db
);
86 base::File::Info file_info
;
87 if (base::GetFileInfo(file_path
, &file_info
)) {
89 DatabaseInfo(identifier
, base::UTF16ToUTF8(db
),
90 base::UTF16ToUTF8(origin
.GetDatabaseDescription(db
)),
91 file_info
.size
, file_info
.last_modified
));
97 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
98 base::Bind(callback
, result
));
101 void BrowsingDataDatabaseHelper::DeleteDatabaseOnFileThread(
102 const std::string
& origin
,
103 const std::string
& name
) {
104 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
107 tracker_
->DeleteDatabase(origin
, base::UTF8ToUTF16(name
),
108 net::CompletionCallback());
111 CannedBrowsingDataDatabaseHelper::PendingDatabaseInfo::PendingDatabaseInfo(
113 const std::string
& name
,
114 const std::string
& description
)
117 description(description
) {
120 CannedBrowsingDataDatabaseHelper::PendingDatabaseInfo::~PendingDatabaseInfo() {}
122 bool CannedBrowsingDataDatabaseHelper::PendingDatabaseInfo::operator<(
123 const PendingDatabaseInfo
& other
) const {
124 if (origin
== other
.origin
)
125 return name
< other
.name
;
126 return origin
< other
.origin
;
129 CannedBrowsingDataDatabaseHelper::CannedBrowsingDataDatabaseHelper(
131 : BrowsingDataDatabaseHelper(profile
) {
134 void CannedBrowsingDataDatabaseHelper::AddDatabase(
136 const std::string
& name
,
137 const std::string
& description
) {
138 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
139 if (!BrowsingDataHelper::HasWebScheme(origin
))
140 return; // Non-websafe state is not considered browsing data.
141 pending_database_info_
.insert(PendingDatabaseInfo(origin
, name
, description
));
144 void CannedBrowsingDataDatabaseHelper::Reset() {
145 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
146 pending_database_info_
.clear();
149 bool CannedBrowsingDataDatabaseHelper::empty() const {
150 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
151 return pending_database_info_
.empty();
154 size_t CannedBrowsingDataDatabaseHelper::GetDatabaseCount() const {
155 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
156 return pending_database_info_
.size();
159 const std::set
<CannedBrowsingDataDatabaseHelper::PendingDatabaseInfo
>&
160 CannedBrowsingDataDatabaseHelper::GetPendingDatabaseInfo() {
161 return pending_database_info_
;
164 void CannedBrowsingDataDatabaseHelper::StartFetching(
165 const FetchCallback
& callback
) {
166 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
167 DCHECK(!callback
.is_null());
169 std::list
<DatabaseInfo
> result
;
170 for (const PendingDatabaseInfo
& info
: pending_database_info_
) {
171 DatabaseIdentifier identifier
=
172 DatabaseIdentifier::CreateFromOrigin(info
.origin
);
175 DatabaseInfo(identifier
, info
.name
, info
.description
, 0, base::Time()));
178 BrowserThread::PostTask(
179 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
182 void CannedBrowsingDataDatabaseHelper::DeleteDatabase(
183 const std::string
& origin_identifier
,
184 const std::string
& name
) {
186 storage::DatabaseIdentifier::Parse(origin_identifier
).ToOrigin();
187 for (std::set
<PendingDatabaseInfo
>::iterator it
=
188 pending_database_info_
.begin();
189 it
!= pending_database_info_
.end();
191 if (it
->origin
== origin
&& it
->name
== name
) {
192 pending_database_info_
.erase(it
);
196 BrowsingDataDatabaseHelper::DeleteDatabase(origin_identifier
, name
);
199 CannedBrowsingDataDatabaseHelper::~CannedBrowsingDataDatabaseHelper() {}