Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_ | |
| 6 #define CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/gtest_prod_util.h" | |
| 11 #include "base/synchronization/lock.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "chromeos/dbus/power_manager_client.h" | |
| 14 | |
| 15 namespace power_manager { | |
| 16 class PowerSupplyProperties; | |
| 17 } | |
| 18 | |
| 19 namespace chromeos { | |
| 20 | |
| 21 // A class which starts collecting power metrics, like the battery charge, as | |
| 22 // soon as it is initialized via Initialize(). | |
| 23 // | |
| 24 // The constructor for this class is private as we want only a single global | |
| 25 // instance to be created via Initialize(). Also, Initialize() should be called | |
| 26 // only after the DBusThreadManager is initialized. The single global instance | |
| 27 // should be destroyed via Shutdown(), and before the DBusThreadManager is | |
| 28 // shutdown. | |
|
stevenjb
2013/12/11 20:54:37
This paragraph could be simplified, e.g.: "This cl
Siva Chandra
2013/12/11 21:30:56
Done.
| |
| 29 class PowerDataCollector | |
| 30 : public PowerManagerClient::Observer, | |
| 31 public base::RefCountedThreadSafe<PowerDataCollector> { | |
| 32 public: | |
| 33 struct PowerSupplySnapshot { | |
| 34 // Generated by TimeTicks::Now. | |
|
stevenjb
2013/12/11 20:54:37
Describe what this represents (the time of the sna
Siva Chandra
2013/12/11 21:30:56
Done.
| |
| 35 base::TimeTicks time; | |
| 36 | |
| 37 // true if connected to external power at the time of the snapshot. | |
|
stevenjb
2013/12/11 20:54:37
True
Siva Chandra
2013/12/11 21:30:56
Done.
| |
| 38 bool external_power; | |
| 39 | |
| 40 // The battery charge as a percentage of full charge in range [0.0, 100.00]. | |
| 41 double battery_charge; | |
| 42 }; | |
| 43 | |
| 44 public: | |
| 45 // Can be called only after DBusThreadManager is initialized. | |
| 46 static void Initialize(); | |
| 47 | |
| 48 // Can be called only if initialized via Initialize, and before | |
| 49 // DBusThreadManager is destroyed. | |
| 50 static void Shutdown(); | |
| 51 | |
| 52 // Returns the global instance of PowerDataCollector. | |
| 53 static PowerDataCollector *Get(); | |
| 54 | |
| 55 // Call this when processing the power supply data received from | |
| 56 // GetPowerSupplyData. Call UnlockPowerSupplyData after done with processing. | |
| 57 void LockPowerSupplyData(void); | |
| 58 | |
| 59 // Returns a reference to a vector of power supply data. Invoke this function | |
| 60 // after locking the data with LockPowerSupplyData, and unlock after done | |
| 61 // processing the data with UnlockPowerSupplyData. | |
| 62 std::vector<PowerSupplySnapshot> &GetPowerSupplyData(void); | |
|
stevenjb
2013/12/11 20:54:37
I don't think we should introduce a lock just to a
Siva Chandra
2013/12/11 21:30:56
Done.
| |
| 63 | |
| 64 // Call this after done with processing the power supply data got from | |
| 65 // &GetPowerSupplyData. | |
| 66 void UnlockPowerSupplyData(void); | |
| 67 | |
| 68 private: | |
| 69 friend class base::RefCountedThreadSafe<PowerDataCollector>; | |
| 70 friend class PowerDataCollectorTest; | |
| 71 FRIEND_TEST_ALL_PREFIXES(PowerDataCollectorTest, AppendPowerSupplySnapshot); | |
| 72 | |
| 73 // When the 'testing' argument is 'true', the instance is created without | |
| 74 // checking for an initialized DBusThreadManager and hence it does not listen | |
| 75 // to any PowerManagerClient notifications. | |
| 76 explicit PowerDataCollector(const bool testing); | |
| 77 | |
| 78 virtual ~PowerDataCollector() {} | |
| 79 | |
| 80 // Used in tests. Initializes the PowerDataCollector so that unit tests | |
| 81 // can be performed without depending on the DBusThreadManager. | |
| 82 static void InitializeTesting(); | |
| 83 | |
| 84 void AppendPowerSupplySnapshot(const PowerSupplySnapshot& snapshot); | |
| 85 void ShutdownInstance(void); | |
| 86 | |
| 87 // PowerManagerClient::Observer implementation: | |
| 88 virtual void PowerChanged( | |
| 89 const power_manager::PowerSupplyProperties& prop) OVERRIDE; | |
| 90 | |
| 91 private: | |
| 92 // Is true for testing instance. | |
| 93 bool testing_; | |
| 94 | |
| 95 base::Lock power_supply_lock_; | |
| 96 std::vector<PowerSupplySnapshot> power_supply_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(PowerDataCollector); | |
| 99 }; | |
| 100 | |
| 101 } // namespace chromeos | |
| 102 | |
| 103 #endif // CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_ | |
| OLD | NEW |