Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: content/browser/background_sync/background_sync_manager.cc

Issue 1282013004: BackgroundSyncManager tracks client registrations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and address comments from PS17 Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/location.h" 9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h" 11 #include "base/thread_task_runner_handle.h"
12 #include "content/browser/background_sync/background_sync_metrics.h" 12 #include "content/browser/background_sync/background_sync_metrics.h"
13 #include "content/browser/background_sync/background_sync_network_observer.h" 13 #include "content/browser/background_sync/background_sync_network_observer.h"
14 #include "content/browser/background_sync/background_sync_power_observer.h" 14 #include "content/browser/background_sync/background_sync_power_observer.h"
15 #include "content/browser/background_sync/background_sync_registration_handle.h"
15 #include "content/browser/background_sync/background_sync_registration_options.h " 16 #include "content/browser/background_sync/background_sync_registration_options.h "
16 #include "content/browser/service_worker/service_worker_context_wrapper.h" 17 #include "content/browser/service_worker/service_worker_context_wrapper.h"
17 #include "content/browser/service_worker/service_worker_storage.h" 18 #include "content/browser/service_worker/service_worker_storage.h"
18 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
19 20
20 #if defined(OS_ANDROID) 21 #if defined(OS_ANDROID)
21 #include "content/browser/android/background_sync_launcher_android.h" 22 #include "content/browser/android/background_sync_launcher_android.h"
22 #endif 23 #endif
23 24
25 namespace content {
26
27 class BackgroundSyncManager::RefCountedRegistration
28 : public base::RefCounted<RefCountedRegistration> {
29 public:
30 BackgroundSyncRegistration* value() { return &registration_; }
31 const BackgroundSyncRegistration* value() const { return &registration_; }
32
33 private:
34 friend class base::RefCounted<RefCountedRegistration>;
35 virtual ~RefCountedRegistration() = default;
36
37 BackgroundSyncRegistration registration_;
38 };
39
24 namespace { 40 namespace {
41
25 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; 42 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData";
43
44 void PostErrorResponse(
45 BackgroundSyncStatus status,
46 const BackgroundSyncManager::StatusAndRegistrationCallback& callback) {
47 base::ThreadTaskRunnerHandle::Get()->PostTask(
48 FROM_HERE,
49 base::Bind(
50 callback, status,
51 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass())));
26 } 52 }
27 53
28 namespace content { 54 } // namespace
29 55
30 BackgroundSyncManager::BackgroundSyncRegistrations:: 56 BackgroundSyncManager::BackgroundSyncRegistrations::
31 BackgroundSyncRegistrations() 57 BackgroundSyncRegistrations()
32 : next_id(BackgroundSyncRegistration::kInitialId) { 58 : next_id(BackgroundSyncRegistration::kInitialId) {
33 } 59 }
34 60
35 BackgroundSyncManager::BackgroundSyncRegistrations:: 61 BackgroundSyncManager::BackgroundSyncRegistrations::
36 ~BackgroundSyncRegistrations() { 62 ~BackgroundSyncRegistrations() {
37 } 63 }
38 64
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = 106 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
81 AreOptionConditionsMet(options) 107 AreOptionConditionsMet(options)
82 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE 108 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
83 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; 109 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
84 110
85 if (disabled_) { 111 if (disabled_) {
86 BackgroundSyncMetrics::CountRegister( 112 BackgroundSyncMetrics::CountRegister(
87 options.periodicity, registration_could_fire, 113 options.periodicity, registration_could_fire,
88 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 114 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
89 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 115 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
90 base::ThreadTaskRunnerHandle::Get()->PostTask( 116 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
91 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
92 BackgroundSyncRegistration()));
93 return; 117 return;
94 } 118 }
95 119
96 op_scheduler_.ScheduleOperation( 120 op_scheduler_.ScheduleOperation(
97 base::Bind(&BackgroundSyncManager::RegisterImpl, 121 base::Bind(&BackgroundSyncManager::RegisterImpl,
98 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, 122 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options,
99 MakeStatusAndRegistrationCompletion(callback))); 123 MakeStatusAndRegistrationCompletion(callback)));
100 } 124 }
101 125
102 void BackgroundSyncManager::Unregister(
103 int64 sw_registration_id,
104 const std::string& sync_registration_tag,
105 SyncPeriodicity periodicity,
106 BackgroundSyncRegistration::RegistrationId sync_registration_id,
107 const StatusCallback& callback) {
108 DCHECK_CURRENTLY_ON(BrowserThread::IO);
109
110 if (disabled_) {
111 BackgroundSyncMetrics::CountUnregister(
112 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
113 base::ThreadTaskRunnerHandle::Get()->PostTask(
114 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR));
115 return;
116 }
117
118 RegistrationKey registration_key(sync_registration_tag, periodicity);
119
120 op_scheduler_.ScheduleOperation(base::Bind(
121 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(),
122 sw_registration_id, registration_key, sync_registration_id, periodicity,
123 MakeStatusCompletion(callback)));
124 }
125
126 void BackgroundSyncManager::GetRegistration( 126 void BackgroundSyncManager::GetRegistration(
127 int64 sw_registration_id, 127 int64 sw_registration_id,
128 const std::string& sync_registration_tag, 128 const std::string& sync_registration_tag,
129 SyncPeriodicity periodicity, 129 SyncPeriodicity periodicity,
130 const StatusAndRegistrationCallback& callback) { 130 const StatusAndRegistrationCallback& callback) {
131 DCHECK_CURRENTLY_ON(BrowserThread::IO); 131 DCHECK_CURRENTLY_ON(BrowserThread::IO);
132 132
133 if (disabled_) { 133 if (disabled_) {
134 base::ThreadTaskRunnerHandle::Get()->PostTask( 134 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
135 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
136 BackgroundSyncRegistration()));
137 return; 135 return;
138 } 136 }
139 137
140 RegistrationKey registration_key(sync_registration_tag, periodicity); 138 RegistrationKey registration_key(sync_registration_tag, periodicity);
141 139
142 op_scheduler_.ScheduleOperation(base::Bind( 140 op_scheduler_.ScheduleOperation(base::Bind(
143 &BackgroundSyncManager::GetRegistrationImpl, 141 &BackgroundSyncManager::GetRegistrationImpl,
144 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, 142 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key,
145 MakeStatusAndRegistrationCompletion(callback))); 143 MakeStatusAndRegistrationCompletion(callback)));
146 } 144 }
147 145
148 void BackgroundSyncManager::GetRegistrations( 146 void BackgroundSyncManager::GetRegistrations(
149 int64 sw_registration_id, 147 int64 sw_registration_id,
150 SyncPeriodicity periodicity, 148 SyncPeriodicity periodicity,
151 const StatusAndRegistrationsCallback& callback) { 149 const StatusAndRegistrationsCallback& callback) {
152 DCHECK_CURRENTLY_ON(BrowserThread::IO); 150 DCHECK_CURRENTLY_ON(BrowserThread::IO);
153 151
154 if (disabled_) { 152 if (disabled_) {
155 base::ThreadTaskRunnerHandle::Get()->PostTask( 153 base::ThreadTaskRunnerHandle::Get()->PostTask(
156 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 154 FROM_HERE,
157 std::vector<BackgroundSyncRegistration>())); 155 base::Bind(
156 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
157 base::Passed(
158 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>()
159 .Pass())));
158 return; 160 return;
159 } 161 }
160 162
161 op_scheduler_.ScheduleOperation( 163 op_scheduler_.ScheduleOperation(
162 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, 164 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl,
163 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 165 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
164 periodicity, MakeStatusAndRegistrationsCompletion(callback))); 166 periodicity, MakeStatusAndRegistrationsCompletion(callback)));
165 } 167 }
166 168
169 // Given a HandleId |handle_id|, return a new handle for the same
170 // registration.
171 scoped_ptr<BackgroundSyncRegistrationHandle>
172 BackgroundSyncManager::DuplicateRegistrationHandle(
173 int64 sw_registration_id,
michaeln 2015/08/28 02:53:11 |sw_registration_id| is not used, did you mean to
174 BackgroundSyncRegistrationHandle::HandleId handle_id) {
175 DCHECK_CURRENTLY_ON(BrowserThread::IO);
176
177 scoped_refptr<RefCountedRegistration>* ref_registration =
178 registration_handle_ids_.Lookup(handle_id);
179 if (!ref_registration)
180 return scoped_ptr<BackgroundSyncRegistrationHandle>();
181 return CreateRegistrationHandle(ref_registration->get());
182 }
183
167 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, 184 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id,
168 const GURL& pattern) { 185 const GURL& pattern) {
169 DCHECK_CURRENTLY_ON(BrowserThread::IO); 186 DCHECK_CURRENTLY_ON(BrowserThread::IO);
170 187
171 // Operations already in the queue will either fail when they write to storage 188 // Operations already in the queue will either fail when they write to storage
172 // or return stale results based on registrations loaded in memory. This is 189 // or return stale results based on registrations loaded in memory. This is
173 // inconsequential since the service worker is gone. 190 // inconsequential since the service worker is gone.
174 op_scheduler_.ScheduleOperation(base::Bind( 191 op_scheduler_.ScheduleOperation(base::Bind(
175 &BackgroundSyncManager::OnRegistrationDeletedImpl, 192 &BackgroundSyncManager::OnRegistrationDeletedImpl,
176 weak_ptr_factory_.GetWeakPtr(), registration_id, MakeEmptyCompletion())); 193 weak_ptr_factory_.GetWeakPtr(), registration_id, MakeEmptyCompletion()));
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 LOG(ERROR) << "BackgroundSync failed to init due to backend failure."; 255 LOG(ERROR) << "BackgroundSync failed to init due to backend failure.";
239 DisableAndClearManager(base::Bind(callback)); 256 DisableAndClearManager(base::Bind(callback));
240 return; 257 return;
241 } 258 }
242 259
243 bool corruption_detected = false; 260 bool corruption_detected = false;
244 for (const std::pair<int64, std::string>& data : user_data) { 261 for (const std::pair<int64, std::string>& data : user_data) {
245 BackgroundSyncRegistrationsProto registrations_proto; 262 BackgroundSyncRegistrationsProto registrations_proto;
246 if (registrations_proto.ParseFromString(data.second)) { 263 if (registrations_proto.ParseFromString(data.second)) {
247 BackgroundSyncRegistrations* registrations = 264 BackgroundSyncRegistrations* registrations =
248 &sw_to_registrations_map_[data.first]; 265 &active_registrations_[data.first];
249 registrations->next_id = registrations_proto.next_registration_id(); 266 registrations->next_id = registrations_proto.next_registration_id();
250 registrations->origin = GURL(registrations_proto.origin()); 267 registrations->origin = GURL(registrations_proto.origin());
251 268
252 for (int i = 0, max = registrations_proto.registration_size(); i < max; 269 for (int i = 0, max = registrations_proto.registration_size(); i < max;
253 ++i) { 270 ++i) {
254 const BackgroundSyncRegistrationProto& registration_proto = 271 const BackgroundSyncRegistrationProto& registration_proto =
255 registrations_proto.registration(i); 272 registrations_proto.registration(i);
256 273
257 if (registration_proto.id() >= registrations->next_id) { 274 if (registration_proto.id() >= registrations->next_id) {
258 corruption_detected = true; 275 corruption_detected = true;
259 break; 276 break;
260 } 277 }
261 278
262 RegistrationKey registration_key(registration_proto.tag(), 279 RegistrationKey registration_key(registration_proto.tag(),
263 registration_proto.periodicity()); 280 registration_proto.periodicity());
264 BackgroundSyncRegistration* registration = 281
265 &registrations->registration_map[registration_key]; 282 scoped_refptr<RefCountedRegistration> ref_registration(
283 new RefCountedRegistration());
284 registrations->registration_map[registration_key] = ref_registration;
285 BackgroundSyncRegistration* registration = ref_registration->value();
266 286
267 BackgroundSyncRegistrationOptions* options = registration->options(); 287 BackgroundSyncRegistrationOptions* options = registration->options();
268 options->tag = registration_proto.tag(); 288 options->tag = registration_proto.tag();
269 options->periodicity = registration_proto.periodicity(); 289 options->periodicity = registration_proto.periodicity();
270 options->min_period = registration_proto.min_period(); 290 options->min_period = registration_proto.min_period();
271 options->network_state = registration_proto.network_state(); 291 options->network_state = registration_proto.network_state();
272 options->power_state = registration_proto.power_state(); 292 options->power_state = registration_proto.power_state();
273 293
274 registration->set_id(registration_proto.id()); 294 registration->set_id(registration_proto.id());
275 registration->set_sync_state(registration_proto.sync_state()); 295 registration->set_sync_state(registration_proto.sync_state());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = 328 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
309 AreOptionConditionsMet(options) 329 AreOptionConditionsMet(options)
310 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE 330 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
311 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; 331 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
312 332
313 if (disabled_) { 333 if (disabled_) {
314 BackgroundSyncMetrics::CountRegister( 334 BackgroundSyncMetrics::CountRegister(
315 options.periodicity, registration_could_fire, 335 options.periodicity, registration_could_fire,
316 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 336 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
317 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 337 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
318 base::ThreadTaskRunnerHandle::Get()->PostTask( 338 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
319 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
320 BackgroundSyncRegistration()));
321 return; 339 return;
322 } 340 }
323 341
324 ServiceWorkerRegistration* sw_registration = 342 ServiceWorkerRegistration* sw_registration =
325 service_worker_context_->GetLiveRegistration(sw_registration_id); 343 service_worker_context_->GetLiveRegistration(sw_registration_id);
326 if (!sw_registration || !sw_registration->active_version()) { 344 if (!sw_registration || !sw_registration->active_version()) {
327 BackgroundSyncMetrics::CountRegister( 345 BackgroundSyncMetrics::CountRegister(
328 options.periodicity, registration_could_fire, 346 options.periodicity, registration_could_fire,
329 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 347 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
330 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); 348 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER);
331 base::ThreadTaskRunnerHandle::Get()->PostTask( 349 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback);
332 FROM_HERE,
333 base::Bind(callback, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER,
334 BackgroundSyncRegistration()));
335 return; 350 return;
336 } 351 }
337 352
338 if (!sw_registration->active_version()->HasWindowClients()) { 353 if (!sw_registration->active_version()->HasWindowClients()) {
339 base::ThreadTaskRunnerHandle::Get()->PostTask( 354 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback);
340 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_ALLOWED,
341 BackgroundSyncRegistration()));
342 return; 355 return;
343 } 356 }
344 357
345 BackgroundSyncRegistration* existing_registration = 358 RefCountedRegistration* existing_registration_ref =
346 LookupRegistration(sw_registration_id, RegistrationKey(options)); 359 LookupActiveRegistration(sw_registration_id, RegistrationKey(options));
347 if (existing_registration && 360 if (existing_registration_ref &&
348 existing_registration->options()->Equals(options)) { 361 existing_registration_ref->value()->options()->Equals(options)) {
362 BackgroundSyncRegistration* existing_registration =
363 existing_registration_ref->value();
349 if (existing_registration->sync_state() == SYNC_STATE_FAILED) { 364 if (existing_registration->sync_state() == SYNC_STATE_FAILED) {
350 existing_registration->set_sync_state(SYNC_STATE_PENDING); 365 existing_registration->set_sync_state(SYNC_STATE_PENDING);
351 StoreRegistrations( 366 StoreRegistrations(
352 sw_registration_id, 367 sw_registration_id,
353 base::Bind(&BackgroundSyncManager::RegisterDidStore, 368 base::Bind(&BackgroundSyncManager::RegisterDidStore,
354 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 369 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
355 *existing_registration, callback)); 370 make_scoped_refptr(existing_registration_ref), callback));
356 return; 371 return;
357 } 372 }
358 373
359 // Record the duplicated registration 374 // Record the duplicated registration
360 BackgroundSyncMetrics::CountRegister( 375 BackgroundSyncMetrics::CountRegister(
361 existing_registration->options()->periodicity, registration_could_fire, 376 existing_registration->options()->periodicity, registration_could_fire,
362 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE, 377 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE,
363 BACKGROUND_SYNC_STATUS_OK); 378 BACKGROUND_SYNC_STATUS_OK);
364 379
365 base::ThreadTaskRunnerHandle::Get()->PostTask( 380 base::ThreadTaskRunnerHandle::Get()->PostTask(
366 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, 381 FROM_HERE,
367 *existing_registration)); 382 base::Bind(
383 callback, BACKGROUND_SYNC_STATUS_OK,
384 base::Passed(
385 CreateRegistrationHandle(existing_registration_ref).Pass())));
368 return; 386 return;
369 } 387 }
370 388
371 BackgroundSyncRegistration new_registration; 389 scoped_refptr<RefCountedRegistration> new_ref_registration(
372 *new_registration.options() = options; 390 new RefCountedRegistration());
391 BackgroundSyncRegistration* new_registration = new_ref_registration->value();
392
393 *new_registration->options() = options;
373 394
374 BackgroundSyncRegistrations* registrations = 395 BackgroundSyncRegistrations* registrations =
375 &sw_to_registrations_map_[sw_registration_id]; 396 &active_registrations_[sw_registration_id];
376 new_registration.set_id(registrations->next_id++); 397 new_registration->set_id(registrations->next_id++);
377 398
378 AddRegistrationToMap(sw_registration_id, 399 AddActiveRegistration(sw_registration_id,
379 sw_registration->pattern().GetOrigin(), 400 sw_registration->pattern().GetOrigin(),
380 new_registration); 401 new_ref_registration);
381 402
382 StoreRegistrations( 403 StoreRegistrations(
383 sw_registration_id, 404 sw_registration_id,
384 base::Bind(&BackgroundSyncManager::RegisterDidStore, 405 base::Bind(&BackgroundSyncManager::RegisterDidStore,
385 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 406 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
386 new_registration, callback)); 407 new_ref_registration, callback));
387 } 408 }
388 409
389 void BackgroundSyncManager::DisableAndClearManager( 410 void BackgroundSyncManager::DisableAndClearManager(
390 const base::Closure& callback) { 411 const base::Closure& callback) {
391 DCHECK_CURRENTLY_ON(BrowserThread::IO); 412 DCHECK_CURRENTLY_ON(BrowserThread::IO);
392 413
393 if (disabled_) { 414 if (disabled_) {
394 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 415 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
395 base::Bind(callback)); 416 base::Bind(callback));
396 return; 417 return;
397 } 418 }
398 419
399 disabled_ = true; 420 disabled_ = true;
400 sw_to_registrations_map_.clear(); 421 active_registrations_.clear();
401 422
402 // Delete all backend entries. The memory representation of registered syncs 423 // Delete all backend entries. The memory representation of registered syncs
403 // may be out of sync with storage (e.g., due to corruption detection on 424 // may be out of sync with storage (e.g., due to corruption detection on
404 // loading from storage), so reload the registrations from storage again. 425 // loading from storage), so reload the registrations from storage again.
405 GetDataFromBackend( 426 GetDataFromBackend(
406 kBackgroundSyncUserDataKey, 427 kBackgroundSyncUserDataKey,
407 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, 428 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations,
408 weak_ptr_factory_.GetWeakPtr(), callback)); 429 weak_ptr_factory_.GetWeakPtr(), callback));
409 } 430 }
410 431
(...skipping 23 matching lines...) Expand all
434 void BackgroundSyncManager::DisableAndClearManagerClearedOne( 455 void BackgroundSyncManager::DisableAndClearManagerClearedOne(
435 const base::Closure& barrier_closure, 456 const base::Closure& barrier_closure,
436 ServiceWorkerStatusCode status) { 457 ServiceWorkerStatusCode status) {
437 DCHECK_CURRENTLY_ON(BrowserThread::IO); 458 DCHECK_CURRENTLY_ON(BrowserThread::IO);
438 459
439 // The status doesn't matter at this point, there is nothing else to be done. 460 // The status doesn't matter at this point, there is nothing else to be done.
440 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 461 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
441 base::Bind(barrier_closure)); 462 base::Bind(barrier_closure));
442 } 463 }
443 464
444 BackgroundSyncRegistration* BackgroundSyncManager::LookupRegistration( 465 BackgroundSyncManager::RefCountedRegistration*
466 BackgroundSyncManager::LookupActiveRegistration(
445 int64 sw_registration_id, 467 int64 sw_registration_id,
446 const RegistrationKey& registration_key) { 468 const RegistrationKey& registration_key) {
447 DCHECK_CURRENTLY_ON(BrowserThread::IO); 469 DCHECK_CURRENTLY_ON(BrowserThread::IO);
448 470
449 SWIdToRegistrationsMap::iterator it = 471 SWIdToRegistrationsMap::iterator it =
450 sw_to_registrations_map_.find(sw_registration_id); 472 active_registrations_.find(sw_registration_id);
451 if (it == sw_to_registrations_map_.end()) 473 if (it == active_registrations_.end())
452 return nullptr; 474 return nullptr;
453 475
454 BackgroundSyncRegistrations& registrations = it->second; 476 BackgroundSyncRegistrations& registrations = it->second;
455 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); 477 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id);
456 DCHECK(!registrations.origin.is_empty()); 478 DCHECK(!registrations.origin.is_empty());
457 479
458 auto key_and_registration_iter = 480 auto key_and_registration_iter =
459 registrations.registration_map.find(registration_key); 481 registrations.registration_map.find(registration_key);
460 if (key_and_registration_iter == registrations.registration_map.end()) 482 if (key_and_registration_iter == registrations.registration_map.end())
461 return nullptr; 483 return nullptr;
462 484
463 return &key_and_registration_iter->second; 485 return key_and_registration_iter->second.get();
464 } 486 }
465 487
466 void BackgroundSyncManager::StoreRegistrations( 488 void BackgroundSyncManager::StoreRegistrations(
467 int64 sw_registration_id, 489 int64 sw_registration_id,
468 const ServiceWorkerStorage::StatusCallback& callback) { 490 const ServiceWorkerStorage::StatusCallback& callback) {
469 DCHECK_CURRENTLY_ON(BrowserThread::IO); 491 DCHECK_CURRENTLY_ON(BrowserThread::IO);
470 492
471 // Serialize the data. 493 // Serialize the data.
472 const BackgroundSyncRegistrations& registrations = 494 const BackgroundSyncRegistrations& registrations =
473 sw_to_registrations_map_[sw_registration_id]; 495 active_registrations_[sw_registration_id];
474 BackgroundSyncRegistrationsProto registrations_proto; 496 BackgroundSyncRegistrationsProto registrations_proto;
475 registrations_proto.set_next_registration_id(registrations.next_id); 497 registrations_proto.set_next_registration_id(registrations.next_id);
476 registrations_proto.set_origin(registrations.origin.spec()); 498 registrations_proto.set_origin(registrations.origin.spec());
477 499
478 for (const auto& key_and_registration : registrations.registration_map) { 500 for (const auto& key_and_registration : registrations.registration_map) {
479 const BackgroundSyncRegistration& registration = 501 const BackgroundSyncRegistration& registration =
480 key_and_registration.second; 502 *key_and_registration.second->value();
481 BackgroundSyncRegistrationProto* registration_proto = 503 BackgroundSyncRegistrationProto* registration_proto =
482 registrations_proto.add_registration(); 504 registrations_proto.add_registration();
483 registration_proto->set_id(registration.id()); 505 registration_proto->set_id(registration.id());
484 registration_proto->set_sync_state(registration.sync_state()); 506 registration_proto->set_sync_state(registration.sync_state());
485 registration_proto->set_tag(registration.options()->tag); 507 registration_proto->set_tag(registration.options()->tag);
486 registration_proto->set_periodicity(registration.options()->periodicity); 508 registration_proto->set_periodicity(registration.options()->periodicity);
487 registration_proto->set_min_period(registration.options()->min_period); 509 registration_proto->set_min_period(registration.options()->min_period);
488 registration_proto->set_network_state( 510 registration_proto->set_network_state(
489 registration.options()->network_state); 511 registration.options()->network_state);
490 registration_proto->set_power_state(registration.options()->power_state); 512 registration_proto->set_power_state(registration.options()->power_state);
491 } 513 }
492 std::string serialized; 514 std::string serialized;
493 bool success = registrations_proto.SerializeToString(&serialized); 515 bool success = registrations_proto.SerializeToString(&serialized);
494 DCHECK(success); 516 DCHECK(success);
495 517
496 StoreDataInBackend(sw_registration_id, registrations.origin, 518 StoreDataInBackend(sw_registration_id, registrations.origin,
497 kBackgroundSyncUserDataKey, serialized, callback); 519 kBackgroundSyncUserDataKey, serialized, callback);
498 } 520 }
499 521
500 void BackgroundSyncManager::RegisterDidStore( 522 void BackgroundSyncManager::RegisterDidStore(
501 int64 sw_registration_id, 523 int64 sw_registration_id,
502 const BackgroundSyncRegistration& new_registration, 524 const scoped_refptr<RefCountedRegistration>& new_registration_ref,
503 const StatusAndRegistrationCallback& callback, 525 const StatusAndRegistrationCallback& callback,
504 ServiceWorkerStatusCode status) { 526 ServiceWorkerStatusCode status) {
505 DCHECK_CURRENTLY_ON(BrowserThread::IO); 527 DCHECK_CURRENTLY_ON(BrowserThread::IO);
506 528
529 const BackgroundSyncRegistration* new_registration =
530 new_registration_ref->value();
531
507 // For UMA, determine here whether the sync could fire immediately 532 // For UMA, determine here whether the sync could fire immediately
508 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = 533 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
509 AreOptionConditionsMet(*new_registration.options()) 534 AreOptionConditionsMet(*new_registration->options())
510 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE 535 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
511 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; 536 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
512 537
513 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { 538 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) {
514 // The service worker registration is gone. 539 // The service worker registration is gone.
515 BackgroundSyncMetrics::CountRegister( 540 BackgroundSyncMetrics::CountRegister(
516 new_registration.options()->periodicity, registration_could_fire, 541 new_registration->options()->periodicity, registration_could_fire,
517 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 542 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
518 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 543 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
519 sw_to_registrations_map_.erase(sw_registration_id); 544 active_registrations_.erase(sw_registration_id);
520 base::ThreadTaskRunnerHandle::Get()->PostTask( 545 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
521 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
522 BackgroundSyncRegistration()));
523 return; 546 return;
524 } 547 }
525 548
526 if (status != SERVICE_WORKER_OK) { 549 if (status != SERVICE_WORKER_OK) {
527 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " 550 LOG(ERROR) << "BackgroundSync failed to store registration due to backend "
528 "failure."; 551 "failure.";
529 BackgroundSyncMetrics::CountRegister( 552 BackgroundSyncMetrics::CountRegister(
530 new_registration.options()->periodicity, registration_could_fire, 553 new_registration->options()->periodicity, registration_could_fire,
531 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 554 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
532 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 555 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
533 DisableAndClearManager(base::Bind(callback, 556 DisableAndClearManager(base::Bind(
534 BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 557 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
535 BackgroundSyncRegistration())); 558 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass())));
536 return; 559 return;
537 } 560 }
538 561
539 BackgroundSyncMetrics::CountRegister( 562 BackgroundSyncMetrics::CountRegister(
540 new_registration.options()->periodicity, registration_could_fire, 563 new_registration->options()->periodicity, registration_could_fire,
541 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 564 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
542 BACKGROUND_SYNC_STATUS_OK); 565 BACKGROUND_SYNC_STATUS_OK);
543 FireReadyEvents(); 566 FireReadyEvents();
544 base::ThreadTaskRunnerHandle::Get()->PostTask( 567 base::ThreadTaskRunnerHandle::Get()->PostTask(
545 FROM_HERE, 568 FROM_HERE,
546 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, new_registration)); 569 base::Bind(
570 callback, BACKGROUND_SYNC_STATUS_OK,
571 base::Passed(
572 CreateRegistrationHandle(new_registration_ref.get()).Pass())));
547 } 573 }
548 574
549 void BackgroundSyncManager::RemoveRegistrationFromMap( 575 void BackgroundSyncManager::RemoveActiveRegistration(
550 int64 sw_registration_id, 576 int64 sw_registration_id,
551 const RegistrationKey& registration_key) { 577 const RegistrationKey& registration_key) {
552 DCHECK_CURRENTLY_ON(BrowserThread::IO); 578 DCHECK_CURRENTLY_ON(BrowserThread::IO);
553 DCHECK(LookupRegistration(sw_registration_id, registration_key)); 579 DCHECK(LookupActiveRegistration(sw_registration_id, registration_key));
554 580
555 BackgroundSyncRegistrations* registrations = 581 BackgroundSyncRegistrations* registrations =
556 &sw_to_registrations_map_[sw_registration_id]; 582 &active_registrations_[sw_registration_id];
557 583
558 registrations->registration_map.erase(registration_key); 584 registrations->registration_map.erase(registration_key);
559 } 585 }
560 586
561 void BackgroundSyncManager::AddRegistrationToMap( 587 void BackgroundSyncManager::AddActiveRegistration(
562 int64 sw_registration_id, 588 int64 sw_registration_id,
563 const GURL& origin, 589 const GURL& origin,
564 const BackgroundSyncRegistration& sync_registration) { 590 const scoped_refptr<RefCountedRegistration>& sync_registration) {
565 DCHECK_CURRENTLY_ON(BrowserThread::IO); 591 DCHECK_CURRENTLY_ON(BrowserThread::IO);
566 DCHECK(sync_registration.IsValid()); 592 DCHECK(sync_registration->value()->IsValid());
567 593
568 BackgroundSyncRegistrations* registrations = 594 BackgroundSyncRegistrations* registrations =
569 &sw_to_registrations_map_[sw_registration_id]; 595 &active_registrations_[sw_registration_id];
570 registrations->origin = origin; 596 registrations->origin = origin;
571 597
572 RegistrationKey registration_key(sync_registration); 598 RegistrationKey registration_key(*sync_registration->value());
573 registrations->registration_map[registration_key] = sync_registration; 599 registrations->registration_map[registration_key] = sync_registration;
574 } 600 }
575 601
576 void BackgroundSyncManager::StoreDataInBackend( 602 void BackgroundSyncManager::StoreDataInBackend(
577 int64 sw_registration_id, 603 int64 sw_registration_id,
578 const GURL& origin, 604 const GURL& origin,
579 const std::string& backend_key, 605 const std::string& backend_key,
580 const std::string& data, 606 const std::string& data,
581 const ServiceWorkerStorage::StatusCallback& callback) { 607 const ServiceWorkerStorage::StatusCallback& callback) {
582 DCHECK_CURRENTLY_ON(BrowserThread::IO); 608 DCHECK_CURRENTLY_ON(BrowserThread::IO);
583 609
584 service_worker_context_->StoreRegistrationUserData( 610 service_worker_context_->StoreRegistrationUserData(
585 sw_registration_id, origin, backend_key, data, callback); 611 sw_registration_id, origin, backend_key, data, callback);
586 } 612 }
587 613
588 void BackgroundSyncManager::GetDataFromBackend( 614 void BackgroundSyncManager::GetDataFromBackend(
589 const std::string& backend_key, 615 const std::string& backend_key,
590 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& 616 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
591 callback) { 617 callback) {
592 DCHECK_CURRENTLY_ON(BrowserThread::IO); 618 DCHECK_CURRENTLY_ON(BrowserThread::IO);
593 619
594 service_worker_context_->GetUserDataForAllRegistrations(backend_key, 620 service_worker_context_->GetUserDataForAllRegistrations(backend_key,
595 callback); 621 callback);
596 } 622 }
597 623
598 void BackgroundSyncManager::FireOneShotSync( 624 void BackgroundSyncManager::FireOneShotSync(
599 const BackgroundSyncRegistration& registration, 625 BackgroundSyncRegistrationHandle::HandleId handle_id,
600 const scoped_refptr<ServiceWorkerVersion>& active_version, 626 const scoped_refptr<ServiceWorkerVersion>& active_version,
601 const ServiceWorkerVersion::StatusCallback& callback) { 627 const ServiceWorkerVersion::StatusCallback& callback) {
602 DCHECK_CURRENTLY_ON(BrowserThread::IO); 628 DCHECK_CURRENTLY_ON(BrowserThread::IO);
629 DCHECK(active_version);
603 630
604 active_version->DispatchSyncEvent( 631 // The ServiceWorkerVersion doesn't know when the client (javascript) is done
605 mojo::ConvertTo<SyncRegistrationPtr>(registration), callback); 632 // with the registration so don't give it a BackgroundSyncRegistrationHandle.
633 // Once the render process gets the handle_id it can create its own handle
634 // (with a new unique handle id).
635 active_version->DispatchSyncEvent(handle_id, callback);
636 }
637
638 scoped_ptr<BackgroundSyncRegistrationHandle>
639 BackgroundSyncManager::CreateRegistrationHandle(
640 RefCountedRegistration* registration) {
641 scoped_refptr<RefCountedRegistration>* ptr =
642 new scoped_refptr<RefCountedRegistration>(registration);
643
644 // Registration handles have unique handle ids. The handle id maps to an
645 // internal RefCountedRegistration (which has the persistent registration id)
646 // via
647 // registration_reference_ids_.
648 BackgroundSyncRegistrationHandle::HandleId handle_id =
649 registration_handle_ids_.Add(ptr);
650
651 return make_scoped_ptr(new BackgroundSyncRegistrationHandle(
652 weak_ptr_factory_.GetWeakPtr(), handle_id));
653 }
654
655 BackgroundSyncRegistration* BackgroundSyncManager::GetRegistrationForHandle(
656 BackgroundSyncRegistrationHandle::HandleId handle_id) const {
657 scoped_refptr<RefCountedRegistration>* ref_registration =
658 registration_handle_ids_.Lookup(handle_id);
659 if (!ref_registration)
660 return nullptr;
661 return (*ref_registration)->value();
662 }
663
664 void BackgroundSyncManager::ReleaseRegistrationHandle(
665 BackgroundSyncRegistrationHandle::HandleId handle_id) {
666 DCHECK_CURRENTLY_ON(BrowserThread::IO);
667 DCHECK(registration_handle_ids_.Lookup(handle_id));
668 registration_handle_ids_.Remove(handle_id);
669 }
670
671 void BackgroundSyncManager::Unregister(
672 int64 sw_registration_id,
673 SyncPeriodicity periodicity,
674 BackgroundSyncRegistrationHandle::HandleId handle_id,
675 const StatusCallback& callback) {
676 DCHECK_CURRENTLY_ON(BrowserThread::IO);
677
678 if (disabled_) {
679 BackgroundSyncMetrics::CountUnregister(
680 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
681 base::ThreadTaskRunnerHandle::Get()->PostTask(
682 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR));
683 return;
684 }
685
686 BackgroundSyncRegistration* registration =
687 GetRegistrationForHandle(handle_id);
688 DCHECK(registration);
689
690 op_scheduler_.ScheduleOperation(base::Bind(
691 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(),
692 sw_registration_id, RegistrationKey(*registration), registration->id(),
693 periodicity, MakeStatusCompletion(callback)));
606 } 694 }
607 695
608 void BackgroundSyncManager::UnregisterImpl( 696 void BackgroundSyncManager::UnregisterImpl(
609 int64 sw_registration_id, 697 int64 sw_registration_id,
610 const RegistrationKey& registration_key, 698 const RegistrationKey& registration_key,
611 BackgroundSyncRegistration::RegistrationId sync_registration_id, 699 BackgroundSyncRegistration::RegistrationId sync_registration_id,
612 SyncPeriodicity periodicity, 700 SyncPeriodicity periodicity,
613 const StatusCallback& callback) { 701 const StatusCallback& callback) {
614 DCHECK_CURRENTLY_ON(BrowserThread::IO); 702 DCHECK_CURRENTLY_ON(BrowserThread::IO);
615 703
616 if (disabled_) { 704 if (disabled_) {
617 BackgroundSyncMetrics::CountUnregister( 705 BackgroundSyncMetrics::CountUnregister(
618 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 706 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
619 base::ThreadTaskRunnerHandle::Get()->PostTask( 707 base::ThreadTaskRunnerHandle::Get()->PostTask(
620 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); 708 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR));
621 return; 709 return;
622 } 710 }
623 711
624 const BackgroundSyncRegistration* existing_registration = 712 const RefCountedRegistration* existing_registration =
625 LookupRegistration(sw_registration_id, registration_key); 713 LookupActiveRegistration(sw_registration_id, registration_key);
714
626 if (!existing_registration || 715 if (!existing_registration ||
627 existing_registration->id() != sync_registration_id) { 716 existing_registration->value()->id() != sync_registration_id) {
628 BackgroundSyncMetrics::CountUnregister(periodicity, 717 BackgroundSyncMetrics::CountUnregister(periodicity,
629 BACKGROUND_SYNC_STATUS_NOT_FOUND); 718 BACKGROUND_SYNC_STATUS_NOT_FOUND);
630 base::ThreadTaskRunnerHandle::Get()->PostTask( 719 base::ThreadTaskRunnerHandle::Get()->PostTask(
631 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); 720 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND));
632 return; 721 return;
633 } 722 }
634 723
635 RemoveRegistrationFromMap(sw_registration_id, registration_key); 724 RemoveActiveRegistration(sw_registration_id, registration_key);
636 725
637 StoreRegistrations(sw_registration_id, 726 StoreRegistrations(sw_registration_id,
638 base::Bind(&BackgroundSyncManager::UnregisterDidStore, 727 base::Bind(&BackgroundSyncManager::UnregisterDidStore,
639 weak_ptr_factory_.GetWeakPtr(), 728 weak_ptr_factory_.GetWeakPtr(),
640 sw_registration_id, periodicity, callback)); 729 sw_registration_id, periodicity, callback));
641 } 730 }
642 731
643 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, 732 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id,
644 SyncPeriodicity periodicity, 733 SyncPeriodicity periodicity,
645 const StatusCallback& callback, 734 const StatusCallback& callback,
646 ServiceWorkerStatusCode status) { 735 ServiceWorkerStatusCode status) {
647 DCHECK_CURRENTLY_ON(BrowserThread::IO); 736 DCHECK_CURRENTLY_ON(BrowserThread::IO);
648 737
649 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { 738 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) {
650 // ServiceWorker was unregistered. 739 // ServiceWorker was unregistered.
651 BackgroundSyncMetrics::CountUnregister( 740 BackgroundSyncMetrics::CountUnregister(
652 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 741 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
653 sw_to_registrations_map_.erase(sw_registration_id); 742 active_registrations_.erase(sw_registration_id);
654 base::ThreadTaskRunnerHandle::Get()->PostTask( 743 base::ThreadTaskRunnerHandle::Get()->PostTask(
655 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); 744 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR));
656 return; 745 return;
657 } 746 }
658 747
659 if (status != SERVICE_WORKER_OK) { 748 if (status != SERVICE_WORKER_OK) {
660 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; 749 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure.";
661 BackgroundSyncMetrics::CountUnregister( 750 BackgroundSyncMetrics::CountUnregister(
662 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 751 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
663 DisableAndClearManager( 752 DisableAndClearManager(
664 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); 753 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR));
665 return; 754 return;
666 } 755 }
667 756
668 BackgroundSyncMetrics::CountUnregister(periodicity, 757 BackgroundSyncMetrics::CountUnregister(periodicity,
669 BACKGROUND_SYNC_STATUS_OK); 758 BACKGROUND_SYNC_STATUS_OK);
670 base::ThreadTaskRunnerHandle::Get()->PostTask( 759 base::ThreadTaskRunnerHandle::Get()->PostTask(
671 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); 760 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK));
672 } 761 }
673 762
674 void BackgroundSyncManager::GetRegistrationImpl( 763 void BackgroundSyncManager::GetRegistrationImpl(
675 int64 sw_registration_id, 764 int64 sw_registration_id,
676 const RegistrationKey& registration_key, 765 const RegistrationKey& registration_key,
677 const StatusAndRegistrationCallback& callback) { 766 const StatusAndRegistrationCallback& callback) {
678 DCHECK_CURRENTLY_ON(BrowserThread::IO); 767 DCHECK_CURRENTLY_ON(BrowserThread::IO);
679 768
680 if (disabled_) { 769 if (disabled_) {
681 base::ThreadTaskRunnerHandle::Get()->PostTask( 770 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
682 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
683 BackgroundSyncRegistration()));
684 return; 771 return;
685 } 772 }
686 773
687 const BackgroundSyncRegistration* out_registration = 774 RefCountedRegistration* registration =
688 LookupRegistration(sw_registration_id, registration_key); 775 LookupActiveRegistration(sw_registration_id, registration_key);
689 if (!out_registration) { 776 if (!registration) {
690 base::ThreadTaskRunnerHandle::Get()->PostTask( 777 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_FOUND, callback);
691 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND,
692 BackgroundSyncRegistration()));
693 return; 778 return;
694 } 779 }
695 780
696 base::ThreadTaskRunnerHandle::Get()->PostTask( 781 base::ThreadTaskRunnerHandle::Get()->PostTask(
697 FROM_HERE, 782 FROM_HERE,
698 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, *out_registration)); 783 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK,
784 base::Passed(CreateRegistrationHandle(registration).Pass())));
699 } 785 }
700 786
701 void BackgroundSyncManager::GetRegistrationsImpl( 787 void BackgroundSyncManager::GetRegistrationsImpl(
702 int64 sw_registration_id, 788 int64 sw_registration_id,
703 SyncPeriodicity periodicity, 789 SyncPeriodicity periodicity,
704 const StatusAndRegistrationsCallback& callback) { 790 const StatusAndRegistrationsCallback& callback) {
705 DCHECK_CURRENTLY_ON(BrowserThread::IO); 791 DCHECK_CURRENTLY_ON(BrowserThread::IO);
706 792
707 std::vector<BackgroundSyncRegistration> out_registrations; 793 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations(
794 new ScopedVector<BackgroundSyncRegistrationHandle>());
708 795
709 if (disabled_) { 796 if (disabled_) {
710 base::ThreadTaskRunnerHandle::Get()->PostTask( 797 base::ThreadTaskRunnerHandle::Get()->PostTask(
711 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 798 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
712 out_registrations)); 799 base::Passed(out_registrations.Pass())));
713 return; 800 return;
714 } 801 }
715 802
716 SWIdToRegistrationsMap::iterator it = 803 SWIdToRegistrationsMap::iterator it =
717 sw_to_registrations_map_.find(sw_registration_id); 804 active_registrations_.find(sw_registration_id);
718 805
719 if (it != sw_to_registrations_map_.end()) { 806 if (it != active_registrations_.end()) {
720 const BackgroundSyncRegistrations& registrations = it->second; 807 const BackgroundSyncRegistrations& registrations = it->second;
721 for (const auto& tag_and_registration : registrations.registration_map) { 808 for (const auto& tag_and_registration : registrations.registration_map) {
722 const BackgroundSyncRegistration& registration = 809 RefCountedRegistration* registration = tag_and_registration.second.get();
723 tag_and_registration.second; 810 if (registration->value()->options()->periodicity == periodicity) {
724 if (registration.options()->periodicity == periodicity) 811 out_registrations->push_back(
725 out_registrations.push_back(registration); 812 CreateRegistrationHandle(registration).release());
813 }
726 } 814 }
727 } 815 }
728 816
729 base::ThreadTaskRunnerHandle::Get()->PostTask( 817 base::ThreadTaskRunnerHandle::Get()->PostTask(
730 FROM_HERE, 818 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK,
731 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, out_registrations)); 819 base::Passed(out_registrations.Pass())));
732 } 820 }
733 821
734 bool BackgroundSyncManager::AreOptionConditionsMet( 822 bool BackgroundSyncManager::AreOptionConditionsMet(
735 const BackgroundSyncRegistrationOptions& options) { 823 const BackgroundSyncRegistrationOptions& options) {
736 DCHECK_CURRENTLY_ON(BrowserThread::IO); 824 DCHECK_CURRENTLY_ON(BrowserThread::IO);
737 return network_observer_->NetworkSufficient(options.network_state) && 825 return network_observer_->NetworkSufficient(options.network_state) &&
738 power_observer_->PowerSufficient(options.power_state); 826 power_observer_->PowerSufficient(options.power_state);
739 } 827 }
740 828
741 bool BackgroundSyncManager::IsRegistrationReadyToFire( 829 bool BackgroundSyncManager::IsRegistrationReadyToFire(
742 const BackgroundSyncRegistration& registration) { 830 const BackgroundSyncRegistration& registration) {
743 DCHECK_CURRENTLY_ON(BrowserThread::IO); 831 DCHECK_CURRENTLY_ON(BrowserThread::IO);
744 832
745 // TODO(jkarlin): Add support for firing periodic registrations. 833 // TODO(jkarlin): Add support for firing periodic registrations.
746 if (registration.options()->periodicity == SYNC_PERIODIC) 834 if (registration.options()->periodicity == SYNC_PERIODIC)
747 return false; 835 return false;
748 836
749 if (registration.sync_state() != SYNC_STATE_PENDING) 837 if (registration.sync_state() != SYNC_STATE_PENDING)
750 return false; 838 return false;
751 839
752 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); 840 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity);
753 841
754 return AreOptionConditionsMet(*registration.options()); 842 return AreOptionConditionsMet(*registration.options());
755 } 843 }
756 844
757 void BackgroundSyncManager::SchedulePendingRegistrations() { 845 void BackgroundSyncManager::SchedulePendingRegistrations() {
758 #if defined(OS_ANDROID) 846 #if defined(OS_ANDROID)
759 bool keep_browser_alive_for_one_shot = false; 847 bool keep_browser_alive_for_one_shot = false;
760 848
761 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { 849 for (const auto& sw_id_and_registrations : active_registrations_) {
762 for (const auto& key_and_registration : 850 for (const auto& key_and_registration :
763 sw_id_and_registrations.second.registration_map) { 851 sw_id_and_registrations.second.registration_map) {
764 const BackgroundSyncRegistration& registration = 852 const BackgroundSyncRegistration& registration =
765 key_and_registration.second; 853 *key_and_registration.second->value();
766 if (registration.sync_state() == SYNC_STATE_PENDING) { 854 if (registration.sync_state() == SYNC_STATE_PENDING) {
767 if (registration.options()->periodicity == SYNC_ONE_SHOT) { 855 if (registration.options()->periodicity == SYNC_ONE_SHOT) {
768 keep_browser_alive_for_one_shot = true; 856 keep_browser_alive_for_one_shot = true;
769 } else { 857 } else {
770 // TODO(jkarlin): Support keeping the browser alive for periodic 858 // TODO(jkarlin): Support keeping the browser alive for periodic
771 // syncs. 859 // syncs.
772 } 860 }
773 } 861 }
774 } 862 }
775 } 863 }
(...skipping 25 matching lines...) Expand all
801 889
802 if (disabled_) { 890 if (disabled_) {
803 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 891 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
804 base::Bind(callback)); 892 base::Bind(callback));
805 return; 893 return;
806 } 894 }
807 895
808 // Find the registrations that are ready to run. 896 // Find the registrations that are ready to run.
809 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire; 897 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire;
810 898
811 for (auto& sw_id_and_registrations : sw_to_registrations_map_) { 899 for (auto& sw_id_and_registrations : active_registrations_) {
812 const int64 service_worker_id = sw_id_and_registrations.first; 900 const int64 service_worker_id = sw_id_and_registrations.first;
813 for (auto& key_and_registration : 901 for (auto& key_and_registration :
814 sw_id_and_registrations.second.registration_map) { 902 sw_id_and_registrations.second.registration_map) {
815 BackgroundSyncRegistration* registration = &key_and_registration.second; 903 BackgroundSyncRegistration* registration =
904 key_and_registration.second->value();
816 if (IsRegistrationReadyToFire(*registration)) { 905 if (IsRegistrationReadyToFire(*registration)) {
817 sw_id_and_keys_to_fire.push_back( 906 sw_id_and_keys_to_fire.push_back(
818 std::make_pair(service_worker_id, key_and_registration.first)); 907 std::make_pair(service_worker_id, key_and_registration.first));
819 // The state change is not saved to persistent storage because 908 // The state change is not saved to persistent storage because
820 // if the sync event is killed mid-sync then it should return to 909 // if the sync event is killed mid-sync then it should return to
821 // SYNC_STATE_PENDING. 910 // SYNC_STATE_PENDING.
822 registration->set_sync_state(SYNC_STATE_FIRING); 911 registration->set_sync_state(SYNC_STATE_FIRING);
823 } 912 }
824 } 913 }
825 } 914 }
(...skipping 12 matching lines...) Expand all
838 sw_id_and_keys_to_fire.size(), base::Bind(callback)); 927 sw_id_and_keys_to_fire.size(), base::Bind(callback));
839 928
840 // Record the total time taken after all events have run to completion. 929 // Record the total time taken after all events have run to completion.
841 base::Closure events_completed_barrier_closure = 930 base::Closure events_completed_barrier_closure =
842 base::BarrierClosure(sw_id_and_keys_to_fire.size(), 931 base::BarrierClosure(sw_id_and_keys_to_fire.size(),
843 base::Bind(&OnAllSyncEventsCompleted, start_time, 932 base::Bind(&OnAllSyncEventsCompleted, start_time,
844 sw_id_and_keys_to_fire.size())); 933 sw_id_and_keys_to_fire.size()));
845 934
846 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { 935 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) {
847 int64 service_worker_id = sw_id_and_key.first; 936 int64 service_worker_id = sw_id_and_key.first;
848 const BackgroundSyncRegistration* registration = 937 const RefCountedRegistration* registration =
849 LookupRegistration(service_worker_id, sw_id_and_key.second); 938 LookupActiveRegistration(service_worker_id, sw_id_and_key.second);
939 DCHECK(registration);
850 940
851 service_worker_context_->FindRegistrationForId( 941 service_worker_context_->FindRegistrationForId(
852 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, 942 service_worker_id, active_registrations_[service_worker_id].origin,
853 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, 943 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration,
854 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, 944 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second,
855 registration->id(), events_fired_barrier_closure, 945 registration->value()->id(), events_fired_barrier_closure,
856 events_completed_barrier_closure)); 946 events_completed_barrier_closure));
857 } 947 }
858 } 948 }
859 949
860 SchedulePendingRegistrations(); 950 SchedulePendingRegistrations();
861 } 951 }
862 952
863 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( 953 void BackgroundSyncManager::FireReadyEventsDidFindRegistration(
864 const RegistrationKey& registration_key, 954 const RegistrationKey& registration_key,
865 BackgroundSyncRegistration::RegistrationId registration_id, 955 BackgroundSyncRegistration::RegistrationId registration_id,
866 const base::Closure& event_fired_callback, 956 const base::Closure& event_fired_callback,
867 const base::Closure& event_completed_callback, 957 const base::Closure& event_completed_callback,
868 ServiceWorkerStatusCode service_worker_status, 958 ServiceWorkerStatusCode service_worker_status,
869 const scoped_refptr<ServiceWorkerRegistration>& 959 const scoped_refptr<ServiceWorkerRegistration>&
870 service_worker_registration) { 960 service_worker_registration) {
871 DCHECK_CURRENTLY_ON(BrowserThread::IO); 961 DCHECK_CURRENTLY_ON(BrowserThread::IO);
872 if (service_worker_status != SERVICE_WORKER_OK) { 962 if (service_worker_status != SERVICE_WORKER_OK) {
873 base::ThreadTaskRunnerHandle::Get()->PostTask( 963 base::ThreadTaskRunnerHandle::Get()->PostTask(
874 FROM_HERE, base::Bind(event_fired_callback)); 964 FROM_HERE, base::Bind(event_fired_callback));
875 base::ThreadTaskRunnerHandle::Get()->PostTask( 965 base::ThreadTaskRunnerHandle::Get()->PostTask(
876 FROM_HERE, base::Bind(event_completed_callback)); 966 FROM_HERE, base::Bind(event_completed_callback));
877 return; 967 return;
878 } 968 }
879 969
880 BackgroundSyncRegistration* registration = 970 RefCountedRegistration* registration = LookupActiveRegistration(
881 LookupRegistration(service_worker_registration->id(), registration_key); 971 service_worker_registration->id(), registration_key);
972 DCHECK(registration);
973
974 // Create a handle and keep it until the sync event completes. The client can
975 // acquire its own handle for longer-term use.
976 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle =
977 CreateRegistrationHandle(registration);
882 978
883 FireOneShotSync( 979 FireOneShotSync(
884 *registration, service_worker_registration->active_version(), 980 registration_handle->handle_id(),
jkarlin 2015/08/25 21:31:46 registration_handle is passed later in this call,
jkarlin 2015/08/26 14:22:50 Done.
885 base::Bind(&BackgroundSyncManager::EventComplete, 981 service_worker_registration->active_version(),
886 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, 982 base::Bind(
887 service_worker_registration->id(), registration_key, 983 &BackgroundSyncManager::EventComplete, weak_ptr_factory_.GetWeakPtr(),
888 registration_id, event_completed_callback)); 984 service_worker_registration, service_worker_registration->id(),
985 base::Passed(registration_handle.Pass()), event_completed_callback));
889 986
890 base::ThreadTaskRunnerHandle::Get()->PostTask( 987 base::ThreadTaskRunnerHandle::Get()->PostTask(
891 FROM_HERE, base::Bind(event_fired_callback)); 988 FROM_HERE, base::Bind(event_fired_callback));
892 } 989 }
893 990
894 // |service_worker_registration| is just to keep the registration alive 991 // |service_worker_registration| is just to keep the registration alive
895 // while the event is firing. 992 // while the event is firing.
896 void BackgroundSyncManager::EventComplete( 993 void BackgroundSyncManager::EventComplete(
897 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, 994 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
898 int64 service_worker_id, 995 int64 service_worker_id,
899 const RegistrationKey& key, 996 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle,
900 BackgroundSyncRegistration::RegistrationId sync_registration_id,
901 const base::Closure& callback, 997 const base::Closure& callback,
902 ServiceWorkerStatusCode status_code) { 998 ServiceWorkerStatusCode status_code) {
903 DCHECK_CURRENTLY_ON(BrowserThread::IO); 999 DCHECK_CURRENTLY_ON(BrowserThread::IO);
904 if (disabled_) 1000 if (disabled_)
905 return; 1001 return;
906 1002
907 op_scheduler_.ScheduleOperation(base::Bind( 1003 op_scheduler_.ScheduleOperation(base::Bind(
908 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), 1004 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(),
909 service_worker_id, key, sync_registration_id, status_code, 1005 service_worker_id, base::Passed(registration_handle.Pass()), status_code,
910 MakeClosureCompletion(callback))); 1006 MakeClosureCompletion(callback)));
911 } 1007 }
912 1008
913 void BackgroundSyncManager::EventCompleteImpl( 1009 void BackgroundSyncManager::EventCompleteImpl(
914 int64 service_worker_id, 1010 int64 service_worker_id,
915 const RegistrationKey& key, 1011 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle,
916 BackgroundSyncRegistration::RegistrationId sync_registration_id,
917 ServiceWorkerStatusCode status_code, 1012 ServiceWorkerStatusCode status_code,
918 const base::Closure& callback) { 1013 const base::Closure& callback) {
919 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1014 DCHECK_CURRENTLY_ON(BrowserThread::IO);
920 1015
921 if (disabled_) { 1016 if (disabled_) {
922 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1017 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
923 base::Bind(callback)); 1018 base::Bind(callback));
924 return; 1019 return;
925 } 1020 }
926 1021
927 BackgroundSyncRegistration* registration = 1022 BackgroundSyncRegistration* registration =
928 LookupRegistration(service_worker_id, key); 1023 registration_handle->registration();
929 if (!registration || registration->id() != sync_registration_id) { 1024 DCHECK(registration);
930 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
931 base::Bind(callback));
932 return;
933 }
934 1025
935 // The event ran to completion, we should count it, no matter what happens 1026 // The event ran to completion, we should count it, no matter what happens
936 // from here. 1027 // from here.
937 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, 1028 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity,
938 status_code == SERVICE_WORKER_OK); 1029 status_code == SERVICE_WORKER_OK);
939 1030
940 if (registration->options()->periodicity == SYNC_ONE_SHOT) { 1031 if (registration->options()->periodicity == SYNC_ONE_SHOT) {
941 if (status_code != SERVICE_WORKER_OK) { 1032 if (status_code != SERVICE_WORKER_OK) {
942 // TODO(jkarlin) Fire the sync event on the next page load controlled by 1033 // TODO(jkarlin) Fire the sync event on the next page load controlled by
943 // this registration. (crbug.com/479665) 1034 // this registration. (crbug.com/479665)
944 registration->set_sync_state(SYNC_STATE_FAILED); 1035 registration->set_sync_state(SYNC_STATE_FAILED);
945 } else { 1036 } else {
946 registration = nullptr; 1037 RegistrationKey key(*registration);
947 RemoveRegistrationFromMap(service_worker_id, key); 1038 // Remove the registration if it's still active.
1039 RefCountedRegistration* active_registration =
1040 LookupActiveRegistration(service_worker_id, key);
1041 if (active_registration &&
1042 active_registration->value()->id() == registration->id())
1043 RemoveActiveRegistration(service_worker_id, key);
948 } 1044 }
949 } else { 1045 } else {
950 // TODO(jkarlin): Add support for running periodic syncs. (crbug.com/479674) 1046 // TODO(jkarlin): Add support for running periodic syncs. (crbug.com/479674)
951 NOTREACHED(); 1047 NOTREACHED();
952 } 1048 }
953 1049
954 StoreRegistrations( 1050 StoreRegistrations(
955 service_worker_id, 1051 service_worker_id,
956 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, 1052 base::Bind(&BackgroundSyncManager::EventCompleteDidStore,
957 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); 1053 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback));
958 } 1054 }
959 1055
960 void BackgroundSyncManager::EventCompleteDidStore( 1056 void BackgroundSyncManager::EventCompleteDidStore(
961 int64 service_worker_id, 1057 int64 service_worker_id,
962 const base::Closure& callback, 1058 const base::Closure& callback,
963 ServiceWorkerStatusCode status_code) { 1059 ServiceWorkerStatusCode status_code) {
964 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1060 DCHECK_CURRENTLY_ON(BrowserThread::IO);
965 1061
966 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) { 1062 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) {
967 // The registration is gone. 1063 // The registration is gone.
968 sw_to_registrations_map_.erase(service_worker_id); 1064 active_registrations_.erase(service_worker_id);
969 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1065 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
970 base::Bind(callback)); 1066 base::Bind(callback));
971 return; 1067 return;
972 } 1068 }
973 1069
974 if (status_code != SERVICE_WORKER_OK) { 1070 if (status_code != SERVICE_WORKER_OK) {
975 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " 1071 LOG(ERROR) << "BackgroundSync failed to store registration due to backend "
976 "failure."; 1072 "failure.";
977 DisableAndClearManager(base::Bind(callback)); 1073 DisableAndClearManager(base::Bind(callback));
978 return; 1074 return;
(...skipping 12 matching lines...) Expand all
991 base::TimeTicks::Now() - start_time, number_of_batched_sync_events); 1087 base::TimeTicks::Now() - start_time, number_of_batched_sync_events);
992 } 1088 }
993 1089
994 void BackgroundSyncManager::OnRegistrationDeletedImpl( 1090 void BackgroundSyncManager::OnRegistrationDeletedImpl(
995 int64 registration_id, 1091 int64 registration_id,
996 const base::Closure& callback) { 1092 const base::Closure& callback) {
997 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1093 DCHECK_CURRENTLY_ON(BrowserThread::IO);
998 1094
999 // The backend (ServiceWorkerStorage) will delete the data, so just delete the 1095 // The backend (ServiceWorkerStorage) will delete the data, so just delete the
1000 // memory representation here. 1096 // memory representation here.
1001 sw_to_registrations_map_.erase(registration_id); 1097 active_registrations_.erase(registration_id);
1002 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1098 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1003 base::Bind(callback)); 1099 base::Bind(callback));
1004 } 1100 }
1005 1101
1006 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { 1102 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) {
1007 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1103 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1008 1104
1009 sw_to_registrations_map_.clear(); 1105 active_registrations_.clear();
1010 disabled_ = false; 1106 disabled_ = false;
1011 InitImpl(callback); 1107 InitImpl(callback);
1012 } 1108 }
1013 1109
1014 void BackgroundSyncManager::OnNetworkChanged() { 1110 void BackgroundSyncManager::OnNetworkChanged() {
1015 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1111 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1016 1112
1017 FireReadyEvents(); 1113 FireReadyEvents();
1018 } 1114 }
1019 1115
1020 void BackgroundSyncManager::OnPowerChanged() { 1116 void BackgroundSyncManager::OnPowerChanged() {
1021 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1117 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1022 1118
1023 FireReadyEvents(); 1119 FireReadyEvents();
1024 } 1120 }
1025 1121
1122 // TODO(jkarlin): Figure out how to pass scoped_ptrs with this.
1026 template <typename CallbackT, typename... Params> 1123 template <typename CallbackT, typename... Params>
1027 void BackgroundSyncManager::CompleteOperationCallback(const CallbackT& callback, 1124 void BackgroundSyncManager::CompleteOperationCallback(const CallbackT& callback,
1028 Params... parameters) { 1125 Params... parameters) {
1029 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1126 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1030 1127
1031 callback.Run(parameters...); 1128 callback.Run(parameters...);
1032 op_scheduler_.CompleteOperationAndRunNext(); 1129 op_scheduler_.CompleteOperationAndRunNext();
1033 } 1130 }
1034 1131
1132 void BackgroundSyncManager::CompleteStatusAndRegistrationCallback(
1133 StatusAndRegistrationCallback callback,
1134 BackgroundSyncStatus status,
1135 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) {
1136 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1137
1138 callback.Run(status, registration_handle.Pass());
1139 op_scheduler_.CompleteOperationAndRunNext();
1140 }
1141
1142 void BackgroundSyncManager::CompleteStatusAndRegistrationsCallback(
1143 StatusAndRegistrationsCallback callback,
1144 BackgroundSyncStatus status,
1145 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>
1146 registration_handles) {
1147 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1148
1149 callback.Run(status, registration_handles.Pass());
1150 op_scheduler_.CompleteOperationAndRunNext();
1151 }
1152
1035 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { 1153 base::Closure BackgroundSyncManager::MakeEmptyCompletion() {
1036 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1154 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1037 1155
1038 return MakeClosureCompletion(base::Bind(base::DoNothing)); 1156 return MakeClosureCompletion(base::Bind(base::DoNothing));
1039 } 1157 }
1040 1158
1041 base::Closure BackgroundSyncManager::MakeClosureCompletion( 1159 base::Closure BackgroundSyncManager::MakeClosureCompletion(
1042 const base::Closure& callback) { 1160 const base::Closure& callback) {
1043 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1161 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1044 1162
1045 return base::Bind( 1163 return base::Bind(
1046 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, 1164 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>,
1047 weak_ptr_factory_.GetWeakPtr(), callback); 1165 weak_ptr_factory_.GetWeakPtr(), callback);
1048 } 1166 }
1049 1167
1050 BackgroundSyncManager::StatusAndRegistrationCallback 1168 BackgroundSyncManager::StatusAndRegistrationCallback
1051 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( 1169 BackgroundSyncManager::MakeStatusAndRegistrationCompletion(
1052 const StatusAndRegistrationCallback& callback) { 1170 const StatusAndRegistrationCallback& callback) {
1053 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1171 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1054 1172
1055 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< 1173 return base::Bind(
1056 StatusAndRegistrationCallback, BackgroundSyncStatus, 1174 &BackgroundSyncManager::CompleteStatusAndRegistrationCallback,
1057 const BackgroundSyncRegistration&>, 1175 weak_ptr_factory_.GetWeakPtr(), callback);
1058 weak_ptr_factory_.GetWeakPtr(), callback);
1059 } 1176 }
1060 1177
1061 BackgroundSyncManager::StatusAndRegistrationsCallback 1178 BackgroundSyncManager::StatusAndRegistrationsCallback
1062 BackgroundSyncManager::MakeStatusAndRegistrationsCompletion( 1179 BackgroundSyncManager::MakeStatusAndRegistrationsCompletion(
1063 const StatusAndRegistrationsCallback& callback) { 1180 const StatusAndRegistrationsCallback& callback) {
1064 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1181 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1065 1182
1066 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< 1183 return base::Bind(
1067 StatusAndRegistrationsCallback, BackgroundSyncStatus, 1184 &BackgroundSyncManager::CompleteStatusAndRegistrationsCallback,
1068 const std::vector<BackgroundSyncRegistration>&>, 1185 weak_ptr_factory_.GetWeakPtr(), callback);
1069 weak_ptr_factory_.GetWeakPtr(), callback);
1070 } 1186 }
1071 1187
1072 BackgroundSyncManager::StatusCallback 1188 BackgroundSyncManager::StatusCallback
1073 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { 1189 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) {
1074 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1190 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1075 1191
1076 return base::Bind( 1192 return base::Bind(
1077 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, 1193 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback,
1078 BackgroundSyncStatus>, 1194 BackgroundSyncStatus>,
1079 weak_ptr_factory_.GetWeakPtr(), callback); 1195 weak_ptr_factory_.GetWeakPtr(), callback);
1080 } 1196 }
1081 1197
1082 } // namespace content 1198 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698