Chromium Code Reviews| Index: content/browser/indexed_db/indexed_db_cursor.cc |
| diff --git a/content/browser/indexed_db/indexed_db_cursor.cc b/content/browser/indexed_db/indexed_db_cursor.cc |
| index f4b2c5fd6f99287c7752f1e0e70c80975af8c0bf..0fe947d9f2f858a2d4914f5a7272201ef96a6835 100644 |
| --- a/content/browser/indexed_db/indexed_db_cursor.cc |
| +++ b/content/browser/indexed_db/indexed_db_cursor.cc |
| @@ -18,6 +18,34 @@ |
| #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseException.h" |
| namespace content { |
| +namespace { |
| +IndexedDBDatabaseError CreateCursorClosedError() { |
| + return IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, |
|
jsbell
2016/11/30 21:30:14
Maybe add a comment that this should never be scri
dmurph
2016/11/30 23:13:07
Done.
|
| + "The cursor has been closed."); |
| +} |
| + |
| +leveldb::Status InvokeOrSucceed(base::WeakPtr<IndexedDBCursor> weak_cursor, |
| + IndexedDBTransaction::Operation operation, |
| + IndexedDBTransaction* transaction) { |
| + if (weak_cursor) |
| + return operation.Run(transaction); |
| + return leveldb::Status::OK(); |
| +} |
| + |
| +template <typename Functor, typename... Args> |
| +IndexedDBTransaction::Operation BindWeakOperation( |
|
jsbell
2016/11/30 21:30:14
Add a comment on usage here.
As an alternative to
dmurph
2016/11/30 23:13:07
Done.
|
| + Functor&& functor, |
| + base::WeakPtr<IndexedDBCursor> weak_cursor, |
| + Args&&... args) { |
| + DCHECK(weak_cursor); |
| + IndexedDBCursor* cursor_ptr = weak_cursor.get(); |
| + return base::Bind( |
| + &InvokeOrSucceed, std::move(weak_cursor), |
| + base::Bind(std::forward<Functor>(functor), base::Unretained(cursor_ptr), |
| + std::forward<Args>(args)...)); |
| +} |
| + |
| +} // namespace |
| IndexedDBCursor::IndexedDBCursor( |
| std::unique_ptr<IndexedDBBackingStore::Cursor> cursor, |
| @@ -28,13 +56,10 @@ IndexedDBCursor::IndexedDBCursor( |
| cursor_type_(cursor_type), |
| transaction_(transaction), |
| cursor_(std::move(cursor)), |
| - closed_(false) { |
| - transaction_->RegisterOpenCursor(this); |
| -} |
| + closed_(false), |
| + ptr_factory_(this) {} |
| -IndexedDBCursor::~IndexedDBCursor() { |
| - transaction_->UnregisterOpenCursor(this); |
| -} |
| +IndexedDBCursor::~IndexedDBCursor() {} |
| void IndexedDBCursor::Continue(std::unique_ptr<IndexedDBKey> key, |
| std::unique_ptr<IndexedDBKey> primary_key, |
| @@ -42,19 +67,15 @@ void IndexedDBCursor::Continue(std::unique_ptr<IndexedDBKey> key, |
| IDB_TRACE("IndexedDBCursor::Continue"); |
| if (closed_) { |
| - callbacks->OnError( |
| - IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, |
| - "The cursor has been closed.")); |
| + callbacks->OnError(CreateCursorClosedError()); |
| return; |
| } |
| transaction_->ScheduleTask( |
| task_type_, |
| - base::Bind(&IndexedDBCursor::CursorIterationOperation, |
| - this, |
| - base::Passed(&key), |
| - base::Passed(&primary_key), |
| - callbacks)); |
| + BindWeakOperation(&IndexedDBCursor::CursorIterationOperation, |
| + ptr_factory_.GetWeakPtr(), base::Passed(&key), |
| + base::Passed(&primary_key), callbacks)); |
| } |
| void IndexedDBCursor::Advance(uint32_t count, |
| @@ -62,16 +83,27 @@ void IndexedDBCursor::Advance(uint32_t count, |
| IDB_TRACE("IndexedDBCursor::Advance"); |
| if (closed_) { |
| - callbacks->OnError( |
| - IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, |
| - "The cursor has been closed.")); |
| + callbacks->OnError(CreateCursorClosedError()); |
| return; |
| } |
| transaction_->ScheduleTask( |
| task_type_, |
| - base::Bind( |
| - &IndexedDBCursor::CursorAdvanceOperation, this, count, callbacks)); |
| + BindWeakOperation(&IndexedDBCursor::CursorAdvanceOperation, |
| + ptr_factory_.GetWeakPtr(), count, callbacks)); |
| +} |
| + |
| +void IndexedDBCursor::Close() { |
| + IDB_TRACE("IndexedDBCursor::Close"); |
| + closed_ = true; |
| + cursor_.reset(); |
| + saved_cursor_.reset(); |
| + transaction_ = nullptr; |
| +} |
| + |
| +void IndexedDBCursor::RemoveCursorFromTransaction() { |
| + if (transaction_) |
| + transaction_->UnregisterOpenCursor(this); |
| } |
| leveldb::Status IndexedDBCursor::CursorAdvanceOperation( |
| @@ -131,18 +163,14 @@ void IndexedDBCursor::PrefetchContinue( |
| IDB_TRACE("IndexedDBCursor::PrefetchContinue"); |
| if (closed_) { |
| - callbacks->OnError( |
| - IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError, |
| - "The cursor has been closed.")); |
| + callbacks->OnError(CreateCursorClosedError()); |
| return; |
| } |
| transaction_->ScheduleTask( |
| task_type_, |
| - base::Bind(&IndexedDBCursor::CursorPrefetchIterationOperation, |
| - this, |
| - number_to_fetch, |
| - callbacks)); |
| + BindWeakOperation(&IndexedDBCursor::CursorPrefetchIterationOperation, |
| + ptr_factory_.GetWeakPtr(), number_to_fetch, callbacks)); |
| } |
| leveldb::Status IndexedDBCursor::CursorPrefetchIterationOperation( |
| @@ -239,11 +267,4 @@ leveldb::Status IndexedDBCursor::PrefetchReset(int used_prefetches, |
| return s; |
| } |
| -void IndexedDBCursor::Close() { |
| - IDB_TRACE("IndexedDBCursor::Close"); |
| - closed_ = true; |
| - cursor_.reset(); |
| - saved_cursor_.reset(); |
| -} |
| - |
| } // namespace content |