Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / power_monitor / power_monitor_controller.py
1 # Copyright 2014 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 import telemetry.core.platform.power_monitor as power_monitor
6
7
8 class PowerMonitorController(power_monitor.PowerMonitor):
9   """
10   PowerMonitor that acts as facade for a list of PowerMonitor objects and uses
11   the first available one.
12   """
13   def __init__(self, power_monitors):
14     super(PowerMonitorController, self).__init__()
15     self._cascading_power_monitors = power_monitors
16     self._active_monitor = None
17
18   def _AsyncPowerMonitor(self):
19     return next(
20         (x for x in self._cascading_power_monitors if x.CanMonitorPowerAsync()),
21         None)
22
23   def CanMonitorPowerAsync(self):
24     return bool(self._AsyncPowerMonitor())
25
26   def StartMonitoringPowerAsync(self):
27     self._active_monitor = self._AsyncPowerMonitor()
28     assert self._active_monitor, 'No available monitor.'
29     self._active_monitor.StartMonitoringPowerAsync()
30
31   def StopMonitoringPowerAsync(self):
32     assert self._active_monitor, 'StartMonitoringPowerAsync() not called.'
33     try:
34       return self._active_monitor.StopMonitoringPowerAsync()
35     finally:
36       self._active_monitor = None