OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/child/indexed_db/webidbdatabase_impl.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/stl_util.h" | |
13 #include "content/child/indexed_db/indexed_db_dispatcher.h" | |
14 #include "content/child/indexed_db/indexed_db_key_builders.h" | |
15 #include "content/child/thread_safe_sender.h" | |
16 #include "content/child/worker_thread_registry.h" | |
17 #include "content/common/indexed_db/indexed_db_messages.h" | |
18 #include "third_party/WebKit/public/platform/WebBlobInfo.h" | |
19 #include "third_party/WebKit/public/platform/WebString.h" | |
20 #include "third_party/WebKit/public/platform/WebVector.h" | |
21 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBKeyPath.h" | |
22 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBMetadata.h" | |
23 | |
24 using blink::WebBlobInfo; | |
25 using blink::WebIDBCallbacks; | |
26 using blink::WebIDBCursor; | |
27 using blink::WebIDBDatabase; | |
28 using blink::WebIDBDatabaseCallbacks; | |
29 using blink::WebIDBMetadata; | |
30 using blink::WebIDBKey; | |
31 using blink::WebIDBKeyPath; | |
32 using blink::WebIDBKeyRange; | |
33 using blink::WebIDBObserver; | |
34 using blink::WebString; | |
35 using blink::WebVector; | |
36 | |
37 namespace content { | |
38 | |
39 WebIDBDatabaseImpl::WebIDBDatabaseImpl(int32_t ipc_database_id, | |
40 int32_t ipc_database_callbacks_id, | |
41 ThreadSafeSender* thread_safe_sender) | |
42 : ipc_database_id_(ipc_database_id), | |
43 ipc_database_callbacks_id_(ipc_database_callbacks_id), | |
44 thread_safe_sender_(thread_safe_sender) {} | |
45 | |
46 WebIDBDatabaseImpl::~WebIDBDatabaseImpl() { | |
47 // It's not possible for there to be pending callbacks that address this | |
48 // object since inside WebKit, they hold a reference to the object which owns | |
49 // this object. But, if that ever changed, then we'd need to invalidate | |
50 // any such pointers. | |
51 thread_safe_sender_->Send( | |
52 new IndexedDBHostMsg_DatabaseDestroyed(ipc_database_id_)); | |
53 IndexedDBDispatcher* dispatcher = | |
54 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
55 dispatcher->DatabaseDestroyed(ipc_database_id_); | |
56 } | |
57 | |
58 void WebIDBDatabaseImpl::createObjectStore(long long transaction_id, | |
59 long long object_store_id, | |
60 const WebString& name, | |
61 const WebIDBKeyPath& key_path, | |
62 bool auto_increment) { | |
63 IndexedDBHostMsg_DatabaseCreateObjectStore_Params params; | |
64 params.ipc_database_id = ipc_database_id_; | |
65 params.transaction_id = transaction_id; | |
66 params.object_store_id = object_store_id; | |
67 params.name = name; | |
68 params.key_path = IndexedDBKeyPathBuilder::Build(key_path); | |
69 params.auto_increment = auto_increment; | |
70 | |
71 thread_safe_sender_->Send( | |
72 new IndexedDBHostMsg_DatabaseCreateObjectStore(params)); | |
73 } | |
74 | |
75 void WebIDBDatabaseImpl::deleteObjectStore(long long transaction_id, | |
76 long long object_store_id) { | |
77 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseDeleteObjectStore( | |
78 ipc_database_id_, transaction_id, object_store_id)); | |
79 } | |
80 | |
81 void WebIDBDatabaseImpl::createTransaction( | |
82 long long transaction_id, | |
83 WebIDBDatabaseCallbacks* callbacks, | |
84 const WebVector<long long>& object_store_ids, | |
85 blink::WebIDBTransactionMode mode) { | |
86 IndexedDBDispatcher* dispatcher = | |
87 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
88 dispatcher->RequestIDBDatabaseCreateTransaction( | |
89 ipc_database_id_, transaction_id, callbacks, object_store_ids, mode); | |
90 } | |
91 | |
92 void WebIDBDatabaseImpl::close() { | |
93 IndexedDBDispatcher* dispatcher = | |
94 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
95 dispatcher->RemoveIDBObservers(observer_ids_); | |
96 dispatcher->RequestIDBDatabaseClose(ipc_database_id_, | |
97 ipc_database_callbacks_id_); | |
98 } | |
99 | |
100 void WebIDBDatabaseImpl::versionChangeIgnored() { | |
101 IndexedDBDispatcher* dispatcher = | |
102 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
103 dispatcher->NotifyIDBDatabaseVersionChangeIgnored(ipc_database_id_); | |
104 } | |
105 | |
106 int32_t WebIDBDatabaseImpl::addObserver( | |
107 std::unique_ptr<WebIDBObserver> observer, | |
108 long long transaction_id) { | |
109 IndexedDBDispatcher* dispatcher = | |
110 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
111 | |
112 int32_t observer_id = dispatcher->AddIDBObserver( | |
113 ipc_database_id_, transaction_id, std::move(observer)); | |
114 observer_ids_.insert(observer_id); | |
115 return observer_id; | |
116 } | |
117 | |
118 bool WebIDBDatabaseImpl::containsObserverId(int32_t id) const { | |
119 return ContainsValue(observer_ids_, id); | |
120 } | |
121 | |
122 void WebIDBDatabaseImpl::removeObservers( | |
123 const std::vector<int32_t>& observer_ids_to_remove) { | |
124 for (int32_t id : observer_ids_to_remove) | |
125 observer_ids_.erase(id); | |
126 | |
127 IndexedDBDispatcher* dispatcher = | |
128 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
129 dispatcher->RemoveIDBObserversFromDatabase(ipc_database_id_, | |
130 observer_ids_to_remove); | |
131 } | |
132 | |
133 void WebIDBDatabaseImpl::get(long long transaction_id, | |
134 long long object_store_id, | |
135 long long index_id, | |
136 const WebIDBKeyRange& key_range, | |
137 bool key_only, | |
138 WebIDBCallbacks* callbacks) { | |
139 IndexedDBDispatcher* dispatcher = | |
140 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
141 dispatcher->RequestIDBDatabaseGet(ipc_database_id_, | |
142 transaction_id, | |
143 object_store_id, | |
144 index_id, | |
145 IndexedDBKeyRangeBuilder::Build(key_range), | |
146 key_only, | |
147 callbacks); | |
148 } | |
149 | |
150 void WebIDBDatabaseImpl::getAll(long long transaction_id, | |
151 long long object_store_id, | |
152 long long index_id, | |
153 const WebIDBKeyRange& key_range, | |
154 long long max_count, | |
155 bool key_only, | |
156 WebIDBCallbacks* callbacks) { | |
157 IndexedDBDispatcher* dispatcher = | |
158 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
159 dispatcher->RequestIDBDatabaseGetAll( | |
160 ipc_database_id_, transaction_id, object_store_id, index_id, | |
161 IndexedDBKeyRangeBuilder::Build(key_range), key_only, max_count, | |
162 callbacks); | |
163 } | |
164 | |
165 void WebIDBDatabaseImpl::put(long long transaction_id, | |
166 long long object_store_id, | |
167 const blink::WebData& value, | |
168 const blink::WebVector<WebBlobInfo>& web_blob_info, | |
169 const WebIDBKey& key, | |
170 blink::WebIDBPutMode put_mode, | |
171 WebIDBCallbacks* callbacks, | |
172 const WebVector<long long>& web_index_ids, | |
173 const WebVector<WebIndexKeys>& web_index_keys) { | |
174 IndexedDBDispatcher* dispatcher = | |
175 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
176 dispatcher->RequestIDBDatabasePut(ipc_database_id_, | |
177 transaction_id, | |
178 object_store_id, | |
179 value, | |
180 web_blob_info, | |
181 IndexedDBKeyBuilder::Build(key), | |
182 put_mode, | |
183 callbacks, | |
184 web_index_ids, | |
185 web_index_keys); | |
186 } | |
187 | |
188 void WebIDBDatabaseImpl::setIndexKeys( | |
189 long long transaction_id, | |
190 long long object_store_id, | |
191 const WebIDBKey& primary_key, | |
192 const WebVector<long long>& index_ids, | |
193 const WebVector<WebIndexKeys>& index_keys) { | |
194 IndexedDBHostMsg_DatabaseSetIndexKeys_Params params; | |
195 params.ipc_database_id = ipc_database_id_; | |
196 params.transaction_id = transaction_id; | |
197 params.object_store_id = object_store_id; | |
198 params.primary_key = IndexedDBKeyBuilder::Build(primary_key); | |
199 | |
200 DCHECK_EQ(index_ids.size(), index_keys.size()); | |
201 params.index_keys.resize(index_ids.size()); | |
202 for (size_t i = 0, len = index_ids.size(); i < len; ++i) { | |
203 params.index_keys[i].first = index_ids[i]; | |
204 params.index_keys[i].second.resize(index_keys[i].size()); | |
205 for (size_t j = 0; j < index_keys[i].size(); ++j) { | |
206 params.index_keys[i].second[j] = | |
207 IndexedDBKey(IndexedDBKeyBuilder::Build(index_keys[i][j])); | |
208 } | |
209 } | |
210 | |
211 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseSetIndexKeys(params)); | |
212 } | |
213 | |
214 void WebIDBDatabaseImpl::setIndexesReady( | |
215 long long transaction_id, | |
216 long long object_store_id, | |
217 const WebVector<long long>& web_index_ids) { | |
218 std::vector<int64_t> index_ids(web_index_ids.data(), | |
219 web_index_ids.data() + web_index_ids.size()); | |
220 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseSetIndexesReady( | |
221 ipc_database_id_, transaction_id, object_store_id, index_ids)); | |
222 } | |
223 | |
224 void WebIDBDatabaseImpl::openCursor(long long transaction_id, | |
225 long long object_store_id, | |
226 long long index_id, | |
227 const WebIDBKeyRange& key_range, | |
228 blink::WebIDBCursorDirection direction, | |
229 bool key_only, | |
230 blink::WebIDBTaskType task_type, | |
231 WebIDBCallbacks* callbacks) { | |
232 IndexedDBDispatcher* dispatcher = | |
233 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
234 dispatcher->RequestIDBDatabaseOpenCursor( | |
235 ipc_database_id_, | |
236 transaction_id, | |
237 object_store_id, | |
238 index_id, | |
239 IndexedDBKeyRangeBuilder::Build(key_range), | |
240 direction, | |
241 key_only, | |
242 task_type, | |
243 callbacks); | |
244 } | |
245 | |
246 void WebIDBDatabaseImpl::count(long long transaction_id, | |
247 long long object_store_id, | |
248 long long index_id, | |
249 const WebIDBKeyRange& key_range, | |
250 WebIDBCallbacks* callbacks) { | |
251 IndexedDBDispatcher* dispatcher = | |
252 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
253 dispatcher->RequestIDBDatabaseCount( | |
254 ipc_database_id_, | |
255 transaction_id, | |
256 object_store_id, | |
257 index_id, | |
258 IndexedDBKeyRangeBuilder::Build(key_range), | |
259 callbacks); | |
260 } | |
261 | |
262 void WebIDBDatabaseImpl::deleteRange(long long transaction_id, | |
263 long long object_store_id, | |
264 const WebIDBKeyRange& key_range, | |
265 WebIDBCallbacks* callbacks) { | |
266 IndexedDBDispatcher* dispatcher = | |
267 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
268 dispatcher->RequestIDBDatabaseDeleteRange( | |
269 ipc_database_id_, | |
270 transaction_id, | |
271 object_store_id, | |
272 IndexedDBKeyRangeBuilder::Build(key_range), | |
273 callbacks); | |
274 } | |
275 | |
276 void WebIDBDatabaseImpl::clear(long long transaction_id, | |
277 long long object_store_id, | |
278 WebIDBCallbacks* callbacks) { | |
279 IndexedDBDispatcher* dispatcher = | |
280 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); | |
281 dispatcher->RequestIDBDatabaseClear( | |
282 ipc_database_id_, transaction_id, object_store_id, callbacks); | |
283 } | |
284 | |
285 void WebIDBDatabaseImpl::createIndex(long long transaction_id, | |
286 long long object_store_id, | |
287 long long index_id, | |
288 const WebString& name, | |
289 const WebIDBKeyPath& key_path, | |
290 bool unique, | |
291 bool multi_entry) { | |
292 IndexedDBHostMsg_DatabaseCreateIndex_Params params; | |
293 params.ipc_database_id = ipc_database_id_; | |
294 params.transaction_id = transaction_id; | |
295 params.object_store_id = object_store_id; | |
296 params.index_id = index_id; | |
297 params.name = name; | |
298 params.key_path = IndexedDBKeyPathBuilder::Build(key_path); | |
299 params.unique = unique; | |
300 params.multi_entry = multi_entry; | |
301 | |
302 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseCreateIndex(params)); | |
303 } | |
304 | |
305 void WebIDBDatabaseImpl::deleteIndex(long long transaction_id, | |
306 long long object_store_id, | |
307 long long index_id) { | |
308 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseDeleteIndex( | |
309 ipc_database_id_, transaction_id, object_store_id, index_id)); | |
310 } | |
311 | |
312 void WebIDBDatabaseImpl::abort(long long transaction_id) { | |
313 thread_safe_sender_->Send( | |
314 new IndexedDBHostMsg_DatabaseAbort(ipc_database_id_, transaction_id)); | |
315 } | |
316 | |
317 void WebIDBDatabaseImpl::commit(long long transaction_id) { | |
318 thread_safe_sender_->Send( | |
319 new IndexedDBHostMsg_DatabaseCommit(ipc_database_id_, transaction_id)); | |
320 } | |
321 | |
322 void WebIDBDatabaseImpl::ackReceivedBlobs(const WebVector<WebString>& uuids) { | |
323 DCHECK(uuids.size()); | |
324 std::vector<std::string> param(uuids.size()); | |
325 for (size_t i = 0; i < uuids.size(); ++i) | |
326 param[i] = uuids[i].latin1().data(); | |
327 thread_safe_sender_->Send(new IndexedDBHostMsg_AckReceivedBlobs(param)); | |
328 } | |
329 | |
330 } // namespace content | |
OLD | NEW |