IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / child / indexed_db / webidbdatabase_impl.cc
blob8a9a7619e5fef61a0fe43e622578c314b8748b5d
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.
5 #include "content/child/indexed_db/webidbdatabase_impl.h"
7 #include <vector>
9 #include "content/child/indexed_db/indexed_db_dispatcher.h"
10 #include "content/child/indexed_db/indexed_db_key_builders.h"
11 #include "content/child/thread_safe_sender.h"
12 #include "content/common/indexed_db/indexed_db_messages.h"
13 #include "third_party/WebKit/public/platform/WebIDBKeyPath.h"
14 #include "third_party/WebKit/public/platform/WebIDBMetadata.h"
15 #include "third_party/WebKit/public/platform/WebString.h"
16 #include "third_party/WebKit/public/platform/WebVector.h"
17 #include "webkit/child/worker_task_runner.h"
19 using blink::WebIDBCallbacks;
20 using blink::WebIDBDatabaseCallbacks;
21 using blink::WebIDBMetadata;
22 using blink::WebIDBKey;
23 using blink::WebIDBKeyPath;
24 using blink::WebIDBKeyRange;
25 using blink::WebString;
26 using blink::WebVector;
27 using webkit_glue::WorkerTaskRunner;
29 namespace content {
31 WebIDBDatabaseImpl::WebIDBDatabaseImpl(int32 ipc_database_id,
32 int32 ipc_database_callbacks_id,
33 ThreadSafeSender* thread_safe_sender)
34 : ipc_database_id_(ipc_database_id),
35 ipc_database_callbacks_id_(ipc_database_callbacks_id),
36 thread_safe_sender_(thread_safe_sender) {}
38 WebIDBDatabaseImpl::~WebIDBDatabaseImpl() {
39 // It's not possible for there to be pending callbacks that address this
40 // object since inside WebKit, they hold a reference to the object which owns
41 // this object. But, if that ever changed, then we'd need to invalidate
42 // any such pointers.
43 thread_safe_sender_->Send(
44 new IndexedDBHostMsg_DatabaseDestroyed(ipc_database_id_));
45 IndexedDBDispatcher* dispatcher =
46 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
47 dispatcher->DatabaseDestroyed(ipc_database_id_);
50 void WebIDBDatabaseImpl::createObjectStore(long long transaction_id,
51 long long object_store_id,
52 const WebString& name,
53 const WebIDBKeyPath& key_path,
54 bool auto_increment) {
55 IndexedDBHostMsg_DatabaseCreateObjectStore_Params params;
56 params.ipc_database_id = ipc_database_id_;
57 params.transaction_id = transaction_id;
58 params.object_store_id = object_store_id;
59 params.name = name;
60 params.key_path = IndexedDBKeyPathBuilder::Build(key_path);
61 params.auto_increment = auto_increment;
63 thread_safe_sender_->Send(
64 new IndexedDBHostMsg_DatabaseCreateObjectStore(params));
67 void WebIDBDatabaseImpl::deleteObjectStore(long long transaction_id,
68 long long object_store_id) {
69 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseDeleteObjectStore(
70 ipc_database_id_, transaction_id, object_store_id));
73 void WebIDBDatabaseImpl::createTransaction(
74 long long transaction_id,
75 WebIDBDatabaseCallbacks* callbacks,
76 const WebVector<long long>& object_store_ids,
77 unsigned short mode) {
78 IndexedDBDispatcher* dispatcher =
79 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
80 dispatcher->RequestIDBDatabaseCreateTransaction(
81 ipc_database_id_, transaction_id, callbacks, object_store_ids, mode);
84 void WebIDBDatabaseImpl::close() {
85 IndexedDBDispatcher* dispatcher =
86 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
87 dispatcher->RequestIDBDatabaseClose(ipc_database_id_,
88 ipc_database_callbacks_id_);
91 void WebIDBDatabaseImpl::get(long long transaction_id,
92 long long object_store_id,
93 long long index_id,
94 const WebIDBKeyRange& key_range,
95 bool key_only,
96 WebIDBCallbacks* callbacks) {
97 IndexedDBDispatcher* dispatcher =
98 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
99 dispatcher->RequestIDBDatabaseGet(ipc_database_id_,
100 transaction_id,
101 object_store_id,
102 index_id,
103 IndexedDBKeyRangeBuilder::Build(key_range),
104 key_only,
105 callbacks);
108 void WebIDBDatabaseImpl::put(long long transaction_id,
109 long long object_store_id,
110 const blink::WebData& value,
111 const WebIDBKey& key,
112 PutMode put_mode,
113 WebIDBCallbacks* callbacks,
114 const WebVector<long long>& web_index_ids,
115 const WebVector<WebIndexKeys>& web_index_keys) {
116 IndexedDBDispatcher* dispatcher =
117 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
118 dispatcher->RequestIDBDatabasePut(ipc_database_id_,
119 transaction_id,
120 object_store_id,
121 value,
122 IndexedDBKeyBuilder::Build(key),
123 put_mode,
124 callbacks,
125 web_index_ids,
126 web_index_keys);
129 void WebIDBDatabaseImpl::setIndexKeys(
130 long long transaction_id,
131 long long object_store_id,
132 const WebIDBKey& primary_key,
133 const WebVector<long long>& index_ids,
134 const WebVector<WebIndexKeys>& index_keys) {
135 IndexedDBHostMsg_DatabaseSetIndexKeys_Params params;
136 params.ipc_database_id = ipc_database_id_;
137 params.transaction_id = transaction_id;
138 params.object_store_id = object_store_id;
139 params.primary_key = IndexedDBKeyBuilder::Build(primary_key);
140 COMPILE_ASSERT(sizeof(params.index_ids[0]) == sizeof(index_ids[0]),
141 Cant_copy);
142 params.index_ids.assign(index_ids.data(),
143 index_ids.data() + index_ids.size());
145 params.index_keys.resize(index_keys.size());
146 for (size_t i = 0; i < index_keys.size(); ++i) {
147 params.index_keys[i].resize(index_keys[i].size());
148 for (size_t j = 0; j < index_keys[i].size(); ++j) {
149 params.index_keys[i][j] = IndexedDBKeyBuilder::Build(index_keys[i][j]);
152 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseSetIndexKeys(params));
155 void WebIDBDatabaseImpl::setIndexesReady(
156 long long transaction_id,
157 long long object_store_id,
158 const WebVector<long long>& web_index_ids) {
159 std::vector<int64> index_ids(web_index_ids.data(),
160 web_index_ids.data() + web_index_ids.size());
161 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseSetIndexesReady(
162 ipc_database_id_, transaction_id, object_store_id, index_ids));
165 void WebIDBDatabaseImpl::openCursor(long long transaction_id,
166 long long object_store_id,
167 long long index_id,
168 const WebIDBKeyRange& key_range,
169 unsigned short direction,
170 bool key_only,
171 TaskType task_type,
172 WebIDBCallbacks* callbacks) {
173 IndexedDBDispatcher* dispatcher =
174 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
175 dispatcher->RequestIDBDatabaseOpenCursor(
176 ipc_database_id_,
177 transaction_id,
178 object_store_id,
179 index_id,
180 IndexedDBKeyRangeBuilder::Build(key_range),
181 direction,
182 key_only,
183 task_type,
184 callbacks);
187 void WebIDBDatabaseImpl::count(long long transaction_id,
188 long long object_store_id,
189 long long index_id,
190 const WebIDBKeyRange& key_range,
191 WebIDBCallbacks* callbacks) {
192 IndexedDBDispatcher* dispatcher =
193 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
194 dispatcher->RequestIDBDatabaseCount(
195 ipc_database_id_,
196 transaction_id,
197 object_store_id,
198 index_id,
199 IndexedDBKeyRangeBuilder::Build(key_range),
200 callbacks);
203 void WebIDBDatabaseImpl::deleteRange(long long transaction_id,
204 long long object_store_id,
205 const WebIDBKeyRange& key_range,
206 WebIDBCallbacks* callbacks) {
207 IndexedDBDispatcher* dispatcher =
208 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
209 dispatcher->RequestIDBDatabaseDeleteRange(
210 ipc_database_id_,
211 transaction_id,
212 object_store_id,
213 IndexedDBKeyRangeBuilder::Build(key_range),
214 callbacks);
217 void WebIDBDatabaseImpl::clear(long long transaction_id,
218 long long object_store_id,
219 WebIDBCallbacks* callbacks) {
220 IndexedDBDispatcher* dispatcher =
221 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
222 dispatcher->RequestIDBDatabaseClear(
223 ipc_database_id_, transaction_id, object_store_id, callbacks);
226 void WebIDBDatabaseImpl::createIndex(long long transaction_id,
227 long long object_store_id,
228 long long index_id,
229 const WebString& name,
230 const WebIDBKeyPath& key_path,
231 bool unique,
232 bool multi_entry) {
233 IndexedDBHostMsg_DatabaseCreateIndex_Params params;
234 params.ipc_database_id = ipc_database_id_;
235 params.transaction_id = transaction_id;
236 params.object_store_id = object_store_id;
237 params.index_id = index_id;
238 params.name = name;
239 params.key_path = IndexedDBKeyPathBuilder::Build(key_path);
240 params.unique = unique;
241 params.multi_entry = multi_entry;
243 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseCreateIndex(params));
246 void WebIDBDatabaseImpl::deleteIndex(long long transaction_id,
247 long long object_store_id,
248 long long index_id) {
249 thread_safe_sender_->Send(new IndexedDBHostMsg_DatabaseDeleteIndex(
250 ipc_database_id_, transaction_id, object_store_id, index_id));
253 void WebIDBDatabaseImpl::abort(long long transaction_id) {
254 thread_safe_sender_->Send(
255 new IndexedDBHostMsg_DatabaseAbort(ipc_database_id_, transaction_id));
258 void WebIDBDatabaseImpl::commit(long long transaction_id) {
259 thread_safe_sender_->Send(
260 new IndexedDBHostMsg_DatabaseCommit(ipc_database_id_, transaction_id));
263 } // namespace content