| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/indexed_db/indexed_db_database.h" | 5 #include "content/browser/indexed_db/indexed_db_database.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <memory> | |
| 11 #include <set> | 10 #include <set> |
| 12 #include <utility> | |
| 13 | 11 |
| 14 #include "base/auto_reset.h" | 12 #include "base/auto_reset.h" |
| 15 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 16 #include "base/logging.h" | 14 #include "base/logging.h" |
| 17 #include "base/macros.h" | 15 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 19 #include "base/memory/scoped_vector.h" | |
| 20 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
| 21 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
| 22 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 23 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 25 #include "content/browser/indexed_db/indexed_db_blob_info.h" | 22 #include "content/browser/indexed_db/indexed_db_blob_info.h" |
| 26 #include "content/browser/indexed_db/indexed_db_class_factory.h" | 23 #include "content/browser/indexed_db/indexed_db_class_factory.h" |
| 27 #include "content/browser/indexed_db/indexed_db_connection.h" | 24 #include "content/browser/indexed_db/indexed_db_connection.h" |
| 28 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 25 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 29 #include "content/browser/indexed_db/indexed_db_cursor.h" | 26 #include "content/browser/indexed_db/indexed_db_cursor.h" |
| (...skipping 1159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1189 DCHECK_EQ(WebIDBKeyTypeNumber, key.type()); | 1186 DCHECK_EQ(WebIDBKeyTypeNumber, key.type()); |
| 1190 return backing_store->MaybeUpdateKeyGeneratorCurrentNumber( | 1187 return backing_store->MaybeUpdateKeyGeneratorCurrentNumber( |
| 1191 transaction->BackingStoreTransaction(), database_id, object_store_id, | 1188 transaction->BackingStoreTransaction(), database_id, object_store_id, |
| 1192 static_cast<int64_t>(floor(key.number())) + 1, check_current); | 1189 static_cast<int64_t>(floor(key.number())) + 1, check_current); |
| 1193 } | 1190 } |
| 1194 | 1191 |
| 1195 struct IndexedDBDatabase::PutOperationParams { | 1192 struct IndexedDBDatabase::PutOperationParams { |
| 1196 PutOperationParams() {} | 1193 PutOperationParams() {} |
| 1197 int64_t object_store_id; | 1194 int64_t object_store_id; |
| 1198 IndexedDBValue value; | 1195 IndexedDBValue value; |
| 1199 ScopedVector<storage::BlobDataHandle> handles; | 1196 std::vector<std::unique_ptr<storage::BlobDataHandle>> handles; |
| 1200 std::unique_ptr<IndexedDBKey> key; | 1197 std::unique_ptr<IndexedDBKey> key; |
| 1201 blink::WebIDBPutMode put_mode; | 1198 blink::WebIDBPutMode put_mode; |
| 1202 scoped_refptr<IndexedDBCallbacks> callbacks; | 1199 scoped_refptr<IndexedDBCallbacks> callbacks; |
| 1203 std::vector<IndexKeys> index_keys; | 1200 std::vector<IndexKeys> index_keys; |
| 1204 | 1201 |
| 1205 private: | 1202 private: |
| 1206 DISALLOW_COPY_AND_ASSIGN(PutOperationParams); | 1203 DISALLOW_COPY_AND_ASSIGN(PutOperationParams); |
| 1207 }; | 1204 }; |
| 1208 | 1205 |
| 1209 void IndexedDBDatabase::Put(int64_t transaction_id, | 1206 void IndexedDBDatabase::Put( |
| 1210 int64_t object_store_id, | 1207 int64_t transaction_id, |
| 1211 IndexedDBValue* value, | 1208 int64_t object_store_id, |
| 1212 ScopedVector<storage::BlobDataHandle>* handles, | 1209 IndexedDBValue* value, |
| 1213 std::unique_ptr<IndexedDBKey> key, | 1210 std::vector<std::unique_ptr<storage::BlobDataHandle>>* handles, |
| 1214 blink::WebIDBPutMode put_mode, | 1211 std::unique_ptr<IndexedDBKey> key, |
| 1215 scoped_refptr<IndexedDBCallbacks> callbacks, | 1212 blink::WebIDBPutMode put_mode, |
| 1216 const std::vector<IndexKeys>& index_keys) { | 1213 scoped_refptr<IndexedDBCallbacks> callbacks, |
| 1214 const std::vector<IndexKeys>& index_keys) { |
| 1217 IDB_TRACE1("IndexedDBDatabase::Put", "txn.id", transaction_id); | 1215 IDB_TRACE1("IndexedDBDatabase::Put", "txn.id", transaction_id); |
| 1218 IndexedDBTransaction* transaction = GetTransaction(transaction_id); | 1216 IndexedDBTransaction* transaction = GetTransaction(transaction_id); |
| 1219 if (!transaction) | 1217 if (!transaction) |
| 1220 return; | 1218 return; |
| 1221 DCHECK_NE(transaction->mode(), blink::WebIDBTransactionModeReadOnly); | 1219 DCHECK_NE(transaction->mode(), blink::WebIDBTransactionModeReadOnly); |
| 1222 | 1220 |
| 1223 if (!ValidateObjectStoreId(object_store_id)) | 1221 if (!ValidateObjectStoreId(object_store_id)) |
| 1224 return; | 1222 return; |
| 1225 | 1223 |
| 1226 DCHECK(key); | 1224 DCHECK(key); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1288 return; | 1286 return; |
| 1289 } | 1287 } |
| 1290 if (found) { | 1288 if (found) { |
| 1291 params->callbacks->OnError( | 1289 params->callbacks->OnError( |
| 1292 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionConstraintError, | 1290 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionConstraintError, |
| 1293 "Key already exists in the object store.")); | 1291 "Key already exists in the object store.")); |
| 1294 return; | 1292 return; |
| 1295 } | 1293 } |
| 1296 } | 1294 } |
| 1297 | 1295 |
| 1298 ScopedVector<IndexWriter> index_writers; | 1296 std::vector<std::unique_ptr<IndexWriter>> index_writers; |
| 1299 base::string16 error_message; | 1297 base::string16 error_message; |
| 1300 bool obeys_constraints = false; | 1298 bool obeys_constraints = false; |
| 1301 bool backing_store_success = MakeIndexWriters(transaction, | 1299 bool backing_store_success = MakeIndexWriters(transaction, |
| 1302 backing_store_.get(), | 1300 backing_store_.get(), |
| 1303 id(), | 1301 id(), |
| 1304 object_store, | 1302 object_store, |
| 1305 *key, | 1303 *key, |
| 1306 key_was_generated, | 1304 key_was_generated, |
| 1307 params->index_keys, | 1305 params->index_keys, |
| 1308 &index_writers, | 1306 &index_writers, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1335 blink::WebIDBDatabaseExceptionUnknownError, | 1333 blink::WebIDBDatabaseExceptionUnknownError, |
| 1336 "Internal error: backing store error performing put/add."); | 1334 "Internal error: backing store error performing put/add."); |
| 1337 params->callbacks->OnError(error); | 1335 params->callbacks->OnError(error); |
| 1338 if (s.IsCorruption()) | 1336 if (s.IsCorruption()) |
| 1339 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); | 1337 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); |
| 1340 return; | 1338 return; |
| 1341 } | 1339 } |
| 1342 { | 1340 { |
| 1343 IDB_TRACE1("IndexedDBDatabase::PutOperation.UpdateIndexes", "txn.id", | 1341 IDB_TRACE1("IndexedDBDatabase::PutOperation.UpdateIndexes", "txn.id", |
| 1344 transaction->id()); | 1342 transaction->id()); |
| 1345 for (size_t i = 0; i < index_writers.size(); ++i) { | 1343 for (const auto& writer : index_writers) { |
| 1346 IndexWriter* index_writer = index_writers[i]; | 1344 writer->WriteIndexKeys(record_identifier, backing_store_.get(), |
| 1347 index_writer->WriteIndexKeys(record_identifier, backing_store_.get(), | 1345 transaction->BackingStoreTransaction(), id(), |
| 1348 transaction->BackingStoreTransaction(), id(), | 1346 params->object_store_id); |
| 1349 params->object_store_id); | |
| 1350 } | 1347 } |
| 1351 } | 1348 } |
| 1352 | 1349 |
| 1353 if (object_store.auto_increment && | 1350 if (object_store.auto_increment && |
| 1354 params->put_mode != blink::WebIDBPutModeCursorUpdate && | 1351 params->put_mode != blink::WebIDBPutModeCursorUpdate && |
| 1355 key->type() == WebIDBKeyTypeNumber) { | 1352 key->type() == WebIDBKeyTypeNumber) { |
| 1356 IDB_TRACE1("IndexedDBDatabase::PutOperation.AutoIncrement", "txn.id", | 1353 IDB_TRACE1("IndexedDBDatabase::PutOperation.AutoIncrement", "txn.id", |
| 1357 transaction->id()); | 1354 transaction->id()); |
| 1358 leveldb::Status s = UpdateKeyGenerator(backing_store_.get(), | 1355 leveldb::Status s = UpdateKeyGenerator(backing_store_.get(), |
| 1359 transaction, | 1356 transaction, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1411 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); | 1408 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); |
| 1412 return; | 1409 return; |
| 1413 } | 1410 } |
| 1414 if (!found) { | 1411 if (!found) { |
| 1415 transaction->Abort(IndexedDBDatabaseError( | 1412 transaction->Abort(IndexedDBDatabaseError( |
| 1416 blink::WebIDBDatabaseExceptionUnknownError, | 1413 blink::WebIDBDatabaseExceptionUnknownError, |
| 1417 "Internal error setting index keys for object store.")); | 1414 "Internal error setting index keys for object store.")); |
| 1418 return; | 1415 return; |
| 1419 } | 1416 } |
| 1420 | 1417 |
| 1421 ScopedVector<IndexWriter> index_writers; | 1418 std::vector<std::unique_ptr<IndexWriter>> index_writers; |
| 1422 base::string16 error_message; | 1419 base::string16 error_message; |
| 1423 bool obeys_constraints = false; | 1420 bool obeys_constraints = false; |
| 1424 DCHECK(metadata_.object_stores.find(object_store_id) != | 1421 DCHECK(metadata_.object_stores.find(object_store_id) != |
| 1425 metadata_.object_stores.end()); | 1422 metadata_.object_stores.end()); |
| 1426 const IndexedDBObjectStoreMetadata& object_store_metadata = | 1423 const IndexedDBObjectStoreMetadata& object_store_metadata = |
| 1427 metadata_.object_stores[object_store_id]; | 1424 metadata_.object_stores[object_store_id]; |
| 1428 bool backing_store_success = MakeIndexWriters(transaction, | 1425 bool backing_store_success = MakeIndexWriters(transaction, |
| 1429 backing_store_.get(), | 1426 backing_store_.get(), |
| 1430 id(), | 1427 id(), |
| 1431 object_store_metadata, | 1428 object_store_metadata, |
| 1432 *primary_key, | 1429 *primary_key, |
| 1433 false, | 1430 false, |
| 1434 index_keys, | 1431 index_keys, |
| 1435 &index_writers, | 1432 &index_writers, |
| 1436 &error_message, | 1433 &error_message, |
| 1437 &obeys_constraints); | 1434 &obeys_constraints); |
| 1438 if (!backing_store_success) { | 1435 if (!backing_store_success) { |
| 1439 transaction->Abort(IndexedDBDatabaseError( | 1436 transaction->Abort(IndexedDBDatabaseError( |
| 1440 blink::WebIDBDatabaseExceptionUnknownError, | 1437 blink::WebIDBDatabaseExceptionUnknownError, |
| 1441 "Internal error: backing store error updating index keys.")); | 1438 "Internal error: backing store error updating index keys.")); |
| 1442 return; | 1439 return; |
| 1443 } | 1440 } |
| 1444 if (!obeys_constraints) { | 1441 if (!obeys_constraints) { |
| 1445 transaction->Abort(IndexedDBDatabaseError( | 1442 transaction->Abort(IndexedDBDatabaseError( |
| 1446 blink::WebIDBDatabaseExceptionConstraintError, error_message)); | 1443 blink::WebIDBDatabaseExceptionConstraintError, error_message)); |
| 1447 return; | 1444 return; |
| 1448 } | 1445 } |
| 1449 | 1446 |
| 1450 for (size_t i = 0; i < index_writers.size(); ++i) { | 1447 for (const auto& writer : index_writers) { |
| 1451 IndexWriter* index_writer = index_writers[i]; | 1448 writer->WriteIndexKeys(record_identifier, backing_store_.get(), |
| 1452 index_writer->WriteIndexKeys(record_identifier, | 1449 transaction->BackingStoreTransaction(), id(), |
| 1453 backing_store_.get(), | 1450 object_store_id); |
| 1454 transaction->BackingStoreTransaction(), | |
| 1455 id(), | |
| 1456 object_store_id); | |
| 1457 } | 1451 } |
| 1458 } | 1452 } |
| 1459 | 1453 |
| 1460 void IndexedDBDatabase::SetIndexesReady(int64_t transaction_id, | 1454 void IndexedDBDatabase::SetIndexesReady(int64_t transaction_id, |
| 1461 int64_t, | 1455 int64_t, |
| 1462 const std::vector<int64_t>& index_ids) { | 1456 const std::vector<int64_t>& index_ids) { |
| 1463 IndexedDBTransaction* transaction = GetTransaction(transaction_id); | 1457 IndexedDBTransaction* transaction = GetTransaction(transaction_id); |
| 1464 if (!transaction) | 1458 if (!transaction) |
| 1465 return; | 1459 return; |
| 1466 DCHECK_EQ(transaction->mode(), blink::WebIDBTransactionModeVersionChange); | 1460 DCHECK_EQ(transaction->mode(), blink::WebIDBTransactionModeVersionChange); |
| (...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2004 | 1998 |
| 2005 void IndexedDBDatabase::VersionChangeAbortOperation( | 1999 void IndexedDBDatabase::VersionChangeAbortOperation( |
| 2006 int64_t previous_version, | 2000 int64_t previous_version, |
| 2007 IndexedDBTransaction* transaction) { | 2001 IndexedDBTransaction* transaction) { |
| 2008 DCHECK(!transaction); | 2002 DCHECK(!transaction); |
| 2009 IDB_TRACE("IndexedDBDatabase::VersionChangeAbortOperation"); | 2003 IDB_TRACE("IndexedDBDatabase::VersionChangeAbortOperation"); |
| 2010 metadata_.version = previous_version; | 2004 metadata_.version = previous_version; |
| 2011 } | 2005 } |
| 2012 | 2006 |
| 2013 } // namespace content | 2007 } // namespace content |
| OLD | NEW |