1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/indexed_db/indexed_db_context_impl.h"
#include <vector>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/message_loop_proxy.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "content/browser/indexed_db/indexed_db_quota_client.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/indexed_db_info.h"
#include "content/public/common/content_switches.h"
#include "third_party/WebKit/public/platform/WebCString.h"
#include "third_party/WebKit/public/platform/WebIDBDatabase.h"
#include "third_party/WebKit/public/platform/WebIDBFactory.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "webkit/base/file_path_string_conversions.h"
#include "webkit/base/origin_url_conversions.h"
#include "webkit/browser/database/database_util.h"
#include "webkit/browser/quota/quota_manager.h"
#include "webkit/browser/quota/special_storage_policy.h"
using webkit_database::DatabaseUtil;
using WebKit::WebIDBDatabase;
using WebKit::WebIDBFactory;
namespace content {
const base::FilePath::CharType IndexedDBContextImpl::kIndexedDBDirectory[] =
FILE_PATH_LITERAL("IndexedDB");
const base::FilePath::CharType IndexedDBContextImpl::kIndexedDBExtension[] =
FILE_PATH_LITERAL(".leveldb");
namespace {
void GetAllOriginsAndPaths(
const base::FilePath& indexeddb_path,
std::vector<GURL>* origins,
std::vector<base::FilePath>* file_paths) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
if (indexeddb_path.empty())
return;
file_util::FileEnumerator file_enumerator(indexeddb_path,
false, file_util::FileEnumerator::DIRECTORIES);
for (base::FilePath file_path = file_enumerator.Next(); !file_path.empty();
file_path = file_enumerator.Next()) {
if (file_path.Extension() == IndexedDBContextImpl::kIndexedDBExtension) {
WebKit::WebString origin_id_webstring =
webkit_base::FilePathToWebString(file_path.BaseName());
origins->push_back(
webkit_base::GetOriginURLFromIdentifier(origin_id_webstring));
if (file_paths)
file_paths->push_back(file_path);
}
}
}
// Deletes session-only databases.
void ClearSessionOnlyOrigins(
const base::FilePath& indexeddb_path,
scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
std::vector<GURL> origins;
std::vector<base::FilePath> file_paths;
GetAllOriginsAndPaths(indexeddb_path, &origins, &file_paths);
DCHECK_EQ(origins.size(), file_paths.size());
std::vector<base::FilePath>::const_iterator file_path_iter =
file_paths.begin();
for (std::vector<GURL>::const_iterator iter = origins.begin();
iter != origins.end(); ++iter, ++file_path_iter) {
if (!special_storage_policy->IsStorageSessionOnly(*iter))
continue;
if (special_storage_policy->IsStorageProtected(*iter))
continue;
file_util::Delete(*file_path_iter, true);
}
}
} // namespace
IndexedDBContextImpl::IndexedDBContextImpl(
const base::FilePath& data_path,
quota::SpecialStoragePolicy* special_storage_policy,
quota::QuotaManagerProxy* quota_manager_proxy,
base::MessageLoopProxy* webkit_thread_loop)
: force_keep_session_state_(false),
special_storage_policy_(special_storage_policy),
quota_manager_proxy_(quota_manager_proxy) {
if (!data_path.empty())
data_path_ = data_path.Append(kIndexedDBDirectory);
if (quota_manager_proxy &&
!CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) {
quota_manager_proxy->RegisterClient(
new IndexedDBQuotaClient(webkit_thread_loop, this));
}
}
WebIDBFactory* IndexedDBContextImpl::GetIDBFactory() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
if (!idb_factory_) {
// Prime our cache of origins with existing databases so we can
// detect when dbs are newly created.
GetOriginSet();
idb_factory_.reset(WebIDBFactory::create());
}
return idb_factory_.get();
}
std::vector<GURL> IndexedDBContextImpl::GetAllOrigins() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
std::vector<GURL> origins;
std::set<GURL>* origins_set = GetOriginSet();
for (std::set<GURL>::const_iterator iter = origins_set->begin();
iter != origins_set->end(); ++iter) {
origins.push_back(*iter);
}
return origins;
}
std::vector<IndexedDBInfo> IndexedDBContextImpl::GetAllOriginsInfo() {
std::vector<GURL> origins = GetAllOrigins();
std::vector<IndexedDBInfo> result;
for (std::vector<GURL>::const_iterator iter = origins.begin();
iter != origins.end(); ++iter) {
const GURL& origin_url = *iter;
base::FilePath idb_directory = GetFilePath(origin_url);
result.push_back(IndexedDBInfo(origin_url,
GetOriginDiskUsage(origin_url),
GetOriginLastModified(origin_url),
idb_directory));
}
return result;
}
int64 IndexedDBContextImpl::GetOriginDiskUsage(const GURL& origin_url) {
if (data_path_.empty() || !IsInOriginSet(origin_url))
return 0;
EnsureDiskUsageCacheInitialized(origin_url);
return origin_size_map_[origin_url];
}
base::Time IndexedDBContextImpl::GetOriginLastModified(const GURL& origin_url) {
if (data_path_.empty() || !IsInOriginSet(origin_url))
return base::Time();
base::FilePath idb_directory = GetFilePath(origin_url);
base::PlatformFileInfo file_info;
if (!file_util::GetFileInfo(idb_directory, &file_info))
return base::Time();
return file_info.last_modified;
}
void IndexedDBContextImpl::DeleteForOrigin(const GURL& origin_url) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
ForceClose(origin_url);
if (data_path_.empty() || !IsInOriginSet(origin_url))
return;
base::FilePath idb_directory = GetFilePath(origin_url);
EnsureDiskUsageCacheInitialized(origin_url);
const bool recursive = true;
bool deleted = file_util::Delete(idb_directory, recursive);
QueryDiskAndUpdateQuotaUsage(origin_url);
if (deleted) {
RemoveFromOriginSet(origin_url);
origin_size_map_.erase(origin_url);
space_available_map_.erase(origin_url);
}
}
void IndexedDBContextImpl::ForceClose(const GURL& origin_url) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
if (data_path_.empty() || !IsInOriginSet(origin_url))
return;
if (connections_.find(origin_url) != connections_.end()) {
ConnectionSet& connections = connections_[origin_url];
ConnectionSet::iterator it = connections.begin();
while (it != connections.end()) {
// Remove before closing so callbacks don't double-erase
WebKit::WebIDBDatabase* db = *it;
connections.erase(it++);
db->forceClose();
}
DCHECK_EQ(connections_[origin_url].size(), 0UL);
connections_.erase(origin_url);
}
}
base::FilePath IndexedDBContextImpl::GetFilePath(const GURL& origin_url) {
base::string16 origin_id =
webkit_base::GetOriginIdentifierFromURL(origin_url);
return GetIndexedDBFilePath(origin_id);
}
base::FilePath IndexedDBContextImpl::GetFilePathForTesting(
const string16& origin_id) const {
return GetIndexedDBFilePath(origin_id);
}
void IndexedDBContextImpl::ConnectionOpened(const GURL& origin_url,
WebIDBDatabase* connection) {
DCHECK_EQ(connections_[origin_url].count(connection), 0UL);
if (quota_manager_proxy()) {
quota_manager_proxy()->NotifyStorageAccessed(
quota::QuotaClient::kIndexedDatabase, origin_url,
quota::kStorageTypeTemporary);
}
connections_[origin_url].insert(connection);
if (AddToOriginSet(origin_url)) {
// A newly created db, notify the quota system.
QueryDiskAndUpdateQuotaUsage(origin_url);
} else {
EnsureDiskUsageCacheInitialized(origin_url);
}
QueryAvailableQuota(origin_url);
}
void IndexedDBContextImpl::ConnectionClosed(const GURL& origin_url,
WebIDBDatabase* connection) {
// May not be in the map if connection was forced to close
if (connections_.find(origin_url) == connections_.end() ||
connections_[origin_url].count(connection) != 1)
return;
if (quota_manager_proxy()) {
quota_manager_proxy()->NotifyStorageAccessed(
quota::QuotaClient::kIndexedDatabase, origin_url,
quota::kStorageTypeTemporary);
}
connections_[origin_url].erase(connection);
if (connections_[origin_url].size() == 0) {
QueryDiskAndUpdateQuotaUsage(origin_url);
connections_.erase(origin_url);
}
}
void IndexedDBContextImpl::TransactionComplete(const GURL& origin_url) {
DCHECK(connections_.find(origin_url) != connections_.end() &&
connections_[origin_url].size() > 0);
QueryDiskAndUpdateQuotaUsage(origin_url);
QueryAvailableQuota(origin_url);
}
bool IndexedDBContextImpl::WouldBeOverQuota(const GURL& origin_url,
int64 additional_bytes) {
if (space_available_map_.find(origin_url) == space_available_map_.end()) {
// We haven't heard back from the QuotaManager yet, just let it through.
return false;
}
bool over_quota = additional_bytes > space_available_map_[origin_url];
return over_quota;
}
bool IndexedDBContextImpl::IsOverQuota(const GURL& origin_url) {
const int kOneAdditionalByte = 1;
return WouldBeOverQuota(origin_url, kOneAdditionalByte);
}
quota::QuotaManagerProxy* IndexedDBContextImpl::quota_manager_proxy() {
return quota_manager_proxy_;
}
IndexedDBContextImpl::~IndexedDBContextImpl() {
WebKit::WebIDBFactory* factory = idb_factory_.release();
if (factory) {
if (!BrowserThread::DeleteSoon(BrowserThread::WEBKIT_DEPRECATED,
FROM_HERE, factory))
delete factory;
}
if (data_path_.empty())
return;
if (force_keep_session_state_)
return;
bool has_session_only_databases =
special_storage_policy_.get() &&
special_storage_policy_->HasSessionOnlyOrigins();
// Clearning only session-only databases, and there are none.
if (!has_session_only_databases)
return;
// No WEBKIT thread here means we are running in a unit test where no clean
// up is needed.
BrowserThread::PostTask(
BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
base::Bind(&ClearSessionOnlyOrigins,
data_path_,
special_storage_policy_));
}
base::FilePath IndexedDBContextImpl::GetIndexedDBFilePath(
const string16& origin_id) const {
DCHECK(!data_path_.empty());
base::FilePath::StringType id =
webkit_base::WebStringToFilePathString(origin_id).append(
FILE_PATH_LITERAL(".indexeddb"));
return data_path_.Append(id.append(kIndexedDBExtension));
}
int64 IndexedDBContextImpl::ReadUsageFromDisk(const GURL& origin_url) const {
if (data_path_.empty())
return 0;
base::string16 origin_id =
webkit_base::GetOriginIdentifierFromURL(origin_url);
base::FilePath file_path = GetIndexedDBFilePath(origin_id);
return file_util::ComputeDirectorySize(file_path);
}
void IndexedDBContextImpl::EnsureDiskUsageCacheInitialized(
const GURL& origin_url) {
if (origin_size_map_.find(origin_url) == origin_size_map_.end())
origin_size_map_[origin_url] = ReadUsageFromDisk(origin_url);
}
void IndexedDBContextImpl::QueryDiskAndUpdateQuotaUsage(
const GURL& origin_url) {
int64 former_disk_usage = origin_size_map_[origin_url];
int64 current_disk_usage = ReadUsageFromDisk(origin_url);
int64 difference = current_disk_usage - former_disk_usage;
if (difference) {
origin_size_map_[origin_url] = current_disk_usage;
// quota_manager_proxy() is NULL in unit tests.
if (quota_manager_proxy())
quota_manager_proxy()->NotifyStorageModified(
quota::QuotaClient::kIndexedDatabase,
origin_url,
quota::kStorageTypeTemporary,
difference);
}
}
void IndexedDBContextImpl::GotUsageAndQuota(const GURL& origin_url,
quota::QuotaStatusCode status,
int64 usage, int64 quota) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(status == quota::kQuotaStatusOk || status == quota::kQuotaErrorAbort)
<< "status was " << status;
if (status == quota::kQuotaErrorAbort) {
// We seem to no longer care to wait around for the answer.
return;
}
BrowserThread::PostTask(
BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
base::Bind(&IndexedDBContextImpl::GotUpdatedQuota, this, origin_url,
usage, quota));
}
void IndexedDBContextImpl::GotUpdatedQuota(const GURL& origin_url, int64 usage,
int64 quota) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
space_available_map_[origin_url] = quota - usage;
}
void IndexedDBContextImpl::QueryAvailableQuota(const GURL& origin_url) {
if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
if (quota_manager_proxy())
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&IndexedDBContextImpl::QueryAvailableQuota, this,
origin_url));
return;
}
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!quota_manager_proxy() || !quota_manager_proxy()->quota_manager())
return;
quota_manager_proxy()->quota_manager()->GetUsageAndQuota(
origin_url,
quota::kStorageTypeTemporary,
base::Bind(&IndexedDBContextImpl::GotUsageAndQuota, this, origin_url));
}
std::set<GURL>* IndexedDBContextImpl::GetOriginSet() {
if (!origin_set_) {
origin_set_.reset(new std::set<GURL>);
std::vector<GURL> origins;
GetAllOriginsAndPaths(data_path_, &origins, NULL);
for (std::vector<GURL>::const_iterator iter = origins.begin();
iter != origins.end(); ++iter) {
origin_set_->insert(*iter);
}
}
return origin_set_.get();
}
void IndexedDBContextImpl::ResetCaches() {
origin_set_.reset();
origin_size_map_.clear();
space_available_map_.clear();
}
} // namespace content
|