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 // Note: Do not change these values; they are used by preferences.
38 ACTION_STOP_SESSION
= 1,
40 ACTION_DO_NOTHING
= 3,
43 // Values of various power-management-related preferences.
47 int ac_screen_dim_delay_ms
;
48 int ac_screen_off_delay_ms
;
49 int ac_screen_lock_delay_ms
;
50 int ac_idle_warning_delay_ms
;
52 int battery_screen_dim_delay_ms
;
53 int battery_screen_off_delay_ms
;
54 int battery_screen_lock_delay_ms
;
55 int battery_idle_warning_delay_ms
;
56 int battery_idle_delay_ms
;
57 Action ac_idle_action
;
58 Action battery_idle_action
;
59 Action lid_closed_action
;
60 bool use_audio_activity
;
61 bool use_video_activity
;
62 double ac_brightness_percent
;
63 double battery_brightness_percent
;
64 bool allow_screen_wake_locks
;
65 bool enable_auto_screen_lock
;
66 double presentation_screen_dim_delay_factor
;
67 double user_activity_screen_dim_delay_factor
;
68 bool wait_for_initial_user_activity
;
71 // Returns a string describing |policy|. Useful for tests.
72 static std::string
GetPolicyDebugString(
73 const power_manager::PowerManagementPolicy
& policy
);
75 // Delay in milliseconds between the screen being turned off and the screen
76 // being locked. Used if the |enable_auto_screen_lock| pref is set but
77 // |*_screen_lock_delay_ms| are unset or set to higher values than what this
78 // constant would imply.
79 static const int kScreenLockAfterOffDelayMs
;
81 // Updates |prefs_policy_| with |values| and sends an updated policy.
82 void ApplyPrefs(const PrefValues
& values
);
84 // Registers a request to temporarily prevent the screen from getting
85 // dimmed or turned off or the system from suspending in response to user
86 // inactivity and sends an updated policy. |reason| is a human-readable
87 // description of the reason the lock was created. Returns a unique ID
88 // that can be passed to RemoveWakeLock() later.
89 int AddScreenWakeLock(const std::string
& reason
);
90 int AddSystemWakeLock(const std::string
& reason
);
92 // Unregisters a request previously created via AddScreenWakeLock() or
93 // AddSystemWakeLock() and sends an updated policy.
94 void RemoveWakeLock(int id
);
96 // PowerManagerClient::Observer implementation:
97 void PowerManagerRestarted() override
;
100 explicit PowerPolicyController(PowerManagerClient
* client
);
101 ~PowerPolicyController() override
;
103 friend class PowerPrefsTest
;
105 typedef std::map
<int, std::string
> WakeLockMap
;
107 // Sends a policy based on |prefs_policy_| to the power manager.
108 void SendCurrentPolicy();
110 PowerManagerClient
* client_
; // weak
112 // Policy derived from values passed to ApplyPrefs().
113 power_manager::PowerManagementPolicy prefs_policy_
;
115 // Was ApplyPrefs() called?
116 bool prefs_were_set_
;
118 // Maps from an ID representing a request to prevent the screen from
119 // getting dimmed or turned off or to prevent the system from suspending
120 // to the reason for the request.
121 WakeLockMap screen_wake_locks_
;
122 WakeLockMap system_wake_locks_
;
124 // Should entries in |screen_wake_locks_| be honored? If false, screen
125 // wake locks are just treated as system wake locks instead.
126 bool honor_screen_wake_locks_
;
128 // Next ID to be used by AddScreenWakeLock() or AddSystemWakeLock().
129 int next_wake_lock_id_
;
131 DISALLOW_COPY_AND_ASSIGN(PowerPolicyController
);
134 } // namespace chromeos
136 #endif // CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_