Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: content/browser/indexed_db/indexed_db_backing_store.cc

Issue 2472213003: [IndexedDB] Refactoring to remove ref ptrs and host transaction ids. (Closed)
Patch Set: rebased & working Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 2250 matching lines...) Expand 10 before | Expand all | Expand 10 after
2261 if (aborted_) { 2261 if (aborted_) {
2262 self_ref_ = NULL; 2262 self_ref_ = NULL;
2263 return; 2263 return;
2264 } 2264 }
2265 if (iter_->size() != -1 && iter_->size() != bytes_written) 2265 if (iter_->size() != -1 && iter_->size() != bytes_written)
2266 succeeded = false; 2266 succeeded = false;
2267 if (succeeded) { 2267 if (succeeded) {
2268 ++iter_; 2268 ++iter_;
2269 WriteNextFile(); 2269 WriteNextFile();
2270 } else { 2270 } else {
2271 callback_->Run(false); 2271 callback_->Run(false, false);
jsbell 2016/11/30 21:30:13 Comment these args, e.g. /* name */ Or maybe cons
dmurph 2016/11/30 23:13:07 Done.
2272 } 2272 }
2273 } 2273 }
2274 2274
2275 void Abort() override { 2275 void Abort() override {
2276 aborted_ = true; 2276 aborted_ = true;
2277 if (!waiting_for_callback_) 2277 if (!waiting_for_callback_)
2278 return; 2278 return;
2279 self_ref_ = this; 2279 self_ref_ = this;
2280 } 2280 }
2281 2281
2282 private: 2282 private:
2283 ~ChainedBlobWriterImpl() override {} 2283 ~ChainedBlobWriterImpl() override {}
2284 2284
2285 void WriteNextFile() { 2285 void WriteNextFile() {
2286 DCHECK(!waiting_for_callback_); 2286 DCHECK(!waiting_for_callback_);
2287 if (aborted_) { 2287 if (aborted_) {
2288 self_ref_ = nullptr; 2288 self_ref_ = nullptr;
2289 return; 2289 return;
2290 } 2290 }
2291 if (iter_ == blobs_.end()) { 2291 if (iter_ == blobs_.end()) {
2292 DCHECK(!self_ref_.get()); 2292 DCHECK(!self_ref_.get());
2293 callback_->Run(true); 2293 callback_->Run(true, false);
jsbell 2016/11/30 21:30:13 ditto
dmurph 2016/11/30 23:13:07 Done.
2294 return; 2294 return;
2295 } else { 2295 } else {
2296 if (!backing_store_->WriteBlobFile(database_id_, *iter_, this)) { 2296 if (!backing_store_->WriteBlobFile(database_id_, *iter_, this)) {
2297 callback_->Run(false); 2297 callback_->Run(false, false);
jsbell 2016/11/30 21:30:13 ditto
dmurph 2016/11/30 23:13:06 Done.
2298 return; 2298 return;
2299 } 2299 }
2300 waiting_for_callback_ = true; 2300 waiting_for_callback_ = true;
2301 } 2301 }
2302 } 2302 }
2303 2303
2304 bool waiting_for_callback_; 2304 bool waiting_for_callback_;
2305 scoped_refptr<ChainedBlobWriterImpl> self_ref_; 2305 scoped_refptr<ChainedBlobWriterImpl> self_ref_;
2306 WriteDescriptorVec blobs_; 2306 WriteDescriptorVec blobs_;
2307 WriteDescriptorVec::const_iterator iter_; 2307 WriteDescriptorVec::const_iterator iter_;
2308 int64_t database_id_; 2308 int64_t database_id_;
2309 IndexedDBBackingStore* backing_store_; 2309 IndexedDBBackingStore* backing_store_;
2310 // Callback result is useless as call stack is no longer transaction's
2311 // operations queue. Errors are instead handled in
2312 // IndexedDBTransaction::BlobWriteComplete.
2310 scoped_refptr<IndexedDBBackingStore::BlobWriteCallback> callback_; 2313 scoped_refptr<IndexedDBBackingStore::BlobWriteCallback> callback_;
2311 std::unique_ptr<FileWriterDelegate> delegate_; 2314 std::unique_ptr<FileWriterDelegate> delegate_;
2312 bool aborted_; 2315 bool aborted_;
2313 2316
2314 DISALLOW_COPY_AND_ASSIGN(ChainedBlobWriterImpl); 2317 DISALLOW_COPY_AND_ASSIGN(ChainedBlobWriterImpl);
2315 }; 2318 };
2316 2319
2317 class LocalWriteClosure : public FileWriterDelegate::DelegateWriteCallback, 2320 class LocalWriteClosure : public FileWriterDelegate::DelegateWriteCallback,
2318 public base::RefCountedThreadSafe<LocalWriteClosure> { 2321 public base::RefCountedThreadSafe<LocalWriteClosure> {
2319 public: 2322 public:
(...skipping 1718 matching lines...) Expand 10 before | Expand all | Expand 10 after
4038 std::unique_ptr<IndexCursorImpl> cursor( 4041 std::unique_ptr<IndexCursorImpl> cursor(
4039 new IndexCursorImpl(this, transaction, database_id, cursor_options)); 4042 new IndexCursorImpl(this, transaction, database_id, cursor_options));
4040 if (!cursor->FirstSeek(s)) 4043 if (!cursor->FirstSeek(s))
4041 return std::unique_ptr<IndexedDBBackingStore::Cursor>(); 4044 return std::unique_ptr<IndexedDBBackingStore::Cursor>();
4042 4045
4043 return std::move(cursor); 4046 return std::move(cursor);
4044 } 4047 }
4045 4048
4046 IndexedDBBackingStore::Transaction::Transaction( 4049 IndexedDBBackingStore::Transaction::Transaction(
4047 IndexedDBBackingStore* backing_store) 4050 IndexedDBBackingStore* backing_store)
4048 : backing_store_(backing_store), database_id_(-1), committing_(false) { 4051 : backing_store_(backing_store),
4049 } 4052 database_id_(-1),
4053 committing_(false),
4054 ptr_factory_(this) {}
4050 4055
4051 IndexedDBBackingStore::Transaction::~Transaction() { 4056 IndexedDBBackingStore::Transaction::~Transaction() {
4052 DCHECK(!committing_); 4057 DCHECK(!committing_);
4053 } 4058 }
4054 4059
4055 void IndexedDBBackingStore::Transaction::Begin() { 4060 void IndexedDBBackingStore::Transaction::Begin() {
4056 IDB_TRACE("IndexedDBBackingStore::Transaction::Begin"); 4061 IDB_TRACE("IndexedDBBackingStore::Transaction::Begin");
4057 DCHECK(!transaction_.get()); 4062 DCHECK(!transaction_.get());
4058 transaction_ = IndexedDBClassFactory::Get()->CreateLevelDBTransaction( 4063 transaction_ = IndexedDBClassFactory::Get()->CreateLevelDBTransaction(
4059 backing_store_->db_.get()); 4064 backing_store_->db_.get());
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
4208 } 4213 }
4209 4214
4210 committing_ = true; 4215 committing_ = true;
4211 ++backing_store_->committing_transaction_count_; 4216 ++backing_store_->committing_transaction_count_;
4212 4217
4213 if (!new_files_to_write.empty()) { 4218 if (!new_files_to_write.empty()) {
4214 // This kicks off the writes of the new blobs, if any. 4219 // This kicks off the writes of the new blobs, if any.
4215 // This call will zero out new_blob_entries and new_files_to_write. 4220 // This call will zero out new_blob_entries and new_files_to_write.
4216 WriteNewBlobs(&new_blob_entries, &new_files_to_write, callback); 4221 WriteNewBlobs(&new_blob_entries, &new_files_to_write, callback);
4217 } else { 4222 } else {
4218 callback->Run(true); 4223 return callback->Run(true, true);
4219 } 4224 }
4220 4225
4221 return leveldb::Status::OK(); 4226 return leveldb::Status::OK();
4222 } 4227 }
4223 4228
4224 leveldb::Status IndexedDBBackingStore::Transaction::CommitPhaseTwo() { 4229 leveldb::Status IndexedDBBackingStore::Transaction::CommitPhaseTwo() {
4225 IDB_TRACE("IndexedDBBackingStore::Transaction::CommitPhaseTwo"); 4230 IDB_TRACE("IndexedDBBackingStore::Transaction::CommitPhaseTwo");
4226 leveldb::Status s; 4231 leveldb::Status s;
4227 4232
4228 DCHECK(committing_); 4233 DCHECK(committing_);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
4313 UpdatePrimaryBlobJournal(update_journal_transaction.get(), 4318 UpdatePrimaryBlobJournal(update_journal_transaction.get(),
4314 saved_primary_journal); 4319 saved_primary_journal);
4315 s = update_journal_transaction->Commit(); 4320 s = update_journal_transaction->Commit();
4316 return s; 4321 return s;
4317 } 4322 }
4318 4323
4319 4324
4320 class IndexedDBBackingStore::Transaction::BlobWriteCallbackWrapper 4325 class IndexedDBBackingStore::Transaction::BlobWriteCallbackWrapper
4321 : public IndexedDBBackingStore::BlobWriteCallback { 4326 : public IndexedDBBackingStore::BlobWriteCallback {
4322 public: 4327 public:
4323 BlobWriteCallbackWrapper(IndexedDBBackingStore::Transaction* transaction, 4328 BlobWriteCallbackWrapper(
4324 scoped_refptr<BlobWriteCallback> callback) 4329 base::WeakPtr<IndexedDBBackingStore::Transaction> transaction,
4325 : transaction_(transaction), callback_(callback) {} 4330 void* tracing_end_ptr,
4326 void Run(bool succeeded) override { 4331 scoped_refptr<BlobWriteCallback> callback)
4332 : transaction_(std::move(transaction)),
4333 tracing_end_ptr_(tracing_end_ptr),
4334 callback_(callback) {}
4335 leveldb::Status Run(bool succeeded, bool sync_call) override {
4336 DCHECK(!sync_call);
4327 IDB_ASYNC_TRACE_END("IndexedDBBackingStore::Transaction::WriteNewBlobs", 4337 IDB_ASYNC_TRACE_END("IndexedDBBackingStore::Transaction::WriteNewBlobs",
4328 transaction_); 4338 tracing_end_ptr_);
4329 callback_->Run(succeeded); 4339 leveldb::Status result = callback_->Run(succeeded, false);
jsbell 2016/11/30 21:30:13 sync_call ?
dmurph 2016/11/30 23:13:06 Done.
4330 if (succeeded) // Else it's already been deleted during rollback. 4340 if (transaction_ && succeeded)
4331 transaction_->chained_blob_writer_ = NULL; 4341 transaction_->chained_blob_writer_ = nullptr;
4342 return result;
4332 } 4343 }
4333 4344
4334 private: 4345 private:
4335 ~BlobWriteCallbackWrapper() override {} 4346 ~BlobWriteCallbackWrapper() override {}
4336 friend class base::RefCounted<IndexedDBBackingStore::BlobWriteCallback>; 4347 friend class base::RefCounted<IndexedDBBackingStore::BlobWriteCallback>;
4337 4348
4338 IndexedDBBackingStore::Transaction* transaction_; 4349 base::WeakPtr<IndexedDBBackingStore::Transaction> transaction_;
4350 const void* const tracing_end_ptr_;
4339 scoped_refptr<BlobWriteCallback> callback_; 4351 scoped_refptr<BlobWriteCallback> callback_;
4340 4352
4341 DISALLOW_COPY_AND_ASSIGN(BlobWriteCallbackWrapper); 4353 DISALLOW_COPY_AND_ASSIGN(BlobWriteCallbackWrapper);
4342 }; 4354 };
4343 4355
4344 void IndexedDBBackingStore::Transaction::WriteNewBlobs( 4356 void IndexedDBBackingStore::Transaction::WriteNewBlobs(
4345 BlobEntryKeyValuePairVec* new_blob_entries, 4357 BlobEntryKeyValuePairVec* new_blob_entries,
4346 WriteDescriptorVec* new_files_to_write, 4358 WriteDescriptorVec* new_files_to_write,
4347 scoped_refptr<BlobWriteCallback> callback) { 4359 scoped_refptr<BlobWriteCallback> callback) {
4348 IDB_ASYNC_TRACE_BEGIN("IndexedDBBackingStore::Transaction::WriteNewBlobs", 4360 IDB_ASYNC_TRACE_BEGIN("IndexedDBBackingStore::Transaction::WriteNewBlobs",
4349 this); 4361 this);
4350 DCHECK(!new_files_to_write->empty()); 4362 DCHECK(!new_files_to_write->empty());
4351 DCHECK_GT(database_id_, 0); 4363 DCHECK_GT(database_id_, 0);
4352 for (auto& blob_entry_iter : *new_blob_entries) { 4364 for (auto& blob_entry_iter : *new_blob_entries) {
4353 // Add the new blob-table entry for each blob to the main transaction, or 4365 // Add the new blob-table entry for each blob to the main transaction, or
4354 // remove any entry that may exist if there's no new one. 4366 // remove any entry that may exist if there's no new one.
4355 if (blob_entry_iter.second.empty()) 4367 if (blob_entry_iter.second.empty())
4356 transaction_->Remove(blob_entry_iter.first.Encode()); 4368 transaction_->Remove(blob_entry_iter.first.Encode());
4357 else 4369 else
4358 transaction_->Put(blob_entry_iter.first.Encode(), 4370 transaction_->Put(blob_entry_iter.first.Encode(),
4359 &blob_entry_iter.second); 4371 &blob_entry_iter.second);
4360 } 4372 }
4361 // Creating the writer will start it going asynchronously. 4373 // Creating the writer will start it going asynchronously. The transaction
4362 chained_blob_writer_ = 4374 // can be destructed before the callback is triggered.
4363 new ChainedBlobWriterImpl(database_id_, 4375 chained_blob_writer_ = new ChainedBlobWriterImpl(
4364 backing_store_, 4376 database_id_, backing_store_, new_files_to_write,
4365 new_files_to_write, 4377 new BlobWriteCallbackWrapper(ptr_factory_.GetWeakPtr(), this, callback));
4366 new BlobWriteCallbackWrapper(this, callback));
4367 } 4378 }
4368 4379
4369 void IndexedDBBackingStore::Transaction::Rollback() { 4380 void IndexedDBBackingStore::Transaction::Rollback() {
4370 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback"); 4381 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback");
4371 if (committing_) { 4382 if (committing_) {
4372 committing_ = false; 4383 committing_ = false;
4373 DCHECK_GT(backing_store_->committing_transaction_count_, 0UL); 4384 DCHECK_GT(backing_store_->committing_transaction_count_, 0UL);
4374 --backing_store_->committing_transaction_count_; 4385 --backing_store_->committing_transaction_count_;
4375 } 4386 }
4376 4387
4377 if (chained_blob_writer_.get()) { 4388 if (chained_blob_writer_.get()) {
4378 chained_blob_writer_->Abort(); 4389 chained_blob_writer_->Abort();
4379 chained_blob_writer_ = NULL; 4390 chained_blob_writer_ = NULL;
4380 } 4391 }
4381 if (transaction_.get() == NULL) 4392 if (!transaction_)
4382 return; 4393 return;
4383 transaction_->Rollback(); 4394 transaction_->Rollback();
4384 transaction_ = NULL; 4395 transaction_ = NULL;
4385 } 4396 }
4386 4397
4387 IndexedDBBackingStore::BlobChangeRecord::BlobChangeRecord( 4398 IndexedDBBackingStore::BlobChangeRecord::BlobChangeRecord(
4388 const std::string& key, 4399 const std::string& key,
4389 int64_t object_store_id) 4400 int64_t object_store_id)
4390 : key_(key), object_store_id_(object_store_id) {} 4401 : key_(key), object_store_id_(object_store_id) {}
4391 4402
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
4508 4519
4509 IndexedDBBackingStore::Transaction::WriteDescriptor::WriteDescriptor( 4520 IndexedDBBackingStore::Transaction::WriteDescriptor::WriteDescriptor(
4510 const WriteDescriptor& other) = default; 4521 const WriteDescriptor& other) = default;
4511 IndexedDBBackingStore::Transaction::WriteDescriptor::~WriteDescriptor() = 4522 IndexedDBBackingStore::Transaction::WriteDescriptor::~WriteDescriptor() =
4512 default; 4523 default;
4513 IndexedDBBackingStore::Transaction::WriteDescriptor& 4524 IndexedDBBackingStore::Transaction::WriteDescriptor&
4514 IndexedDBBackingStore::Transaction::WriteDescriptor:: 4525 IndexedDBBackingStore::Transaction::WriteDescriptor::
4515 operator=(const WriteDescriptor& other) = default; 4526 operator=(const WriteDescriptor& other) = default;
4516 4527
4517 } // namespace content 4528 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698