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