| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/child/indexed_db/webidbcursor_impl.h" | 5 #include "content/child/indexed_db/webidbcursor_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "content/child/indexed_db/indexed_db_dispatcher.h" | 9 #include "content/child/indexed_db/indexed_db_dispatcher.h" |
| 10 #include "content/child/indexed_db/indexed_db_key_builders.h" | 10 #include "content/child/indexed_db/indexed_db_key_builders.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 IndexedDBDispatcher* dispatcher = | 40 IndexedDBDispatcher* dispatcher = |
| 41 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | 41 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); |
| 42 dispatcher->CursorDestroyed(ipc_cursor_id_); | 42 dispatcher->CursorDestroyed(ipc_cursor_id_); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void WebIDBCursorImpl::advance(unsigned long count, | 45 void WebIDBCursorImpl::advance(unsigned long count, |
| 46 WebIDBCallbacks* callbacks_ptr) { | 46 WebIDBCallbacks* callbacks_ptr) { |
| 47 IndexedDBDispatcher* dispatcher = | 47 IndexedDBDispatcher* dispatcher = |
| 48 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | 48 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); |
| 49 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | 49 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); |
| 50 if (count <= prefetch_keys_.size()) { |
| 51 CachedAdvance(count, callbacks.get()); |
| 52 return; |
| 53 } |
| 50 ResetPrefetchCache(); | 54 ResetPrefetchCache(); |
| 51 dispatcher->RequestIDBCursorAdvance( | 55 dispatcher->RequestIDBCursorAdvance( |
| 52 count, callbacks.release(), ipc_cursor_id_); | 56 count, callbacks.release(), ipc_cursor_id_); |
| 53 } | 57 } |
| 54 | 58 |
| 55 void WebIDBCursorImpl::continueFunction(const WebIDBKey& key, | 59 void WebIDBCursorImpl::continueFunction(const WebIDBKey& key, |
| 56 WebIDBCallbacks* callbacks_ptr) { | 60 WebIDBCallbacks* callbacks_ptr) { |
| 57 continueFunction(key, WebIDBKey::createNull(), callbacks_ptr); | 61 continueFunction(key, WebIDBKey::createNull(), callbacks_ptr); |
| 58 } | 62 } |
| 59 | 63 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 99 |
| 96 dispatcher->RequestIDBCursorContinue(IndexedDBKeyBuilder::Build(key), | 100 dispatcher->RequestIDBCursorContinue(IndexedDBKeyBuilder::Build(key), |
| 97 IndexedDBKeyBuilder::Build(primary_key), | 101 IndexedDBKeyBuilder::Build(primary_key), |
| 98 callbacks.release(), | 102 callbacks.release(), |
| 99 ipc_cursor_id_); | 103 ipc_cursor_id_); |
| 100 } | 104 } |
| 101 | 105 |
| 102 void WebIDBCursorImpl::postSuccessHandlerCallback() { | 106 void WebIDBCursorImpl::postSuccessHandlerCallback() { |
| 103 pending_onsuccess_callbacks_--; | 107 pending_onsuccess_callbacks_--; |
| 104 | 108 |
| 105 // If the onsuccess callback called continue() on the cursor again, | 109 // If the onsuccess callback called continue()/advance() on the cursor |
| 106 // and that continue was served by the prefetch cache, then | 110 // again, and that request was served by the prefetch cache, then |
| 107 // pending_onsuccess_callbacks_ would be incremented. | 111 // pending_onsuccess_callbacks_ would be incremented. If not, it means the |
| 108 // If not, it means the callback did something else, or nothing at all, | 112 // callback did something else, or nothing at all, in which case we need to |
| 109 // in which case we need to reset the cache. | 113 // reset the cache. |
| 110 | 114 |
| 111 if (pending_onsuccess_callbacks_ == 0) | 115 if (pending_onsuccess_callbacks_ == 0) |
| 112 ResetPrefetchCache(); | 116 ResetPrefetchCache(); |
| 113 } | 117 } |
| 114 | 118 |
| 115 void WebIDBCursorImpl::SetPrefetchData( | 119 void WebIDBCursorImpl::SetPrefetchData( |
| 116 const std::vector<IndexedDBKey>& keys, | 120 const std::vector<IndexedDBKey>& keys, |
| 117 const std::vector<IndexedDBKey>& primary_keys, | 121 const std::vector<IndexedDBKey>& primary_keys, |
| 118 const std::vector<WebData>& values) { | 122 const std::vector<WebData>& values) { |
| 119 prefetch_keys_.assign(keys.begin(), keys.end()); | 123 prefetch_keys_.assign(keys.begin(), keys.end()); |
| 120 prefetch_primary_keys_.assign(primary_keys.begin(), primary_keys.end()); | 124 prefetch_primary_keys_.assign(primary_keys.begin(), primary_keys.end()); |
| 121 prefetch_values_.assign(values.begin(), values.end()); | 125 prefetch_values_.assign(values.begin(), values.end()); |
| 122 | 126 |
| 123 used_prefetches_ = 0; | 127 used_prefetches_ = 0; |
| 124 pending_onsuccess_callbacks_ = 0; | 128 pending_onsuccess_callbacks_ = 0; |
| 125 } | 129 } |
| 126 | 130 |
| 131 void WebIDBCursorImpl::CachedAdvance(unsigned long count, |
| 132 WebIDBCallbacks* callbacks) { |
| 133 DCHECK_GE(prefetch_keys_.size(), count); |
| 134 DCHECK_EQ(prefetch_primary_keys_.size(), prefetch_keys_.size()); |
| 135 DCHECK_EQ(prefetch_values_.size(), prefetch_keys_.size()); |
| 136 |
| 137 while (count > 1) { |
| 138 prefetch_keys_.pop_front(); |
| 139 prefetch_primary_keys_.pop_front(); |
| 140 prefetch_values_.pop_front(); |
| 141 ++used_prefetches_; |
| 142 --count; |
| 143 } |
| 144 |
| 145 CachedContinue(callbacks); |
| 146 } |
| 147 |
| 127 void WebIDBCursorImpl::CachedContinue(WebIDBCallbacks* callbacks) { | 148 void WebIDBCursorImpl::CachedContinue(WebIDBCallbacks* callbacks) { |
| 128 DCHECK_GT(prefetch_keys_.size(), 0ul); | 149 DCHECK_GT(prefetch_keys_.size(), 0ul); |
| 129 DCHECK(prefetch_primary_keys_.size() == prefetch_keys_.size()); | 150 DCHECK_EQ(prefetch_primary_keys_.size(), prefetch_keys_.size()); |
| 130 DCHECK(prefetch_values_.size() == prefetch_keys_.size()); | 151 DCHECK_EQ(prefetch_values_.size(), prefetch_keys_.size()); |
| 131 | 152 |
| 132 IndexedDBKey key = prefetch_keys_.front(); | 153 IndexedDBKey key = prefetch_keys_.front(); |
| 133 IndexedDBKey primary_key = prefetch_primary_keys_.front(); | 154 IndexedDBKey primary_key = prefetch_primary_keys_.front(); |
| 134 // this could be a real problem.. we need 2 CachedContinues | |
| 135 WebData value = prefetch_values_.front(); | 155 WebData value = prefetch_values_.front(); |
| 136 | 156 |
| 137 prefetch_keys_.pop_front(); | 157 prefetch_keys_.pop_front(); |
| 138 prefetch_primary_keys_.pop_front(); | 158 prefetch_primary_keys_.pop_front(); |
| 139 prefetch_values_.pop_front(); | 159 prefetch_values_.pop_front(); |
| 140 used_prefetches_++; | 160 ++used_prefetches_; |
| 141 | 161 |
| 142 pending_onsuccess_callbacks_++; | 162 ++pending_onsuccess_callbacks_; |
| 143 | 163 |
| 144 callbacks->onSuccess(WebIDBKeyBuilder::Build(key), | 164 callbacks->onSuccess(WebIDBKeyBuilder::Build(key), |
| 145 WebIDBKeyBuilder::Build(primary_key), | 165 WebIDBKeyBuilder::Build(primary_key), |
| 146 value); | 166 value); |
| 147 } | 167 } |
| 148 | 168 |
| 149 void WebIDBCursorImpl::ResetPrefetchCache() { | 169 void WebIDBCursorImpl::ResetPrefetchCache() { |
| 150 continue_count_ = 0; | 170 continue_count_ = 0; |
| 151 prefetch_amount_ = kMinPrefetchAmount; | 171 prefetch_amount_ = kMinPrefetchAmount; |
| 152 | 172 |
| 153 if (!prefetch_keys_.size()) { | 173 if (!prefetch_keys_.size()) { |
| 154 // No prefetch cache, so no need to reset the cursor in the back-end. | 174 // No prefetch cache, so no need to reset the cursor in the back-end. |
| 155 return; | 175 return; |
| 156 } | 176 } |
| 157 | 177 |
| 158 IndexedDBDispatcher* dispatcher = | 178 IndexedDBDispatcher* dispatcher = |
| 159 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | 179 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); |
| 160 dispatcher->RequestIDBCursorPrefetchReset( | 180 dispatcher->RequestIDBCursorPrefetchReset( |
| 161 used_prefetches_, prefetch_keys_.size(), ipc_cursor_id_); | 181 used_prefetches_, prefetch_keys_.size(), ipc_cursor_id_); |
| 162 prefetch_keys_.clear(); | 182 prefetch_keys_.clear(); |
| 163 prefetch_primary_keys_.clear(); | 183 prefetch_primary_keys_.clear(); |
| 164 prefetch_values_.clear(); | 184 prefetch_values_.clear(); |
| 165 | 185 |
| 166 pending_onsuccess_callbacks_ = 0; | 186 pending_onsuccess_callbacks_ = 0; |
| 167 } | 187 } |
| 168 | 188 |
| 169 } // namespace content | 189 } // namespace content |
| OLD | NEW |