OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_DEVICE_POWER_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_DEVICE_POWER_HANDLER_H_ |
6 #define CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_DEVICE_POWER_HANDLER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_DEVICE_POWER_HANDLER_H_ |
7 | 7 |
| 8 #include <memory> |
| 9 |
8 #include "ash/system/power/power_status.h" | 10 #include "ash/system/power/power_status.h" |
9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" |
10 #include "base/scoped_observer.h" | 13 #include "base/scoped_observer.h" |
11 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
12 #include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h" | 15 #include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h" |
| 16 #include "chromeos/dbus/power_manager_client.h" |
| 17 #include "chromeos/dbus/power_policy_controller.h" |
| 18 |
| 19 class PrefChangeRegistrar; |
| 20 class PrefService; |
13 | 21 |
14 namespace base { | 22 namespace base { |
15 class ListValue; | 23 class ListValue; |
| 24 class TimeTicks; |
16 } | 25 } |
17 | 26 |
18 namespace chromeos { | 27 namespace chromeos { |
19 namespace settings { | 28 namespace settings { |
20 | 29 |
21 // Chrome OS battery status and power settings handler. | 30 // Chrome OS battery status and power settings handler. |
22 class PowerHandler : public ::settings::SettingsPageUIHandler, | 31 class PowerHandler : public ::settings::SettingsPageUIHandler, |
23 public ash::PowerStatus::Observer { | 32 public ash::PowerStatus::Observer, |
| 33 public PowerManagerClient::Observer { |
24 public: | 34 public: |
25 PowerHandler(); | 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_STAY_AWAKE = 1, |
| 41 DISPLAY_ON = 2, |
| 42 OTHER = 3, |
| 43 }; |
| 44 |
| 45 // WebUI message name and dictionary keys. Shared with tests. |
| 46 static const char kPowerManagementSettingsChangedName[]; |
| 47 static const char kIdleBehaviorKey[]; |
| 48 static const char kIdleControlledKey[]; |
| 49 static const char kLidClosedBehaviorKey[]; |
| 50 static const char kLidClosedControlledKey[]; |
| 51 static const char kHasLidKey[]; |
| 52 |
| 53 // Class used by tests to interact with PowerHandler internals. |
| 54 class TestAPI { |
| 55 public: |
| 56 explicit TestAPI(PowerHandler* handler); |
| 57 ~TestAPI(); |
| 58 |
| 59 void RequestPowerManagementSettings(); |
| 60 void SetIdleBehavior(IdleBehavior behavior); |
| 61 void SetLidClosedBehavior(PowerPolicyController::Action behavior); |
| 62 |
| 63 private: |
| 64 PowerHandler* handler_; // Not owned. |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(TestAPI); |
| 67 }; |
| 68 |
| 69 explicit PowerHandler(PrefService* prefs); |
26 ~PowerHandler() override; | 70 ~PowerHandler() override; |
27 | 71 |
28 // SettingsPageUIHandler implementation. | 72 // SettingsPageUIHandler implementation. |
29 void RegisterMessages() override; | 73 void RegisterMessages() override; |
30 void OnJavascriptAllowed() override; | 74 void OnJavascriptAllowed() override; |
31 void OnJavascriptDisallowed() override; | 75 void OnJavascriptDisallowed() override; |
32 | 76 |
33 // ash::PowerStatus::Observer implementation. | 77 // ash::PowerStatus::Observer implementation. |
34 void OnPowerStatusChanged() override; | 78 void OnPowerStatusChanged() override; |
35 | 79 |
| 80 // PowerManagerClient implementation. |
| 81 void PowerManagerRestarted() override; |
| 82 void LidEventReceived(PowerManagerClient::LidState state, |
| 83 const base::TimeTicks& timestamp) override; |
| 84 |
36 private: | 85 private: |
37 // Handler to request updating the power status. | 86 // Handler to request updating the power status. |
38 void HandleUpdatePowerStatus(const base::ListValue* args); | 87 void HandleUpdatePowerStatus(const base::ListValue* args); |
39 | 88 |
40 // Handler to change the power source. | 89 // Handler to change the power source. |
41 void HandleSetPowerSource(const base::ListValue* args); | 90 void HandleSetPowerSource(const base::ListValue* args); |
42 | 91 |
| 92 // Handler to request the current power management settings. Just calls |
| 93 // SendPowerManagementSettings(). |
| 94 void HandleRequestPowerManagementSettings(const base::ListValue* args); |
| 95 |
| 96 // Handlers to change the idle and lid-closed behaviors. |
| 97 void HandleSetIdleBehavior(const base::ListValue* args); |
| 98 void HandleSetLidClosedBehavior(const base::ListValue* args); |
| 99 |
43 // Updates the UI with the current battery status. | 100 // Updates the UI with the current battery status. |
44 void SendBatteryStatus(); | 101 void SendBatteryStatus(); |
45 | 102 |
46 // Updates the UI with a list of available dual-role power sources. | 103 // Updates the UI with a list of available dual-role power sources. |
47 void SendPowerSources(); | 104 void SendPowerSources(); |
48 | 105 |
49 ash::PowerStatus* power_status_; | 106 // Updates the UI to display the current power management settings. If the |
| 107 // settings didn't change since the previous call, nothing is sent unless |
| 108 // |force| is true. |
| 109 void SendPowerManagementSettings(bool force); |
50 | 110 |
51 ScopedObserver<ash::PowerStatus, PowerHandler> power_observer_; | 111 // Callback used to receive switch states from PowerManagerClient. |
| 112 void OnGotSwitchStates(PowerManagerClient::LidState lid_state, |
| 113 PowerManagerClient::TabletMode tablet_mode); |
| 114 |
| 115 PrefService* prefs_; // Not owned. |
| 116 ash::PowerStatus* power_status_; // Not owned. |
| 117 |
| 118 // Used to watch power management prefs for changes so the UI can be notified. |
| 119 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; |
| 120 |
| 121 ScopedObserver<ash::PowerStatus, PowerHandler> power_status_observer_; |
| 122 ScopedObserver<PowerManagerClient, PowerHandler> |
| 123 power_manager_client_observer_; |
| 124 |
| 125 // Last lid state received from powerd. |
| 126 PowerManagerClient::LidState lid_state_ = PowerManagerClient::LidState::OPEN; |
| 127 |
| 128 // Last values sent by SendPowerManagementSettings(), cached here so |
| 129 // SendPowerManagementSettings() can avoid spamming the UI after this class |
| 130 // changes multiple prefs at once. |
| 131 IdleBehavior last_idle_behavior_ = IdleBehavior::DISPLAY_OFF_SLEEP; |
| 132 PowerPolicyController::Action last_lid_closed_behavior_ = |
| 133 PowerPolicyController::ACTION_SUSPEND; |
| 134 bool last_idle_controlled_ = false; |
| 135 bool last_lid_closed_controlled_ = false; |
| 136 bool last_has_lid_ = true; |
| 137 |
| 138 base::WeakPtrFactory<PowerHandler> weak_ptr_factory_; |
52 | 139 |
53 DISALLOW_COPY_AND_ASSIGN(PowerHandler); | 140 DISALLOW_COPY_AND_ASSIGN(PowerHandler); |
54 }; | 141 }; |
55 | 142 |
56 } // namespace settings | 143 } // namespace settings |
57 } // namespace chromeos | 144 } // namespace chromeos |
58 | 145 |
59 #endif // CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_DEVICE_POWER_HANDLER_H_ | 146 #endif // CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_DEVICE_POWER_HANDLER_H_ |
OLD | NEW |