OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_storage.h" | 5 #include "content/browser/service_worker/service_worker_storage.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 FROM_HERE, | 320 FROM_HERE, |
321 base::Bind( | 321 base::Bind( |
322 &FindForPatternInDB, | 322 &FindForPatternInDB, |
323 database_.get(), | 323 database_.get(), |
324 base::MessageLoopProxy::current(), | 324 base::MessageLoopProxy::current(), |
325 scope, | 325 scope, |
326 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForPattern, | 326 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForPattern, |
327 weak_factory_.GetWeakPtr(), scope, callback))); | 327 weak_factory_.GetWeakPtr(), scope, callback))); |
328 } | 328 } |
329 | 329 |
330 scoped_refptr<ServiceWorkerRegistration> | |
331 ServiceWorkerStorage::GetUninstallingRegistration(const GURL& scope) { | |
332 if (state_ != INITIALIZED || !context_) | |
333 return NULL; | |
334 for (RegistrationRefsById::const_iterator it = | |
335 uninstalling_registrations_.begin(); | |
336 it != uninstalling_registrations_.end(); | |
337 ++it) { | |
338 if (it->second->pattern() == scope) { | |
339 DCHECK(it->second->is_uninstalling()); | |
340 return it->second; | |
341 } | |
342 } | |
343 return NULL; | |
344 } | |
345 | |
330 void ServiceWorkerStorage::FindRegistrationForId( | 346 void ServiceWorkerStorage::FindRegistrationForId( |
331 int64 registration_id, | 347 int64 registration_id, |
332 const GURL& origin, | 348 const GURL& origin, |
333 const FindRegistrationCallback& callback) { | 349 const FindRegistrationCallback& callback) { |
334 if (!LazyInitialize(base::Bind( | 350 if (!LazyInitialize(base::Bind( |
335 &ServiceWorkerStorage::FindRegistrationForId, | 351 &ServiceWorkerStorage::FindRegistrationForId, |
336 weak_factory_.GetWeakPtr(), registration_id, origin, callback))) { | 352 weak_factory_.GetWeakPtr(), registration_id, origin, callback))) { |
337 if (state_ != INITIALIZING || !context_) { | 353 if (state_ != INITIALIZING || !context_) { |
338 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), | 354 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), |
339 SERVICE_WORKER_ERROR_FAILED, callback); | 355 SERVICE_WORKER_ERROR_FAILED, callback); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
411 return; | 427 return; |
412 } | 428 } |
413 | 429 |
414 ServiceWorkerDatabase::RegistrationData data; | 430 ServiceWorkerDatabase::RegistrationData data; |
415 data.registration_id = registration->id(); | 431 data.registration_id = registration->id(); |
416 data.scope = registration->pattern(); | 432 data.scope = registration->pattern(); |
417 data.script = registration->script_url(); | 433 data.script = registration->script_url(); |
418 data.has_fetch_handler = true; | 434 data.has_fetch_handler = true; |
419 data.version_id = version->version_id(); | 435 data.version_id = version->version_id(); |
420 data.last_update_check = base::Time::Now(); | 436 data.last_update_check = base::Time::Now(); |
421 data.is_active = false; // initially stored in the waiting state | 437 data.is_active = (version == registration->active_version()); |
422 | 438 |
423 ResourceList resources; | 439 ResourceList resources; |
424 version->script_cache_map()->GetResources(&resources); | 440 version->script_cache_map()->GetResources(&resources); |
425 | 441 |
426 if (!has_checked_for_stale_resources_) | 442 if (!has_checked_for_stale_resources_) |
427 DeleteStaleResources(); | 443 DeleteStaleResources(); |
428 | 444 |
429 database_task_runner_->PostTask( | 445 database_task_runner_->PostTask( |
430 FROM_HERE, | 446 FROM_HERE, |
431 base::Bind(&WriteRegistrationInDB, | 447 base::Bind(&WriteRegistrationInDB, |
432 database_.get(), | 448 database_.get(), |
433 base::MessageLoopProxy::current(), | 449 base::MessageLoopProxy::current(), |
434 data, resources, | 450 data, resources, |
435 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, | 451 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, |
436 weak_factory_.GetWeakPtr(), | 452 weak_factory_.GetWeakPtr(), |
437 callback))); | 453 callback))); |
454 | |
455 registration->set_is_deleted(false); | |
438 } | 456 } |
439 | 457 |
440 void ServiceWorkerStorage::UpdateToActiveState( | 458 void ServiceWorkerStorage::UpdateToActiveState( |
441 ServiceWorkerRegistration* registration, | 459 ServiceWorkerRegistration* registration, |
442 const StatusCallback& callback) { | 460 const StatusCallback& callback) { |
443 DCHECK(registration); | 461 DCHECK(registration); |
444 | 462 |
445 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; | 463 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; |
446 if (IsDisabled() || !context_) { | 464 if (IsDisabled() || !context_) { |
447 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 465 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
485 base::MessageLoopProxy::current(), | 503 base::MessageLoopProxy::current(), |
486 registration_id, origin, | 504 registration_id, origin, |
487 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration, | 505 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration, |
488 weak_factory_.GetWeakPtr(), params))); | 506 weak_factory_.GetWeakPtr(), params))); |
489 | 507 |
490 // The registration should no longer be findable. | 508 // The registration should no longer be findable. |
491 pending_deletions_.insert(registration_id); | 509 pending_deletions_.insert(registration_id); |
492 ServiceWorkerRegistration* registration = | 510 ServiceWorkerRegistration* registration = |
493 context_->GetLiveRegistration(registration_id); | 511 context_->GetLiveRegistration(registration_id); |
494 if (registration) | 512 if (registration) |
495 registration->set_is_deleted(); | 513 registration->set_is_deleted(true); |
496 } | 514 } |
497 | 515 |
498 scoped_ptr<ServiceWorkerResponseReader> | 516 scoped_ptr<ServiceWorkerResponseReader> |
499 ServiceWorkerStorage::CreateResponseReader(int64 response_id) { | 517 ServiceWorkerStorage::CreateResponseReader(int64 response_id) { |
500 return make_scoped_ptr( | 518 return make_scoped_ptr( |
501 new ServiceWorkerResponseReader(response_id, disk_cache())); | 519 new ServiceWorkerResponseReader(response_id, disk_cache())); |
502 } | 520 } |
503 | 521 |
504 scoped_ptr<ServiceWorkerResponseWriter> | 522 scoped_ptr<ServiceWorkerResponseWriter> |
505 ServiceWorkerStorage::CreateResponseWriter(int64 response_id) { | 523 ServiceWorkerStorage::CreateResponseWriter(int64 response_id) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
599 | 617 |
600 database_task_runner_->PostTask( | 618 database_task_runner_->PostTask( |
601 FROM_HERE, | 619 FROM_HERE, |
602 base::Bind(base::IgnoreResult( | 620 base::Bind(base::IgnoreResult( |
603 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), | 621 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), |
604 base::Unretained(database_.get()), | 622 base::Unretained(database_.get()), |
605 ids)); | 623 ids)); |
606 } | 624 } |
607 } | 625 } |
608 | 626 |
627 void ServiceWorkerStorage::NotifyUninstallingRegistration( | |
628 ServiceWorkerRegistration* registration) { | |
michaeln
2014/08/12 02:15:36
maybe a DCHECK to assert that reg is not already b
falken
2014/08/12 09:06:06
Done. Also added to the other Notify* functions fo
| |
629 uninstalling_registrations_[registration->id()] = registration; | |
630 | |
631 DeleteRegistration(registration->id(), | |
michaeln
2014/08/12 02:15:36
I was wondering where this method call went and wa
| |
632 registration->script_url().GetOrigin(), | |
633 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | |
634 } | |
635 | |
636 void ServiceWorkerStorage::NotifyAbortedUninstallingRegistration( | |
637 ServiceWorkerRegistration* registration) { | |
638 uninstalling_registrations_.erase(registration->id()); | |
639 installing_registrations_[registration->id()] = registration; | |
michaeln
2014/08/12 02:15:36
should this be a call to NotifyInstallingRegistrat
falken
2014/08/12 09:06:06
Done. I decided for symmetry to make SWRegistratio
| |
640 | |
641 if (registration->waiting_version()) { | |
642 StoreRegistration(registration, | |
643 registration->waiting_version(), | |
644 base::Bind(&ServiceWorkerStorage::DidRestoreRegistration, | |
645 weak_factory_.GetWeakPtr(), | |
646 registration->id())); | |
647 } else if (registration->active_version()) { | |
648 StoreRegistration(registration, | |
649 registration->active_version(), | |
650 base::Bind(&ServiceWorkerStorage::DidRestoreRegistration, | |
651 weak_factory_.GetWeakPtr(), | |
652 registration->id())); | |
653 } | |
654 registration->set_is_deleted(false); | |
655 } | |
656 | |
657 void ServiceWorkerStorage::NotifyUninstalledRegistration( | |
658 ServiceWorkerRegistration* registration) { | |
659 uninstalling_registrations_.erase(registration->id()); | |
660 } | |
661 | |
609 void ServiceWorkerStorage::Disable() { | 662 void ServiceWorkerStorage::Disable() { |
610 state_ = DISABLED; | 663 state_ = DISABLED; |
611 if (disk_cache_) | 664 if (disk_cache_) |
612 disk_cache_->Disable(); | 665 disk_cache_->Disable(); |
613 } | 666 } |
614 | 667 |
615 bool ServiceWorkerStorage::IsDisabled() const { | 668 bool ServiceWorkerStorage::IsDisabled() const { |
616 return state_ == DISABLED; | 669 return state_ == DISABLED; |
617 } | 670 } |
618 | 671 |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
874 callback.Run(DatabaseStatusToStatusCode(status)); | 927 callback.Run(DatabaseStatusToStatusCode(status)); |
875 return; | 928 return; |
876 } | 929 } |
877 registered_origins_.insert(origin); | 930 registered_origins_.insert(origin); |
878 callback.Run(SERVICE_WORKER_OK); | 931 callback.Run(SERVICE_WORKER_OK); |
879 | 932 |
880 if (!context_ || !context_->GetLiveVersion(deleted_version_id)) | 933 if (!context_ || !context_->GetLiveVersion(deleted_version_id)) |
881 StartPurgingResources(newly_purgeable_resources); | 934 StartPurgingResources(newly_purgeable_resources); |
882 } | 935 } |
883 | 936 |
937 void ServiceWorkerStorage::DidRestoreRegistration( | |
938 int64 registration_id, | |
939 ServiceWorkerStatusCode status) { | |
940 if (status == SERVICE_WORKER_OK) | |
941 installing_registrations_.erase(registration_id); | |
michaeln
2014/08/12 02:15:36
And this be a call to NotifyDoneInstallingRegistr
falken
2014/08/12 09:06:06
My idea actually was to leave it in the map, that
| |
942 } | |
943 | |
884 void ServiceWorkerStorage::DidUpdateToActiveState( | 944 void ServiceWorkerStorage::DidUpdateToActiveState( |
885 const StatusCallback& callback, | 945 const StatusCallback& callback, |
886 ServiceWorkerDatabase::Status status) { | 946 ServiceWorkerDatabase::Status status) { |
887 if (status != ServiceWorkerDatabase::STATUS_OK && | 947 if (status != ServiceWorkerDatabase::STATUS_OK && |
888 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 948 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
889 ScheduleDeleteAndStartOver(); | 949 ScheduleDeleteAndStartOver(); |
890 } | 950 } |
891 callback.Run(DatabaseStatusToStatusCode(status)); | 951 callback.Run(DatabaseStatusToStatusCode(status)); |
892 } | 952 } |
893 | 953 |
(...skipping 23 matching lines...) Expand all Loading... | |
917 const ResourceList& resources) { | 977 const ResourceList& resources) { |
918 scoped_refptr<ServiceWorkerRegistration> registration = | 978 scoped_refptr<ServiceWorkerRegistration> registration = |
919 context_->GetLiveRegistration(data.registration_id); | 979 context_->GetLiveRegistration(data.registration_id); |
920 if (registration) | 980 if (registration) |
921 return registration; | 981 return registration; |
922 | 982 |
923 registration = new ServiceWorkerRegistration( | 983 registration = new ServiceWorkerRegistration( |
924 data.scope, data.script, data.registration_id, context_); | 984 data.scope, data.script, data.registration_id, context_); |
925 if (pending_deletions_.find(data.registration_id) != | 985 if (pending_deletions_.find(data.registration_id) != |
926 pending_deletions_.end()) { | 986 pending_deletions_.end()) { |
927 registration->set_is_deleted(); | 987 registration->set_is_deleted(true); |
928 } | 988 } |
929 scoped_refptr<ServiceWorkerVersion> version = | 989 scoped_refptr<ServiceWorkerVersion> version = |
930 context_->GetLiveVersion(data.version_id); | 990 context_->GetLiveVersion(data.version_id); |
931 if (!version) { | 991 if (!version) { |
932 version = new ServiceWorkerVersion(registration, data.version_id, context_); | 992 version = new ServiceWorkerVersion(registration, data.version_id, context_); |
933 version->SetStatus(data.is_active ? | 993 version->SetStatus(data.is_active ? |
934 ServiceWorkerVersion::ACTIVATED : ServiceWorkerVersion::INSTALLED); | 994 ServiceWorkerVersion::ACTIVATED : ServiceWorkerVersion::INSTALLED); |
935 version->script_cache_map()->SetResources(resources); | 995 version->script_cache_map()->SetResources(resources); |
936 } | 996 } |
937 | 997 |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1346 // Give up the corruption recovery until the browser restarts. | 1406 // Give up the corruption recovery until the browser restarts. |
1347 LOG(ERROR) << "Failed to delete the diskcache."; | 1407 LOG(ERROR) << "Failed to delete the diskcache."; |
1348 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1408 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
1349 return; | 1409 return; |
1350 } | 1410 } |
1351 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1411 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
1352 callback.Run(SERVICE_WORKER_OK); | 1412 callback.Run(SERVICE_WORKER_OK); |
1353 } | 1413 } |
1354 | 1414 |
1355 } // namespace content | 1415 } // namespace content |
OLD | NEW |