| 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 ServiceWorkerRegistration* ServiceWorkerStorage::GetUninstallingRegistration( |
| 331 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 | 592 |
| 575 int64 ServiceWorkerStorage::NewResourceId() { | 593 int64 ServiceWorkerStorage::NewResourceId() { |
| 576 if (state_ == DISABLED) | 594 if (state_ == DISABLED) |
| 577 return kInvalidServiceWorkerResourceId; | 595 return kInvalidServiceWorkerResourceId; |
| 578 DCHECK_EQ(INITIALIZED, state_); | 596 DCHECK_EQ(INITIALIZED, state_); |
| 579 return next_resource_id_++; | 597 return next_resource_id_++; |
| 580 } | 598 } |
| 581 | 599 |
| 582 void ServiceWorkerStorage::NotifyInstallingRegistration( | 600 void ServiceWorkerStorage::NotifyInstallingRegistration( |
| 583 ServiceWorkerRegistration* registration) { | 601 ServiceWorkerRegistration* registration) { |
| 602 DCHECK(installing_registrations_.find(registration->id()) == |
| 603 installing_registrations_.end()); |
| 584 installing_registrations_[registration->id()] = registration; | 604 installing_registrations_[registration->id()] = registration; |
| 585 } | 605 } |
| 586 | 606 |
| 587 void ServiceWorkerStorage::NotifyDoneInstallingRegistration( | 607 void ServiceWorkerStorage::NotifyDoneInstallingRegistration( |
| 588 ServiceWorkerRegistration* registration, | 608 ServiceWorkerRegistration* registration, |
| 589 ServiceWorkerVersion* version, | 609 ServiceWorkerVersion* version, |
| 590 ServiceWorkerStatusCode status) { | 610 ServiceWorkerStatusCode status) { |
| 591 installing_registrations_.erase(registration->id()); | 611 installing_registrations_.erase(registration->id()); |
| 592 if (status != SERVICE_WORKER_OK && version) { | 612 if (status != SERVICE_WORKER_OK && version) { |
| 593 ResourceList resources; | 613 ResourceList resources; |
| 594 version->script_cache_map()->GetResources(&resources); | 614 version->script_cache_map()->GetResources(&resources); |
| 595 | 615 |
| 596 std::set<int64> ids; | 616 std::set<int64> ids; |
| 597 for (size_t i = 0; i < resources.size(); ++i) | 617 for (size_t i = 0; i < resources.size(); ++i) |
| 598 ids.insert(resources[i].resource_id); | 618 ids.insert(resources[i].resource_id); |
| 599 | 619 |
| 600 database_task_runner_->PostTask( | 620 database_task_runner_->PostTask( |
| 601 FROM_HERE, | 621 FROM_HERE, |
| 602 base::Bind(base::IgnoreResult( | 622 base::Bind(base::IgnoreResult( |
| 603 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), | 623 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), |
| 604 base::Unretained(database_.get()), | 624 base::Unretained(database_.get()), |
| 605 ids)); | 625 ids)); |
| 606 } | 626 } |
| 607 } | 627 } |
| 608 | 628 |
| 629 void ServiceWorkerStorage::NotifyUninstallingRegistration( |
| 630 ServiceWorkerRegistration* registration) { |
| 631 DCHECK(uninstalling_registrations_.find(registration->id()) == |
| 632 uninstalling_registrations_.end()); |
| 633 uninstalling_registrations_[registration->id()] = registration; |
| 634 } |
| 635 |
| 636 void ServiceWorkerStorage::NotifyDoneUninstallingRegistration( |
| 637 ServiceWorkerRegistration* registration) { |
| 638 uninstalling_registrations_.erase(registration->id()); |
| 639 } |
| 640 |
| 609 void ServiceWorkerStorage::Disable() { | 641 void ServiceWorkerStorage::Disable() { |
| 610 state_ = DISABLED; | 642 state_ = DISABLED; |
| 611 if (disk_cache_) | 643 if (disk_cache_) |
| 612 disk_cache_->Disable(); | 644 disk_cache_->Disable(); |
| 613 } | 645 } |
| 614 | 646 |
| 615 bool ServiceWorkerStorage::IsDisabled() const { | 647 bool ServiceWorkerStorage::IsDisabled() const { |
| 616 return state_ == DISABLED; | 648 return state_ == DISABLED; |
| 617 } | 649 } |
| 618 | 650 |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 917 const ResourceList& resources) { | 949 const ResourceList& resources) { |
| 918 scoped_refptr<ServiceWorkerRegistration> registration = | 950 scoped_refptr<ServiceWorkerRegistration> registration = |
| 919 context_->GetLiveRegistration(data.registration_id); | 951 context_->GetLiveRegistration(data.registration_id); |
| 920 if (registration) | 952 if (registration) |
| 921 return registration; | 953 return registration; |
| 922 | 954 |
| 923 registration = new ServiceWorkerRegistration( | 955 registration = new ServiceWorkerRegistration( |
| 924 data.scope, data.script, data.registration_id, context_); | 956 data.scope, data.script, data.registration_id, context_); |
| 925 if (pending_deletions_.find(data.registration_id) != | 957 if (pending_deletions_.find(data.registration_id) != |
| 926 pending_deletions_.end()) { | 958 pending_deletions_.end()) { |
| 927 registration->set_is_deleted(); | 959 registration->set_is_deleted(true); |
| 928 } | 960 } |
| 929 scoped_refptr<ServiceWorkerVersion> version = | 961 scoped_refptr<ServiceWorkerVersion> version = |
| 930 context_->GetLiveVersion(data.version_id); | 962 context_->GetLiveVersion(data.version_id); |
| 931 if (!version) { | 963 if (!version) { |
| 932 version = new ServiceWorkerVersion(registration, data.version_id, context_); | 964 version = new ServiceWorkerVersion(registration, data.version_id, context_); |
| 933 version->SetStatus(data.is_active ? | 965 version->SetStatus(data.is_active ? |
| 934 ServiceWorkerVersion::ACTIVATED : ServiceWorkerVersion::INSTALLED); | 966 ServiceWorkerVersion::ACTIVATED : ServiceWorkerVersion::INSTALLED); |
| 935 version->script_cache_map()->SetResources(resources); | 967 version->script_cache_map()->SetResources(resources); |
| 936 } | 968 } |
| 937 | 969 |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1346 // Give up the corruption recovery until the browser restarts. | 1378 // Give up the corruption recovery until the browser restarts. |
| 1347 LOG(ERROR) << "Failed to delete the diskcache."; | 1379 LOG(ERROR) << "Failed to delete the diskcache."; |
| 1348 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1380 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 1349 return; | 1381 return; |
| 1350 } | 1382 } |
| 1351 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1383 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
| 1352 callback.Run(SERVICE_WORKER_OK); | 1384 callback.Run(SERVICE_WORKER_OK); |
| 1353 } | 1385 } |
| 1354 | 1386 |
| 1355 } // namespace content | 1387 } // namespace content |
| OLD | NEW |