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