| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/battery/battery_status_service.h" | 5 #include "device/battery/battery_status_service.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | 40 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 41 DCHECK(!is_shutdown_); | 41 DCHECK(!is_shutdown_); |
| 42 | 42 |
| 43 if (!battery_fetcher_) | 43 if (!battery_fetcher_) |
| 44 battery_fetcher_ = BatteryStatusManager::Create(update_callback_); | 44 battery_fetcher_ = BatteryStatusManager::Create(update_callback_); |
| 45 | 45 |
| 46 if (callback_list_.empty()) { | 46 if (callback_list_.empty()) { |
| 47 bool success = battery_fetcher_->StartListeningBatteryChange(); | 47 bool success = battery_fetcher_->StartListeningBatteryChange(); |
| 48 // On failure pass the default values back. | 48 // On failure pass the default values back. |
| 49 if (!success) | 49 if (!success) |
| 50 callback.Run(BatteryStatus()); | 50 callback.Run(mojom::BatteryStatus()); |
| 51 } | 51 } |
| 52 | 52 |
| 53 if (status_updated_) { | 53 if (status_updated_) { |
| 54 // Send recent status to the new callback if already available. | 54 // Send recent status to the new callback if already available. |
| 55 callback.Run(status_); | 55 callback.Run(status_); |
| 56 } | 56 } |
| 57 | 57 |
| 58 return callback_list_.Add(callback); | 58 return callback_list_.Add(callback); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void BatteryStatusService::ConsumersChanged() { | 61 void BatteryStatusService::ConsumersChanged() { |
| 62 if (is_shutdown_) | 62 if (is_shutdown_) |
| 63 return; | 63 return; |
| 64 | 64 |
| 65 if (callback_list_.empty()) { | 65 if (callback_list_.empty()) { |
| 66 battery_fetcher_->StopListeningBatteryChange(); | 66 battery_fetcher_->StopListeningBatteryChange(); |
| 67 status_updated_ = false; | 67 status_updated_ = false; |
| 68 } | 68 } |
| 69 } | 69 } |
| 70 | 70 |
| 71 void BatteryStatusService::NotifyConsumers(const BatteryStatus& status) { | 71 void BatteryStatusService::NotifyConsumers(const mojom::BatteryStatus& status) { |
| 72 DCHECK(!is_shutdown_); | 72 DCHECK(!is_shutdown_); |
| 73 | 73 |
| 74 main_thread_task_runner_->PostTask(FROM_HERE, base::Bind( | 74 main_thread_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 75 &BatteryStatusService::NotifyConsumersOnMainThread, | 75 &BatteryStatusService::NotifyConsumersOnMainThread, |
| 76 base::Unretained(this), | 76 base::Unretained(this), |
| 77 status)); | 77 status)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void BatteryStatusService::NotifyConsumersOnMainThread( | 80 void BatteryStatusService::NotifyConsumersOnMainThread( |
| 81 const BatteryStatus& status) { | 81 const mojom::BatteryStatus& status) { |
| 82 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | 82 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 83 if (callback_list_.empty()) | 83 if (callback_list_.empty()) |
| 84 return; | 84 return; |
| 85 | 85 |
| 86 status_ = status; | 86 status_ = status; |
| 87 status_updated_ = true; | 87 status_updated_ = true; |
| 88 callback_list_.Notify(status_); | 88 callback_list_.Notify(status_); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void BatteryStatusService::Shutdown() { | 91 void BatteryStatusService::Shutdown() { |
| 92 if (!callback_list_.empty()) | 92 if (!callback_list_.empty()) |
| 93 battery_fetcher_->StopListeningBatteryChange(); | 93 battery_fetcher_->StopListeningBatteryChange(); |
| 94 battery_fetcher_.reset(); | 94 battery_fetcher_.reset(); |
| 95 is_shutdown_ = true; | 95 is_shutdown_ = true; |
| 96 } | 96 } |
| 97 | 97 |
| 98 const BatteryStatusService::BatteryUpdateCallback& | 98 const BatteryStatusService::BatteryUpdateCallback& |
| 99 BatteryStatusService::GetUpdateCallbackForTesting() const { | 99 BatteryStatusService::GetUpdateCallbackForTesting() const { |
| 100 return update_callback_; | 100 return update_callback_; |
| 101 } | 101 } |
| 102 | 102 |
| 103 void BatteryStatusService::SetBatteryManagerForTesting( | 103 void BatteryStatusService::SetBatteryManagerForTesting( |
| 104 std::unique_ptr<BatteryStatusManager> test_battery_manager) { | 104 std::unique_ptr<BatteryStatusManager> test_battery_manager) { |
| 105 battery_fetcher_ = std::move(test_battery_manager); | 105 battery_fetcher_ = std::move(test_battery_manager); |
| 106 status_ = BatteryStatus(); | 106 status_ = mojom::BatteryStatus(); |
| 107 status_updated_ = false; | 107 status_updated_ = false; |
| 108 } | 108 } |
| 109 | 109 |
| 110 } // namespace device | 110 } // namespace device |
| OLD | NEW |