Only fsync leveldb's directory when the manifest is being updated.
[chromium-blink-merge.git] / chromeos / dbus / power_policy_controller.cc
blobab28a289a288c1a4190e0f24841565fdfac6d79c
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 #include "chromeos/dbus/power_policy_controller.h"
7 #include "base/format_macros.h"
8 #include "base/logging.h"
9 #include "base/string_util.h"
10 #include "base/stringprintf.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
13 namespace chromeos {
15 namespace {
17 // Appends a description of |field|, a field within |delays|, a
18 // power_manager::PowerManagementPolicy::Delays object, to |str|, an
19 // std::string, if the field is set. |name| is a char* describing the
20 // field.
21 #define APPEND_DELAY(str, delays, field, name) \
22 { \
23 if (delays.has_##field()) \
24 str += base::StringPrintf(name "=%" PRId64 " ", delays.field()); \
27 // Appends descriptions of all of the set delays in |delays|, a
28 // power_manager::PowerManagementPolicy::Delays object, to |str|, an
29 // std::string. |prefix| should be a char* containing either "ac" or
30 // "battery".
31 #define APPEND_DELAYS(str, delays, prefix) \
32 { \
33 APPEND_DELAY(str, delays, screen_dim_ms, prefix "_screen_dim_ms"); \
34 APPEND_DELAY(str, delays, screen_off_ms, prefix "_screen_off_ms"); \
35 APPEND_DELAY(str, delays, screen_lock_ms, prefix "_screen_lock_ms"); \
36 APPEND_DELAY(str, delays, idle_warning_ms, prefix "_idle_warning_ms"); \
37 APPEND_DELAY(str, delays, idle_ms, prefix "_idle_ms"); \
40 // Returns the power_manager::PowerManagementPolicy_Action value
41 // corresponding to |action|.
42 power_manager::PowerManagementPolicy_Action GetProtoAction(
43 PowerPolicyController::Action action) {
44 switch (action) {
45 case PowerPolicyController::ACTION_SUSPEND:
46 return power_manager::PowerManagementPolicy_Action_SUSPEND;
47 case PowerPolicyController::ACTION_STOP_SESSION:
48 return power_manager::PowerManagementPolicy_Action_STOP_SESSION;
49 case PowerPolicyController::ACTION_SHUT_DOWN:
50 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN;
51 case PowerPolicyController::ACTION_DO_NOTHING:
52 return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
53 default:
54 NOTREACHED() << "Unhandled action " << action;
55 return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
59 } // namespace
61 const int PowerPolicyController::kScreenLockAfterOffDelayMs = 10000; // 10 sec.
63 // -1 is interpreted as "unset" by powerd, resulting in powerd's default
64 // delays being used instead. There are no similarly-interpreted values
65 // for the other fields, unfortunately (but the constructor-assigned values
66 // will only reach powerd if Chrome messes up and forgets to override them
67 // with the pref-assigned values).
68 PowerPolicyController::PrefValues::PrefValues()
69 : ac_screen_dim_delay_ms(-1),
70 ac_screen_off_delay_ms(-1),
71 ac_screen_lock_delay_ms(-1),
72 ac_idle_warning_delay_ms(-1),
73 ac_idle_delay_ms(-1),
74 battery_screen_dim_delay_ms(-1),
75 battery_screen_off_delay_ms(-1),
76 battery_screen_lock_delay_ms(-1),
77 battery_idle_warning_delay_ms(-1),
78 battery_idle_delay_ms(-1),
79 idle_action(ACTION_SUSPEND),
80 lid_closed_action(ACTION_SUSPEND),
81 use_audio_activity(true),
82 use_video_activity(true),
83 allow_screen_wake_locks(true),
84 enable_screen_lock(false),
85 presentation_idle_delay_factor(2.0),
86 user_activity_screen_dim_delay_factor(1.0) {}
88 // static
89 std::string PowerPolicyController::GetPolicyDebugString(
90 const power_manager::PowerManagementPolicy& policy) {
91 std::string str;
92 if (policy.has_ac_delays())
93 APPEND_DELAYS(str, policy.ac_delays(), "ac");
94 if (policy.has_battery_delays())
95 APPEND_DELAYS(str, policy.battery_delays(), "battery");
96 if (policy.has_idle_action())
97 str += base::StringPrintf("idle=%d ", policy.idle_action());
98 if (policy.has_lid_closed_action())
99 str += base::StringPrintf("lid_closed=%d ", policy.lid_closed_action());
100 if (policy.has_use_audio_activity())
101 str += base::StringPrintf("use_audio=%d ", policy.use_audio_activity());
102 if (policy.has_use_video_activity())
103 str += base::StringPrintf("use_video=%d ", policy.use_audio_activity());
104 if (policy.has_presentation_idle_delay_factor()) {
105 str += base::StringPrintf("presentation_idle_delay_factor=%f ",
106 policy.presentation_idle_delay_factor());
108 if (policy.has_user_activity_screen_dim_delay_factor()) {
109 str += base::StringPrintf("user_activity_screen_dim_delay_factor=%f ",
110 policy.user_activity_screen_dim_delay_factor());
112 if (policy.has_reason())
113 str += base::StringPrintf("reason=\"%s\" ", policy.reason().c_str());
114 TrimWhitespace(str, TRIM_TRAILING, &str);
115 return str;
118 PowerPolicyController::PowerPolicyController(DBusThreadManager* manager,
119 PowerManagerClient* client)
120 : manager_(manager),
121 client_(client),
122 prefs_were_set_(false),
123 honor_screen_wake_locks_(true),
124 next_wake_lock_id_(1) {
125 manager_->AddObserver(this);
126 client_->AddObserver(this);
127 SendCurrentPolicy();
130 PowerPolicyController::~PowerPolicyController() {
131 // The power manager's policy is reset before this point, in
132 // OnDBusThreadManagerDestroying(). At the time that
133 // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy
134 // to the power manager is already gone.
135 client_->RemoveObserver(this);
136 client_ = NULL;
137 manager_->RemoveObserver(this);
138 manager_ = NULL;
141 void PowerPolicyController::ApplyPrefs(const PrefValues& values) {
142 prefs_policy_.Clear();
144 power_manager::PowerManagementPolicy::Delays* delays =
145 prefs_policy_.mutable_ac_delays();
146 delays->set_screen_dim_ms(values.ac_screen_dim_delay_ms);
147 delays->set_screen_off_ms(values.ac_screen_off_delay_ms);
148 delays->set_screen_lock_ms(values.ac_screen_lock_delay_ms);
149 delays->set_idle_warning_ms(values.ac_idle_warning_delay_ms);
150 delays->set_idle_ms(values.ac_idle_delay_ms);
152 // If screen-locking is enabled, ensure that the screen is locked soon
153 // after it's turned off due to user inactivity.
154 int64 lock_ms = delays->screen_off_ms() + kScreenLockAfterOffDelayMs;
155 if (values.enable_screen_lock && delays->screen_off_ms() > 0 &&
156 (delays->screen_lock_ms() <= 0 || lock_ms < delays->screen_lock_ms()) &&
157 lock_ms < delays->idle_ms())
158 delays->set_screen_lock_ms(lock_ms);
160 delays = prefs_policy_.mutable_battery_delays();
161 delays->set_screen_dim_ms(values.battery_screen_dim_delay_ms);
162 delays->set_screen_off_ms(values.battery_screen_off_delay_ms);
163 delays->set_screen_lock_ms(values.battery_screen_lock_delay_ms);
164 delays->set_idle_warning_ms(values.battery_idle_warning_delay_ms);
165 delays->set_idle_ms(values.battery_idle_delay_ms);
167 lock_ms = delays->screen_off_ms() + kScreenLockAfterOffDelayMs;
168 if (values.enable_screen_lock && delays->screen_off_ms() > 0 &&
169 (delays->screen_lock_ms() <= 0 || lock_ms < delays->screen_lock_ms()) &&
170 lock_ms < delays->idle_ms())
171 delays->set_screen_lock_ms(lock_ms);
173 prefs_policy_.set_idle_action(GetProtoAction(values.idle_action));
174 prefs_policy_.set_lid_closed_action(GetProtoAction(values.lid_closed_action));
175 prefs_policy_.set_use_audio_activity(values.use_audio_activity);
176 prefs_policy_.set_use_video_activity(values.use_video_activity);
177 prefs_policy_.set_presentation_idle_delay_factor(
178 values.presentation_idle_delay_factor);
179 prefs_policy_.set_user_activity_screen_dim_delay_factor(
180 values.user_activity_screen_dim_delay_factor);
182 honor_screen_wake_locks_ = values.allow_screen_wake_locks;
184 prefs_were_set_ = true;
185 SendCurrentPolicy();
188 int PowerPolicyController::AddScreenWakeLock(const std::string& reason) {
189 int id = next_wake_lock_id_++;
190 screen_wake_locks_[id] = reason;
191 SendCurrentPolicy();
192 return id;
195 int PowerPolicyController::AddSystemWakeLock(const std::string& reason) {
196 int id = next_wake_lock_id_++;
197 system_wake_locks_[id] = reason;
198 SendCurrentPolicy();
199 return id;
202 void PowerPolicyController::RemoveWakeLock(int id) {
203 if (!screen_wake_locks_.erase(id) && !system_wake_locks_.erase(id))
204 LOG(WARNING) << "Ignoring request to remove nonexistent wake lock " << id;
205 else
206 SendCurrentPolicy();
209 void PowerPolicyController::OnDBusThreadManagerDestroying(
210 DBusThreadManager* manager) {
211 DCHECK_EQ(manager, manager_);
212 SendEmptyPolicy();
215 void PowerPolicyController::PowerManagerRestarted() {
216 SendCurrentPolicy();
219 void PowerPolicyController::SendCurrentPolicy() {
220 std::string reason;
222 power_manager::PowerManagementPolicy policy = prefs_policy_;
223 if (prefs_were_set_)
224 reason = "Prefs";
226 if (honor_screen_wake_locks_ && !screen_wake_locks_.empty()) {
227 policy.mutable_ac_delays()->set_screen_dim_ms(0);
228 policy.mutable_ac_delays()->set_screen_off_ms(0);
229 policy.mutable_ac_delays()->set_screen_lock_ms(0);
230 policy.mutable_battery_delays()->set_screen_dim_ms(0);
231 policy.mutable_battery_delays()->set_screen_off_ms(0);
232 policy.mutable_battery_delays()->set_screen_lock_ms(0);
235 if ((!screen_wake_locks_.empty() || !system_wake_locks_.empty()) &&
236 (!policy.has_idle_action() || policy.idle_action() ==
237 power_manager::PowerManagementPolicy_Action_SUSPEND)) {
238 policy.set_idle_action(
239 power_manager::PowerManagementPolicy_Action_DO_NOTHING);
242 for (WakeLockMap::const_iterator it = screen_wake_locks_.begin();
243 it != screen_wake_locks_.end(); ++it) {
244 reason += (reason.empty() ? "" : ", ") + it->second;
246 for (WakeLockMap::const_iterator it = system_wake_locks_.begin();
247 it != system_wake_locks_.end(); ++it) {
248 reason += (reason.empty() ? "" : ", ") + it->second;
251 if (!reason.empty())
252 policy.set_reason(reason);
253 client_->SetPolicy(policy);
256 void PowerPolicyController::SendEmptyPolicy() {
257 client_->SetPolicy(power_manager::PowerManagementPolicy());
260 } // namespace chromeos