OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/background_sync/background_sync_manager.h" | 5 #include "content/browser/background_sync/background_sync_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
14 #include "base/time/default_clock.h" | 14 #include "base/time/default_clock.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 #include "content/browser/background_sync/background_sync_metrics.h" | 16 #include "content/browser/background_sync/background_sync_metrics.h" |
17 #include "content/browser/background_sync/background_sync_network_observer.h" | 17 #include "content/browser/background_sync/background_sync_network_observer.h" |
18 #include "content/browser/background_sync/background_sync_power_observer.h" | |
19 #include "content/browser/background_sync/background_sync_registration_handle.h" | 18 #include "content/browser/background_sync/background_sync_registration_handle.h" |
20 #include "content/browser/background_sync/background_sync_registration_options.h
" | 19 #include "content/browser/background_sync/background_sync_registration_options.h
" |
21 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 20 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
22 #include "content/browser/service_worker/service_worker_storage.h" | 21 #include "content/browser/service_worker/service_worker_storage.h" |
23 #include "content/browser/storage_partition_impl.h" | 22 #include "content/browser/storage_partition_impl.h" |
24 #include "content/common/service_worker/service_worker_type_converters.h" | 23 #include "content/common/service_worker/service_worker_type_converters.h" |
25 #include "content/public/browser/background_sync_controller.h" | 24 #include "content/public/browser/background_sync_controller.h" |
26 #include "content/public/browser/browser_context.h" | 25 #include "content/public/browser/browser_context.h" |
27 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
28 #include "content/public/common/background_sync.mojom.h" | 27 #include "content/public/common/background_sync.mojom.h" |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 | 308 |
310 #if defined(OS_ANDROID) | 309 #if defined(OS_ANDROID) |
311 network_observer_.reset(new BackgroundSyncNetworkObserverAndroid( | 310 network_observer_.reset(new BackgroundSyncNetworkObserverAndroid( |
312 base::Bind(&BackgroundSyncManager::OnNetworkChanged, | 311 base::Bind(&BackgroundSyncManager::OnNetworkChanged, |
313 weak_ptr_factory_.GetWeakPtr()))); | 312 weak_ptr_factory_.GetWeakPtr()))); |
314 #else | 313 #else |
315 network_observer_.reset(new BackgroundSyncNetworkObserver( | 314 network_observer_.reset(new BackgroundSyncNetworkObserver( |
316 base::Bind(&BackgroundSyncManager::OnNetworkChanged, | 315 base::Bind(&BackgroundSyncManager::OnNetworkChanged, |
317 weak_ptr_factory_.GetWeakPtr()))); | 316 weak_ptr_factory_.GetWeakPtr()))); |
318 #endif | 317 #endif |
319 power_observer_.reset(new BackgroundSyncPowerObserver(base::Bind( | |
320 &BackgroundSyncManager::OnPowerChanged, weak_ptr_factory_.GetWeakPtr()))); | |
321 } | 318 } |
322 | 319 |
323 void BackgroundSyncManager::Init() { | 320 void BackgroundSyncManager::Init() { |
324 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 321 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
325 DCHECK(!op_scheduler_.ScheduledOperations()); | 322 DCHECK(!op_scheduler_.ScheduledOperations()); |
326 DCHECK(!disabled_); | 323 DCHECK(!disabled_); |
327 | 324 |
328 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, | 325 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, |
329 weak_ptr_factory_.GetWeakPtr(), | 326 weak_ptr_factory_.GetWeakPtr(), |
330 MakeEmptyCompletion())); | 327 MakeEmptyCompletion())); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 scoped_refptr<RefCountedRegistration> ref_registration( | 403 scoped_refptr<RefCountedRegistration> ref_registration( |
407 new RefCountedRegistration()); | 404 new RefCountedRegistration()); |
408 registrations->registration_map[registration_key] = ref_registration; | 405 registrations->registration_map[registration_key] = ref_registration; |
409 BackgroundSyncRegistration* registration = ref_registration->value(); | 406 BackgroundSyncRegistration* registration = ref_registration->value(); |
410 | 407 |
411 BackgroundSyncRegistrationOptions* options = registration->options(); | 408 BackgroundSyncRegistrationOptions* options = registration->options(); |
412 options->tag = registration_proto.tag(); | 409 options->tag = registration_proto.tag(); |
413 options->periodicity = registration_proto.periodicity(); | 410 options->periodicity = registration_proto.periodicity(); |
414 options->min_period = registration_proto.min_period(); | 411 options->min_period = registration_proto.min_period(); |
415 options->network_state = registration_proto.network_state(); | 412 options->network_state = registration_proto.network_state(); |
416 options->power_state = registration_proto.power_state(); | |
417 | 413 |
418 registration->set_id(registration_proto.id()); | 414 registration->set_id(registration_proto.id()); |
419 registration->set_num_attempts(registration_proto.num_attempts()); | 415 registration->set_num_attempts(registration_proto.num_attempts()); |
420 registration->set_delay_until( | 416 registration->set_delay_until( |
421 base::Time::FromInternalValue(registration_proto.delay_until())); | 417 base::Time::FromInternalValue(registration_proto.delay_until())); |
422 } | 418 } |
423 } | 419 } |
424 | 420 |
425 if (corruption_detected) | 421 if (corruption_detected) |
426 break; | 422 break; |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
667 const BackgroundSyncRegistration& registration = | 663 const BackgroundSyncRegistration& registration = |
668 *key_and_registration.second->value(); | 664 *key_and_registration.second->value(); |
669 BackgroundSyncRegistrationProto* registration_proto = | 665 BackgroundSyncRegistrationProto* registration_proto = |
670 registrations_proto.add_registration(); | 666 registrations_proto.add_registration(); |
671 registration_proto->set_id(registration.id()); | 667 registration_proto->set_id(registration.id()); |
672 registration_proto->set_tag(registration.options()->tag); | 668 registration_proto->set_tag(registration.options()->tag); |
673 registration_proto->set_periodicity(registration.options()->periodicity); | 669 registration_proto->set_periodicity(registration.options()->periodicity); |
674 registration_proto->set_min_period(registration.options()->min_period); | 670 registration_proto->set_min_period(registration.options()->min_period); |
675 registration_proto->set_network_state( | 671 registration_proto->set_network_state( |
676 registration.options()->network_state); | 672 registration.options()->network_state); |
677 registration_proto->set_power_state(registration.options()->power_state); | |
678 registration_proto->set_num_attempts(registration.num_attempts()); | 673 registration_proto->set_num_attempts(registration.num_attempts()); |
679 registration_proto->set_delay_until( | 674 registration_proto->set_delay_until( |
680 registration.delay_until().ToInternalValue()); | 675 registration.delay_until().ToInternalValue()); |
681 } | 676 } |
682 std::string serialized; | 677 std::string serialized; |
683 bool success = registrations_proto.SerializeToString(&serialized); | 678 bool success = registrations_proto.SerializeToString(&serialized); |
684 DCHECK(success); | 679 DCHECK(success); |
685 | 680 |
686 StoreDataInBackend(sw_registration_id, registrations.origin, | 681 StoreDataInBackend(sw_registration_id, registrations.origin, |
687 kBackgroundSyncUserDataKey, serialized, callback); | 682 kBackgroundSyncUserDataKey, serialized, callback); |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1068 } | 1063 } |
1069 | 1064 |
1070 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1065 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1071 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | 1066 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
1072 base::Passed(std::move(out_registrations)))); | 1067 base::Passed(std::move(out_registrations)))); |
1073 } | 1068 } |
1074 | 1069 |
1075 bool BackgroundSyncManager::AreOptionConditionsMet( | 1070 bool BackgroundSyncManager::AreOptionConditionsMet( |
1076 const BackgroundSyncRegistrationOptions& options) { | 1071 const BackgroundSyncRegistrationOptions& options) { |
1077 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1072 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1078 return network_observer_->NetworkSufficient(options.network_state) && | 1073 return network_observer_->NetworkSufficient(options.network_state); |
1079 power_observer_->PowerSufficient(options.power_state); | |
1080 } | 1074 } |
1081 | 1075 |
1082 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 1076 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
1083 const BackgroundSyncRegistration& registration) { | 1077 const BackgroundSyncRegistration& registration) { |
1084 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1078 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1085 | 1079 |
1086 // TODO(jkarlin): Add support for firing periodic registrations. | 1080 // TODO(jkarlin): Add support for firing periodic registrations. |
1087 if (registration.options()->periodicity == SYNC_PERIODIC) | 1081 if (registration.options()->periodicity == SYNC_PERIODIC) |
1088 return false; | 1082 return false; |
1089 | 1083 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1448 disabled_ = false; | 1442 disabled_ = false; |
1449 InitImpl(callback); | 1443 InitImpl(callback); |
1450 } | 1444 } |
1451 | 1445 |
1452 void BackgroundSyncManager::OnNetworkChanged() { | 1446 void BackgroundSyncManager::OnNetworkChanged() { |
1453 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1447 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1454 | 1448 |
1455 FireReadyEvents(); | 1449 FireReadyEvents(); |
1456 } | 1450 } |
1457 | 1451 |
1458 void BackgroundSyncManager::OnPowerChanged() { | |
1459 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
1460 | |
1461 FireReadyEvents(); | |
1462 } | |
1463 | |
1464 void BackgroundSyncManager::SetMaxSyncAttemptsImpl( | 1452 void BackgroundSyncManager::SetMaxSyncAttemptsImpl( |
1465 int max_attempts, | 1453 int max_attempts, |
1466 const base::Closure& callback) { | 1454 const base::Closure& callback) { |
1467 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1455 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1468 | 1456 |
1469 parameters_->max_sync_attempts = max_attempts; | 1457 parameters_->max_sync_attempts = max_attempts; |
1470 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 1458 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
1471 } | 1459 } |
1472 | 1460 |
1473 // TODO(jkarlin): Figure out how to pass scoped_ptrs with this. | 1461 // TODO(jkarlin): Figure out how to pass scoped_ptrs with this. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1540 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1528 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1541 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1529 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1542 | 1530 |
1543 return base::Bind( | 1531 return base::Bind( |
1544 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1532 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1545 BackgroundSyncStatus>, | 1533 BackgroundSyncStatus>, |
1546 weak_ptr_factory_.GetWeakPtr(), callback); | 1534 weak_ptr_factory_.GetWeakPtr(), callback); |
1547 } | 1535 } |
1548 | 1536 |
1549 } // namespace content | 1537 } // namespace content |
OLD | NEW |