OLD | NEW |
(Empty) | |
| 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 #include "base/logging.h" |
| 9 #include "content/browser/indexed_db/indexed_db_callbacks_wrapper.h" |
| 10 #include "content/browser/indexed_db/indexed_db_cursor.h" |
| 11 #include "content/browser/indexed_db/indexed_db_database.h" |
| 12 #include "content/browser/indexed_db/indexed_db_metadata.h" |
| 13 #include "content/common/indexed_db/indexed_db_key_range.h" |
| 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebData.h" |
| 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebIDBCallbacks.h" |
| 16 #include "third_party/WebKit/Source/Platform/chromium/public/WebIDBDatabaseCallb
acks.h" |
| 17 #include "third_party/WebKit/Source/Platform/chromium/public/WebIDBDatabaseError
.h" |
| 18 #include "third_party/WebKit/Source/Platform/chromium/public/WebIDBKey.h" |
| 19 #include "third_party/WebKit/Source/Platform/chromium/public/WebIDBKeyRange.h" |
| 20 #include "third_party/WebKit/Source/Platform/chromium/public/WebIDBMetadata.h" |
| 21 |
| 22 using WebKit::WebString; |
| 23 using WebKit::WebIDBKey; |
| 24 using WebKit::WebData; |
| 25 using WebKit::WebIDBKeyPath; |
| 26 using WebKit::WebIDBKeyRange; |
| 27 using WebKit::WebIDBDatabaseCallbacks; |
| 28 using WebKit::WebIDBCallbacks; |
| 29 using WebKit::WebVector; |
| 30 using WebKit::WebIDBDatabaseError; |
| 31 |
| 32 namespace content { |
| 33 |
| 34 WebIDBDatabaseImpl::WebIDBDatabaseImpl( |
| 35 scoped_refptr<IndexedDBDatabase> database_backend, |
| 36 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> database_callbacks) |
| 37 : database_backend_(database_backend), |
| 38 database_callbacks_(database_callbacks) {} |
| 39 |
| 40 WebIDBDatabaseImpl::~WebIDBDatabaseImpl() {} |
| 41 |
| 42 void WebIDBDatabaseImpl::createObjectStore(long long transaction_id, |
| 43 long long object_store_id, |
| 44 const WebString& name, |
| 45 const WebIDBKeyPath& key_path, |
| 46 bool auto_increment) { |
| 47 database_backend_->CreateObjectStore(transaction_id, |
| 48 object_store_id, |
| 49 name, |
| 50 IndexedDBKeyPath(key_path), |
| 51 auto_increment); |
| 52 } |
| 53 |
| 54 void WebIDBDatabaseImpl::deleteObjectStore(long long transaction_id, |
| 55 long long object_store_id) { |
| 56 database_backend_->DeleteObjectStore(transaction_id, object_store_id); |
| 57 } |
| 58 |
| 59 void WebIDBDatabaseImpl::createTransaction( |
| 60 long long id, |
| 61 WebIDBDatabaseCallbacks*, |
| 62 const WebVector<long long>& object_store_ids, |
| 63 unsigned short mode) { |
| 64 if (!database_callbacks_) |
| 65 return; |
| 66 std::vector<int64_t> object_store_id_list(object_store_ids.size()); |
| 67 for (size_t i = 0; i < object_store_ids.size(); ++i) |
| 68 object_store_id_list[i] = object_store_ids[i]; |
| 69 database_backend_->CreateTransaction( |
| 70 id, database_callbacks_.get(), object_store_id_list, mode); |
| 71 } |
| 72 |
| 73 void WebIDBDatabaseImpl::close() { |
| 74 // Use the callbacks passed in to the constructor so that the backend in |
| 75 // multi-process chromium knows which database connection is closing. |
| 76 if (!database_callbacks_) |
| 77 return; |
| 78 database_backend_->Close(database_callbacks_); |
| 79 database_callbacks_ = NULL; |
| 80 } |
| 81 |
| 82 void WebIDBDatabaseImpl::forceClose() { |
| 83 if (!database_callbacks_) |
| 84 return; |
| 85 database_backend_->Close(database_callbacks_); |
| 86 database_callbacks_->OnForcedClose(); |
| 87 database_callbacks_ = NULL; |
| 88 } |
| 89 |
| 90 void WebIDBDatabaseImpl::abort(long long transaction_id) { |
| 91 if (database_backend_) |
| 92 database_backend_->Abort(transaction_id); |
| 93 } |
| 94 |
| 95 void WebIDBDatabaseImpl::abort(long long transaction_id, |
| 96 const WebIDBDatabaseError& error) { |
| 97 if (database_backend_) |
| 98 database_backend_->Abort(transaction_id, |
| 99 IndexedDBDatabaseError::Create(error)); |
| 100 } |
| 101 |
| 102 void WebIDBDatabaseImpl::commit(long long transaction_id) { |
| 103 if (database_backend_) |
| 104 database_backend_->Commit(transaction_id); |
| 105 } |
| 106 |
| 107 void WebIDBDatabaseImpl::openCursor(long long transaction_id, |
| 108 long long object_store_id, |
| 109 long long index_id, |
| 110 const WebIDBKeyRange& key_range, |
| 111 unsigned short direction, |
| 112 bool key_only, |
| 113 TaskType task_type, |
| 114 WebIDBCallbacks* callbacks) { |
| 115 if (database_backend_) |
| 116 database_backend_->OpenCursor( |
| 117 transaction_id, |
| 118 object_store_id, |
| 119 index_id, |
| 120 make_scoped_ptr(new IndexedDBKeyRange(key_range)), |
| 121 static_cast<indexed_db::CursorDirection>(direction), |
| 122 key_only, |
| 123 static_cast<IndexedDBDatabase::TaskType>(task_type), |
| 124 IndexedDBCallbacksWrapper::Create(callbacks)); |
| 125 } |
| 126 |
| 127 void WebIDBDatabaseImpl::count(long long transaction_id, |
| 128 long long object_store_id, |
| 129 long long index_id, |
| 130 const WebIDBKeyRange& key_range, |
| 131 WebIDBCallbacks* callbacks) { |
| 132 if (database_backend_) |
| 133 database_backend_->Count(transaction_id, |
| 134 object_store_id, |
| 135 index_id, |
| 136 make_scoped_ptr(new IndexedDBKeyRange(key_range)), |
| 137 IndexedDBCallbacksWrapper::Create(callbacks)); |
| 138 } |
| 139 |
| 140 void WebIDBDatabaseImpl::get(long long transaction_id, |
| 141 long long object_store_id, |
| 142 long long index_id, |
| 143 const WebIDBKeyRange& key_range, |
| 144 bool key_only, |
| 145 WebIDBCallbacks* callbacks) { |
| 146 if (database_backend_) |
| 147 database_backend_->Get(transaction_id, |
| 148 object_store_id, |
| 149 index_id, |
| 150 make_scoped_ptr(new IndexedDBKeyRange(key_range)), |
| 151 key_only, |
| 152 IndexedDBCallbacksWrapper::Create(callbacks)); |
| 153 } |
| 154 |
| 155 void WebIDBDatabaseImpl::put(long long transaction_id, |
| 156 long long object_store_id, |
| 157 const WebData& value, |
| 158 const WebIDBKey& key, |
| 159 PutMode put_mode, |
| 160 WebIDBCallbacks* callbacks, |
| 161 const WebVector<long long>& web_index_ids, |
| 162 const WebVector<WebIndexKeys>& web_index_keys) { |
| 163 if (!database_backend_) |
| 164 return; |
| 165 |
| 166 DCHECK_EQ(web_index_ids.size(), web_index_keys.size()); |
| 167 std::vector<int64_t> index_ids(web_index_ids.size()); |
| 168 std::vector<IndexedDBDatabase::IndexKeys> index_keys(web_index_keys.size()); |
| 169 |
| 170 for (size_t i = 0; i < web_index_ids.size(); ++i) { |
| 171 index_ids[i] = web_index_ids[i]; |
| 172 IndexedDBKey::KeyArray index_key_list; |
| 173 for (size_t j = 0; j < web_index_keys[i].size(); ++j) |
| 174 index_key_list.push_back(IndexedDBKey(web_index_keys[i][j])); |
| 175 index_keys[i] = index_key_list; |
| 176 } |
| 177 |
| 178 std::vector<char> value_buffer(value.data(), value.data() + value.size()); |
| 179 database_backend_->Put(transaction_id, |
| 180 object_store_id, |
| 181 &value_buffer, |
| 182 make_scoped_ptr(new IndexedDBKey(key)), |
| 183 static_cast<IndexedDBDatabase::PutMode>(put_mode), |
| 184 IndexedDBCallbacksWrapper::Create(callbacks), |
| 185 index_ids, |
| 186 index_keys); |
| 187 } |
| 188 |
| 189 void WebIDBDatabaseImpl::setIndexKeys( |
| 190 long long transaction_id, |
| 191 long long object_store_id, |
| 192 const WebIDBKey& primary_key, |
| 193 const WebVector<long long>& web_index_ids, |
| 194 const WebVector<WebIndexKeys>& web_index_keys) { |
| 195 if (!database_backend_) |
| 196 return; |
| 197 |
| 198 DCHECK_EQ(web_index_ids.size(), web_index_keys.size()); |
| 199 std::vector<int64_t> index_ids(web_index_ids.size()); |
| 200 std::vector<IndexedDBDatabase::IndexKeys> index_keys(web_index_keys.size()); |
| 201 |
| 202 for (size_t i = 0; i < web_index_ids.size(); ++i) { |
| 203 index_ids[i] = web_index_ids[i]; |
| 204 IndexedDBKey::KeyArray index_key_list; |
| 205 for (size_t j = 0; j < web_index_keys[i].size(); ++j) |
| 206 index_key_list.push_back(IndexedDBKey(web_index_keys[i][j])); |
| 207 index_keys[i] = index_key_list; |
| 208 } |
| 209 database_backend_->SetIndexKeys( |
| 210 transaction_id, |
| 211 object_store_id, |
| 212 make_scoped_ptr(new IndexedDBKey(primary_key)), |
| 213 index_ids, |
| 214 index_keys); |
| 215 } |
| 216 |
| 217 void WebIDBDatabaseImpl::setIndexesReady( |
| 218 long long transaction_id, |
| 219 long long object_store_id, |
| 220 const WebVector<long long>& web_index_ids) { |
| 221 if (!database_backend_) |
| 222 return; |
| 223 |
| 224 std::vector<int64_t> index_ids(web_index_ids.size()); |
| 225 for (size_t i = 0; i < web_index_ids.size(); ++i) |
| 226 index_ids[i] = web_index_ids[i]; |
| 227 database_backend_->SetIndexesReady( |
| 228 transaction_id, object_store_id, index_ids); |
| 229 } |
| 230 |
| 231 void WebIDBDatabaseImpl::deleteRange(long long transaction_id, |
| 232 long long object_store_id, |
| 233 const WebIDBKeyRange& key_range, |
| 234 WebIDBCallbacks* callbacks) { |
| 235 if (database_backend_) |
| 236 database_backend_->DeleteRange( |
| 237 transaction_id, |
| 238 object_store_id, |
| 239 make_scoped_ptr(new IndexedDBKeyRange(key_range)), |
| 240 IndexedDBCallbacksWrapper::Create(callbacks)); |
| 241 } |
| 242 |
| 243 void WebIDBDatabaseImpl::clear(long long transaction_id, |
| 244 long long object_store_id, |
| 245 WebIDBCallbacks* callbacks) { |
| 246 if (database_backend_) |
| 247 database_backend_->Clear(transaction_id, |
| 248 object_store_id, |
| 249 IndexedDBCallbacksWrapper::Create(callbacks)); |
| 250 } |
| 251 |
| 252 void WebIDBDatabaseImpl::createIndex(long long transaction_id, |
| 253 long long object_store_id, |
| 254 long long index_id, |
| 255 const WebString& name, |
| 256 const WebIDBKeyPath& key_path, |
| 257 bool unique, |
| 258 bool multi_entry) { |
| 259 if (database_backend_) |
| 260 database_backend_->CreateIndex(transaction_id, |
| 261 object_store_id, |
| 262 index_id, |
| 263 name, |
| 264 IndexedDBKeyPath(key_path), |
| 265 unique, |
| 266 multi_entry); |
| 267 } |
| 268 |
| 269 void WebIDBDatabaseImpl::deleteIndex(long long transaction_id, |
| 270 long long object_store_id, |
| 271 long long index_id) { |
| 272 if (database_backend_) |
| 273 database_backend_->DeleteIndex(transaction_id, object_store_id, index_id); |
| 274 } |
| 275 |
| 276 } // namespace WebKit |
OLD | NEW |