Home | History | Annotate | Download | only in power
      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.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_PERIPHERAL_BATTERY_OBSERVER_H_
      6 #define CHROME_BROWSER_CHROMEOS_POWER_PERIPHERAL_BATTERY_OBSERVER_H_
      7 
      8 #include <map>
      9 
     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"
     17 
     18 namespace chromeos {
     19 
     20 class BluetoothDevice;
     21 class PeripheralBatteryObserverTest;
     22 
     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();
     31 
     32   void set_testing_clock(base::SimpleTestTickClock* clock) {
     33     testing_clock_ = clock;
     34   }
     35 
     36   // PowerManagerClient::Observer implementation.
     37   virtual void PeripheralBatteryStatusReceived(const std::string& path,
     38                                                const std::string& name,
     39                                                int level) OVERRIDE;
     40 
     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;
     46 
     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);
     52 
     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) {
     61     }
     62 
     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;
     68   };
     69 
     70   void InitializeOnBluetoothReady(
     71       scoped_refptr<device::BluetoothAdapter> adapter);
     72 
     73   void RemoveBattery(const std::string& address);
     74 
     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);
     78 
     79   void CancelNotification(const std::string& address);
     80 
     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_;
     84 
     85   // PeripheralBatteryObserver is an observer of |bluetooth_adapter_| for
     86   // bluetooth device change/remove events.
     87   scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
     88 
     89   // Used only for helping test. Not owned and can be NULL.
     90   base::SimpleTestTickClock* testing_clock_;
     91 
     92   scoped_ptr<base::WeakPtrFactory<PeripheralBatteryObserver> > weakptr_factory_;
     93 
     94   DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryObserver);
     95 };
     96 
     97 }  // namespace chromeos
     98 
     99 #endif  // CHROME_BROWSER_CHROMEOS_POWER_PERIPHERAL_BATTERY_OBSERVER_H_
    100