Add ICU message format support
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_cursor.cc
blob7172dab8f93a0d39aa4650c60e4e7297dbe2910d
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"
7 #include <vector>
9 #include "base/bind.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"
17 namespace content {
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()),
28 closed_(false) {
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(
42 task_type_,
43 base::Bind(&IndexedDBCursor::CursorIterationOperation,
44 this,
45 base::Passed(&key),
46 base::Passed(&primary_key),
47 callbacks));
50 void IndexedDBCursor::Advance(uint32 count,
51 scoped_refptr<IndexedDBCallbacks> callbacks) {
52 IDB_TRACE("IndexedDBCursor::Advance");
54 transaction_->ScheduleTask(
55 task_type_,
56 base::Bind(
57 &IndexedDBCursor::CursorAdvanceOperation, this, count, callbacks));
60 void IndexedDBCursor::CursorAdvanceOperation(
61 uint32 count,
62 scoped_refptr<IndexedDBCallbacks> callbacks,
63 IndexedDBTransaction* /*transaction*/) {
64 IDB_TRACE("IndexedDBCursor::CursorAdvanceOperation");
65 leveldb::Status s;
66 // TODO(cmumford): Handle this error (crbug.com/363397). Although this will
67 // properly fail, caller will not know why, and any corruption
68 // will be ignored.
69 if (!cursor_ || !cursor_->Advance(count, &s)) {
70 cursor_.reset();
71 callbacks->OnSuccess(nullptr);
72 return;
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");
84 leveldb::Status s;
85 // TODO(cmumford): Handle this error (crbug.com/363397). Although this will
86 // properly fail, caller will not know why, and any corruption
87 // will be ignored.
88 if (!cursor_ || !cursor_->Continue(key.get(),
89 primary_key.get(),
90 IndexedDBBackingStore::Cursor::SEEK,
91 &s) || !s.ok()) {
92 cursor_.reset();
93 callbacks->OnSuccess(nullptr);
94 return;
97 callbacks->OnSuccess(this->key(), this->primary_key(), Value());
100 void IndexedDBCursor::PrefetchContinue(
101 int number_to_fetch,
102 scoped_refptr<IndexedDBCallbacks> callbacks) {
103 IDB_TRACE("IndexedDBCursor::PrefetchContinue");
105 transaction_->ScheduleTask(
106 task_type_,
107 base::Bind(&IndexedDBCursor::CursorPrefetchIterationOperation,
108 this,
109 number_to_fetch,
110 callbacks));
113 void IndexedDBCursor::CursorPrefetchIterationOperation(
114 int number_to_fetch,
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;
127 leveldb::Status s;
129 // TODO(cmumford): Handle this error (crbug.com/363397). Although this will
130 // properly fail, caller will not know why, and any corruption
131 // will be ignored.
132 for (int i = 0; i < number_to_fetch; ++i) {
133 if (!cursor_ || !cursor_->Continue(&s)) {
134 cursor_.reset();
135 break;
138 if (i == 0) {
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());
150 break;
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);
156 break;
158 default:
159 NOTREACHED();
161 size_estimate += cursor_->key().size_estimate();
162 size_estimate += cursor_->primary_key().size_estimate();
164 if (size_estimate > max_size_estimate)
165 break;
168 if (!found_keys.size()) {
169 callbacks->OnSuccess(nullptr);
170 return;
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();
182 leveldb::Status s;
184 if (closed_)
185 return s;
186 if (cursor_) {
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);
191 DCHECK(ok);
195 return s;
198 void IndexedDBCursor::Close() {
199 IDB_TRACE("IndexedDBCursor::Close");
200 closed_ = true;
201 cursor_.reset();
202 saved_cursor_.reset();
205 } // namespace content