Elim cr-checkbox
[chromium-blink-merge.git] / ash / system / chromeos / power / power_status.h
blob501b5a2693b2b2fe4f9efae91f66dd74fffd789e
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.
5 #ifndef ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_
6 #define ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_
8 #include <string>
9 #include <vector>
11 #include "ash/ash_export.h"
12 #include "base/basictypes.h"
13 #include "base/observer_list.h"
14 #include "base/strings/string16.h"
15 #include "base/time/time.h"
16 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
17 #include "chromeos/dbus/power_manager_client.h"
18 #include "ui/gfx/image/image_skia.h"
20 namespace ash {
22 // PowerStatus is a singleton that receives updates about the system's
23 // power status from chromeos::PowerManagerClient and makes the information
24 // available to interested classes within Ash.
25 class ASH_EXPORT PowerStatus : public chromeos::PowerManagerClient::Observer {
26 public:
27 // Different styles of battery icons.
28 enum IconSet {
29 ICON_LIGHT,
30 ICON_DARK
33 // Interface for classes that wish to be notified when the power status
34 // has changed.
35 class Observer {
36 public:
37 // Called when the power status changes.
38 virtual void OnPowerStatusChanged() = 0;
40 protected:
41 virtual ~Observer() {}
44 // Power source types.
45 enum DeviceType {
46 // Dedicated charger (AC adapter, USB power supply, etc.).
47 DEDICATED_CHARGER,
49 // Dual-role device.
50 DUAL_ROLE_USB,
53 // Port locations.
54 enum Port {
55 // Unknown, or the only port.
56 UNKNOWN_PORT,
57 LEFT_PORT,
58 RIGHT_PORT,
59 BACK_PORT,
60 FRONT_PORT,
62 // First word takes precedence, e.g. "frontmost port on the left side".
63 LEFT_FRONT_PORT,
64 LEFT_BACK_PORT,
65 RIGHT_FRONT_PORT,
66 RIGHT_BACK_PORT,
67 BACK_LEFT_PORT,
68 BACK_RIGHT_PORT,
71 // Information about an available power source.
72 struct PowerSource {
73 // ID provided by kernel.
74 std::string id;
76 // Type of power source.
77 DeviceType type;
79 // Location of the port used.
80 Port port;
83 // Maximum battery time-to-full or time-to-empty that should be displayed
84 // in the UI. If the current is close to zero, battery time estimates can
85 // get very large; avoid displaying these large numbers.
86 static const int kMaxBatteryTimeToDisplaySec;
88 // Sets the global instance. Must be called before any calls to Get().
89 static void Initialize();
91 // Destroys the global instance.
92 static void Shutdown();
94 // Returns true if the global instance is initialized.
95 static bool IsInitialized();
97 // Gets the global instance. Initialize must be called first.
98 static PowerStatus* Get();
100 // Returns true if |time|, a time returned by GetBatteryTimeToEmpty() or
101 // GetBatteryTimeToFull(), should be displayed in the UI.
102 // Less-than-a-minute or very large values aren't displayed.
103 static bool ShouldDisplayBatteryTime(const base::TimeDelta& time);
105 // Copies the hour and minute components of |time| to |hours| and |minutes|.
106 // The minute component is rounded rather than truncated: a |time| value
107 // corresponding to 92 seconds will produce a |minutes| value of 2, for
108 // example.
109 static void SplitTimeIntoHoursAndMinutes(const base::TimeDelta& time,
110 int* hours,
111 int* minutes);
113 // Adds or removes an observer.
114 void AddObserver(Observer* observer);
115 void RemoveObserver(Observer* observer);
117 // Requests updated status from the power manager.
118 void RequestStatusUpdate();
120 // Changes the power source to the source with the given ID.
121 void SetPowerSource(const std::string& id);
123 // Returns true if a battery is present.
124 bool IsBatteryPresent() const;
126 // Returns true if the battery is full. This also implies that a charger
127 // is connected.
128 bool IsBatteryFull() const;
130 // Returns true if the battery is charging. Note that this implies that a
131 // charger is connected but the converse is not necessarily true: the
132 // battery may be discharging even while a (perhaps low-power) charger is
133 // connected. Use Is*Connected() to test for the presence of a charger
134 // and also see IsBatteryDischargingOnLinePower().
135 bool IsBatteryCharging() const;
137 // Returns true if the battery is discharging (or neither charging nor
138 // discharging while not being full) while line power is connected.
139 bool IsBatteryDischargingOnLinePower() const;
141 // Returns the battery's remaining charge as a value in the range [0.0,
142 // 100.0].
143 double GetBatteryPercent() const;
145 // Returns the battery's remaining charge, rounded to an integer with a
146 // maximum value of 100.
147 int GetRoundedBatteryPercent() const;
149 // Returns true if the battery's time-to-full and time-to-empty estimates
150 // should not be displayed because the power manager is still calculating
151 // them.
152 bool IsBatteryTimeBeingCalculated() const;
154 // Returns the estimated time until the battery is empty (if line power
155 // is disconnected) or full (if line power is connected). These estimates
156 // should only be used if IsBatteryTimeBeingCalculated() returns false.
157 base::TimeDelta GetBatteryTimeToEmpty() const;
158 base::TimeDelta GetBatteryTimeToFull() const;
160 // Returns true if line power (including a charger of any type) is connected.
161 bool IsLinePowerConnected() const;
163 // Returns true if an official, non-USB charger is connected.
164 bool IsMainsChargerConnected() const;
166 // Returns true if a USB charger (which is likely to only support a low
167 // charging rate) is connected.
168 bool IsUsbChargerConnected() const;
170 // Returns true if the system allows some connected devices to function as
171 // either power sources or sinks.
172 bool SupportsDualRoleDevices() const;
174 // Returns true if at least one dual-role device is connected.
175 bool HasDualRoleDevices() const;
177 // Returns a list of available power sources which the user may select.
178 std::vector<PowerSource> GetPowerSources() const;
180 // Returns the ID of the currently used power source, or an empty string if no
181 // power source is selected.
182 std::string GetCurrentPowerSourceID() const;
184 // Returns the image that should be shown for the battery's current state.
185 gfx::ImageSkia GetBatteryImage(IconSet icon_set) const;
187 // Returns an string describing the current state for accessibility.
188 base::string16 GetAccessibleNameString(bool full_description) const;
190 // Updates |proto_|. Does not notify observers.
191 void SetProtoForTesting(const power_manager::PowerSupplyProperties& proto);
193 protected:
194 PowerStatus();
195 ~PowerStatus() override;
197 private:
198 // Overriden from PowerManagerClient::Observer.
199 void PowerChanged(const power_manager::PowerSupplyProperties& proto) override;
201 base::ObserverList<Observer> observers_;
203 // Current state.
204 power_manager::PowerSupplyProperties proto_;
206 DISALLOW_COPY_AND_ASSIGN(PowerStatus);
209 } // namespace ash
211 #endif // ASH_SYSTEM_CHROMEOS_POWER_POWER_STATUS_H_