OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chromeos/dbus/power_policy_controller.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/values.h" | |
9 #include "chromeos/dbus/dbus_thread_manager.h" | |
10 #include "chromeos/dbus/power_manager_client.h" | |
11 | |
12 namespace chromeos { | |
13 | |
14 namespace { | |
15 | |
16 // If |pref|, a PrefServiceBase::Preference containing an integer, has been | |
17 // explicitly set to 0 or a positive value, assigns it to |proto_field|, a | |
18 // int32 field in |proto|, a google::protobuf::MessageLite*. | |
19 #define SET_DELAY_FROM_PREF(pref, proto_field, proto) \ | |
20 if (int value = GetIntPrefValue(pref) >= 0) \ | |
21 (proto)->set_##proto_field(value); | |
22 | |
23 // Similar to SET_DELAY_FROM_PREF() but sets a | |
24 // power_manager::PowerManagementPolicy_Action field instead. | |
25 #define SET_ACTION_FROM_PREF(pref, proto_field, proto) \ | |
26 if (int value = GetIntPrefValue(pref) >= 0) \ | |
27 (proto)->set_##proto_field( \ | |
28 static_cast<power_manager::PowerManagementPolicy_Action>(value)); | |
29 | |
30 // If |pref|, a PrefServiceBase::Preference containing a bool, has been | |
31 // set, assigns it to |proto_field|, a bool field in |proto|, a | |
32 // google::protobuf::MessageLite*. | |
33 #define SET_BOOL_FROM_PREF(pref, proto_field, proto) \ | |
34 if (!pref.IsDefaultValue()) { \ | |
35 bool value = false; \ | |
36 if (pref.GetValue()->GetAsBoolean(&value)) \ | |
37 (proto)->set_##proto_field(value); \ | |
38 else \ | |
39 LOG(DFATAL) << pref.name() << " pref has non-boolean value"; \ | |
40 } | |
41 | |
42 // Returns a zero or positive integer value from |pref|. Returns -1 if the | |
43 // pref is unset and logs an error if it's set to a negative value. | |
44 int GetIntPrefValue(const PrefServiceBase::Preference& pref) { | |
45 if (pref.IsDefaultValue()) | |
Mattias Nissler (ping if slow)
2013/02/05 13:16:18
Hm, why not allow default values to be passed down
Daniel Erat
2013/02/05 14:05:58
The default values aren't expressive enough to rep
| |
46 return -1; | |
47 | |
48 int value = -1; | |
49 if (!pref.GetValue()->GetAsInteger(&value)) { | |
50 LOG(DFATAL) << pref.name() << " pref has non-integer value"; | |
51 return -1; | |
52 } | |
53 | |
54 if (value < 0) | |
55 LOG(WARNING) << pref.name() << " pref has negative value"; | |
56 return value; | |
57 } | |
58 | |
59 // Returns the power_manager::PowerManagementPolicy_Action value | |
60 // corresponding to |action|. | |
61 power_manager::PowerManagementPolicy_Action GetProtoAction( | |
62 PowerPolicyController::Action action) { | |
63 switch (action) { | |
64 case PowerPolicyController::ACTION_SUSPEND: | |
65 return power_manager::PowerManagementPolicy_Action_SUSPEND; | |
66 case PowerPolicyController::ACTION_STOP_SESSION: | |
67 return power_manager::PowerManagementPolicy_Action_STOP_SESSION; | |
68 case PowerPolicyController::ACTION_SHUT_DOWN: | |
69 return power_manager::PowerManagementPolicy_Action_SHUT_DOWN; | |
70 case PowerPolicyController::ACTION_DO_NOTHING: | |
71 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; | |
72 default: | |
73 NOTREACHED() << "Unhandled action " << action; | |
74 return power_manager::PowerManagementPolicy_Action_DO_NOTHING; | |
75 } | |
76 } | |
77 | |
78 } // namespace | |
79 | |
80 PowerPolicyController::PowerPolicyController(PowerManagerClient* client) | |
81 : client_(client) { | |
82 client_->SetPolicy(power_manager::PowerManagementPolicy()); | |
83 } | |
84 | |
85 PowerPolicyController::~PowerPolicyController() { | |
86 client_->SetPolicy(power_manager::PowerManagementPolicy()); | |
87 client_ = NULL; | |
88 } | |
89 | |
90 void PowerPolicyController::UpdatePolicyFromPrefs( | |
91 const PrefServiceBase::Preference& ac_screen_dim_delay_ms_pref, | |
92 const PrefServiceBase::Preference& ac_screen_off_delay_ms_pref, | |
93 const PrefServiceBase::Preference& ac_screen_lock_delay_ms_pref, | |
94 const PrefServiceBase::Preference& ac_idle_delay_ms_pref, | |
95 const PrefServiceBase::Preference& battery_screen_dim_delay_ms_pref, | |
96 const PrefServiceBase::Preference& battery_screen_off_delay_ms_pref, | |
97 const PrefServiceBase::Preference& battery_screen_lock_delay_ms_pref, | |
98 const PrefServiceBase::Preference& battery_idle_delay_ms_pref, | |
99 const PrefServiceBase::Preference& idle_action_pref, | |
100 const PrefServiceBase::Preference& lid_closed_action_pref, | |
101 const PrefServiceBase::Preference& use_audio_activity_pref, | |
102 const PrefServiceBase::Preference& use_video_activity_pref, | |
103 const PrefServiceBase::Preference& presentation_idle_delay_factor_pref) { | |
104 prefs_policy_.Clear(); | |
105 | |
106 power_manager::PowerManagementPolicy::Delays* delays = | |
107 prefs_policy_.mutable_ac_delays(); | |
108 SET_DELAY_FROM_PREF(ac_screen_dim_delay_ms_pref, screen_dim_ms, delays); | |
109 SET_DELAY_FROM_PREF(ac_screen_off_delay_ms_pref, screen_off_ms, delays); | |
110 SET_DELAY_FROM_PREF(ac_screen_lock_delay_ms_pref, screen_lock_ms, delays); | |
111 SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays); | |
112 | |
113 delays = prefs_policy_.mutable_battery_delays(); | |
114 SET_DELAY_FROM_PREF(battery_screen_dim_delay_ms_pref, screen_dim_ms, delays); | |
115 SET_DELAY_FROM_PREF(battery_screen_off_delay_ms_pref, screen_off_ms, delays); | |
116 SET_DELAY_FROM_PREF( | |
117 battery_screen_lock_delay_ms_pref, screen_lock_ms, delays); | |
118 SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays); | |
119 | |
120 SET_ACTION_FROM_PREF(idle_action_pref, idle_action, &prefs_policy_); | |
121 SET_ACTION_FROM_PREF( | |
122 lid_closed_action_pref, lid_closed_action, &prefs_policy_); | |
123 | |
124 SET_BOOL_FROM_PREF( | |
125 use_audio_activity_pref, use_audio_activity, &prefs_policy_); | |
126 SET_BOOL_FROM_PREF( | |
127 use_video_activity_pref, use_video_activity, &prefs_policy_); | |
128 | |
129 if (!presentation_idle_delay_factor_pref.IsDefaultValue()) { | |
130 double value = 0.0; | |
131 if (presentation_idle_delay_factor_pref.GetValue()->GetAsDouble(&value)) { | |
132 prefs_policy_.set_presentation_idle_delay_factor(value); | |
133 } else { | |
134 LOG(DFATAL) << presentation_idle_delay_factor_pref.name() | |
135 << " pref has non-double value"; | |
136 } | |
137 } | |
138 | |
139 // TODO(derat): Incorporate other information into the policy that is | |
140 // sent, e.g. from content::PowerSaveBlocker. | |
141 client_->SetPolicy(prefs_policy_); | |
142 } | |
143 | |
144 } // namespace chromeos | |
OLD | NEW |