OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/browser/indexed_db/indexed_db_cursor_impl.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "content/browser/indexed_db/indexed_db_backing_store.h" | |
9 #include "content/browser/indexed_db/indexed_db_callbacks_wrapper.h" | |
10 #include "content/browser/indexed_db/indexed_db_database_error.h" | |
11 #include "content/browser/indexed_db/indexed_db_database_impl.h" | |
12 #include "content/browser/indexed_db/indexed_db_tracing.h" | |
13 #include "content/browser/indexed_db/indexed_db_transaction.h" | |
14 #include "content/common/indexed_db/indexed_db_key_range.h" | |
15 | |
16 namespace content { | |
17 | |
18 class IndexedDBCursorImpl::CursorIterationOperation | |
19 : public IndexedDBTransaction::Operation { | |
20 public: | |
21 CursorIterationOperation(scoped_refptr<IndexedDBCursorImpl> cursor, | |
22 scoped_ptr<IndexedDBKey> key, | |
23 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) | |
24 : cursor_(cursor), key_(key.Pass()), callbacks_(callbacks) {} | |
25 virtual void Perform(IndexedDBTransaction* transaction) OVERRIDE; | |
26 | |
27 private: | |
28 scoped_refptr<IndexedDBCursorImpl> cursor_; | |
29 scoped_ptr<IndexedDBKey> key_; | |
30 scoped_refptr<IndexedDBCallbacksWrapper> callbacks_; | |
31 }; | |
32 | |
33 class IndexedDBCursorImpl::CursorAdvanceOperation | |
34 : public IndexedDBTransaction::Operation { | |
35 public: | |
36 CursorAdvanceOperation(scoped_refptr<IndexedDBCursorImpl> cursor, | |
37 unsigned long count, | |
38 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) | |
39 : cursor_(cursor), count_(count), callbacks_(callbacks) {} | |
40 virtual void Perform(IndexedDBTransaction* transaction) OVERRIDE; | |
41 | |
42 private: | |
43 scoped_refptr<IndexedDBCursorImpl> cursor_; | |
44 unsigned long count_; | |
45 scoped_refptr<IndexedDBCallbacksWrapper> callbacks_; | |
46 }; | |
47 | |
48 class IndexedDBCursorImpl::CursorPrefetchIterationOperation | |
49 : public IndexedDBTransaction::Operation { | |
50 public: | |
51 CursorPrefetchIterationOperation( | |
52 scoped_refptr<IndexedDBCursorImpl> cursor, | |
53 int number_to_fetch, | |
54 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) | |
55 : cursor_(cursor), | |
56 number_to_fetch_(number_to_fetch), | |
57 callbacks_(callbacks) {} | |
58 virtual void Perform(IndexedDBTransaction* transaction) OVERRIDE; | |
59 | |
60 private: | |
61 scoped_refptr<IndexedDBCursorImpl> cursor_; | |
62 int number_to_fetch_; | |
63 scoped_refptr<IndexedDBCallbacksWrapper> callbacks_; | |
64 }; | |
65 | |
66 IndexedDBCursorImpl::IndexedDBCursorImpl( | |
67 scoped_ptr<IndexedDBBackingStore::Cursor> cursor, | |
68 indexed_db::CursorType cursor_type, | |
69 IndexedDBDatabase::TaskType task_type, | |
70 IndexedDBTransaction* transaction, | |
71 int64 object_store_id) | |
72 : task_type_(task_type), | |
73 cursor_type_(cursor_type), | |
74 transaction_(transaction), | |
75 object_store_id_(object_store_id), | |
76 cursor_(cursor.Pass()), | |
77 closed_(false) { | |
78 transaction_->RegisterOpenCursor(this); | |
79 } | |
80 | |
81 IndexedDBCursorImpl::~IndexedDBCursorImpl() { | |
82 transaction_->UnregisterOpenCursor(this); | |
83 } | |
84 | |
85 void IndexedDBCursorImpl::ContinueFunction( | |
86 scoped_ptr<IndexedDBKey> key, | |
87 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) { | |
88 IDB_TRACE("IndexedDBCursorImpl::continue"); | |
89 | |
90 transaction_->ScheduleTask( | |
91 task_type_, new CursorIterationOperation(this, key.Pass(), callbacks)); | |
92 } | |
93 | |
94 void IndexedDBCursorImpl::Advance( | |
95 unsigned long count, | |
96 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) { | |
97 IDB_TRACE("IndexedDBCursorImpl::advance"); | |
98 | |
99 transaction_->ScheduleTask( | |
100 new CursorAdvanceOperation(this, count, callbacks)); | |
101 } | |
102 | |
103 void IndexedDBCursorImpl::CursorAdvanceOperation::Perform( | |
104 IndexedDBTransaction* /*transaction*/) { | |
105 IDB_TRACE("CursorAdvanceOperation"); | |
106 if (!cursor_->cursor_ || !cursor_->cursor_->Advance(count_)) { | |
107 cursor_->cursor_.reset(); | |
108 callbacks_->OnSuccess(static_cast<std::vector<char>*>(NULL)); | |
109 return; | |
110 } | |
111 | |
112 callbacks_->OnSuccess( | |
113 cursor_->key(), cursor_->primary_key(), cursor_->Value()); | |
114 } | |
115 | |
116 void IndexedDBCursorImpl::CursorIterationOperation::Perform( | |
117 IndexedDBTransaction* /*transaction*/) { | |
118 IDB_TRACE("CursorIterationOperation"); | |
119 if (!cursor_->cursor_ || !cursor_->cursor_->ContinueFunction(key_.get())) { | |
120 cursor_->cursor_.reset(); | |
121 callbacks_->OnSuccess(static_cast<std::vector<char>*>(NULL)); | |
122 return; | |
123 } | |
124 | |
125 callbacks_->OnSuccess( | |
126 cursor_->key(), cursor_->primary_key(), cursor_->Value()); | |
127 } | |
128 | |
129 void IndexedDBCursorImpl::DeleteFunction( | |
130 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) { | |
131 IDB_TRACE("IndexedDBCursorImpl::delete"); | |
132 DCHECK_NE(transaction_->mode(), indexed_db::TRANSACTION_READ_ONLY); | |
133 scoped_ptr<IndexedDBKeyRange> key_range = | |
134 make_scoped_ptr(new IndexedDBKeyRange(cursor_->primary_key())); | |
135 transaction_->database()->DeleteRange( | |
136 transaction_->id(), object_store_id_, key_range.Pass(), callbacks); | |
137 } | |
138 | |
139 void IndexedDBCursorImpl::PrefetchContinue( | |
140 int number_to_fetch, | |
141 scoped_refptr<IndexedDBCallbacksWrapper> callbacks) { | |
142 IDB_TRACE("IndexedDBCursorImpl::prefetch_continue"); | |
143 | |
144 transaction_->ScheduleTask( | |
145 task_type_, | |
146 new CursorPrefetchIterationOperation(this, number_to_fetch, callbacks)); | |
147 } | |
148 | |
149 void IndexedDBCursorImpl::CursorPrefetchIterationOperation::Perform( | |
150 IndexedDBTransaction* /*transaction*/) { | |
151 IDB_TRACE("CursorPrefetchIterationOperation"); | |
152 | |
153 std::vector<IndexedDBKey> found_keys; | |
154 std::vector<IndexedDBKey> found_primary_keys; | |
155 std::vector<std::vector<char> > found_values; | |
156 | |
157 if (cursor_->cursor_) | |
158 cursor_->saved_cursor_.reset(cursor_->cursor_->Clone()); | |
159 const size_t max_size_estimate = 10 * 1024 * 1024; | |
160 size_t size_estimate = 0; | |
161 | |
162 for (int i = 0; i < number_to_fetch_; ++i) { | |
163 if (!cursor_->cursor_ || !cursor_->cursor_->ContinueFunction(0)) { | |
164 cursor_->cursor_.reset(); | |
165 break; | |
166 } | |
167 | |
168 found_keys.push_back(cursor_->cursor_->key()); | |
169 found_primary_keys.push_back(cursor_->cursor_->primary_key()); | |
170 | |
171 switch (cursor_->cursor_type_) { | |
172 case indexed_db::CURSOR_KEY_ONLY: | |
173 found_values.push_back(std::vector<char>()); | |
174 break; | |
175 case indexed_db::CURSOR_KEY_AND_VALUE: { | |
176 std::vector<char> value; | |
177 value.swap(*cursor_->cursor_->Value()); | |
178 size_estimate += value.size(); | |
179 found_values.push_back(value); | |
180 break; | |
181 } | |
182 default: | |
183 NOTREACHED(); | |
184 } | |
185 size_estimate += cursor_->cursor_->key().size_estimate(); | |
186 size_estimate += cursor_->cursor_->primary_key().size_estimate(); | |
187 | |
188 if (size_estimate > max_size_estimate) | |
189 break; | |
190 } | |
191 | |
192 if (!found_keys.size()) { | |
193 callbacks_->OnSuccess(static_cast<std::vector<char>*>(NULL)); | |
194 return; | |
195 } | |
196 | |
197 callbacks_->OnSuccessWithPrefetch( | |
198 found_keys, found_primary_keys, found_values); | |
199 } | |
200 | |
201 void IndexedDBCursorImpl::PrefetchReset(int used_prefetches, int) { | |
202 IDB_TRACE("IndexedDBCursorImpl::prefetch_reset"); | |
203 cursor_.swap(saved_cursor_); | |
204 saved_cursor_.reset(); | |
205 | |
206 if (closed_) | |
207 return; | |
208 if (cursor_) { | |
209 for (int i = 0; i < used_prefetches; ++i) { | |
210 bool ok = cursor_->ContinueFunction(); | |
211 DCHECK(ok); | |
212 } | |
213 } | |
214 } | |
215 | |
216 void IndexedDBCursorImpl::Close() { | |
217 IDB_TRACE("IndexedDBCursorImpl::close"); | |
218 closed_ = true; | |
219 cursor_.reset(); | |
220 saved_cursor_.reset(); | |
221 } | |
222 | |
223 } // namespace content | |
OLD | NEW |