OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/indexed_db/renderer_webidbcursor_impl.h" | |
6 | |
7 #include "content/common/indexed_db/indexed_db_messages.h" | |
8 #include "content/renderer/indexed_db/indexed_db_dispatcher.h" | |
9 #include "content/renderer/render_thread_impl.h" | |
10 | |
11 using WebKit::WebExceptionCode; | |
12 using WebKit::WebIDBCallbacks; | |
13 using WebKit::WebIDBKey; | |
14 using WebKit::WebSerializedScriptValue; | |
15 | |
16 RendererWebIDBCursorImpl::RendererWebIDBCursorImpl(int32 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) { | |
22 } | |
23 | |
24 RendererWebIDBCursorImpl::~RendererWebIDBCursorImpl() { | |
25 // It's not possible for there to be pending callbacks that address this | |
26 // object since inside WebKit, they hold a reference to the object wich owns | |
27 // this object. But, if that ever changed, then we'd need to invalidate | |
28 // any such pointers. | |
29 ChildThread::current()->Send(new IndexedDBHostMsg_CursorDestroyed( | |
30 idb_cursor_id_)); | |
31 IndexedDBDispatcher* dispatcher = | |
32 IndexedDBDispatcher::ThreadSpecificInstance(); | |
33 dispatcher->CursorDestroyed(idb_cursor_id_); | |
34 } | |
35 | |
36 unsigned short RendererWebIDBCursorImpl::direction() const { | |
37 int direction; | |
38 ChildThread::current()->Send( | |
39 new IndexedDBHostMsg_CursorDirection(idb_cursor_id_, &direction)); | |
40 return direction; | |
41 } | |
42 | |
43 WebIDBKey RendererWebIDBCursorImpl::key() const { | |
44 return key_; | |
45 } | |
46 | |
47 WebIDBKey RendererWebIDBCursorImpl::primaryKey() const { | |
48 return primary_key_; | |
49 } | |
50 | |
51 WebSerializedScriptValue RendererWebIDBCursorImpl::value() const { | |
52 return value_; | |
53 } | |
54 | |
55 void RendererWebIDBCursorImpl::update(const WebSerializedScriptValue& value, | |
56 WebIDBCallbacks* callbacks, | |
57 WebExceptionCode& ec) { | |
58 IndexedDBDispatcher* dispatcher = | |
59 IndexedDBDispatcher::ThreadSpecificInstance(); | |
60 dispatcher->RequestIDBCursorUpdate( | |
61 content::SerializedScriptValue(value), callbacks, idb_cursor_id_, &ec); | |
62 } | |
63 | |
64 void RendererWebIDBCursorImpl::continueFunction(const WebIDBKey& key, | |
65 WebIDBCallbacks* callbacks, | |
66 WebExceptionCode& ec) { | |
67 IndexedDBDispatcher* dispatcher = | |
68 IndexedDBDispatcher::ThreadSpecificInstance(); | |
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 | |
97 dispatcher->RequestIDBCursorContinue(IndexedDBKey(key), callbacks, | |
98 idb_cursor_id_, &ec); | |
99 } | |
100 | |
101 void RendererWebIDBCursorImpl::deleteFunction(WebIDBCallbacks* callbacks, | |
102 WebExceptionCode& ec) { | |
103 IndexedDBDispatcher* dispatcher = | |
104 IndexedDBDispatcher::ThreadSpecificInstance(); | |
105 dispatcher->RequestIDBCursorDelete(callbacks, idb_cursor_id_, &ec); | |
106 } | |
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 | |
122 void RendererWebIDBCursorImpl::SetKeyAndValue( | |
123 const IndexedDBKey& key, | |
124 const IndexedDBKey& primary_key, | |
125 const content::SerializedScriptValue& value) { | |
126 key_ = key; | |
127 primary_key_ = primary_key; | |
128 value_ = value; | |
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 IndexedDBDispatcher::ThreadSpecificInstance(); | |
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 |