Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / power / peripheral_battery_observer.h
blob0e958be13b1675c204be9a2d8e205d6ac695397c
1 // Copyright (c) 2013 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 CHROME_BROWSER_CHROMEOS_POWER_PERIPHERAL_BATTERY_OBSERVER_H_
6 #define CHROME_BROWSER_CHROMEOS_POWER_PERIPHERAL_BATTERY_OBSERVER_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/test/simple_test_tick_clock.h"
15 #include "chromeos/dbus/power_manager_client.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
18 namespace chromeos {
20 class BluetoothDevice;
21 class PeripheralBatteryObserverTest;
23 // This observer listens for peripheral device battery status and shows
24 // notifications for low battery conditions.
25 class PeripheralBatteryObserver : public PowerManagerClient::Observer,
26 public device::BluetoothAdapter::Observer {
27 public:
28 // This class registers/unregisters itself as an observer in ctor/dtor.
29 PeripheralBatteryObserver();
30 virtual ~PeripheralBatteryObserver();
32 void set_testing_clock(base::SimpleTestTickClock* clock) {
33 testing_clock_ = clock;
36 // PowerManagerClient::Observer implementation.
37 virtual void PeripheralBatteryStatusReceived(const std::string& path,
38 const std::string& name,
39 int level) OVERRIDE;
41 // device::BluetoothAdapter::Observer implementation.
42 virtual void DeviceChanged(device::BluetoothAdapter* adapter,
43 device::BluetoothDevice* device) OVERRIDE;
44 virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
45 device::BluetoothDevice* device) OVERRIDE;
47 private:
48 friend class PeripheralBatteryObserverTest;
49 FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryObserverTest, Basic);
50 FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryObserverTest, InvalidBatteryInfo);
51 FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryObserverTest, DeviceRemove);
53 struct BatteryInfo {
54 BatteryInfo() : level(-1) {}
55 BatteryInfo(const std::string& name,
56 int level,
57 base::TimeTicks notification_timestamp)
58 : name(name),
59 level(level),
60 last_notification_timestamp(notification_timestamp) {
63 // Human readable name for the device. It is changeable.
64 std::string name;
65 // Battery level within range [0, 100], and -1 for unknown level.
66 int level;
67 base::TimeTicks last_notification_timestamp;
70 void InitializeOnBluetoothReady(
71 scoped_refptr<device::BluetoothAdapter> adapter);
73 void RemoveBattery(const std::string& address);
75 // Posts a low battery notification with unique id |address|. Returns true
76 // if the notification is posted, false if not.
77 bool PostNotification(const std::string& address, const BatteryInfo& battery);
79 void CancelNotification(const std::string& address);
81 // Record of existing battery infomation. For bluetooth HID device, the key
82 // is the address of the bluetooth device.
83 std::map<std::string, BatteryInfo> batteries_;
85 // PeripheralBatteryObserver is an observer of |bluetooth_adapter_| for
86 // bluetooth device change/remove events.
87 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
89 // Used only for helping test. Not owned and can be NULL.
90 base::SimpleTestTickClock* testing_clock_;
92 scoped_ptr<base::WeakPtrFactory<PeripheralBatteryObserver> > weakptr_factory_;
94 DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryObserver);
97 } // namespace chromeos
99 #endif // CHROME_BROWSER_CHROMEOS_POWER_PERIPHERAL_BATTERY_OBSERVER_H_