| 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/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" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "content/browser/service_worker/service_worker_database.pb.h" | 17 #include "content/browser/service_worker/service_worker_database.pb.h" |
| 18 #include "content/common/service_worker/service_worker_types.h" |
| 18 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 19 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
| 19 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 20 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 20 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 21 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
| 21 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 22 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 22 | 23 |
| 23 // LevelDB database schema | 24 // LevelDB database schema |
| 24 // ======================= | 25 // ======================= |
| 25 // | 26 // |
| 26 // NOTE | 27 // NOTE |
| 27 // - int64 value is serialized as a string by base::Int64ToString(). | 28 // - int64 value is serialized as a string by base::Int64ToString(). |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 return ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND; | 221 return ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND; |
| 221 else if (status.IsCorruption()) | 222 else if (status.IsCorruption()) |
| 222 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; | 223 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; |
| 223 else | 224 else |
| 224 return ServiceWorkerDatabase::STATUS_ERROR_FAILED; | 225 return ServiceWorkerDatabase::STATUS_ERROR_FAILED; |
| 225 } | 226 } |
| 226 | 227 |
| 227 } // namespace | 228 } // namespace |
| 228 | 229 |
| 229 ServiceWorkerDatabase::RegistrationData::RegistrationData() | 230 ServiceWorkerDatabase::RegistrationData::RegistrationData() |
| 230 : registration_id(-1), | 231 : registration_id(kInvalidServiceWorkerRegistrationId), |
| 231 version_id(-1), | 232 version_id(kInvalidServiceWorkerVersionId), |
| 232 is_active(false), | 233 is_active(false), |
| 233 has_fetch_handler(false) { | 234 has_fetch_handler(false) { |
| 234 } | 235 } |
| 235 | 236 |
| 236 ServiceWorkerDatabase::RegistrationData::~RegistrationData() { | 237 ServiceWorkerDatabase::RegistrationData::~RegistrationData() { |
| 237 } | 238 } |
| 238 | 239 |
| 239 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) | 240 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) |
| 240 : path_(path), | 241 : path_(path), |
| 241 next_avail_registration_id_(0), | 242 next_avail_registration_id_(0), |
| (...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteResourceIdsInBatch( | 872 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteResourceIdsInBatch( |
| 872 const char* id_key_prefix, | 873 const char* id_key_prefix, |
| 873 const std::set<int64>& ids, | 874 const std::set<int64>& ids, |
| 874 leveldb::WriteBatch* batch) { | 875 leveldb::WriteBatch* batch) { |
| 875 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 876 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 876 DCHECK(id_key_prefix); | 877 DCHECK(id_key_prefix); |
| 877 | 878 |
| 878 Status status = LazyOpen(true); | 879 Status status = LazyOpen(true); |
| 879 if (status != STATUS_OK) | 880 if (status != STATUS_OK) |
| 880 return status; | 881 return status; |
| 881 if (ids.empty()) | |
| 882 return STATUS_OK; | |
| 883 | 882 |
| 884 for (std::set<int64>::const_iterator itr = ids.begin(); | 883 for (std::set<int64>::const_iterator itr = ids.begin(); |
| 885 itr != ids.end(); ++itr) { | 884 itr != ids.end(); ++itr) { |
| 886 // Value should be empty. | 885 // Value should be empty. |
| 887 batch->Put(CreateResourceIdKey(id_key_prefix, *itr), ""); | 886 batch->Put(CreateResourceIdKey(id_key_prefix, *itr), ""); |
| 888 } | 887 } |
| 889 return STATUS_OK; | 888 return STATUS_OK; |
| 890 } | 889 } |
| 891 | 890 |
| 892 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteResourceIds( | 891 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteResourceIds( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 904 const std::set<int64>& ids, | 903 const std::set<int64>& ids, |
| 905 leveldb::WriteBatch* batch) { | 904 leveldb::WriteBatch* batch) { |
| 906 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 905 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 907 DCHECK(id_key_prefix); | 906 DCHECK(id_key_prefix); |
| 908 | 907 |
| 909 Status status = LazyOpen(false); | 908 Status status = LazyOpen(false); |
| 910 if (IsNewOrNonexistentDatabase(status)) | 909 if (IsNewOrNonexistentDatabase(status)) |
| 911 return STATUS_OK; | 910 return STATUS_OK; |
| 912 if (status != STATUS_OK) | 911 if (status != STATUS_OK) |
| 913 return status; | 912 return status; |
| 914 if (ids.empty()) | |
| 915 return STATUS_OK; | |
| 916 | 913 |
| 917 for (std::set<int64>::const_iterator itr = ids.begin(); | 914 for (std::set<int64>::const_iterator itr = ids.begin(); |
| 918 itr != ids.end(); ++itr) { | 915 itr != ids.end(); ++itr) { |
| 919 batch->Delete(CreateResourceIdKey(id_key_prefix, *itr)); | 916 batch->Delete(CreateResourceIdKey(id_key_prefix, *itr)); |
| 920 } | 917 } |
| 921 return STATUS_OK; | 918 return STATUS_OK; |
| 922 } | 919 } |
| 923 | 920 |
| 924 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadDatabaseVersion( | 921 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadDatabaseVersion( |
| 925 int64* db_version) { | 922 int64* db_version) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 995 const tracked_objects::Location& from_here, | 992 const tracked_objects::Location& from_here, |
| 996 const leveldb::Status& status) { | 993 const leveldb::Status& status) { |
| 997 // TODO(nhiroki): Add an UMA histogram. | 994 // TODO(nhiroki): Add an UMA histogram. |
| 998 DLOG(ERROR) << "Failed at: " << from_here.ToString() | 995 DLOG(ERROR) << "Failed at: " << from_here.ToString() |
| 999 << " with error: " << status.ToString(); | 996 << " with error: " << status.ToString(); |
| 1000 state_ = DISABLED; | 997 state_ = DISABLED; |
| 1001 db_.reset(); | 998 db_.reset(); |
| 1002 } | 999 } |
| 1003 | 1000 |
| 1004 } // namespace content | 1001 } // namespace content |
| OLD | NEW |