| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/common_child/indexed_db/proxy_webidbcursor_impl.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "content/common/child_thread.h" | |
| 10 #include "content/common/indexed_db/indexed_db_messages.h" | |
| 11 #include "content/common_child/indexed_db/indexed_db_dispatcher.h" | |
| 12 | |
| 13 using WebKit::WebData; | |
| 14 using WebKit::WebIDBCallbacks; | |
| 15 using WebKit::WebIDBKey; | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 RendererWebIDBCursorImpl::RendererWebIDBCursorImpl(int32 ipc_cursor_id) | |
| 20 : ipc_cursor_id_(ipc_cursor_id), | |
| 21 continue_count_(0), | |
| 22 used_prefetches_(0), | |
| 23 pending_onsuccess_callbacks_(0), | |
| 24 prefetch_amount_(kMinPrefetchAmount) {} | |
| 25 | |
| 26 RendererWebIDBCursorImpl::~RendererWebIDBCursorImpl() { | |
| 27 // It's not possible for there to be pending callbacks that address this | |
| 28 // object since inside WebKit, they hold a reference to the object which owns | |
| 29 // this object. But, if that ever changed, then we'd need to invalidate | |
| 30 // any such pointers. | |
| 31 | |
| 32 if (ipc_cursor_id_ != kInvalidCursorId) { | |
| 33 // Invalid ID used in tests to avoid really sending this message. | |
| 34 IndexedDBDispatcher::Send( | |
| 35 new IndexedDBHostMsg_CursorDestroyed(ipc_cursor_id_)); | |
| 36 } | |
| 37 IndexedDBDispatcher* dispatcher = | |
| 38 IndexedDBDispatcher::ThreadSpecificInstance(); | |
| 39 dispatcher->CursorDestroyed(ipc_cursor_id_); | |
| 40 } | |
| 41 | |
| 42 void RendererWebIDBCursorImpl::advance(unsigned long count, | |
| 43 WebIDBCallbacks* callbacks_ptr) { | |
| 44 IndexedDBDispatcher* dispatcher = | |
| 45 IndexedDBDispatcher::ThreadSpecificInstance(); | |
| 46 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
| 47 ResetPrefetchCache(); | |
| 48 dispatcher->RequestIDBCursorAdvance( | |
| 49 count, callbacks.release(), ipc_cursor_id_); | |
| 50 } | |
| 51 | |
| 52 void RendererWebIDBCursorImpl::continueFunction( | |
| 53 const WebIDBKey& key, | |
| 54 WebIDBCallbacks* callbacks_ptr) { | |
| 55 IndexedDBDispatcher* dispatcher = | |
| 56 IndexedDBDispatcher::ThreadSpecificInstance(); | |
| 57 scoped_ptr<WebIDBCallbacks> callbacks(callbacks_ptr); | |
| 58 | |
| 59 if (key.type() == WebIDBKey::NullType) { | |
| 60 // No key, so this would qualify for a prefetch. | |
| 61 ++continue_count_; | |
| 62 | |
| 63 if (!prefetch_keys_.empty()) { | |
| 64 // We have a prefetch cache, so serve the result from that. | |
| 65 CachedContinue(callbacks.get()); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 if (continue_count_ > kPrefetchContinueThreshold) { | |
| 70 // Request pre-fetch. | |
| 71 ++pending_onsuccess_callbacks_; | |
| 72 dispatcher->RequestIDBCursorPrefetch( | |
| 73 prefetch_amount_, callbacks.release(), ipc_cursor_id_); | |
| 74 | |
| 75 // Increase prefetch_amount_ exponentially. | |
| 76 prefetch_amount_ *= 2; | |
| 77 if (prefetch_amount_ > kMaxPrefetchAmount) | |
| 78 prefetch_amount_ = kMaxPrefetchAmount; | |
| 79 | |
| 80 return; | |
| 81 } | |
| 82 } else { | |
| 83 // Key argument supplied. We couldn't prefetch this. | |
| 84 ResetPrefetchCache(); | |
| 85 } | |
| 86 | |
| 87 dispatcher->RequestIDBCursorContinue( | |
| 88 IndexedDBKey(key), callbacks.release(), ipc_cursor_id_); | |
| 89 } | |
| 90 | |
| 91 void RendererWebIDBCursorImpl::postSuccessHandlerCallback() { | |
| 92 pending_onsuccess_callbacks_--; | |
| 93 | |
| 94 // If the onsuccess callback called continue() on the cursor again, | |
| 95 // and that continue was served by the prefetch cache, then | |
| 96 // pending_onsuccess_callbacks_ would be incremented. | |
| 97 // If not, it means the callback did something else, or nothing at all, | |
| 98 // in which case we need to reset the cache. | |
| 99 | |
| 100 if (pending_onsuccess_callbacks_ == 0) | |
| 101 ResetPrefetchCache(); | |
| 102 } | |
| 103 | |
| 104 void RendererWebIDBCursorImpl::SetPrefetchData( | |
| 105 const std::vector<IndexedDBKey>& keys, | |
| 106 const std::vector<IndexedDBKey>& primary_keys, | |
| 107 const std::vector<WebData>& values) { | |
| 108 prefetch_keys_.assign(keys.begin(), keys.end()); | |
| 109 prefetch_primary_keys_.assign(primary_keys.begin(), primary_keys.end()); | |
| 110 prefetch_values_.assign(values.begin(), values.end()); | |
| 111 | |
| 112 used_prefetches_ = 0; | |
| 113 pending_onsuccess_callbacks_ = 0; | |
| 114 } | |
| 115 | |
| 116 void RendererWebIDBCursorImpl::CachedContinue( | |
| 117 WebKit::WebIDBCallbacks* callbacks) { | |
| 118 DCHECK_GT(prefetch_keys_.size(), 0ul); | |
| 119 DCHECK(prefetch_primary_keys_.size() == prefetch_keys_.size()); | |
| 120 DCHECK(prefetch_values_.size() == prefetch_keys_.size()); | |
| 121 | |
| 122 IndexedDBKey key = prefetch_keys_.front(); | |
| 123 IndexedDBKey primary_key = prefetch_primary_keys_.front(); | |
| 124 // this could be a real problem.. we need 2 CachedContinues | |
| 125 WebData value = prefetch_values_.front(); | |
| 126 | |
| 127 prefetch_keys_.pop_front(); | |
| 128 prefetch_primary_keys_.pop_front(); | |
| 129 prefetch_values_.pop_front(); | |
| 130 used_prefetches_++; | |
| 131 | |
| 132 pending_onsuccess_callbacks_++; | |
| 133 | |
| 134 callbacks->onSuccess(key, primary_key, value); | |
| 135 } | |
| 136 | |
| 137 void RendererWebIDBCursorImpl::ResetPrefetchCache() { | |
| 138 continue_count_ = 0; | |
| 139 prefetch_amount_ = kMinPrefetchAmount; | |
| 140 | |
| 141 if (!prefetch_keys_.size()) { | |
| 142 // No prefetch cache, so no need to reset the cursor in the back-end. | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 IndexedDBDispatcher* dispatcher = | |
| 147 IndexedDBDispatcher::ThreadSpecificInstance(); | |
| 148 dispatcher->RequestIDBCursorPrefetchReset( | |
| 149 used_prefetches_, prefetch_keys_.size(), ipc_cursor_id_); | |
| 150 prefetch_keys_.clear(); | |
| 151 prefetch_primary_keys_.clear(); | |
| 152 prefetch_values_.clear(); | |
| 153 | |
| 154 pending_onsuccess_callbacks_ = 0; | |
| 155 } | |
| 156 | |
| 157 } // namespace content | |
| OLD | NEW |