| 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/browser/background_sync/background_sync_manager.h" | 5 #include "content/browser/background_sync/background_sync_manager.h" |
| 6 | 6 |
| 7 #include "base/barrier_closure.h" | 7 #include "base/barrier_closure.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "content/browser/background_sync/background_sync.pb.h" | 9 #include "content/browser/background_sync/background_sync.pb.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 // static | 39 // static |
| 40 scoped_ptr<BackgroundSyncManager> BackgroundSyncManager::Create( | 40 scoped_ptr<BackgroundSyncManager> BackgroundSyncManager::Create( |
| 41 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) { | 41 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) { |
| 42 BackgroundSyncManager* sync_manager = | 42 BackgroundSyncManager* sync_manager = |
| 43 new BackgroundSyncManager(service_worker_context); | 43 new BackgroundSyncManager(service_worker_context); |
| 44 sync_manager->Init(); | 44 sync_manager->Init(); |
| 45 return make_scoped_ptr(sync_manager); | 45 return make_scoped_ptr(sync_manager); |
| 46 } | 46 } |
| 47 | 47 |
| 48 BackgroundSyncManager::~BackgroundSyncManager() { | 48 BackgroundSyncManager::~BackgroundSyncManager() { |
| 49 service_worker_context_->RemoveObserver(this); |
| 49 } | 50 } |
| 50 | 51 |
| 51 void BackgroundSyncManager::Register( | 52 void BackgroundSyncManager::Register( |
| 52 const GURL& origin, | 53 const GURL& origin, |
| 53 int64 sw_registration_id, | 54 int64 sw_registration_id, |
| 54 const BackgroundSyncRegistration& sync_registration, | 55 const BackgroundSyncRegistration& sync_registration, |
| 55 const StatusAndRegistrationCallback& callback) { | 56 const StatusAndRegistrationCallback& callback) { |
| 56 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 57 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 57 DCHECK_EQ(BackgroundSyncRegistration::kInvalidRegistrationId, | 58 DCHECK_EQ(BackgroundSyncRegistration::kInvalidRegistrationId, |
| 58 sync_registration.id); | 59 sync_registration.id); |
| 59 | 60 |
| 60 StatusAndRegistrationCallback pending_callback = | 61 if (disabled_) { |
| 61 base::Bind(&BackgroundSyncManager::PendingStatusAndRegistrationCallback, | 62 base::MessageLoop::current()->PostTask( |
| 62 weak_ptr_factory_.GetWeakPtr(), callback); | 63 FROM_HERE, |
| 64 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 65 return; |
| 66 } |
| 63 | 67 |
| 64 op_scheduler_.ScheduleOperation(base::Bind( | 68 op_scheduler_.ScheduleOperation(base::Bind( |
| 65 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), | 69 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 66 origin, sw_registration_id, sync_registration, pending_callback)); | 70 origin, sw_registration_id, sync_registration, |
| 71 MakeStatusAndRegistrationCompletion(callback))); |
| 67 } | 72 } |
| 68 | 73 |
| 69 void BackgroundSyncManager::Unregister( | 74 void BackgroundSyncManager::Unregister( |
| 70 const GURL& origin, | 75 const GURL& origin, |
| 71 int64 sw_registration_id, | 76 int64 sw_registration_id, |
| 72 const std::string& sync_registration_name, | 77 const std::string& sync_registration_name, |
| 73 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 78 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 74 const StatusCallback& callback) { | 79 const StatusCallback& callback) { |
| 75 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 76 | 81 |
| 77 StatusCallback pending_callback = | 82 if (disabled_) { |
| 78 base::Bind(&BackgroundSyncManager::PendingStatusCallback, | 83 base::MessageLoop::current()->PostTask( |
| 79 weak_ptr_factory_.GetWeakPtr(), callback); | 84 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 85 return; |
| 86 } |
| 80 | 87 |
| 81 op_scheduler_.ScheduleOperation(base::Bind( | 88 op_scheduler_.ScheduleOperation(base::Bind( |
| 82 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | 89 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 83 origin, sw_registration_id, sync_registration_name, sync_registration_id, | 90 origin, sw_registration_id, sync_registration_name, sync_registration_id, |
| 84 pending_callback)); | 91 MakeStatusCompletion(callback))); |
| 85 } | 92 } |
| 86 | 93 |
| 87 void BackgroundSyncManager::GetRegistration( | 94 void BackgroundSyncManager::GetRegistration( |
| 88 const GURL& origin, | 95 const GURL& origin, |
| 89 int64 sw_registration_id, | 96 int64 sw_registration_id, |
| 90 const std::string sync_registration_name, | 97 const std::string sync_registration_name, |
| 91 const StatusAndRegistrationCallback& callback) { | 98 const StatusAndRegistrationCallback& callback) { |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 99 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 93 | 100 |
| 94 StatusAndRegistrationCallback pending_callback = | 101 if (disabled_) { |
| 95 base::Bind(&BackgroundSyncManager::PendingStatusAndRegistrationCallback, | 102 base::MessageLoop::current()->PostTask( |
| 96 weak_ptr_factory_.GetWeakPtr(), callback); | 103 FROM_HERE, |
| 104 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 105 return; |
| 106 } |
| 97 | 107 |
| 108 op_scheduler_.ScheduleOperation(base::Bind( |
| 109 &BackgroundSyncManager::GetRegistrationImpl, |
| 110 weak_ptr_factory_.GetWeakPtr(), origin, sw_registration_id, |
| 111 sync_registration_name, MakeStatusAndRegistrationCompletion(callback))); |
| 112 } |
| 113 |
| 114 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, |
| 115 const GURL& pattern) { |
| 116 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 117 |
| 118 // Operations already in the queue will either fail when they write to storage |
| 119 // or return stale results based on registrations loaded in memory. This is |
| 120 // inconsequential since the service worker is gone. |
| 121 op_scheduler_.ScheduleOperation(base::Bind( |
| 122 &BackgroundSyncManager::OnRegistrationDeletedImpl, |
| 123 weak_ptr_factory_.GetWeakPtr(), registration_id, MakeEmptyCompletion())); |
| 124 } |
| 125 |
| 126 void BackgroundSyncManager::OnStorageWiped() { |
| 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 128 // Operations already in the queue will either fail when they write to storage |
| 129 // or return stale results based on registrations loaded in memory. This is |
| 130 // inconsequential since the service workers are gone. |
| 98 op_scheduler_.ScheduleOperation( | 131 op_scheduler_.ScheduleOperation( |
| 99 base::Bind(&BackgroundSyncManager::GetRegistrationImpl, | 132 base::Bind(&BackgroundSyncManager::OnStorageWipedImpl, |
| 100 weak_ptr_factory_.GetWeakPtr(), origin, sw_registration_id, | 133 weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion())); |
| 101 sync_registration_name, pending_callback)); | |
| 102 } | 134 } |
| 103 | 135 |
| 104 BackgroundSyncManager::BackgroundSyncManager( | 136 BackgroundSyncManager::BackgroundSyncManager( |
| 105 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 137 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) |
| 106 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { | 138 : service_worker_context_(service_worker_context), |
| 139 disabled_(false), |
| 140 weak_ptr_factory_(this) { |
| 141 service_worker_context_->AddObserver(this); |
| 107 } | 142 } |
| 108 | 143 |
| 109 void BackgroundSyncManager::Init() { | 144 void BackgroundSyncManager::Init() { |
| 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 145 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 111 DCHECK(!op_scheduler_.ScheduledOperations()); | 146 DCHECK(!op_scheduler_.ScheduledOperations()); |
| 147 DCHECK(!disabled_); |
| 112 | 148 |
| 113 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, | 149 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, |
| 114 weak_ptr_factory_.GetWeakPtr())); | 150 weak_ptr_factory_.GetWeakPtr(), |
| 151 MakeEmptyCompletion())); |
| 115 } | 152 } |
| 116 | 153 |
| 117 void BackgroundSyncManager::InitImpl() { | 154 void BackgroundSyncManager::InitImpl(const base::Closure& callback) { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 155 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 119 | 156 |
| 157 if (disabled_) { |
| 158 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); |
| 159 return; |
| 160 } |
| 161 |
| 120 GetDataFromBackend( | 162 GetDataFromBackend( |
| 121 kBackgroundSyncUserDataKey, | 163 kBackgroundSyncUserDataKey, |
| 122 base::Bind(&BackgroundSyncManager::InitDidGetDataFromBackend, | 164 base::Bind(&BackgroundSyncManager::InitDidGetDataFromBackend, |
| 123 weak_ptr_factory_.GetWeakPtr())); | 165 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 124 } | 166 } |
| 125 | 167 |
| 126 void BackgroundSyncManager::InitDidGetDataFromBackend( | 168 void BackgroundSyncManager::InitDidGetDataFromBackend( |
| 169 const base::Closure& callback, |
| 127 const std::vector<std::pair<int64, std::string>>& user_data, | 170 const std::vector<std::pair<int64, std::string>>& user_data, |
| 128 ServiceWorkerStatusCode status) { | 171 ServiceWorkerStatusCode status) { |
| 129 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) | 172 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 130 LOG(ERROR) << "Background Sync Failed to load from backend."; | 173 LOG(ERROR) << "BackgroundSync failed to init due to backend failure."; |
| 174 DisableAndClearManager(base::Bind(callback)); |
| 175 return; |
| 176 } |
| 131 | 177 |
| 132 bool corruption_detected = false; | 178 bool corruption_detected = false; |
| 133 for (const std::pair<int64, std::string>& data : user_data) { | 179 for (const std::pair<int64, std::string>& data : user_data) { |
| 134 BackgroundSyncRegistrationsProto registrations_proto; | 180 BackgroundSyncRegistrationsProto registrations_proto; |
| 135 if (registrations_proto.ParseFromString(data.second)) { | 181 if (registrations_proto.ParseFromString(data.second)) { |
| 136 sw_to_registrations_map_[data.first] = BackgroundSyncRegistrations( | 182 sw_to_registrations_map_[data.first] = BackgroundSyncRegistrations( |
| 137 registrations_proto.next_registration_id()); | 183 registrations_proto.next_registration_id()); |
| 138 BackgroundSyncRegistrations* registrations = | 184 BackgroundSyncRegistrations* registrations = |
| 139 &sw_to_registrations_map_[data.first]; | 185 &sw_to_registrations_map_[data.first]; |
| 140 | 186 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 156 registration; | 202 registration; |
| 157 } | 203 } |
| 158 } | 204 } |
| 159 | 205 |
| 160 if (corruption_detected) | 206 if (corruption_detected) |
| 161 break; | 207 break; |
| 162 } | 208 } |
| 163 | 209 |
| 164 if (corruption_detected) { | 210 if (corruption_detected) { |
| 165 LOG(ERROR) << "Corruption detected in background sync backend"; | 211 LOG(ERROR) << "Corruption detected in background sync backend"; |
| 166 sw_to_registrations_map_.clear(); | 212 DisableAndClearManager(base::Bind(callback)); |
| 213 return; |
| 167 } | 214 } |
| 168 | 215 |
| 169 // TODO(jkarlin): Call the scheduling algorithm here. | 216 // TODO(jkarlin): Call the scheduling algorithm here. |
| 170 | 217 |
| 171 op_scheduler_.CompleteOperationAndRunNext(); | 218 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); |
| 172 } | 219 } |
| 173 | 220 |
| 174 void BackgroundSyncManager::RegisterImpl( | 221 void BackgroundSyncManager::RegisterImpl( |
| 175 const GURL& origin, | 222 const GURL& origin, |
| 176 int64 sw_registration_id, | 223 int64 sw_registration_id, |
| 177 const BackgroundSyncRegistration& sync_registration, | 224 const BackgroundSyncRegistration& sync_registration, |
| 178 const StatusAndRegistrationCallback& callback) { | 225 const StatusAndRegistrationCallback& callback) { |
| 226 if (disabled_) { |
| 227 base::MessageLoop::current()->PostTask( |
| 228 FROM_HERE, |
| 229 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 230 return; |
| 231 } |
| 232 |
| 179 BackgroundSyncRegistration existing_registration; | 233 BackgroundSyncRegistration existing_registration; |
| 180 if (LookupRegistration(sw_registration_id, sync_registration.name, | 234 if (LookupRegistration(sw_registration_id, sync_registration.name, |
| 181 &existing_registration)) { | 235 &existing_registration)) { |
| 182 if (existing_registration.Equals(sync_registration)) { | 236 if (existing_registration.Equals(sync_registration)) { |
| 183 base::MessageLoop::current()->PostTask( | 237 base::MessageLoop::current()->PostTask( |
| 184 FROM_HERE, | 238 FROM_HERE, |
| 185 base::Bind(callback, ERROR_TYPE_OK, existing_registration)); | 239 base::Bind(callback, ERROR_TYPE_OK, existing_registration)); |
| 186 return; | 240 return; |
| 187 } | 241 } |
| 188 } | 242 } |
| 189 | 243 |
| 190 BackgroundSyncRegistration new_registration = sync_registration; | 244 BackgroundSyncRegistration new_registration = sync_registration; |
| 191 BackgroundSyncRegistrations* registrations = | 245 BackgroundSyncRegistrations* registrations = |
| 192 &sw_to_registrations_map_[sw_registration_id]; | 246 &sw_to_registrations_map_[sw_registration_id]; |
| 193 new_registration.id = registrations->next_id++; | 247 new_registration.id = registrations->next_id++; |
| 194 | 248 |
| 195 AddRegistrationToMap(sw_registration_id, new_registration); | 249 AddRegistrationToMap(sw_registration_id, new_registration); |
| 196 | 250 |
| 197 StoreRegistrations( | 251 StoreRegistrations( |
| 198 origin, sw_registration_id, | 252 origin, sw_registration_id, |
| 199 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 253 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
| 200 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 254 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
| 201 new_registration, existing_registration, callback)); | 255 new_registration, callback)); |
| 256 } |
| 257 |
| 258 void BackgroundSyncManager::DisableAndClearManager( |
| 259 const base::Closure& callback) { |
| 260 if (disabled_) { |
| 261 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); |
| 262 return; |
| 263 } |
| 264 |
| 265 disabled_ = true; |
| 266 sw_to_registrations_map_.clear(); |
| 267 |
| 268 // Delete all backend entries. The memory representation of registered syncs |
| 269 // may be out of sync with storage (e.g., due to corruption detection on |
| 270 // loading from storage), so reload the registrations from storage again. |
| 271 GetDataFromBackend( |
| 272 kBackgroundSyncUserDataKey, |
| 273 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, |
| 274 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 275 } |
| 276 |
| 277 void BackgroundSyncManager::DisableAndClearDidGetRegistrations( |
| 278 const base::Closure& callback, |
| 279 const std::vector<std::pair<int64, std::string>>& user_data, |
| 280 ServiceWorkerStatusCode status) { |
| 281 if (status != SERVICE_WORKER_OK || user_data.empty()) { |
| 282 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); |
| 283 return; |
| 284 } |
| 285 |
| 286 base::Closure barrier_closure = |
| 287 base::BarrierClosure(user_data.size(), base::Bind(callback)); |
| 288 |
| 289 for (const auto& sw_id_and_regs : user_data) { |
| 290 service_worker_context_->context()->storage()->ClearUserData( |
| 291 sw_id_and_regs.first, kBackgroundSyncUserDataKey, |
| 292 base::Bind(&BackgroundSyncManager::DisableAndClearManagerClearedOne, |
| 293 weak_ptr_factory_.GetWeakPtr(), barrier_closure)); |
| 294 } |
| 295 } |
| 296 |
| 297 void BackgroundSyncManager::DisableAndClearManagerClearedOne( |
| 298 const base::Closure& barrier_closure, |
| 299 ServiceWorkerStatusCode status) { |
| 300 // The status doesn't matter at this point, there is nothing else to be done. |
| 301 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 302 base::Bind(barrier_closure)); |
| 202 } | 303 } |
| 203 | 304 |
| 204 bool BackgroundSyncManager::LookupRegistration( | 305 bool BackgroundSyncManager::LookupRegistration( |
| 205 int64 sw_registration_id, | 306 int64 sw_registration_id, |
| 206 const std::string& sync_registration_name, | 307 const std::string& sync_registration_name, |
| 207 BackgroundSyncRegistration* existing_registration) { | 308 BackgroundSyncRegistration* existing_registration) { |
| 208 SWIdToRegistrationsMap::iterator it = | 309 SWIdToRegistrationsMap::iterator it = |
| 209 sw_to_registrations_map_.find(sw_registration_id); | 310 sw_to_registrations_map_.find(sw_registration_id); |
| 210 if (it == sw_to_registrations_map_.end()) | 311 if (it == sw_to_registrations_map_.end()) |
| 211 return false; | 312 return false; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 bool success = registrations_proto.SerializeToString(&serialized); | 349 bool success = registrations_proto.SerializeToString(&serialized); |
| 249 DCHECK(success); | 350 DCHECK(success); |
| 250 | 351 |
| 251 StoreDataInBackend(sw_registration_id, origin, kBackgroundSyncUserDataKey, | 352 StoreDataInBackend(sw_registration_id, origin, kBackgroundSyncUserDataKey, |
| 252 serialized, callback); | 353 serialized, callback); |
| 253 } | 354 } |
| 254 | 355 |
| 255 void BackgroundSyncManager::RegisterDidStore( | 356 void BackgroundSyncManager::RegisterDidStore( |
| 256 int64 sw_registration_id, | 357 int64 sw_registration_id, |
| 257 const BackgroundSyncRegistration& new_registration, | 358 const BackgroundSyncRegistration& new_registration, |
| 258 const BackgroundSyncRegistration& previous_registration, | |
| 259 const StatusAndRegistrationCallback& callback, | 359 const StatusAndRegistrationCallback& callback, |
| 260 ServiceWorkerStatusCode status) { | 360 ServiceWorkerStatusCode status) { |
| 261 if (status != SERVICE_WORKER_OK) { | 361 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 262 // Restore the previous state. | 362 // The registration is gone. |
| 263 if (previous_registration.id != | 363 sw_to_registrations_map_.erase(sw_registration_id); |
| 264 BackgroundSyncRegistration::kInvalidRegistrationId) { | |
| 265 AddRegistrationToMap(sw_registration_id, previous_registration); | |
| 266 } else { | |
| 267 RemoveRegistrationFromMap(sw_registration_id, new_registration.name, | |
| 268 nullptr); | |
| 269 } | |
| 270 base::MessageLoop::current()->PostTask( | 364 base::MessageLoop::current()->PostTask( |
| 271 FROM_HERE, | 365 FROM_HERE, |
| 272 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 366 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 273 return; | 367 return; |
| 274 } | 368 } |
| 275 | 369 |
| 370 if (status != SERVICE_WORKER_OK) { |
| 371 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
| 372 "failure."; |
| 373 DisableAndClearManager( |
| 374 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 375 return; |
| 376 } |
| 377 |
| 276 // TODO(jkarlin): Run the registration algorithm. | 378 // TODO(jkarlin): Run the registration algorithm. |
| 277 base::MessageLoop::current()->PostTask( | 379 base::MessageLoop::current()->PostTask( |
| 278 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); | 380 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); |
| 279 } | 381 } |
| 280 | 382 |
| 281 void BackgroundSyncManager::RemoveRegistrationFromMap( | 383 void BackgroundSyncManager::RemoveRegistrationFromMap( |
| 282 int64 sw_registration_id, | 384 int64 sw_registration_id, |
| 283 const std::string& sync_registration_name, | 385 const std::string& sync_registration_name, |
| 284 BackgroundSyncRegistration* old_registration) { | 386 BackgroundSyncRegistration* old_registration) { |
| 285 DCHECK( | 387 DCHECK( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 service_worker_context_->context()->storage()->GetUserDataForAllRegistrations( | 429 service_worker_context_->context()->storage()->GetUserDataForAllRegistrations( |
| 328 key, callback); | 430 key, callback); |
| 329 } | 431 } |
| 330 | 432 |
| 331 void BackgroundSyncManager::UnregisterImpl( | 433 void BackgroundSyncManager::UnregisterImpl( |
| 332 const GURL& origin, | 434 const GURL& origin, |
| 333 int64 sw_registration_id, | 435 int64 sw_registration_id, |
| 334 const std::string& sync_registration_name, | 436 const std::string& sync_registration_name, |
| 335 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 437 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 336 const StatusCallback& callback) { | 438 const StatusCallback& callback) { |
| 439 if (disabled_) { |
| 440 base::MessageLoop::current()->PostTask( |
| 441 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 442 return; |
| 443 } |
| 444 |
| 337 BackgroundSyncRegistration existing_registration; | 445 BackgroundSyncRegistration existing_registration; |
| 338 if (!LookupRegistration(sw_registration_id, sync_registration_name, | 446 if (!LookupRegistration(sw_registration_id, sync_registration_name, |
| 339 &existing_registration) || | 447 &existing_registration) || |
| 340 existing_registration.id != sync_registration_id) { | 448 existing_registration.id != sync_registration_id) { |
| 341 base::MessageLoop::current()->PostTask( | 449 base::MessageLoop::current()->PostTask( |
| 342 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); | 450 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); |
| 343 return; | 451 return; |
| 344 } | 452 } |
| 345 | 453 |
| 346 BackgroundSyncRegistration old_sync_registration; | 454 BackgroundSyncRegistration old_sync_registration; |
| 347 RemoveRegistrationFromMap(sw_registration_id, sync_registration_name, | 455 RemoveRegistrationFromMap(sw_registration_id, sync_registration_name, |
| 348 &old_sync_registration); | 456 &old_sync_registration); |
| 349 | 457 |
| 350 StoreRegistrations( | 458 StoreRegistrations( |
| 351 origin, sw_registration_id, | 459 origin, sw_registration_id, |
| 352 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 460 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
| 353 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 461 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
| 354 old_sync_registration, callback)); | 462 old_sync_registration, callback)); |
| 355 } | 463 } |
| 356 | 464 |
| 357 void BackgroundSyncManager::UnregisterDidStore( | 465 void BackgroundSyncManager::UnregisterDidStore( |
| 358 int64 sw_registration_id, | 466 int64 sw_registration_id, |
| 359 const BackgroundSyncRegistration& old_sync_registration, | 467 const BackgroundSyncRegistration& old_sync_registration, |
| 360 const StatusCallback& callback, | 468 const StatusCallback& callback, |
| 361 ServiceWorkerStatusCode status) { | 469 ServiceWorkerStatusCode status) { |
| 362 if (status != SERVICE_WORKER_OK) { | 470 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 363 // Restore the previous state. | 471 // ServiceWorker was unregistered. |
| 364 AddRegistrationToMap(sw_registration_id, old_sync_registration); | 472 sw_to_registrations_map_.erase(sw_registration_id); |
| 365 base::MessageLoop::current()->PostTask( | 473 base::MessageLoop::current()->PostTask( |
| 366 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 474 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 367 return; | 475 return; |
| 368 } | 476 } |
| 369 | 477 |
| 478 if (status != SERVICE_WORKER_OK) { |
| 479 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
| 480 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 481 return; |
| 482 } |
| 483 |
| 370 // TODO(jkarlin): Run the registration algorithm. | 484 // TODO(jkarlin): Run the registration algorithm. |
| 371 base::MessageLoop::current()->PostTask(FROM_HERE, | 485 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 372 base::Bind(callback, ERROR_TYPE_OK)); | 486 base::Bind(callback, ERROR_TYPE_OK)); |
| 373 } | 487 } |
| 374 | 488 |
| 375 void BackgroundSyncManager::GetRegistrationImpl( | 489 void BackgroundSyncManager::GetRegistrationImpl( |
| 376 const GURL& origin, | 490 const GURL& origin, |
| 377 int64 sw_registration_id, | 491 int64 sw_registration_id, |
| 378 const std::string sync_registration_name, | 492 const std::string sync_registration_name, |
| 379 const StatusAndRegistrationCallback& callback) { | 493 const StatusAndRegistrationCallback& callback) { |
| 494 if (disabled_) { |
| 495 base::MessageLoop::current()->PostTask( |
| 496 FROM_HERE, |
| 497 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 498 return; |
| 499 } |
| 500 |
| 380 BackgroundSyncRegistration out_registration; | 501 BackgroundSyncRegistration out_registration; |
| 381 if (!LookupRegistration(sw_registration_id, sync_registration_name, | 502 if (!LookupRegistration(sw_registration_id, sync_registration_name, |
| 382 &out_registration)) { | 503 &out_registration)) { |
| 383 base::MessageLoop::current()->PostTask( | 504 base::MessageLoop::current()->PostTask( |
| 384 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND, | 505 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND, |
| 385 BackgroundSyncRegistration())); | 506 BackgroundSyncRegistration())); |
| 386 return; | 507 return; |
| 387 } | 508 } |
| 388 | 509 |
| 389 base::MessageLoop::current()->PostTask( | 510 base::MessageLoop::current()->PostTask( |
| 390 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registration)); | 511 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registration)); |
| 391 } | 512 } |
| 392 | 513 |
| 514 void BackgroundSyncManager::OnRegistrationDeletedImpl( |
| 515 int64 registration_id, |
| 516 const base::Closure& callback) { |
| 517 // The backend (ServiceWorkerStorage) will delete the data, so just delete the |
| 518 // memory representation here. |
| 519 sw_to_registrations_map_.erase(registration_id); |
| 520 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); |
| 521 } |
| 522 |
| 523 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { |
| 524 sw_to_registrations_map_.clear(); |
| 525 disabled_ = false; |
| 526 InitImpl(callback); |
| 527 } |
| 528 |
| 393 void BackgroundSyncManager::PendingStatusAndRegistrationCallback( | 529 void BackgroundSyncManager::PendingStatusAndRegistrationCallback( |
| 394 const StatusAndRegistrationCallback& callback, | 530 const StatusAndRegistrationCallback& callback, |
| 395 ErrorType error, | 531 ErrorType error, |
| 396 const BackgroundSyncRegistration& sync_registration) { | 532 const BackgroundSyncRegistration& sync_registration) { |
| 397 // The callback might delete this object, so hang onto a weak ptr to find out. | 533 // The callback might delete this object, so hang onto a weak ptr to find out. |
| 398 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); | 534 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); |
| 399 callback.Run(error, sync_registration); | 535 callback.Run(error, sync_registration); |
| 400 if (manager) | 536 if (manager) |
| 401 op_scheduler_.CompleteOperationAndRunNext(); | 537 op_scheduler_.CompleteOperationAndRunNext(); |
| 402 } | 538 } |
| 403 | 539 |
| 404 void BackgroundSyncManager::PendingStatusCallback( | 540 void BackgroundSyncManager::PendingStatusCallback( |
| 405 const StatusCallback& callback, | 541 const StatusCallback& callback, |
| 406 ErrorType error) { | 542 ErrorType error) { |
| 407 // The callback might delete this object, so hang onto a weak ptr to find out. | 543 // The callback might delete this object, so hang onto a weak ptr to find out. |
| 408 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); | 544 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); |
| 409 callback.Run(error); | 545 callback.Run(error); |
| 410 if (manager) | 546 if (manager) |
| 411 op_scheduler_.CompleteOperationAndRunNext(); | 547 op_scheduler_.CompleteOperationAndRunNext(); |
| 412 } | 548 } |
| 413 | 549 |
| 550 void BackgroundSyncManager::PendingClosure(const base::Closure& callback) { |
| 551 // The callback might delete this object, so hang onto a weak ptr to find out. |
| 552 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); |
| 553 callback.Run(); |
| 554 if (manager) |
| 555 op_scheduler_.CompleteOperationAndRunNext(); |
| 556 } |
| 557 |
| 558 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { |
| 559 return base::Bind(&BackgroundSyncManager::PendingClosure, |
| 560 weak_ptr_factory_.GetWeakPtr(), |
| 561 base::Bind(base::DoNothing)); |
| 562 } |
| 563 |
| 564 BackgroundSyncManager::StatusAndRegistrationCallback |
| 565 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( |
| 566 const StatusAndRegistrationCallback& callback) { |
| 567 return base::Bind( |
| 568 &BackgroundSyncManager::PendingStatusAndRegistrationCallback, |
| 569 weak_ptr_factory_.GetWeakPtr(), callback); |
| 570 } |
| 571 |
| 572 BackgroundSyncManager::StatusCallback |
| 573 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
| 574 return base::Bind(&BackgroundSyncManager::PendingStatusCallback, |
| 575 weak_ptr_factory_.GetWeakPtr(), callback); |
| 576 } |
| 577 |
| 414 } // namespace content | 578 } // namespace content |
| OLD | NEW |