Chromium Code Reviews| Index: chromeos/power/power_data_collector.h |
| diff --git a/chromeos/power/power_data_collector.h b/chromeos/power/power_data_collector.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3c40b67a4e2cf3909d086159bccbe9417336421f |
| --- /dev/null |
| +++ b/chromeos/power/power_data_collector.h |
| @@ -0,0 +1,98 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROMEOS_DBUS_POWER_DATA_COLLECTOR_H_ |
|
Daniel Erat
2013/12/11 01:06:52
nit: update these to reflect the new path (don't f
Siva Chandra
2013/12/11 20:32:02
Done.
|
| +#define CHROMEOS_DBUS_POWER_DATA_COLLECTOR_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/gtest_prod_util.h" |
| +#include "base/synchronization/lock.h" |
| +#include "chromeos/dbus/power_manager_client.h" |
| + |
| +namespace power_manager { |
| +class PowerSupplyProperties; |
| +} |
| + |
| +namespace chromeos { |
| + |
| +// A class which starts collecting power metrics, like the battery charge, as |
| +// soon as it is initialized via Initialize(). |
| +// |
| +// The constructor for this class is private as we want only a single global |
| +// instance to be created via Initialize(). Also, Initialize() should be called |
| +// only after the DBusThreadManager is initialized. The single global instance |
| +// should be destroyed via Shutdown(), and before the DBusThreadManager is |
| +// shutdown. |
| +class PowerDataCollector |
| + : public PowerManagerClient::Observer, |
| + public base::RefCountedThreadSafe<PowerDataCollector> { |
| + public: |
| + struct PowerSupplySnapshot { |
| + // Generated by Time::ToInternalValue. |
| + int64 time; |
|
Daniel Erat
2013/12/11 01:06:52
why not just use base::TimeTicks here? the interna
Siva Chandra
2013/12/11 20:32:02
Done.
How does one convert time ticks to human un
Daniel Erat
2013/12/11 20:59:20
base::TimeDelta is a class that represents an inte
|
| + |
| + // true if connected to externel power at the time of the snapshot. |
|
Daniel Erat
2013/12/11 01:06:52
nit: s/externel/external/
Siva Chandra
2013/12/11 20:32:02
Done.
|
| + bool external_power; |
| + |
| + // The battery charge as a percentage of full charge in range [0.0, 100.00]. |
| + double battery_charge; |
| + }; |
| + |
| + public: |
| + // Can be called only after DBusThreadManager is initialized. |
| + static void Initialize(); |
| + |
| + // Can be called only if initialized via Initialize, and before |
| + // DBusThreadManager is destroyed. |
| + static void Shutdown(); |
| + |
| + // Call this when processing the power supply data got from |
|
Daniel Erat
2013/12/11 01:06:52
nit: s/got/received/
Siva Chandra
2013/12/11 20:32:02
Done.
|
| + // GetPowerSupplyData. Call UnlockPowerSupplyData after done with processing. |
| + static void LockPowerSupplyData(void); |
|
Daniel Erat
2013/12/11 01:06:52
this lock/unlock mechanism seems error-prone. why
Siva Chandra
2013/12/11 20:32:02
Why is it error-prone?
Daniel Erat
2013/12/11 20:59:20
it is easy for a caller to acquire the lock and la
|
| + |
| + // Returns a reference to a vector of power supply data. Invoke this function |
| + // after locking the data with LockPowerSupplyData, and unlock after done |
| + // processing the data with UnlockPowerSupplyData. |
| + static std::vector<PowerSupplySnapshot> &GetPowerSupplyData(void); |
|
Daniel Erat
2013/12/11 01:06:52
instead of having a bunch of static methods, i thi
Siva Chandra
2013/12/11 20:32:02
Done.
|
| + |
| + // Call this after done with processing the power supply data got from |
| + // &GetPowerSupplyData. |
| + static void UnlockPowerSupplyData(void); |
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<PowerDataCollector>; |
| + friend class PowerDataCollectorTest; |
| + FRIEND_TEST_ALL_PREFIXES(PowerDataCollectorTest, AppendPowerSupplySnapshot); |
| + |
| + // When the 'testing' argument is 'true', the instance is created without |
| + // checking for an initialized DBusThreadManager and hence it does not listen |
| + // to any PowerManagerClient notifications. |
| + explicit PowerDataCollector(const bool testing); |
| + |
| + virtual ~PowerDataCollector() {} |
| + |
| + // Used in tests. Initializes the PowerDataCollector so that unit tests |
| + // can be performed without depending on the DBusThreadManager. |
| + static void InitializeTesting(); |
| + |
| + // Used in tests. Returns the global instance of PowerDataCollector. |
| + static PowerDataCollector *Get(); |
| + |
| + virtual void PowerChanged(const power_manager::PowerSupplyProperties& prop); |
|
Daniel Erat
2013/12/11 01:06:52
list virtual methods after other methods, add "//
Siva Chandra
2013/12/11 20:32:02
Done.
|
| + void AppendPowerSupplySnapshot(const PowerSupplySnapshot& snapshot); |
| + void Shutdown_Instance(void); |
|
Daniel Erat
2013/12/11 01:06:52
google c++ naming style would be ShutdownInstance
Siva Chandra
2013/12/11 20:32:02
Done.
|
| + |
| + // Is true for testing instance. |
| + bool testing_; |
| + |
| + base::Lock power_supply_lock_; |
| + std::vector<PowerSupplySnapshot> power_supply_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PowerDataCollector); |
| +}; |
| + |
| +} // namespace chromeos |
| + |
| +#endif // CHROMEOS_DBUS_POWER_DATA_COLLECTOR_H_ |