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