[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 1 | // 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] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 17 | #include "third_party/WebKit/public/platform/WebIDBDatabaseError.h" |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 18 | #include "third_party/WebKit/public/platform/WebIDBMetadata.h" |
| 19 | |
| 20 | using WebKit::WebString; |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 21 | using WebKit::WebData; |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 22 | using WebKit::WebIDBDatabaseError; |
| 23 | |
| 24 | namespace content { |
| 25 | |
| 26 | WebIDBDatabaseImpl::WebIDBDatabaseImpl( |
| 27 | scoped_refptr<IndexedDBDatabase> database_backend, |
| 28 | scoped_refptr<IndexedDBDatabaseCallbacksWrapper> database_callbacks) |
| 29 | : database_backend_(database_backend), |
| 30 | database_callbacks_(database_callbacks) {} |
| 31 | |
| 32 | WebIDBDatabaseImpl::~WebIDBDatabaseImpl() {} |
| 33 | |
| 34 | void WebIDBDatabaseImpl::createObjectStore(long long transaction_id, |
| 35 | long long object_store_id, |
| 36 | const WebString& name, |
[email protected] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 37 | const IndexedDBKeyPath& key_path, |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 38 | bool auto_increment) { |
| 39 | database_backend_->CreateObjectStore(transaction_id, |
| 40 | object_store_id, |
| 41 | name, |
| 42 | IndexedDBKeyPath(key_path), |
| 43 | auto_increment); |
| 44 | } |
| 45 | |
| 46 | void WebIDBDatabaseImpl::deleteObjectStore(long long transaction_id, |
| 47 | long long object_store_id) { |
| 48 | database_backend_->DeleteObjectStore(transaction_id, object_store_id); |
| 49 | } |
| 50 | |
| 51 | void WebIDBDatabaseImpl::createTransaction( |
| 52 | long long id, |
[email protected] | 2efb1c1 | 2013-06-10 02:30:47 | [diff] [blame] | 53 | IndexedDBDatabaseCallbacks* /*callbacks*/, |
[email protected] | 31423152 | 2013-06-11 12:52:08 | [diff] [blame] | 54 | const std::vector<int64>& object_store_ids, |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 55 | unsigned short mode) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 56 | if (!database_callbacks_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 57 | return; |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 58 | database_backend_->CreateTransaction( |
[email protected] | 31423152 | 2013-06-11 12:52:08 | [diff] [blame] | 59 | id, database_callbacks_.get(), object_store_ids, mode); |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void 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] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 65 | if (!database_callbacks_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 66 | return; |
| 67 | database_backend_->Close(database_callbacks_); |
| 68 | database_callbacks_ = NULL; |
| 69 | } |
| 70 | |
| 71 | void WebIDBDatabaseImpl::forceClose() { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 72 | if (!database_callbacks_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 73 | return; |
| 74 | database_backend_->Close(database_callbacks_); |
| 75 | database_callbacks_->OnForcedClose(); |
| 76 | database_callbacks_ = NULL; |
| 77 | } |
| 78 | |
| 79 | void WebIDBDatabaseImpl::abort(long long transaction_id) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 80 | if (database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 81 | database_backend_->Abort(transaction_id); |
| 82 | } |
| 83 | |
| 84 | void WebIDBDatabaseImpl::abort(long long transaction_id, |
| 85 | const WebIDBDatabaseError& error) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 86 | if (database_backend_.get()) |
[email protected] | e3e8f25 | 2013-06-04 23:53:44 | [diff] [blame] | 87 | database_backend_->Abort(transaction_id, IndexedDBDatabaseError(error)); |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void WebIDBDatabaseImpl::commit(long long transaction_id) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 91 | if (database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 92 | database_backend_->Commit(transaction_id); |
| 93 | } |
| 94 | |
| 95 | void WebIDBDatabaseImpl::openCursor(long long transaction_id, |
| 96 | long long object_store_id, |
| 97 | long long index_id, |
[email protected] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 98 | const IndexedDBKeyRange& key_range, |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 99 | unsigned short direction, |
| 100 | bool key_only, |
[email protected] | 2efb1c1 | 2013-06-10 02:30:47 | [diff] [blame] | 101 | WebKit::WebIDBDatabase::TaskType task_type, |
| 102 | IndexedDBCallbacksBase* callbacks) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 103 | if (database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 104 | 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 | |
| 115 | void WebIDBDatabaseImpl::count(long long transaction_id, |
| 116 | long long object_store_id, |
| 117 | long long index_id, |
[email protected] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 118 | const IndexedDBKeyRange& key_range, |
[email protected] | 2efb1c1 | 2013-06-10 02:30:47 | [diff] [blame] | 119 | IndexedDBCallbacksBase* callbacks) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 120 | if (database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 121 | 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 | |
| 128 | void WebIDBDatabaseImpl::get(long long transaction_id, |
| 129 | long long object_store_id, |
| 130 | long long index_id, |
[email protected] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 131 | const IndexedDBKeyRange& key_range, |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 132 | bool key_only, |
[email protected] | 2efb1c1 | 2013-06-10 02:30:47 | [diff] [blame] | 133 | IndexedDBCallbacksBase* callbacks) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 134 | if (database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 135 | 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] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 143 | void WebIDBDatabaseImpl::put(long long transaction_id, |
| 144 | long long object_store_id, |
[email protected] | 31423152 | 2013-06-11 12:52:08 | [diff] [blame] | 145 | std::vector<char>* value, |
[email protected] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 146 | const IndexedDBKey& key, |
| 147 | WebKit::WebIDBDatabase::PutMode put_mode, |
| 148 | IndexedDBCallbacksBase* callbacks, |
[email protected] | 31423152 | 2013-06-11 12:52:08 | [diff] [blame] | 149 | const std::vector<int64>& index_ids, |
[email protected] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 150 | const std::vector<IndexKeys>& index_keys) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 151 | if (!database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 152 | return; |
| 153 | |
[email protected] | 31423152 | 2013-06-11 12:52:08 | [diff] [blame] | 154 | DCHECK_EQ(index_ids.size(), index_keys.size()); |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 155 | |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 156 | database_backend_->Put(transaction_id, |
| 157 | object_store_id, |
[email protected] | 31423152 | 2013-06-11 12:52:08 | [diff] [blame] | 158 | value, |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 159 | 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 | |
| 166 | void WebIDBDatabaseImpl::setIndexKeys( |
| 167 | long long transaction_id, |
| 168 | long long object_store_id, |
[email protected] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 169 | const IndexedDBKey& primary_key, |
[email protected] | 31423152 | 2013-06-11 12:52:08 | [diff] [blame] | 170 | const std::vector<int64>& index_ids, |
| 171 | const std::vector<IndexKeys>& index_keys) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 172 | if (!database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 173 | return; |
| 174 | |
[email protected] | 31423152 | 2013-06-11 12:52:08 | [diff] [blame] | 175 | DCHECK_EQ(index_ids.size(), index_keys.size()); |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 176 | 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 | |
| 184 | void WebIDBDatabaseImpl::setIndexesReady( |
| 185 | long long transaction_id, |
| 186 | long long object_store_id, |
[email protected] | 31423152 | 2013-06-11 12:52:08 | [diff] [blame] | 187 | const std::vector<int64>& web_index_ids) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 188 | if (!database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 189 | 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 | |
| 198 | void WebIDBDatabaseImpl::deleteRange(long long transaction_id, |
| 199 | long long object_store_id, |
[email protected] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 200 | const IndexedDBKeyRange& key_range, |
[email protected] | 2efb1c1 | 2013-06-10 02:30:47 | [diff] [blame] | 201 | IndexedDBCallbacksBase* callbacks) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 202 | if (database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 203 | database_backend_->DeleteRange( |
| 204 | transaction_id, |
| 205 | object_store_id, |
| 206 | make_scoped_ptr(new IndexedDBKeyRange(key_range)), |
| 207 | IndexedDBCallbacksWrapper::Create(callbacks)); |
| 208 | } |
| 209 | |
| 210 | void WebIDBDatabaseImpl::clear(long long transaction_id, |
| 211 | long long object_store_id, |
[email protected] | 2efb1c1 | 2013-06-10 02:30:47 | [diff] [blame] | 212 | IndexedDBCallbacksBase* callbacks) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 213 | if (database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 214 | database_backend_->Clear(transaction_id, |
| 215 | object_store_id, |
| 216 | IndexedDBCallbacksWrapper::Create(callbacks)); |
| 217 | } |
| 218 | |
| 219 | void WebIDBDatabaseImpl::createIndex(long long transaction_id, |
| 220 | long long object_store_id, |
| 221 | long long index_id, |
| 222 | const WebString& name, |
[email protected] | ab482874 | 2013-06-10 20:55:53 | [diff] [blame] | 223 | const IndexedDBKeyPath& key_path, |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 224 | bool unique, |
| 225 | bool multi_entry) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 226 | if (database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 227 | 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 | |
| 236 | void WebIDBDatabaseImpl::deleteIndex(long long transaction_id, |
| 237 | long long object_store_id, |
| 238 | long long index_id) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame^] | 239 | if (database_backend_.get()) |
[email protected] | 72a4183d | 2013-05-31 18:33:10 | [diff] [blame] | 240 | database_backend_->DeleteIndex(transaction_id, object_store_id, index_id); |
| 241 | } |
| 242 | |
| 243 | } // namespace WebKit |