Chromium Code Reviews| 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_network_observer.h" | 9 #include "content/browser/background_sync/background_sync_network_observer.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 : RegistrationKey(registration.tag, registration.periodicity) { | 55 : RegistrationKey(registration.tag, registration.periodicity) { |
| 56 } | 56 } |
| 57 | 57 |
| 58 BackgroundSyncManager::RegistrationKey::RegistrationKey( | 58 BackgroundSyncManager::RegistrationKey::RegistrationKey( |
| 59 const std::string& tag, | 59 const std::string& tag, |
| 60 SyncPeriodicity periodicity) | 60 SyncPeriodicity periodicity) |
| 61 : value_(periodicity == SYNC_ONE_SHOT ? "o_" + tag : "p_" + tag) { | 61 : value_(periodicity == SYNC_ONE_SHOT ? "o_" + tag : "p_" + tag) { |
| 62 } | 62 } |
| 63 | 63 |
| 64 void BackgroundSyncManager::Register( | 64 void BackgroundSyncManager::Register( |
| 65 const GURL& origin, | |
| 66 int64 sw_registration_id, | 65 int64 sw_registration_id, |
| 67 const BackgroundSyncRegistration& sync_registration, | 66 const BackgroundSyncRegistration& sync_registration, |
| 68 const StatusAndRegistrationCallback& callback) { | 67 const StatusAndRegistrationCallback& callback) { |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 68 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 70 DCHECK_EQ(BackgroundSyncRegistration::kInvalidRegistrationId, | 69 DCHECK_EQ(BackgroundSyncRegistration::kInvalidRegistrationId, |
| 71 sync_registration.id); | 70 sync_registration.id); |
| 72 | 71 |
| 73 if (disabled_) { | 72 if (disabled_) { |
| 74 base::MessageLoop::current()->PostTask( | 73 base::MessageLoop::current()->PostTask( |
| 75 FROM_HERE, | 74 FROM_HERE, |
| 76 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 75 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 77 return; | 76 return; |
| 78 } | 77 } |
| 79 | 78 |
| 80 op_scheduler_.ScheduleOperation(base::Bind( | 79 op_scheduler_.ScheduleOperation(base::Bind( |
| 81 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), | 80 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 82 origin, sw_registration_id, sync_registration, | 81 sw_registration_id, sync_registration, |
| 83 MakeStatusAndRegistrationCompletion(callback))); | 82 MakeStatusAndRegistrationCompletion(callback))); |
| 84 } | 83 } |
| 85 | 84 |
| 86 void BackgroundSyncManager::Unregister( | 85 void BackgroundSyncManager::Unregister( |
| 87 const GURL& origin, | |
| 88 int64 sw_registration_id, | 86 int64 sw_registration_id, |
| 89 const std::string& sync_registration_tag, | 87 const std::string& sync_registration_tag, |
| 90 SyncPeriodicity periodicity, | 88 SyncPeriodicity periodicity, |
| 91 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 89 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 92 const StatusCallback& callback) { | 90 const StatusCallback& callback) { |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 91 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 94 | 92 |
| 95 if (disabled_) { | 93 if (disabled_) { |
| 96 base::MessageLoop::current()->PostTask( | 94 base::MessageLoop::current()->PostTask( |
| 97 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 95 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 98 return; | 96 return; |
| 99 } | 97 } |
| 100 | 98 |
| 101 RegistrationKey registration_key(sync_registration_tag, periodicity); | 99 RegistrationKey registration_key(sync_registration_tag, periodicity); |
| 102 | 100 |
| 103 op_scheduler_.ScheduleOperation(base::Bind( | 101 op_scheduler_.ScheduleOperation(base::Bind( |
| 104 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | 102 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 105 origin, sw_registration_id, registration_key, sync_registration_id, | 103 sw_registration_id, registration_key, sync_registration_id, |
| 106 MakeStatusCompletion(callback))); | 104 MakeStatusCompletion(callback))); |
| 107 } | 105 } |
| 108 | 106 |
| 109 void BackgroundSyncManager::GetRegistration( | 107 void BackgroundSyncManager::GetRegistration( |
| 110 const GURL& origin, | |
| 111 int64 sw_registration_id, | 108 int64 sw_registration_id, |
| 112 const std::string sync_registration_tag, | 109 const std::string sync_registration_tag, |
| 113 SyncPeriodicity periodicity, | 110 SyncPeriodicity periodicity, |
| 114 const StatusAndRegistrationCallback& callback) { | 111 const StatusAndRegistrationCallback& callback) { |
| 115 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 116 | 113 |
| 117 if (disabled_) { | 114 if (disabled_) { |
| 118 base::MessageLoop::current()->PostTask( | 115 base::MessageLoop::current()->PostTask( |
| 119 FROM_HERE, | 116 FROM_HERE, |
| 120 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 117 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 121 return; | 118 return; |
| 122 } | 119 } |
| 123 | 120 |
| 124 RegistrationKey registration_key(sync_registration_tag, periodicity); | 121 RegistrationKey registration_key(sync_registration_tag, periodicity); |
| 125 | 122 |
| 126 op_scheduler_.ScheduleOperation(base::Bind( | 123 op_scheduler_.ScheduleOperation(base::Bind( |
| 127 &BackgroundSyncManager::GetRegistrationImpl, | 124 &BackgroundSyncManager::GetRegistrationImpl, |
| 128 weak_ptr_factory_.GetWeakPtr(), origin, sw_registration_id, | 125 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, |
| 129 registration_key, MakeStatusAndRegistrationCompletion(callback))); | 126 MakeStatusAndRegistrationCompletion(callback))); |
| 130 } | 127 } |
| 131 | 128 |
| 132 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, | 129 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, |
| 133 const GURL& pattern) { | 130 const GURL& pattern) { |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 135 | 132 |
| 136 // Operations already in the queue will either fail when they write to storage | 133 // Operations already in the queue will either fail when they write to storage |
| 137 // or return stale results based on registrations loaded in memory. This is | 134 // or return stale results based on registrations loaded in memory. This is |
| 138 // inconsequential since the service worker is gone. | 135 // inconsequential since the service worker is gone. |
| 139 op_scheduler_.ScheduleOperation(base::Bind( | 136 op_scheduler_.ScheduleOperation(base::Bind( |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 241 DisableAndClearManager(base::Bind(callback)); | 238 DisableAndClearManager(base::Bind(callback)); |
| 242 return; | 239 return; |
| 243 } | 240 } |
| 244 | 241 |
| 245 // TODO(jkarlin): Call the scheduling algorithm here. | 242 // TODO(jkarlin): Call the scheduling algorithm here. |
| 246 | 243 |
| 247 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | 244 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); |
| 248 } | 245 } |
| 249 | 246 |
| 250 void BackgroundSyncManager::RegisterImpl( | 247 void BackgroundSyncManager::RegisterImpl( |
| 251 const GURL& origin, | |
| 252 int64 sw_registration_id, | 248 int64 sw_registration_id, |
| 253 const BackgroundSyncRegistration& sync_registration, | 249 const BackgroundSyncRegistration& sync_registration, |
| 254 const StatusAndRegistrationCallback& callback) { | 250 const StatusAndRegistrationCallback& callback) { |
| 255 if (disabled_) { | 251 if (disabled_) { |
| 256 base::MessageLoop::current()->PostTask( | 252 base::MessageLoop::current()->PostTask( |
| 257 FROM_HERE, | 253 FROM_HERE, |
| 258 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 254 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 259 return; | 255 return; |
| 260 } | 256 } |
| 261 | 257 |
| 262 const BackgroundSyncRegistration* existing_registration = LookupRegistration( | 258 const BackgroundSyncRegistration* existing_registration = LookupRegistration( |
| 263 sw_registration_id, RegistrationKey(sync_registration)); | 259 sw_registration_id, RegistrationKey(sync_registration)); |
| 264 if (existing_registration && | 260 if (existing_registration && |
| 265 existing_registration->Equals(sync_registration)) { | 261 existing_registration->Equals(sync_registration)) { |
| 266 base::MessageLoop::current()->PostTask( | 262 base::MessageLoop::current()->PostTask( |
| 267 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); | 263 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, *existing_registration)); |
| 268 return; | 264 return; |
| 269 } | 265 } |
| 270 | 266 |
| 271 BackgroundSyncRegistration new_registration = sync_registration; | 267 BackgroundSyncRegistration new_registration = sync_registration; |
| 272 BackgroundSyncRegistrations* registrations = | 268 BackgroundSyncRegistrations* registrations = |
| 273 &sw_to_registrations_map_[sw_registration_id]; | 269 &sw_to_registrations_map_[sw_registration_id]; |
| 274 new_registration.id = registrations->next_id++; | 270 new_registration.id = registrations->next_id++; |
| 275 | 271 |
| 276 AddRegistrationToMap(sw_registration_id, origin, new_registration); | 272 ServiceWorkerRegistration* sw_registration = |
| 273 service_worker_context_->GetLiveRegistration(sw_registration_id); | |
| 274 if (!sw_registration || !sw_registration->active_version()) { | |
| 275 base::MessageLoop::current()->PostTask( | |
| 276 FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER, | |
| 277 BackgroundSyncRegistration())); | |
| 278 return; | |
| 279 } | |
| 280 | |
| 281 AddRegistrationToMap(sw_registration_id, | |
| 282 sw_registration->pattern().GetOrigin(), | |
|
jsbell
2015/04/28 20:52:33
It seems to me we should probably have ServiceWork
| |
| 283 new_registration); | |
| 277 | 284 |
| 278 StoreRegistrations( | 285 StoreRegistrations( |
| 279 sw_registration_id, | 286 sw_registration_id, |
| 280 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 287 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
| 281 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 288 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
| 282 new_registration, callback)); | 289 new_registration, callback)); |
| 283 } | 290 } |
| 284 | 291 |
| 285 void BackgroundSyncManager::DisableAndClearManager( | 292 void BackgroundSyncManager::DisableAndClearManager( |
| 286 const base::Closure& callback) { | 293 const base::Closure& callback) { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 447 const std::string& backend_key, | 454 const std::string& backend_key, |
| 448 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | 455 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| 449 callback) { | 456 callback) { |
| 450 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 457 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 451 | 458 |
| 452 service_worker_context_->GetUserDataForAllRegistrations(backend_key, | 459 service_worker_context_->GetUserDataForAllRegistrations(backend_key, |
| 453 callback); | 460 callback); |
| 454 } | 461 } |
| 455 | 462 |
| 456 void BackgroundSyncManager::UnregisterImpl( | 463 void BackgroundSyncManager::UnregisterImpl( |
| 457 const GURL& origin, | |
| 458 int64 sw_registration_id, | 464 int64 sw_registration_id, |
| 459 const RegistrationKey& registration_key, | 465 const RegistrationKey& registration_key, |
| 460 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 466 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 461 const StatusCallback& callback) { | 467 const StatusCallback& callback) { |
| 462 if (disabled_) { | 468 if (disabled_) { |
| 463 base::MessageLoop::current()->PostTask( | 469 base::MessageLoop::current()->PostTask( |
| 464 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 470 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 465 return; | 471 return; |
| 466 } | 472 } |
| 467 | 473 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); | 505 DisableAndClearManager(base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 500 return; | 506 return; |
| 501 } | 507 } |
| 502 | 508 |
| 503 // TODO(jkarlin): Run the registration algorithm. | 509 // TODO(jkarlin): Run the registration algorithm. |
| 504 base::MessageLoop::current()->PostTask(FROM_HERE, | 510 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 505 base::Bind(callback, ERROR_TYPE_OK)); | 511 base::Bind(callback, ERROR_TYPE_OK)); |
| 506 } | 512 } |
| 507 | 513 |
| 508 void BackgroundSyncManager::GetRegistrationImpl( | 514 void BackgroundSyncManager::GetRegistrationImpl( |
| 509 const GURL& origin, | |
| 510 int64 sw_registration_id, | 515 int64 sw_registration_id, |
| 511 const RegistrationKey& registration_key, | 516 const RegistrationKey& registration_key, |
| 512 const StatusAndRegistrationCallback& callback) { | 517 const StatusAndRegistrationCallback& callback) { |
| 513 if (disabled_) { | 518 if (disabled_) { |
| 514 base::MessageLoop::current()->PostTask( | 519 base::MessageLoop::current()->PostTask( |
| 515 FROM_HERE, | 520 FROM_HERE, |
| 516 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 521 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 517 return; | 522 return; |
| 518 } | 523 } |
| 519 | 524 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 593 weak_ptr_factory_.GetWeakPtr(), callback); | 598 weak_ptr_factory_.GetWeakPtr(), callback); |
| 594 } | 599 } |
| 595 | 600 |
| 596 BackgroundSyncManager::StatusCallback | 601 BackgroundSyncManager::StatusCallback |
| 597 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 602 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
| 598 return base::Bind(&BackgroundSyncManager::PendingStatusCallback, | 603 return base::Bind(&BackgroundSyncManager::PendingStatusCallback, |
| 599 weak_ptr_factory_.GetWeakPtr(), callback); | 604 weak_ptr_factory_.GetWeakPtr(), callback); |
| 600 } | 605 } |
| 601 | 606 |
| 602 } // namespace content | 607 } // namespace content |
| OLD | NEW |