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
|
// Copyright (c) 2013 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_factory.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "content/browser/indexed_db/indexed_db_backing_store.h"
#include "content/browser/indexed_db/indexed_db_context_impl.h"
#include "content/browser/indexed_db/indexed_db_tracing.h"
#include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
#include "third_party/WebKit/public/platform/WebIDBDatabaseException.h"
#include "webkit/common/database/database_identifier.h"
namespace content {
const int64 kBackingStoreGracePeriodMs = 2000;
IndexedDBFactory::IndexedDBFactory(IndexedDBContextImpl* context)
: context_(context) {}
IndexedDBFactory::~IndexedDBFactory() {}
void IndexedDBFactory::ReleaseDatabase(
const IndexedDBDatabase::Identifier& identifier,
bool forcedClose) {
IndexedDBDatabaseMap::iterator it = database_map_.find(identifier);
DCHECK(it != database_map_.end());
DCHECK(!it->second->backing_store());
database_map_.erase(it);
// No grace period on a forced-close, as the initiator is
// assuming the backing store will be released once all
// connections are closed.
ReleaseBackingStore(identifier.first, forcedClose);
}
void IndexedDBFactory::ReleaseBackingStore(const GURL& origin_url,
bool immediate) {
// Only close if this is the last reference.
if (!HasLastBackingStoreReference(origin_url))
return;
// If this factory does hold the last reference to the backing store, it can
// be closed - but unless requested to close it immediately, keep it around
// for a short period so that a re-open is fast.
if (immediate) {
CloseBackingStore(origin_url);
return;
}
// Start a timer to close the backing store, unless something else opens it
// in the mean time.
DCHECK(!backing_store_map_[origin_url]->close_timer()->IsRunning());
backing_store_map_[origin_url]->close_timer()->Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kBackingStoreGracePeriodMs),
base::Bind(&IndexedDBFactory::MaybeCloseBackingStore, this, origin_url));
}
void IndexedDBFactory::MaybeCloseBackingStore(const GURL& origin_url) {
// Another reference may have opened since the maybe-close was posted, so it
// is necessary to check again.
if (HasLastBackingStoreReference(origin_url))
CloseBackingStore(origin_url);
}
void IndexedDBFactory::CloseBackingStore(const GURL& origin_url) {
IndexedDBBackingStoreMap::iterator it = backing_store_map_.find(origin_url);
DCHECK(it != backing_store_map_.end());
// Stop the timer (if it's running) - this may happen if the timer was started
// and then a forced close occurs.
it->second->close_timer()->Stop();
backing_store_map_.erase(it);
}
bool IndexedDBFactory::HasLastBackingStoreReference(const GURL& origin_url)
const {
IndexedDBBackingStore* ptr;
{
// Scope so that the implicit scoped_refptr<> is freed.
IndexedDBBackingStoreMap::const_iterator it =
backing_store_map_.find(origin_url);
DCHECK(it != backing_store_map_.end());
ptr = it->second.get();
}
return ptr->HasOneRef();
}
void IndexedDBFactory::ForceClose(const GURL& origin_url) {
if (backing_store_map_.find(origin_url) != backing_store_map_.end())
ReleaseBackingStore(origin_url, true /* immediate */);
}
void IndexedDBFactory::ContextDestroyed() {
// Timers on backing stores hold a reference to this factory. When the
// context (which nominally owns this factory) is destroyed during thread
// termination the timers must be stopped so that this factory and the
// stores can be disposed of.
for (IndexedDBBackingStoreMap::iterator it = backing_store_map_.begin();
it != backing_store_map_.end();
++it)
it->second->close_timer()->Stop();
backing_store_map_.clear();
context_ = NULL;
}
void IndexedDBFactory::GetDatabaseNames(
scoped_refptr<IndexedDBCallbacks> callbacks,
const GURL& origin_url,
const base::FilePath& data_directory) {
IDB_TRACE("IndexedDBFactory::GetDatabaseNames");
// TODO(dgrogan): Plumb data_loss back to script eventually?
blink::WebIDBDataLoss data_loss;
std::string data_loss_message;
bool disk_full;
scoped_refptr<IndexedDBBackingStore> backing_store =
OpenBackingStore(origin_url,
data_directory,
&data_loss,
&data_loss_message,
&disk_full);
if (!backing_store) {
callbacks->OnError(
IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError,
"Internal error opening backing store for "
"indexedDB.webkitGetDatabaseNames."));
return;
}
callbacks->OnSuccess(backing_store->GetDatabaseNames());
ReleaseBackingStore(origin_url, false /* immediate */);
}
void IndexedDBFactory::DeleteDatabase(
const base::string16& name,
scoped_refptr<IndexedDBCallbacks> callbacks,
const GURL& origin_url,
const base::FilePath& data_directory) {
IDB_TRACE("IndexedDBFactory::DeleteDatabase");
IndexedDBDatabase::Identifier unique_identifier(origin_url, name);
IndexedDBDatabaseMap::iterator it = database_map_.find(unique_identifier);
if (it != database_map_.end()) {
// If there are any connections to the database, directly delete the
// database.
it->second->DeleteDatabase(callbacks);
return;
}
// TODO(dgrogan): Plumb data_loss back to script eventually?
blink::WebIDBDataLoss data_loss;
std::string data_loss_message;
bool disk_full = false;
scoped_refptr<IndexedDBBackingStore> backing_store =
OpenBackingStore(origin_url,
data_directory,
&data_loss,
&data_loss_message,
&disk_full);
if (!backing_store) {
callbacks->OnError(
IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError,
ASCIIToUTF16(
"Internal error opening backing store "
"for indexedDB.deleteDatabase.")));
return;
}
scoped_refptr<IndexedDBDatabase> database =
IndexedDBDatabase::Create(name, backing_store, this, unique_identifier);
if (!database) {
callbacks->OnError(IndexedDBDatabaseError(
blink::WebIDBDatabaseExceptionUnknownError,
ASCIIToUTF16(
"Internal error creating database backend for "
"indexedDB.deleteDatabase.")));
return;
}
database_map_[unique_identifier] = database;
database->DeleteDatabase(callbacks);
database_map_.erase(unique_identifier);
ReleaseBackingStore(origin_url, false /* immediate */);
}
void IndexedDBFactory::DatabaseDeleted(
const IndexedDBDatabase::Identifier& identifier) {
// NULL after ContextDestroyed() called, and in some unit tests.
if (!context_)
return;
context_->DatabaseDeleted(identifier.first);
}
void IndexedDBFactory::HandleBackingStoreFailure(const GURL& origin_url) {
// NULL after ContextDestroyed() called, and in some unit tests.
if (!context_)
return;
context_->ForceClose(origin_url);
}
bool IndexedDBFactory::IsDatabaseOpen(const GURL& origin_url,
const base::string16& name) const {
return !!database_map_.count(IndexedDBDatabase::Identifier(origin_url, name));
}
bool IndexedDBFactory::IsBackingStoreOpen(const GURL& origin_url) const {
return backing_store_map_.find(origin_url) != backing_store_map_.end();
}
bool IndexedDBFactory::IsBackingStorePendingClose(const GURL& origin_url)
const {
IndexedDBBackingStoreMap::const_iterator it =
backing_store_map_.find(origin_url);
if (it == backing_store_map_.end())
return false;
return it->second->close_timer()->IsRunning();
}
scoped_refptr<IndexedDBBackingStore> IndexedDBFactory::OpenBackingStore(
const GURL& origin_url,
const base::FilePath& data_directory,
blink::WebIDBDataLoss* data_loss,
std::string* data_loss_message,
bool* disk_full) {
const bool open_in_memory = data_directory.empty();
IndexedDBBackingStoreMap::iterator it2 = backing_store_map_.find(origin_url);
if (it2 != backing_store_map_.end()) {
it2->second->close_timer()->Stop();
return it2->second;
}
scoped_refptr<IndexedDBBackingStore> backing_store;
if (open_in_memory) {
backing_store = IndexedDBBackingStore::OpenInMemory(origin_url);
} else {
backing_store = IndexedDBBackingStore::Open(origin_url,
data_directory,
data_loss,
data_loss_message,
disk_full);
}
if (backing_store.get()) {
backing_store_map_[origin_url] = backing_store;
// If an in-memory database, bind lifetime to this factory instance.
if (open_in_memory)
session_only_backing_stores_.insert(backing_store);
// All backing stores associated with this factory should be of the same
// type.
DCHECK(session_only_backing_stores_.empty() || open_in_memory);
return backing_store;
}
return 0;
}
void IndexedDBFactory::Open(
const base::string16& name,
int64 version,
int64 transaction_id,
scoped_refptr<IndexedDBCallbacks> callbacks,
scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
const GURL& origin_url,
const base::FilePath& data_directory) {
IDB_TRACE("IndexedDBFactory::Open");
scoped_refptr<IndexedDBDatabase> database;
IndexedDBDatabase::Identifier unique_identifier(origin_url, name);
IndexedDBDatabaseMap::iterator it = database_map_.find(unique_identifier);
blink::WebIDBDataLoss data_loss =
blink::WebIDBDataLossNone;
std::string data_loss_message;
bool disk_full = false;
bool was_open = (it != database_map_.end());
if (!was_open) {
scoped_refptr<IndexedDBBackingStore> backing_store =
OpenBackingStore(origin_url,
data_directory,
&data_loss,
&data_loss_message,
&disk_full);
if (!backing_store) {
if (disk_full) {
callbacks->OnError(
IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionQuotaError,
ASCIIToUTF16(
"Encountered full disk while opening "
"backing store for indexedDB.open.")));
return;
}
callbacks->OnError(IndexedDBDatabaseError(
blink::WebIDBDatabaseExceptionUnknownError,
ASCIIToUTF16(
"Internal error opening backing store for indexedDB.open.")));
return;
}
database =
IndexedDBDatabase::Create(name, backing_store, this, unique_identifier);
if (!database) {
callbacks->OnError(IndexedDBDatabaseError(
blink::WebIDBDatabaseExceptionUnknownError,
ASCIIToUTF16(
"Internal error creating database backend for indexedDB.open.")));
return;
}
} else {
database = it->second;
}
database->OpenConnection(callbacks,
database_callbacks,
transaction_id,
version,
data_loss,
data_loss_message);
if (!was_open && database->ConnectionCount() > 0)
database_map_[unique_identifier] = database;
}
std::vector<IndexedDBDatabase*> IndexedDBFactory::GetOpenDatabasesForOrigin(
const GURL& origin_url) const {
std::vector<IndexedDBDatabase*> result;
for (IndexedDBDatabaseMap::const_iterator it = database_map_.begin();
it != database_map_.end();
++it) {
if (it->first.first == origin_url)
result.push_back(it->second.get());
}
return result;
}
} // namespace content
|