1 // Copyright (c) 2013 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 "content/browser/indexed_db/indexed_db_factory_impl.h"
10 #include "base/logging.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "content/browser/indexed_db/indexed_db_backing_store.h"
14 #include "content/browser/indexed_db/indexed_db_context_impl.h"
15 #include "content/browser/indexed_db/indexed_db_database_error.h"
16 #include "content/browser/indexed_db/indexed_db_tracing.h"
17 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
18 #include "storage/common/database/database_identifier.h"
19 #include "third_party/WebKit/public/platform/WebIDBDatabaseException.h"
20 #include "third_party/leveldatabase/env_chromium.h"
22 using base::ASCIIToUTF16
;
26 const int64 kBackingStoreGracePeriodMs
= 2000;
28 IndexedDBFactoryImpl::IndexedDBFactoryImpl(IndexedDBContextImpl
* context
)
32 IndexedDBFactoryImpl::~IndexedDBFactoryImpl() {
35 void IndexedDBFactoryImpl::RemoveDatabaseFromMaps(
36 const IndexedDBDatabase::Identifier
& identifier
) {
37 IndexedDBDatabaseMap::iterator it
= database_map_
.find(identifier
);
38 DCHECK(it
!= database_map_
.end());
39 IndexedDBDatabase
* database
= it
->second
;
40 database_map_
.erase(it
);
42 std::pair
<OriginDBMap::iterator
, OriginDBMap::iterator
> range
=
43 origin_dbs_
.equal_range(database
->identifier().first
);
44 DCHECK(range
.first
!= range
.second
);
45 for (OriginDBMap::iterator it2
= range
.first
; it2
!= range
.second
; ++it2
) {
46 if (it2
->second
== database
) {
47 origin_dbs_
.erase(it2
);
53 void IndexedDBFactoryImpl::ReleaseDatabase(
54 const IndexedDBDatabase::Identifier
& identifier
,
56 DCHECK(!database_map_
.find(identifier
)->second
->backing_store());
58 RemoveDatabaseFromMaps(identifier
);
60 // No grace period on a forced-close, as the initiator is
61 // assuming the backing store will be released once all
62 // connections are closed.
63 ReleaseBackingStore(identifier
.first
, forced_close
);
66 void IndexedDBFactoryImpl::ReleaseBackingStore(const GURL
& origin_url
,
69 IndexedDBBackingStoreMap::iterator it
=
70 backing_stores_with_active_blobs_
.find(origin_url
);
71 if (it
!= backing_stores_with_active_blobs_
.end()) {
72 it
->second
->active_blob_registry()->ForceShutdown();
73 backing_stores_with_active_blobs_
.erase(it
);
77 // Only close if this is the last reference.
78 if (!HasLastBackingStoreReference(origin_url
))
81 // If this factory does hold the last reference to the backing store, it can
82 // be closed - but unless requested to close it immediately, keep it around
83 // for a short period so that a re-open is fast.
85 CloseBackingStore(origin_url
);
89 // Start a timer to close the backing store, unless something else opens it
91 DCHECK(!backing_store_map_
[origin_url
]->close_timer()->IsRunning());
92 backing_store_map_
[origin_url
]->close_timer()->Start(
94 base::TimeDelta::FromMilliseconds(kBackingStoreGracePeriodMs
),
96 &IndexedDBFactoryImpl::MaybeCloseBackingStore
, this, origin_url
));
99 void IndexedDBFactoryImpl::MaybeCloseBackingStore(const GURL
& origin_url
) {
100 // Another reference may have opened since the maybe-close was posted, so it
101 // is necessary to check again.
102 if (HasLastBackingStoreReference(origin_url
))
103 CloseBackingStore(origin_url
);
106 void IndexedDBFactoryImpl::CloseBackingStore(const GURL
& origin_url
) {
107 IndexedDBBackingStoreMap::iterator it
= backing_store_map_
.find(origin_url
);
108 DCHECK(it
!= backing_store_map_
.end());
109 // Stop the timer (if it's running) - this may happen if the timer was started
110 // and then a forced close occurs.
111 it
->second
->close_timer()->Stop();
112 backing_store_map_
.erase(it
);
115 bool IndexedDBFactoryImpl::HasLastBackingStoreReference(
116 const GURL
& origin_url
) const {
117 IndexedDBBackingStore
* ptr
;
119 // Scope so that the implicit scoped_refptr<> is freed.
120 IndexedDBBackingStoreMap::const_iterator it
=
121 backing_store_map_
.find(origin_url
);
122 DCHECK(it
!= backing_store_map_
.end());
123 ptr
= it
->second
.get();
125 return ptr
->HasOneRef();
128 void IndexedDBFactoryImpl::ForceClose(const GURL
& origin_url
) {
129 OriginDBs range
= GetOpenDatabasesForOrigin(origin_url
);
131 while (range
.first
!= range
.second
) {
132 IndexedDBDatabase
* db
= range
.first
->second
;
137 if (backing_store_map_
.find(origin_url
) != backing_store_map_
.end())
138 ReleaseBackingStore(origin_url
, true /* immediate */);
141 void IndexedDBFactoryImpl::ContextDestroyed() {
142 // Timers on backing stores hold a reference to this factory. When the
143 // context (which nominally owns this factory) is destroyed during thread
144 // termination the timers must be stopped so that this factory and the
145 // stores can be disposed of.
146 for (const auto& it
: backing_store_map_
)
147 it
.second
->close_timer()->Stop();
148 backing_store_map_
.clear();
149 backing_stores_with_active_blobs_
.clear();
153 void IndexedDBFactoryImpl::ReportOutstandingBlobs(const GURL
& origin_url
,
154 bool blobs_outstanding
) {
157 if (blobs_outstanding
) {
158 DCHECK(!backing_stores_with_active_blobs_
.count(origin_url
));
159 IndexedDBBackingStoreMap::iterator it
= backing_store_map_
.find(origin_url
);
160 if (it
!= backing_store_map_
.end())
161 backing_stores_with_active_blobs_
.insert(*it
);
165 IndexedDBBackingStoreMap::iterator it
=
166 backing_stores_with_active_blobs_
.find(origin_url
);
167 if (it
!= backing_stores_with_active_blobs_
.end()) {
168 backing_stores_with_active_blobs_
.erase(it
);
169 ReleaseBackingStore(origin_url
, false /* immediate */);
174 void IndexedDBFactoryImpl::GetDatabaseNames(
175 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
176 const GURL
& origin_url
,
177 const base::FilePath
& data_directory
,
178 net::URLRequestContext
* request_context
) {
179 IDB_TRACE("IndexedDBFactoryImpl::GetDatabaseNames");
180 // TODO(dgrogan): Plumb data_loss back to script eventually?
181 blink::WebIDBDataLoss data_loss
;
182 std::string data_loss_message
;
185 // TODO(cmumford): Handle this error
186 scoped_refptr
<IndexedDBBackingStore
> backing_store
=
187 OpenBackingStore(origin_url
,
194 if (!backing_store
.get()) {
196 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError
,
197 "Internal error opening backing store for "
198 "indexedDB.webkitGetDatabaseNames."));
202 std::vector
<base::string16
> names
= backing_store
->GetDatabaseNames(&s
);
204 DLOG(ERROR
) << "Internal error getting database names";
205 IndexedDBDatabaseError
error(blink::WebIDBDatabaseExceptionUnknownError
,
206 "Internal error opening backing store for "
207 "indexedDB.webkitGetDatabaseNames.");
208 callbacks
->OnError(error
);
209 backing_store
= NULL
;
210 if (s
.IsCorruption())
211 HandleBackingStoreCorruption(origin_url
, error
);
214 callbacks
->OnSuccess(names
);
215 backing_store
= NULL
;
216 ReleaseBackingStore(origin_url
, false /* immediate */);
219 void IndexedDBFactoryImpl::DeleteDatabase(
220 const base::string16
& name
,
221 net::URLRequestContext
* request_context
,
222 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
223 const GURL
& origin_url
,
224 const base::FilePath
& data_directory
) {
225 IDB_TRACE("IndexedDBFactoryImpl::DeleteDatabase");
226 IndexedDBDatabase::Identifier
unique_identifier(origin_url
, name
);
227 IndexedDBDatabaseMap::iterator it
= database_map_
.find(unique_identifier
);
228 if (it
!= database_map_
.end()) {
229 // If there are any connections to the database, directly delete the
231 it
->second
->DeleteDatabase(callbacks
);
235 // TODO(dgrogan): Plumb data_loss back to script eventually?
236 blink::WebIDBDataLoss data_loss
;
237 std::string data_loss_message
;
238 bool disk_full
= false;
240 scoped_refptr
<IndexedDBBackingStore
> backing_store
=
241 OpenBackingStore(origin_url
,
248 if (!backing_store
.get()) {
249 IndexedDBDatabaseError
error(blink::WebIDBDatabaseExceptionUnknownError
,
251 "Internal error opening backing store "
252 "for indexedDB.deleteDatabase."));
253 callbacks
->OnError(error
);
254 if (s
.IsCorruption()) {
255 HandleBackingStoreCorruption(origin_url
, error
);
260 scoped_refptr
<IndexedDBDatabase
> database
= IndexedDBDatabase::Create(
261 name
, backing_store
.get(), this, unique_identifier
, &s
);
262 if (!database
.get()) {
263 IndexedDBDatabaseError
error(
264 blink::WebIDBDatabaseExceptionUnknownError
,
266 "Internal error creating database backend for "
267 "indexedDB.deleteDatabase."));
268 callbacks
->OnError(error
);
269 if (s
.IsCorruption()) {
270 backing_store
= NULL
;
271 HandleBackingStoreCorruption(origin_url
, error
);
276 database_map_
[unique_identifier
] = database
.get();
277 origin_dbs_
.insert(std::make_pair(origin_url
, database
.get()));
278 database
->DeleteDatabase(callbacks
);
279 RemoveDatabaseFromMaps(unique_identifier
);
281 backing_store
= NULL
;
282 ReleaseBackingStore(origin_url
, false /* immediate */);
285 void IndexedDBFactoryImpl::DatabaseDeleted(
286 const IndexedDBDatabase::Identifier
& identifier
) {
287 // NULL after ContextDestroyed() called, and in some unit tests.
290 context_
->DatabaseDeleted(identifier
.first
);
293 void IndexedDBFactoryImpl::HandleBackingStoreFailure(const GURL
& origin_url
) {
294 // NULL after ContextDestroyed() called, and in some unit tests.
297 context_
->ForceClose(origin_url
,
298 IndexedDBContextImpl::FORCE_CLOSE_BACKING_STORE_FAILURE
);
301 void IndexedDBFactoryImpl::HandleBackingStoreCorruption(
302 const GURL
& origin_url
,
303 const IndexedDBDatabaseError
& error
) {
304 // Make a copy of origin_url as this is likely a reference to a member of a
305 // backing store which this function will be deleting.
306 GURL
saved_origin_url(origin_url
);
308 base::FilePath path_base
= context_
->data_path();
309 IndexedDBBackingStore::RecordCorruptionInfo(
310 path_base
, saved_origin_url
, base::UTF16ToUTF8(error
.message()));
311 HandleBackingStoreFailure(saved_origin_url
);
312 // Note: DestroyBackingStore only deletes LevelDB files, leaving all others,
313 // so our corruption info file will remain.
315 IndexedDBBackingStore::DestroyBackingStore(path_base
, saved_origin_url
);
317 DLOG(ERROR
) << "Unable to delete backing store: " << s
.ToString();
320 bool IndexedDBFactoryImpl::IsDatabaseOpen(const GURL
& origin_url
,
321 const base::string16
& name
) const {
322 return !!database_map_
.count(IndexedDBDatabase::Identifier(origin_url
, name
));
325 bool IndexedDBFactoryImpl::IsBackingStoreOpen(const GURL
& origin_url
) const {
326 return backing_store_map_
.find(origin_url
) != backing_store_map_
.end();
329 bool IndexedDBFactoryImpl::IsBackingStorePendingClose(
330 const GURL
& origin_url
) const {
331 IndexedDBBackingStoreMap::const_iterator it
=
332 backing_store_map_
.find(origin_url
);
333 if (it
== backing_store_map_
.end())
335 return it
->second
->close_timer()->IsRunning();
338 scoped_refptr
<IndexedDBBackingStore
>
339 IndexedDBFactoryImpl::OpenBackingStoreHelper(
340 const GURL
& origin_url
,
341 const base::FilePath
& data_directory
,
342 net::URLRequestContext
* request_context
,
343 blink::WebIDBDataLoss
* data_loss
,
344 std::string
* data_loss_message
,
347 leveldb::Status
* status
) {
348 return IndexedDBBackingStore::Open(this,
355 context_
->TaskRunner(),
360 scoped_refptr
<IndexedDBBackingStore
> IndexedDBFactoryImpl::OpenBackingStore(
361 const GURL
& origin_url
,
362 const base::FilePath
& data_directory
,
363 net::URLRequestContext
* request_context
,
364 blink::WebIDBDataLoss
* data_loss
,
365 std::string
* data_loss_message
,
367 leveldb::Status
* status
) {
368 const bool open_in_memory
= data_directory
.empty();
370 IndexedDBBackingStoreMap::iterator it2
= backing_store_map_
.find(origin_url
);
371 if (it2
!= backing_store_map_
.end()) {
372 it2
->second
->close_timer()->Stop();
376 scoped_refptr
<IndexedDBBackingStore
> backing_store
;
377 bool first_time
= false;
378 if (open_in_memory
) {
379 backing_store
= IndexedDBBackingStore::OpenInMemory(
380 origin_url
, context_
->TaskRunner(), status
);
382 first_time
= !backends_opened_since_boot_
.count(origin_url
);
384 backing_store
= OpenBackingStoreHelper(origin_url
,
394 if (backing_store
.get()) {
396 backends_opened_since_boot_
.insert(origin_url
);
397 backing_store_map_
[origin_url
] = backing_store
;
398 // If an in-memory database, bind lifetime to this factory instance.
400 session_only_backing_stores_
.insert(backing_store
);
402 // All backing stores associated with this factory should be of the same
404 DCHECK_NE(session_only_backing_stores_
.empty(), open_in_memory
);
406 return backing_store
;
412 void IndexedDBFactoryImpl::Open(const base::string16
& name
,
413 const IndexedDBPendingConnection
& connection
,
414 net::URLRequestContext
* request_context
,
415 const GURL
& origin_url
,
416 const base::FilePath
& data_directory
) {
417 IDB_TRACE("IndexedDBFactoryImpl::Open");
418 scoped_refptr
<IndexedDBDatabase
> database
;
419 IndexedDBDatabase::Identifier
unique_identifier(origin_url
, name
);
420 IndexedDBDatabaseMap::iterator it
= database_map_
.find(unique_identifier
);
421 blink::WebIDBDataLoss data_loss
=
422 blink::WebIDBDataLossNone
;
423 std::string data_loss_message
;
424 bool disk_full
= false;
425 bool was_open
= (it
!= database_map_
.end());
428 scoped_refptr
<IndexedDBBackingStore
> backing_store
=
429 OpenBackingStore(origin_url
,
436 if (!backing_store
.get()) {
438 connection
.callbacks
->OnError(
439 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionQuotaError
,
441 "Encountered full disk while opening "
442 "backing store for indexedDB.open.")));
445 IndexedDBDatabaseError
error(blink::WebIDBDatabaseExceptionUnknownError
,
447 "Internal error opening backing store"
448 " for indexedDB.open."));
449 connection
.callbacks
->OnError(error
);
450 if (s
.IsCorruption()) {
451 HandleBackingStoreCorruption(origin_url
, error
);
456 database
= IndexedDBDatabase::Create(
457 name
, backing_store
.get(), this, unique_identifier
, &s
);
458 if (!database
.get()) {
459 DLOG(ERROR
) << "Unable to create the database";
460 IndexedDBDatabaseError
error(blink::WebIDBDatabaseExceptionUnknownError
,
462 "Internal error creating "
463 "database backend for "
465 connection
.callbacks
->OnError(error
);
466 if (s
.IsCorruption()) {
467 backing_store
= NULL
; // Closes the LevelDB so that it can be deleted
468 HandleBackingStoreCorruption(origin_url
, error
);
473 database
= it
->second
;
476 if (data_loss
!= blink::WebIDBDataLossNone
)
477 connection
.callbacks
->OnDataLoss(data_loss
, data_loss_message
);
479 database
->OpenConnection(connection
);
481 if (!was_open
&& database
->ConnectionCount() > 0) {
482 database_map_
[unique_identifier
] = database
.get();
483 origin_dbs_
.insert(std::make_pair(origin_url
, database
.get()));
487 std::pair
<IndexedDBFactoryImpl::OriginDBMapIterator
,
488 IndexedDBFactoryImpl::OriginDBMapIterator
>
489 IndexedDBFactoryImpl::GetOpenDatabasesForOrigin(const GURL
& origin_url
) const {
490 return origin_dbs_
.equal_range(origin_url
);
493 size_t IndexedDBFactoryImpl::GetConnectionCount(const GURL
& origin_url
) const {
496 OriginDBs range
= GetOpenDatabasesForOrigin(origin_url
);
497 for (OriginDBMapIterator it
= range
.first
; it
!= range
.second
; ++it
)
498 count
+= it
->second
->ConnectionCount();
503 } // namespace content