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

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: Clean up 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
24 namespace { 27 namespace {
28
25 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; 29 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData";
30
31 void PostErrorResponse(
32 BackgroundSyncStatus status,
33 const BackgroundSyncManager::StatusAndRegistrationCallback& callback) {
34 base::ThreadTaskRunnerHandle::Get()->PostTask(
35 FROM_HERE,
36 base::Bind(
37 callback, status,
38 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass())));
26 } 39 }
27 40
28 namespace content { 41 } // namespace
29 42
30 BackgroundSyncManager::BackgroundSyncRegistrations:: 43 BackgroundSyncManager::BackgroundSyncRegistrations::
31 BackgroundSyncRegistrations() 44 BackgroundSyncRegistrations()
32 : next_id(BackgroundSyncRegistration::kInitialId) { 45 : next_id(BackgroundSyncRegistration::kInitialId) {
33 } 46 }
34 47
35 BackgroundSyncManager::BackgroundSyncRegistrations:: 48 BackgroundSyncManager::BackgroundSyncRegistrations::
36 ~BackgroundSyncRegistrations() { 49 ~BackgroundSyncRegistrations() {
37 } 50 }
38 51
39 // static 52 // static
40 scoped_ptr<BackgroundSyncManager> BackgroundSyncManager::Create( 53 scoped_ptr<BackgroundSyncManager> BackgroundSyncManager::Create(
41 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) { 54 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) {
42 DCHECK_CURRENTLY_ON(BrowserThread::IO); 55 DCHECK_CURRENTLY_ON(BrowserThread::IO);
43 56
44 BackgroundSyncManager* sync_manager = 57 BackgroundSyncManager* sync_manager =
45 new BackgroundSyncManager(service_worker_context); 58 new BackgroundSyncManager(service_worker_context);
46 sync_manager->Init(); 59 sync_manager->Init();
47 return make_scoped_ptr(sync_manager); 60 return make_scoped_ptr(sync_manager);
48 } 61 }
49 62
50 BackgroundSyncManager::~BackgroundSyncManager() { 63 BackgroundSyncManager::~BackgroundSyncManager() {
51 DCHECK_CURRENTLY_ON(BrowserThread::IO); 64 DCHECK_CURRENTLY_ON(BrowserThread::IO);
52 65
66 // This could race in the extremely unlikely case that the
67 // BackgroundSyncManager is deleted before a dispatched sync request is able
68 // to call BackgroundSyncServiceImpl::TrackRegistration.
69 DCHECK(registration_handle_ids_.IsEmpty());
70
53 service_worker_context_->RemoveObserver(this); 71 service_worker_context_->RemoveObserver(this);
54 } 72 }
55 73
56 BackgroundSyncManager::RegistrationKey::RegistrationKey( 74 BackgroundSyncManager::RegistrationKey::RegistrationKey(
57 const BackgroundSyncRegistration& registration) 75 const BackgroundSyncRegistration& registration)
58 : RegistrationKey(registration.options()->tag, 76 : RegistrationKey(registration.options()->tag,
59 registration.options()->periodicity) { 77 registration.options()->periodicity) {
60 } 78 }
61 79
62 BackgroundSyncManager::RegistrationKey::RegistrationKey( 80 BackgroundSyncManager::RegistrationKey::RegistrationKey(
(...skipping 17 matching lines...) Expand all
80 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = 98 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
81 AreOptionConditionsMet(options) 99 AreOptionConditionsMet(options)
82 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE 100 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
83 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; 101 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
84 102
85 if (disabled_) { 103 if (disabled_) {
86 BackgroundSyncMetrics::CountRegister( 104 BackgroundSyncMetrics::CountRegister(
87 options.periodicity, registration_could_fire, 105 options.periodicity, registration_could_fire,
88 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 106 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
89 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 107 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
90 base::ThreadTaskRunnerHandle::Get()->PostTask( 108 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
91 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
92 BackgroundSyncRegistration()));
93 return; 109 return;
94 } 110 }
95 111
96 op_scheduler_.ScheduleOperation( 112 op_scheduler_.ScheduleOperation(
97 base::Bind(&BackgroundSyncManager::RegisterImpl, 113 base::Bind(&BackgroundSyncManager::RegisterImpl,
98 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, 114 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options,
99 MakeStatusAndRegistrationCompletion(callback))); 115 MakeStatusAndRegistrationCompletion(callback)));
100 } 116 }
101 117
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( 118 void BackgroundSyncManager::GetRegistration(
127 int64 sw_registration_id, 119 int64 sw_registration_id,
128 const std::string& sync_registration_tag, 120 const std::string& sync_registration_tag,
129 SyncPeriodicity periodicity, 121 SyncPeriodicity periodicity,
130 const StatusAndRegistrationCallback& callback) { 122 const StatusAndRegistrationCallback& callback) {
131 DCHECK_CURRENTLY_ON(BrowserThread::IO); 123 DCHECK_CURRENTLY_ON(BrowserThread::IO);
132 124
133 if (disabled_) { 125 if (disabled_) {
134 base::ThreadTaskRunnerHandle::Get()->PostTask( 126 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
135 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
136 BackgroundSyncRegistration()));
137 return; 127 return;
138 } 128 }
139 129
140 RegistrationKey registration_key(sync_registration_tag, periodicity); 130 RegistrationKey registration_key(sync_registration_tag, periodicity);
141 131
142 op_scheduler_.ScheduleOperation(base::Bind( 132 op_scheduler_.ScheduleOperation(base::Bind(
143 &BackgroundSyncManager::GetRegistrationImpl, 133 &BackgroundSyncManager::GetRegistrationImpl,
144 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, 134 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key,
145 MakeStatusAndRegistrationCompletion(callback))); 135 MakeStatusAndRegistrationCompletion(callback)));
146 } 136 }
147 137
148 void BackgroundSyncManager::GetRegistrations( 138 void BackgroundSyncManager::GetRegistrations(
149 int64 sw_registration_id, 139 int64 sw_registration_id,
150 SyncPeriodicity periodicity, 140 SyncPeriodicity periodicity,
151 const StatusAndRegistrationsCallback& callback) { 141 const StatusAndRegistrationsCallback& callback) {
152 DCHECK_CURRENTLY_ON(BrowserThread::IO); 142 DCHECK_CURRENTLY_ON(BrowserThread::IO);
153 143
154 if (disabled_) { 144 if (disabled_) {
155 base::ThreadTaskRunnerHandle::Get()->PostTask( 145 base::ThreadTaskRunnerHandle::Get()->PostTask(
156 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 146 FROM_HERE,
157 std::vector<BackgroundSyncRegistration>())); 147 base::Bind(
148 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
149 base::Passed(
150 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>()
151 .Pass())));
158 return; 152 return;
159 } 153 }
160 154
161 op_scheduler_.ScheduleOperation( 155 op_scheduler_.ScheduleOperation(
162 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, 156 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl,
163 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 157 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
164 periodicity, MakeStatusAndRegistrationsCompletion(callback))); 158 periodicity, MakeStatusAndRegistrationsCompletion(callback)));
165 } 159 }
166 160
161 const BackgroundSyncRegistration*
162 BackgroundSyncManager::GetRegistrationForHandle(
163 BackgroundSyncRegistrationHandle::HandleId handle_id) const {
164 scoped_refptr<const RefCountedRegistration>* ref_registration =
165 registration_handle_ids_.Lookup(handle_id);
166 if (!ref_registration)
167 return nullptr;
168 return (*ref_registration)->value();
169 }
170
171 void BackgroundSyncManager::ReleaseHandleId(
172 BackgroundSyncRegistrationHandle::HandleId handle_id) {
173 DCHECK_CURRENTLY_ON(BrowserThread::IO);
174 DCHECK(registration_handle_ids_.Lookup(handle_id));
175 registration_handle_ids_.Remove(handle_id);
176 }
177
167 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, 178 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id,
168 const GURL& pattern) { 179 const GURL& pattern) {
169 DCHECK_CURRENTLY_ON(BrowserThread::IO); 180 DCHECK_CURRENTLY_ON(BrowserThread::IO);
170 181
171 // Operations already in the queue will either fail when they write to storage 182 // 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 183 // or return stale results based on registrations loaded in memory. This is
173 // inconsequential since the service worker is gone. 184 // inconsequential since the service worker is gone.
174 op_scheduler_.ScheduleOperation(base::Bind( 185 op_scheduler_.ScheduleOperation(base::Bind(
175 &BackgroundSyncManager::OnRegistrationDeletedImpl, 186 &BackgroundSyncManager::OnRegistrationDeletedImpl,
176 weak_ptr_factory_.GetWeakPtr(), registration_id, MakeEmptyCompletion())); 187 weak_ptr_factory_.GetWeakPtr(), registration_id, MakeEmptyCompletion()));
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 const BackgroundSyncRegistrationProto& registration_proto = 265 const BackgroundSyncRegistrationProto& registration_proto =
255 registrations_proto.registration(i); 266 registrations_proto.registration(i);
256 267
257 if (registration_proto.id() >= registrations->next_id) { 268 if (registration_proto.id() >= registrations->next_id) {
258 corruption_detected = true; 269 corruption_detected = true;
259 break; 270 break;
260 } 271 }
261 272
262 RegistrationKey registration_key(registration_proto.tag(), 273 RegistrationKey registration_key(registration_proto.tag(),
263 registration_proto.periodicity()); 274 registration_proto.periodicity());
264 BackgroundSyncRegistration* registration = 275
265 &registrations->registration_map[registration_key]; 276 scoped_refptr<RefCountedRegistration> ref_registration(
277 new RefCountedRegistration());
278 registrations->registration_map[registration_key] = ref_registration;
279 BackgroundSyncRegistration* registration = ref_registration->value();
266 280
267 BackgroundSyncRegistrationOptions* options = registration->options(); 281 BackgroundSyncRegistrationOptions* options = registration->options();
268 options->tag = registration_proto.tag(); 282 options->tag = registration_proto.tag();
269 options->periodicity = registration_proto.periodicity(); 283 options->periodicity = registration_proto.periodicity();
270 options->min_period = registration_proto.min_period(); 284 options->min_period = registration_proto.min_period();
271 options->network_state = registration_proto.network_state(); 285 options->network_state = registration_proto.network_state();
272 options->power_state = registration_proto.power_state(); 286 options->power_state = registration_proto.power_state();
273 287
274 registration->set_id(registration_proto.id()); 288 registration->set_id(registration_proto.id());
275 registration->set_sync_state(registration_proto.sync_state()); 289 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 = 322 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
309 AreOptionConditionsMet(options) 323 AreOptionConditionsMet(options)
310 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE 324 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
311 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; 325 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
312 326
313 if (disabled_) { 327 if (disabled_) {
314 BackgroundSyncMetrics::CountRegister( 328 BackgroundSyncMetrics::CountRegister(
315 options.periodicity, registration_could_fire, 329 options.periodicity, registration_could_fire,
316 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 330 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
317 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 331 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
318 base::ThreadTaskRunnerHandle::Get()->PostTask( 332 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
319 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
320 BackgroundSyncRegistration()));
321 return; 333 return;
322 } 334 }
323 335
324 ServiceWorkerRegistration* sw_registration = 336 ServiceWorkerRegistration* sw_registration =
325 service_worker_context_->GetLiveRegistration(sw_registration_id); 337 service_worker_context_->GetLiveRegistration(sw_registration_id);
326 if (!sw_registration || !sw_registration->active_version()) { 338 if (!sw_registration || !sw_registration->active_version()) {
327 BackgroundSyncMetrics::CountRegister( 339 BackgroundSyncMetrics::CountRegister(
328 options.periodicity, registration_could_fire, 340 options.periodicity, registration_could_fire,
329 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 341 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
330 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); 342 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER);
331 base::ThreadTaskRunnerHandle::Get()->PostTask( 343 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; 344 return;
336 } 345 }
337 346
338 if (!sw_registration->active_version()->HasWindowClients()) { 347 if (!sw_registration->active_version()->HasWindowClients()) {
339 base::ThreadTaskRunnerHandle::Get()->PostTask( 348 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback);
340 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_ALLOWED,
341 BackgroundSyncRegistration()));
342 return; 349 return;
343 } 350 }
344 351
345 BackgroundSyncRegistration* existing_registration = 352 RefCountedRegistration* existing_registration_ref =
346 LookupRegistration(sw_registration_id, RegistrationKey(options)); 353 LookupRegistration(sw_registration_id, RegistrationKey(options));
347 if (existing_registration && 354 if (existing_registration_ref &&
348 existing_registration->options()->Equals(options)) { 355 existing_registration_ref->value()->options()->Equals(options)) {
356 BackgroundSyncRegistration* existing_registration =
357 existing_registration_ref->value();
349 if (existing_registration->sync_state() == SYNC_STATE_FAILED) { 358 if (existing_registration->sync_state() == SYNC_STATE_FAILED) {
350 existing_registration->set_sync_state(SYNC_STATE_PENDING); 359 existing_registration->set_sync_state(SYNC_STATE_PENDING);
351 StoreRegistrations( 360 StoreRegistrations(
352 sw_registration_id, 361 sw_registration_id,
353 base::Bind(&BackgroundSyncManager::RegisterDidStore, 362 base::Bind(&BackgroundSyncManager::RegisterDidStore,
354 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 363 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
355 *existing_registration, callback)); 364 make_scoped_refptr(existing_registration_ref), callback));
356 return; 365 return;
357 } 366 }
358 367
359 // Record the duplicated registration 368 // Record the duplicated registration
360 BackgroundSyncMetrics::CountRegister( 369 BackgroundSyncMetrics::CountRegister(
361 existing_registration->options()->periodicity, registration_could_fire, 370 existing_registration->options()->periodicity, registration_could_fire,
362 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE, 371 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE,
363 BACKGROUND_SYNC_STATUS_OK); 372 BACKGROUND_SYNC_STATUS_OK);
364 373
365 base::ThreadTaskRunnerHandle::Get()->PostTask( 374 base::ThreadTaskRunnerHandle::Get()->PostTask(
366 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, 375 FROM_HERE,
367 *existing_registration)); 376 base::Bind(
377 callback, BACKGROUND_SYNC_STATUS_OK,
378 base::Passed(
379 CreateRegistrationHandle(*existing_registration_ref).Pass())));
368 return; 380 return;
369 } 381 }
370 382
371 BackgroundSyncRegistration new_registration; 383 scoped_refptr<RefCountedRegistration> new_ref_registration(
michaeln 2015/08/21 02:39:24 Is it possible for there to be handle to a registr
jkarlin 2015/08/25 17:32:58 Yes, but it will have a different registration id.
372 *new_registration.options() = options; 384 new RefCountedRegistration());
385 BackgroundSyncRegistration* new_registration = new_ref_registration->value();
386
387 *new_registration->options() = options;
373 388
374 BackgroundSyncRegistrations* registrations = 389 BackgroundSyncRegistrations* registrations =
375 &sw_to_registrations_map_[sw_registration_id]; 390 &sw_to_registrations_map_[sw_registration_id];
376 new_registration.set_id(registrations->next_id++); 391 new_registration->set_id(registrations->next_id++);
377 392
378 AddRegistrationToMap(sw_registration_id, 393 AddRegistrationToMap(sw_registration_id,
379 sw_registration->pattern().GetOrigin(), 394 sw_registration->pattern().GetOrigin(),
380 new_registration); 395 new_ref_registration);
381 396
382 StoreRegistrations( 397 StoreRegistrations(
383 sw_registration_id, 398 sw_registration_id,
384 base::Bind(&BackgroundSyncManager::RegisterDidStore, 399 base::Bind(&BackgroundSyncManager::RegisterDidStore,
385 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 400 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
386 new_registration, callback)); 401 new_ref_registration, callback));
387 } 402 }
388 403
389 void BackgroundSyncManager::DisableAndClearManager( 404 void BackgroundSyncManager::DisableAndClearManager(
390 const base::Closure& callback) { 405 const base::Closure& callback) {
391 DCHECK_CURRENTLY_ON(BrowserThread::IO); 406 DCHECK_CURRENTLY_ON(BrowserThread::IO);
392 407
393 if (disabled_) { 408 if (disabled_) {
394 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 409 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
395 base::Bind(callback)); 410 base::Bind(callback));
396 return; 411 return;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 void BackgroundSyncManager::DisableAndClearManagerClearedOne( 449 void BackgroundSyncManager::DisableAndClearManagerClearedOne(
435 const base::Closure& barrier_closure, 450 const base::Closure& barrier_closure,
436 ServiceWorkerStatusCode status) { 451 ServiceWorkerStatusCode status) {
437 DCHECK_CURRENTLY_ON(BrowserThread::IO); 452 DCHECK_CURRENTLY_ON(BrowserThread::IO);
438 453
439 // The status doesn't matter at this point, there is nothing else to be done. 454 // The status doesn't matter at this point, there is nothing else to be done.
440 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 455 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
441 base::Bind(barrier_closure)); 456 base::Bind(barrier_closure));
442 } 457 }
443 458
444 BackgroundSyncRegistration* BackgroundSyncManager::LookupRegistration( 459 BackgroundSyncManager::RefCountedRegistration*
460 BackgroundSyncManager::LookupRegistration(
445 int64 sw_registration_id, 461 int64 sw_registration_id,
446 const RegistrationKey& registration_key) { 462 const RegistrationKey& registration_key) {
447 DCHECK_CURRENTLY_ON(BrowserThread::IO); 463 DCHECK_CURRENTLY_ON(BrowserThread::IO);
448 464
449 SWIdToRegistrationsMap::iterator it = 465 SWIdToRegistrationsMap::iterator it =
450 sw_to_registrations_map_.find(sw_registration_id); 466 sw_to_registrations_map_.find(sw_registration_id);
451 if (it == sw_to_registrations_map_.end()) 467 if (it == sw_to_registrations_map_.end())
452 return nullptr; 468 return nullptr;
453 469
454 BackgroundSyncRegistrations& registrations = it->second; 470 BackgroundSyncRegistrations& registrations = it->second;
455 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); 471 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id);
456 DCHECK(!registrations.origin.is_empty()); 472 DCHECK(!registrations.origin.is_empty());
457 473
458 auto key_and_registration_iter = 474 auto key_and_registration_iter =
459 registrations.registration_map.find(registration_key); 475 registrations.registration_map.find(registration_key);
460 if (key_and_registration_iter == registrations.registration_map.end()) 476 if (key_and_registration_iter == registrations.registration_map.end())
461 return nullptr; 477 return nullptr;
462 478
463 return &key_and_registration_iter->second; 479 return key_and_registration_iter->second.get();
464 } 480 }
465 481
466 void BackgroundSyncManager::StoreRegistrations( 482 void BackgroundSyncManager::StoreRegistrations(
467 int64 sw_registration_id, 483 int64 sw_registration_id,
468 const ServiceWorkerStorage::StatusCallback& callback) { 484 const ServiceWorkerStorage::StatusCallback& callback) {
469 DCHECK_CURRENTLY_ON(BrowserThread::IO); 485 DCHECK_CURRENTLY_ON(BrowserThread::IO);
470 486
471 // Serialize the data. 487 // Serialize the data.
472 const BackgroundSyncRegistrations& registrations = 488 const BackgroundSyncRegistrations& registrations =
473 sw_to_registrations_map_[sw_registration_id]; 489 sw_to_registrations_map_[sw_registration_id];
474 BackgroundSyncRegistrationsProto registrations_proto; 490 BackgroundSyncRegistrationsProto registrations_proto;
475 registrations_proto.set_next_registration_id(registrations.next_id); 491 registrations_proto.set_next_registration_id(registrations.next_id);
476 registrations_proto.set_origin(registrations.origin.spec()); 492 registrations_proto.set_origin(registrations.origin.spec());
477 493
478 for (const auto& key_and_registration : registrations.registration_map) { 494 for (const auto& key_and_registration : registrations.registration_map) {
479 const BackgroundSyncRegistration& registration = 495 const BackgroundSyncRegistration& registration =
480 key_and_registration.second; 496 *key_and_registration.second->value();
481 BackgroundSyncRegistrationProto* registration_proto = 497 BackgroundSyncRegistrationProto* registration_proto =
482 registrations_proto.add_registration(); 498 registrations_proto.add_registration();
483 registration_proto->set_id(registration.id()); 499 registration_proto->set_id(registration.id());
484 registration_proto->set_sync_state(registration.sync_state()); 500 registration_proto->set_sync_state(registration.sync_state());
485 registration_proto->set_tag(registration.options()->tag); 501 registration_proto->set_tag(registration.options()->tag);
486 registration_proto->set_periodicity(registration.options()->periodicity); 502 registration_proto->set_periodicity(registration.options()->periodicity);
487 registration_proto->set_min_period(registration.options()->min_period); 503 registration_proto->set_min_period(registration.options()->min_period);
488 registration_proto->set_network_state( 504 registration_proto->set_network_state(
489 registration.options()->network_state); 505 registration.options()->network_state);
490 registration_proto->set_power_state(registration.options()->power_state); 506 registration_proto->set_power_state(registration.options()->power_state);
491 } 507 }
492 std::string serialized; 508 std::string serialized;
493 bool success = registrations_proto.SerializeToString(&serialized); 509 bool success = registrations_proto.SerializeToString(&serialized);
494 DCHECK(success); 510 DCHECK(success);
495 511
496 StoreDataInBackend(sw_registration_id, registrations.origin, 512 StoreDataInBackend(sw_registration_id, registrations.origin,
497 kBackgroundSyncUserDataKey, serialized, callback); 513 kBackgroundSyncUserDataKey, serialized, callback);
498 } 514 }
499 515
500 void BackgroundSyncManager::RegisterDidStore( 516 void BackgroundSyncManager::RegisterDidStore(
501 int64 sw_registration_id, 517 int64 sw_registration_id,
502 const BackgroundSyncRegistration& new_registration, 518 const scoped_refptr<RefCountedRegistration>& new_registration_ref,
503 const StatusAndRegistrationCallback& callback, 519 const StatusAndRegistrationCallback& callback,
504 ServiceWorkerStatusCode status) { 520 ServiceWorkerStatusCode status) {
505 DCHECK_CURRENTLY_ON(BrowserThread::IO); 521 DCHECK_CURRENTLY_ON(BrowserThread::IO);
506 522
523 const BackgroundSyncRegistration* new_registration =
524 new_registration_ref->value();
525
507 // For UMA, determine here whether the sync could fire immediately 526 // For UMA, determine here whether the sync could fire immediately
508 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = 527 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
509 AreOptionConditionsMet(*new_registration.options()) 528 AreOptionConditionsMet(*new_registration->options())
510 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE 529 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
511 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; 530 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
512 531
513 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { 532 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) {
514 // The service worker registration is gone. 533 // The service worker registration is gone.
515 BackgroundSyncMetrics::CountRegister( 534 BackgroundSyncMetrics::CountRegister(
516 new_registration.options()->periodicity, registration_could_fire, 535 new_registration->options()->periodicity, registration_could_fire,
517 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 536 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
518 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 537 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
519 sw_to_registrations_map_.erase(sw_registration_id); 538 sw_to_registrations_map_.erase(sw_registration_id);
520 base::ThreadTaskRunnerHandle::Get()->PostTask( 539 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
521 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
522 BackgroundSyncRegistration()));
523 return; 540 return;
524 } 541 }
525 542
526 if (status != SERVICE_WORKER_OK) { 543 if (status != SERVICE_WORKER_OK) {
527 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " 544 LOG(ERROR) << "BackgroundSync failed to store registration due to backend "
528 "failure."; 545 "failure.";
529 BackgroundSyncMetrics::CountRegister( 546 BackgroundSyncMetrics::CountRegister(
530 new_registration.options()->periodicity, registration_could_fire, 547 new_registration->options()->periodicity, registration_could_fire,
531 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 548 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
532 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 549 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
533 DisableAndClearManager(base::Bind(callback, 550 DisableAndClearManager(base::Bind(
534 BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 551 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
535 BackgroundSyncRegistration())); 552 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass())));
536 return; 553 return;
537 } 554 }
538 555
539 BackgroundSyncMetrics::CountRegister( 556 BackgroundSyncMetrics::CountRegister(
540 new_registration.options()->periodicity, registration_could_fire, 557 new_registration->options()->periodicity, registration_could_fire,
541 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, 558 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE,
542 BACKGROUND_SYNC_STATUS_OK); 559 BACKGROUND_SYNC_STATUS_OK);
543 FireReadyEvents(); 560 FireReadyEvents();
544 base::ThreadTaskRunnerHandle::Get()->PostTask( 561 base::ThreadTaskRunnerHandle::Get()->PostTask(
545 FROM_HERE, 562 FROM_HERE,
546 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, new_registration)); 563 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK,
564 base::Passed(
565 CreateRegistrationHandle(*new_registration_ref).Pass())));
547 } 566 }
548 567
549 void BackgroundSyncManager::RemoveRegistrationFromMap( 568 void BackgroundSyncManager::RemoveRegistrationFromMap(
550 int64 sw_registration_id, 569 int64 sw_registration_id,
551 const RegistrationKey& registration_key) { 570 const RegistrationKey& registration_key) {
552 DCHECK_CURRENTLY_ON(BrowserThread::IO); 571 DCHECK_CURRENTLY_ON(BrowserThread::IO);
553 DCHECK(LookupRegistration(sw_registration_id, registration_key)); 572 DCHECK(LookupRegistration(sw_registration_id, registration_key));
554 573
555 BackgroundSyncRegistrations* registrations = 574 BackgroundSyncRegistrations* registrations =
556 &sw_to_registrations_map_[sw_registration_id]; 575 &sw_to_registrations_map_[sw_registration_id];
557 576
558 registrations->registration_map.erase(registration_key); 577 registrations->registration_map.erase(registration_key);
559 } 578 }
560 579
561 void BackgroundSyncManager::AddRegistrationToMap( 580 void BackgroundSyncManager::AddRegistrationToMap(
562 int64 sw_registration_id, 581 int64 sw_registration_id,
563 const GURL& origin, 582 const GURL& origin,
564 const BackgroundSyncRegistration& sync_registration) { 583 const scoped_refptr<RefCountedRegistration>& sync_registration) {
565 DCHECK_CURRENTLY_ON(BrowserThread::IO); 584 DCHECK_CURRENTLY_ON(BrowserThread::IO);
566 DCHECK(sync_registration.IsValid()); 585 DCHECK(sync_registration->value()->IsValid());
567 586
568 BackgroundSyncRegistrations* registrations = 587 BackgroundSyncRegistrations* registrations =
569 &sw_to_registrations_map_[sw_registration_id]; 588 &sw_to_registrations_map_[sw_registration_id];
570 registrations->origin = origin; 589 registrations->origin = origin;
571 590
572 RegistrationKey registration_key(sync_registration); 591 RegistrationKey registration_key(*sync_registration->value());
573 registrations->registration_map[registration_key] = sync_registration; 592 registrations->registration_map[registration_key] = sync_registration;
574 } 593 }
575 594
576 void BackgroundSyncManager::StoreDataInBackend( 595 void BackgroundSyncManager::StoreDataInBackend(
577 int64 sw_registration_id, 596 int64 sw_registration_id,
578 const GURL& origin, 597 const GURL& origin,
579 const std::string& backend_key, 598 const std::string& backend_key,
580 const std::string& data, 599 const std::string& data,
581 const ServiceWorkerStorage::StatusCallback& callback) { 600 const ServiceWorkerStorage::StatusCallback& callback) {
582 DCHECK_CURRENTLY_ON(BrowserThread::IO); 601 DCHECK_CURRENTLY_ON(BrowserThread::IO);
583 602
584 service_worker_context_->StoreRegistrationUserData( 603 service_worker_context_->StoreRegistrationUserData(
585 sw_registration_id, origin, backend_key, data, callback); 604 sw_registration_id, origin, backend_key, data, callback);
586 } 605 }
587 606
588 void BackgroundSyncManager::GetDataFromBackend( 607 void BackgroundSyncManager::GetDataFromBackend(
589 const std::string& backend_key, 608 const std::string& backend_key,
590 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& 609 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
591 callback) { 610 callback) {
592 DCHECK_CURRENTLY_ON(BrowserThread::IO); 611 DCHECK_CURRENTLY_ON(BrowserThread::IO);
593 612
594 service_worker_context_->GetUserDataForAllRegistrations(backend_key, 613 service_worker_context_->GetUserDataForAllRegistrations(backend_key,
595 callback); 614 callback);
596 } 615 }
597 616
598 void BackgroundSyncManager::FireOneShotSync( 617 void BackgroundSyncManager::FireOneShotSync(
599 const BackgroundSyncRegistration& registration, 618 const RefCountedRegistration& registration,
600 const scoped_refptr<ServiceWorkerVersion>& active_version, 619 const scoped_refptr<ServiceWorkerVersion>& active_version,
601 const ServiceWorkerVersion::StatusCallback& callback) { 620 const ServiceWorkerVersion::StatusCallback& callback) {
602 DCHECK_CURRENTLY_ON(BrowserThread::IO); 621 DCHECK_CURRENTLY_ON(BrowserThread::IO);
603 622
604 active_version->DispatchSyncEvent( 623 // The ServiceWorkerVersion doesn't know when the client (javascript) is done
605 mojo::ConvertTo<SyncRegistrationPtr>(registration), callback); 624 // with the registration so don't give it a BackgroundSyncRegistrationHandle.
625 // The render process must call BackgroundSyncServiceImpl::TrackRegistration
626 // which creates the corresponding BackgroundSyncRegistrationHandle. In the
627 // unlikely event that the render process is killed before TrackRegistration
628 // is called, the manager will hold the reference until it is destroyed.
629 // The ~BackgroundSyncManager DCHECKS verify that this is uncommon.
630 SyncRegistrationPtr sync_registration =
631 mojo::ConvertTo<SyncRegistrationPtr>(*registration.value());
632 scoped_refptr<const RefCountedRegistration>* ptr =
633 new scoped_refptr<const RefCountedRegistration>(&registration);
634 sync_registration->id = registration_handle_ids_.Add(ptr);
635
636 active_version->DispatchSyncEvent(sync_registration.Pass(), callback);
637 }
638
639 scoped_ptr<BackgroundSyncRegistrationHandle>
640 BackgroundSyncManager::CreateRegistrationHandle(
641 const RefCountedRegistration& registration) {
642 scoped_refptr<const RefCountedRegistration>* ptr =
643 new scoped_refptr<const RefCountedRegistration>(&registration);
644
645 // Registration handles have unique handle ids. The handle id maps to an
646 // internal RefCountedRegistration (which has the persistent registration id)
647 // via
648 // registration_reference_ids_.
649 BackgroundSyncRegistrationHandle::HandleId handle_id =
650 registration_handle_ids_.Add(ptr);
651
652 return make_scoped_ptr(new BackgroundSyncRegistrationHandle(this, handle_id));
653 }
654
655 void BackgroundSyncManager::Unregister(
656 int64 sw_registration_id,
657 SyncPeriodicity periodicity,
658 BackgroundSyncRegistrationHandle::HandleId handle_id,
659 const StatusCallback& callback) {
660 DCHECK_CURRENTLY_ON(BrowserThread::IO);
661
662 if (disabled_) {
663 BackgroundSyncMetrics::CountUnregister(
664 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
665 base::ThreadTaskRunnerHandle::Get()->PostTask(
666 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR));
667 return;
668 }
669
670 op_scheduler_.ScheduleOperation(
671 base::Bind(&BackgroundSyncManager::UnregisterImpl,
michaeln 2015/08/21 02:39:24 What prevents ReleaseHandleId(xxx) from happening
jkarlin 2015/08/25 17:32:58 Thanks for that catch. Resolved by reverting Unreg
672 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
673 periodicity, handle_id, MakeStatusCompletion(callback)));
606 } 674 }
607 675
608 void BackgroundSyncManager::UnregisterImpl( 676 void BackgroundSyncManager::UnregisterImpl(
609 int64 sw_registration_id, 677 int64 sw_registration_id,
610 const RegistrationKey& registration_key,
611 BackgroundSyncRegistration::RegistrationId sync_registration_id,
612 SyncPeriodicity periodicity, 678 SyncPeriodicity periodicity,
679 BackgroundSyncRegistrationHandle::HandleId handle_id,
613 const StatusCallback& callback) { 680 const StatusCallback& callback) {
614 DCHECK_CURRENTLY_ON(BrowserThread::IO); 681 DCHECK_CURRENTLY_ON(BrowserThread::IO);
615 682
616 if (disabled_) { 683 if (disabled_) {
617 BackgroundSyncMetrics::CountUnregister( 684 BackgroundSyncMetrics::CountUnregister(
618 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 685 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
619 base::ThreadTaskRunnerHandle::Get()->PostTask( 686 base::ThreadTaskRunnerHandle::Get()->PostTask(
620 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); 687 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR));
621 return; 688 return;
622 } 689 }
623 690
624 const BackgroundSyncRegistration* existing_registration = 691 scoped_refptr<const RefCountedRegistration>* handle_ref =
692 registration_handle_ids_.Lookup(handle_id);
693 DCHECK(handle_ref);
694
695 const BackgroundSyncRegistration* handle_registration =
696 (*handle_ref)->value();
697
698 RegistrationKey registration_key(handle_registration->options()->tag,
699 periodicity);
700
701 const RefCountedRegistration* existing_registration =
625 LookupRegistration(sw_registration_id, registration_key); 702 LookupRegistration(sw_registration_id, registration_key);
703
626 if (!existing_registration || 704 if (!existing_registration ||
627 existing_registration->id() != sync_registration_id) { 705 existing_registration->value()->id() != handle_registration->id()) {
628 BackgroundSyncMetrics::CountUnregister(periodicity, 706 BackgroundSyncMetrics::CountUnregister(periodicity,
629 BACKGROUND_SYNC_STATUS_NOT_FOUND); 707 BACKGROUND_SYNC_STATUS_NOT_FOUND);
630 base::ThreadTaskRunnerHandle::Get()->PostTask( 708 base::ThreadTaskRunnerHandle::Get()->PostTask(
631 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); 709 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND));
632 return; 710 return;
633 } 711 }
634 712
635 RemoveRegistrationFromMap(sw_registration_id, registration_key); 713 RemoveRegistrationFromMap(sw_registration_id, registration_key);
636 714
637 StoreRegistrations(sw_registration_id, 715 StoreRegistrations(sw_registration_id,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); 749 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK));
672 } 750 }
673 751
674 void BackgroundSyncManager::GetRegistrationImpl( 752 void BackgroundSyncManager::GetRegistrationImpl(
675 int64 sw_registration_id, 753 int64 sw_registration_id,
676 const RegistrationKey& registration_key, 754 const RegistrationKey& registration_key,
677 const StatusAndRegistrationCallback& callback) { 755 const StatusAndRegistrationCallback& callback) {
678 DCHECK_CURRENTLY_ON(BrowserThread::IO); 756 DCHECK_CURRENTLY_ON(BrowserThread::IO);
679 757
680 if (disabled_) { 758 if (disabled_) {
681 base::ThreadTaskRunnerHandle::Get()->PostTask( 759 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
682 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
683 BackgroundSyncRegistration()));
684 return; 760 return;
685 } 761 }
686 762
687 const BackgroundSyncRegistration* out_registration = 763 const RefCountedRegistration* registration =
688 LookupRegistration(sw_registration_id, registration_key); 764 LookupRegistration(sw_registration_id, registration_key);
689 if (!out_registration) { 765 if (!registration) {
690 base::ThreadTaskRunnerHandle::Get()->PostTask( 766 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_FOUND, callback);
691 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND,
692 BackgroundSyncRegistration()));
693 return; 767 return;
694 } 768 }
695 769
696 base::ThreadTaskRunnerHandle::Get()->PostTask( 770 base::ThreadTaskRunnerHandle::Get()->PostTask(
697 FROM_HERE, 771 FROM_HERE,
698 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, *out_registration)); 772 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK,
773 base::Passed(CreateRegistrationHandle(*registration).Pass())));
699 } 774 }
700 775
701 void BackgroundSyncManager::GetRegistrationsImpl( 776 void BackgroundSyncManager::GetRegistrationsImpl(
702 int64 sw_registration_id, 777 int64 sw_registration_id,
703 SyncPeriodicity periodicity, 778 SyncPeriodicity periodicity,
704 const StatusAndRegistrationsCallback& callback) { 779 const StatusAndRegistrationsCallback& callback) {
705 DCHECK_CURRENTLY_ON(BrowserThread::IO); 780 DCHECK_CURRENTLY_ON(BrowserThread::IO);
706 781
707 std::vector<BackgroundSyncRegistration> out_registrations; 782 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations(
783 new ScopedVector<BackgroundSyncRegistrationHandle>());
708 784
709 if (disabled_) { 785 if (disabled_) {
710 base::ThreadTaskRunnerHandle::Get()->PostTask( 786 base::ThreadTaskRunnerHandle::Get()->PostTask(
711 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 787 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
712 out_registrations)); 788 base::Passed(out_registrations.Pass())));
713 return; 789 return;
714 } 790 }
715 791
716 SWIdToRegistrationsMap::iterator it = 792 SWIdToRegistrationsMap::iterator it =
717 sw_to_registrations_map_.find(sw_registration_id); 793 sw_to_registrations_map_.find(sw_registration_id);
718 794
719 if (it != sw_to_registrations_map_.end()) { 795 if (it != sw_to_registrations_map_.end()) {
720 const BackgroundSyncRegistrations& registrations = it->second; 796 const BackgroundSyncRegistrations& registrations = it->second;
721 for (const auto& tag_and_registration : registrations.registration_map) { 797 for (const auto& tag_and_registration : registrations.registration_map) {
722 const BackgroundSyncRegistration& registration = 798 const RefCountedRegistration& registration =
723 tag_and_registration.second; 799 *tag_and_registration.second.get();
724 if (registration.options()->periodicity == periodicity) 800 if (registration.value()->options()->periodicity == periodicity) {
725 out_registrations.push_back(registration); 801 out_registrations->push_back(
802 CreateRegistrationHandle(registration).release());
803 }
726 } 804 }
727 } 805 }
728 806
729 base::ThreadTaskRunnerHandle::Get()->PostTask( 807 base::ThreadTaskRunnerHandle::Get()->PostTask(
730 FROM_HERE, 808 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK,
731 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, out_registrations)); 809 base::Passed(out_registrations.Pass())));
732 } 810 }
733 811
734 bool BackgroundSyncManager::AreOptionConditionsMet( 812 bool BackgroundSyncManager::AreOptionConditionsMet(
735 const BackgroundSyncRegistrationOptions& options) { 813 const BackgroundSyncRegistrationOptions& options) {
736 DCHECK_CURRENTLY_ON(BrowserThread::IO); 814 DCHECK_CURRENTLY_ON(BrowserThread::IO);
737 return network_observer_->NetworkSufficient(options.network_state) && 815 return network_observer_->NetworkSufficient(options.network_state) &&
738 power_observer_->PowerSufficient(options.power_state); 816 power_observer_->PowerSufficient(options.power_state);
739 } 817 }
740 818
741 bool BackgroundSyncManager::IsRegistrationReadyToFire( 819 bool BackgroundSyncManager::IsRegistrationReadyToFire(
(...skipping 13 matching lines...) Expand all
755 } 833 }
756 834
757 void BackgroundSyncManager::SchedulePendingRegistrations() { 835 void BackgroundSyncManager::SchedulePendingRegistrations() {
758 #if defined(OS_ANDROID) 836 #if defined(OS_ANDROID)
759 bool keep_browser_alive_for_one_shot = false; 837 bool keep_browser_alive_for_one_shot = false;
760 838
761 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { 839 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) {
762 for (const auto& key_and_registration : 840 for (const auto& key_and_registration :
763 sw_id_and_registrations.second.registration_map) { 841 sw_id_and_registrations.second.registration_map) {
764 const BackgroundSyncRegistration& registration = 842 const BackgroundSyncRegistration& registration =
765 key_and_registration.second; 843 *key_and_registration.second->value();
766 if (registration.sync_state() == SYNC_STATE_PENDING) { 844 if (registration.sync_state() == SYNC_STATE_PENDING) {
767 if (registration.options()->periodicity == SYNC_ONE_SHOT) { 845 if (registration.options()->periodicity == SYNC_ONE_SHOT) {
768 keep_browser_alive_for_one_shot = true; 846 keep_browser_alive_for_one_shot = true;
769 } else { 847 } else {
770 // TODO(jkarlin): Support keeping the browser alive for periodic 848 // TODO(jkarlin): Support keeping the browser alive for periodic
771 // syncs. 849 // syncs.
772 } 850 }
773 } 851 }
774 } 852 }
775 } 853 }
(...skipping 29 matching lines...) Expand all
805 return; 883 return;
806 } 884 }
807 885
808 // Find the registrations that are ready to run. 886 // Find the registrations that are ready to run.
809 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire; 887 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire;
810 888
811 for (auto& sw_id_and_registrations : sw_to_registrations_map_) { 889 for (auto& sw_id_and_registrations : sw_to_registrations_map_) {
812 const int64 service_worker_id = sw_id_and_registrations.first; 890 const int64 service_worker_id = sw_id_and_registrations.first;
813 for (auto& key_and_registration : 891 for (auto& key_and_registration :
814 sw_id_and_registrations.second.registration_map) { 892 sw_id_and_registrations.second.registration_map) {
815 BackgroundSyncRegistration* registration = &key_and_registration.second; 893 BackgroundSyncRegistration* registration =
894 key_and_registration.second->value();
816 if (IsRegistrationReadyToFire(*registration)) { 895 if (IsRegistrationReadyToFire(*registration)) {
817 sw_id_and_keys_to_fire.push_back( 896 sw_id_and_keys_to_fire.push_back(
818 std::make_pair(service_worker_id, key_and_registration.first)); 897 std::make_pair(service_worker_id, key_and_registration.first));
819 // The state change is not saved to persistent storage because 898 // The state change is not saved to persistent storage because
820 // if the sync event is killed mid-sync then it should return to 899 // if the sync event is killed mid-sync then it should return to
821 // SYNC_STATE_PENDING. 900 // SYNC_STATE_PENDING.
822 registration->set_sync_state(SYNC_STATE_FIRING); 901 registration->set_sync_state(SYNC_STATE_FIRING);
823 } 902 }
824 } 903 }
825 } 904 }
(...skipping 12 matching lines...) Expand all
838 sw_id_and_keys_to_fire.size(), base::Bind(callback)); 917 sw_id_and_keys_to_fire.size(), base::Bind(callback));
839 918
840 // Record the total time taken after all events have run to completion. 919 // Record the total time taken after all events have run to completion.
841 base::Closure events_completed_barrier_closure = 920 base::Closure events_completed_barrier_closure =
842 base::BarrierClosure(sw_id_and_keys_to_fire.size(), 921 base::BarrierClosure(sw_id_and_keys_to_fire.size(),
843 base::Bind(&OnAllSyncEventsCompleted, start_time, 922 base::Bind(&OnAllSyncEventsCompleted, start_time,
844 sw_id_and_keys_to_fire.size())); 923 sw_id_and_keys_to_fire.size()));
845 924
846 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { 925 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) {
847 int64 service_worker_id = sw_id_and_key.first; 926 int64 service_worker_id = sw_id_and_key.first;
848 const BackgroundSyncRegistration* registration = 927 const RefCountedRegistration* registration =
849 LookupRegistration(service_worker_id, sw_id_and_key.second); 928 LookupRegistration(service_worker_id, sw_id_and_key.second);
929 DCHECK(registration);
850 930
851 service_worker_context_->FindRegistrationForId( 931 service_worker_context_->FindRegistrationForId(
852 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, 932 service_worker_id, sw_to_registrations_map_[service_worker_id].origin,
853 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, 933 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration,
854 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, 934 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second,
855 registration->id(), events_fired_barrier_closure, 935 registration->value()->id(), events_fired_barrier_closure,
856 events_completed_barrier_closure)); 936 events_completed_barrier_closure));
857 } 937 }
858 } 938 }
859 939
860 SchedulePendingRegistrations(); 940 SchedulePendingRegistrations();
861 } 941 }
862 942
863 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( 943 void BackgroundSyncManager::FireReadyEventsDidFindRegistration(
864 const RegistrationKey& registration_key, 944 const RegistrationKey& registration_key,
865 BackgroundSyncRegistration::RegistrationId registration_id, 945 BackgroundSyncRegistration::RegistrationId registration_id,
866 const base::Closure& event_fired_callback, 946 const base::Closure& event_fired_callback,
867 const base::Closure& event_completed_callback, 947 const base::Closure& event_completed_callback,
868 ServiceWorkerStatusCode service_worker_status, 948 ServiceWorkerStatusCode service_worker_status,
869 const scoped_refptr<ServiceWorkerRegistration>& 949 const scoped_refptr<ServiceWorkerRegistration>&
870 service_worker_registration) { 950 service_worker_registration) {
871 DCHECK_CURRENTLY_ON(BrowserThread::IO); 951 DCHECK_CURRENTLY_ON(BrowserThread::IO);
872 if (service_worker_status != SERVICE_WORKER_OK) { 952 if (service_worker_status != SERVICE_WORKER_OK) {
873 base::ThreadTaskRunnerHandle::Get()->PostTask( 953 base::ThreadTaskRunnerHandle::Get()->PostTask(
874 FROM_HERE, base::Bind(event_fired_callback)); 954 FROM_HERE, base::Bind(event_fired_callback));
875 base::ThreadTaskRunnerHandle::Get()->PostTask( 955 base::ThreadTaskRunnerHandle::Get()->PostTask(
876 FROM_HERE, base::Bind(event_completed_callback)); 956 FROM_HERE, base::Bind(event_completed_callback));
877 return; 957 return;
878 } 958 }
879 959
880 BackgroundSyncRegistration* registration = 960 const RefCountedRegistration* registration =
881 LookupRegistration(service_worker_registration->id(), registration_key); 961 LookupRegistration(service_worker_registration->id(), registration_key);
962 DCHECK(registration);
882 963
883 FireOneShotSync( 964 FireOneShotSync(
884 *registration, service_worker_registration->active_version(), 965 *registration, service_worker_registration->active_version(),
885 base::Bind(&BackgroundSyncManager::EventComplete, 966 base::Bind(&BackgroundSyncManager::EventComplete,
michaeln 2015/08/21 02:39:24 I think we might be able to improve upon the life
jkarlin 2015/08/25 17:32:58 This is a good idea. In my implementation the rend
886 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, 967 weak_ptr_factory_.GetWeakPtr(), service_worker_registration,
887 service_worker_registration->id(), registration_key, 968 service_worker_registration->id(), registration_key,
888 registration_id, event_completed_callback)); 969 registration_id, event_completed_callback));
889 970
890 base::ThreadTaskRunnerHandle::Get()->PostTask( 971 base::ThreadTaskRunnerHandle::Get()->PostTask(
891 FROM_HERE, base::Bind(event_fired_callback)); 972 FROM_HERE, base::Bind(event_fired_callback));
892 } 973 }
893 974
894 // |service_worker_registration| is just to keep the registration alive 975 // |service_worker_registration| is just to keep the registration alive
895 // while the event is firing. 976 // while the event is firing.
(...skipping 21 matching lines...) Expand all
917 ServiceWorkerStatusCode status_code, 998 ServiceWorkerStatusCode status_code,
918 const base::Closure& callback) { 999 const base::Closure& callback) {
919 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1000 DCHECK_CURRENTLY_ON(BrowserThread::IO);
920 1001
921 if (disabled_) { 1002 if (disabled_) {
922 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1003 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
923 base::Bind(callback)); 1004 base::Bind(callback));
924 return; 1005 return;
925 } 1006 }
926 1007
927 BackgroundSyncRegistration* registration = 1008 RefCountedRegistration* registration_ref =
928 LookupRegistration(service_worker_id, key); 1009 LookupRegistration(service_worker_id, key);
929 if (!registration || registration->id() != sync_registration_id) { 1010 if (!registration_ref ||
1011 registration_ref->value()->id() != sync_registration_id) {
930 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1012 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
931 base::Bind(callback)); 1013 base::Bind(callback));
932 return; 1014 return;
933 } 1015 }
934 1016
1017 BackgroundSyncRegistration* registration = registration_ref->value();
1018
935 // The event ran to completion, we should count it, no matter what happens 1019 // The event ran to completion, we should count it, no matter what happens
936 // from here. 1020 // from here.
937 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, 1021 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity,
938 status_code == SERVICE_WORKER_OK); 1022 status_code == SERVICE_WORKER_OK);
939 1023
940 if (registration->options()->periodicity == SYNC_ONE_SHOT) { 1024 if (registration->options()->periodicity == SYNC_ONE_SHOT) {
941 if (status_code != SERVICE_WORKER_OK) { 1025 if (status_code != SERVICE_WORKER_OK) {
942 // TODO(jkarlin) Fire the sync event on the next page load controlled by 1026 // TODO(jkarlin) Fire the sync event on the next page load controlled by
943 // this registration. (crbug.com/479665) 1027 // this registration. (crbug.com/479665)
944 registration->set_sync_state(SYNC_STATE_FAILED); 1028 registration->set_sync_state(SYNC_STATE_FAILED);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 1100
1017 FireReadyEvents(); 1101 FireReadyEvents();
1018 } 1102 }
1019 1103
1020 void BackgroundSyncManager::OnPowerChanged() { 1104 void BackgroundSyncManager::OnPowerChanged() {
1021 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1105 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1022 1106
1023 FireReadyEvents(); 1107 FireReadyEvents();
1024 } 1108 }
1025 1109
1110 // TODO(jkarlin): Figure out how to pass scoped_ptrs with this.
1026 template <typename CallbackT, typename... Params> 1111 template <typename CallbackT, typename... Params>
1027 void BackgroundSyncManager::CompleteOperationCallback(const CallbackT& callback, 1112 void BackgroundSyncManager::CompleteOperationCallback(const CallbackT& callback,
1028 Params... parameters) { 1113 Params... parameters) {
1029 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1114 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1030 1115
1031 callback.Run(parameters...); 1116 callback.Run(parameters...);
1032 op_scheduler_.CompleteOperationAndRunNext(); 1117 op_scheduler_.CompleteOperationAndRunNext();
1033 } 1118 }
1034 1119
1120 void BackgroundSyncManager::CompleteStatusAndRegistrationCallback(
1121 StatusAndRegistrationCallback callback,
1122 BackgroundSyncStatus status,
1123 scoped_ptr<BackgroundSyncRegistrationHandle> result) {
1124 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1125
1126 callback.Run(status, result.Pass());
1127 op_scheduler_.CompleteOperationAndRunNext();
1128 }
1129
1130 void BackgroundSyncManager::CompleteStatusAndRegistrationsCallback(
1131 StatusAndRegistrationsCallback callback,
1132 BackgroundSyncStatus status,
1133 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> results) {
1134 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1135
1136 callback.Run(status, results.Pass());
1137 op_scheduler_.CompleteOperationAndRunNext();
1138 }
1139
1035 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { 1140 base::Closure BackgroundSyncManager::MakeEmptyCompletion() {
1036 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1141 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1037 1142
1038 return MakeClosureCompletion(base::Bind(base::DoNothing)); 1143 return MakeClosureCompletion(base::Bind(base::DoNothing));
1039 } 1144 }
1040 1145
1041 base::Closure BackgroundSyncManager::MakeClosureCompletion( 1146 base::Closure BackgroundSyncManager::MakeClosureCompletion(
1042 const base::Closure& callback) { 1147 const base::Closure& callback) {
1043 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1148 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1044 1149
1045 return base::Bind( 1150 return base::Bind(
1046 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, 1151 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>,
1047 weak_ptr_factory_.GetWeakPtr(), callback); 1152 weak_ptr_factory_.GetWeakPtr(), callback);
1048 } 1153 }
1049 1154
1050 BackgroundSyncManager::StatusAndRegistrationCallback 1155 BackgroundSyncManager::StatusAndRegistrationCallback
1051 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( 1156 BackgroundSyncManager::MakeStatusAndRegistrationCompletion(
1052 const StatusAndRegistrationCallback& callback) { 1157 const StatusAndRegistrationCallback& callback) {
1053 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1158 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1054 1159
1055 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< 1160 return base::Bind(
1056 StatusAndRegistrationCallback, BackgroundSyncStatus, 1161 &BackgroundSyncManager::CompleteStatusAndRegistrationCallback,
1057 const BackgroundSyncRegistration&>, 1162 weak_ptr_factory_.GetWeakPtr(), callback);
1058 weak_ptr_factory_.GetWeakPtr(), callback);
1059 } 1163 }
1060 1164
1061 BackgroundSyncManager::StatusAndRegistrationsCallback 1165 BackgroundSyncManager::StatusAndRegistrationsCallback
1062 BackgroundSyncManager::MakeStatusAndRegistrationsCompletion( 1166 BackgroundSyncManager::MakeStatusAndRegistrationsCompletion(
1063 const StatusAndRegistrationsCallback& callback) { 1167 const StatusAndRegistrationsCallback& callback) {
1064 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1168 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1065 1169
1066 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< 1170 return base::Bind(
1067 StatusAndRegistrationsCallback, BackgroundSyncStatus, 1171 &BackgroundSyncManager::CompleteStatusAndRegistrationsCallback,
1068 const std::vector<BackgroundSyncRegistration>&>, 1172 weak_ptr_factory_.GetWeakPtr(), callback);
1069 weak_ptr_factory_.GetWeakPtr(), callback);
1070 } 1173 }
1071 1174
1072 BackgroundSyncManager::StatusCallback 1175 BackgroundSyncManager::StatusCallback
1073 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { 1176 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) {
1074 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1177 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1075 1178
1076 return base::Bind( 1179 return base::Bind(
1077 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, 1180 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback,
1078 BackgroundSyncStatus>, 1181 BackgroundSyncStatus>,
1079 weak_ptr_factory_.GetWeakPtr(), callback); 1182 weak_ptr_factory_.GetWeakPtr(), callback);
1080 } 1183 }
1081 1184
1082 } // namespace content 1185 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698