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

Side by Side Diff: content/browser/indexed_db/indexed_db_cursor.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_cursor.h" 5 #include "content/browser/indexed_db/indexed_db_cursor.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "content/browser/indexed_db/indexed_db_callbacks.h" 13 #include "content/browser/indexed_db/indexed_db_callbacks.h"
14 #include "content/browser/indexed_db/indexed_db_database_error.h" 14 #include "content/browser/indexed_db/indexed_db_database_error.h"
15 #include "content/browser/indexed_db/indexed_db_tracing.h" 15 #include "content/browser/indexed_db/indexed_db_tracing.h"
16 #include "content/browser/indexed_db/indexed_db_transaction.h" 16 #include "content/browser/indexed_db/indexed_db_transaction.h"
17 #include "content/browser/indexed_db/indexed_db_value.h" 17 #include "content/browser/indexed_db/indexed_db_value.h"
18 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h" 18 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h"
19 19
20 namespace content { 20 namespace content {
21 namespace {
22 IndexedDBDatabaseError CreateCursorClosedError() {
23 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.
24 "The cursor has been closed.");
25 }
26
27 leveldb::Status InvokeOrSucceed(base::WeakPtr<IndexedDBCursor> weak_cursor,
28 IndexedDBTransaction::Operation operation,
29 IndexedDBTransaction* transaction) {
30 if (weak_cursor)
31 return operation.Run(transaction);
32 return leveldb::Status::OK();
33 }
34
35 template <typename Functor, typename... Args>
36 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.
37 Functor&& functor,
38 base::WeakPtr<IndexedDBCursor> weak_cursor,
39 Args&&... args) {
40 DCHECK(weak_cursor);
41 IndexedDBCursor* cursor_ptr = weak_cursor.get();
42 return base::Bind(
43 &InvokeOrSucceed, std::move(weak_cursor),
44 base::Bind(std::forward<Functor>(functor), base::Unretained(cursor_ptr),
45 std::forward<Args>(args)...));
46 }
47
48 } // namespace
21 49
22 IndexedDBCursor::IndexedDBCursor( 50 IndexedDBCursor::IndexedDBCursor(
23 std::unique_ptr<IndexedDBBackingStore::Cursor> cursor, 51 std::unique_ptr<IndexedDBBackingStore::Cursor> cursor,
24 indexed_db::CursorType cursor_type, 52 indexed_db::CursorType cursor_type,
25 blink::WebIDBTaskType task_type, 53 blink::WebIDBTaskType task_type,
26 IndexedDBTransaction* transaction) 54 IndexedDBTransaction* transaction)
27 : task_type_(task_type), 55 : task_type_(task_type),
28 cursor_type_(cursor_type), 56 cursor_type_(cursor_type),
29 transaction_(transaction), 57 transaction_(transaction),
30 cursor_(std::move(cursor)), 58 cursor_(std::move(cursor)),
31 closed_(false) { 59 closed_(false),
32 transaction_->RegisterOpenCursor(this); 60 ptr_factory_(this) {}
33 }
34 61
35 IndexedDBCursor::~IndexedDBCursor() { 62 IndexedDBCursor::~IndexedDBCursor() {}
36 transaction_->UnregisterOpenCursor(this);
37 }
38 63
39 void IndexedDBCursor::Continue(std::unique_ptr<IndexedDBKey> key, 64 void IndexedDBCursor::Continue(std::unique_ptr<IndexedDBKey> key,
40 std::unique_ptr<IndexedDBKey> primary_key, 65 std::unique_ptr<IndexedDBKey> primary_key,
41 scoped_refptr<IndexedDBCallbacks> callbacks) { 66 scoped_refptr<IndexedDBCallbacks> callbacks) {
42 IDB_TRACE("IndexedDBCursor::Continue"); 67 IDB_TRACE("IndexedDBCursor::Continue");
43 68
44 if (closed_) { 69 if (closed_) {
45 callbacks->OnError( 70 callbacks->OnError(CreateCursorClosedError());
46 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError,
47 "The cursor has been closed."));
48 return; 71 return;
49 } 72 }
50 73
51 transaction_->ScheduleTask( 74 transaction_->ScheduleTask(
52 task_type_, 75 task_type_,
53 base::Bind(&IndexedDBCursor::CursorIterationOperation, 76 BindWeakOperation(&IndexedDBCursor::CursorIterationOperation,
54 this, 77 ptr_factory_.GetWeakPtr(), base::Passed(&key),
55 base::Passed(&key), 78 base::Passed(&primary_key), callbacks));
56 base::Passed(&primary_key),
57 callbacks));
58 } 79 }
59 80
60 void IndexedDBCursor::Advance(uint32_t count, 81 void IndexedDBCursor::Advance(uint32_t count,
61 scoped_refptr<IndexedDBCallbacks> callbacks) { 82 scoped_refptr<IndexedDBCallbacks> callbacks) {
62 IDB_TRACE("IndexedDBCursor::Advance"); 83 IDB_TRACE("IndexedDBCursor::Advance");
63 84
64 if (closed_) { 85 if (closed_) {
65 callbacks->OnError( 86 callbacks->OnError(CreateCursorClosedError());
66 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError,
67 "The cursor has been closed."));
68 return; 87 return;
69 } 88 }
70 89
71 transaction_->ScheduleTask( 90 transaction_->ScheduleTask(
72 task_type_, 91 task_type_,
73 base::Bind( 92 BindWeakOperation(&IndexedDBCursor::CursorAdvanceOperation,
74 &IndexedDBCursor::CursorAdvanceOperation, this, count, callbacks)); 93 ptr_factory_.GetWeakPtr(), count, callbacks));
94 }
95
96 void IndexedDBCursor::Close() {
97 IDB_TRACE("IndexedDBCursor::Close");
98 closed_ = true;
99 cursor_.reset();
100 saved_cursor_.reset();
101 transaction_ = nullptr;
102 }
103
104 void IndexedDBCursor::RemoveCursorFromTransaction() {
105 if (transaction_)
106 transaction_->UnregisterOpenCursor(this);
75 } 107 }
76 108
77 leveldb::Status IndexedDBCursor::CursorAdvanceOperation( 109 leveldb::Status IndexedDBCursor::CursorAdvanceOperation(
78 uint32_t count, 110 uint32_t count,
79 scoped_refptr<IndexedDBCallbacks> callbacks, 111 scoped_refptr<IndexedDBCallbacks> callbacks,
80 IndexedDBTransaction* /*transaction*/) { 112 IndexedDBTransaction* /*transaction*/) {
81 IDB_TRACE("IndexedDBCursor::CursorAdvanceOperation"); 113 IDB_TRACE("IndexedDBCursor::CursorAdvanceOperation");
82 leveldb::Status s = leveldb::Status::OK(); 114 leveldb::Status s = leveldb::Status::OK();
83 115
84 if (!cursor_ || !cursor_->Advance(count, &s)) { 116 if (!cursor_ || !cursor_->Advance(count, &s)) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 callbacks->OnSuccess(this->key(), this->primary_key(), Value()); 156 callbacks->OnSuccess(this->key(), this->primary_key(), Value());
125 return s; 157 return s;
126 } 158 }
127 159
128 void IndexedDBCursor::PrefetchContinue( 160 void IndexedDBCursor::PrefetchContinue(
129 int number_to_fetch, 161 int number_to_fetch,
130 scoped_refptr<IndexedDBCallbacks> callbacks) { 162 scoped_refptr<IndexedDBCallbacks> callbacks) {
131 IDB_TRACE("IndexedDBCursor::PrefetchContinue"); 163 IDB_TRACE("IndexedDBCursor::PrefetchContinue");
132 164
133 if (closed_) { 165 if (closed_) {
134 callbacks->OnError( 166 callbacks->OnError(CreateCursorClosedError());
135 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError,
136 "The cursor has been closed."));
137 return; 167 return;
138 } 168 }
139 169
140 transaction_->ScheduleTask( 170 transaction_->ScheduleTask(
141 task_type_, 171 task_type_,
142 base::Bind(&IndexedDBCursor::CursorPrefetchIterationOperation, 172 BindWeakOperation(&IndexedDBCursor::CursorPrefetchIterationOperation,
143 this, 173 ptr_factory_.GetWeakPtr(), number_to_fetch, callbacks));
144 number_to_fetch,
145 callbacks));
146 } 174 }
147 175
148 leveldb::Status IndexedDBCursor::CursorPrefetchIterationOperation( 176 leveldb::Status IndexedDBCursor::CursorPrefetchIterationOperation(
149 int number_to_fetch, 177 int number_to_fetch,
150 scoped_refptr<IndexedDBCallbacks> callbacks, 178 scoped_refptr<IndexedDBCallbacks> callbacks,
151 IndexedDBTransaction* /*transaction*/) { 179 IndexedDBTransaction* /*transaction*/) {
152 IDB_TRACE("IndexedDBCursor::CursorPrefetchIterationOperation"); 180 IDB_TRACE("IndexedDBCursor::CursorPrefetchIterationOperation");
153 leveldb::Status s = leveldb::Status::OK(); 181 leveldb::Status s = leveldb::Status::OK();
154 182
155 std::vector<IndexedDBKey> found_keys; 183 std::vector<IndexedDBKey> found_keys;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 DCHECK_GT(used_prefetches, 0); 260 DCHECK_GT(used_prefetches, 0);
233 for (int i = 0; i < used_prefetches - 1; ++i) { 261 for (int i = 0; i < used_prefetches - 1; ++i) {
234 bool ok = cursor_->Continue(&s); 262 bool ok = cursor_->Continue(&s);
235 DCHECK(ok); 263 DCHECK(ok);
236 } 264 }
237 } 265 }
238 266
239 return s; 267 return s;
240 } 268 }
241 269
242 void IndexedDBCursor::Close() {
243 IDB_TRACE("IndexedDBCursor::Close");
244 closed_ = true;
245 cursor_.reset();
246 saved_cursor_.reset();
247 }
248
249 } // namespace content 270 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698