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 #include "content/browser/indexed_db/indexed_db_cursor.h"
10 #include "base/logging.h"
11 #include "content/browser/indexed_db/indexed_db_callbacks.h"
12 #include "content/browser/indexed_db/indexed_db_database_error.h"
13 #include "content/browser/indexed_db/indexed_db_tracing.h"
14 #include "content/browser/indexed_db/indexed_db_transaction.h"
15 #include "content/browser/indexed_db/indexed_db_value.h"
19 IndexedDBCursor::IndexedDBCursor(
20 scoped_ptr
<IndexedDBBackingStore::Cursor
> cursor
,
21 indexed_db::CursorType cursor_type
,
22 blink::WebIDBTaskType task_type
,
23 IndexedDBTransaction
* transaction
)
24 : task_type_(task_type
),
25 cursor_type_(cursor_type
),
26 transaction_(transaction
),
27 cursor_(cursor
.Pass()),
29 transaction_
->RegisterOpenCursor(this);
32 IndexedDBCursor::~IndexedDBCursor() {
33 transaction_
->UnregisterOpenCursor(this);
36 void IndexedDBCursor::Continue(scoped_ptr
<IndexedDBKey
> key
,
37 scoped_ptr
<IndexedDBKey
> primary_key
,
38 scoped_refptr
<IndexedDBCallbacks
> callbacks
) {
39 IDB_TRACE("IndexedDBCursor::Continue");
41 transaction_
->ScheduleTask(
43 base::Bind(&IndexedDBCursor::CursorIterationOperation
,
46 base::Passed(&primary_key
),
50 void IndexedDBCursor::Advance(uint32 count
,
51 scoped_refptr
<IndexedDBCallbacks
> callbacks
) {
52 IDB_TRACE("IndexedDBCursor::Advance");
54 transaction_
->ScheduleTask(
57 &IndexedDBCursor::CursorAdvanceOperation
, this, count
, callbacks
));
60 void IndexedDBCursor::CursorAdvanceOperation(
62 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
63 IndexedDBTransaction
* /*transaction*/) {
64 IDB_TRACE("IndexedDBCursor::CursorAdvanceOperation");
66 // TODO(cmumford): Handle this error (crbug.com/363397). Although this will
67 // properly fail, caller will not know why, and any corruption
69 if (!cursor_
|| !cursor_
->Advance(count
, &s
)) {
71 callbacks
->OnSuccess(nullptr);
75 callbacks
->OnSuccess(key(), primary_key(), Value());
78 void IndexedDBCursor::CursorIterationOperation(
79 scoped_ptr
<IndexedDBKey
> key
,
80 scoped_ptr
<IndexedDBKey
> primary_key
,
81 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
82 IndexedDBTransaction
* /*transaction*/) {
83 IDB_TRACE("IndexedDBCursor::CursorIterationOperation");
85 // TODO(cmumford): Handle this error (crbug.com/363397). Although this will
86 // properly fail, caller will not know why, and any corruption
88 if (!cursor_
|| !cursor_
->Continue(key
.get(),
90 IndexedDBBackingStore::Cursor::SEEK
,
93 callbacks
->OnSuccess(nullptr);
97 callbacks
->OnSuccess(this->key(), this->primary_key(), Value());
100 void IndexedDBCursor::PrefetchContinue(
102 scoped_refptr
<IndexedDBCallbacks
> callbacks
) {
103 IDB_TRACE("IndexedDBCursor::PrefetchContinue");
105 transaction_
->ScheduleTask(
107 base::Bind(&IndexedDBCursor::CursorPrefetchIterationOperation
,
113 void IndexedDBCursor::CursorPrefetchIterationOperation(
115 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
116 IndexedDBTransaction
* /*transaction*/) {
117 IDB_TRACE("IndexedDBCursor::CursorPrefetchIterationOperation");
119 std::vector
<IndexedDBKey
> found_keys
;
120 std::vector
<IndexedDBKey
> found_primary_keys
;
121 std::vector
<IndexedDBValue
> found_values
;
123 saved_cursor_
.reset();
124 // TODO(cmumford): Use IPC::Channel::kMaximumMessageSize
125 const size_t max_size_estimate
= 10 * 1024 * 1024;
126 size_t size_estimate
= 0;
129 // TODO(cmumford): Handle this error (crbug.com/363397). Although this will
130 // properly fail, caller will not know why, and any corruption
132 for (int i
= 0; i
< number_to_fetch
; ++i
) {
133 if (!cursor_
|| !cursor_
->Continue(&s
)) {
139 // First prefetched result is always used, so that's the position
140 // a cursor should be reset to if the prefetch is invalidated.
141 saved_cursor_
.reset(cursor_
->Clone());
144 found_keys
.push_back(cursor_
->key());
145 found_primary_keys
.push_back(cursor_
->primary_key());
147 switch (cursor_type_
) {
148 case indexed_db::CURSOR_KEY_ONLY
:
149 found_values
.push_back(IndexedDBValue());
151 case indexed_db::CURSOR_KEY_AND_VALUE
: {
152 IndexedDBValue value
;
153 value
.swap(*cursor_
->value());
154 size_estimate
+= value
.SizeEstimate();
155 found_values
.push_back(value
);
161 size_estimate
+= cursor_
->key().size_estimate();
162 size_estimate
+= cursor_
->primary_key().size_estimate();
164 if (size_estimate
> max_size_estimate
)
168 if (!found_keys
.size()) {
169 callbacks
->OnSuccess(nullptr);
173 callbacks
->OnSuccessWithPrefetch(
174 found_keys
, found_primary_keys
, &found_values
);
177 leveldb::Status
IndexedDBCursor::PrefetchReset(int used_prefetches
,
178 int /* unused_prefetches */) {
179 IDB_TRACE("IndexedDBCursor::PrefetchReset");
180 cursor_
.swap(saved_cursor_
);
181 saved_cursor_
.reset();
187 // First prefetched result is always used.
188 DCHECK_GT(used_prefetches
, 0);
189 for (int i
= 0; i
< used_prefetches
- 1; ++i
) {
190 bool ok
= cursor_
->Continue(&s
);
198 void IndexedDBCursor::Close() {
199 IDB_TRACE("IndexedDBCursor::Close");
202 saved_cursor_
.reset();
205 } // namespace content