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. 4 5 #include "chrome/browser/chromeos/policy/login_screen_power_management_policy.h" 6 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" 15 16 namespace policy { 17 18 namespace { 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"; 30 31 const char kActionSuspend[] = "Suspend"; 32 const char kActionShutdown[] = "Shutdown"; 33 const char kActionDoNothing[] = "DoNothing"; 34 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>(); 42 43 if (!value->IsType(type)) { 44 if (errors) { 45 errors->AddError(key::kDeviceLoginScreenPowerManagement, 46 key, 47 IDS_POLICY_TYPE_ERROR, 48 ConfigurationPolicyHandler::ValueTypeToString(type)); 49 } 50 return scoped_ptr<base::Value>(); 51 } 52 53 return scoped_ptr<base::Value>(value->DeepCopy()); 54 } 55 56 scoped_ptr<base::Value> GetInteger(const base::DictionaryValue* dict, 57 const std::string& key, 58 int minimum, 59 PolicyErrorMap* errors) { 60 scoped_ptr<base::Value> value = 61 GetValue(dict, key, base::Value::TYPE_INTEGER, errors); 62 int integer; 63 if (!value || !value->GetAsInteger(&integer) || integer >= minimum) 64 return value.Pass(); 65 66 if (errors) { 67 errors->AddError(key::kDeviceLoginScreenPowerManagement, 68 key, 69 IDS_POLICY_OUT_OF_RANGE_ERROR, 70 base::IntToString(integer)); 71 } 72 return scoped_ptr<base::Value>(); 73 } 74 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); 80 std::string action; 81 if (!value || !value->GetAsString(&action)) 82 return value.Pass(); 83 84 if (action == kActionSuspend) { 85 return scoped_ptr<base::Value>(new base::FundamentalValue( 86 chromeos::PowerPolicyController::ACTION_SUSPEND)); 87 } 88 if (action == kActionShutdown) { 89 return scoped_ptr<base::Value>(new base::FundamentalValue( 90 chromeos::PowerPolicyController::ACTION_SHUT_DOWN)); 91 } 92 if (action == kActionDoNothing) { 93 return scoped_ptr<base::Value>(new base::FundamentalValue( 94 chromeos::PowerPolicyController::ACTION_DO_NOTHING)); 95 } 96 97 if (errors) { 98 errors->AddError(key::kDeviceLoginScreenPowerManagement, 99 key, 100 IDS_POLICY_OUT_OF_RANGE_ERROR, 101 action); 102 } 103 return scoped_ptr<base::Value>(); 104 } 105 106 } // namespace 107 108 LoginScreenPowerManagementPolicy::LoginScreenPowerManagementPolicy() { 109 } 110 111 LoginScreenPowerManagementPolicy::~LoginScreenPowerManagementPolicy() { 112 } 113 114 bool LoginScreenPowerManagementPolicy::Init(const std::string& json, 115 PolicyErrorMap* errors) { 116 std::string error; 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)) { 121 if (errors) { 122 errors->AddError(key::kDeviceLoginScreenPowerManagement, 123 IDS_POLICY_JSON_PARSE_ERROR, 124 error); 125 } 126 // Errors in JSON decoding are fatal. 127 return false; 128 } 129 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); 143 144 // Validation errors for individual power policies are non-fatal as other 145 // power policies that pass validation will still be applied. 146 return true; 147 } 148 149 const base::Value* 150 LoginScreenPowerManagementPolicy::GetScreenDimDelayAC() const { 151 return screen_dim_delay_ac_.get(); 152 } 153 154 const base::Value* 155 LoginScreenPowerManagementPolicy::GetScreenOffDelayAC() const { 156 return screen_off_delay_ac_.get(); 157 } 158 159 const base::Value* LoginScreenPowerManagementPolicy::GetIdleDelayAC() const { 160 return idle_delay_ac_.get(); 161 } 162 163 const base::Value* 164 LoginScreenPowerManagementPolicy::GetScreenDimDelayBattery() const { 165 return screen_dim_delay_battery_.get(); 166 } 167 168 const base::Value* 169 LoginScreenPowerManagementPolicy::GetScreenOffDelayBattery() const { 170 return screen_off_delay_battery_.get(); 171 } 172 173 const base::Value* 174 LoginScreenPowerManagementPolicy::GetIdleDelayBattery() const { 175 return idle_delay_battery_.get(); 176 } 177 178 const base::Value* LoginScreenPowerManagementPolicy::GetIdleActionAC() const { 179 return idle_action_ac_.get(); 180 } 181 182 const base::Value* 183 LoginScreenPowerManagementPolicy::GetIdleActionBattery() const { 184 return idle_action_battery_.get(); 185 } 186 187 const base::Value* LoginScreenPowerManagementPolicy::GetLidCloseAction() const { 188 return lid_close_action_.get(); 189 } 190 191 const base::Value* LoginScreenPowerManagementPolicy:: 192 GetUserActivityScreenDimDelayScale() const { 193 return user_activity_screen_dim_delay_scale_.get(); 194 } 195 196 } // namespace policy 197