| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/stl_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 16 #include "content/browser/service_worker/service_worker_database.pb.h" | 17 #include "content/browser/service_worker/service_worker_database.pb.h" |
| 17 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 18 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
| 18 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 19 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 19 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 20 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
| 20 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 21 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 21 | 22 |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 std::vector<int64>* newly_purgeable_resources) { | 417 std::vector<int64>* newly_purgeable_resources) { |
| 417 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 418 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 418 Status status = LazyOpen(true); | 419 Status status = LazyOpen(true); |
| 419 if (status != STATUS_OK) | 420 if (status != STATUS_OK) |
| 420 return status; | 421 return status; |
| 421 | 422 |
| 422 leveldb::WriteBatch batch; | 423 leveldb::WriteBatch batch; |
| 423 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch); | 424 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch); |
| 424 BumpNextVersionIdIfNeeded(registration.version_id, &batch); | 425 BumpNextVersionIdIfNeeded(registration.version_id, &batch); |
| 425 | 426 |
| 426 // TODO(nhiroki): Skip to add the origin into the unique origin list if it | |
| 427 // has already been added. | |
| 428 PutUniqueOriginToBatch(registration.scope.GetOrigin(), &batch); | 427 PutUniqueOriginToBatch(registration.scope.GetOrigin(), &batch); |
| 429 | |
| 430 PutRegistrationDataToBatch(registration, &batch); | 428 PutRegistrationDataToBatch(registration, &batch); |
| 431 | 429 |
| 432 // Retrieve a previous version to sweep purgeable resources. | |
| 433 RegistrationData old_registration; | |
| 434 status = ReadRegistrationData(registration.registration_id, | |
| 435 registration.scope.GetOrigin(), | |
| 436 &old_registration); | |
| 437 if (status != STATUS_OK && status != STATUS_ERROR_NOT_FOUND) | |
| 438 return status; | |
| 439 if (status == STATUS_OK) { | |
| 440 DCHECK_LT(old_registration.version_id, registration.version_id); | |
| 441 // Currently resource sharing across versions and registrations is not | |
| 442 // suppported, so resource ids should not be overlapped between | |
| 443 // |registration| and |old_registration|. | |
| 444 // TODO(nhiroki): Add DCHECK to make sure the overlap does not exist. | |
| 445 status = DeleteResourceRecords( | |
| 446 old_registration.version_id, newly_purgeable_resources, &batch); | |
| 447 if (status != STATUS_OK) | |
| 448 return status; | |
| 449 } | |
| 450 | |
| 451 // Used for avoiding multiple writes for the same resource id or url. | 430 // Used for avoiding multiple writes for the same resource id or url. |
| 452 std::set<int64> pushed_resources; | 431 std::set<int64> pushed_resources; |
| 453 std::set<GURL> pushed_urls; | 432 std::set<GURL> pushed_urls; |
| 454 for (std::vector<ResourceRecord>::const_iterator itr = resources.begin(); | 433 for (std::vector<ResourceRecord>::const_iterator itr = resources.begin(); |
| 455 itr != resources.end(); ++itr) { | 434 itr != resources.end(); ++itr) { |
| 456 if (!itr->url.is_valid()) | 435 if (!itr->url.is_valid()) |
| 457 return STATUS_ERROR_FAILED; | 436 return STATUS_ERROR_FAILED; |
| 458 | 437 |
| 459 // Duplicated resource id or url should not exist. | 438 // Duplicated resource id or url should not exist. |
| 460 DCHECK(pushed_resources.insert(itr->resource_id).second); | 439 DCHECK(pushed_resources.insert(itr->resource_id).second); |
| 461 DCHECK(pushed_urls.insert(itr->url).second); | 440 DCHECK(pushed_urls.insert(itr->url).second); |
| 462 | 441 |
| 463 PutResourceRecordToBatch(*itr, registration.version_id, &batch); | 442 PutResourceRecordToBatch(*itr, registration.version_id, &batch); |
| 464 | 443 |
| 465 // Delete a resource from the uncommitted list. | 444 // Delete a resource from the uncommitted list. |
| 466 batch.Delete(CreateResourceIdKey( | 445 batch.Delete(CreateResourceIdKey( |
| 467 kUncommittedResIdKeyPrefix, itr->resource_id)); | 446 kUncommittedResIdKeyPrefix, itr->resource_id)); |
| 468 } | 447 } |
| 469 | 448 |
| 449 // Retrieve a previous version to sweep purgeable resources. |
| 450 RegistrationData old_registration; |
| 451 status = ReadRegistrationData(registration.registration_id, |
| 452 registration.scope.GetOrigin(), |
| 453 &old_registration); |
| 454 if (status != STATUS_OK && status != STATUS_ERROR_NOT_FOUND) |
| 455 return status; |
| 456 if (status == STATUS_OK) { |
| 457 DCHECK_LT(old_registration.version_id, registration.version_id); |
| 458 status = DeleteResourceRecords( |
| 459 old_registration.version_id, newly_purgeable_resources, &batch); |
| 460 if (status != STATUS_OK) |
| 461 return status; |
| 462 |
| 463 // Currently resource sharing across versions and registrations is not |
| 464 // supported, so resource ids should not be overlapped between |
| 465 // |registration| and |old_registration|. |
| 466 std::set<int64> deleted_resources(newly_purgeable_resources->begin(), |
| 467 newly_purgeable_resources->end()); |
| 468 DCHECK(base::STLSetIntersection<std::set<int64> >( |
| 469 pushed_resources, deleted_resources).empty()); |
| 470 } |
| 471 |
| 470 return WriteBatch(&batch); | 472 return WriteBatch(&batch); |
| 471 } | 473 } |
| 472 | 474 |
| 473 ServiceWorkerDatabase::Status ServiceWorkerDatabase::UpdateVersionToActive( | 475 ServiceWorkerDatabase::Status ServiceWorkerDatabase::UpdateVersionToActive( |
| 474 int64 registration_id, | 476 int64 registration_id, |
| 475 const GURL& origin) { | 477 const GURL& origin) { |
| 476 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 478 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 477 ServiceWorkerDatabase::Status status = LazyOpen(false); | 479 ServiceWorkerDatabase::Status status = LazyOpen(false); |
| 478 if (IsNewOrNonexistentDatabase(status)) | 480 if (IsNewOrNonexistentDatabase(status)) |
| 479 return STATUS_ERROR_NOT_FOUND; | 481 return STATUS_ERROR_NOT_FOUND; |
| (...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 965 // TODO(nhiroki): Add an UMA histogram. | 967 // TODO(nhiroki): Add an UMA histogram. |
| 966 DLOG(ERROR) << "Failed at: " << from_here.ToString() | 968 DLOG(ERROR) << "Failed at: " << from_here.ToString() |
| 967 << " with error: " << status.ToString(); | 969 << " with error: " << status.ToString(); |
| 968 is_disabled_ = true; | 970 is_disabled_ = true; |
| 969 if (status.IsCorruption()) | 971 if (status.IsCorruption()) |
| 970 was_corruption_detected_ = true; | 972 was_corruption_detected_ = true; |
| 971 db_.reset(); | 973 db_.reset(); |
| 972 } | 974 } |
| 973 | 975 |
| 974 } // namespace content | 976 } // namespace content |
| OLD | NEW |