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