1 // Copyright 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 "chrome/browser/chromeos/policy/login_screen_power_management_policy.h"
7 #include "base/json/json_reader.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/values.h"
10 #include "chromeos/dbus/power_policy_controller.h"
11 #include "components/policy/core/browser/configuration_policy_handler.h"
12 #include "components/policy/core/browser/policy_error_map.h"
13 #include "grit/component_strings.h"
14 #include "policy/policy_constants.h"
19 const char kScreenDimDelayAC
[] = "AC.Delays.ScreenDim";
20 const char kScreenOffDelayAC
[] = "AC.Delays.ScreenOff";
21 const char kIdleDelayAC
[] = "AC.Delays.Idle";
22 const char kScreenDimDelayBattery
[] = "Battery.Delays.ScreenDim";
23 const char kScreenOffDelayBattery
[] = "Battery.Delays.ScreenOff";
24 const char kIdleDelayBattery
[] = "Battery.Delays.Idle";
25 const char kIdleActionAC
[] = "AC.IdleAction";
26 const char kIdleActionBattery
[] = "Battery.IdleAction";
27 const char kLidCloseAction
[] = "LidCloseAction";
28 const char kUserActivityScreenDimDelayScale
[] =
29 "UserActivityScreenDimDelayScale";
31 const char kActionSuspend
[] = "Suspend";
32 const char kActionShutdown
[] = "Shutdown";
33 const char kActionDoNothing
[] = "DoNothing";
35 scoped_ptr
<base::Value
> GetValue(const base::DictionaryValue
* dict
,
36 const std::string
& key
,
37 base::Value::Type type
,
38 PolicyErrorMap
* errors
) {
39 const base::Value
* value
;
40 if (!dict
->Get(key
, &value
))
41 return scoped_ptr
<base::Value
>();
43 if (!value
->IsType(type
)) {
45 errors
->AddError(key::kDeviceLoginScreenPowerManagement
,
47 IDS_POLICY_TYPE_ERROR
,
48 ConfigurationPolicyHandler::ValueTypeToString(type
));
50 return scoped_ptr
<base::Value
>();
53 return scoped_ptr
<base::Value
>(value
->DeepCopy());
56 scoped_ptr
<base::Value
> GetInteger(const base::DictionaryValue
* dict
,
57 const std::string
& key
,
59 PolicyErrorMap
* errors
) {
60 scoped_ptr
<base::Value
> value
=
61 GetValue(dict
, key
, base::Value::TYPE_INTEGER
, errors
);
63 if (!value
|| !value
->GetAsInteger(&integer
) || integer
>= minimum
)
67 errors
->AddError(key::kDeviceLoginScreenPowerManagement
,
69 IDS_POLICY_OUT_OF_RANGE_ERROR
,
70 base::IntToString(integer
));
72 return scoped_ptr
<base::Value
>();
75 scoped_ptr
<base::Value
> GetAction(const base::DictionaryValue
* dict
,
76 const std::string
& key
,
77 PolicyErrorMap
* errors
) {
78 scoped_ptr
<base::Value
> value
=
79 GetValue(dict
, key
, base::Value::TYPE_STRING
, errors
);
81 if (!value
|| !value
->GetAsString(&action
))
84 if (action
== kActionSuspend
) {
85 return scoped_ptr
<base::Value
>(new base::FundamentalValue(
86 chromeos::PowerPolicyController::ACTION_SUSPEND
));
88 if (action
== kActionShutdown
) {
89 return scoped_ptr
<base::Value
>(new base::FundamentalValue(
90 chromeos::PowerPolicyController::ACTION_SHUT_DOWN
));
92 if (action
== kActionDoNothing
) {
93 return scoped_ptr
<base::Value
>(new base::FundamentalValue(
94 chromeos::PowerPolicyController::ACTION_DO_NOTHING
));
98 errors
->AddError(key::kDeviceLoginScreenPowerManagement
,
100 IDS_POLICY_OUT_OF_RANGE_ERROR
,
103 return scoped_ptr
<base::Value
>();
108 LoginScreenPowerManagementPolicy::LoginScreenPowerManagementPolicy() {
111 LoginScreenPowerManagementPolicy::~LoginScreenPowerManagementPolicy() {
114 bool LoginScreenPowerManagementPolicy::Init(const std::string
& json
,
115 PolicyErrorMap
* errors
) {
117 scoped_ptr
<base::Value
> root(base::JSONReader::ReadAndReturnError(
118 json
, base::JSON_ALLOW_TRAILING_COMMAS
, NULL
, &error
));
119 base::DictionaryValue
* dict
= NULL
;
120 if (!root
|| !root
->GetAsDictionary(&dict
)) {
122 errors
->AddError(key::kDeviceLoginScreenPowerManagement
,
123 IDS_POLICY_JSON_PARSE_ERROR
,
126 // Errors in JSON decoding are fatal.
130 screen_dim_delay_ac_
= GetInteger(dict
, kScreenDimDelayAC
, 0, errors
);
131 screen_off_delay_ac_
= GetInteger(dict
, kScreenOffDelayAC
, 0, errors
);
132 idle_delay_ac_
= GetInteger(dict
, kIdleDelayAC
, 0, errors
);
133 screen_dim_delay_battery_
=
134 GetInteger(dict
, kScreenDimDelayBattery
, 0, errors
);
135 screen_off_delay_battery_
=
136 GetInteger(dict
, kScreenOffDelayBattery
, 0, errors
);
137 idle_delay_battery_
= GetInteger(dict
, kIdleDelayBattery
, 0, errors
);
138 idle_action_ac_
= GetAction(dict
, kIdleActionAC
, errors
);
139 idle_action_battery_
= GetAction(dict
, kIdleActionBattery
, errors
);
140 lid_close_action_
= GetAction(dict
, kLidCloseAction
, errors
);
141 user_activity_screen_dim_delay_scale_
=
142 GetInteger(dict
, kUserActivityScreenDimDelayScale
, 100, errors
);
144 // Validation errors for individual power policies are non-fatal as other
145 // power policies that pass validation will still be applied.
150 LoginScreenPowerManagementPolicy::GetScreenDimDelayAC() const {
151 return screen_dim_delay_ac_
.get();
155 LoginScreenPowerManagementPolicy::GetScreenOffDelayAC() const {
156 return screen_off_delay_ac_
.get();
159 const base::Value
* LoginScreenPowerManagementPolicy::GetIdleDelayAC() const {
160 return idle_delay_ac_
.get();
164 LoginScreenPowerManagementPolicy::GetScreenDimDelayBattery() const {
165 return screen_dim_delay_battery_
.get();
169 LoginScreenPowerManagementPolicy::GetScreenOffDelayBattery() const {
170 return screen_off_delay_battery_
.get();
174 LoginScreenPowerManagementPolicy::GetIdleDelayBattery() const {
175 return idle_delay_battery_
.get();
178 const base::Value
* LoginScreenPowerManagementPolicy::GetIdleActionAC() const {
179 return idle_action_ac_
.get();
183 LoginScreenPowerManagementPolicy::GetIdleActionBattery() const {
184 return idle_action_battery_
.get();
187 const base::Value
* LoginScreenPowerManagementPolicy::GetLidCloseAction() const {
188 return lid_close_action_
.get();
191 const base::Value
* LoginScreenPowerManagementPolicy::
192 GetUserActivityScreenDimDelayScale() const {
193 return user_activity_screen_dim_delay_scale_
.get();
196 } // namespace policy