OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/child/background_sync/background_sync_provider.h" | 5 #include "content/child/background_sync/background_sync_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "content/child/background_sync/background_sync_type_converters.h" | 9 #include "content/child/background_sync/background_sync_type_converters.h" |
10 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 10 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 DCHECK(callbacks); | 86 DCHECK(callbacks); |
87 int64 service_worker_registration_id = | 87 int64 service_worker_registration_id = |
88 GetServiceWorkerRegistrationId(service_worker_registration); | 88 GetServiceWorkerRegistrationId(service_worker_registration); |
89 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks); | 89 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks); |
90 | 90 |
91 // base::Unretained is safe here, as the mojo channel will be deleted (and | 91 // base::Unretained is safe here, as the mojo channel will be deleted (and |
92 // will wipe its callbacks) before 'this' is deleted. | 92 // will wipe its callbacks) before 'this' is deleted. |
93 GetBackgroundSyncServicePtr()->GetRegistration( | 93 GetBackgroundSyncServicePtr()->GetRegistration( |
94 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), tag.utf8(), | 94 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), tag.utf8(), |
95 service_worker_registration_id, | 95 service_worker_registration_id, |
96 base::Bind(&BackgroundSyncProvider::RegisterCallback, | 96 base::Bind(&BackgroundSyncProvider::GetRegistrationCallback, |
97 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); | 97 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); |
98 } | 98 } |
99 | 99 |
100 void BackgroundSyncProvider::getRegistrations( | 100 void BackgroundSyncProvider::getRegistrations( |
101 blink::WebSyncRegistration::Periodicity periodicity, | 101 blink::WebSyncRegistration::Periodicity periodicity, |
102 blink::WebServiceWorkerRegistration* service_worker_registration, | 102 blink::WebServiceWorkerRegistration* service_worker_registration, |
103 blink::WebSyncGetRegistrationsCallbacks* callbacks) { | 103 blink::WebSyncGetRegistrationsCallbacks* callbacks) { |
104 DCHECK(service_worker_registration); | 104 DCHECK(service_worker_registration); |
105 DCHECK(callbacks); | 105 DCHECK(callbacks); |
106 int64 service_worker_registration_id = | 106 int64 service_worker_registration_id = |
107 GetServiceWorkerRegistrationId(service_worker_registration); | 107 GetServiceWorkerRegistrationId(service_worker_registration); |
108 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacksPtr(callbacks); | 108 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacksPtr(callbacks); |
109 | 109 |
110 // base::Unretained is safe here, as the mojo channel will be deleted (and | 110 // base::Unretained is safe here, as the mojo channel will be deleted (and |
111 // will wipe its callbacks) before 'this' is deleted. | 111 // will wipe its callbacks) before 'this' is deleted. |
112 GetBackgroundSyncServicePtr()->GetRegistrations( | 112 GetBackgroundSyncServicePtr()->GetRegistrations( |
113 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), | 113 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), |
114 service_worker_registration_id, | 114 service_worker_registration_id, |
115 base::Bind(&BackgroundSyncProvider::GetRegistrationsCallback, | 115 base::Bind(&BackgroundSyncProvider::GetRegistrationsCallback, |
116 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); | 116 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); |
117 } | 117 } |
118 | 118 |
119 void BackgroundSyncProvider::RegisterCallback( | 119 void BackgroundSyncProvider::RegisterCallback( |
120 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks, | 120 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks, |
| 121 BackgroundSyncError error, |
121 const SyncRegistrationPtr& options) { | 122 const SyncRegistrationPtr& options) { |
122 // TODO(iclelland): Pass through the various errors from the manager to here | 123 // TODO(iclelland): Determine the correct error message to return in each case |
123 // and handle them appropriately. | |
124 scoped_ptr<blink::WebSyncRegistration> result; | 124 scoped_ptr<blink::WebSyncRegistration> result; |
125 if (!options.is_null()) | 125 switch (error) { |
126 result = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options); | 126 case BACKGROUND_SYNC_ERROR_NONE: |
127 | 127 if (!options.is_null()) |
128 callbacks->onSuccess(result.release()); | 128 result = |
| 129 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options); |
| 130 callbacks->onSuccess(result.release()); |
| 131 break; |
| 132 case BACKGROUND_SYNC_ERROR_NOT_FOUND: |
| 133 NOTREACHED(); |
| 134 break; |
| 135 case BACKGROUND_SYNC_ERROR_STORAGE: |
| 136 callbacks->onError( |
| 137 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, |
| 138 "Background Sync is disabled.")); |
| 139 break; |
| 140 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER: |
| 141 callbacks->onError( |
| 142 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, |
| 143 "No service worker is active.")); |
| 144 break; |
| 145 } |
129 } | 146 } |
130 | 147 |
131 void BackgroundSyncProvider::UnregisterCallback( | 148 void BackgroundSyncProvider::UnregisterCallback( |
132 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks, | 149 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks, |
133 bool success) { | 150 BackgroundSyncError error) { |
134 // TODO(iclelland): Pass through the various errors from the manager to here | 151 // TODO(iclelland): Determine the correct error message to return in each case |
135 // and handle them appropriately. | 152 switch (error) { |
136 if (success) { | 153 case BACKGROUND_SYNC_ERROR_NONE: |
137 callbacks->onSuccess(new bool(success)); | 154 callbacks->onSuccess(new bool(true)); |
138 } else { | 155 break; |
139 callbacks->onError( | 156 case BACKGROUND_SYNC_ERROR_NOT_FOUND: |
140 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, | 157 callbacks->onSuccess(new bool(false)); |
141 "Sync registration does not exist")); | 158 break; |
| 159 case BACKGROUND_SYNC_ERROR_STORAGE: |
| 160 callbacks->onError( |
| 161 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, |
| 162 "Background Sync is disabled.")); |
| 163 break; |
| 164 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER: |
| 165 callbacks->onError( |
| 166 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, |
| 167 "No service worker is active.")); |
| 168 break; |
| 169 } |
| 170 } |
| 171 |
| 172 void BackgroundSyncProvider::GetRegistrationCallback( |
| 173 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks, |
| 174 BackgroundSyncError error, |
| 175 const SyncRegistrationPtr& options) { |
| 176 // TODO(iclelland): Determine the correct error message to return in each case |
| 177 scoped_ptr<blink::WebSyncRegistration> result; |
| 178 switch (error) { |
| 179 case BACKGROUND_SYNC_ERROR_NONE: |
| 180 if (!options.is_null()) |
| 181 result = |
| 182 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options); |
| 183 callbacks->onSuccess(result.release()); |
| 184 break; |
| 185 case BACKGROUND_SYNC_ERROR_NOT_FOUND: |
| 186 callbacks->onSuccess(nullptr); |
| 187 break; |
| 188 case BACKGROUND_SYNC_ERROR_STORAGE: |
| 189 callbacks->onError( |
| 190 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, |
| 191 "Background Sync is disabled.")); |
| 192 break; |
| 193 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER: |
| 194 callbacks->onError( |
| 195 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, |
| 196 "No service worker is active.")); |
| 197 break; |
142 } | 198 } |
143 } | 199 } |
144 | 200 |
145 void BackgroundSyncProvider::GetRegistrationsCallback( | 201 void BackgroundSyncProvider::GetRegistrationsCallback( |
146 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks, | 202 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks, |
| 203 BackgroundSyncError error, |
147 const mojo::Array<SyncRegistrationPtr>& registrations) { | 204 const mojo::Array<SyncRegistrationPtr>& registrations) { |
148 // TODO(iclelland): Pass through the various errors from the manager to here | 205 // TODO(iclelland): Determine the correct error message to return in each case |
149 // and handle them appropriately. | 206 blink::WebVector<blink::WebSyncRegistration*>* results; |
150 blink::WebVector<blink::WebSyncRegistration*>* results = | 207 switch (error) { |
151 new blink::WebVector<blink::WebSyncRegistration*>(registrations.size()); | 208 case BACKGROUND_SYNC_ERROR_NONE: |
152 for (size_t i = 0; i < registrations.size(); ++i) { | 209 results = new blink::WebVector<blink::WebSyncRegistration*>( |
153 (*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>( | 210 registrations.size()); |
154 registrations[i]).release(); | 211 for (size_t i = 0; i < registrations.size(); ++i) { |
| 212 (*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>( |
| 213 registrations[i]).release(); |
| 214 } |
| 215 callbacks->onSuccess(results); |
| 216 case BACKGROUND_SYNC_ERROR_NOT_FOUND: |
| 217 // This error should never be returned from |
| 218 // BackgroundSyncManager::GetRegistrations |
| 219 NOTREACHED(); |
| 220 break; |
| 221 case BACKGROUND_SYNC_ERROR_STORAGE: |
| 222 callbacks->onError( |
| 223 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, |
| 224 "Background Sync is disabled.")); |
| 225 break; |
| 226 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER: |
| 227 callbacks->onError( |
| 228 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, |
| 229 "No service worker is active.")); |
| 230 break; |
155 } | 231 } |
156 callbacks->onSuccess(results); | |
157 } | 232 } |
158 | 233 |
159 BackgroundSyncServicePtr& | 234 BackgroundSyncServicePtr& |
160 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { | 235 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { |
161 if (!background_sync_service_.get()) | 236 if (!background_sync_service_.get()) |
162 service_registry_->ConnectToRemoteService(&background_sync_service_); | 237 service_registry_->ConnectToRemoteService(&background_sync_service_); |
163 return background_sync_service_; | 238 return background_sync_service_; |
164 } | 239 } |
165 | 240 |
166 } // namespace content | 241 } // namespace content |
OLD | NEW |