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