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> |
| 8 |
7 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
8 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
9 #include "base/location.h" | 11 #include "base/location.h" |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
12 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
13 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
15 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { | 339 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { |
338 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 340 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
339 DCHECK(origins->empty()); | 341 DCHECK(origins->empty()); |
340 | 342 |
341 Status status = LazyOpen(false); | 343 Status status = LazyOpen(false); |
342 if (IsNewOrNonexistentDatabase(status)) | 344 if (IsNewOrNonexistentDatabase(status)) |
343 return STATUS_OK; | 345 return STATUS_OK; |
344 if (status != STATUS_OK) | 346 if (status != STATUS_OK) |
345 return status; | 347 return status; |
346 | 348 |
347 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 349 { |
348 for (itr->Seek(kUniqueOriginKey); itr->Valid(); itr->Next()) { | 350 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
349 status = LevelDBStatusToStatus(itr->status()); | 351 for (itr->Seek(kUniqueOriginKey); itr->Valid(); itr->Next()) { |
350 if (status != STATUS_OK) { | 352 status = LevelDBStatusToStatus(itr->status()); |
351 HandleReadResult(FROM_HERE, status); | 353 if (status != STATUS_OK) { |
352 origins->clear(); | 354 origins->clear(); |
353 return status; | 355 break; |
| 356 } |
| 357 |
| 358 std::string origin_str; |
| 359 if (!RemovePrefix(itr->key().ToString(), kUniqueOriginKey, &origin_str)) |
| 360 break; |
| 361 |
| 362 GURL origin(origin_str); |
| 363 if (!origin.is_valid()) { |
| 364 status = STATUS_ERROR_CORRUPTED; |
| 365 origins->clear(); |
| 366 break; |
| 367 } |
| 368 |
| 369 origins->insert(origin); |
354 } | 370 } |
355 | |
356 std::string origin_str; | |
357 if (!RemovePrefix(itr->key().ToString(), kUniqueOriginKey, &origin_str)) | |
358 break; | |
359 | |
360 GURL origin(origin_str); | |
361 if (!origin.is_valid()) { | |
362 status = STATUS_ERROR_CORRUPTED; | |
363 HandleReadResult(FROM_HERE, status); | |
364 origins->clear(); | |
365 return status; | |
366 } | |
367 | |
368 origins->insert(origin); | |
369 } | 371 } |
370 | 372 |
371 HandleReadResult(FROM_HERE, status); | 373 HandleReadResult(FROM_HERE, status); |
372 return status; | 374 return status; |
373 } | 375 } |
374 | 376 |
375 ServiceWorkerDatabase::Status | 377 ServiceWorkerDatabase::Status |
376 ServiceWorkerDatabase::GetOriginsWithForeignFetchRegistrations( | 378 ServiceWorkerDatabase::GetOriginsWithForeignFetchRegistrations( |
377 std::set<GURL>* origins) { | 379 std::set<GURL>* origins) { |
378 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 380 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
379 DCHECK(origins->empty()); | 381 DCHECK(origins->empty()); |
380 | 382 |
381 Status status = LazyOpen(false); | 383 Status status = LazyOpen(false); |
382 if (IsNewOrNonexistentDatabase(status)) | 384 if (IsNewOrNonexistentDatabase(status)) |
383 return STATUS_OK; | 385 return STATUS_OK; |
384 if (status != STATUS_OK) | 386 if (status != STATUS_OK) |
385 return status; | 387 return status; |
386 | 388 |
387 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 389 { |
388 for (itr->Seek(kForeignFetchOriginKey); itr->Valid(); itr->Next()) { | 390 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
389 status = LevelDBStatusToStatus(itr->status()); | 391 for (itr->Seek(kForeignFetchOriginKey); itr->Valid(); itr->Next()) { |
390 if (status != STATUS_OK) { | 392 status = LevelDBStatusToStatus(itr->status()); |
391 HandleReadResult(FROM_HERE, status); | 393 if (status != STATUS_OK) { |
392 origins->clear(); | 394 origins->clear(); |
393 return status; | 395 break; |
| 396 } |
| 397 |
| 398 std::string origin_str; |
| 399 if (!RemovePrefix(itr->key().ToString(), kForeignFetchOriginKey, |
| 400 &origin_str)) |
| 401 break; |
| 402 |
| 403 GURL origin(origin_str); |
| 404 if (!origin.is_valid()) { |
| 405 status = STATUS_ERROR_CORRUPTED; |
| 406 origins->clear(); |
| 407 break; |
| 408 } |
| 409 |
| 410 origins->insert(origin); |
394 } | 411 } |
395 | |
396 std::string origin_str; | |
397 if (!RemovePrefix(itr->key().ToString(), kForeignFetchOriginKey, | |
398 &origin_str)) | |
399 break; | |
400 | |
401 GURL origin(origin_str); | |
402 if (!origin.is_valid()) { | |
403 status = STATUS_ERROR_CORRUPTED; | |
404 HandleReadResult(FROM_HERE, status); | |
405 origins->clear(); | |
406 return status; | |
407 } | |
408 | |
409 origins->insert(origin); | |
410 } | 412 } |
411 | 413 |
412 HandleReadResult(FROM_HERE, status); | 414 HandleReadResult(FROM_HERE, status); |
413 return status; | 415 return status; |
414 } | 416 } |
415 | 417 |
416 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin( | 418 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin( |
417 const GURL& origin, | 419 const GURL& origin, |
418 std::vector<RegistrationData>* registrations, | 420 std::vector<RegistrationData>* registrations, |
419 std::vector<std::vector<ResourceRecord>>* opt_resources_list) { | 421 std::vector<std::vector<ResourceRecord>>* opt_resources_list) { |
420 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 422 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
421 DCHECK(registrations->empty()); | 423 DCHECK(registrations->empty()); |
422 | 424 |
423 Status status = LazyOpen(false); | 425 Status status = LazyOpen(false); |
424 if (IsNewOrNonexistentDatabase(status)) | 426 if (IsNewOrNonexistentDatabase(status)) |
425 return STATUS_OK; | 427 return STATUS_OK; |
426 if (status != STATUS_OK) | 428 if (status != STATUS_OK) |
427 return status; | 429 return status; |
428 | 430 |
429 std::string prefix = CreateRegistrationKeyPrefix(origin); | 431 std::string prefix = CreateRegistrationKeyPrefix(origin); |
430 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 432 { |
431 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { | 433 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
432 status = LevelDBStatusToStatus(itr->status()); | 434 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { |
433 if (status != STATUS_OK) { | 435 status = LevelDBStatusToStatus(itr->status()); |
434 HandleReadResult(FROM_HERE, status); | 436 if (status != STATUS_OK) { |
435 registrations->clear(); | 437 registrations->clear(); |
436 if (opt_resources_list) | 438 if (opt_resources_list) |
437 opt_resources_list->clear(); | 439 opt_resources_list->clear(); |
438 return status; | 440 break; |
439 } | 441 } |
440 | 442 |
441 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) | 443 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) |
442 break; | 444 break; |
443 | 445 |
444 RegistrationData registration; | 446 RegistrationData registration; |
445 status = ParseRegistrationData(itr->value().ToString(), ®istration); | 447 status = ParseRegistrationData(itr->value().ToString(), ®istration); |
446 if (status != STATUS_OK) { | 448 if (status != STATUS_OK) { |
447 HandleReadResult(FROM_HERE, status); | 449 registrations->clear(); |
448 registrations->clear(); | 450 if (opt_resources_list) |
449 if (opt_resources_list) | 451 opt_resources_list->clear(); |
450 opt_resources_list->clear(); | 452 break; |
451 return status; | 453 } |
452 } | 454 registrations->push_back(registration); |
453 registrations->push_back(registration); | |
454 | 455 |
455 if (opt_resources_list) { | 456 if (opt_resources_list) { |
456 std::vector<ResourceRecord> resources; | 457 std::vector<ResourceRecord> resources; |
457 status = ReadResourceRecords(registration.version_id, &resources); | 458 status = ReadResourceRecords(registration.version_id, &resources); |
458 if (status != STATUS_OK) { | 459 if (status != STATUS_OK) { |
459 HandleReadResult(FROM_HERE, status); | 460 registrations->clear(); |
460 registrations->clear(); | 461 opt_resources_list->clear(); |
461 opt_resources_list->clear(); | 462 break; |
462 return status; | 463 } |
| 464 opt_resources_list->push_back(resources); |
463 } | 465 } |
464 opt_resources_list->push_back(resources); | |
465 } | 466 } |
466 } | 467 } |
467 | 468 |
468 HandleReadResult(FROM_HERE, status); | 469 HandleReadResult(FROM_HERE, status); |
469 return status; | 470 return status; |
470 } | 471 } |
471 | 472 |
472 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( | 473 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( |
473 std::vector<RegistrationData>* registrations) { | 474 std::vector<RegistrationData>* registrations) { |
474 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 475 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
475 DCHECK(registrations->empty()); | 476 DCHECK(registrations->empty()); |
476 | 477 |
477 Status status = LazyOpen(false); | 478 Status status = LazyOpen(false); |
478 if (IsNewOrNonexistentDatabase(status)) | 479 if (IsNewOrNonexistentDatabase(status)) |
479 return STATUS_OK; | 480 return STATUS_OK; |
480 if (status != STATUS_OK) | 481 if (status != STATUS_OK) |
481 return status; | 482 return status; |
482 | 483 |
483 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 484 { |
484 for (itr->Seek(kRegKeyPrefix); itr->Valid(); itr->Next()) { | 485 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
485 status = LevelDBStatusToStatus(itr->status()); | 486 for (itr->Seek(kRegKeyPrefix); itr->Valid(); itr->Next()) { |
486 if (status != STATUS_OK) { | 487 status = LevelDBStatusToStatus(itr->status()); |
487 HandleReadResult(FROM_HERE, status); | 488 if (status != STATUS_OK) { |
488 registrations->clear(); | 489 registrations->clear(); |
489 return status; | 490 break; |
| 491 } |
| 492 |
| 493 if (!RemovePrefix(itr->key().ToString(), kRegKeyPrefix, NULL)) |
| 494 break; |
| 495 |
| 496 RegistrationData registration; |
| 497 status = ParseRegistrationData(itr->value().ToString(), ®istration); |
| 498 if (status != STATUS_OK) { |
| 499 registrations->clear(); |
| 500 break; |
| 501 } |
| 502 registrations->push_back(registration); |
490 } | 503 } |
491 | |
492 if (!RemovePrefix(itr->key().ToString(), kRegKeyPrefix, NULL)) | |
493 break; | |
494 | |
495 RegistrationData registration; | |
496 status = ParseRegistrationData(itr->value().ToString(), ®istration); | |
497 if (status != STATUS_OK) { | |
498 HandleReadResult(FROM_HERE, status); | |
499 registrations->clear(); | |
500 return status; | |
501 } | |
502 registrations->push_back(registration); | |
503 } | 504 } |
504 | 505 |
505 HandleReadResult(FROM_HERE, status); | 506 HandleReadResult(FROM_HERE, status); |
506 return status; | 507 return status; |
507 } | 508 } |
508 | 509 |
509 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadRegistration( | 510 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadRegistration( |
510 int64_t registration_id, | 511 int64_t registration_id, |
511 const GURL& origin, | 512 const GURL& origin, |
512 RegistrationData* registration, | 513 RegistrationData* registration, |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
868 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 869 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
869 DCHECK(user_data->empty()); | 870 DCHECK(user_data->empty()); |
870 | 871 |
871 Status status = LazyOpen(false); | 872 Status status = LazyOpen(false); |
872 if (IsNewOrNonexistentDatabase(status)) | 873 if (IsNewOrNonexistentDatabase(status)) |
873 return STATUS_OK; | 874 return STATUS_OK; |
874 if (status != STATUS_OK) | 875 if (status != STATUS_OK) |
875 return status; | 876 return status; |
876 | 877 |
877 std::string key_prefix = CreateHasUserDataKeyPrefix(user_data_name); | 878 std::string key_prefix = CreateHasUserDataKeyPrefix(user_data_name); |
878 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 879 { |
879 for (itr->Seek(key_prefix); itr->Valid(); itr->Next()) { | 880 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
880 status = LevelDBStatusToStatus(itr->status()); | 881 for (itr->Seek(key_prefix); itr->Valid(); itr->Next()) { |
881 if (status != STATUS_OK) { | 882 status = LevelDBStatusToStatus(itr->status()); |
882 HandleReadResult(FROM_HERE, status); | 883 if (status != STATUS_OK) { |
883 user_data->clear(); | 884 user_data->clear(); |
884 return status; | 885 break; |
| 886 } |
| 887 |
| 888 std::string registration_id_string; |
| 889 if (!RemovePrefix(itr->key().ToString(), key_prefix, |
| 890 ®istration_id_string)) { |
| 891 break; |
| 892 } |
| 893 |
| 894 int64_t registration_id; |
| 895 status = ParseId(registration_id_string, ®istration_id); |
| 896 if (status != STATUS_OK) { |
| 897 user_data->clear(); |
| 898 break; |
| 899 } |
| 900 |
| 901 std::string value; |
| 902 status = LevelDBStatusToStatus( |
| 903 db_->Get(leveldb::ReadOptions(), |
| 904 CreateUserDataKey(registration_id, user_data_name), &value)); |
| 905 if (status != STATUS_OK) { |
| 906 user_data->clear(); |
| 907 break; |
| 908 } |
| 909 user_data->push_back(std::make_pair(registration_id, value)); |
885 } | 910 } |
886 | |
887 std::string registration_id_string; | |
888 if (!RemovePrefix(itr->key().ToString(), key_prefix, | |
889 ®istration_id_string)) { | |
890 break; | |
891 } | |
892 | |
893 int64_t registration_id; | |
894 status = ParseId(registration_id_string, ®istration_id); | |
895 if (status != STATUS_OK) { | |
896 HandleReadResult(FROM_HERE, status); | |
897 user_data->clear(); | |
898 return status; | |
899 } | |
900 | |
901 std::string value; | |
902 status = LevelDBStatusToStatus( | |
903 db_->Get(leveldb::ReadOptions(), | |
904 CreateUserDataKey(registration_id, user_data_name), &value)); | |
905 if (status != STATUS_OK) { | |
906 HandleReadResult(FROM_HERE, status); | |
907 user_data->clear(); | |
908 return status; | |
909 } | |
910 user_data->push_back(std::make_pair(registration_id, value)); | |
911 } | 911 } |
912 | 912 |
913 HandleReadResult(FROM_HERE, status); | 913 HandleReadResult(FROM_HERE, status); |
914 return status; | 914 return status; |
915 } | 915 } |
916 | 916 |
917 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetUncommittedResourceIds( | 917 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetUncommittedResourceIds( |
918 std::set<int64_t>* ids) { | 918 std::set<int64_t>* ids) { |
919 return ReadResourceIds(kUncommittedResIdKeyPrefix, ids); | 919 return ReadResourceIds(kUncommittedResIdKeyPrefix, ids); |
920 } | 920 } |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1233 } | 1233 } |
1234 | 1234 |
1235 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceRecords( | 1235 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceRecords( |
1236 int64_t version_id, | 1236 int64_t version_id, |
1237 std::vector<ResourceRecord>* resources) { | 1237 std::vector<ResourceRecord>* resources) { |
1238 DCHECK(resources->empty()); | 1238 DCHECK(resources->empty()); |
1239 | 1239 |
1240 Status status = STATUS_OK; | 1240 Status status = STATUS_OK; |
1241 const std::string prefix = CreateResourceRecordKeyPrefix(version_id); | 1241 const std::string prefix = CreateResourceRecordKeyPrefix(version_id); |
1242 | 1242 |
1243 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 1243 { |
1244 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { | 1244 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
1245 Status status = LevelDBStatusToStatus(itr->status()); | 1245 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { |
1246 if (status != STATUS_OK) { | 1246 Status status = LevelDBStatusToStatus(itr->status()); |
1247 HandleReadResult(FROM_HERE, status); | 1247 if (status != STATUS_OK) { |
1248 resources->clear(); | 1248 resources->clear(); |
1249 return status; | 1249 break; |
| 1250 } |
| 1251 |
| 1252 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) |
| 1253 break; |
| 1254 |
| 1255 ResourceRecord resource; |
| 1256 status = ParseResourceRecord(itr->value().ToString(), &resource); |
| 1257 if (status != STATUS_OK) { |
| 1258 resources->clear(); |
| 1259 break; |
| 1260 } |
| 1261 resources->push_back(resource); |
1250 } | 1262 } |
1251 | |
1252 if (!RemovePrefix(itr->key().ToString(), prefix, NULL)) | |
1253 break; | |
1254 | |
1255 ResourceRecord resource; | |
1256 status = ParseResourceRecord(itr->value().ToString(), &resource); | |
1257 if (status != STATUS_OK) { | |
1258 HandleReadResult(FROM_HERE, status); | |
1259 resources->clear(); | |
1260 return status; | |
1261 } | |
1262 resources->push_back(resource); | |
1263 } | 1263 } |
1264 | 1264 |
1265 HandleReadResult(FROM_HERE, status); | 1265 HandleReadResult(FROM_HERE, status); |
1266 return status; | 1266 return status; |
1267 } | 1267 } |
1268 | 1268 |
1269 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ParseResourceRecord( | 1269 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ParseResourceRecord( |
1270 const std::string& serialized, | 1270 const std::string& serialized, |
1271 ServiceWorkerDatabase::ResourceRecord* out) { | 1271 ServiceWorkerDatabase::ResourceRecord* out) { |
1272 DCHECK(out); | 1272 DCHECK(out); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1321 | 1321 |
1322 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteResourceRecords( | 1322 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteResourceRecords( |
1323 int64_t version_id, | 1323 int64_t version_id, |
1324 std::vector<int64_t>* newly_purgeable_resources, | 1324 std::vector<int64_t>* newly_purgeable_resources, |
1325 leveldb::WriteBatch* batch) { | 1325 leveldb::WriteBatch* batch) { |
1326 DCHECK(batch); | 1326 DCHECK(batch); |
1327 | 1327 |
1328 Status status = STATUS_OK; | 1328 Status status = STATUS_OK; |
1329 const std::string prefix = CreateResourceRecordKeyPrefix(version_id); | 1329 const std::string prefix = CreateResourceRecordKeyPrefix(version_id); |
1330 | 1330 |
1331 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 1331 { |
1332 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { | 1332 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
1333 status = LevelDBStatusToStatus(itr->status()); | 1333 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { |
1334 if (status != STATUS_OK) { | 1334 status = LevelDBStatusToStatus(itr->status()); |
1335 HandleReadResult(FROM_HERE, status); | 1335 if (status != STATUS_OK) |
1336 return status; | 1336 break; |
| 1337 |
| 1338 const std::string key = itr->key().ToString(); |
| 1339 std::string unprefixed; |
| 1340 if (!RemovePrefix(key, prefix, &unprefixed)) |
| 1341 break; |
| 1342 |
| 1343 int64_t resource_id; |
| 1344 status = ParseId(unprefixed, &resource_id); |
| 1345 if (status != STATUS_OK) |
| 1346 break; |
| 1347 |
| 1348 // Remove a resource record. |
| 1349 batch->Delete(key); |
| 1350 |
| 1351 // Currently resource sharing across versions and registrations is not |
| 1352 // supported, so we can purge this without caring about it. |
| 1353 PutPurgeableResourceIdToBatch(resource_id, batch); |
| 1354 newly_purgeable_resources->push_back(resource_id); |
1337 } | 1355 } |
1338 | |
1339 const std::string key = itr->key().ToString(); | |
1340 std::string unprefixed; | |
1341 if (!RemovePrefix(key, prefix, &unprefixed)) | |
1342 break; | |
1343 | |
1344 int64_t resource_id; | |
1345 status = ParseId(unprefixed, &resource_id); | |
1346 if (status != STATUS_OK) { | |
1347 HandleReadResult(FROM_HERE, status); | |
1348 return status; | |
1349 } | |
1350 | |
1351 // Remove a resource record. | |
1352 batch->Delete(key); | |
1353 | |
1354 // Currently resource sharing across versions and registrations is not | |
1355 // supported, so we can purge this without caring about it. | |
1356 PutPurgeableResourceIdToBatch(resource_id, batch); | |
1357 newly_purgeable_resources->push_back(resource_id); | |
1358 } | 1356 } |
1359 | 1357 |
1360 HandleReadResult(FROM_HERE, status); | 1358 HandleReadResult(FROM_HERE, status); |
1361 return status; | 1359 return status; |
1362 } | 1360 } |
1363 | 1361 |
1364 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceIds( | 1362 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceIds( |
1365 const char* id_key_prefix, | 1363 const char* id_key_prefix, |
1366 std::set<int64_t>* ids) { | 1364 std::set<int64_t>* ids) { |
1367 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 1365 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
1368 DCHECK(id_key_prefix); | 1366 DCHECK(id_key_prefix); |
1369 DCHECK(ids->empty()); | 1367 DCHECK(ids->empty()); |
1370 | 1368 |
1371 Status status = LazyOpen(false); | 1369 Status status = LazyOpen(false); |
1372 if (IsNewOrNonexistentDatabase(status)) | 1370 if (IsNewOrNonexistentDatabase(status)) |
1373 return STATUS_OK; | 1371 return STATUS_OK; |
1374 if (status != STATUS_OK) | 1372 if (status != STATUS_OK) |
1375 return status; | 1373 return status; |
1376 | 1374 |
1377 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 1375 { |
1378 for (itr->Seek(id_key_prefix); itr->Valid(); itr->Next()) { | 1376 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
1379 status = LevelDBStatusToStatus(itr->status()); | 1377 for (itr->Seek(id_key_prefix); itr->Valid(); itr->Next()) { |
1380 if (status != STATUS_OK) { | 1378 status = LevelDBStatusToStatus(itr->status()); |
1381 HandleReadResult(FROM_HERE, status); | 1379 if (status != STATUS_OK) { |
1382 ids->clear(); | 1380 ids->clear(); |
1383 return status; | 1381 break; |
| 1382 } |
| 1383 |
| 1384 std::string unprefixed; |
| 1385 if (!RemovePrefix(itr->key().ToString(), id_key_prefix, &unprefixed)) |
| 1386 break; |
| 1387 |
| 1388 int64_t resource_id; |
| 1389 status = ParseId(unprefixed, &resource_id); |
| 1390 if (status != STATUS_OK) { |
| 1391 ids->clear(); |
| 1392 break; |
| 1393 } |
| 1394 ids->insert(resource_id); |
1384 } | 1395 } |
1385 | |
1386 std::string unprefixed; | |
1387 if (!RemovePrefix(itr->key().ToString(), id_key_prefix, &unprefixed)) | |
1388 break; | |
1389 | |
1390 int64_t resource_id; | |
1391 status = ParseId(unprefixed, &resource_id); | |
1392 if (status != STATUS_OK) { | |
1393 HandleReadResult(FROM_HERE, status); | |
1394 ids->clear(); | |
1395 return status; | |
1396 } | |
1397 ids->insert(resource_id); | |
1398 } | 1396 } |
1399 | 1397 |
1400 HandleReadResult(FROM_HERE, status); | 1398 HandleReadResult(FROM_HERE, status); |
1401 return status; | 1399 return status; |
1402 } | 1400 } |
1403 | 1401 |
1404 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteResourceIdsInBatch( | 1402 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteResourceIdsInBatch( |
1405 const char* id_key_prefix, | 1403 const char* id_key_prefix, |
1406 const std::set<int64_t>& ids, | 1404 const std::set<int64_t>& ids, |
1407 leveldb::WriteBatch* batch) { | 1405 leveldb::WriteBatch* batch) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1445 } | 1443 } |
1446 | 1444 |
1447 ServiceWorkerDatabase::Status | 1445 ServiceWorkerDatabase::Status |
1448 ServiceWorkerDatabase::DeleteUserDataForRegistration( | 1446 ServiceWorkerDatabase::DeleteUserDataForRegistration( |
1449 int64_t registration_id, | 1447 int64_t registration_id, |
1450 leveldb::WriteBatch* batch) { | 1448 leveldb::WriteBatch* batch) { |
1451 DCHECK(batch); | 1449 DCHECK(batch); |
1452 Status status = STATUS_OK; | 1450 Status status = STATUS_OK; |
1453 const std::string prefix = CreateUserDataKeyPrefix(registration_id); | 1451 const std::string prefix = CreateUserDataKeyPrefix(registration_id); |
1454 | 1452 |
1455 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | 1453 { |
1456 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { | 1454 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); |
1457 status = LevelDBStatusToStatus(itr->status()); | 1455 for (itr->Seek(prefix); itr->Valid(); itr->Next()) { |
1458 if (status != STATUS_OK) { | 1456 status = LevelDBStatusToStatus(itr->status()); |
1459 HandleReadResult(FROM_HERE, status); | 1457 if (status != STATUS_OK) |
1460 return status; | 1458 break; |
| 1459 |
| 1460 const std::string key = itr->key().ToString(); |
| 1461 std::string user_data_name; |
| 1462 if (!RemovePrefix(key, prefix, &user_data_name)) |
| 1463 break; |
| 1464 batch->Delete(key); |
| 1465 batch->Delete(CreateHasUserDataKey(registration_id, user_data_name)); |
1461 } | 1466 } |
| 1467 } |
1462 | 1468 |
1463 const std::string key = itr->key().ToString(); | 1469 HandleReadResult(FROM_HERE, status); |
1464 std::string user_data_name; | |
1465 if (!RemovePrefix(key, prefix, &user_data_name)) | |
1466 break; | |
1467 batch->Delete(key); | |
1468 batch->Delete(CreateHasUserDataKey(registration_id, user_data_name)); | |
1469 } | |
1470 return status; | 1470 return status; |
1471 } | 1471 } |
1472 | 1472 |
1473 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadDatabaseVersion( | 1473 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadDatabaseVersion( |
1474 int64_t* db_version) { | 1474 int64_t* db_version) { |
1475 std::string value; | 1475 std::string value; |
1476 Status status = LevelDBStatusToStatus( | 1476 Status status = LevelDBStatusToStatus( |
1477 db_->Get(leveldb::ReadOptions(), kDatabaseVersionKey, &value)); | 1477 db_->Get(leveldb::ReadOptions(), kDatabaseVersionKey, &value)); |
1478 if (status == STATUS_ERROR_NOT_FOUND) { | 1478 if (status == STATUS_ERROR_NOT_FOUND) { |
1479 // The database hasn't been initialized yet. | 1479 // The database hasn't been initialized yet. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1585 if (status != STATUS_OK) | 1585 if (status != STATUS_OK) |
1586 Disable(from_here, status); | 1586 Disable(from_here, status); |
1587 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1587 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
1588 } | 1588 } |
1589 | 1589 |
1590 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { | 1590 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { |
1591 return path_.empty(); | 1591 return path_.empty(); |
1592 } | 1592 } |
1593 | 1593 |
1594 } // namespace content | 1594 } // namespace content |
OLD | NEW |