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