| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/location.h" | 11 #include "base/location.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 14 #include "base/power_monitor/power_monitor.h" | 15 #include "base/power_monitor/power_monitor.h" |
| 15 #include "base/power_monitor/power_monitor_source.h" | 16 #include "base/power_monitor/power_monitor_source.h" |
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 // power_monitor_ takes ownership of power_monitor_source. | 334 // power_monitor_ takes ownership of power_monitor_source. |
| 334 power_monitor_.reset(new base::PowerMonitor( | 335 power_monitor_.reset(new base::PowerMonitor( |
| 335 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_))); | 336 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_))); |
| 336 | 337 |
| 337 SetOnBatteryPower(false); | 338 SetOnBatteryPower(false); |
| 338 | 339 |
| 339 scoped_ptr<TestBackgroundSyncController> background_sync_controller( | 340 scoped_ptr<TestBackgroundSyncController> background_sync_controller( |
| 340 new TestBackgroundSyncController()); | 341 new TestBackgroundSyncController()); |
| 341 test_controller_ = background_sync_controller.get(); | 342 test_controller_ = background_sync_controller.get(); |
| 342 helper_->browser_context()->SetBackgroundSyncController( | 343 helper_->browser_context()->SetBackgroundSyncController( |
| 343 background_sync_controller.Pass()); | 344 std::move(background_sync_controller)); |
| 344 | 345 |
| 345 SetMaxSyncAttemptsAndRestartManager(1); | 346 SetMaxSyncAttemptsAndRestartManager(1); |
| 346 | 347 |
| 347 // Wait for storage to finish initializing before registering service | 348 // Wait for storage to finish initializing before registering service |
| 348 // workers. | 349 // workers. |
| 349 base::RunLoop().RunUntilIdle(); | 350 base::RunLoop().RunUntilIdle(); |
| 350 RegisterServiceWorkers(); | 351 RegisterServiceWorkers(); |
| 351 } | 352 } |
| 352 | 353 |
| 353 void TearDown() override { | 354 void TearDown() override { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 power_monitor_source_->GeneratePowerStateEvent(on_battery_power); | 402 power_monitor_source_->GeneratePowerStateEvent(on_battery_power); |
| 402 base::RunLoop().RunUntilIdle(); | 403 base::RunLoop().RunUntilIdle(); |
| 403 } | 404 } |
| 404 | 405 |
| 405 void StatusAndRegistrationCallback( | 406 void StatusAndRegistrationCallback( |
| 406 bool* was_called, | 407 bool* was_called, |
| 407 BackgroundSyncStatus status, | 408 BackgroundSyncStatus status, |
| 408 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) { | 409 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) { |
| 409 *was_called = true; | 410 *was_called = true; |
| 410 callback_status_ = status; | 411 callback_status_ = status; |
| 411 callback_registration_handle_ = registration_handle.Pass(); | 412 callback_registration_handle_ = std::move(registration_handle); |
| 412 } | 413 } |
| 413 | 414 |
| 414 void StatusAndRegistrationsCallback( | 415 void StatusAndRegistrationsCallback( |
| 415 bool* was_called, | 416 bool* was_called, |
| 416 BackgroundSyncStatus status, | 417 BackgroundSyncStatus status, |
| 417 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> | 418 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> |
| 418 registration_handles) { | 419 registration_handles) { |
| 419 *was_called = true; | 420 *was_called = true; |
| 420 callback_status_ = status; | 421 callback_status_ = status; |
| 421 callback_registration_handles_ = registration_handles.Pass(); | 422 callback_registration_handles_ = std::move(registration_handles); |
| 422 } | 423 } |
| 423 | 424 |
| 424 void StatusCallback(bool* was_called, BackgroundSyncStatus status) { | 425 void StatusCallback(bool* was_called, BackgroundSyncStatus status) { |
| 425 *was_called = true; | 426 *was_called = true; |
| 426 callback_status_ = status; | 427 callback_status_ = status; |
| 427 } | 428 } |
| 428 | 429 |
| 429 protected: | 430 protected: |
| 430 void CreateBackgroundSyncManager() { | 431 void CreateBackgroundSyncManager() { |
| 431 ClearRegistrationHandles(); | 432 ClearRegistrationHandles(); |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 | 715 |
| 715 TEST_F(BackgroundSyncManagerTest, RegisterWithoutActiveSWRegistration) { | 716 TEST_F(BackgroundSyncManagerTest, RegisterWithoutActiveSWRegistration) { |
| 716 sw_registration_1_->UnsetVersion(sw_registration_1_->active_version()); | 717 sw_registration_1_->UnsetVersion(sw_registration_1_->active_version()); |
| 717 EXPECT_FALSE(Register(sync_options_1_)); | 718 EXPECT_FALSE(Register(sync_options_1_)); |
| 718 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback_status_); | 719 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback_status_); |
| 719 } | 720 } |
| 720 | 721 |
| 721 TEST_F(BackgroundSyncManagerTest, RegisterOverwrites) { | 722 TEST_F(BackgroundSyncManagerTest, RegisterOverwrites) { |
| 722 EXPECT_TRUE(Register(sync_options_1_)); | 723 EXPECT_TRUE(Register(sync_options_1_)); |
| 723 scoped_ptr<BackgroundSyncRegistrationHandle> first_registration_handle = | 724 scoped_ptr<BackgroundSyncRegistrationHandle> first_registration_handle = |
| 724 callback_registration_handle_.Pass(); | 725 std::move(callback_registration_handle_); |
| 725 | 726 |
| 726 sync_options_1_.min_period = 100; | 727 sync_options_1_.min_period = 100; |
| 727 EXPECT_TRUE(Register(sync_options_1_)); | 728 EXPECT_TRUE(Register(sync_options_1_)); |
| 728 EXPECT_LT(first_registration_handle->handle_id(), | 729 EXPECT_LT(first_registration_handle->handle_id(), |
| 729 callback_registration_handle_->handle_id()); | 730 callback_registration_handle_->handle_id()); |
| 730 EXPECT_FALSE(first_registration_handle->options()->Equals( | 731 EXPECT_FALSE(first_registration_handle->options()->Equals( |
| 731 *callback_registration_handle_->options())); | 732 *callback_registration_handle_->options())); |
| 732 } | 733 } |
| 733 | 734 |
| 734 TEST_F(BackgroundSyncManagerTest, RegisterOverlappingPeriodicAndOneShotTags) { | 735 TEST_F(BackgroundSyncManagerTest, RegisterOverlappingPeriodicAndOneShotTags) { |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 888 EXPECT_TRUE(Unregister(callback_registration_handle_.get())); | 889 EXPECT_TRUE(Unregister(callback_registration_handle_.get())); |
| 889 | 890 |
| 890 sync_options_1_.tag = std::string(MaxTagLength() + 1, 'a'); | 891 sync_options_1_.tag = std::string(MaxTagLength() + 1, 'a'); |
| 891 EXPECT_FALSE(Register(sync_options_1_)); | 892 EXPECT_FALSE(Register(sync_options_1_)); |
| 892 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback_status_); | 893 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback_status_); |
| 893 } | 894 } |
| 894 | 895 |
| 895 TEST_F(BackgroundSyncManagerTest, RegistrationIncreasesId) { | 896 TEST_F(BackgroundSyncManagerTest, RegistrationIncreasesId) { |
| 896 EXPECT_TRUE(Register(sync_options_1_)); | 897 EXPECT_TRUE(Register(sync_options_1_)); |
| 897 scoped_ptr<BackgroundSyncRegistrationHandle> registered_handle = | 898 scoped_ptr<BackgroundSyncRegistrationHandle> registered_handle = |
| 898 callback_registration_handle_.Pass(); | 899 std::move(callback_registration_handle_); |
| 899 BackgroundSyncRegistration::RegistrationId cur_id = | 900 BackgroundSyncRegistration::RegistrationId cur_id = |
| 900 registered_handle->handle_id(); | 901 registered_handle->handle_id(); |
| 901 | 902 |
| 902 EXPECT_TRUE(GetRegistration(sync_options_1_)); | 903 EXPECT_TRUE(GetRegistration(sync_options_1_)); |
| 903 EXPECT_TRUE(Register(sync_options_2_)); | 904 EXPECT_TRUE(Register(sync_options_2_)); |
| 904 EXPECT_LT(cur_id, callback_registration_handle_->handle_id()); | 905 EXPECT_LT(cur_id, callback_registration_handle_->handle_id()); |
| 905 cur_id = callback_registration_handle_->handle_id(); | 906 cur_id = callback_registration_handle_->handle_id(); |
| 906 | 907 |
| 907 EXPECT_TRUE(Unregister(registered_handle.get())); | 908 EXPECT_TRUE(Unregister(registered_handle.get())); |
| 908 EXPECT_TRUE(Register(sync_options_1_)); | 909 EXPECT_TRUE(Register(sync_options_1_)); |
| (...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1390 | 1391 |
| 1391 TEST_F(BackgroundSyncManagerTest, OverwritePendingRegistration) { | 1392 TEST_F(BackgroundSyncManagerTest, OverwritePendingRegistration) { |
| 1392 // An overwritten pending registration should complete with | 1393 // An overwritten pending registration should complete with |
| 1393 // BACKGROUND_SYNC_STATE_UNREGISTERED. | 1394 // BACKGROUND_SYNC_STATE_UNREGISTERED. |
| 1394 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; | 1395 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; |
| 1395 EXPECT_TRUE(Register(sync_options_1_)); | 1396 EXPECT_TRUE(Register(sync_options_1_)); |
| 1396 EXPECT_TRUE(GetRegistration(sync_options_1_)); | 1397 EXPECT_TRUE(GetRegistration(sync_options_1_)); |
| 1397 EXPECT_EQ(POWER_STATE_AVOID_DRAINING, | 1398 EXPECT_EQ(POWER_STATE_AVOID_DRAINING, |
| 1398 callback_registration_handle_->options()->power_state); | 1399 callback_registration_handle_->options()->power_state); |
| 1399 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle = | 1400 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle = |
| 1400 callback_registration_handle_.Pass(); | 1401 std::move(callback_registration_handle_); |
| 1401 | 1402 |
| 1402 // Overwrite the pending registration. | 1403 // Overwrite the pending registration. |
| 1403 sync_options_1_.power_state = POWER_STATE_AUTO; | 1404 sync_options_1_.power_state = POWER_STATE_AUTO; |
| 1404 EXPECT_TRUE(Register(sync_options_1_)); | 1405 EXPECT_TRUE(Register(sync_options_1_)); |
| 1405 EXPECT_TRUE(GetRegistration(sync_options_1_)); | 1406 EXPECT_TRUE(GetRegistration(sync_options_1_)); |
| 1406 EXPECT_EQ(POWER_STATE_AUTO, | 1407 EXPECT_EQ(POWER_STATE_AUTO, |
| 1407 callback_registration_handle_->options()->power_state); | 1408 callback_registration_handle_->options()->power_state); |
| 1408 | 1409 |
| 1409 EXPECT_TRUE(NotifyWhenFinished(original_handle.get())); | 1410 EXPECT_TRUE(NotifyWhenFinished(original_handle.get())); |
| 1410 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, FinishedState()); | 1411 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, FinishedState()); |
| 1411 EXPECT_EQ(0, sync_events_called_); | 1412 EXPECT_EQ(0, sync_events_called_); |
| 1412 } | 1413 } |
| 1413 | 1414 |
| 1414 TEST_F(BackgroundSyncManagerTest, OverwriteFiringRegistrationWhichSucceeds) { | 1415 TEST_F(BackgroundSyncManagerTest, OverwriteFiringRegistrationWhichSucceeds) { |
| 1415 // An overwritten pending registration should complete with | 1416 // An overwritten pending registration should complete with |
| 1416 // BACKGROUND_SYNC_STATE_SUCCESS if firing completes successfully. | 1417 // BACKGROUND_SYNC_STATE_SUCCESS if firing completes successfully. |
| 1417 InitDelayedSyncEventTest(); | 1418 InitDelayedSyncEventTest(); |
| 1418 | 1419 |
| 1419 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; | 1420 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; |
| 1420 RegisterAndVerifySyncEventDelayed(sync_options_1_); | 1421 RegisterAndVerifySyncEventDelayed(sync_options_1_); |
| 1421 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle = | 1422 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle = |
| 1422 callback_registration_handle_.Pass(); | 1423 std::move(callback_registration_handle_); |
| 1423 | 1424 |
| 1424 // The next registration won't block. | 1425 // The next registration won't block. |
| 1425 InitSyncEventTest(); | 1426 InitSyncEventTest(); |
| 1426 | 1427 |
| 1427 // Overwrite the firing registration. | 1428 // Overwrite the firing registration. |
| 1428 sync_options_1_.power_state = POWER_STATE_AUTO; | 1429 sync_options_1_.power_state = POWER_STATE_AUTO; |
| 1429 EXPECT_TRUE(Register(sync_options_1_)); | 1430 EXPECT_TRUE(Register(sync_options_1_)); |
| 1430 EXPECT_FALSE(NotifyWhenFinished(original_handle.get())); | 1431 EXPECT_FALSE(NotifyWhenFinished(original_handle.get())); |
| 1431 | 1432 |
| 1432 // Successfully finish the first event. | 1433 // Successfully finish the first event. |
| 1433 sync_fired_callback_.Run(SERVICE_WORKER_OK); | 1434 sync_fired_callback_.Run(SERVICE_WORKER_OK); |
| 1434 base::RunLoop().RunUntilIdle(); | 1435 base::RunLoop().RunUntilIdle(); |
| 1435 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, FinishedState()); | 1436 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, FinishedState()); |
| 1436 } | 1437 } |
| 1437 | 1438 |
| 1438 TEST_F(BackgroundSyncManagerTest, OverwriteFiringRegistrationWhichFails) { | 1439 TEST_F(BackgroundSyncManagerTest, OverwriteFiringRegistrationWhichFails) { |
| 1439 // An overwritten pending registration should complete with | 1440 // An overwritten pending registration should complete with |
| 1440 // BACKGROUND_SYNC_STATE_FAILED if firing fails. | 1441 // BACKGROUND_SYNC_STATE_FAILED if firing fails. |
| 1441 InitDelayedSyncEventTest(); | 1442 InitDelayedSyncEventTest(); |
| 1442 | 1443 |
| 1443 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; | 1444 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; |
| 1444 RegisterAndVerifySyncEventDelayed(sync_options_1_); | 1445 RegisterAndVerifySyncEventDelayed(sync_options_1_); |
| 1445 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle = | 1446 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle = |
| 1446 callback_registration_handle_.Pass(); | 1447 std::move(callback_registration_handle_); |
| 1447 | 1448 |
| 1448 // The next registration won't block. | 1449 // The next registration won't block. |
| 1449 InitSyncEventTest(); | 1450 InitSyncEventTest(); |
| 1450 | 1451 |
| 1451 // Overwrite the firing registration. | 1452 // Overwrite the firing registration. |
| 1452 sync_options_1_.power_state = POWER_STATE_AUTO; | 1453 sync_options_1_.power_state = POWER_STATE_AUTO; |
| 1453 EXPECT_TRUE(Register(sync_options_1_)); | 1454 EXPECT_TRUE(Register(sync_options_1_)); |
| 1454 EXPECT_FALSE(NotifyWhenFinished(original_handle.get())); | 1455 EXPECT_FALSE(NotifyWhenFinished(original_handle.get())); |
| 1455 | 1456 |
| 1456 // Fail the first event. | 1457 // Fail the first event. |
| (...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2027 // Run it again. | 2028 // Run it again. |
| 2028 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); | 2029 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); |
| 2029 test_background_sync_manager_->delayed_task().Run(); | 2030 test_background_sync_manager_->delayed_task().Run(); |
| 2030 base::RunLoop().RunUntilIdle(); | 2031 base::RunLoop().RunUntilIdle(); |
| 2031 EXPECT_FALSE(GetRegistration(sync_options_1_)); | 2032 EXPECT_FALSE(GetRegistration(sync_options_1_)); |
| 2032 EXPECT_EQ(BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE, | 2033 EXPECT_EQ(BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE, |
| 2033 test_background_sync_manager_->last_chance()); | 2034 test_background_sync_manager_->last_chance()); |
| 2034 } | 2035 } |
| 2035 | 2036 |
| 2036 } // namespace content | 2037 } // namespace content |
| OLD | NEW |