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_backing_store.h" | 5 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 if (!found) { | 216 if (!found) { |
217 *known = true; | 217 *known = true; |
218 return true; | 218 return true; |
219 } | 219 } |
220 if (db_schema_version > kLatestKnownSchemaVersion) { | 220 if (db_schema_version > kLatestKnownSchemaVersion) { |
221 *known = false; | 221 *known = false; |
222 return true; | 222 return true; |
223 } | 223 } |
224 | 224 |
225 const uint32 latest_known_data_version = | 225 const uint32 latest_known_data_version = |
226 WebKit::kSerializedScriptValueVersion; | 226 blink::kSerializedScriptValueVersion; |
227 int64 db_data_version = 0; | 227 int64 db_data_version = 0; |
228 ok = GetInt(db, DataVersionKey::Encode(), &db_data_version, &found); | 228 ok = GetInt(db, DataVersionKey::Encode(), &db_data_version, &found); |
229 if (!ok) | 229 if (!ok) |
230 return false; | 230 return false; |
231 if (!found) { | 231 if (!found) { |
232 *known = true; | 232 *known = true; |
233 return true; | 233 return true; |
234 } | 234 } |
235 | 235 |
236 if (db_data_version > latest_known_data_version) { | 236 if (db_data_version > latest_known_data_version) { |
237 *known = false; | 237 *known = false; |
238 return true; | 238 return true; |
239 } | 239 } |
240 | 240 |
241 *known = true; | 241 *known = true; |
242 return true; | 242 return true; |
243 } | 243 } |
244 | 244 |
245 WARN_UNUSED_RESULT static bool SetUpMetadata( | 245 WARN_UNUSED_RESULT static bool SetUpMetadata( |
246 LevelDBDatabase* db, | 246 LevelDBDatabase* db, |
247 const std::string& origin_identifier) { | 247 const std::string& origin_identifier) { |
248 const uint32 latest_known_data_version = | 248 const uint32 latest_known_data_version = |
249 WebKit::kSerializedScriptValueVersion; | 249 blink::kSerializedScriptValueVersion; |
250 const std::string schema_version_key = SchemaVersionKey::Encode(); | 250 const std::string schema_version_key = SchemaVersionKey::Encode(); |
251 const std::string data_version_key = DataVersionKey::Encode(); | 251 const std::string data_version_key = DataVersionKey::Encode(); |
252 | 252 |
253 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db); | 253 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db); |
254 | 254 |
255 int64 db_schema_version = 0; | 255 int64 db_schema_version = 0; |
256 int64 db_data_version = 0; | 256 int64 db_data_version = 0; |
257 bool found = false; | 257 bool found = false; |
258 bool ok = | 258 bool ok = |
259 GetInt(transaction.get(), schema_version_key, &db_schema_version, &found); | 259 GetInt(transaction.get(), schema_version_key, &db_schema_version, &found); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 std::string int_version_key = DatabaseMetaDataKey::Encode( | 295 std::string int_version_key = DatabaseMetaDataKey::Encode( |
296 database_id, DatabaseMetaDataKey::USER_INT_VERSION); | 296 database_id, DatabaseMetaDataKey::USER_INT_VERSION); |
297 PutVarInt(transaction.get(), | 297 PutVarInt(transaction.get(), |
298 int_version_key, | 298 int_version_key, |
299 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); | 299 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
300 } | 300 } |
301 } | 301 } |
302 if (db_schema_version < 2) { | 302 if (db_schema_version < 2) { |
303 db_schema_version = 2; | 303 db_schema_version = 2; |
304 PutInt(transaction.get(), schema_version_key, db_schema_version); | 304 PutInt(transaction.get(), schema_version_key, db_schema_version); |
305 db_data_version = WebKit::kSerializedScriptValueVersion; | 305 db_data_version = blink::kSerializedScriptValueVersion; |
306 PutInt(transaction.get(), data_version_key, db_data_version); | 306 PutInt(transaction.get(), data_version_key, db_data_version); |
307 } | 307 } |
308 } | 308 } |
309 | 309 |
310 // All new values will be written using this serialization version. | 310 // All new values will be written using this serialization version. |
311 found = false; | 311 found = false; |
312 ok = GetInt(transaction.get(), data_version_key, &db_data_version, &found); | 312 ok = GetInt(transaction.get(), data_version_key, &db_data_version, &found); |
313 if (!ok) { | 313 if (!ok) { |
314 INTERNAL_READ_ERROR(SET_UP_METADATA); | 314 INTERNAL_READ_ERROR(SET_UP_METADATA); |
315 return false; | 315 return false; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 INDEXED_DB_BACKING_STORE_OPEN_DISK_FULL_DEPRECATED, | 417 INDEXED_DB_BACKING_STORE_OPEN_DISK_FULL_DEPRECATED, |
418 INDEXED_DB_BACKING_STORE_OPEN_ORIGIN_TOO_LONG, | 418 INDEXED_DB_BACKING_STORE_OPEN_ORIGIN_TOO_LONG, |
419 INDEXED_DB_BACKING_STORE_OPEN_NO_RECOVERY, | 419 INDEXED_DB_BACKING_STORE_OPEN_NO_RECOVERY, |
420 INDEXED_DB_BACKING_STORE_OPEN_MAX, | 420 INDEXED_DB_BACKING_STORE_OPEN_MAX, |
421 }; | 421 }; |
422 | 422 |
423 // static | 423 // static |
424 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( | 424 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( |
425 const GURL& origin_url, | 425 const GURL& origin_url, |
426 const base::FilePath& path_base, | 426 const base::FilePath& path_base, |
427 WebKit::WebIDBCallbacks::DataLoss* data_loss, | 427 blink::WebIDBCallbacks::DataLoss* data_loss, |
428 std::string* data_loss_message, | 428 std::string* data_loss_message, |
429 bool* disk_full) { | 429 bool* disk_full) { |
430 *data_loss = WebKit::WebIDBCallbacks::DataLossNone; | 430 *data_loss = blink::WebIDBCallbacks::DataLossNone; |
431 DefaultLevelDBFactory leveldb_factory; | 431 DefaultLevelDBFactory leveldb_factory; |
432 return IndexedDBBackingStore::Open(origin_url, | 432 return IndexedDBBackingStore::Open(origin_url, |
433 path_base, | 433 path_base, |
434 data_loss, | 434 data_loss, |
435 data_loss_message, | 435 data_loss_message, |
436 disk_full, | 436 disk_full, |
437 &leveldb_factory); | 437 &leveldb_factory); |
438 } | 438 } |
439 | 439 |
440 static void HistogramOpenStatus(IndexedDBBackingStoreOpenResult result) { | 440 static void HistogramOpenStatus(IndexedDBBackingStoreOpenResult result) { |
(...skipping 29 matching lines...) Expand all Loading... |
470 num_buckets); | 470 num_buckets); |
471 return true; | 471 return true; |
472 } | 472 } |
473 return false; | 473 return false; |
474 } | 474 } |
475 | 475 |
476 // static | 476 // static |
477 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( | 477 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( |
478 const GURL& origin_url, | 478 const GURL& origin_url, |
479 const base::FilePath& path_base, | 479 const base::FilePath& path_base, |
480 WebKit::WebIDBCallbacks::DataLoss* data_loss, | 480 blink::WebIDBCallbacks::DataLoss* data_loss, |
481 std::string* data_loss_message, | 481 std::string* data_loss_message, |
482 bool* is_disk_full, | 482 bool* is_disk_full, |
483 LevelDBFactory* leveldb_factory) { | 483 LevelDBFactory* leveldb_factory) { |
484 IDB_TRACE("IndexedDBBackingStore::Open"); | 484 IDB_TRACE("IndexedDBBackingStore::Open"); |
485 DCHECK(!path_base.empty()); | 485 DCHECK(!path_base.empty()); |
486 *data_loss = WebKit::WebIDBCallbacks::DataLossNone; | 486 *data_loss = blink::WebIDBCallbacks::DataLossNone; |
487 *data_loss_message = ""; | 487 *data_loss_message = ""; |
488 *is_disk_full = false; | 488 *is_disk_full = false; |
489 | 489 |
490 scoped_ptr<LevelDBComparator> comparator(new Comparator()); | 490 scoped_ptr<LevelDBComparator> comparator(new Comparator()); |
491 | 491 |
492 if (!IsStringASCII(path_base.AsUTF8Unsafe())) { | 492 if (!IsStringASCII(path_base.AsUTF8Unsafe())) { |
493 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII); | 493 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII); |
494 } | 494 } |
495 if (!file_util::CreateDirectory(path_base)) { | 495 if (!file_util::CreateDirectory(path_base)) { |
496 LOG(ERROR) << "Unable to create IndexedDB database path " | 496 LOG(ERROR) << "Unable to create IndexedDB database path " |
(...skipping 12 matching lines...) Expand all Loading... |
509 | 509 |
510 scoped_ptr<LevelDBDatabase> db; | 510 scoped_ptr<LevelDBDatabase> db; |
511 leveldb::Status status = leveldb_factory->OpenLevelDB( | 511 leveldb::Status status = leveldb_factory->OpenLevelDB( |
512 file_path, comparator.get(), &db, is_disk_full); | 512 file_path, comparator.get(), &db, is_disk_full); |
513 | 513 |
514 DCHECK(!db == !status.ok()); | 514 DCHECK(!db == !status.ok()); |
515 if (!status.ok()) { | 515 if (!status.ok()) { |
516 if (leveldb_env::IndicatesDiskFull(status)) { | 516 if (leveldb_env::IndicatesDiskFull(status)) { |
517 *is_disk_full = true; | 517 *is_disk_full = true; |
518 } else if (leveldb_env::IsCorruption(status)) { | 518 } else if (leveldb_env::IsCorruption(status)) { |
519 *data_loss = WebKit::WebIDBCallbacks::DataLossTotal; | 519 *data_loss = blink::WebIDBCallbacks::DataLossTotal; |
520 *data_loss_message = leveldb_env::GetCorruptionMessage(status); | 520 *data_loss_message = leveldb_env::GetCorruptionMessage(status); |
521 } | 521 } |
522 } | 522 } |
523 | 523 |
524 bool is_schema_known = false; | 524 bool is_schema_known = false; |
525 if (db) { | 525 if (db) { |
526 bool ok = IsSchemaKnown(db.get(), &is_schema_known); | 526 bool ok = IsSchemaKnown(db.get(), &is_schema_known); |
527 if (!ok) { | 527 if (!ok) { |
528 LOG(ERROR) << "IndexedDB had IO error checking schema, treating it as " | 528 LOG(ERROR) << "IndexedDB had IO error checking schema, treating it as " |
529 "failure to open"; | 529 "failure to open"; |
530 HistogramOpenStatus( | 530 HistogramOpenStatus( |
531 INDEXED_DB_BACKING_STORE_OPEN_FAILED_IO_ERROR_CHECKING_SCHEMA); | 531 INDEXED_DB_BACKING_STORE_OPEN_FAILED_IO_ERROR_CHECKING_SCHEMA); |
532 db.reset(); | 532 db.reset(); |
533 *data_loss = WebKit::WebIDBCallbacks::DataLossTotal; | 533 *data_loss = blink::WebIDBCallbacks::DataLossTotal; |
534 *data_loss_message = "I/O error checking schema"; | 534 *data_loss_message = "I/O error checking schema"; |
535 } else if (!is_schema_known) { | 535 } else if (!is_schema_known) { |
536 LOG(ERROR) << "IndexedDB backing store had unknown schema, treating it " | 536 LOG(ERROR) << "IndexedDB backing store had unknown schema, treating it " |
537 "as failure to open"; | 537 "as failure to open"; |
538 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_FAILED_UNKNOWN_SCHEMA); | 538 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_FAILED_UNKNOWN_SCHEMA); |
539 db.reset(); | 539 db.reset(); |
540 *data_loss = WebKit::WebIDBCallbacks::DataLossTotal; | 540 *data_loss = blink::WebIDBCallbacks::DataLossTotal; |
541 *data_loss_message = "Unknown schema"; | 541 *data_loss_message = "Unknown schema"; |
542 } | 542 } |
543 } | 543 } |
544 | 544 |
545 DCHECK(status.ok() || !is_schema_known || leveldb_env::IsIOError(status) || | 545 DCHECK(status.ok() || !is_schema_known || leveldb_env::IsIOError(status) || |
546 leveldb_env::IsCorruption(status)); | 546 leveldb_env::IsCorruption(status)); |
547 | 547 |
548 if (db) { | 548 if (db) { |
549 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_SUCCESS); | 549 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_SUCCESS); |
550 } else if (leveldb_env::IsIOError(status)) { | 550 } else if (leveldb_env::IsIOError(status)) { |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 { | 958 { |
959 StringPiece slice(it->Value()); | 959 StringPiece slice(it->Value()); |
960 if (!DecodeBool(&slice, &has_key_path)) | 960 if (!DecodeBool(&slice, &has_key_path)) |
961 INTERNAL_CONSISTENCY_ERROR(GET_OBJECT_STORES); | 961 INTERNAL_CONSISTENCY_ERROR(GET_OBJECT_STORES); |
962 } | 962 } |
963 // This check accounts for two layers of legacy coding: | 963 // This check accounts for two layers of legacy coding: |
964 // (1) Initially, has_key_path was added to distinguish null vs. string. | 964 // (1) Initially, has_key_path was added to distinguish null vs. string. |
965 // (2) Later, null vs. string vs. array was stored in the key_path itself. | 965 // (2) Later, null vs. string vs. array was stored in the key_path itself. |
966 // So this check is only relevant for string-type key_paths. | 966 // So this check is only relevant for string-type key_paths. |
967 if (!has_key_path && | 967 if (!has_key_path && |
968 (key_path.type() == WebKit::WebIDBKeyPathTypeString && | 968 (key_path.type() == blink::WebIDBKeyPathTypeString && |
969 !key_path.string().empty())) { | 969 !key_path.string().empty())) { |
970 INTERNAL_CONSISTENCY_ERROR(GET_OBJECT_STORES); | 970 INTERNAL_CONSISTENCY_ERROR(GET_OBJECT_STORES); |
971 break; | 971 break; |
972 } | 972 } |
973 if (!has_key_path) | 973 if (!has_key_path) |
974 key_path = IndexedDBKeyPath(); | 974 key_path = IndexedDBKeyPath(); |
975 it->Next(); | 975 it->Next(); |
976 } | 976 } |
977 | 977 |
978 int64 key_generator_current_number = -1; | 978 int64 key_generator_current_number = -1; |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1317 for (it->Seek(start_key); | 1317 for (it->Seek(start_key); |
1318 it->IsValid() && CompareKeys(it->Key(), stop_key) < 0; | 1318 it->IsValid() && CompareKeys(it->Key(), stop_key) < 0; |
1319 it->Next()) { | 1319 it->Next()) { |
1320 StringPiece slice(it->Key()); | 1320 StringPiece slice(it->Key()); |
1321 ObjectStoreDataKey data_key; | 1321 ObjectStoreDataKey data_key; |
1322 if (!ObjectStoreDataKey::Decode(&slice, &data_key)) { | 1322 if (!ObjectStoreDataKey::Decode(&slice, &data_key)) { |
1323 INTERNAL_READ_ERROR(GET_KEY_GENERATOR_CURRENT_NUMBER); | 1323 INTERNAL_READ_ERROR(GET_KEY_GENERATOR_CURRENT_NUMBER); |
1324 return false; | 1324 return false; |
1325 } | 1325 } |
1326 scoped_ptr<IndexedDBKey> user_key = data_key.user_key(); | 1326 scoped_ptr<IndexedDBKey> user_key = data_key.user_key(); |
1327 if (user_key->type() == WebKit::WebIDBKeyTypeNumber) { | 1327 if (user_key->type() == blink::WebIDBKeyTypeNumber) { |
1328 int64 n = static_cast<int64>(user_key->number()); | 1328 int64 n = static_cast<int64>(user_key->number()); |
1329 if (n > max_numeric_key) | 1329 if (n > max_numeric_key) |
1330 max_numeric_key = n; | 1330 max_numeric_key = n; |
1331 } | 1331 } |
1332 } | 1332 } |
1333 | 1333 |
1334 *key_generator_current_number = max_numeric_key + 1; | 1334 *key_generator_current_number = max_numeric_key + 1; |
1335 return true; | 1335 return true; |
1336 } | 1336 } |
1337 | 1337 |
(...skipping 1206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2544 } | 2544 } |
2545 | 2545 |
2546 void IndexedDBBackingStore::Transaction::Rollback() { | 2546 void IndexedDBBackingStore::Transaction::Rollback() { |
2547 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback"); | 2547 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback"); |
2548 DCHECK(transaction_.get()); | 2548 DCHECK(transaction_.get()); |
2549 transaction_->Rollback(); | 2549 transaction_->Rollback(); |
2550 transaction_ = NULL; | 2550 transaction_ = NULL; |
2551 } | 2551 } |
2552 | 2552 |
2553 } // namespace content | 2553 } // namespace content |
OLD | NEW |