OLD | NEW |
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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "content/browser/indexed_db/indexed_db_callbacks.h" | 9 #include "content/browser/indexed_db/indexed_db_callbacks.h" |
10 #include "content/browser/indexed_db/indexed_db_database_error.h" | 10 #include "content/browser/indexed_db/indexed_db_database_error.h" |
11 #include "content/browser/indexed_db/indexed_db_tracing.h" | 11 #include "content/browser/indexed_db/indexed_db_tracing.h" |
12 #include "content/browser/indexed_db/indexed_db_transaction.h" | 12 #include "content/browser/indexed_db/indexed_db_transaction.h" |
| 13 #include "content/browser/indexed_db/indexed_db_value.h" |
13 | 14 |
14 namespace content { | 15 namespace content { |
15 | 16 |
16 IndexedDBCursor::IndexedDBCursor( | 17 IndexedDBCursor::IndexedDBCursor( |
17 scoped_ptr<IndexedDBBackingStore::Cursor> cursor, | 18 scoped_ptr<IndexedDBBackingStore::Cursor> cursor, |
18 indexed_db::CursorType cursor_type, | 19 indexed_db::CursorType cursor_type, |
19 IndexedDBDatabase::TaskType task_type, | 20 IndexedDBDatabase::TaskType task_type, |
20 IndexedDBTransaction* transaction) | 21 IndexedDBTransaction* transaction) |
21 : task_type_(task_type), | 22 : task_type_(task_type), |
22 cursor_type_(cursor_type), | 23 cursor_type_(cursor_type), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 &IndexedDBCursor::CursorAdvanceOperation, this, count, callbacks)); | 55 &IndexedDBCursor::CursorAdvanceOperation, this, count, callbacks)); |
55 } | 56 } |
56 | 57 |
57 void IndexedDBCursor::CursorAdvanceOperation( | 58 void IndexedDBCursor::CursorAdvanceOperation( |
58 uint32 count, | 59 uint32 count, |
59 scoped_refptr<IndexedDBCallbacks> callbacks, | 60 scoped_refptr<IndexedDBCallbacks> callbacks, |
60 IndexedDBTransaction* /*transaction*/) { | 61 IndexedDBTransaction* /*transaction*/) { |
61 IDB_TRACE("IndexedDBCursor::CursorAdvanceOperation"); | 62 IDB_TRACE("IndexedDBCursor::CursorAdvanceOperation"); |
62 if (!cursor_ || !cursor_->Advance(count)) { | 63 if (!cursor_ || !cursor_->Advance(count)) { |
63 cursor_.reset(); | 64 cursor_.reset(); |
64 callbacks->OnSuccess(static_cast<std::string*>(NULL)); | 65 callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL)); |
65 return; | 66 return; |
66 } | 67 } |
67 | 68 |
68 callbacks->OnSuccess(key(), primary_key(), Value()); | 69 callbacks->OnSuccess(key(), primary_key(), Value()); |
69 } | 70 } |
70 | 71 |
71 void IndexedDBCursor::CursorIterationOperation( | 72 void IndexedDBCursor::CursorIterationOperation( |
72 scoped_ptr<IndexedDBKey> key, | 73 scoped_ptr<IndexedDBKey> key, |
73 scoped_ptr<IndexedDBKey> primary_key, | 74 scoped_ptr<IndexedDBKey> primary_key, |
74 scoped_refptr<IndexedDBCallbacks> callbacks, | 75 scoped_refptr<IndexedDBCallbacks> callbacks, |
75 IndexedDBTransaction* /*transaction*/) { | 76 IndexedDBTransaction* /*transaction*/) { |
76 IDB_TRACE("IndexedDBCursor::CursorIterationOperation"); | 77 IDB_TRACE("IndexedDBCursor::CursorIterationOperation"); |
77 if (!cursor_ || | 78 if (!cursor_ || |
78 !cursor_->Continue( | 79 !cursor_->Continue( |
79 key.get(), primary_key.get(), IndexedDBBackingStore::Cursor::SEEK)) { | 80 key.get(), primary_key.get(), IndexedDBBackingStore::Cursor::SEEK)) { |
80 cursor_.reset(); | 81 cursor_.reset(); |
81 callbacks->OnSuccess(static_cast<std::string*>(NULL)); | 82 callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL)); |
82 return; | 83 return; |
83 } | 84 } |
84 | 85 |
85 callbacks->OnSuccess(this->key(), this->primary_key(), Value()); | 86 callbacks->OnSuccess(this->key(), this->primary_key(), Value()); |
86 } | 87 } |
87 | 88 |
88 void IndexedDBCursor::PrefetchContinue( | 89 void IndexedDBCursor::PrefetchContinue( |
89 int number_to_fetch, | 90 int number_to_fetch, |
90 scoped_refptr<IndexedDBCallbacks> callbacks) { | 91 scoped_refptr<IndexedDBCallbacks> callbacks) { |
91 IDB_TRACE("IndexedDBCursor::PrefetchContinue"); | 92 IDB_TRACE("IndexedDBCursor::PrefetchContinue"); |
92 | 93 |
93 transaction_->ScheduleTask( | 94 transaction_->ScheduleTask( |
94 task_type_, | 95 task_type_, |
95 base::Bind(&IndexedDBCursor::CursorPrefetchIterationOperation, | 96 base::Bind(&IndexedDBCursor::CursorPrefetchIterationOperation, |
96 this, | 97 this, |
97 number_to_fetch, | 98 number_to_fetch, |
98 callbacks)); | 99 callbacks)); |
99 } | 100 } |
100 | 101 |
101 void IndexedDBCursor::CursorPrefetchIterationOperation( | 102 void IndexedDBCursor::CursorPrefetchIterationOperation( |
102 int number_to_fetch, | 103 int number_to_fetch, |
103 scoped_refptr<IndexedDBCallbacks> callbacks, | 104 scoped_refptr<IndexedDBCallbacks> callbacks, |
104 IndexedDBTransaction* /*transaction*/) { | 105 IndexedDBTransaction* /*transaction*/) { |
105 IDB_TRACE("IndexedDBCursor::CursorPrefetchIterationOperation"); | 106 IDB_TRACE("IndexedDBCursor::CursorPrefetchIterationOperation"); |
106 | 107 |
107 std::vector<IndexedDBKey> found_keys; | 108 std::vector<IndexedDBKey> found_keys; |
108 std::vector<IndexedDBKey> found_primary_keys; | 109 std::vector<IndexedDBKey> found_primary_keys; |
109 std::vector<std::string> found_values; | 110 std::vector<IndexedDBValue> found_values; |
110 | 111 |
111 if (cursor_) | 112 if (cursor_) |
112 saved_cursor_.reset(cursor_->Clone()); | 113 saved_cursor_.reset(cursor_->Clone()); |
113 const size_t max_size_estimate = 10 * 1024 * 1024; | 114 const size_t max_size_estimate = 10 * 1024 * 1024; |
114 size_t size_estimate = 0; | 115 size_t size_estimate = 0; |
115 | 116 |
116 for (int i = 0; i < number_to_fetch; ++i) { | 117 for (int i = 0; i < number_to_fetch; ++i) { |
117 if (!cursor_ || !cursor_->Continue()) { | 118 if (!cursor_ || !cursor_->Continue()) { |
118 cursor_.reset(); | 119 cursor_.reset(); |
119 break; | 120 break; |
120 } | 121 } |
121 | 122 |
122 found_keys.push_back(cursor_->key()); | 123 found_keys.push_back(cursor_->key()); |
123 found_primary_keys.push_back(cursor_->primary_key()); | 124 found_primary_keys.push_back(cursor_->primary_key()); |
124 | 125 |
125 switch (cursor_type_) { | 126 switch (cursor_type_) { |
126 case indexed_db::CURSOR_KEY_ONLY: | 127 case indexed_db::CURSOR_KEY_ONLY: |
127 found_values.push_back(std::string()); | 128 // TODO(ericu): Can we skip this? |
| 129 found_values.push_back(IndexedDBValue()); |
128 break; | 130 break; |
129 case indexed_db::CURSOR_KEY_AND_VALUE: { | 131 case indexed_db::CURSOR_KEY_AND_VALUE: { |
130 std::string value; | 132 IndexedDBValue value; |
131 value.swap(*cursor_->Value()); | 133 value.swap(*cursor_->Value()); |
132 size_estimate += value.size(); | 134 size_estimate += value.bits.size(); |
133 found_values.push_back(value); | 135 found_values.push_back(value); |
134 break; | 136 break; |
135 } | 137 } |
136 default: | 138 default: |
137 NOTREACHED(); | 139 NOTREACHED(); |
138 } | 140 } |
139 size_estimate += cursor_->key().size_estimate(); | 141 size_estimate += cursor_->key().size_estimate(); |
140 size_estimate += cursor_->primary_key().size_estimate(); | 142 size_estimate += cursor_->primary_key().size_estimate(); |
141 | 143 |
142 if (size_estimate > max_size_estimate) | 144 if (size_estimate > max_size_estimate) |
143 break; | 145 break; |
144 } | 146 } |
145 | 147 |
146 if (!found_keys.size()) { | 148 if (!found_keys.size()) { |
147 callbacks->OnSuccess(static_cast<std::string*>(NULL)); | 149 callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL)); |
148 return; | 150 return; |
149 } | 151 } |
150 | 152 |
151 callbacks->OnSuccessWithPrefetch( | 153 callbacks->OnSuccessWithPrefetch( |
152 found_keys, found_primary_keys, found_values); | 154 found_keys, found_primary_keys, found_values); |
153 } | 155 } |
154 | 156 |
155 void IndexedDBCursor::PrefetchReset(int used_prefetches, int) { | 157 void IndexedDBCursor::PrefetchReset(int used_prefetches, int) { |
156 IDB_TRACE("IndexedDBCursor::PrefetchReset"); | 158 IDB_TRACE("IndexedDBCursor::PrefetchReset"); |
157 cursor_.swap(saved_cursor_); | 159 cursor_.swap(saved_cursor_); |
(...skipping 10 matching lines...) Expand all Loading... |
168 } | 170 } |
169 | 171 |
170 void IndexedDBCursor::Close() { | 172 void IndexedDBCursor::Close() { |
171 IDB_TRACE("IndexedDBCursor::Close"); | 173 IDB_TRACE("IndexedDBCursor::Close"); |
172 closed_ = true; | 174 closed_ = true; |
173 cursor_.reset(); | 175 cursor_.reset(); |
174 saved_cursor_.reset(); | 176 saved_cursor_.reset(); |
175 } | 177 } |
176 | 178 |
177 } // namespace content | 179 } // namespace content |
OLD | NEW |