Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / device / battery / battery_status_service.h
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 "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"
13
14 namespace base {
15 class SingleThreadTaskRunner;
16 }
17
18 namespace device {
19 class BatteryStatusManager;
20
21 class DEVICE_BATTERY_EXPORT BatteryStatusService {
22  public:
23   typedef base::Callback<void(const BatteryStatus&)> BatteryUpdateCallback;
24   typedef base::CallbackList<void(const BatteryStatus&)>
25       BatteryUpdateCallbackList;
26   typedef BatteryUpdateCallbackList::Subscription BatteryUpdateSubscription;
27
28   // Returns the BatteryStatusService singleton.
29   static BatteryStatusService* GetInstance();
30
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   scoped_ptr<BatteryUpdateSubscription> AddCallback(
34       const BatteryUpdateCallback& callback);
35
36   // Gracefully clean-up.
37   void Shutdown();
38
39   // Injects a custom battery status manager for testing purposes.
40   void SetBatteryManagerForTesting(
41       scoped_ptr<BatteryStatusManager> test_battery_manager);
42
43   // Returns callback to invoke when battery is changed. Used for testing.
44   const BatteryUpdateCallback& GetUpdateCallbackForTesting() const;
45
46  private:
47   friend struct DefaultSingletonTraits<BatteryStatusService>;
48
49   BatteryStatusService();
50   virtual ~BatteryStatusService();
51
52   // Updates current battery status and sends new status to interested
53   // render processes. Can be called on any thread via a callback.
54   void NotifyConsumers(const BatteryStatus& status);
55   void NotifyConsumersOnMainThread(const BatteryStatus& status);
56   void ConsumersChanged();
57
58   scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
59   scoped_ptr<BatteryStatusManager> battery_fetcher_;
60   BatteryUpdateCallbackList callback_list_;
61   BatteryUpdateCallback update_callback_;
62   BatteryStatus status_;
63   bool status_updated_;
64   bool is_shutdown_;
65
66   DISALLOW_COPY_AND_ASSIGN(BatteryStatusService);
67 };
68
69 }  // namespace device
70
71 #endif  // DEVICE_BATTERY_BATTERY_STATUS_SERVICE_H_