| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/service_worker/service_worker_database.h" | 5 #include "content/browser/service_worker/service_worker_database.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 // (ex. "RES:123456\x00654321") | 66 // (ex. "RES:123456\x00654321") |
| 67 // value: <ServiceWorkerResourceRecord serialized as a string> | 67 // value: <ServiceWorkerResourceRecord serialized as a string> |
| 68 // | 68 // |
| 69 // key: "URES:" + <int64 'uncommitted_resource_id'> | 69 // key: "URES:" + <int64 'uncommitted_resource_id'> |
| 70 // value: <empty> | 70 // value: <empty> |
| 71 // | 71 // |
| 72 // Version 2 | 72 // Version 2 |
| 73 // | 73 // |
| 74 // key: "REGID_TO_ORIGIN:" + <int64 'registration_id'> | 74 // key: "REGID_TO_ORIGIN:" + <int64 'registration_id'> |
| 75 // value: <GURL 'origin'> | 75 // value: <GURL 'origin'> |
| 76 // |
| 77 // key: "INITDATA_DISKCACHE_MIGRATION_NOT_NEEDED" |
| 78 // value: <empty> |
| 79 // - This entry represents that the diskcache uses the Simple backend and |
| 80 // does not have to do diskcache migration (http://crbug.com/487482). |
| 81 // |
| 82 // key: "INITDATA_OLD_DISKCACHE_DELETION_NOT_NEEDED" |
| 83 // value: <empty> |
| 84 // - This entry represents that the old BlockFile diskcache was deleted |
| 85 // after diskcache migration (http://crbug.com/487482). |
| 76 namespace content { | 86 namespace content { |
| 77 | 87 |
| 78 namespace { | 88 namespace { |
| 79 | 89 |
| 80 const char kDatabaseVersionKey[] = "INITDATA_DB_VERSION"; | 90 const char kDatabaseVersionKey[] = "INITDATA_DB_VERSION"; |
| 81 const char kNextRegIdKey[] = "INITDATA_NEXT_REGISTRATION_ID"; | 91 const char kNextRegIdKey[] = "INITDATA_NEXT_REGISTRATION_ID"; |
| 82 const char kNextResIdKey[] = "INITDATA_NEXT_RESOURCE_ID"; | 92 const char kNextResIdKey[] = "INITDATA_NEXT_RESOURCE_ID"; |
| 83 const char kNextVerIdKey[] = "INITDATA_NEXT_VERSION_ID"; | 93 const char kNextVerIdKey[] = "INITDATA_NEXT_VERSION_ID"; |
| 84 const char kUniqueOriginKey[] = "INITDATA_UNIQUE_ORIGIN:"; | 94 const char kUniqueOriginKey[] = "INITDATA_UNIQUE_ORIGIN:"; |
| 95 const char kDiskCacheMigrationNotNeededKey[] = |
| 96 "INITDATA_DISKCACHE_MIGRATION_NOT_NEEDED"; |
| 97 const char kOldDiskCacheDeletionNotNeededKey[] = |
| 98 "INITDATA_OLD_DISKCACHE_DELETION_NOT_NEEDED"; |
| 85 | 99 |
| 86 const char kRegKeyPrefix[] = "REG:"; | 100 const char kRegKeyPrefix[] = "REG:"; |
| 87 const char kRegUserDataKeyPrefix[] = "REG_USER_DATA:"; | 101 const char kRegUserDataKeyPrefix[] = "REG_USER_DATA:"; |
| 88 const char kRegHasUserDataKeyPrefix[] = "REG_HAS_USER_DATA:"; | 102 const char kRegHasUserDataKeyPrefix[] = "REG_HAS_USER_DATA:"; |
| 89 const char kRegIdToOriginKeyPrefix[] = "REGID_TO_ORIGIN:"; | 103 const char kRegIdToOriginKeyPrefix[] = "REGID_TO_ORIGIN:"; |
| 90 const char kResKeyPrefix[] = "RES:"; | 104 const char kResKeyPrefix[] = "RES:"; |
| 91 const char kKeySeparator = '\x00'; | 105 const char kKeySeparator = '\x00'; |
| 106 const char kEmptyValue[] = ""; |
| 92 | 107 |
| 93 const char kUncommittedResIdKeyPrefix[] = "URES:"; | 108 const char kUncommittedResIdKeyPrefix[] = "URES:"; |
| 94 const char kPurgeableResIdKeyPrefix[] = "PRES:"; | 109 const char kPurgeableResIdKeyPrefix[] = "PRES:"; |
| 95 | 110 |
| 96 const int64 kCurrentSchemaVersion = 2; | 111 const int64 kCurrentSchemaVersion = 2; |
| 97 | 112 |
| 98 class ServiceWorkerEnv : public leveldb_env::ChromiumEnv { | 113 class ServiceWorkerEnv : public leveldb_env::ChromiumEnv { |
| 99 public: | 114 public: |
| 100 ServiceWorkerEnv() | 115 ServiceWorkerEnv() |
| 101 : ChromiumEnv("LevelDBEnv.ServiceWorker", false /* make_backup */) {} | 116 : ChromiumEnv("LevelDBEnv.ServiceWorker", false /* make_backup */) {} |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 } | 382 } |
| 368 | 383 |
| 369 ServiceWorkerDatabase::RegistrationData::~RegistrationData() { | 384 ServiceWorkerDatabase::RegistrationData::~RegistrationData() { |
| 370 } | 385 } |
| 371 | 386 |
| 372 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) | 387 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) |
| 373 : path_(path), | 388 : path_(path), |
| 374 next_avail_registration_id_(0), | 389 next_avail_registration_id_(0), |
| 375 next_avail_resource_id_(0), | 390 next_avail_resource_id_(0), |
| 376 next_avail_version_id_(0), | 391 next_avail_version_id_(0), |
| 377 state_(UNINITIALIZED) { | 392 state_(UNINITIALIZED), |
| 393 skip_writing_diskcache_migration_state_on_init_(false) { |
| 378 sequence_checker_.DetachFromSequence(); | 394 sequence_checker_.DetachFromSequence(); |
| 379 } | 395 } |
| 380 | 396 |
| 381 ServiceWorkerDatabase::~ServiceWorkerDatabase() { | 397 ServiceWorkerDatabase::~ServiceWorkerDatabase() { |
| 382 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 398 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 383 db_.reset(); | 399 db_.reset(); |
| 384 } | 400 } |
| 385 | 401 |
| 386 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetNextAvailableIds( | 402 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetNextAvailableIds( |
| 387 int64* next_avail_registration_id, | 403 int64* next_avail_registration_id, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 411 status = ReadNextAvailableId(kNextResIdKey, &next_avail_resource_id_); | 427 status = ReadNextAvailableId(kNextResIdKey, &next_avail_resource_id_); |
| 412 if (status != STATUS_OK) | 428 if (status != STATUS_OK) |
| 413 return status; | 429 return status; |
| 414 | 430 |
| 415 *next_avail_registration_id = next_avail_registration_id_; | 431 *next_avail_registration_id = next_avail_registration_id_; |
| 416 *next_avail_version_id = next_avail_version_id_; | 432 *next_avail_version_id = next_avail_version_id_; |
| 417 *next_avail_resource_id = next_avail_resource_id_; | 433 *next_avail_resource_id = next_avail_resource_id_; |
| 418 return STATUS_OK; | 434 return STATUS_OK; |
| 419 } | 435 } |
| 420 | 436 |
| 437 ServiceWorkerDatabase::Status ServiceWorkerDatabase::IsDiskCacheMigrationNeeded( |
| 438 bool* migration_needed) { |
| 439 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 440 |
| 441 Status status = LazyOpen(false); |
| 442 if (IsNewOrNonexistentDatabase(status)) { |
| 443 *migration_needed = false; |
| 444 return STATUS_OK; |
| 445 } |
| 446 if (status != STATUS_OK) |
| 447 return status; |
| 448 |
| 449 std::string value; |
| 450 status = LevelDBStatusToStatus(db_->Get( |
| 451 leveldb::ReadOptions(), kDiskCacheMigrationNotNeededKey, &value)); |
| 452 if (status == STATUS_ERROR_NOT_FOUND) { |
| 453 *migration_needed = true; |
| 454 HandleReadResult(FROM_HERE, STATUS_OK); |
| 455 return STATUS_OK; |
| 456 } |
| 457 if (status != STATUS_OK) { |
| 458 HandleReadResult(FROM_HERE, status); |
| 459 return status; |
| 460 } |
| 461 |
| 462 *migration_needed = false; |
| 463 HandleReadResult(FROM_HERE, status); |
| 464 return status; |
| 465 } |
| 466 |
| 467 ServiceWorkerDatabase::Status |
| 468 ServiceWorkerDatabase::SetDiskCacheMigrationNotNeeded() { |
| 469 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 470 |
| 471 Status status = LazyOpen(true); |
| 472 if (status != STATUS_OK) |
| 473 return status; |
| 474 |
| 475 leveldb::WriteBatch batch; |
| 476 batch.Put(kDiskCacheMigrationNotNeededKey, kEmptyValue); |
| 477 return WriteBatch(&batch); |
| 478 } |
| 479 |
| 480 ServiceWorkerDatabase::Status |
| 481 ServiceWorkerDatabase::IsOldDiskCacheDeletionNeeded(bool* deletion_needed) { |
| 482 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 483 |
| 484 Status status = LazyOpen(false); |
| 485 if (IsNewOrNonexistentDatabase(status)) { |
| 486 *deletion_needed = false; |
| 487 return STATUS_OK; |
| 488 } |
| 489 if (status != STATUS_OK) |
| 490 return status; |
| 491 |
| 492 std::string value; |
| 493 status = LevelDBStatusToStatus(db_->Get( |
| 494 leveldb::ReadOptions(), kOldDiskCacheDeletionNotNeededKey, &value)); |
| 495 if (status == STATUS_ERROR_NOT_FOUND) { |
| 496 *deletion_needed = true; |
| 497 HandleReadResult(FROM_HERE, STATUS_OK); |
| 498 return STATUS_OK; |
| 499 } |
| 500 if (status != STATUS_OK) { |
| 501 HandleReadResult(FROM_HERE, status); |
| 502 return status; |
| 503 } |
| 504 |
| 505 *deletion_needed = false; |
| 506 HandleReadResult(FROM_HERE, status); |
| 507 return status; |
| 508 } |
| 509 |
| 510 ServiceWorkerDatabase::Status |
| 511 ServiceWorkerDatabase::SetOldDiskCacheDeletionNotNeeded() { |
| 512 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 513 |
| 514 Status status = LazyOpen(true); |
| 515 if (status != STATUS_OK) |
| 516 return status; |
| 517 |
| 518 leveldb::WriteBatch batch; |
| 519 batch.Put(kOldDiskCacheDeletionNotNeededKey, kEmptyValue); |
| 520 return WriteBatch(&batch); |
| 521 } |
| 522 |
| 421 ServiceWorkerDatabase::Status | 523 ServiceWorkerDatabase::Status |
| 422 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { | 524 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { |
| 423 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 525 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 424 DCHECK(origins->empty()); | 526 DCHECK(origins->empty()); |
| 425 | 527 |
| 426 Status status = LazyOpen(false); | 528 Status status = LazyOpen(false); |
| 427 if (IsNewOrNonexistentDatabase(status)) | 529 if (IsNewOrNonexistentDatabase(status)) |
| 428 return STATUS_OK; | 530 return STATUS_OK; |
| 429 if (status != STATUS_OK) | 531 if (status != STATUS_OK) |
| 430 return status; | 532 return status; |
| (...skipping 987 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1418 HandleReadResult(FROM_HERE, status); | 1520 HandleReadResult(FROM_HERE, status); |
| 1419 return status; | 1521 return status; |
| 1420 } | 1522 } |
| 1421 | 1523 |
| 1422 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteBatch( | 1524 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteBatch( |
| 1423 leveldb::WriteBatch* batch) { | 1525 leveldb::WriteBatch* batch) { |
| 1424 DCHECK(batch); | 1526 DCHECK(batch); |
| 1425 DCHECK_NE(DISABLED, state_); | 1527 DCHECK_NE(DISABLED, state_); |
| 1426 | 1528 |
| 1427 if (state_ == UNINITIALIZED) { | 1529 if (state_ == UNINITIALIZED) { |
| 1428 // Write the database schema version. | 1530 // Write database default values. |
| 1429 batch->Put(kDatabaseVersionKey, base::Int64ToString(kCurrentSchemaVersion)); | 1531 batch->Put(kDatabaseVersionKey, base::Int64ToString(kCurrentSchemaVersion)); |
| 1532 if (!skip_writing_diskcache_migration_state_on_init_) { |
| 1533 batch->Put(kDiskCacheMigrationNotNeededKey, kEmptyValue); |
| 1534 batch->Put(kOldDiskCacheDeletionNotNeededKey, kEmptyValue); |
| 1535 } |
| 1430 state_ = INITIALIZED; | 1536 state_ = INITIALIZED; |
| 1431 } | 1537 } |
| 1432 | 1538 |
| 1433 Status status = LevelDBStatusToStatus( | 1539 Status status = LevelDBStatusToStatus( |
| 1434 db_->Write(leveldb::WriteOptions(), batch)); | 1540 db_->Write(leveldb::WriteOptions(), batch)); |
| 1435 HandleWriteResult(FROM_HERE, status); | 1541 HandleWriteResult(FROM_HERE, status); |
| 1436 return status; | 1542 return status; |
| 1437 } | 1543 } |
| 1438 | 1544 |
| 1439 void ServiceWorkerDatabase::BumpNextRegistrationIdIfNeeded( | 1545 void ServiceWorkerDatabase::BumpNextRegistrationIdIfNeeded( |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1497 | 1603 |
| 1498 void ServiceWorkerDatabase::HandleWriteResult( | 1604 void ServiceWorkerDatabase::HandleWriteResult( |
| 1499 const tracked_objects::Location& from_here, | 1605 const tracked_objects::Location& from_here, |
| 1500 Status status) { | 1606 Status status) { |
| 1501 if (status != STATUS_OK) | 1607 if (status != STATUS_OK) |
| 1502 Disable(from_here, status); | 1608 Disable(from_here, status); |
| 1503 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1609 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
| 1504 } | 1610 } |
| 1505 | 1611 |
| 1506 } // namespace content | 1612 } // namespace content |
| OLD | NEW |