| 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 #ifndef DEVICE_BATTERY_BATTERY_STATUS_SERVICE_H_ | 5 #ifndef DEVICE_BATTERY_BATTERY_STATUS_SERVICE_H_ |
| 6 #define DEVICE_BATTERY_BATTERY_STATUS_SERVICE_H_ | 6 #define DEVICE_BATTERY_BATTERY_STATUS_SERVICE_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 9 |
| 8 #include "base/callback_list.h" | 10 #include "base/callback_list.h" |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 12 #include "device/battery/battery_export.h" | 13 #include "device/battery/battery_export.h" |
| 13 #include "device/battery/battery_status.mojom.h" | 14 #include "device/battery/battery_status.mojom.h" |
| 14 | 15 |
| 15 namespace base { | 16 namespace base { |
| 16 class SingleThreadTaskRunner; | 17 class SingleThreadTaskRunner; |
| 17 } | 18 } |
| 18 | 19 |
| 19 namespace device { | 20 namespace device { |
| 20 class BatteryStatusManager; | 21 class BatteryStatusManager; |
| 21 | 22 |
| 22 class DEVICE_BATTERY_EXPORT BatteryStatusService { | 23 class DEVICE_BATTERY_EXPORT BatteryStatusService { |
| 23 public: | 24 public: |
| 24 typedef base::Callback<void(const BatteryStatus&)> BatteryUpdateCallback; | 25 typedef base::Callback<void(const BatteryStatus&)> BatteryUpdateCallback; |
| 25 typedef base::CallbackList<void(const BatteryStatus&)> | 26 typedef base::CallbackList<void(const BatteryStatus&)> |
| 26 BatteryUpdateCallbackList; | 27 BatteryUpdateCallbackList; |
| 27 typedef BatteryUpdateCallbackList::Subscription BatteryUpdateSubscription; | 28 typedef BatteryUpdateCallbackList::Subscription BatteryUpdateSubscription; |
| 28 | 29 |
| 29 // Returns the BatteryStatusService singleton. | 30 // Returns the BatteryStatusService singleton. |
| 30 static BatteryStatusService* GetInstance(); | 31 static BatteryStatusService* GetInstance(); |
| 31 | 32 |
| 32 // Adds a callback to receive battery status updates. Must be called on the | 33 // Adds a callback to receive battery status updates. Must be called on the |
| 33 // main thread. The callback itself will be called on the main thread as well. | 34 // main thread. The callback itself will be called on the main thread as well. |
| 34 // NOTE: The callback may be run before AddCallback returns! | 35 // NOTE: The callback may be run before AddCallback returns! |
| 35 scoped_ptr<BatteryUpdateSubscription> AddCallback( | 36 std::unique_ptr<BatteryUpdateSubscription> AddCallback( |
| 36 const BatteryUpdateCallback& callback); | 37 const BatteryUpdateCallback& callback); |
| 37 | 38 |
| 38 // Gracefully clean-up. | 39 // Gracefully clean-up. |
| 39 void Shutdown(); | 40 void Shutdown(); |
| 40 | 41 |
| 41 // Injects a custom battery status manager for testing purposes. | 42 // Injects a custom battery status manager for testing purposes. |
| 42 void SetBatteryManagerForTesting( | 43 void SetBatteryManagerForTesting( |
| 43 scoped_ptr<BatteryStatusManager> test_battery_manager); | 44 std::unique_ptr<BatteryStatusManager> test_battery_manager); |
| 44 | 45 |
| 45 // Returns callback to invoke when battery is changed. Used for testing. | 46 // Returns callback to invoke when battery is changed. Used for testing. |
| 46 const BatteryUpdateCallback& GetUpdateCallbackForTesting() const; | 47 const BatteryUpdateCallback& GetUpdateCallbackForTesting() const; |
| 47 | 48 |
| 48 private: | 49 private: |
| 49 friend struct base::DefaultSingletonTraits<BatteryStatusService>; | 50 friend struct base::DefaultSingletonTraits<BatteryStatusService>; |
| 50 friend class BatteryStatusServiceTest; | 51 friend class BatteryStatusServiceTest; |
| 51 | 52 |
| 52 BatteryStatusService(); | 53 BatteryStatusService(); |
| 53 virtual ~BatteryStatusService(); | 54 virtual ~BatteryStatusService(); |
| 54 | 55 |
| 55 // Updates current battery status and sends new status to interested | 56 // Updates current battery status and sends new status to interested |
| 56 // render processes. Can be called on any thread via a callback. | 57 // render processes. Can be called on any thread via a callback. |
| 57 void NotifyConsumers(const BatteryStatus& status); | 58 void NotifyConsumers(const BatteryStatus& status); |
| 58 void NotifyConsumersOnMainThread(const BatteryStatus& status); | 59 void NotifyConsumersOnMainThread(const BatteryStatus& status); |
| 59 void ConsumersChanged(); | 60 void ConsumersChanged(); |
| 60 | 61 |
| 61 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 62 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
| 62 scoped_ptr<BatteryStatusManager> battery_fetcher_; | 63 std::unique_ptr<BatteryStatusManager> battery_fetcher_; |
| 63 BatteryUpdateCallbackList callback_list_; | 64 BatteryUpdateCallbackList callback_list_; |
| 64 BatteryUpdateCallback update_callback_; | 65 BatteryUpdateCallback update_callback_; |
| 65 BatteryStatus status_; | 66 BatteryStatus status_; |
| 66 bool status_updated_; | 67 bool status_updated_; |
| 67 bool is_shutdown_; | 68 bool is_shutdown_; |
| 68 | 69 |
| 69 DISALLOW_COPY_AND_ASSIGN(BatteryStatusService); | 70 DISALLOW_COPY_AND_ASSIGN(BatteryStatusService); |
| 70 }; | 71 }; |
| 71 | 72 |
| 72 } // namespace device | 73 } // namespace device |
| 73 | 74 |
| 74 #endif // DEVICE_BATTERY_BATTERY_STATUS_SERVICE_H_ | 75 #endif // DEVICE_BATTERY_BATTERY_STATUS_SERVICE_H_ |
| OLD | NEW |