Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1913)

Unified Diff: chromeos/power/power_data_collector.h

Issue 101963004: [chromeos] New PowerManagerClient observer to collect power data. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments in patch set 2 Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..da92429a4725a1fccf02c2d6fd71991bf8f2d957
--- /dev/null
+++ b/chromeos/power/power_data_collector.h
@@ -0,0 +1,103 @@
+// 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_POWER_POWER_DATA_COLLECTOR_H_
+#define CHROMEOS_POWER_POWER_DATA_COLLECTOR_H_
+
+#include <vector>
+
+#include "base/gtest_prod_util.h"
+#include "base/synchronization/lock.h"
+#include "base/time/time.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.
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.
+class PowerDataCollector
+ : public PowerManagerClient::Observer,
+ public base::RefCountedThreadSafe<PowerDataCollector> {
+ public:
+ struct PowerSupplySnapshot {
+ // 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.
+ base::TimeTicks time;
+
+ // 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.
+ 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();
+
+ // Returns the global instance of PowerDataCollector.
+ static PowerDataCollector *Get();
+
+ // Call this when processing the power supply data received from
+ // GetPowerSupplyData. Call UnlockPowerSupplyData after done with processing.
+ void LockPowerSupplyData(void);
+
+ // 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.
+ 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.
+
+ // Call this after done with processing the power supply data got from
+ // &GetPowerSupplyData.
+ 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();
+
+ void AppendPowerSupplySnapshot(const PowerSupplySnapshot& snapshot);
+ void ShutdownInstance(void);
+
+ // PowerManagerClient::Observer implementation:
+ virtual void PowerChanged(
+ const power_manager::PowerSupplyProperties& prop) OVERRIDE;
+
+ private:
+ // 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_POWER_POWER_DATA_COLLECTOR_H_

Powered by Google App Engine
This is Rietveld 408576698