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