| 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 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 FROM_HERE, | 322 FROM_HERE, |
| 323 base::Bind( | 323 base::Bind( |
| 324 &FindForPatternInDB, | 324 &FindForPatternInDB, |
| 325 database_.get(), | 325 database_.get(), |
| 326 base::MessageLoopProxy::current(), | 326 base::MessageLoopProxy::current(), |
| 327 scope, | 327 scope, |
| 328 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForPattern, | 328 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForPattern, |
| 329 weak_factory_.GetWeakPtr(), scope, callback))); | 329 weak_factory_.GetWeakPtr(), scope, callback))); |
| 330 } | 330 } |
| 331 | 331 |
| 332 scoped_refptr<ServiceWorkerRegistration> |
| 333 ServiceWorkerStorage::GetUninstallingRegistration(const GURL& scope) { |
| 334 if (state_ != INITIALIZED || !context_) |
| 335 return NULL; |
| 336 for (RegistrationRefsById::const_iterator it = |
| 337 uninstalling_registrations_.begin(); |
| 338 it != uninstalling_registrations_.end(); |
| 339 ++it) { |
| 340 if (it->second->pattern() == scope) { |
| 341 DCHECK(it->second->is_uninstalling()); |
| 342 return it->second; |
| 343 } |
| 344 } |
| 345 return NULL; |
| 346 } |
| 347 |
| 332 void ServiceWorkerStorage::FindRegistrationForId( | 348 void ServiceWorkerStorage::FindRegistrationForId( |
| 333 int64 registration_id, | 349 int64 registration_id, |
| 334 const GURL& origin, | 350 const GURL& origin, |
| 335 const FindRegistrationCallback& callback) { | 351 const FindRegistrationCallback& callback) { |
| 336 if (!LazyInitialize(base::Bind( | 352 if (!LazyInitialize(base::Bind( |
| 337 &ServiceWorkerStorage::FindRegistrationForId, | 353 &ServiceWorkerStorage::FindRegistrationForId, |
| 338 weak_factory_.GetWeakPtr(), registration_id, origin, callback))) { | 354 weak_factory_.GetWeakPtr(), registration_id, origin, callback))) { |
| 339 if (state_ != INITIALIZING || !context_) { | 355 if (state_ != INITIALIZING || !context_) { |
| 340 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), | 356 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), |
| 341 SERVICE_WORKER_ERROR_FAILED, callback); | 357 SERVICE_WORKER_ERROR_FAILED, callback); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 return; | 429 return; |
| 414 } | 430 } |
| 415 | 431 |
| 416 ServiceWorkerDatabase::RegistrationData data; | 432 ServiceWorkerDatabase::RegistrationData data; |
| 417 data.registration_id = registration->id(); | 433 data.registration_id = registration->id(); |
| 418 data.scope = registration->pattern(); | 434 data.scope = registration->pattern(); |
| 419 data.script = registration->script_url(); | 435 data.script = registration->script_url(); |
| 420 data.has_fetch_handler = true; | 436 data.has_fetch_handler = true; |
| 421 data.version_id = version->version_id(); | 437 data.version_id = version->version_id(); |
| 422 data.last_update_check = base::Time::Now(); | 438 data.last_update_check = base::Time::Now(); |
| 423 data.is_active = false; // initially stored in the waiting state | 439 data.is_active = (version == registration->active_version()); |
| 424 | 440 |
| 425 ResourceList resources; | 441 ResourceList resources; |
| 426 version->script_cache_map()->GetResources(&resources); | 442 version->script_cache_map()->GetResources(&resources); |
| 427 | 443 |
| 428 if (!has_checked_for_stale_resources_) | 444 if (!has_checked_for_stale_resources_) |
| 429 DeleteStaleResources(); | 445 DeleteStaleResources(); |
| 430 | 446 |
| 431 database_task_runner_->PostTask( | 447 database_task_runner_->PostTask( |
| 432 FROM_HERE, | 448 FROM_HERE, |
| 433 base::Bind(&WriteRegistrationInDB, | 449 base::Bind(&WriteRegistrationInDB, |
| 434 database_.get(), | 450 database_.get(), |
| 435 base::MessageLoopProxy::current(), | 451 base::MessageLoopProxy::current(), |
| 436 data, resources, | 452 data, resources, |
| 437 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, | 453 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, |
| 438 weak_factory_.GetWeakPtr(), | 454 weak_factory_.GetWeakPtr(), |
| 439 callback))); | 455 callback))); |
| 456 |
| 457 registration->set_is_deleted(false); |
| 440 } | 458 } |
| 441 | 459 |
| 442 void ServiceWorkerStorage::UpdateToActiveState( | 460 void ServiceWorkerStorage::UpdateToActiveState( |
| 443 ServiceWorkerRegistration* registration, | 461 ServiceWorkerRegistration* registration, |
| 444 const StatusCallback& callback) { | 462 const StatusCallback& callback) { |
| 445 DCHECK(registration); | 463 DCHECK(registration); |
| 446 | 464 |
| 447 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; | 465 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; |
| 448 if (IsDisabled() || !context_) { | 466 if (IsDisabled() || !context_) { |
| 449 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 467 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 base::MessageLoopProxy::current(), | 505 base::MessageLoopProxy::current(), |
| 488 registration_id, origin, | 506 registration_id, origin, |
| 489 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration, | 507 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration, |
| 490 weak_factory_.GetWeakPtr(), params))); | 508 weak_factory_.GetWeakPtr(), params))); |
| 491 | 509 |
| 492 // The registration should no longer be findable. | 510 // The registration should no longer be findable. |
| 493 pending_deletions_.insert(registration_id); | 511 pending_deletions_.insert(registration_id); |
| 494 ServiceWorkerRegistration* registration = | 512 ServiceWorkerRegistration* registration = |
| 495 context_->GetLiveRegistration(registration_id); | 513 context_->GetLiveRegistration(registration_id); |
| 496 if (registration) | 514 if (registration) |
| 497 registration->set_is_deleted(); | 515 registration->set_is_deleted(true); |
| 498 } | 516 } |
| 499 | 517 |
| 500 scoped_ptr<ServiceWorkerResponseReader> | 518 scoped_ptr<ServiceWorkerResponseReader> |
| 501 ServiceWorkerStorage::CreateResponseReader(int64 response_id) { | 519 ServiceWorkerStorage::CreateResponseReader(int64 response_id) { |
| 502 return make_scoped_ptr( | 520 return make_scoped_ptr( |
| 503 new ServiceWorkerResponseReader(response_id, disk_cache())); | 521 new ServiceWorkerResponseReader(response_id, disk_cache())); |
| 504 } | 522 } |
| 505 | 523 |
| 506 scoped_ptr<ServiceWorkerResponseWriter> | 524 scoped_ptr<ServiceWorkerResponseWriter> |
| 507 ServiceWorkerStorage::CreateResponseWriter(int64 response_id) { | 525 ServiceWorkerStorage::CreateResponseWriter(int64 response_id) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 | 619 |
| 602 database_task_runner_->PostTask( | 620 database_task_runner_->PostTask( |
| 603 FROM_HERE, | 621 FROM_HERE, |
| 604 base::Bind(base::IgnoreResult( | 622 base::Bind(base::IgnoreResult( |
| 605 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), | 623 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), |
| 606 base::Unretained(database_.get()), | 624 base::Unretained(database_.get()), |
| 607 ids)); | 625 ids)); |
| 608 } | 626 } |
| 609 } | 627 } |
| 610 | 628 |
| 629 void ServiceWorkerStorage::NotifyUninstallingRegistration( |
| 630 ServiceWorkerRegistration* registration) { |
| 631 uninstalling_registrations_[registration->id()] = registration; |
| 632 |
| 633 DeleteRegistration(registration->id(), |
| 634 registration->script_url().GetOrigin(), |
| 635 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 636 } |
| 637 |
| 638 void ServiceWorkerStorage::NotifyAbortedUninstallingRegistration( |
| 639 ServiceWorkerRegistration* registration) { |
| 640 uninstalling_registrations_.erase(registration->id()); |
| 641 installing_registrations_[registration->id()] = registration; |
| 642 |
| 643 if (registration->waiting_version()) { |
| 644 StoreRegistration(registration, |
| 645 registration->waiting_version(), |
| 646 base::Bind(&ServiceWorkerStorage::DidRestoreRegistration, |
| 647 weak_factory_.GetWeakPtr(), |
| 648 registration->id())); |
| 649 } else if (registration->active_version()) { |
| 650 StoreRegistration(registration, |
| 651 registration->active_version(), |
| 652 base::Bind(&ServiceWorkerStorage::DidRestoreRegistration, |
| 653 weak_factory_.GetWeakPtr(), |
| 654 registration->id())); |
| 655 } |
| 656 registration->set_is_deleted(false); |
| 657 } |
| 658 |
| 659 void ServiceWorkerStorage::NotifyUninstalledRegistration( |
| 660 ServiceWorkerRegistration* registration) { |
| 661 uninstalling_registrations_.erase(registration->id()); |
| 662 } |
| 663 |
| 611 void ServiceWorkerStorage::Disable() { | 664 void ServiceWorkerStorage::Disable() { |
| 612 state_ = DISABLED; | 665 state_ = DISABLED; |
| 613 if (disk_cache_) | 666 if (disk_cache_) |
| 614 disk_cache_->Disable(); | 667 disk_cache_->Disable(); |
| 615 } | 668 } |
| 616 | 669 |
| 617 bool ServiceWorkerStorage::IsDisabled() const { | 670 bool ServiceWorkerStorage::IsDisabled() const { |
| 618 return state_ == DISABLED; | 671 return state_ == DISABLED; |
| 619 } | 672 } |
| 620 | 673 |
| (...skipping 253 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); |
| 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 |