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 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_
14 #include "base/basictypes.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/strings/string_piece.h"
19 #include "base/time/time.h"
20 #include "base/timer/timer.h"
21 #include "content/browser/indexed_db/indexed_db.h"
22 #include "content/browser/indexed_db/indexed_db_active_blob_registry.h"
23 #include "content/browser/indexed_db/indexed_db_blob_info.h"
24 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h"
25 #include "content/browser/indexed_db/indexed_db_metadata.h"
26 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h"
27 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h"
28 #include "content/common/content_export.h"
29 #include "content/common/indexed_db/indexed_db_key.h"
30 #include "content/common/indexed_db/indexed_db_key_path.h"
31 #include "content/common/indexed_db/indexed_db_key_range.h"
32 #include "storage/browser/blob/blob_data_handle.h"
33 #include "third_party/leveldatabase/src/include/leveldb/status.h"
37 class SequencedTaskRunner
;
41 class FileWriterDelegate
;
45 class URLRequestContext
;
50 class IndexedDBFactory
;
51 class LevelDBComparator
;
52 class LevelDBDatabase
;
54 struct IndexedDBValue
;
56 class CONTENT_EXPORT IndexedDBBackingStore
57 : public base::RefCounted
<IndexedDBBackingStore
> {
59 class CONTENT_EXPORT Comparator
: public LevelDBComparator
{
61 int Compare(const base::StringPiece
& a
,
62 const base::StringPiece
& b
) const override
;
63 const char* Name() const override
;
66 class CONTENT_EXPORT RecordIdentifier
{
68 RecordIdentifier(const std::string
& primary_key
, int64 version
);
72 const std::string
& primary_key() const { return primary_key_
; }
73 int64
version() const { return version_
; }
74 void Reset(const std::string
& primary_key
, int64 version
) {
75 primary_key_
= primary_key
;
80 // TODO(jsbell): Make it more clear that this is the *encoded* version of
82 std::string primary_key_
;
84 DISALLOW_COPY_AND_ASSIGN(RecordIdentifier
);
87 class BlobWriteCallback
: public base::RefCounted
<BlobWriteCallback
> {
89 virtual void Run(bool succeeded
) = 0;
92 friend class base::RefCounted
<BlobWriteCallback
>;
93 virtual ~BlobWriteCallback() {}
96 class BlobChangeRecord
{
98 BlobChangeRecord(const std::string
& key
, int64 object_store_id
);
101 const std::string
& key() const { return key_
; }
102 int64
object_store_id() const { return object_store_id_
; }
103 void SetBlobInfo(std::vector
<IndexedDBBlobInfo
>* blob_info
);
104 std::vector
<IndexedDBBlobInfo
>& mutable_blob_info() { return blob_info_
; }
105 const std::vector
<IndexedDBBlobInfo
>& blob_info() const {
108 void SetHandles(ScopedVector
<storage::BlobDataHandle
>* handles
);
109 scoped_ptr
<BlobChangeRecord
> Clone() const;
113 int64 object_store_id_
;
114 std::vector
<IndexedDBBlobInfo
> blob_info_
;
115 ScopedVector
<storage::BlobDataHandle
> handles_
;
116 DISALLOW_COPY_AND_ASSIGN(BlobChangeRecord
);
118 typedef std::map
<std::string
, BlobChangeRecord
*> BlobChangeMap
;
120 class CONTENT_EXPORT Transaction
{
122 explicit Transaction(IndexedDBBackingStore
* backing_store
);
123 virtual ~Transaction();
125 virtual void Begin();
127 // CommitPhaseOne determines what blobs (if any) need to be written to disk
128 // and updates the primary blob journal, and kicks off the async writing
129 // of the blob files. In case of crash/rollback, the journal indicates what
130 // files should be cleaned up.
131 // The callback will be called eventually on success or failure, or
132 // immediately if phase one is complete due to lack of any blobs to write.
133 virtual leveldb::Status
CommitPhaseOne(scoped_refptr
<BlobWriteCallback
>);
135 // CommitPhaseTwo is called once the blob files (if any) have been written
136 // to disk, and commits the actual transaction to the backing store,
137 // including blob journal updates, then deletes any blob files deleted
138 // by the transaction and not referenced by running scripts.
139 virtual leveldb::Status
CommitPhaseTwo();
141 virtual void Rollback();
143 backing_store_
= NULL
;
146 leveldb::Status
PutBlobInfoIfNeeded(
148 int64 object_store_id
,
149 const std::string
& object_store_data_key
,
150 std::vector
<IndexedDBBlobInfo
>*,
151 ScopedVector
<storage::BlobDataHandle
>* handles
);
152 void PutBlobInfo(int64 database_id
,
153 int64 object_store_id
,
154 const std::string
& object_store_data_key
,
155 std::vector
<IndexedDBBlobInfo
>*,
156 ScopedVector
<storage::BlobDataHandle
>* handles
);
158 LevelDBTransaction
* transaction() { return transaction_
.get(); }
160 leveldb::Status
GetBlobInfoForRecord(
162 const std::string
& object_store_data_key
,
163 IndexedDBValue
* value
);
165 // This holds a BlobEntryKey and the encoded IndexedDBBlobInfo vector stored
167 typedef std::vector
<std::pair
<BlobEntryKey
, std::string
> >
168 BlobEntryKeyValuePairVec
;
170 class CONTENT_EXPORT WriteDescriptor
{
172 WriteDescriptor(const GURL
& url
,
175 base::Time last_modified
);
176 WriteDescriptor(const base::FilePath
& path
,
179 base::Time last_modified
);
180 WriteDescriptor(const WriteDescriptor
& other
);
182 WriteDescriptor
& operator=(const WriteDescriptor
& other
);
184 bool is_file() const { return is_file_
; }
185 const GURL
& url() const {
189 const base::FilePath
& file_path() const {
193 int64_t key() const { return key_
; }
194 int64_t size() const { return size_
; }
195 base::Time
last_modified() const { return last_modified_
; }
200 base::FilePath file_path_
;
203 base::Time last_modified_
;
206 class ChainedBlobWriter
207 : public base::RefCountedThreadSafe
<ChainedBlobWriter
> {
209 virtual void set_delegate(
210 scoped_ptr
<storage::FileWriterDelegate
> delegate
) = 0;
212 // TODO(ericu): Add a reason in the event of failure.
213 virtual void ReportWriteCompletion(bool succeeded
,
214 int64 bytes_written
) = 0;
216 virtual void Abort() = 0;
219 friend class base::RefCountedThreadSafe
<ChainedBlobWriter
>;
220 virtual ~ChainedBlobWriter() {}
223 class ChainedBlobWriterImpl
;
225 typedef std::vector
<WriteDescriptor
> WriteDescriptorVec
;
228 class BlobWriteCallbackWrapper
;
230 // Called by CommitPhaseOne: Identifies the blob entries to write and adds
231 // them to the primary blob journal directly (i.e. not as part of the
232 // transaction). Populates blobs_to_write_.
233 leveldb::Status
HandleBlobPreTransaction(
234 BlobEntryKeyValuePairVec
* new_blob_entries
,
235 WriteDescriptorVec
* new_files_to_write
);
237 // Called by CommitPhaseOne: Populates blob_files_to_remove_ by
238 // determining which blobs are deleted as part of the transaction, and
239 // adds blob entry cleanup operations to the transaction. Returns true on
240 // success, false on failure.
241 bool CollectBlobFilesToRemove();
243 // Called by CommitPhaseOne: Kicks off the asynchronous writes of blobs
244 // identified in HandleBlobPreTransaction. The callback will be called
245 // eventually on success or failure.
246 void WriteNewBlobs(BlobEntryKeyValuePairVec
* new_blob_entries
,
247 WriteDescriptorVec
* new_files_to_write
,
248 scoped_refptr
<BlobWriteCallback
> callback
);
250 // Called by CommitPhaseTwo: Partition blob references in blobs_to_remove_
251 // into live (active references) and dead (no references).
252 void PartitionBlobsToRemove(BlobJournalType
* dead_blobs
,
253 BlobJournalType
* live_blobs
) const;
255 IndexedDBBackingStore
* backing_store_
;
256 scoped_refptr
<LevelDBTransaction
> transaction_
;
257 BlobChangeMap blob_change_map_
;
258 BlobChangeMap incognito_blob_map_
;
261 // List of blob files being newly written as part of this transaction.
262 // These will be added to the primary blob journal prior to commit, then
263 // removed after a sucessful commit.
264 BlobJournalType blobs_to_write_
;
266 // List of blob files being deleted as part of this transaction. These will
267 // be added to either the primary or live blob journal as appropriate
268 // following a successful commit.
269 BlobJournalType blobs_to_remove_
;
270 scoped_refptr
<ChainedBlobWriter
> chained_blob_writer_
;
272 // Set to true between CommitPhaseOne and CommitPhaseTwo/Rollback, to
273 // indicate that the committing_transaction_count_ on the backing store
274 // has been bumped, and journal cleaning should be deferred.
280 enum IteratorState
{ READY
= 0, SEEK
};
284 struct CursorOptions
{
288 int64 object_store_id
;
292 std::string high_key
;
298 const IndexedDBKey
& key() const { return *current_key_
; }
299 bool Continue(leveldb::Status
* s
) { return Continue(NULL
, NULL
, SEEK
, s
); }
300 bool Continue(const IndexedDBKey
* key
,
302 leveldb::Status
* s
) {
303 return Continue(key
, NULL
, state
, s
);
305 bool Continue(const IndexedDBKey
* key
,
306 const IndexedDBKey
* primary_key
,
309 bool Advance(uint32 count
, leveldb::Status
*);
310 bool FirstSeek(leveldb::Status
*);
312 virtual Cursor
* Clone() = 0;
313 virtual const IndexedDBKey
& primary_key() const;
314 virtual IndexedDBValue
* value() = 0;
315 virtual const RecordIdentifier
& record_identifier() const;
316 virtual bool LoadCurrentRow() = 0;
319 Cursor(scoped_refptr
<IndexedDBBackingStore
> backing_store
,
320 Transaction
* transaction
,
322 const CursorOptions
& cursor_options
);
323 explicit Cursor(const IndexedDBBackingStore::Cursor
* other
);
325 virtual std::string
EncodeKey(const IndexedDBKey
& key
) = 0;
326 virtual std::string
EncodeKey(const IndexedDBKey
& key
,
327 const IndexedDBKey
& primary_key
) = 0;
329 bool IsPastBounds() const;
330 bool HaveEnteredRange() const;
332 IndexedDBBackingStore
* backing_store_
;
333 Transaction
* transaction_
;
335 const CursorOptions cursor_options_
;
336 scoped_ptr
<LevelDBIterator
> iterator_
;
337 scoped_ptr
<IndexedDBKey
> current_key_
;
338 IndexedDBBackingStore::RecordIdentifier record_identifier_
;
341 DISALLOW_COPY_AND_ASSIGN(Cursor
);
344 const GURL
& origin_url() const { return origin_url_
; }
345 IndexedDBFactory
* factory() const { return indexed_db_factory_
; }
346 base::SequencedTaskRunner
* task_runner() const { return task_runner_
.get(); }
347 base::OneShotTimer
<IndexedDBBackingStore
>* close_timer() {
348 return &close_timer_
;
350 IndexedDBActiveBlobRegistry
* active_blob_registry() {
351 return &active_blob_registry_
;
354 static scoped_refptr
<IndexedDBBackingStore
> Open(
355 IndexedDBFactory
* indexed_db_factory
,
356 const GURL
& origin_url
,
357 const base::FilePath
& path_base
,
358 net::URLRequestContext
* request_context
,
359 blink::WebIDBDataLoss
* data_loss
,
360 std::string
* data_loss_message
,
362 base::SequencedTaskRunner
* task_runner
,
364 leveldb::Status
* status
);
365 static scoped_refptr
<IndexedDBBackingStore
> Open(
366 IndexedDBFactory
* indexed_db_factory
,
367 const GURL
& origin_url
,
368 const base::FilePath
& path_base
,
369 net::URLRequestContext
* request_context
,
370 blink::WebIDBDataLoss
* data_loss
,
371 std::string
* data_loss_message
,
373 LevelDBFactory
* leveldb_factory
,
374 base::SequencedTaskRunner
* task_runner
,
376 leveldb::Status
* status
);
377 static scoped_refptr
<IndexedDBBackingStore
> OpenInMemory(
378 const GURL
& origin_url
,
379 base::SequencedTaskRunner
* task_runner
,
380 leveldb::Status
* status
);
381 static scoped_refptr
<IndexedDBBackingStore
> OpenInMemory(
382 const GURL
& origin_url
,
383 LevelDBFactory
* leveldb_factory
,
384 base::SequencedTaskRunner
* task_runner
,
385 leveldb::Status
* status
);
387 void GrantChildProcessPermissions(int child_process_id
);
389 // Compact is public for testing.
390 virtual void Compact();
391 virtual std::vector
<base::string16
> GetDatabaseNames(leveldb::Status
*);
392 virtual leveldb::Status
GetIDBDatabaseMetaData(
393 const base::string16
& name
,
394 IndexedDBDatabaseMetadata
* metadata
,
395 bool* success
) WARN_UNUSED_RESULT
;
396 virtual leveldb::Status
CreateIDBDatabaseMetaData(
397 const base::string16
& name
,
398 const base::string16
& version
,
401 virtual bool UpdateIDBDatabaseIntVersion(
402 IndexedDBBackingStore::Transaction
* transaction
,
405 virtual leveldb::Status
DeleteDatabase(const base::string16
& name
);
407 // Assumes caller has already closed the backing store.
408 static leveldb::Status
DestroyBackingStore(const base::FilePath
& path_base
,
409 const GURL
& origin_url
);
410 static bool RecordCorruptionInfo(const base::FilePath
& path_base
,
411 const GURL
& origin_url
,
412 const std::string
& message
);
413 leveldb::Status
GetObjectStores(
415 IndexedDBDatabaseMetadata::ObjectStoreMap
* map
) WARN_UNUSED_RESULT
;
416 virtual leveldb::Status
CreateObjectStore(
417 IndexedDBBackingStore::Transaction
* transaction
,
419 int64 object_store_id
,
420 const base::string16
& name
,
421 const IndexedDBKeyPath
& key_path
,
422 bool auto_increment
);
423 virtual leveldb::Status
DeleteObjectStore(
424 IndexedDBBackingStore::Transaction
* transaction
,
426 int64 object_store_id
) WARN_UNUSED_RESULT
;
428 virtual leveldb::Status
GetRecord(
429 IndexedDBBackingStore::Transaction
* transaction
,
431 int64 object_store_id
,
432 const IndexedDBKey
& key
,
433 IndexedDBValue
* record
) WARN_UNUSED_RESULT
;
434 virtual leveldb::Status
PutRecord(
435 IndexedDBBackingStore::Transaction
* transaction
,
437 int64 object_store_id
,
438 const IndexedDBKey
& key
,
439 IndexedDBValue
* value
,
440 ScopedVector
<storage::BlobDataHandle
>* handles
,
441 RecordIdentifier
* record
) WARN_UNUSED_RESULT
;
442 virtual leveldb::Status
ClearObjectStore(
443 IndexedDBBackingStore::Transaction
* transaction
,
445 int64 object_store_id
) WARN_UNUSED_RESULT
;
446 virtual leveldb::Status
DeleteRecord(
447 IndexedDBBackingStore::Transaction
* transaction
,
449 int64 object_store_id
,
450 const RecordIdentifier
& record
) WARN_UNUSED_RESULT
;
451 virtual leveldb::Status
DeleteRange(
452 IndexedDBBackingStore::Transaction
* transaction
,
454 int64 object_store_id
,
455 const IndexedDBKeyRange
&) WARN_UNUSED_RESULT
;
456 virtual leveldb::Status
GetKeyGeneratorCurrentNumber(
457 IndexedDBBackingStore::Transaction
* transaction
,
459 int64 object_store_id
,
460 int64
* current_number
) WARN_UNUSED_RESULT
;
461 virtual leveldb::Status
MaybeUpdateKeyGeneratorCurrentNumber(
462 IndexedDBBackingStore::Transaction
* transaction
,
464 int64 object_store_id
,
466 bool check_current
) WARN_UNUSED_RESULT
;
467 virtual leveldb::Status
KeyExistsInObjectStore(
468 IndexedDBBackingStore::Transaction
* transaction
,
470 int64 object_store_id
,
471 const IndexedDBKey
& key
,
472 RecordIdentifier
* found_record_identifier
,
473 bool* found
) WARN_UNUSED_RESULT
;
475 virtual leveldb::Status
CreateIndex(
476 IndexedDBBackingStore::Transaction
* transaction
,
478 int64 object_store_id
,
480 const base::string16
& name
,
481 const IndexedDBKeyPath
& key_path
,
483 bool is_multi_entry
) WARN_UNUSED_RESULT
;
484 virtual leveldb::Status
DeleteIndex(
485 IndexedDBBackingStore::Transaction
* transaction
,
487 int64 object_store_id
,
488 int64 index_id
) WARN_UNUSED_RESULT
;
489 virtual leveldb::Status
PutIndexDataForRecord(
490 IndexedDBBackingStore::Transaction
* transaction
,
492 int64 object_store_id
,
494 const IndexedDBKey
& key
,
495 const RecordIdentifier
& record
) WARN_UNUSED_RESULT
;
496 virtual leveldb::Status
GetPrimaryKeyViaIndex(
497 IndexedDBBackingStore::Transaction
* transaction
,
499 int64 object_store_id
,
501 const IndexedDBKey
& key
,
502 scoped_ptr
<IndexedDBKey
>* primary_key
) WARN_UNUSED_RESULT
;
503 virtual leveldb::Status
KeyExistsInIndex(
504 IndexedDBBackingStore::Transaction
* transaction
,
506 int64 object_store_id
,
508 const IndexedDBKey
& key
,
509 scoped_ptr
<IndexedDBKey
>* found_primary_key
,
510 bool* exists
) WARN_UNUSED_RESULT
;
512 // Public for IndexedDBActiveBlobRegistry::ReleaseBlobRef.
513 virtual void ReportBlobUnused(int64 database_id
, int64 blob_key
);
515 base::FilePath
GetBlobFileName(int64 database_id
, int64 key
) const;
517 virtual scoped_ptr
<Cursor
> OpenObjectStoreKeyCursor(
518 IndexedDBBackingStore::Transaction
* transaction
,
520 int64 object_store_id
,
521 const IndexedDBKeyRange
& key_range
,
522 blink::WebIDBCursorDirection
,
524 virtual scoped_ptr
<Cursor
> OpenObjectStoreCursor(
525 IndexedDBBackingStore::Transaction
* transaction
,
527 int64 object_store_id
,
528 const IndexedDBKeyRange
& key_range
,
529 blink::WebIDBCursorDirection
,
531 virtual scoped_ptr
<Cursor
> OpenIndexKeyCursor(
532 IndexedDBBackingStore::Transaction
* transaction
,
534 int64 object_store_id
,
536 const IndexedDBKeyRange
& key_range
,
537 blink::WebIDBCursorDirection
,
539 virtual scoped_ptr
<Cursor
> OpenIndexCursor(
540 IndexedDBBackingStore::Transaction
* transaction
,
542 int64 object_store_id
,
544 const IndexedDBKeyRange
& key_range
,
545 blink::WebIDBCursorDirection
,
549 friend class base::RefCounted
<IndexedDBBackingStore
>;
551 IndexedDBBackingStore(IndexedDBFactory
* indexed_db_factory
,
552 const GURL
& origin_url
,
553 const base::FilePath
& blob_path
,
554 net::URLRequestContext
* request_context
,
555 scoped_ptr
<LevelDBDatabase
> db
,
556 scoped_ptr
<LevelDBComparator
> comparator
,
557 base::SequencedTaskRunner
* task_runner
);
558 virtual ~IndexedDBBackingStore();
560 bool is_incognito() const { return !indexed_db_factory_
; }
562 leveldb::Status
SetUpMetadata();
564 virtual bool WriteBlobFile(
566 const Transaction::WriteDescriptor
& descriptor
,
567 Transaction::ChainedBlobWriter
* chained_blob_writer
);
569 // Remove the referenced file on disk.
570 virtual bool RemoveBlobFile(int64 database_id
, int64 key
) const;
572 // Schedule a call to CleanPrimaryJournalIgnoreReturn() via
573 // an owned timer. If this object is destroyed, the timer
574 // will automatically be cancelled.
575 virtual void StartJournalCleaningTimer();
577 // Attempt to clean the primary journal. This will remove
578 // any referenced files and delete the journal entry. If any
579 // transaction is currently committing this will be deferred
580 // via StartJournalCleaningTimer().
581 void CleanPrimaryJournalIgnoreReturn();
584 static scoped_refptr
<IndexedDBBackingStore
> Create(
585 IndexedDBFactory
* indexed_db_factory
,
586 const GURL
& origin_url
,
587 const base::FilePath
& blob_path
,
588 net::URLRequestContext
* request_context
,
589 scoped_ptr
<LevelDBDatabase
> db
,
590 scoped_ptr
<LevelDBComparator
> comparator
,
591 base::SequencedTaskRunner
* task_runner
,
592 leveldb::Status
* status
);
594 static bool ReadCorruptionInfo(const base::FilePath
& path_base
,
595 const GURL
& origin_url
,
596 std::string
* message
);
598 leveldb::Status
FindKeyInIndex(
599 IndexedDBBackingStore::Transaction
* transaction
,
601 int64 object_store_id
,
603 const IndexedDBKey
& key
,
604 std::string
* found_encoded_primary_key
,
606 leveldb::Status
GetIndexes(int64 database_id
,
607 int64 object_store_id
,
608 IndexedDBObjectStoreMetadata::IndexMap
* map
)
611 // Remove the blob directory for the specified database and all contained
613 bool RemoveBlobDirectory(int64 database_id
) const;
615 // Synchronously read the key-specified blob journal entry from the backing
616 // store, delete all referenced blob files, and erase the journal entry.
617 // This must not be used while temporary entries are present e.g. during
618 // a two-stage transaction commit with blobs.
619 leveldb::Status
CleanUpBlobJournal(const std::string
& level_db_key
) const;
621 // Synchronously delete the files and/or directories on disk referenced by
623 leveldb::Status
CleanUpBlobJournalEntries(
624 const BlobJournalType
& journal
) const;
626 IndexedDBFactory
* indexed_db_factory_
;
627 const GURL origin_url_
;
628 base::FilePath blob_path_
;
630 // The origin identifier is a key prefix unique to the origin used in the
631 // leveldb backing store to partition data by origin. It is a normalized
632 // version of the origin URL with a versioning suffix appended, e.g.
633 // "http_localhost_81@1" Since only one origin is stored per backing store
634 // this is redundant but necessary for backwards compatibility; the suffix
635 // provides for future flexibility.
636 const std::string origin_identifier_
;
638 net::URLRequestContext
* request_context_
;
639 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
640 std::set
<int> child_process_ids_granted_
;
641 BlobChangeMap incognito_blob_map_
;
642 base::OneShotTimer
<IndexedDBBackingStore
> journal_cleaning_timer_
;
644 scoped_ptr
<LevelDBDatabase
> db_
;
645 scoped_ptr
<LevelDBComparator
> comparator_
;
646 // Whenever blobs are registered in active_blob_registry_, indexed_db_factory_
647 // will hold a reference to this backing store.
648 IndexedDBActiveBlobRegistry active_blob_registry_
;
649 base::OneShotTimer
<IndexedDBBackingStore
> close_timer_
;
651 // Incremented whenever a transaction starts committing, decremented when
652 // complete. While > 0, temporary journal entries may exist so out-of-band
653 // journal cleaning must be deferred.
654 size_t committing_transaction_count_
;
656 DISALLOW_COPY_AND_ASSIGN(IndexedDBBackingStore
);
659 } // namespace content
661 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_