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" |
| 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/metrics/histogram.h" | |
| 12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 17 #include "content/browser/service_worker/service_worker_database.pb.h" | 18 #include "content/browser/service_worker/service_worker_database.pb.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" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 | 70 |
| 70 const char kRegKeyPrefix[] = "REG:"; | 71 const char kRegKeyPrefix[] = "REG:"; |
| 71 const char kResKeyPrefix[] = "RES:"; | 72 const char kResKeyPrefix[] = "RES:"; |
| 72 const char kKeySeparator = '\x00'; | 73 const char kKeySeparator = '\x00'; |
| 73 | 74 |
| 74 const char kUncommittedResIdKeyPrefix[] = "URES:"; | 75 const char kUncommittedResIdKeyPrefix[] = "URES:"; |
| 75 const char kPurgeableResIdKeyPrefix[] = "PRES:"; | 76 const char kPurgeableResIdKeyPrefix[] = "PRES:"; |
| 76 | 77 |
| 77 const int64 kCurrentSchemaVersion = 1; | 78 const int64 kCurrentSchemaVersion = 1; |
| 78 | 79 |
| 80 // For histogram. | |
| 81 const char kOpenHistogramLabel[] = | |
| 82 "ServiceWorker.Database.Open"; | |
| 83 const char kOperationErrorHistogramLabel[] = | |
| 84 "ServiceWorker.Database.OperationError"; | |
| 85 | |
| 79 bool RemovePrefix(const std::string& str, | 86 bool RemovePrefix(const std::string& str, |
| 80 const std::string& prefix, | 87 const std::string& prefix, |
| 81 std::string* out) { | 88 std::string* out) { |
| 82 if (!StartsWithASCII(str, prefix, true)) | 89 if (!StartsWithASCII(str, prefix, true)) |
| 83 return false; | 90 return false; |
| 84 if (out) | 91 if (out) |
| 85 *out = str.substr(prefix.size()); | 92 *out = str.substr(prefix.size()); |
| 86 return true; | 93 return true; |
| 87 } | 94 } |
| 88 | 95 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 out->url = url; | 218 out->url = url; |
| 212 return true; | 219 return true; |
| 213 } | 220 } |
| 214 | 221 |
| 215 ServiceWorkerDatabase::Status LevelDBStatusToStatus( | 222 ServiceWorkerDatabase::Status LevelDBStatusToStatus( |
| 216 const leveldb::Status& status) { | 223 const leveldb::Status& status) { |
| 217 if (status.ok()) | 224 if (status.ok()) |
| 218 return ServiceWorkerDatabase::STATUS_OK; | 225 return ServiceWorkerDatabase::STATUS_OK; |
| 219 else if (status.IsNotFound()) | 226 else if (status.IsNotFound()) |
| 220 return ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND; | 227 return ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND; |
| 228 else if (status.IsIOError()) | |
| 229 return ServiceWorkerDatabase::STATUS_ERROR_IO_ERROR; | |
| 221 else if (status.IsCorruption()) | 230 else if (status.IsCorruption()) |
| 222 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; | 231 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; |
| 223 else | 232 else |
| 224 return ServiceWorkerDatabase::STATUS_ERROR_FAILED; | 233 return ServiceWorkerDatabase::STATUS_ERROR_FAILED; |
| 225 } | 234 } |
| 226 | 235 |
| 227 } // namespace | 236 } // namespace |
| 228 | 237 |
| 229 ServiceWorkerDatabase::RegistrationData::RegistrationData() | 238 ServiceWorkerDatabase::RegistrationData::RegistrationData() |
| 230 : registration_id(-1), | 239 : registration_id(-1), |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 669 leveldb::Options options; | 678 leveldb::Options options; |
| 670 options.create_if_missing = create_if_missing; | 679 options.create_if_missing = create_if_missing; |
| 671 if (use_in_memory_db) { | 680 if (use_in_memory_db) { |
| 672 env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); | 681 env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); |
| 673 options.env = env_.get(); | 682 options.env = env_.get(); |
| 674 } | 683 } |
| 675 | 684 |
| 676 leveldb::DB* db = NULL; | 685 leveldb::DB* db = NULL; |
| 677 leveldb::Status db_status = | 686 leveldb::Status db_status = |
| 678 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db); | 687 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db); |
| 688 Status status = LevelDBStatusToStatus(db_status); | |
| 689 UMA_HISTOGRAM_ENUMERATION(kOpenHistogramLabel, status, STATUS_ERROR_MAX); | |
| 679 if (!db_status.ok()) { | 690 if (!db_status.ok()) { |
| 680 DCHECK(!db); | 691 DCHECK(!db); |
| 681 // TODO(nhiroki): Should we retry to open the database? | 692 // TODO(nhiroki): Should we retry to open the database? |
| 682 HandleError(FROM_HERE, db_status); | 693 HandleError(FROM_HERE, db_status); |
| 683 return LevelDBStatusToStatus(db_status); | 694 return status; |
| 684 } | 695 } |
| 685 db_.reset(db); | 696 db_.reset(db); |
| 686 | 697 |
| 687 int64 db_version; | 698 int64 db_version; |
| 688 Status status = ReadDatabaseVersion(&db_version); | 699 status = ReadDatabaseVersion(&db_version); |
| 689 if (status != STATUS_OK) | 700 if (status != STATUS_OK) |
| 690 return status; | 701 return status; |
| 691 DCHECK_LE(0, db_version); | 702 DCHECK_LE(0, db_version); |
| 692 if (db_version > 0) | 703 if (db_version > 0) |
| 693 state_ = INITIALIZED; | 704 state_ = INITIALIZED; |
| 694 return STATUS_OK; | 705 return STATUS_OK; |
| 695 } | 706 } |
| 696 | 707 |
| 697 bool ServiceWorkerDatabase::IsNewOrNonexistentDatabase( | 708 bool ServiceWorkerDatabase::IsNewOrNonexistentDatabase( |
| 698 ServiceWorkerDatabase::Status status) { | 709 ServiceWorkerDatabase::Status status) { |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 987 } | 998 } |
| 988 } | 999 } |
| 989 | 1000 |
| 990 bool ServiceWorkerDatabase::IsOpen() { | 1001 bool ServiceWorkerDatabase::IsOpen() { |
| 991 return db_ != NULL; | 1002 return db_ != NULL; |
| 992 } | 1003 } |
| 993 | 1004 |
| 994 void ServiceWorkerDatabase::HandleError( | 1005 void ServiceWorkerDatabase::HandleError( |
| 995 const tracked_objects::Location& from_here, | 1006 const tracked_objects::Location& from_here, |
| 996 const leveldb::Status& status) { | 1007 const leveldb::Status& status) { |
| 997 // TODO(nhiroki): Add an UMA histogram. | 1008 DCHECK(!status.ok()); |
| 998 DLOG(ERROR) << "Failed at: " << from_here.ToString() | 1009 DLOG(ERROR) << "Failed at: " << from_here.ToString() |
| 999 << " with error: " << status.ToString(); | 1010 << " with error: " << status.ToString(); |
| 1011 UMA_HISTOGRAM_ENUMERATION(kOperationErrorHistogramLabel, | |
|
michaeln
2014/05/29 23:46:28
I'm not sure how fine grained we want this to be?
nhiroki
2014/06/02 04:54:47
SGTM. The latest patchset records read/write resul
| |
| 1012 LevelDBStatusToStatus(status), | |
| 1013 STATUS_ERROR_MAX); | |
| 1000 state_ = DISABLED; | 1014 state_ = DISABLED; |
| 1001 db_.reset(); | 1015 db_.reset(); |
| 1002 } | 1016 } |
| 1003 | 1017 |
| 1004 } // namespace content | 1018 } // namespace content |
| OLD | NEW |