blob: 1c7c3ce3a6206be5c20d92ff06e9db8c4eb9b967 [file] [log] [blame]
[email protected]72a4183d2013-05-31 18:33:101// Copyright (c) 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/browser/indexed_db/webidbdatabase_impl.h"
6
7#include <vector>
8
9#include "base/basictypes.h"
10#include "base/logging.h"
11#include "content/browser/indexed_db/indexed_db_callbacks_wrapper.h"
12#include "content/browser/indexed_db/indexed_db_cursor.h"
13#include "content/browser/indexed_db/indexed_db_database.h"
14#include "content/browser/indexed_db/indexed_db_metadata.h"
15#include "content/common/indexed_db/indexed_db_key_range.h"
16#include "third_party/WebKit/public/platform/WebData.h"
[email protected]72a4183d2013-05-31 18:33:1017#include "third_party/WebKit/public/platform/WebIDBDatabaseError.h"
[email protected]72a4183d2013-05-31 18:33:1018#include "third_party/WebKit/public/platform/WebIDBMetadata.h"
19
20using WebKit::WebString;
[email protected]72a4183d2013-05-31 18:33:1021using WebKit::WebData;
[email protected]72a4183d2013-05-31 18:33:1022using WebKit::WebIDBDatabaseError;
23
24namespace content {
25
26WebIDBDatabaseImpl::WebIDBDatabaseImpl(
27 scoped_refptr<IndexedDBDatabase> database_backend,
28 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> database_callbacks)
29 : database_backend_(database_backend),
30 database_callbacks_(database_callbacks) {}
31
32WebIDBDatabaseImpl::~WebIDBDatabaseImpl() {}
33
34void WebIDBDatabaseImpl::createObjectStore(long long transaction_id,
35 long long object_store_id,
36 const WebString& name,
[email protected]ab4828742013-06-10 20:55:5337 const IndexedDBKeyPath& key_path,
[email protected]72a4183d2013-05-31 18:33:1038 bool auto_increment) {
39 database_backend_->CreateObjectStore(transaction_id,
40 object_store_id,
41 name,
42 IndexedDBKeyPath(key_path),
43 auto_increment);
44}
45
46void WebIDBDatabaseImpl::deleteObjectStore(long long transaction_id,
47 long long object_store_id) {
48 database_backend_->DeleteObjectStore(transaction_id, object_store_id);
49}
50
51void WebIDBDatabaseImpl::createTransaction(
52 long long id,
[email protected]2efb1c12013-06-10 02:30:4753 IndexedDBDatabaseCallbacks* /*callbacks*/,
[email protected]314231522013-06-11 12:52:0854 const std::vector<int64>& object_store_ids,
[email protected]72a4183d2013-05-31 18:33:1055 unsigned short mode) {
[email protected]cadac622013-06-11 16:46:3656 if (!database_callbacks_.get())
[email protected]72a4183d2013-05-31 18:33:1057 return;
[email protected]72a4183d2013-05-31 18:33:1058 database_backend_->CreateTransaction(
[email protected]314231522013-06-11 12:52:0859 id, database_callbacks_.get(), object_store_ids, mode);
[email protected]72a4183d2013-05-31 18:33:1060}
61
62void WebIDBDatabaseImpl::close() {
63 // Use the callbacks passed in to the constructor so that the backend in
64 // multi-process chromium knows which database connection is closing.
[email protected]cadac622013-06-11 16:46:3665 if (!database_callbacks_.get())
[email protected]72a4183d2013-05-31 18:33:1066 return;
67 database_backend_->Close(database_callbacks_);
68 database_callbacks_ = NULL;
69}
70
71void WebIDBDatabaseImpl::forceClose() {
[email protected]cadac622013-06-11 16:46:3672 if (!database_callbacks_.get())
[email protected]72a4183d2013-05-31 18:33:1073 return;
74 database_backend_->Close(database_callbacks_);
75 database_callbacks_->OnForcedClose();
76 database_callbacks_ = NULL;
77}
78
79void WebIDBDatabaseImpl::abort(long long transaction_id) {
[email protected]cadac622013-06-11 16:46:3680 if (database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:1081 database_backend_->Abort(transaction_id);
82}
83
84void WebIDBDatabaseImpl::abort(long long transaction_id,
85 const WebIDBDatabaseError& error) {
[email protected]cadac622013-06-11 16:46:3686 if (database_backend_.get())
[email protected]e3e8f252013-06-04 23:53:4487 database_backend_->Abort(transaction_id, IndexedDBDatabaseError(error));
[email protected]72a4183d2013-05-31 18:33:1088}
89
90void WebIDBDatabaseImpl::commit(long long transaction_id) {
[email protected]cadac622013-06-11 16:46:3691 if (database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:1092 database_backend_->Commit(transaction_id);
93}
94
95void WebIDBDatabaseImpl::openCursor(long long transaction_id,
96 long long object_store_id,
97 long long index_id,
[email protected]ab4828742013-06-10 20:55:5398 const IndexedDBKeyRange& key_range,
[email protected]72a4183d2013-05-31 18:33:1099 unsigned short direction,
100 bool key_only,
[email protected]2efb1c12013-06-10 02:30:47101 WebKit::WebIDBDatabase::TaskType task_type,
102 IndexedDBCallbacksBase* callbacks) {
[email protected]cadac622013-06-11 16:46:36103 if (database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10104 database_backend_->OpenCursor(
105 transaction_id,
106 object_store_id,
107 index_id,
108 make_scoped_ptr(new IndexedDBKeyRange(key_range)),
109 static_cast<indexed_db::CursorDirection>(direction),
110 key_only,
111 static_cast<IndexedDBDatabase::TaskType>(task_type),
112 IndexedDBCallbacksWrapper::Create(callbacks));
113}
114
115void WebIDBDatabaseImpl::count(long long transaction_id,
116 long long object_store_id,
117 long long index_id,
[email protected]ab4828742013-06-10 20:55:53118 const IndexedDBKeyRange& key_range,
[email protected]2efb1c12013-06-10 02:30:47119 IndexedDBCallbacksBase* callbacks) {
[email protected]cadac622013-06-11 16:46:36120 if (database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10121 database_backend_->Count(transaction_id,
122 object_store_id,
123 index_id,
124 make_scoped_ptr(new IndexedDBKeyRange(key_range)),
125 IndexedDBCallbacksWrapper::Create(callbacks));
126}
127
128void WebIDBDatabaseImpl::get(long long transaction_id,
129 long long object_store_id,
130 long long index_id,
[email protected]ab4828742013-06-10 20:55:53131 const IndexedDBKeyRange& key_range,
[email protected]72a4183d2013-05-31 18:33:10132 bool key_only,
[email protected]2efb1c12013-06-10 02:30:47133 IndexedDBCallbacksBase* callbacks) {
[email protected]cadac622013-06-11 16:46:36134 if (database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10135 database_backend_->Get(transaction_id,
136 object_store_id,
137 index_id,
138 make_scoped_ptr(new IndexedDBKeyRange(key_range)),
139 key_only,
140 IndexedDBCallbacksWrapper::Create(callbacks));
141}
142
[email protected]ab4828742013-06-10 20:55:53143void WebIDBDatabaseImpl::put(long long transaction_id,
144 long long object_store_id,
[email protected]314231522013-06-11 12:52:08145 std::vector<char>* value,
[email protected]ab4828742013-06-10 20:55:53146 const IndexedDBKey& key,
147 WebKit::WebIDBDatabase::PutMode put_mode,
148 IndexedDBCallbacksBase* callbacks,
[email protected]314231522013-06-11 12:52:08149 const std::vector<int64>& index_ids,
[email protected]ab4828742013-06-10 20:55:53150 const std::vector<IndexKeys>& index_keys) {
[email protected]cadac622013-06-11 16:46:36151 if (!database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10152 return;
153
[email protected]314231522013-06-11 12:52:08154 DCHECK_EQ(index_ids.size(), index_keys.size());
[email protected]72a4183d2013-05-31 18:33:10155
[email protected]72a4183d2013-05-31 18:33:10156 database_backend_->Put(transaction_id,
157 object_store_id,
[email protected]314231522013-06-11 12:52:08158 value,
[email protected]72a4183d2013-05-31 18:33:10159 make_scoped_ptr(new IndexedDBKey(key)),
160 static_cast<IndexedDBDatabase::PutMode>(put_mode),
161 IndexedDBCallbacksWrapper::Create(callbacks),
162 index_ids,
163 index_keys);
164}
165
166void WebIDBDatabaseImpl::setIndexKeys(
167 long long transaction_id,
168 long long object_store_id,
[email protected]ab4828742013-06-10 20:55:53169 const IndexedDBKey& primary_key,
[email protected]314231522013-06-11 12:52:08170 const std::vector<int64>& index_ids,
171 const std::vector<IndexKeys>& index_keys) {
[email protected]cadac622013-06-11 16:46:36172 if (!database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10173 return;
174
[email protected]314231522013-06-11 12:52:08175 DCHECK_EQ(index_ids.size(), index_keys.size());
[email protected]72a4183d2013-05-31 18:33:10176 database_backend_->SetIndexKeys(
177 transaction_id,
178 object_store_id,
179 make_scoped_ptr(new IndexedDBKey(primary_key)),
180 index_ids,
181 index_keys);
182}
183
184void WebIDBDatabaseImpl::setIndexesReady(
185 long long transaction_id,
186 long long object_store_id,
[email protected]314231522013-06-11 12:52:08187 const std::vector<int64>& web_index_ids) {
[email protected]cadac622013-06-11 16:46:36188 if (!database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10189 return;
190
191 std::vector<int64> index_ids(web_index_ids.size());
192 for (size_t i = 0; i < web_index_ids.size(); ++i)
193 index_ids[i] = web_index_ids[i];
194 database_backend_->SetIndexesReady(
195 transaction_id, object_store_id, index_ids);
196}
197
198void WebIDBDatabaseImpl::deleteRange(long long transaction_id,
199 long long object_store_id,
[email protected]ab4828742013-06-10 20:55:53200 const IndexedDBKeyRange& key_range,
[email protected]2efb1c12013-06-10 02:30:47201 IndexedDBCallbacksBase* callbacks) {
[email protected]cadac622013-06-11 16:46:36202 if (database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10203 database_backend_->DeleteRange(
204 transaction_id,
205 object_store_id,
206 make_scoped_ptr(new IndexedDBKeyRange(key_range)),
207 IndexedDBCallbacksWrapper::Create(callbacks));
208}
209
210void WebIDBDatabaseImpl::clear(long long transaction_id,
211 long long object_store_id,
[email protected]2efb1c12013-06-10 02:30:47212 IndexedDBCallbacksBase* callbacks) {
[email protected]cadac622013-06-11 16:46:36213 if (database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10214 database_backend_->Clear(transaction_id,
215 object_store_id,
216 IndexedDBCallbacksWrapper::Create(callbacks));
217}
218
219void WebIDBDatabaseImpl::createIndex(long long transaction_id,
220 long long object_store_id,
221 long long index_id,
222 const WebString& name,
[email protected]ab4828742013-06-10 20:55:53223 const IndexedDBKeyPath& key_path,
[email protected]72a4183d2013-05-31 18:33:10224 bool unique,
225 bool multi_entry) {
[email protected]cadac622013-06-11 16:46:36226 if (database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10227 database_backend_->CreateIndex(transaction_id,
228 object_store_id,
229 index_id,
230 name,
231 IndexedDBKeyPath(key_path),
232 unique,
233 multi_entry);
234}
235
236void WebIDBDatabaseImpl::deleteIndex(long long transaction_id,
237 long long object_store_id,
238 long long index_id) {
[email protected]cadac622013-06-11 16:46:36239 if (database_backend_.get())
[email protected]72a4183d2013-05-31 18:33:10240 database_backend_->DeleteIndex(transaction_id, object_store_id, index_id);
241}
242
243} // namespace WebKit