OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/renderer/renderer_webidbcursor_impl.h" | 5 #include "content/renderer/renderer_webidbcursor_impl.h" |
6 | 6 |
7 #include "content/common/indexed_db_messages.h" | 7 #include "content/common/indexed_db_messages.h" |
8 #include "content/renderer/indexed_db_dispatcher.h" | 8 #include "content/renderer/indexed_db_dispatcher.h" |
9 #include "content/renderer/render_thread_impl.h" | 9 #include "content/renderer/render_thread_impl.h" |
10 | 10 |
11 using WebKit::WebExceptionCode; | 11 using WebKit::WebExceptionCode; |
12 using WebKit::WebIDBCallbacks; | 12 using WebKit::WebIDBCallbacks; |
13 using WebKit::WebIDBKey; | 13 using WebKit::WebIDBKey; |
14 using WebKit::WebSerializedScriptValue; | 14 using WebKit::WebSerializedScriptValue; |
15 | 15 |
16 RendererWebIDBCursorImpl::RendererWebIDBCursorImpl(int32 idb_cursor_id) | 16 RendererWebIDBCursorImpl::RendererWebIDBCursorImpl(int32 idb_cursor_id) |
17 : idb_cursor_id_(idb_cursor_id) { | 17 : idb_cursor_id_(idb_cursor_id), |
| 18 continue_count_(0), |
| 19 used_prefetches_(0), |
| 20 pending_onsuccess_callbacks_(0), |
| 21 prefetch_amount_(kMinPrefetchAmount) { |
18 } | 22 } |
19 | 23 |
20 RendererWebIDBCursorImpl::~RendererWebIDBCursorImpl() { | 24 RendererWebIDBCursorImpl::~RendererWebIDBCursorImpl() { |
21 // It's not possible for there to be pending callbacks that address this | 25 // It's not possible for there to be pending callbacks that address this |
22 // object since inside WebKit, they hold a reference to the object wich owns | 26 // object since inside WebKit, they hold a reference to the object wich owns |
23 // this object. But, if that ever changed, then we'd need to invalidate | 27 // this object. But, if that ever changed, then we'd need to invalidate |
24 // any such pointers. | 28 // any such pointers. |
25 ChildThread::current()->Send(new IndexedDBHostMsg_CursorDestroyed( | 29 ChildThread::current()->Send(new IndexedDBHostMsg_CursorDestroyed( |
26 idb_cursor_id_)); | 30 idb_cursor_id_)); |
27 IndexedDBDispatcher* dispatcher = | 31 IndexedDBDispatcher* dispatcher = |
(...skipping 27 matching lines...) Expand all Loading... |
55 RenderThreadImpl::current()->indexed_db_dispatcher(); | 59 RenderThreadImpl::current()->indexed_db_dispatcher(); |
56 dispatcher->RequestIDBCursorUpdate( | 60 dispatcher->RequestIDBCursorUpdate( |
57 content::SerializedScriptValue(value), callbacks, idb_cursor_id_, &ec); | 61 content::SerializedScriptValue(value), callbacks, idb_cursor_id_, &ec); |
58 } | 62 } |
59 | 63 |
60 void RendererWebIDBCursorImpl::continueFunction(const WebIDBKey& key, | 64 void RendererWebIDBCursorImpl::continueFunction(const WebIDBKey& key, |
61 WebIDBCallbacks* callbacks, | 65 WebIDBCallbacks* callbacks, |
62 WebExceptionCode& ec) { | 66 WebExceptionCode& ec) { |
63 IndexedDBDispatcher* dispatcher = | 67 IndexedDBDispatcher* dispatcher = |
64 RenderThreadImpl::current()->indexed_db_dispatcher(); | 68 RenderThreadImpl::current()->indexed_db_dispatcher(); |
| 69 |
| 70 if (key.type() == WebIDBKey::InvalidType) { |
| 71 // No key, so this would qualify for a prefetch. |
| 72 ++continue_count_; |
| 73 |
| 74 if (!prefetch_keys_.empty()) { |
| 75 // We have a prefetch cache, so serve the result from that. |
| 76 CachedContinue(callbacks); |
| 77 return; |
| 78 } |
| 79 |
| 80 if (continue_count_ > kPrefetchContinueThreshold) { |
| 81 // Request pre-fetch. |
| 82 dispatcher->RequestIDBCursorPrefetch(prefetch_amount_, callbacks, |
| 83 idb_cursor_id_, &ec); |
| 84 |
| 85 // Increase prefetch_amount_ exponentially. |
| 86 prefetch_amount_ *= 2; |
| 87 if (prefetch_amount_ > kMaxPrefetchAmount) |
| 88 prefetch_amount_ = kMaxPrefetchAmount; |
| 89 |
| 90 return; |
| 91 } |
| 92 } else { |
| 93 // Key argument supplied. We couldn't prefetch this. |
| 94 ResetPrefetchCache(); |
| 95 } |
| 96 |
65 dispatcher->RequestIDBCursorContinue(IndexedDBKey(key), callbacks, | 97 dispatcher->RequestIDBCursorContinue(IndexedDBKey(key), callbacks, |
66 idb_cursor_id_, &ec); | 98 idb_cursor_id_, &ec); |
67 } | 99 } |
68 | 100 |
69 void RendererWebIDBCursorImpl::deleteFunction(WebIDBCallbacks* callbacks, | 101 void RendererWebIDBCursorImpl::deleteFunction(WebIDBCallbacks* callbacks, |
70 WebExceptionCode& ec) { | 102 WebExceptionCode& ec) { |
71 IndexedDBDispatcher* dispatcher = | 103 IndexedDBDispatcher* dispatcher = |
72 RenderThreadImpl::current()->indexed_db_dispatcher(); | 104 RenderThreadImpl::current()->indexed_db_dispatcher(); |
73 dispatcher->RequestIDBCursorDelete(callbacks, idb_cursor_id_, &ec); | 105 dispatcher->RequestIDBCursorDelete(callbacks, idb_cursor_id_, &ec); |
74 } | 106 } |
75 | 107 |
| 108 void RendererWebIDBCursorImpl::postSuccessHandlerCallback() |
| 109 { |
| 110 pending_onsuccess_callbacks_--; |
| 111 |
| 112 // If the onsuccess callback called continue() on the cursor again, |
| 113 // and that continue was served by the prefetch cache, then |
| 114 // pending_onsuccess_callbacks_ would be incremented. |
| 115 // If not, it means the callback did something else, or nothing at all, |
| 116 // in which case we need to reset the cache. |
| 117 |
| 118 if (pending_onsuccess_callbacks_ == 0) |
| 119 ResetPrefetchCache(); |
| 120 } |
| 121 |
76 void RendererWebIDBCursorImpl::SetKeyAndValue( | 122 void RendererWebIDBCursorImpl::SetKeyAndValue( |
77 const IndexedDBKey& key, | 123 const IndexedDBKey& key, |
78 const IndexedDBKey& primary_key, | 124 const IndexedDBKey& primary_key, |
79 const content::SerializedScriptValue& value) { | 125 const content::SerializedScriptValue& value) { |
80 key_ = key; | 126 key_ = key; |
81 primary_key_ = primary_key; | 127 primary_key_ = primary_key; |
82 value_ = value; | 128 value_ = value; |
83 } | 129 } |
| 130 |
| 131 void RendererWebIDBCursorImpl::SetPrefetchData( |
| 132 const std::vector<IndexedDBKey>& keys, |
| 133 const std::vector<IndexedDBKey>& primary_keys, |
| 134 const std::vector<content::SerializedScriptValue>& values) { |
| 135 prefetch_keys_.assign(keys.begin(), keys.end()); |
| 136 prefetch_primary_keys_.assign(primary_keys.begin(), primary_keys.end()); |
| 137 prefetch_values_.assign(values.begin(), values.end()); |
| 138 |
| 139 used_prefetches_ = 0; |
| 140 pending_onsuccess_callbacks_ = 0; |
| 141 } |
| 142 |
| 143 void RendererWebIDBCursorImpl::CachedContinue( |
| 144 WebKit::WebIDBCallbacks* callbacks) { |
| 145 DCHECK(prefetch_keys_.size() > 0); |
| 146 DCHECK(prefetch_primary_keys_.size() == prefetch_keys_.size()); |
| 147 DCHECK(prefetch_values_.size() == prefetch_keys_.size()); |
| 148 |
| 149 key_ = prefetch_keys_.front(); |
| 150 primary_key_ = prefetch_primary_keys_.front(); |
| 151 value_ = prefetch_values_.front(); |
| 152 |
| 153 prefetch_keys_.pop_front(); |
| 154 prefetch_primary_keys_.pop_front(); |
| 155 prefetch_values_.pop_front(); |
| 156 used_prefetches_++; |
| 157 |
| 158 pending_onsuccess_callbacks_++; |
| 159 callbacks->onSuccessWithContinuation(); |
| 160 } |
| 161 |
| 162 void RendererWebIDBCursorImpl::ResetPrefetchCache() { |
| 163 continue_count_ = 0; |
| 164 prefetch_amount_ = kMinPrefetchAmount; |
| 165 |
| 166 if (!prefetch_keys_.size()) { |
| 167 // No prefetch cache, so no need to reset the cursor in the back-end. |
| 168 return; |
| 169 } |
| 170 |
| 171 IndexedDBDispatcher* dispatcher = |
| 172 RenderThreadImpl::current()->indexed_db_dispatcher(); |
| 173 dispatcher->RequestIDBCursorPrefetchReset(used_prefetches_, |
| 174 prefetch_keys_.size(), |
| 175 idb_cursor_id_); |
| 176 prefetch_keys_.clear(); |
| 177 prefetch_primary_keys_.clear(); |
| 178 prefetch_values_.clear(); |
| 179 |
| 180 pending_onsuccess_callbacks_ = 0; |
| 181 } |
OLD | NEW |