1 // Copyright 2017 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 CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_DEVICE_POWER_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_DEVICE_POWER_HANDLER_H_
7 
8 #include <memory>
9 #include <set>
10 
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/optional.h"
14 #include "base/scoped_observer.h"
15 #include "base/strings/string16.h"
16 #include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h"
17 #include "chromeos/dbus/power/power_manager_client.h"
18 #include "chromeos/dbus/power/power_policy_controller.h"
19 
20 class PrefChangeRegistrar;
21 class PrefService;
22 
23 namespace base {
24 class ListValue;
25 class TimeTicks;
26 }  // namespace base
27 
28 namespace chromeos {
29 namespace settings {
30 
31 // Chrome OS battery status and power settings handler.
32 class PowerHandler : public ::settings::SettingsPageUIHandler,
33                      public PowerManagerClient::Observer {
34  public:
35   // Idle behaviors presented in the UI. These are mapped to preferences by
36   // HandleSetIdleBehavior(). Values are shared with JS and exposed here for
37   // tests.
38   enum class IdleBehavior {
39     DISPLAY_OFF_SLEEP = 0,
40     DISPLAY_OFF = 1,
41     DISPLAY_ON = 2,
42     SHUT_DOWN = 3,
43     STOP_SESSION = 4,
44   };
45 
46   // WebUI message name and dictionary keys. Shared with tests.
47   static const char kPowerManagementSettingsChangedName[];
48   static const char kPossibleAcIdleBehaviorsKey[];
49   static const char kPossibleBatteryIdleBehaviorsKey[];
50   static const char kAcIdleManagedKey[];
51   static const char kBatteryIdleManagedKey[];
52   static const char kCurrentAcIdleBehaviorKey[];
53   static const char kCurrentBatteryIdleBehaviorKey[];
54   static const char kLidClosedBehaviorKey[];
55   static const char kLidClosedControlledKey[];
56   static const char kHasLidKey[];
57 
58   // Class used by tests to interact with PowerHandler internals.
59   class TestAPI {
60    public:
61     explicit TestAPI(PowerHandler* handler);
62     ~TestAPI();
63 
64     void RequestPowerManagementSettings();
65     // Sets AC idle behavior to |behavior| if |when_on_ac| is true. Otherwise
66     // sets battery idle behavior to |behavior|.
67     void SetIdleBehavior(IdleBehavior behavior, bool when_on_ac);
68     void SetLidClosedBehavior(PowerPolicyController::Action behavior);
69 
70    private:
71     PowerHandler* handler_;  // Not owned.
72 
73     DISALLOW_COPY_AND_ASSIGN(TestAPI);
74   };
75 
76   explicit PowerHandler(PrefService* prefs);
77   ~PowerHandler() override;
78 
79   // SettingsPageUIHandler implementation.
80   void RegisterMessages() override;
81   void OnJavascriptAllowed() override;
82   void OnJavascriptDisallowed() override;
83 
84   // PowerManagerClient implementation.
85   void PowerChanged(const power_manager::PowerSupplyProperties& proto) override;
86   void PowerManagerRestarted() override;
87   void LidEventReceived(PowerManagerClient::LidState state,
88                         const base::TimeTicks& timestamp) override;
89 
90  private:
91   enum class PowerSource { kAc, kBattery };
92 
93   // Struct holding possible idle behaviors and the current behavior while
94   // charging/when on battery.
95   struct IdleBehaviorInfo {
96     IdleBehaviorInfo();
97     IdleBehaviorInfo(const std::set<IdleBehavior>& possible_behaviors,
98                      const IdleBehavior& current_behavior,
99                      const bool is_managed);
100 
101     IdleBehaviorInfo(const IdleBehaviorInfo& o);
102     ~IdleBehaviorInfo();
103 
104     bool operator==(const IdleBehaviorInfo& o) const {
105       return (possible_behaviors == o.possible_behaviors &&
106               current_behavior == o.current_behavior &&
107               is_managed == o.is_managed);
108     }
109 
110     // All possible idle behaviors.
111     std::set<IdleBehavior> possible_behaviors;
112     // Current idle behavior.
113     IdleBehavior current_behavior = IdleBehavior::DISPLAY_OFF_SLEEP;
114     // Whether enterpise policy manages idle behavior.
115     bool is_managed = false;
116   };
117 
118   // Handler to request updating the power status.
119   void HandleUpdatePowerStatus(const base::ListValue* args);
120 
121   // Handler to change the power source.
122   void HandleSetPowerSource(const base::ListValue* args);
123 
124   // Handler to request the current power management settings. Just calls
125   // SendPowerManagementSettings().
126   void HandleRequestPowerManagementSettings(const base::ListValue* args);
127 
128   // Handlers to change the idle and lid-closed behaviors.
129   void HandleSetIdleBehavior(const base::ListValue* args);
130   void HandleSetLidClosedBehavior(const base::ListValue* args);
131 
132   // Updates the UI with the current battery status.
133   void SendBatteryStatus();
134 
135   // Updates the UI with a list of available dual-role power sources.
136   void SendPowerSources();
137 
138   // Updates the UI to display the current power management settings. If the
139   // settings didn't change since the previous call, nothing is sent unless
140   // |force| is true.
141   void SendPowerManagementSettings(bool force);
142 
143   // Callback used to receive switch states from PowerManagerClient.
144   void OnGotSwitchStates(
145       base::Optional<PowerManagerClient::SwitchStates> result);
146 
147   // Returns all possible idle behaviors (that a user can choose from) and
148   // current idle behavior based on enterprise policy and other factors when on
149   // |power_source|.
150   IdleBehaviorInfo GetAllowedIdleBehaviors(PowerSource power_source);
151 
152   // Returns true if the enterprise policy enforces any settings that can impact
153   // the idle behavior of the device when on |power_source|.
154   bool IsIdleManaged(PowerSource power_source);
155 
156   PrefService* const prefs_;
157 
158   // Used to watch power management prefs for changes so the UI can be notified.
159   std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
160 
161   ScopedObserver<PowerManagerClient, PowerManagerClient::Observer>
162       power_manager_client_observer_{this};
163 
164   // Last lid state received from powerd.
165   PowerManagerClient::LidState lid_state_ = PowerManagerClient::LidState::OPEN;
166 
167   // Last values sent by SendPowerManagementSettings(), cached here so
168   // SendPowerManagementSettings() can avoid spamming the UI after this class
169   // changes multiple prefs at once.
170   IdleBehaviorInfo last_ac_idle_info_;
171   IdleBehaviorInfo last_battery_idle_info_;
172   PowerPolicyController::Action last_lid_closed_behavior_ =
173       PowerPolicyController::ACTION_SUSPEND;
174   bool last_lid_closed_controlled_ = false;
175   bool last_has_lid_ = true;
176 
177   base::WeakPtrFactory<PowerHandler> weak_ptr_factory_{this};
178 
179   DISALLOW_COPY_AND_ASSIGN(PowerHandler);
180 };
181 
182 }  // namespace settings
183 }  // namespace chromeos
184 
185 #endif  // CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_DEVICE_POWER_HANDLER_H_
186