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_
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"
22 class BluetoothDevice
;
23 class PeripheralBatteryObserverTest
;
25 // This observer listens for peripheral device battery status and shows
26 // notifications for low battery conditions.
27 class PeripheralBatteryObserver
: public PowerManagerClient::Observer
,
28 public device::BluetoothAdapter::Observer
{
30 // This class registers/unregisters itself as an observer in ctor/dtor.
31 PeripheralBatteryObserver();
32 ~PeripheralBatteryObserver() override
;
34 void set_testing_clock(base::SimpleTestTickClock
* clock
) {
35 testing_clock_
= clock
;
38 // PowerManagerClient::Observer implementation.
39 void PeripheralBatteryStatusReceived(const std::string
& path
,
40 const std::string
& name
,
43 // device::BluetoothAdapter::Observer implementation.
44 void DeviceChanged(device::BluetoothAdapter
* adapter
,
45 device::BluetoothDevice
* device
) override
;
46 void DeviceRemoved(device::BluetoothAdapter
* adapter
,
47 device::BluetoothDevice
* device
) override
;
50 friend class PeripheralBatteryObserverTest
;
51 FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryObserverTest
, Basic
);
52 FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryObserverTest
, InvalidBatteryInfo
);
53 FRIEND_TEST_ALL_PREFIXES(PeripheralBatteryObserverTest
, DeviceRemove
);
56 BatteryInfo() : level(-1) {}
57 BatteryInfo(const std::string
& name
,
59 base::TimeTicks notification_timestamp
)
62 last_notification_timestamp(notification_timestamp
) {
65 // Human readable name for the device. It is changeable.
67 // Battery level within range [0, 100], and -1 for unknown level.
69 base::TimeTicks last_notification_timestamp
;
72 void InitializeOnBluetoothReady(
73 scoped_refptr
<device::BluetoothAdapter
> adapter
);
75 void RemoveBattery(const std::string
& address
);
77 // Posts a low battery notification with unique id |address|. Returns true
78 // if the notification is posted, false if not.
79 bool PostNotification(const std::string
& address
, const BatteryInfo
& battery
);
81 void CancelNotification(const std::string
& address
);
83 // Record of existing battery infomation. For bluetooth HID device, the key
84 // is the address of the bluetooth device.
85 std::map
<std::string
, BatteryInfo
> batteries_
;
87 // PeripheralBatteryObserver is an observer of |bluetooth_adapter_| for
88 // bluetooth device change/remove events.
89 scoped_refptr
<device::BluetoothAdapter
> bluetooth_adapter_
;
91 // Used only for helping test. Not owned and can be NULL.
92 base::SimpleTestTickClock
* testing_clock_
;
94 // Record the profile used when adding message center notifications.
95 Profile
* notification_profile_
;
97 scoped_ptr
<base::WeakPtrFactory
<PeripheralBatteryObserver
> > weakptr_factory_
;
99 DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryObserver
);
102 } // namespace chromeos
104 #endif // CHROME_BROWSER_CHROMEOS_POWER_PERIPHERAL_BATTERY_OBSERVER_H_