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

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

Powered by Google App Engine
This is Rietveld 408576698