| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 if (status != STATUS_OK) { | 454 if (status != STATUS_OK) { |
| 455 registrations->clear(); | 455 registrations->clear(); |
| 456 if (opt_resources_list) | 456 if (opt_resources_list) |
| 457 opt_resources_list->clear(); | 457 opt_resources_list->clear(); |
| 458 break; | 458 break; |
| 459 } | 459 } |
| 460 registrations->push_back(registration); | 460 registrations->push_back(registration); |
| 461 | 461 |
| 462 if (opt_resources_list) { | 462 if (opt_resources_list) { |
| 463 std::vector<ResourceRecord> resources; | 463 std::vector<ResourceRecord> resources; |
| 464 status = ReadResourceRecords(registration.version_id, &resources); | 464 status = ReadResourceRecords(registration, &resources); |
| 465 if (status != STATUS_OK) { | 465 if (status != STATUS_OK) { |
| 466 registrations->clear(); | 466 registrations->clear(); |
| 467 opt_resources_list->clear(); | 467 opt_resources_list->clear(); |
| 468 break; | 468 break; |
| 469 } | 469 } |
| 470 opt_resources_list->push_back(resources); | 470 opt_resources_list->push_back(resources); |
| 471 } | 471 } |
| 472 } | 472 } |
| 473 } | 473 } |
| 474 | 474 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 if (IsNewOrNonexistentDatabase(status)) | 527 if (IsNewOrNonexistentDatabase(status)) |
| 528 return STATUS_ERROR_NOT_FOUND; | 528 return STATUS_ERROR_NOT_FOUND; |
| 529 if (status != STATUS_OK) | 529 if (status != STATUS_OK) |
| 530 return status; | 530 return status; |
| 531 | 531 |
| 532 RegistrationData value; | 532 RegistrationData value; |
| 533 status = ReadRegistrationData(registration_id, origin, &value); | 533 status = ReadRegistrationData(registration_id, origin, &value); |
| 534 if (status != STATUS_OK) | 534 if (status != STATUS_OK) |
| 535 return status; | 535 return status; |
| 536 | 536 |
| 537 status = ReadResourceRecords(value.version_id, resources); | 537 status = ReadResourceRecords(value, resources); |
| 538 if (status != STATUS_OK) | 538 if (status != STATUS_OK) |
| 539 return status; | 539 return status; |
| 540 | 540 |
| 541 // ResourceRecord must contain the ServiceWorker's main script. | 541 // ResourceRecord must contain the ServiceWorker's main script. |
| 542 if (resources->empty()) | 542 if (resources->empty()) |
| 543 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; | 543 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; |
| 544 | 544 |
| 545 *registration = value; | 545 *registration = value; |
| 546 return STATUS_OK; | 546 return STATUS_OK; |
| 547 } | 547 } |
| (...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1258 data.add_foreign_fetch_origin(origin.Serialize()); | 1258 data.add_foreign_fetch_origin(origin.Serialize()); |
| 1259 | 1259 |
| 1260 std::string value; | 1260 std::string value; |
| 1261 bool success = data.SerializeToString(&value); | 1261 bool success = data.SerializeToString(&value); |
| 1262 DCHECK(success); | 1262 DCHECK(success); |
| 1263 GURL origin = registration.scope.GetOrigin(); | 1263 GURL origin = registration.scope.GetOrigin(); |
| 1264 batch->Put(CreateRegistrationKey(data.registration_id(), origin), value); | 1264 batch->Put(CreateRegistrationKey(data.registration_id(), origin), value); |
| 1265 } | 1265 } |
| 1266 | 1266 |
| 1267 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceRecords( | 1267 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceRecords( |
| 1268 int64_t version_id, | 1268 const RegistrationData& registration, |
| 1269 std::vector<ResourceRecord>* resources) { | 1269 std::vector<ResourceRecord>* resources) { |
| 1270 DCHECK(resources->empty()); | 1270 DCHECK(resources->empty()); |
| 1271 | 1271 |
| 1272 Status status = STATUS_OK; | 1272 Status status = STATUS_OK; |
| 1273 const std::string prefix = CreateResourceRecordKeyPrefix(version_id); | 1273 bool has_main_resource = false; |
| 1274 | 1274 const std::string prefix = |
| 1275 CreateResourceRecordKeyPrefix(registration.version_id); |
| 1275 { | 1276 { |
| 1276 std::unique_ptr<leveldb::Iterator> itr( | 1277 std::unique_ptr<leveldb::Iterator> itr( |
| 1277 db_->NewIterator(leveldb::ReadOptions())); | 1278 db_->NewIterator(leveldb::ReadOptions())); |
| 1278 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { | 1279 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { |
| 1279 Status status = LevelDBStatusToStatus(itr->status()); | 1280 Status status = LevelDBStatusToStatus(itr->status()); |
| 1280 if (status != STATUS_OK) { | 1281 if (status != STATUS_OK) { |
| 1281 resources->clear(); | 1282 resources->clear(); |
| 1282 break; | 1283 break; |
| 1283 } | 1284 } |
| 1284 | 1285 |
| 1285 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) | 1286 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) |
| 1286 break; | 1287 break; |
| 1287 | 1288 |
| 1288 ResourceRecord resource; | 1289 ResourceRecord resource; |
| 1289 status = ParseResourceRecord(itr->value().ToString(), &resource); | 1290 status = ParseResourceRecord(itr->value().ToString(), &resource); |
| 1290 if (status != STATUS_OK) { | 1291 if (status != STATUS_OK) { |
| 1291 resources->clear(); | 1292 resources->clear(); |
| 1292 break; | 1293 break; |
| 1293 } | 1294 } |
| 1295 |
| 1296 if (registration.script == resource.url) { |
| 1297 DCHECK(!has_main_resource); |
| 1298 has_main_resource = true; |
| 1299 } |
| 1300 |
| 1294 resources->push_back(resource); | 1301 resources->push_back(resource); |
| 1295 } | 1302 } |
| 1296 } | 1303 } |
| 1297 | 1304 |
| 1305 // |resources| should contain the main script. |
| 1306 if (!has_main_resource) { |
| 1307 resources->clear(); |
| 1308 status = STATUS_ERROR_CORRUPTED; |
| 1309 } |
| 1310 |
| 1298 HandleReadResult(FROM_HERE, status); | 1311 HandleReadResult(FROM_HERE, status); |
| 1299 return status; | 1312 return status; |
| 1300 } | 1313 } |
| 1301 | 1314 |
| 1302 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ParseResourceRecord( | 1315 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ParseResourceRecord( |
| 1303 const std::string& serialized, | 1316 const std::string& serialized, |
| 1304 ServiceWorkerDatabase::ResourceRecord* out) { | 1317 ServiceWorkerDatabase::ResourceRecord* out) { |
| 1305 DCHECK(out); | 1318 DCHECK(out); |
| 1306 ServiceWorkerResourceRecord record; | 1319 ServiceWorkerResourceRecord record; |
| 1307 if (!record.ParseFromString(serialized)) | 1320 if (!record.ParseFromString(serialized)) |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1621 if (status != STATUS_OK) | 1634 if (status != STATUS_OK) |
| 1622 Disable(from_here, status); | 1635 Disable(from_here, status); |
| 1623 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1636 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
| 1624 } | 1637 } |
| 1625 | 1638 |
| 1626 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { | 1639 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { |
| 1627 return path_.empty(); | 1640 return path_.empty(); |
| 1628 } | 1641 } |
| 1629 | 1642 |
| 1630 } // namespace content | 1643 } // namespace content |
| OLD | NEW |