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/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 | 127 |
| 128 // Convert RegistrationData to ServiceWorkerRegistrationData. | 128 // Convert RegistrationData to ServiceWorkerRegistrationData. |
| 129 ServiceWorkerRegistrationData data; | 129 ServiceWorkerRegistrationData data; |
| 130 data.set_registration_id(input.registration_id); | 130 data.set_registration_id(input.registration_id); |
| 131 data.set_scope_url(input.scope.spec()); | 131 data.set_scope_url(input.scope.spec()); |
| 132 data.set_script_url(input.script.spec()); | 132 data.set_script_url(input.script.spec()); |
| 133 data.set_version_id(input.version_id); | 133 data.set_version_id(input.version_id); |
| 134 data.set_is_active(input.is_active); | 134 data.set_is_active(input.is_active); |
| 135 data.set_has_fetch_handler(input.has_fetch_handler); | 135 data.set_has_fetch_handler(input.has_fetch_handler); |
| 136 data.set_last_update_check_time(input.last_update_check.ToInternalValue()); | 136 data.set_last_update_check_time(input.last_update_check.ToInternalValue()); |
| 137 data.set_resources_total_size_bytes(input.resources_total_size_bytes); | |
| 137 | 138 |
| 138 std::string value; | 139 std::string value; |
| 139 bool success = data.SerializeToString(&value); | 140 bool success = data.SerializeToString(&value); |
| 140 DCHECK(success); | 141 DCHECK(success); |
| 141 GURL origin = input.scope.GetOrigin(); | 142 GURL origin = input.scope.GetOrigin(); |
| 142 batch->Put(CreateRegistrationKey(data.registration_id(), origin), value); | 143 batch->Put(CreateRegistrationKey(data.registration_id(), origin), value); |
| 143 } | 144 } |
| 144 | 145 |
| 145 void PutResourceRecordToBatch( | 146 void PutResourceRecordToBatch( |
| 146 const ServiceWorkerDatabase::ResourceRecord& input, | 147 const ServiceWorkerDatabase::ResourceRecord& input, |
| 147 int64 version_id, | 148 int64 version_id, |
| 148 leveldb::WriteBatch* batch) { | 149 leveldb::WriteBatch* batch) { |
| 149 DCHECK(batch); | 150 DCHECK(batch); |
| 151 DCHECK_GE(input.size_bytes, 0); | |
| 150 | 152 |
| 151 // Convert ResourceRecord to ServiceWorkerResourceRecord. | 153 // Convert ResourceRecord to ServiceWorkerResourceRecord. |
| 152 ServiceWorkerResourceRecord record; | 154 ServiceWorkerResourceRecord record; |
| 153 record.set_resource_id(input.resource_id); | 155 record.set_resource_id(input.resource_id); |
| 154 record.set_url(input.url.spec()); | 156 record.set_url(input.url.spec()); |
| 157 record.set_size_bytes(input.size_bytes); | |
| 155 | 158 |
| 156 std::string value; | 159 std::string value; |
| 157 bool success = record.SerializeToString(&value); | 160 bool success = record.SerializeToString(&value); |
| 158 DCHECK(success); | 161 DCHECK(success); |
| 159 batch->Put(CreateResourceRecordKey(version_id, input.resource_id), value); | 162 batch->Put(CreateResourceRecordKey(version_id, input.resource_id), value); |
| 160 } | 163 } |
| 161 | 164 |
| 162 void PutUniqueOriginToBatch(const GURL& origin, | 165 void PutUniqueOriginToBatch(const GURL& origin, |
| 163 leveldb::WriteBatch* batch) { | 166 leveldb::WriteBatch* batch) { |
| 164 // Value should be empty. | 167 // Value should be empty. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 | 223 |
| 221 // Convert ServiceWorkerRegistrationData to RegistrationData. | 224 // Convert ServiceWorkerRegistrationData to RegistrationData. |
| 222 out->registration_id = data.registration_id(); | 225 out->registration_id = data.registration_id(); |
| 223 out->scope = scope_url; | 226 out->scope = scope_url; |
| 224 out->script = script_url; | 227 out->script = script_url; |
| 225 out->version_id = data.version_id(); | 228 out->version_id = data.version_id(); |
| 226 out->is_active = data.is_active(); | 229 out->is_active = data.is_active(); |
| 227 out->has_fetch_handler = data.has_fetch_handler(); | 230 out->has_fetch_handler = data.has_fetch_handler(); |
| 228 out->last_update_check = | 231 out->last_update_check = |
| 229 base::Time::FromInternalValue(data.last_update_check_time()); | 232 base::Time::FromInternalValue(data.last_update_check_time()); |
| 233 out->resources_total_size_bytes = data.resources_total_size_bytes(); | |
| 234 | |
| 230 return ServiceWorkerDatabase::STATUS_OK; | 235 return ServiceWorkerDatabase::STATUS_OK; |
| 231 } | 236 } |
| 232 | 237 |
| 233 ServiceWorkerDatabase::Status ParseResourceRecord( | 238 ServiceWorkerDatabase::Status ParseResourceRecord( |
| 234 const std::string& serialized, | 239 const std::string& serialized, |
| 235 ServiceWorkerDatabase::ResourceRecord* out) { | 240 ServiceWorkerDatabase::ResourceRecord* out) { |
| 236 DCHECK(out); | 241 DCHECK(out); |
| 237 ServiceWorkerResourceRecord record; | 242 ServiceWorkerResourceRecord record; |
| 238 if (!record.ParseFromString(serialized)) | 243 if (!record.ParseFromString(serialized)) |
| 239 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; | 244 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; |
| 240 | 245 |
| 241 GURL url(record.url()); | 246 GURL url(record.url()); |
| 242 if (!url.is_valid()) | 247 if (!url.is_valid()) |
| 243 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; | 248 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; |
| 244 | 249 |
| 245 // Convert ServiceWorkerResourceRecord to ResourceRecord. | 250 // Convert ServiceWorkerResourceRecord to ResourceRecord. |
| 246 out->resource_id = record.resource_id(); | 251 out->resource_id = record.resource_id(); |
| 247 out->url = url; | 252 out->url = url; |
| 253 out->size_bytes = record.size_bytes(); | |
| 248 return ServiceWorkerDatabase::STATUS_OK; | 254 return ServiceWorkerDatabase::STATUS_OK; |
| 249 } | 255 } |
| 250 | 256 |
| 251 ServiceWorkerDatabase::Status LevelDBStatusToStatus( | 257 ServiceWorkerDatabase::Status LevelDBStatusToStatus( |
| 252 const leveldb::Status& status) { | 258 const leveldb::Status& status) { |
| 253 if (status.ok()) | 259 if (status.ok()) |
| 254 return ServiceWorkerDatabase::STATUS_OK; | 260 return ServiceWorkerDatabase::STATUS_OK; |
| 255 else if (status.IsNotFound()) | 261 else if (status.IsNotFound()) |
| 256 return ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND; | 262 return ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND; |
| 257 else if (status.IsIOError()) | 263 else if (status.IsIOError()) |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 279 return "Database unknown error"; | 285 return "Database unknown error"; |
| 280 } | 286 } |
| 281 NOTREACHED(); | 287 NOTREACHED(); |
| 282 return "Database unknown error"; | 288 return "Database unknown error"; |
| 283 } | 289 } |
| 284 | 290 |
| 285 } // namespace | 291 } // namespace |
| 286 | 292 |
| 287 ServiceWorkerDatabase::RegistrationData::RegistrationData() | 293 ServiceWorkerDatabase::RegistrationData::RegistrationData() |
| 288 : registration_id(kInvalidServiceWorkerRegistrationId), | 294 : registration_id(kInvalidServiceWorkerRegistrationId), |
| 289 version_id(kInvalidServiceWorkerVersionId), | 295 version_id(kInvalidServiceWorkerVersionId), |
|
michaeln
2014/10/17 20:29:53
oh... also zero init the new data member here
dmurph
2014/10/17 22:55:19
Done.
| |
| 290 is_active(false), | 296 is_active(false), |
| 291 has_fetch_handler(false) { | 297 has_fetch_handler(false) { |
| 292 } | 298 } |
| 293 | 299 |
| 294 ServiceWorkerDatabase::RegistrationData::~RegistrationData() { | 300 ServiceWorkerDatabase::RegistrationData::~RegistrationData() { |
| 295 } | 301 } |
| 296 | 302 |
| 297 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) | 303 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) |
| 298 : path_(path), | 304 : path_(path), |
| 299 next_avail_registration_id_(0), | 305 next_avail_registration_id_(0), |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 487 *deleted_version_id = kInvalidServiceWorkerVersionId; | 493 *deleted_version_id = kInvalidServiceWorkerVersionId; |
| 488 Status status = LazyOpen(true); | 494 Status status = LazyOpen(true); |
| 489 if (status != STATUS_OK) | 495 if (status != STATUS_OK) |
| 490 return status; | 496 return status; |
| 491 | 497 |
| 492 leveldb::WriteBatch batch; | 498 leveldb::WriteBatch batch; |
| 493 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch); | 499 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch); |
| 494 BumpNextVersionIdIfNeeded(registration.version_id, &batch); | 500 BumpNextVersionIdIfNeeded(registration.version_id, &batch); |
| 495 | 501 |
| 496 PutUniqueOriginToBatch(registration.scope.GetOrigin(), &batch); | 502 PutUniqueOriginToBatch(registration.scope.GetOrigin(), &batch); |
| 503 #if DCHECK_IS_ON | |
| 504 uint64 total_size_bytes = 0; | |
| 505 for (const auto& resource : resources) { | |
| 506 total_size_bytes += resource.size_bytes; | |
| 507 } | |
| 508 DCHECK_EQ(total_size_bytes, registration.resources_total_size_bytes); | |
| 509 #endif | |
| 497 PutRegistrationDataToBatch(registration, &batch); | 510 PutRegistrationDataToBatch(registration, &batch); |
| 498 | 511 |
| 499 // Used for avoiding multiple writes for the same resource id or url. | 512 // Used for avoiding multiple writes for the same resource id or url. |
| 500 std::set<int64> pushed_resources; | 513 std::set<int64> pushed_resources; |
| 501 std::set<GURL> pushed_urls; | 514 std::set<GURL> pushed_urls; |
| 502 for (std::vector<ResourceRecord>::const_iterator itr = resources.begin(); | 515 for (std::vector<ResourceRecord>::const_iterator itr = resources.begin(); |
| 503 itr != resources.end(); ++itr) { | 516 itr != resources.end(); ++itr) { |
| 504 if (!itr->url.is_valid()) | 517 if (!itr->url.is_valid()) |
| 505 return STATUS_ERROR_FAILED; | 518 return STATUS_ERROR_FAILED; |
| 506 | 519 |
| (...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1113 | 1126 |
| 1114 void ServiceWorkerDatabase::HandleWriteResult( | 1127 void ServiceWorkerDatabase::HandleWriteResult( |
| 1115 const tracked_objects::Location& from_here, | 1128 const tracked_objects::Location& from_here, |
| 1116 Status status) { | 1129 Status status) { |
| 1117 if (status != STATUS_OK) | 1130 if (status != STATUS_OK) |
| 1118 Disable(from_here, status); | 1131 Disable(from_here, status); |
| 1119 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1132 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
| 1120 } | 1133 } |
| 1121 | 1134 |
| 1122 } // namespace content | 1135 } // namespace content |
| OLD | NEW |