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 CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_
6 #define CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_
11 #include "base/macros.h"
12 #include "chromeos/chromeos_export.h"
13 #include "chromeos/dbus/power_manager/policy.pb.h"
14 #include "chromeos/dbus/power_manager_client.h"
18 // PowerPolicyController is responsible for sending Chrome's assorted power
19 // management preferences to the Chrome OS power manager.
20 class CHROMEOS_EXPORT PowerPolicyController
21 : public PowerManagerClient::Observer
{
23 // Sets the global instance. Must be called before any calls to Get().
24 static void Initialize(PowerManagerClient
* power_manager_client
);
26 // Returns true if the global instance has been initialized.
27 static bool IsInitialized();
29 // Destroys the global instance.
30 static void Shutdown();
32 // Returns the global instance. Initialize() must be called first.
33 static PowerPolicyController
* Get();
35 // Reasons why a wake lock may be added.
37 REASON_AUDIO_PLAYBACK
,
38 REASON_VIDEO_PLAYBACK
,
42 // Note: Do not change these values; they are used by preferences.
45 ACTION_STOP_SESSION
= 1,
47 ACTION_DO_NOTHING
= 3,
50 // Values of various power-management-related preferences.
54 int ac_screen_dim_delay_ms
;
55 int ac_screen_off_delay_ms
;
56 int ac_screen_lock_delay_ms
;
57 int ac_idle_warning_delay_ms
;
59 int battery_screen_dim_delay_ms
;
60 int battery_screen_off_delay_ms
;
61 int battery_screen_lock_delay_ms
;
62 int battery_idle_warning_delay_ms
;
63 int battery_idle_delay_ms
;
64 Action ac_idle_action
;
65 Action battery_idle_action
;
66 Action lid_closed_action
;
67 bool use_audio_activity
;
68 bool use_video_activity
;
69 double ac_brightness_percent
;
70 double battery_brightness_percent
;
71 bool allow_screen_wake_locks
;
72 bool enable_auto_screen_lock
;
73 double presentation_screen_dim_delay_factor
;
74 double user_activity_screen_dim_delay_factor
;
75 bool wait_for_initial_user_activity
;
76 bool force_nonzero_brightness_for_user_activity
;
79 // Returns a string describing |policy|. Useful for tests.
80 static std::string
GetPolicyDebugString(
81 const power_manager::PowerManagementPolicy
& policy
);
83 // Delay in milliseconds between the screen being turned off and the screen
84 // being locked. Used if the |enable_auto_screen_lock| pref is set but
85 // |*_screen_lock_delay_ms| are unset or set to higher values than what this
86 // constant would imply.
87 static const int kScreenLockAfterOffDelayMs
;
89 // String added to a PowerManagementPolicy |reason| field if the policy has
90 // been modified by preferences.
91 static const char kPrefsReason
[];
93 // Updates |prefs_policy_| with |values| and sends an updated policy.
94 void ApplyPrefs(const PrefValues
& values
);
96 // Registers a request to temporarily prevent the screen from getting dimmed
97 // or turned off or the system from suspending in response to user inactivity
98 // and sends an updated policy. |description| is a human-readable description
99 // of the reason the lock was created. Returns a unique ID that can be passed
100 // to RemoveWakeLock() later.
101 int AddScreenWakeLock(WakeLockReason reason
, const std::string
& description
);
102 int AddSystemWakeLock(WakeLockReason reason
, const std::string
& description
);
104 // Unregisters a request previously created via AddScreenWakeLock() or
105 // AddSystemWakeLock() and sends an updated policy.
106 void RemoveWakeLock(int id
);
108 // PowerManagerClient::Observer implementation:
109 void PowerManagerRestarted() override
;
112 explicit PowerPolicyController(PowerManagerClient
* client
);
113 ~PowerPolicyController() override
;
115 friend class PowerPrefsTest
;
117 // Details about a wake lock added via AddScreenWakeLock() or
118 // AddSystemWakeLock().
125 WakeLock(Type type
, WakeLockReason reason
, const std::string
& description
);
129 const WakeLockReason reason
;
130 const std::string description
;
133 using WakeLockMap
= std::map
<int, WakeLock
>;
135 // Helper method for AddScreenWakeLock() and AddSystemWakeLock().
136 int AddWakeLockInternal(WakeLock::Type type
,
137 WakeLockReason reason
,
138 const std::string
& description
);
140 // Sends a policy based on |prefs_policy_| to the power manager.
141 void SendCurrentPolicy();
143 PowerManagerClient
* client_
; // weak
145 // Policy derived from values passed to ApplyPrefs().
146 power_manager::PowerManagementPolicy prefs_policy_
;
148 // Was ApplyPrefs() called?
149 bool prefs_were_set_
;
151 // Maps from an ID representing a request to prevent the screen from
152 // getting dimmed or turned off or to prevent the system from suspending
153 // to details about the request.
154 WakeLockMap wake_locks_
;
156 // Should TYPE_SCREEN entries in |wake_locks_| be honored? If false, screen
157 // wake locks are just treated as TYPE_SYSTEM instead.
158 bool honor_screen_wake_locks_
;
160 // Next ID to be used by AddScreenWakeLock() or AddSystemWakeLock().
161 int next_wake_lock_id_
;
163 DISALLOW_COPY_AND_ASSIGN(PowerPolicyController
);
166 } // namespace chromeos
168 #endif // CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_