| 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 saved_cursor_.reset(); | 112 saved_cursor_.reset(); |
| 112 const size_t max_size_estimate = 10 * 1024 * 1024; | 113 const size_t max_size_estimate = 10 * 1024 * 1024; |
| 113 size_t size_estimate = 0; | 114 size_t size_estimate = 0; |
| 114 | 115 |
| 115 for (int i = 0; i < number_to_fetch; ++i) { | 116 for (int i = 0; i < number_to_fetch; ++i) { |
| 116 if (!cursor_ || !cursor_->Continue()) { | 117 if (!cursor_ || !cursor_->Continue()) { |
| 117 cursor_.reset(); | 118 cursor_.reset(); |
| 118 break; | 119 break; |
| 119 } | 120 } |
| 120 | 121 |
| 121 if (i == 0) { | 122 if (i == 0) { |
| 122 // First prefetched result is always used, so that's the position | 123 // First prefetched result is always used, so that's the position |
| 123 // a cursor should be reset to if the prefetch is invalidated. | 124 // a cursor should be reset to if the prefetch is invalidated. |
| 124 saved_cursor_.reset(cursor_->Clone()); | 125 saved_cursor_.reset(cursor_->Clone()); |
| 125 } | 126 } |
| 126 | 127 |
| 127 found_keys.push_back(cursor_->key()); | 128 found_keys.push_back(cursor_->key()); |
| 128 found_primary_keys.push_back(cursor_->primary_key()); | 129 found_primary_keys.push_back(cursor_->primary_key()); |
| 129 | 130 |
| 130 switch (cursor_type_) { | 131 switch (cursor_type_) { |
| 131 case indexed_db::CURSOR_KEY_ONLY: | 132 case indexed_db::CURSOR_KEY_ONLY: |
| 132 found_values.push_back(std::string()); | 133 found_values.push_back(IndexedDBValue()); |
| 133 break; | 134 break; |
| 134 case indexed_db::CURSOR_KEY_AND_VALUE: { | 135 case indexed_db::CURSOR_KEY_AND_VALUE: { |
| 135 std::string value; | 136 IndexedDBValue value; |
| 136 value.swap(*cursor_->value()); | 137 value.swap(*cursor_->value()); |
| 137 size_estimate += value.size(); | 138 size_estimate += value.SizeEstimate(); |
| 138 found_values.push_back(value); | 139 found_values.push_back(value); |
| 139 break; | 140 break; |
| 140 } | 141 } |
| 141 default: | 142 default: |
| 142 NOTREACHED(); | 143 NOTREACHED(); |
| 143 } | 144 } |
| 144 size_estimate += cursor_->key().size_estimate(); | 145 size_estimate += cursor_->key().size_estimate(); |
| 145 size_estimate += cursor_->primary_key().size_estimate(); | 146 size_estimate += cursor_->primary_key().size_estimate(); |
| 146 | 147 |
| 147 if (size_estimate > max_size_estimate) | 148 if (size_estimate > max_size_estimate) |
| 148 break; | 149 break; |
| 149 } | 150 } |
| 150 | 151 |
| 151 if (!found_keys.size()) { | 152 if (!found_keys.size()) { |
| 152 callbacks->OnSuccess(static_cast<std::string*>(NULL)); | 153 callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL)); |
| 153 return; | 154 return; |
| 154 } | 155 } |
| 155 | 156 |
| 156 callbacks->OnSuccessWithPrefetch( | 157 callbacks->OnSuccessWithPrefetch( |
| 157 found_keys, found_primary_keys, found_values); | 158 found_keys, found_primary_keys, found_values); |
| 158 } | 159 } |
| 159 | 160 |
| 160 void IndexedDBCursor::PrefetchReset(int used_prefetches, | 161 void IndexedDBCursor::PrefetchReset(int used_prefetches, |
| 161 int /* unused_prefetches */) { | 162 int /* unused_prefetches */) { |
| 162 IDB_TRACE("IndexedDBCursor::PrefetchReset"); | 163 IDB_TRACE("IndexedDBCursor::PrefetchReset"); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 176 } | 177 } |
| 177 | 178 |
| 178 void IndexedDBCursor::Close() { | 179 void IndexedDBCursor::Close() { |
| 179 IDB_TRACE("IndexedDBCursor::Close"); | 180 IDB_TRACE("IndexedDBCursor::Close"); |
| 180 closed_ = true; | 181 closed_ = true; |
| 181 cursor_.reset(); | 182 cursor_.reset(); |
| 182 saved_cursor_.reset(); | 183 saved_cursor_.reset(); |
| 183 } | 184 } |
| 184 | 185 |
| 185 } // namespace content | 186 } // namespace content |
| OLD | NEW |