Chromium Code Reviews| 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" |
|
michaeln
2015/06/12 20:47:57
In current sources, when chrome is run for the fir
nhiroki
2015/06/15 16:43:26
Acknowledged.
| |
| 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" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| (...skipping 50 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_MIGRATED" | |
| 78 // value: <empty> | |
| 79 // - This entry represents that the diskcache was migrated from BlockFile | |
| 80 // backend to Simple backend (http://crbug.com/487482). | |
| 81 // | |
| 82 // key: "INITDATA_OLD_DISKCACHE_EXISTS" | |
| 83 // value: <empty> | |
| 84 // - This entry represents that the diskcahes was migrate, but the old | |
| 85 // BlockFile diskcache still exists (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 kDiskCacheMigratedKey[] = "INITDATA_DISKCACHE_MIGRATED"; | |
|
michaeln
2015/06/12 20:47:58
kDiskCacheMigrationNotNeededKey = "INITDATA_DISKCA
nhiroki
2015/06/15 16:43:27
Done.
| |
| 96 const char kOldDiskCacheExistsKey[] = "INITDATA_OLD_DISKCACHE_EXISTS"; | |
|
michaeln
2015/06/12 20:47:57
INITDATA_OLD_DISKCACHE_DELETION_NOT_NEEDED
nhiroki
2015/06/15 16:43:26
Done.
| |
| 85 | 97 |
| 86 const char kRegKeyPrefix[] = "REG:"; | 98 const char kRegKeyPrefix[] = "REG:"; |
| 87 const char kRegUserDataKeyPrefix[] = "REG_USER_DATA:"; | 99 const char kRegUserDataKeyPrefix[] = "REG_USER_DATA:"; |
| 88 const char kRegHasUserDataKeyPrefix[] = "REG_HAS_USER_DATA:"; | 100 const char kRegHasUserDataKeyPrefix[] = "REG_HAS_USER_DATA:"; |
| 89 const char kRegIdToOriginKeyPrefix[] = "REGID_TO_ORIGIN:"; | 101 const char kRegIdToOriginKeyPrefix[] = "REGID_TO_ORIGIN:"; |
| 90 const char kResKeyPrefix[] = "RES:"; | 102 const char kResKeyPrefix[] = "RES:"; |
| 91 const char kKeySeparator = '\x00'; | 103 const char kKeySeparator = '\x00'; |
| 104 const char kEmptyValue[] = ""; | |
| 92 | 105 |
| 93 const char kUncommittedResIdKeyPrefix[] = "URES:"; | 106 const char kUncommittedResIdKeyPrefix[] = "URES:"; |
| 94 const char kPurgeableResIdKeyPrefix[] = "PRES:"; | 107 const char kPurgeableResIdKeyPrefix[] = "PRES:"; |
| 95 | 108 |
| 96 const int64 kCurrentSchemaVersion = 2; | 109 const int64 kCurrentSchemaVersion = 2; |
| 97 | 110 |
| 98 class ServiceWorkerEnv : public leveldb_env::ChromiumEnv { | 111 class ServiceWorkerEnv : public leveldb_env::ChromiumEnv { |
| 99 public: | 112 public: |
| 100 ServiceWorkerEnv() | 113 ServiceWorkerEnv() |
| 101 : ChromiumEnv("LevelDBEnv.ServiceWorker", false /* make_backup */) {} | 114 : ChromiumEnv("LevelDBEnv.ServiceWorker", false /* make_backup */) {} |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 if (status != STATUS_OK) | 425 if (status != STATUS_OK) |
| 413 return status; | 426 return status; |
| 414 | 427 |
| 415 *next_avail_registration_id = next_avail_registration_id_; | 428 *next_avail_registration_id = next_avail_registration_id_; |
| 416 *next_avail_version_id = next_avail_version_id_; | 429 *next_avail_version_id = next_avail_version_id_; |
| 417 *next_avail_resource_id = next_avail_resource_id_; | 430 *next_avail_resource_id = next_avail_resource_id_; |
| 418 return STATUS_OK; | 431 return STATUS_OK; |
| 419 } | 432 } |
| 420 | 433 |
| 421 ServiceWorkerDatabase::Status | 434 ServiceWorkerDatabase::Status |
| 435 ServiceWorkerDatabase::ReadDiskCacheMigrationState( | |
|
michaeln
2015/06/12 20:47:58
The simple ReadX and WriteX names are misleading.
nhiroki
2015/06/15 16:43:27
Thank you for the detailed explanation. This looks
| |
| 436 DiskCacheMigrationState* state) { | |
| 437 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
| 438 | |
| 439 Status status = LazyOpen(false); | |
| 440 if (IsNewOrNonexistentDatabase(status)) { | |
| 441 *state = DISKCACHE_NOT_USED; | |
| 442 return STATUS_OK; | |
| 443 } | |
| 444 if (status != STATUS_OK) | |
| 445 return status; | |
| 446 | |
| 447 std::string value; | |
| 448 status = LevelDBStatusToStatus( | |
| 449 db_->Get(leveldb::ReadOptions(), kDiskCacheMigratedKey, &value)); | |
| 450 if (status != STATUS_ERROR_NOT_FOUND) { | |
| 451 if (status == STATUS_OK) | |
| 452 *state = DISKCACHE_MIGRATED; | |
| 453 HandleReadResult(FROM_HERE, status); | |
| 454 return status; | |
| 455 } | |
| 456 HandleReadResult(FROM_HERE, STATUS_OK); | |
| 457 | |
| 458 status = LevelDBStatusToStatus( | |
| 459 db_->Get(leveldb::ReadOptions(), kOldDiskCacheExistsKey, &value)); | |
| 460 if (status != STATUS_ERROR_NOT_FOUND) { | |
| 461 if (status == STATUS_OK) | |
| 462 *state = DISKCACHE_NEEDS_TO_DELETE_OLD; | |
| 463 HandleReadResult(FROM_HERE, status); | |
| 464 return status; | |
| 465 } | |
| 466 | |
| 467 *state = DISKCACHE_NEEDS_TO_MIGRATE; | |
| 468 HandleReadResult(FROM_HERE, STATUS_OK); | |
| 469 return STATUS_OK; | |
| 470 } | |
| 471 | |
| 472 ServiceWorkerDatabase::Status | |
| 473 ServiceWorkerDatabase::WriteDiskCacheMigrationState( | |
| 474 DiskCacheMigrationState state) { | |
| 475 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
| 476 DCHECK(state == DISKCACHE_MIGRATED || state == DISKCACHE_NEEDS_TO_DELETE_OLD) | |
| 477 << state; | |
| 478 | |
| 479 Status status = LazyOpen(true); | |
| 480 if (status != STATUS_OK) | |
| 481 return status; | |
| 482 | |
| 483 leveldb::WriteBatch batch; | |
| 484 if (state == DISKCACHE_MIGRATED) { | |
| 485 batch.Put(kDiskCacheMigratedKey, kEmptyValue); | |
| 486 batch.Delete(kOldDiskCacheExistsKey); | |
| 487 } else if (state == DISKCACHE_NEEDS_TO_DELETE_OLD) { | |
| 488 batch.Put(kOldDiskCacheExistsKey, kEmptyValue); | |
| 489 } | |
| 490 return WriteBatch(&batch); | |
| 491 } | |
| 492 | |
| 493 ServiceWorkerDatabase::Status | |
| 422 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { | 494 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { |
| 423 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 495 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 424 DCHECK(origins->empty()); | 496 DCHECK(origins->empty()); |
| 425 | 497 |
| 426 Status status = LazyOpen(false); | 498 Status status = LazyOpen(false); |
| 427 if (IsNewOrNonexistentDatabase(status)) | 499 if (IsNewOrNonexistentDatabase(status)) |
| 428 return STATUS_OK; | 500 return STATUS_OK; |
| 429 if (status != STATUS_OK) | 501 if (status != STATUS_OK) |
| 430 return status; | 502 return status; |
| 431 | 503 |
| (...skipping 988 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1420 } | 1492 } |
| 1421 | 1493 |
| 1422 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteBatch( | 1494 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteBatch( |
| 1423 leveldb::WriteBatch* batch) { | 1495 leveldb::WriteBatch* batch) { |
| 1424 DCHECK(batch); | 1496 DCHECK(batch); |
| 1425 DCHECK_NE(DISABLED, state_); | 1497 DCHECK_NE(DISABLED, state_); |
| 1426 | 1498 |
| 1427 if (state_ == UNINITIALIZED) { | 1499 if (state_ == UNINITIALIZED) { |
| 1428 // Write the database schema version. | 1500 // Write the database schema version. |
| 1429 batch->Put(kDatabaseVersionKey, base::Int64ToString(kCurrentSchemaVersion)); | 1501 batch->Put(kDatabaseVersionKey, base::Int64ToString(kCurrentSchemaVersion)); |
| 1430 state_ = INITIALIZED; | 1502 state_ = INITIALIZED; |
|
michaeln
2015/06/12 20:47:58
we're initializing a brand new db, put the default
nhiroki
2015/06/15 16:43:26
Ah..., yes. This is the most suitable place to set
| |
| 1431 } | 1503 } |
| 1432 | 1504 |
| 1433 Status status = LevelDBStatusToStatus( | 1505 Status status = LevelDBStatusToStatus( |
| 1434 db_->Write(leveldb::WriteOptions(), batch)); | 1506 db_->Write(leveldb::WriteOptions(), batch)); |
| 1435 HandleWriteResult(FROM_HERE, status); | 1507 HandleWriteResult(FROM_HERE, status); |
| 1436 return status; | 1508 return status; |
| 1437 } | 1509 } |
| 1438 | 1510 |
| 1439 void ServiceWorkerDatabase::BumpNextRegistrationIdIfNeeded( | 1511 void ServiceWorkerDatabase::BumpNextRegistrationIdIfNeeded( |
| 1440 int64 used_id, leveldb::WriteBatch* batch) { | 1512 int64 used_id, leveldb::WriteBatch* batch) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1497 | 1569 |
| 1498 void ServiceWorkerDatabase::HandleWriteResult( | 1570 void ServiceWorkerDatabase::HandleWriteResult( |
| 1499 const tracked_objects::Location& from_here, | 1571 const tracked_objects::Location& from_here, |
| 1500 Status status) { | 1572 Status status) { |
| 1501 if (status != STATUS_OK) | 1573 if (status != STATUS_OK) |
| 1502 Disable(from_here, status); | 1574 Disable(from_here, status); |
| 1503 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1575 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
| 1504 } | 1576 } |
| 1505 | 1577 |
| 1506 } // namespace content | 1578 } // namespace content |
| OLD | NEW |