OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 5 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 if (!found) | 479 if (!found) |
480 *max_object_store_id = 0; | 480 *max_object_store_id = 0; |
481 | 481 |
482 DCHECK_GE(*max_object_store_id, 0); | 482 DCHECK_GE(*max_object_store_id, 0); |
483 return s; | 483 return s; |
484 } | 484 } |
485 | 485 |
486 class DefaultLevelDBFactory : public LevelDBFactory { | 486 class DefaultLevelDBFactory : public LevelDBFactory { |
487 public: | 487 public: |
488 DefaultLevelDBFactory() {} | 488 DefaultLevelDBFactory() {} |
489 virtual leveldb::Status OpenLevelDB(const base::FilePath& file_name, | 489 leveldb::Status OpenLevelDB(const base::FilePath& file_name, |
490 const LevelDBComparator* comparator, | 490 const LevelDBComparator* comparator, |
491 scoped_ptr<LevelDBDatabase>* db, | 491 scoped_ptr<LevelDBDatabase>* db, |
492 bool* is_disk_full) override { | 492 bool* is_disk_full) override { |
493 return LevelDBDatabase::Open(file_name, comparator, db, is_disk_full); | 493 return LevelDBDatabase::Open(file_name, comparator, db, is_disk_full); |
494 } | 494 } |
495 virtual leveldb::Status DestroyLevelDB(const base::FilePath& file_name) | 495 leveldb::Status DestroyLevelDB(const base::FilePath& file_name) override { |
496 override { | |
497 return LevelDBDatabase::Destroy(file_name); | 496 return LevelDBDatabase::Destroy(file_name); |
498 } | 497 } |
499 | 498 |
500 private: | 499 private: |
501 DISALLOW_COPY_AND_ASSIGN(DefaultLevelDBFactory); | 500 DISALLOW_COPY_AND_ASSIGN(DefaultLevelDBFactory); |
502 }; | 501 }; |
503 | 502 |
504 static bool GetBlobKeyGeneratorCurrentNumber( | 503 static bool GetBlobKeyGeneratorCurrentNumber( |
505 LevelDBTransaction* leveldb_transaction, | 504 LevelDBTransaction* leveldb_transaction, |
506 int64 database_id, | 505 int64 database_id, |
(...skipping 1693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2200 database_id_(database_id), | 2199 database_id_(database_id), |
2201 backing_store_(backing_store), | 2200 backing_store_(backing_store), |
2202 callback_(callback), | 2201 callback_(callback), |
2203 aborted_(false) { | 2202 aborted_(false) { |
2204 blobs_.swap(*blobs); | 2203 blobs_.swap(*blobs); |
2205 iter_ = blobs_.begin(); | 2204 iter_ = blobs_.begin(); |
2206 backing_store->task_runner()->PostTask( | 2205 backing_store->task_runner()->PostTask( |
2207 FROM_HERE, base::Bind(&ChainedBlobWriterImpl::WriteNextFile, this)); | 2206 FROM_HERE, base::Bind(&ChainedBlobWriterImpl::WriteNextFile, this)); |
2208 } | 2207 } |
2209 | 2208 |
2210 virtual void set_delegate(scoped_ptr<FileWriterDelegate> delegate) override { | 2209 void set_delegate(scoped_ptr<FileWriterDelegate> delegate) override { |
2211 delegate_.reset(delegate.release()); | 2210 delegate_.reset(delegate.release()); |
2212 } | 2211 } |
2213 | 2212 |
2214 virtual void ReportWriteCompletion(bool succeeded, | 2213 void ReportWriteCompletion(bool succeeded, int64 bytes_written) override { |
2215 int64 bytes_written) override { | |
2216 DCHECK(waiting_for_callback_); | 2214 DCHECK(waiting_for_callback_); |
2217 DCHECK(!succeeded || bytes_written >= 0); | 2215 DCHECK(!succeeded || bytes_written >= 0); |
2218 waiting_for_callback_ = false; | 2216 waiting_for_callback_ = false; |
2219 if (delegate_.get()) // Only present for Blob, not File. | 2217 if (delegate_.get()) // Only present for Blob, not File. |
2220 content::BrowserThread::DeleteSoon( | 2218 content::BrowserThread::DeleteSoon( |
2221 content::BrowserThread::IO, FROM_HERE, delegate_.release()); | 2219 content::BrowserThread::IO, FROM_HERE, delegate_.release()); |
2222 if (aborted_) { | 2220 if (aborted_) { |
2223 self_ref_ = NULL; | 2221 self_ref_ = NULL; |
2224 return; | 2222 return; |
2225 } | 2223 } |
2226 if (iter_->size() != -1 && iter_->size() != bytes_written) | 2224 if (iter_->size() != -1 && iter_->size() != bytes_written) |
2227 succeeded = false; | 2225 succeeded = false; |
2228 if (succeeded) { | 2226 if (succeeded) { |
2229 ++iter_; | 2227 ++iter_; |
2230 WriteNextFile(); | 2228 WriteNextFile(); |
2231 } else { | 2229 } else { |
2232 callback_->Run(false); | 2230 callback_->Run(false); |
2233 } | 2231 } |
2234 } | 2232 } |
2235 | 2233 |
2236 virtual void Abort() override { | 2234 void Abort() override { |
2237 if (!waiting_for_callback_) | 2235 if (!waiting_for_callback_) |
2238 return; | 2236 return; |
2239 self_ref_ = this; | 2237 self_ref_ = this; |
2240 aborted_ = true; | 2238 aborted_ = true; |
2241 } | 2239 } |
2242 | 2240 |
2243 private: | 2241 private: |
2244 virtual ~ChainedBlobWriterImpl() {} | 2242 ~ChainedBlobWriterImpl() override {} |
2245 | 2243 |
2246 void WriteNextFile() { | 2244 void WriteNextFile() { |
2247 DCHECK(!waiting_for_callback_); | 2245 DCHECK(!waiting_for_callback_); |
2248 DCHECK(!aborted_); | 2246 DCHECK(!aborted_); |
2249 if (iter_ == blobs_.end()) { | 2247 if (iter_ == blobs_.end()) { |
2250 DCHECK(!self_ref_.get()); | 2248 DCHECK(!self_ref_.get()); |
2251 callback_->Run(true); | 2249 callback_->Run(true); |
2252 return; | 2250 return; |
2253 } else { | 2251 } else { |
2254 if (!backing_store_->WriteBlobFile(database_id_, *iter_, this)) { | 2252 if (!backing_store_->WriteBlobFile(database_id_, *iter_, this)) { |
(...skipping 990 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3245 ObjectStoreKeyCursorImpl( | 3243 ObjectStoreKeyCursorImpl( |
3246 scoped_refptr<IndexedDBBackingStore> backing_store, | 3244 scoped_refptr<IndexedDBBackingStore> backing_store, |
3247 IndexedDBBackingStore::Transaction* transaction, | 3245 IndexedDBBackingStore::Transaction* transaction, |
3248 int64 database_id, | 3246 int64 database_id, |
3249 const IndexedDBBackingStore::Cursor::CursorOptions& cursor_options) | 3247 const IndexedDBBackingStore::Cursor::CursorOptions& cursor_options) |
3250 : IndexedDBBackingStore::Cursor(backing_store, | 3248 : IndexedDBBackingStore::Cursor(backing_store, |
3251 transaction, | 3249 transaction, |
3252 database_id, | 3250 database_id, |
3253 cursor_options) {} | 3251 cursor_options) {} |
3254 | 3252 |
3255 virtual Cursor* Clone() override { | 3253 Cursor* Clone() override { return new ObjectStoreKeyCursorImpl(this); } |
3256 return new ObjectStoreKeyCursorImpl(this); | |
3257 } | |
3258 | 3254 |
3259 // IndexedDBBackingStore::Cursor | 3255 // IndexedDBBackingStore::Cursor |
3260 virtual IndexedDBValue* value() override { | 3256 IndexedDBValue* value() override { |
3261 NOTREACHED(); | 3257 NOTREACHED(); |
3262 return NULL; | 3258 return NULL; |
3263 } | 3259 } |
3264 virtual bool LoadCurrentRow() override; | 3260 bool LoadCurrentRow() override; |
3265 | 3261 |
3266 protected: | 3262 protected: |
3267 virtual std::string EncodeKey(const IndexedDBKey& key) override { | 3263 std::string EncodeKey(const IndexedDBKey& key) override { |
3268 return ObjectStoreDataKey::Encode( | 3264 return ObjectStoreDataKey::Encode( |
3269 cursor_options_.database_id, cursor_options_.object_store_id, key); | 3265 cursor_options_.database_id, cursor_options_.object_store_id, key); |
3270 } | 3266 } |
3271 virtual std::string EncodeKey(const IndexedDBKey& key, | 3267 std::string EncodeKey(const IndexedDBKey& key, |
3272 const IndexedDBKey& primary_key) override { | 3268 const IndexedDBKey& primary_key) override { |
3273 NOTREACHED(); | 3269 NOTREACHED(); |
3274 return std::string(); | 3270 return std::string(); |
3275 } | 3271 } |
3276 | 3272 |
3277 private: | 3273 private: |
3278 explicit ObjectStoreKeyCursorImpl(const ObjectStoreKeyCursorImpl* other) | 3274 explicit ObjectStoreKeyCursorImpl(const ObjectStoreKeyCursorImpl* other) |
3279 : IndexedDBBackingStore::Cursor(other) {} | 3275 : IndexedDBBackingStore::Cursor(other) {} |
3280 | 3276 |
3281 DISALLOW_COPY_AND_ASSIGN(ObjectStoreKeyCursorImpl); | 3277 DISALLOW_COPY_AND_ASSIGN(ObjectStoreKeyCursorImpl); |
3282 }; | 3278 }; |
(...skipping 28 matching lines...) Expand all Loading... |
3311 ObjectStoreCursorImpl( | 3307 ObjectStoreCursorImpl( |
3312 scoped_refptr<IndexedDBBackingStore> backing_store, | 3308 scoped_refptr<IndexedDBBackingStore> backing_store, |
3313 IndexedDBBackingStore::Transaction* transaction, | 3309 IndexedDBBackingStore::Transaction* transaction, |
3314 int64 database_id, | 3310 int64 database_id, |
3315 const IndexedDBBackingStore::Cursor::CursorOptions& cursor_options) | 3311 const IndexedDBBackingStore::Cursor::CursorOptions& cursor_options) |
3316 : IndexedDBBackingStore::Cursor(backing_store, | 3312 : IndexedDBBackingStore::Cursor(backing_store, |
3317 transaction, | 3313 transaction, |
3318 database_id, | 3314 database_id, |
3319 cursor_options) {} | 3315 cursor_options) {} |
3320 | 3316 |
3321 virtual Cursor* Clone() override { return new ObjectStoreCursorImpl(this); } | 3317 Cursor* Clone() override { return new ObjectStoreCursorImpl(this); } |
3322 | 3318 |
3323 // IndexedDBBackingStore::Cursor | 3319 // IndexedDBBackingStore::Cursor |
3324 virtual IndexedDBValue* value() override { return ¤t_value_; } | 3320 IndexedDBValue* value() override { return ¤t_value_; } |
3325 virtual bool LoadCurrentRow() override; | 3321 bool LoadCurrentRow() override; |
3326 | 3322 |
3327 protected: | 3323 protected: |
3328 virtual std::string EncodeKey(const IndexedDBKey& key) override { | 3324 std::string EncodeKey(const IndexedDBKey& key) override { |
3329 return ObjectStoreDataKey::Encode( | 3325 return ObjectStoreDataKey::Encode( |
3330 cursor_options_.database_id, cursor_options_.object_store_id, key); | 3326 cursor_options_.database_id, cursor_options_.object_store_id, key); |
3331 } | 3327 } |
3332 virtual std::string EncodeKey(const IndexedDBKey& key, | 3328 std::string EncodeKey(const IndexedDBKey& key, |
3333 const IndexedDBKey& primary_key) override { | 3329 const IndexedDBKey& primary_key) override { |
3334 NOTREACHED(); | 3330 NOTREACHED(); |
3335 return std::string(); | 3331 return std::string(); |
3336 } | 3332 } |
3337 | 3333 |
3338 private: | 3334 private: |
3339 explicit ObjectStoreCursorImpl(const ObjectStoreCursorImpl* other) | 3335 explicit ObjectStoreCursorImpl(const ObjectStoreCursorImpl* other) |
3340 : IndexedDBBackingStore::Cursor(other), | 3336 : IndexedDBBackingStore::Cursor(other), |
3341 current_value_(other->current_value_) {} | 3337 current_value_(other->current_value_) {} |
3342 | 3338 |
3343 IndexedDBValue current_value_; | 3339 IndexedDBValue current_value_; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3381 IndexKeyCursorImpl( | 3377 IndexKeyCursorImpl( |
3382 scoped_refptr<IndexedDBBackingStore> backing_store, | 3378 scoped_refptr<IndexedDBBackingStore> backing_store, |
3383 IndexedDBBackingStore::Transaction* transaction, | 3379 IndexedDBBackingStore::Transaction* transaction, |
3384 int64 database_id, | 3380 int64 database_id, |
3385 const IndexedDBBackingStore::Cursor::CursorOptions& cursor_options) | 3381 const IndexedDBBackingStore::Cursor::CursorOptions& cursor_options) |
3386 : IndexedDBBackingStore::Cursor(backing_store, | 3382 : IndexedDBBackingStore::Cursor(backing_store, |
3387 transaction, | 3383 transaction, |
3388 database_id, | 3384 database_id, |
3389 cursor_options) {} | 3385 cursor_options) {} |
3390 | 3386 |
3391 virtual Cursor* Clone() override { return new IndexKeyCursorImpl(this); } | 3387 Cursor* Clone() override { return new IndexKeyCursorImpl(this); } |
3392 | 3388 |
3393 // IndexedDBBackingStore::Cursor | 3389 // IndexedDBBackingStore::Cursor |
3394 virtual IndexedDBValue* value() override { | 3390 IndexedDBValue* value() override { |
3395 NOTREACHED(); | 3391 NOTREACHED(); |
3396 return NULL; | 3392 return NULL; |
3397 } | 3393 } |
3398 virtual const IndexedDBKey& primary_key() const override { | 3394 const IndexedDBKey& primary_key() const override { return *primary_key_; } |
3399 return *primary_key_; | 3395 const IndexedDBBackingStore::RecordIdentifier& record_identifier() |
3400 } | |
3401 virtual const IndexedDBBackingStore::RecordIdentifier& record_identifier() | |
3402 const override { | 3396 const override { |
3403 NOTREACHED(); | 3397 NOTREACHED(); |
3404 return record_identifier_; | 3398 return record_identifier_; |
3405 } | 3399 } |
3406 virtual bool LoadCurrentRow() override; | 3400 bool LoadCurrentRow() override; |
3407 | 3401 |
3408 protected: | 3402 protected: |
3409 virtual std::string EncodeKey(const IndexedDBKey& key) override { | 3403 std::string EncodeKey(const IndexedDBKey& key) override { |
3410 return IndexDataKey::Encode(cursor_options_.database_id, | 3404 return IndexDataKey::Encode(cursor_options_.database_id, |
3411 cursor_options_.object_store_id, | 3405 cursor_options_.object_store_id, |
3412 cursor_options_.index_id, | 3406 cursor_options_.index_id, |
3413 key); | 3407 key); |
3414 } | 3408 } |
3415 virtual std::string EncodeKey(const IndexedDBKey& key, | 3409 std::string EncodeKey(const IndexedDBKey& key, |
3416 const IndexedDBKey& primary_key) override { | 3410 const IndexedDBKey& primary_key) override { |
3417 return IndexDataKey::Encode(cursor_options_.database_id, | 3411 return IndexDataKey::Encode(cursor_options_.database_id, |
3418 cursor_options_.object_store_id, | 3412 cursor_options_.object_store_id, |
3419 cursor_options_.index_id, | 3413 cursor_options_.index_id, |
3420 key, | 3414 key, |
3421 primary_key); | 3415 primary_key); |
3422 } | 3416 } |
3423 | 3417 |
3424 private: | 3418 private: |
3425 explicit IndexKeyCursorImpl(const IndexKeyCursorImpl* other) | 3419 explicit IndexKeyCursorImpl(const IndexKeyCursorImpl* other) |
3426 : IndexedDBBackingStore::Cursor(other), | 3420 : IndexedDBBackingStore::Cursor(other), |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3496 IndexCursorImpl( | 3490 IndexCursorImpl( |
3497 scoped_refptr<IndexedDBBackingStore> backing_store, | 3491 scoped_refptr<IndexedDBBackingStore> backing_store, |
3498 IndexedDBBackingStore::Transaction* transaction, | 3492 IndexedDBBackingStore::Transaction* transaction, |
3499 int64 database_id, | 3493 int64 database_id, |
3500 const IndexedDBBackingStore::Cursor::CursorOptions& cursor_options) | 3494 const IndexedDBBackingStore::Cursor::CursorOptions& cursor_options) |
3501 : IndexedDBBackingStore::Cursor(backing_store, | 3495 : IndexedDBBackingStore::Cursor(backing_store, |
3502 transaction, | 3496 transaction, |
3503 database_id, | 3497 database_id, |
3504 cursor_options) {} | 3498 cursor_options) {} |
3505 | 3499 |
3506 virtual Cursor* Clone() override { return new IndexCursorImpl(this); } | 3500 Cursor* Clone() override { return new IndexCursorImpl(this); } |
3507 | 3501 |
3508 // IndexedDBBackingStore::Cursor | 3502 // IndexedDBBackingStore::Cursor |
3509 virtual IndexedDBValue* value() override { return ¤t_value_; } | 3503 IndexedDBValue* value() override { return ¤t_value_; } |
3510 virtual const IndexedDBKey& primary_key() const override { | 3504 const IndexedDBKey& primary_key() const override { return *primary_key_; } |
3511 return *primary_key_; | 3505 const IndexedDBBackingStore::RecordIdentifier& record_identifier() |
3512 } | |
3513 virtual const IndexedDBBackingStore::RecordIdentifier& record_identifier() | |
3514 const override { | 3506 const override { |
3515 NOTREACHED(); | 3507 NOTREACHED(); |
3516 return record_identifier_; | 3508 return record_identifier_; |
3517 } | 3509 } |
3518 virtual bool LoadCurrentRow() override; | 3510 bool LoadCurrentRow() override; |
3519 | 3511 |
3520 protected: | 3512 protected: |
3521 virtual std::string EncodeKey(const IndexedDBKey& key) override { | 3513 std::string EncodeKey(const IndexedDBKey& key) override { |
3522 return IndexDataKey::Encode(cursor_options_.database_id, | 3514 return IndexDataKey::Encode(cursor_options_.database_id, |
3523 cursor_options_.object_store_id, | 3515 cursor_options_.object_store_id, |
3524 cursor_options_.index_id, | 3516 cursor_options_.index_id, |
3525 key); | 3517 key); |
3526 } | 3518 } |
3527 virtual std::string EncodeKey(const IndexedDBKey& key, | 3519 std::string EncodeKey(const IndexedDBKey& key, |
3528 const IndexedDBKey& primary_key) override { | 3520 const IndexedDBKey& primary_key) override { |
3529 return IndexDataKey::Encode(cursor_options_.database_id, | 3521 return IndexDataKey::Encode(cursor_options_.database_id, |
3530 cursor_options_.object_store_id, | 3522 cursor_options_.object_store_id, |
3531 cursor_options_.index_id, | 3523 cursor_options_.index_id, |
3532 key, | 3524 key, |
3533 primary_key); | 3525 primary_key); |
3534 } | 3526 } |
3535 | 3527 |
3536 private: | 3528 private: |
3537 explicit IndexCursorImpl(const IndexCursorImpl* other) | 3529 explicit IndexCursorImpl(const IndexCursorImpl* other) |
3538 : IndexedDBBackingStore::Cursor(other), | 3530 : IndexedDBBackingStore::Cursor(other), |
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4100 return s; | 4092 return s; |
4101 } | 4093 } |
4102 | 4094 |
4103 | 4095 |
4104 class IndexedDBBackingStore::Transaction::BlobWriteCallbackWrapper | 4096 class IndexedDBBackingStore::Transaction::BlobWriteCallbackWrapper |
4105 : public IndexedDBBackingStore::BlobWriteCallback { | 4097 : public IndexedDBBackingStore::BlobWriteCallback { |
4106 public: | 4098 public: |
4107 BlobWriteCallbackWrapper(IndexedDBBackingStore::Transaction* transaction, | 4099 BlobWriteCallbackWrapper(IndexedDBBackingStore::Transaction* transaction, |
4108 scoped_refptr<BlobWriteCallback> callback) | 4100 scoped_refptr<BlobWriteCallback> callback) |
4109 : transaction_(transaction), callback_(callback) {} | 4101 : transaction_(transaction), callback_(callback) {} |
4110 virtual void Run(bool succeeded) override { | 4102 void Run(bool succeeded) override { |
4111 callback_->Run(succeeded); | 4103 callback_->Run(succeeded); |
4112 if (succeeded) // Else it's already been deleted during rollback. | 4104 if (succeeded) // Else it's already been deleted during rollback. |
4113 transaction_->chained_blob_writer_ = NULL; | 4105 transaction_->chained_blob_writer_ = NULL; |
4114 } | 4106 } |
4115 | 4107 |
4116 private: | 4108 private: |
4117 virtual ~BlobWriteCallbackWrapper() {} | 4109 ~BlobWriteCallbackWrapper() override {} |
4118 friend class base::RefCounted<IndexedDBBackingStore::BlobWriteCallback>; | 4110 friend class base::RefCounted<IndexedDBBackingStore::BlobWriteCallback>; |
4119 | 4111 |
4120 IndexedDBBackingStore::Transaction* transaction_; | 4112 IndexedDBBackingStore::Transaction* transaction_; |
4121 scoped_refptr<BlobWriteCallback> callback_; | 4113 scoped_refptr<BlobWriteCallback> callback_; |
4122 | 4114 |
4123 DISALLOW_COPY_AND_ASSIGN(BlobWriteCallbackWrapper); | 4115 DISALLOW_COPY_AND_ASSIGN(BlobWriteCallbackWrapper); |
4124 }; | 4116 }; |
4125 | 4117 |
4126 void IndexedDBBackingStore::Transaction::WriteNewBlobs( | 4118 void IndexedDBBackingStore::Transaction::WriteNewBlobs( |
4127 BlobEntryKeyValuePairVec* new_blob_entries, | 4119 BlobEntryKeyValuePairVec* new_blob_entries, |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4265 int64_t size, | 4257 int64_t size, |
4266 base::Time last_modified) | 4258 base::Time last_modified) |
4267 : is_file_(true), | 4259 : is_file_(true), |
4268 file_path_(file_path), | 4260 file_path_(file_path), |
4269 key_(key), | 4261 key_(key), |
4270 size_(size), | 4262 size_(size), |
4271 last_modified_(last_modified) { | 4263 last_modified_(last_modified) { |
4272 } | 4264 } |
4273 | 4265 |
4274 } // namespace content | 4266 } // namespace content |
OLD | NEW |