| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/service_worker/service_worker_register_job.h" |
| 6 |
| 7 #include "content/browser/service_worker/service_worker_registration.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "url/gurl.h" |
| 10 |
| 11 namespace content { |
| 12 |
| 13 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( |
| 14 const base::WeakPtr<ServiceWorkerStorage>& storage, |
| 15 const RegistrationCompleteCallback& callback) |
| 16 : storage_(storage), callback_(callback), weak_factory_(this) {} |
| 17 |
| 18 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {} |
| 19 |
| 20 void ServiceWorkerRegisterJob::StartRegister(const GURL& pattern, |
| 21 const GURL& script_url) { |
| 22 // Set up a chain of callbacks, in reverse order. Each of these |
| 23 // callbacks may be called asynchronously by the previous callback. |
| 24 ServiceWorkerStorage::RegistrationCallback finish_registration(base::Bind( |
| 25 &ServiceWorkerRegisterJob::RegisterComplete, weak_factory_.GetWeakPtr())); |
| 26 |
| 27 ServiceWorkerStorage::UnregistrationCallback register_new( |
| 28 base::Bind(&ServiceWorkerRegisterJob::RegisterPatternAndContinue, |
| 29 weak_factory_.GetWeakPtr(), |
| 30 pattern, |
| 31 script_url, |
| 32 finish_registration)); |
| 33 |
| 34 ServiceWorkerStorage::RegistrationCallback unregister_old( |
| 35 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, |
| 36 weak_factory_.GetWeakPtr(), |
| 37 pattern, |
| 38 script_url, |
| 39 register_new)); |
| 40 |
| 41 storage_->FindRegistrationForPattern(pattern, unregister_old); |
| 42 } |
| 43 |
| 44 void ServiceWorkerRegisterJob::StartUnregister(const GURL& pattern) { |
| 45 // Set up a chain of callbacks, in reverse order. Each of these |
| 46 // callbacks may be called asynchronously by the previous callback. |
| 47 ServiceWorkerStorage::UnregistrationCallback finish_unregistration( |
| 48 base::Bind(&ServiceWorkerRegisterJob::UnregisterComplete, |
| 49 weak_factory_.GetWeakPtr())); |
| 50 |
| 51 ServiceWorkerStorage::RegistrationCallback unregister( |
| 52 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, |
| 53 weak_factory_.GetWeakPtr(), |
| 54 pattern, |
| 55 GURL(), |
| 56 finish_unregistration)); |
| 57 |
| 58 storage_->FindRegistrationForPattern(pattern, unregister); |
| 59 } |
| 60 |
| 61 void ServiceWorkerRegisterJob::RegisterPatternAndContinue( |
| 62 const GURL& pattern, |
| 63 const GURL& script_url, |
| 64 const ServiceWorkerStorage::RegistrationCallback& callback, |
| 65 ServiceWorkerRegistrationStatus previous_status) { |
| 66 if (previous_status != REGISTRATION_OK && |
| 67 previous_status != REGISTRATION_NOT_FOUND) { |
| 68 BrowserThread::PostTask( |
| 69 BrowserThread::IO, |
| 70 FROM_HERE, |
| 71 base::Bind(callback, |
| 72 previous_status, |
| 73 scoped_refptr<ServiceWorkerRegistration>())); |
| 74 } |
| 75 |
| 76 // TODO: Eventually RegisterInternal will be replaced by an asynchronous |
| 77 // operation. Pass its resulting status through 'callback'. |
| 78 scoped_refptr<ServiceWorkerRegistration> registration = |
| 79 storage_->RegisterInternal(pattern, script_url); |
| 80 BrowserThread::PostTask(BrowserThread::IO, |
| 81 FROM_HERE, |
| 82 base::Bind(callback, REGISTRATION_OK, registration)); |
| 83 } |
| 84 |
| 85 void ServiceWorkerRegisterJob::UnregisterPatternAndContinue( |
| 86 const GURL& pattern, |
| 87 const GURL& new_script_url, |
| 88 const ServiceWorkerStorage::UnregistrationCallback& callback, |
| 89 ServiceWorkerRegistrationStatus previous_status, |
| 90 const scoped_refptr<ServiceWorkerRegistration>& previous_registration) { |
| 91 DCHECK(previous_status == REGISTRATION_OK || |
| 92 previous_status == REGISTRATION_NOT_FOUND); |
| 93 |
| 94 // The previous registration may not exist, which is ok. |
| 95 if (previous_status == REGISTRATION_OK && |
| 96 (new_script_url.is_empty() || |
| 97 previous_registration->script_url() != new_script_url)) { |
| 98 // TODO: Eventually UnregisterInternal will be replaced by an |
| 99 // asynchronous operation. Pass its resulting status though |
| 100 // 'callback'. |
| 101 storage_->UnregisterInternal(pattern); |
| 102 } |
| 103 BrowserThread::PostTask( |
| 104 BrowserThread::IO, FROM_HERE, base::Bind(callback, previous_status)); |
| 105 } |
| 106 |
| 107 void ServiceWorkerRegisterJob::UnregisterComplete( |
| 108 ServiceWorkerRegistrationStatus status) { |
| 109 callback_.Run(this, status, NULL); |
| 110 } |
| 111 |
| 112 void ServiceWorkerRegisterJob::RegisterComplete( |
| 113 ServiceWorkerRegistrationStatus status, |
| 114 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 115 callback_.Run(this, status, registration); |
| 116 } |
| 117 |
| 118 } // namespace content |
| OLD | NEW |