| 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 24 matching lines...) Expand all Loading... |
| 282 return "Database unknown error"; | 288 return "Database unknown error"; |
| 283 } | 289 } |
| 284 NOTREACHED(); | 290 NOTREACHED(); |
| 285 return "Database unknown error"; | 291 return "Database unknown error"; |
| 286 } | 292 } |
| 287 | 293 |
| 288 ServiceWorkerDatabase::RegistrationData::RegistrationData() | 294 ServiceWorkerDatabase::RegistrationData::RegistrationData() |
| 289 : registration_id(kInvalidServiceWorkerRegistrationId), | 295 : registration_id(kInvalidServiceWorkerRegistrationId), |
| 290 version_id(kInvalidServiceWorkerVersionId), | 296 version_id(kInvalidServiceWorkerVersionId), |
| 291 is_active(false), | 297 is_active(false), |
| 292 has_fetch_handler(false) { | 298 has_fetch_handler(false), |
| 299 resources_total_size_bytes(0) { |
| 293 } | 300 } |
| 294 | 301 |
| 295 ServiceWorkerDatabase::RegistrationData::~RegistrationData() { | 302 ServiceWorkerDatabase::RegistrationData::~RegistrationData() { |
| 296 } | 303 } |
| 297 | 304 |
| 298 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) | 305 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) |
| 299 : path_(path), | 306 : path_(path), |
| 300 next_avail_registration_id_(0), | 307 next_avail_registration_id_(0), |
| 301 next_avail_resource_id_(0), | 308 next_avail_resource_id_(0), |
| 302 next_avail_version_id_(0), | 309 next_avail_version_id_(0), |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 *deleted_version_id = kInvalidServiceWorkerVersionId; | 495 *deleted_version_id = kInvalidServiceWorkerVersionId; |
| 489 Status status = LazyOpen(true); | 496 Status status = LazyOpen(true); |
| 490 if (status != STATUS_OK) | 497 if (status != STATUS_OK) |
| 491 return status; | 498 return status; |
| 492 | 499 |
| 493 leveldb::WriteBatch batch; | 500 leveldb::WriteBatch batch; |
| 494 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch); | 501 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch); |
| 495 BumpNextVersionIdIfNeeded(registration.version_id, &batch); | 502 BumpNextVersionIdIfNeeded(registration.version_id, &batch); |
| 496 | 503 |
| 497 PutUniqueOriginToBatch(registration.scope.GetOrigin(), &batch); | 504 PutUniqueOriginToBatch(registration.scope.GetOrigin(), &batch); |
| 505 #if DCHECK_IS_ON |
| 506 uint64 total_size_bytes = 0; |
| 507 for (const auto& resource : resources) { |
| 508 total_size_bytes += resource.size_bytes; |
| 509 } |
| 510 DCHECK_EQ(total_size_bytes, registration.resources_total_size_bytes) |
| 511 << "The total size in the registration must match the cumulative " |
| 512 << "sizes of the resources."; |
| 513 #endif |
| 498 PutRegistrationDataToBatch(registration, &batch); | 514 PutRegistrationDataToBatch(registration, &batch); |
| 499 | 515 |
| 500 // Used for avoiding multiple writes for the same resource id or url. | 516 // Used for avoiding multiple writes for the same resource id or url. |
| 501 std::set<int64> pushed_resources; | 517 std::set<int64> pushed_resources; |
| 502 std::set<GURL> pushed_urls; | 518 std::set<GURL> pushed_urls; |
| 503 for (std::vector<ResourceRecord>::const_iterator itr = resources.begin(); | 519 for (std::vector<ResourceRecord>::const_iterator itr = resources.begin(); |
| 504 itr != resources.end(); ++itr) { | 520 itr != resources.end(); ++itr) { |
| 505 if (!itr->url.is_valid()) | 521 if (!itr->url.is_valid()) |
| 506 return STATUS_ERROR_FAILED; | 522 return STATUS_ERROR_FAILED; |
| 507 | 523 |
| (...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1126 | 1142 |
| 1127 void ServiceWorkerDatabase::HandleWriteResult( | 1143 void ServiceWorkerDatabase::HandleWriteResult( |
| 1128 const tracked_objects::Location& from_here, | 1144 const tracked_objects::Location& from_here, |
| 1129 Status status) { | 1145 Status status) { |
| 1130 if (status != STATUS_OK) | 1146 if (status != STATUS_OK) |
| 1131 Disable(from_here, status); | 1147 Disable(from_here, status); |
| 1132 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1148 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
| 1133 } | 1149 } |
| 1134 | 1150 |
| 1135 } // namespace content | 1151 } // namespace content |
| OLD | NEW |