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

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

Powered by Google App Engine
This is Rietveld 408576698