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.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 |
| 61 if (disabled_) { | |
| 62 base::MessageLoop::current()->PostTask( | |
| 63 FROM_HERE, | |
| 64 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | |
| 65 return; | |
| 66 } | |
| 67 | |
| 60 StatusAndRegistrationCallback pending_callback = | 68 StatusAndRegistrationCallback pending_callback = |
| 61 base::Bind(&BackgroundSyncManager::PendingStatusAndRegistrationCallback, | 69 base::Bind(&BackgroundSyncManager::PendingStatusAndRegistrationCallback, |
| 62 weak_ptr_factory_.GetWeakPtr(), callback); | 70 weak_ptr_factory_.GetWeakPtr(), callback); |
| 63 | 71 |
| 64 op_scheduler_.ScheduleOperation(base::Bind( | 72 op_scheduler_.ScheduleOperation(base::Bind( |
| 65 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), | 73 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 66 origin, sw_registration_id, sync_registration, pending_callback)); | 74 origin, sw_registration_id, sync_registration, pending_callback)); |
| 67 } | 75 } |
| 68 | 76 |
| 69 void BackgroundSyncManager::Unregister( | 77 void BackgroundSyncManager::Unregister( |
| 70 const GURL& origin, | 78 const GURL& origin, |
| 71 int64 sw_registration_id, | 79 int64 sw_registration_id, |
| 72 const std::string& sync_registration_name, | 80 const std::string& sync_registration_name, |
| 73 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 81 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 74 const StatusCallback& callback) { | 82 const StatusCallback& callback) { |
| 75 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 83 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 76 | 84 |
| 85 if (disabled_) { | |
| 86 base::MessageLoop::current()->PostTask( | |
| 87 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | |
| 88 return; | |
| 89 } | |
| 90 | |
| 77 StatusCallback pending_callback = | 91 StatusCallback pending_callback = |
| 78 base::Bind(&BackgroundSyncManager::PendingStatusCallback, | 92 base::Bind(&BackgroundSyncManager::PendingStatusCallback, |
| 79 weak_ptr_factory_.GetWeakPtr(), callback); | 93 weak_ptr_factory_.GetWeakPtr(), callback); |
| 80 | 94 |
| 81 op_scheduler_.ScheduleOperation(base::Bind( | 95 op_scheduler_.ScheduleOperation(base::Bind( |
| 82 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | 96 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 83 origin, sw_registration_id, sync_registration_name, sync_registration_id, | 97 origin, sw_registration_id, sync_registration_name, sync_registration_id, |
| 84 pending_callback)); | 98 pending_callback)); |
| 85 } | 99 } |
| 86 | 100 |
| 87 void BackgroundSyncManager::GetRegistration( | 101 void BackgroundSyncManager::GetRegistration( |
| 88 const GURL& origin, | 102 const GURL& origin, |
| 89 int64 sw_registration_id, | 103 int64 sw_registration_id, |
| 90 const std::string sync_registration_name, | 104 const std::string sync_registration_name, |
| 91 const StatusAndRegistrationCallback& callback) { | 105 const StatusAndRegistrationCallback& callback) { |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 106 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 93 | 107 |
| 108 if (disabled_) { | |
| 109 base::MessageLoop::current()->PostTask( | |
| 110 FROM_HERE, | |
| 111 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 94 StatusAndRegistrationCallback pending_callback = | 115 StatusAndRegistrationCallback pending_callback = |
| 95 base::Bind(&BackgroundSyncManager::PendingStatusAndRegistrationCallback, | 116 base::Bind(&BackgroundSyncManager::PendingStatusAndRegistrationCallback, |
| 96 weak_ptr_factory_.GetWeakPtr(), callback); | 117 weak_ptr_factory_.GetWeakPtr(), callback); |
| 97 | 118 |
| 98 op_scheduler_.ScheduleOperation( | 119 op_scheduler_.ScheduleOperation( |
| 99 base::Bind(&BackgroundSyncManager::GetRegistrationImpl, | 120 base::Bind(&BackgroundSyncManager::GetRegistrationImpl, |
| 100 weak_ptr_factory_.GetWeakPtr(), origin, sw_registration_id, | 121 weak_ptr_factory_.GetWeakPtr(), origin, sw_registration_id, |
| 101 sync_registration_name, pending_callback)); | 122 sync_registration_name, pending_callback)); |
| 102 } | 123 } |
| 103 | 124 |
| 125 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, | |
| 126 const GURL& pattern) { | |
| 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 128 base::Closure pending_callback = | |
|
jsbell
2015/04/01 21:54:54
Can you add a comment that this do-nothing callbac
jkarlin
2015/04/02 17:57:56
Nice suggestion. I made such functions for all of
michaeln
2015/04/02 23:06:20
Nice, those helpers help a lot.
| |
| 129 base::Bind(&BackgroundSyncManager::PendingClosure, | |
| 130 weak_ptr_factory_.GetWeakPtr(), base::Bind(base::DoNothing)); | |
| 131 | |
| 132 // Operations already in the queue will either fail when they write to storage | |
| 133 // or return stale results based on registrations loaded in memory. This is | |
| 134 // inconsequential since the service worker is gone. | |
| 135 op_scheduler_.ScheduleOperation(base::Bind( | |
| 136 &BackgroundSyncManager::OnRegistrationDeletedImpl, | |
| 137 weak_ptr_factory_.GetWeakPtr(), registration_id, pending_callback)); | |
| 138 } | |
| 139 | |
| 140 void BackgroundSyncManager::OnStorageWiped() { | |
| 141 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 142 base::Closure pending_callback = | |
| 143 base::Bind(&BackgroundSyncManager::PendingClosure, | |
| 144 weak_ptr_factory_.GetWeakPtr(), base::Bind(base::DoNothing)); | |
| 145 | |
| 146 // Operations already in the queue will either fail when they write to storage | |
| 147 // or return stale results based on registrations loaded in memory. This is | |
| 148 // inconsequential since the service workers are gone. | |
| 149 op_scheduler_.ScheduleOperation( | |
| 150 base::Bind(&BackgroundSyncManager::OnStorageWipedImpl, | |
| 151 weak_ptr_factory_.GetWeakPtr(), pending_callback)); | |
| 152 } | |
| 153 | |
| 104 BackgroundSyncManager::BackgroundSyncManager( | 154 BackgroundSyncManager::BackgroundSyncManager( |
| 105 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 155 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) |
| 106 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { | 156 : service_worker_context_(service_worker_context), |
| 157 disabled_(false), | |
| 158 weak_ptr_factory_(this) { | |
| 159 service_worker_context_->AddObserver(this); | |
| 107 } | 160 } |
| 108 | 161 |
| 109 void BackgroundSyncManager::Init() { | 162 void BackgroundSyncManager::Init() { |
| 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 111 DCHECK(!op_scheduler_.ScheduledOperations()); | 164 DCHECK(!op_scheduler_.ScheduledOperations()); |
| 165 DCHECK(!disabled_); | |
| 166 | |
| 167 base::Closure pending_callback = | |
| 168 base::Bind(&BackgroundSyncManager::PendingClosure, | |
| 169 weak_ptr_factory_.GetWeakPtr(), base::Bind(base::DoNothing)); | |
| 112 | 170 |
| 113 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, | 171 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, |
| 114 weak_ptr_factory_.GetWeakPtr())); | 172 weak_ptr_factory_.GetWeakPtr(), |
| 173 pending_callback)); | |
| 115 } | 174 } |
| 116 | 175 |
| 117 void BackgroundSyncManager::InitImpl() { | 176 void BackgroundSyncManager::InitImpl(const base::Closure& callback) { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 177 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 119 | 178 |
| 179 if (disabled_) { | |
| 180 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | |
| 181 return; | |
| 182 } | |
| 183 | |
| 120 GetDataFromBackend( | 184 GetDataFromBackend( |
| 121 kBackgroundSyncUserDataKey, | 185 kBackgroundSyncUserDataKey, |
| 122 base::Bind(&BackgroundSyncManager::InitDidGetDataFromBackend, | 186 base::Bind(&BackgroundSyncManager::InitDidGetDataFromBackend, |
| 123 weak_ptr_factory_.GetWeakPtr())); | 187 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 124 } | 188 } |
| 125 | 189 |
| 126 void BackgroundSyncManager::InitDidGetDataFromBackend( | 190 void BackgroundSyncManager::InitDidGetDataFromBackend( |
| 191 const base::Closure& callback, | |
| 127 const std::vector<std::pair<int64, std::string>>& user_data, | 192 const std::vector<std::pair<int64, std::string>>& user_data, |
| 128 ServiceWorkerStatusCode status) { | 193 ServiceWorkerStatusCode status) { |
| 129 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) | 194 if (status != SERVICE_WORKER_OK && status != SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 130 LOG(ERROR) << "Background Sync Failed to load from backend."; | 195 LOG(ERROR) << "BackgroundSync failed to init due to backend failure."; |
| 196 DisableAndClearManager(); | |
| 197 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | |
| 198 return; | |
| 199 } | |
| 131 | 200 |
| 132 bool corruption_detected = false; | 201 bool corruption_detected = false; |
| 133 for (const std::pair<int64, std::string>& data : user_data) { | 202 for (const std::pair<int64, std::string>& data : user_data) { |
| 134 BackgroundSyncRegistrationsProto registrations_proto; | 203 BackgroundSyncRegistrationsProto registrations_proto; |
| 135 if (registrations_proto.ParseFromString(data.second)) { | 204 if (registrations_proto.ParseFromString(data.second)) { |
| 136 sw_to_registrations_map_[data.first] = BackgroundSyncRegistrations( | 205 sw_to_registrations_map_[data.first] = BackgroundSyncRegistrations( |
| 137 registrations_proto.next_registration_id()); | 206 registrations_proto.next_registration_id()); |
| 138 BackgroundSyncRegistrations* registrations = | 207 BackgroundSyncRegistrations* registrations = |
| 139 &sw_to_registrations_map_[data.first]; | 208 &sw_to_registrations_map_[data.first]; |
| 140 | 209 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 156 registration; | 225 registration; |
| 157 } | 226 } |
| 158 } | 227 } |
| 159 | 228 |
| 160 if (corruption_detected) | 229 if (corruption_detected) |
| 161 break; | 230 break; |
| 162 } | 231 } |
| 163 | 232 |
| 164 if (corruption_detected) { | 233 if (corruption_detected) { |
| 165 LOG(ERROR) << "Corruption detected in background sync backend"; | 234 LOG(ERROR) << "Corruption detected in background sync backend"; |
| 166 sw_to_registrations_map_.clear(); | 235 DisableAndClearManager(); |
| 167 } | 236 } |
| 168 | 237 |
| 169 // TODO(jkarlin): Call the scheduling algorithm here. | 238 // TODO(jkarlin): Call the scheduling algorithm here. |
| 170 | 239 |
| 171 op_scheduler_.CompleteOperationAndRunNext(); | 240 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); |
| 172 } | 241 } |
| 173 | 242 |
| 174 void BackgroundSyncManager::RegisterImpl( | 243 void BackgroundSyncManager::RegisterImpl( |
| 175 const GURL& origin, | 244 const GURL& origin, |
| 176 int64 sw_registration_id, | 245 int64 sw_registration_id, |
| 177 const BackgroundSyncRegistration& sync_registration, | 246 const BackgroundSyncRegistration& sync_registration, |
| 178 const StatusAndRegistrationCallback& callback) { | 247 const StatusAndRegistrationCallback& callback) { |
| 248 if (disabled_) { | |
| 249 base::MessageLoop::current()->PostTask( | |
| 250 FROM_HERE, | |
| 251 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | |
| 252 return; | |
| 253 } | |
| 254 | |
| 179 BackgroundSyncRegistration existing_registration; | 255 BackgroundSyncRegistration existing_registration; |
| 180 if (LookupRegistration(sw_registration_id, sync_registration.name, | 256 if (LookupRegistration(sw_registration_id, sync_registration.name, |
| 181 &existing_registration)) { | 257 &existing_registration)) { |
| 182 if (existing_registration.Equals(sync_registration)) { | 258 if (existing_registration.Equals(sync_registration)) { |
| 183 base::MessageLoop::current()->PostTask( | 259 base::MessageLoop::current()->PostTask( |
| 184 FROM_HERE, | 260 FROM_HERE, |
| 185 base::Bind(callback, ERROR_TYPE_OK, existing_registration)); | 261 base::Bind(callback, ERROR_TYPE_OK, existing_registration)); |
| 186 return; | 262 return; |
| 187 } | 263 } |
| 188 } | 264 } |
| 189 | 265 |
| 190 BackgroundSyncRegistration new_registration = sync_registration; | 266 BackgroundSyncRegistration new_registration = sync_registration; |
| 191 BackgroundSyncRegistrations* registrations = | 267 BackgroundSyncRegistrations* registrations = |
| 192 &sw_to_registrations_map_[sw_registration_id]; | 268 &sw_to_registrations_map_[sw_registration_id]; |
| 193 new_registration.id = registrations->next_id++; | 269 new_registration.id = registrations->next_id++; |
| 194 | 270 |
| 195 AddRegistrationToMap(sw_registration_id, new_registration); | 271 AddRegistrationToMap(sw_registration_id, new_registration); |
| 196 | 272 |
| 197 StoreRegistrations( | 273 StoreRegistrations( |
| 198 origin, sw_registration_id, | 274 origin, sw_registration_id, |
| 199 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 275 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
| 200 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 276 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
| 201 new_registration, existing_registration, callback)); | 277 new_registration, callback)); |
| 278 } | |
| 279 | |
| 280 void BackgroundSyncManager::DisableAndClearManager() { | |
| 281 disabled_ = true; | |
| 282 base::Closure pending_callback = | |
| 283 base::Bind(&BackgroundSyncManager::PendingClosure, | |
| 284 weak_ptr_factory_.GetWeakPtr(), base::Bind(base::DoNothing)); | |
| 285 | |
| 286 op_scheduler_.ScheduleOperation( | |
| 287 base::Bind(&BackgroundSyncManager::DisableAndClearManagerImpl, | |
| 288 weak_ptr_factory_.GetWeakPtr(), pending_callback)); | |
| 289 } | |
| 290 | |
| 291 void BackgroundSyncManager::DisableAndClearManagerImpl( | |
|
jsbell
2015/04/01 21:54:54
Out of curiosity, are there any relevant "I'm runn
jkarlin
2015/04/02 17:57:56
There is a check that an operation is currently ru
jsbell
2015/04/02 20:25:13
sgtm
| |
| 292 const base::Closure& callback) { | |
| 293 DCHECK(disabled_); | |
|
michaeln
2015/04/01 21:37:30
Can BackgroundSyncManager::OnStorageWipedImpl happ
jkarlin
2015/04/02 17:57:56
Yes, there is a race on disabled_ if DisableAndCle
michaeln
2015/04/02 23:06:20
yup, that works!
| |
| 294 | |
| 295 sw_to_registrations_map_.clear(); | |
| 296 | |
| 297 // Delete all backend entries. The memory representation of registered syncs | |
| 298 // may be corrupt so reload the registrations from storage again. | |
|
jsbell
2015/04/01 21:54:54
Maybe clarify in the comment why memory is "corrup
jkarlin
2015/04/02 17:57:56
Done.
| |
| 299 GetDataFromBackend( | |
| 300 kBackgroundSyncUserDataKey, | |
| 301 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, | |
| 302 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 303 } | |
| 304 | |
| 305 void BackgroundSyncManager::DisableAndClearDidGetRegistrations( | |
| 306 const base::Closure& callback, | |
| 307 const std::vector<std::pair<int64, std::string>>& user_data, | |
| 308 ServiceWorkerStatusCode status) { | |
| 309 if (status != SERVICE_WORKER_OK || user_data.empty()) { | |
| 310 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | |
| 311 return; | |
| 312 } | |
| 313 | |
| 314 base::Closure barrier_closure = | |
| 315 base::BarrierClosure(user_data.size(), base::Bind(callback)); | |
| 316 | |
| 317 for (const auto& sw_id_and_regs : user_data) { | |
| 318 service_worker_context_->context()->storage()->ClearUserData( | |
| 319 sw_id_and_regs.first, kBackgroundSyncUserDataKey, | |
| 320 base::Bind(&BackgroundSyncManager::DisableAndClearManagerClearedOne, | |
| 321 weak_ptr_factory_.GetWeakPtr(), barrier_closure)); | |
| 322 } | |
| 323 } | |
| 324 | |
| 325 void BackgroundSyncManager::DisableAndClearManagerClearedOne( | |
| 326 const base::Closure& barrier_closure, | |
| 327 ServiceWorkerStatusCode status) { | |
| 328 // The status doesn't matter at this point, there is nothing else to be done. | |
| 329 base::MessageLoop::current()->PostTask(FROM_HERE, | |
| 330 base::Bind(barrier_closure)); | |
| 202 } | 331 } |
| 203 | 332 |
| 204 bool BackgroundSyncManager::LookupRegistration( | 333 bool BackgroundSyncManager::LookupRegistration( |
| 205 int64 sw_registration_id, | 334 int64 sw_registration_id, |
| 206 const std::string& sync_registration_name, | 335 const std::string& sync_registration_name, |
| 207 BackgroundSyncRegistration* existing_registration) { | 336 BackgroundSyncRegistration* existing_registration) { |
| 208 SWIdToRegistrationsMap::iterator it = | 337 SWIdToRegistrationsMap::iterator it = |
| 209 sw_to_registrations_map_.find(sw_registration_id); | 338 sw_to_registrations_map_.find(sw_registration_id); |
| 210 if (it == sw_to_registrations_map_.end()) | 339 if (it == sw_to_registrations_map_.end()) |
| 211 return false; | 340 return false; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 bool success = registrations_proto.SerializeToString(&serialized); | 377 bool success = registrations_proto.SerializeToString(&serialized); |
| 249 DCHECK(success); | 378 DCHECK(success); |
| 250 | 379 |
| 251 StoreDataInBackend(sw_registration_id, origin, kBackgroundSyncUserDataKey, | 380 StoreDataInBackend(sw_registration_id, origin, kBackgroundSyncUserDataKey, |
| 252 serialized, callback); | 381 serialized, callback); |
| 253 } | 382 } |
| 254 | 383 |
| 255 void BackgroundSyncManager::RegisterDidStore( | 384 void BackgroundSyncManager::RegisterDidStore( |
| 256 int64 sw_registration_id, | 385 int64 sw_registration_id, |
| 257 const BackgroundSyncRegistration& new_registration, | 386 const BackgroundSyncRegistration& new_registration, |
| 258 const BackgroundSyncRegistration& previous_registration, | |
| 259 const StatusAndRegistrationCallback& callback, | 387 const StatusAndRegistrationCallback& callback, |
| 260 ServiceWorkerStatusCode status) { | 388 ServiceWorkerStatusCode status) { |
| 261 if (status != SERVICE_WORKER_OK) { | 389 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 262 // Restore the previous state. | 390 // The registration is gone. |
| 263 if (previous_registration.id != | 391 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( | 392 base::MessageLoop::current()->PostTask( |
| 271 FROM_HERE, | 393 FROM_HERE, |
| 272 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | 394 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); |
| 395 return; | |
| 396 } | |
| 397 | |
| 398 if (status != SERVICE_WORKER_OK) { | |
| 399 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | |
| 400 "failure."; | |
| 401 DisableAndClearManager(); | |
| 402 base::MessageLoop::current()->PostTask( | |
| 403 FROM_HERE, | |
| 404 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | |
| 273 return; | 405 return; |
| 274 } | 406 } |
| 275 | 407 |
| 276 // TODO(jkarlin): Run the registration algorithm. | 408 // TODO(jkarlin): Run the registration algorithm. |
| 277 base::MessageLoop::current()->PostTask( | 409 base::MessageLoop::current()->PostTask( |
| 278 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); | 410 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, new_registration)); |
| 279 } | 411 } |
| 280 | 412 |
| 281 void BackgroundSyncManager::RemoveRegistrationFromMap( | 413 void BackgroundSyncManager::RemoveRegistrationFromMap( |
| 282 int64 sw_registration_id, | 414 int64 sw_registration_id, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 service_worker_context_->context()->storage()->GetUserDataForAllRegistrations( | 459 service_worker_context_->context()->storage()->GetUserDataForAllRegistrations( |
| 328 key, callback); | 460 key, callback); |
| 329 } | 461 } |
| 330 | 462 |
| 331 void BackgroundSyncManager::UnregisterImpl( | 463 void BackgroundSyncManager::UnregisterImpl( |
| 332 const GURL& origin, | 464 const GURL& origin, |
| 333 int64 sw_registration_id, | 465 int64 sw_registration_id, |
| 334 const std::string& sync_registration_name, | 466 const std::string& sync_registration_name, |
| 335 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 467 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 336 const StatusCallback& callback) { | 468 const StatusCallback& callback) { |
| 469 if (disabled_) { | |
| 470 base::MessageLoop::current()->PostTask( | |
| 471 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | |
| 472 return; | |
| 473 } | |
| 474 | |
| 337 BackgroundSyncRegistration existing_registration; | 475 BackgroundSyncRegistration existing_registration; |
| 338 if (!LookupRegistration(sw_registration_id, sync_registration_name, | 476 if (!LookupRegistration(sw_registration_id, sync_registration_name, |
| 339 &existing_registration) || | 477 &existing_registration) || |
| 340 existing_registration.id != sync_registration_id) { | 478 existing_registration.id != sync_registration_id) { |
| 341 base::MessageLoop::current()->PostTask( | 479 base::MessageLoop::current()->PostTask( |
| 342 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); | 480 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); |
| 343 return; | 481 return; |
| 344 } | 482 } |
| 345 | 483 |
| 346 BackgroundSyncRegistration old_sync_registration; | 484 BackgroundSyncRegistration old_sync_registration; |
| 347 RemoveRegistrationFromMap(sw_registration_id, sync_registration_name, | 485 RemoveRegistrationFromMap(sw_registration_id, sync_registration_name, |
| 348 &old_sync_registration); | 486 &old_sync_registration); |
| 349 | 487 |
| 350 StoreRegistrations( | 488 StoreRegistrations( |
| 351 origin, sw_registration_id, | 489 origin, sw_registration_id, |
| 352 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 490 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
| 353 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 491 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
| 354 old_sync_registration, callback)); | 492 old_sync_registration, callback)); |
| 355 } | 493 } |
| 356 | 494 |
| 357 void BackgroundSyncManager::UnregisterDidStore( | 495 void BackgroundSyncManager::UnregisterDidStore( |
| 358 int64 sw_registration_id, | 496 int64 sw_registration_id, |
| 359 const BackgroundSyncRegistration& old_sync_registration, | 497 const BackgroundSyncRegistration& old_sync_registration, |
| 360 const StatusCallback& callback, | 498 const StatusCallback& callback, |
| 361 ServiceWorkerStatusCode status) { | 499 ServiceWorkerStatusCode status) { |
| 362 if (status != SERVICE_WORKER_OK) { | 500 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 363 // Restore the previous state. | 501 // ServiceWorker was unregistered. |
| 364 AddRegistrationToMap(sw_registration_id, old_sync_registration); | 502 sw_to_registrations_map_.erase(sw_registration_id); |
| 365 base::MessageLoop::current()->PostTask( | 503 base::MessageLoop::current()->PostTask( |
| 366 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | 504 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); |
| 367 return; | 505 return; |
| 506 } | |
| 507 | |
| 508 if (status != SERVICE_WORKER_OK) { | |
| 509 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; | |
| 510 DisableAndClearManager(); | |
| 511 base::MessageLoop::current()->PostTask( | |
| 512 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | |
| 513 return; | |
| 368 } | 514 } |
| 369 | 515 |
| 370 // TODO(jkarlin): Run the registration algorithm. | 516 // TODO(jkarlin): Run the registration algorithm. |
| 371 base::MessageLoop::current()->PostTask(FROM_HERE, | 517 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 372 base::Bind(callback, ERROR_TYPE_OK)); | 518 base::Bind(callback, ERROR_TYPE_OK)); |
| 373 } | 519 } |
| 374 | 520 |
| 375 void BackgroundSyncManager::GetRegistrationImpl( | 521 void BackgroundSyncManager::GetRegistrationImpl( |
| 376 const GURL& origin, | 522 const GURL& origin, |
| 377 int64 sw_registration_id, | 523 int64 sw_registration_id, |
| 378 const std::string sync_registration_name, | 524 const std::string sync_registration_name, |
| 379 const StatusAndRegistrationCallback& callback) { | 525 const StatusAndRegistrationCallback& callback) { |
| 526 if (disabled_) { | |
| 527 base::MessageLoop::current()->PostTask( | |
| 528 FROM_HERE, | |
| 529 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | |
| 530 return; | |
| 531 } | |
| 532 | |
| 380 BackgroundSyncRegistration out_registration; | 533 BackgroundSyncRegistration out_registration; |
| 381 if (!LookupRegistration(sw_registration_id, sync_registration_name, | 534 if (!LookupRegistration(sw_registration_id, sync_registration_name, |
| 382 &out_registration)) { | 535 &out_registration)) { |
| 383 base::MessageLoop::current()->PostTask( | 536 base::MessageLoop::current()->PostTask( |
| 384 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND, | 537 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND, |
| 385 BackgroundSyncRegistration())); | 538 BackgroundSyncRegistration())); |
| 386 return; | 539 return; |
| 387 } | 540 } |
| 388 | 541 |
| 389 base::MessageLoop::current()->PostTask( | 542 base::MessageLoop::current()->PostTask( |
| 390 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registration)); | 543 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, out_registration)); |
| 391 } | 544 } |
| 392 | 545 |
| 546 void BackgroundSyncManager::OnRegistrationDeletedImpl( | |
| 547 int64 registration_id, | |
| 548 const base::Closure& callback) { | |
| 549 // The backend (ServiceWorkerStorage) will delete the data, so just delete the | |
| 550 // memory representation here. | |
| 551 sw_to_registrations_map_.erase(registration_id); | |
| 552 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback)); | |
| 553 } | |
| 554 | |
| 555 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { | |
| 556 sw_to_registrations_map_.clear(); | |
| 557 disabled_ = false; | |
|
michaeln
2015/04/01 21:37:30
would it make sense to schedule an op to reinit?
jkarlin
2015/04/02 17:57:56
Scheduling the op might allow some new requests to
| |
| 558 InitImpl(callback); | |
| 559 } | |
| 560 | |
| 393 void BackgroundSyncManager::PendingStatusAndRegistrationCallback( | 561 void BackgroundSyncManager::PendingStatusAndRegistrationCallback( |
| 394 const StatusAndRegistrationCallback& callback, | 562 const StatusAndRegistrationCallback& callback, |
| 395 ErrorType error, | 563 ErrorType error, |
| 396 const BackgroundSyncRegistration& sync_registration) { | 564 const BackgroundSyncRegistration& sync_registration) { |
| 397 // The callback might delete this object, so hang onto a weak ptr to find out. | 565 // The callback might delete this object, so hang onto a weak ptr to find out. |
| 398 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); | 566 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); |
| 399 callback.Run(error, sync_registration); | 567 callback.Run(error, sync_registration); |
| 400 if (manager) | 568 if (manager) |
| 401 op_scheduler_.CompleteOperationAndRunNext(); | 569 op_scheduler_.CompleteOperationAndRunNext(); |
| 402 } | 570 } |
| 403 | 571 |
| 404 void BackgroundSyncManager::PendingStatusCallback( | 572 void BackgroundSyncManager::PendingStatusCallback( |
| 405 const StatusCallback& callback, | 573 const StatusCallback& callback, |
| 406 ErrorType error) { | 574 ErrorType error) { |
| 407 // The callback might delete this object, so hang onto a weak ptr to find out. | 575 // The callback might delete this object, so hang onto a weak ptr to find out. |
| 408 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); | 576 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); |
| 409 callback.Run(error); | 577 callback.Run(error); |
| 410 if (manager) | 578 if (manager) |
| 411 op_scheduler_.CompleteOperationAndRunNext(); | 579 op_scheduler_.CompleteOperationAndRunNext(); |
| 412 } | 580 } |
| 413 | 581 |
| 582 void BackgroundSyncManager::PendingClosure(const base::Closure& callback) { | |
| 583 // The callback might delete this object, so hang onto a weak ptr to find out. | |
| 584 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); | |
| 585 callback.Run(); | |
| 586 if (manager) | |
| 587 op_scheduler_.CompleteOperationAndRunNext(); | |
| 588 } | |
| 589 | |
| 414 } // namespace content | 590 } // namespace content |
| OLD | NEW |