Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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/policy/configuration_policy_handler.h" | |
| 6 | |
| 7 #include <sstream> | |
| 8 | |
| 9 #include "base/file_path.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "chrome/browser/download/download_util.h" | |
| 13 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
| 14 #include "chrome/browser/policy/policy_path_parser.h" | |
| 15 #include "chrome/browser/prefs/proxy_config_dictionary.h" | |
| 16 #include "chrome/browser/prefs/proxy_prefs.h" | |
| 17 #include "chrome/browser/search_engines/search_terms_data.h" | |
| 18 #include "chrome/browser/search_engines/template_url.h" | |
| 19 #include "chrome/common/pref_names.h" | |
| 20 | |
| 21 namespace policy { | |
| 22 | |
| 23 // CheckPolicyValueType | |
| 24 CheckPolicyValueType::CheckPolicyValueType( | |
| 25 ConfigurationPolicyType policy, | |
| 26 Value::Type value_type, | |
| 27 const char* pref_path) : policy_type_(policy), | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
please break before the :
I don't think the style
simo
2011/09/22 11:43:26
Done.
| |
| 28 value_type_(value_type), | |
| 29 pref_path_(pref_path) { | |
| 30 } | |
| 31 | |
| 32 CheckPolicyValueType::~CheckPolicyValueType() { | |
| 33 } | |
| 34 | |
| 35 ConfigurationPolicyType CheckPolicyValueType::policy_type() const { | |
| 36 return policy_type_; | |
| 37 } | |
| 38 | |
| 39 const char* CheckPolicyValueType::pref_path() const { | |
| 40 return pref_path_; | |
| 41 } | |
| 42 | |
| 43 bool CheckPolicyValueType::CheckPolicySettings(PolicyMap* policies, | |
| 44 PolicyErrorMap* errors) { | |
| 45 const Value* value = policies->Get(policy_type_); | |
| 46 if (value && value_type_ != value->GetType()) { | |
| 47 std::string message = "Expected " + ValueTypeToString(value_type_); | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
Ah, so what about i18n? These errors show up in th
simo
2011/09/22 11:43:26
Done.
| |
| 48 errors->AddError(policy_type_, Value::CreateStringValue(message)); | |
| 49 LOG(WARNING) << "Mismatch in provided and expected policy value for policy " | |
| 50 << pref_path_ << ". expected = " << value_type_ | |
| 51 << ", actual = " << value->GetType(); | |
| 52 return false; | |
| 53 } | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 std::string CheckPolicyValueType::ValueTypeToString(Value::Type type) { | |
| 58 static const char* strings[] = { "null", "boolean", | |
| 59 "integer", "double", | |
| 60 "string", "binary", | |
| 61 "dictionary", "list" }; | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
each on one line please.
simo
2011/09/22 11:43:26
Done.
| |
| 62 DCHECK(static_cast<size_t>(type) < arraysize(strings)); | |
| 63 return std::string(strings[type]); | |
| 64 } | |
| 65 | |
| 66 // SimplePolicyHandler | |
| 67 SimplePolicyHandler::SimplePolicyHandler( | |
| 68 ConfigurationPolicyType policy, | |
| 69 Value::Type value_type, | |
| 70 const char* pref_path) | |
| 71 : CheckPolicyValueType(policy, value_type, pref_path) { | |
| 72 } | |
| 73 | |
| 74 SimplePolicyHandler::~SimplePolicyHandler() { | |
| 75 } | |
| 76 | |
| 77 void SimplePolicyHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 78 PrefValueMap* prefs) { | |
| 79 PolicyErrorMap errors; | |
| 80 const Value* value = policies->Get(policy_type()); | |
| 81 if (value && CheckPolicySettings(policies, &errors)) | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
This three-line pattern repeats a lot. Have you th
simo
2011/09/22 11:43:26
Done.
| |
| 82 prefs->SetValue(pref_path(), value->DeepCopy()); | |
| 83 } | |
| 84 | |
| 85 // SyncPolicyHandler | |
| 86 SyncPolicyHandler::SyncPolicyHandler() | |
| 87 : CheckPolicyValueType(kPolicySyncDisabled, | |
| 88 Value::TYPE_BOOLEAN, | |
| 89 prefs::kSyncManaged) { | |
| 90 } | |
| 91 | |
| 92 SyncPolicyHandler::~SyncPolicyHandler() { | |
| 93 } | |
| 94 | |
| 95 void SyncPolicyHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 96 PrefValueMap* prefs) { | |
| 97 PolicyErrorMap errors; | |
| 98 const Value* value = policies->Get(policy_type()); | |
| 99 if (value && CheckPolicySettings(policies, &errors)) { | |
| 100 bool disable_sync; | |
| 101 value->GetAsBoolean(&disable_sync); | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
you should check the return value of GetAsBoolean
simo
2011/09/22 11:43:26
Is it enough if I do this in a DCHECK? The call to
| |
| 102 if (disable_sync) | |
| 103 prefs->SetValue(prefs::kSyncManaged, value->DeepCopy()); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 // AutofillPolicyHandler | |
| 108 AutofillPolicyHandler::AutofillPolicyHandler() | |
| 109 : CheckPolicyValueType(kPolicyAutoFillEnabled, | |
| 110 Value::TYPE_BOOLEAN, | |
| 111 prefs::kAutofillEnabled) { | |
| 112 } | |
| 113 | |
| 114 AutofillPolicyHandler::~AutofillPolicyHandler() { | |
| 115 } | |
| 116 | |
| 117 void AutofillPolicyHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 118 PrefValueMap* prefs) { | |
| 119 PolicyErrorMap errors; | |
| 120 const Value* value = policies->Get(policy_type()); | |
| 121 if (value && CheckPolicySettings(policies, &errors)) { | |
| 122 bool auto_fill_enabled; | |
| 123 value->GetAsBoolean(&auto_fill_enabled); | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
check return value.
simo
2011/09/22 11:43:26
Done.
| |
| 124 if (!auto_fill_enabled) { | |
| 125 prefs->SetValue(prefs::kAutofillEnabled, | |
| 126 Value::CreateBooleanValue(false)); | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 // DownloadDirPolicyHandler | |
| 132 DownloadDirPolicyHandler::DownloadDirPolicyHandler() | |
| 133 : CheckPolicyValueType(kPolicyDownloadDirectory, | |
| 134 Value::TYPE_STRING, | |
| 135 prefs::kDownloadDefaultDirectory) { | |
| 136 } | |
| 137 | |
| 138 DownloadDirPolicyHandler::~DownloadDirPolicyHandler() { | |
| 139 } | |
| 140 | |
| 141 bool DownloadDirPolicyHandler::CheckPolicySettings(PolicyMap* policies, | |
| 142 PolicyErrorMap* errors) { | |
| 143 // This policy is ignored on ChromeOS because the download path there is fixed | |
| 144 // and can not be configured by the user. | |
| 145 #if !defined(OS_CHROMEOS) | |
| 146 return CheckPolicyValueType::CheckPolicySettings(policies, errors); | |
| 147 #endif // defined(OS_CHROMEOS) | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 void DownloadDirPolicyHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 152 PrefValueMap* prefs) { | |
| 153 #if !defined(OS_CHROMEOS) | |
| 154 PolicyErrorMap errors; | |
| 155 const Value* value = policies->Get(policy_type()); | |
| 156 if (value && CheckPolicySettings(policies, &errors)) { | |
| 157 FilePath::StringType string_value; | |
| 158 value->GetAsString(&string_value); | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
check return value.
simo
2011/09/22 11:43:26
Done.
| |
| 159 FilePath::StringType expanded_value = | |
| 160 policy::path_parser::ExpandPathVariables(string_value); | |
| 161 // Leaving the policy empty would revert to the default download location | |
| 162 // else we would point in an undefined location. We do this after the | |
| 163 // path expansion because it might lead to an empty string(e.g. for "\"\""). | |
| 164 if (expanded_value.empty()) | |
| 165 expanded_value = download_util::GetDefaultDownloadDirectory().value(); | |
| 166 prefs->SetValue(prefs::kDownloadDefaultDirectory, | |
| 167 Value::CreateStringValue(expanded_value)); | |
| 168 prefs->SetValue(prefs::kPromptForDownload, | |
| 169 Value::CreateBooleanValue(false)); | |
| 170 #endif // !defined(OS_CHROMEOS) | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
this preprocessor guard is misplaced, should move
simo
2011/09/22 11:43:26
Done.
| |
| 171 } | |
| 172 } | |
| 173 | |
| 174 // DiskCacheDirPolicyHandler | |
| 175 DiskCacheDirPolicyHandler::DiskCacheDirPolicyHandler() | |
| 176 : CheckPolicyValueType(kPolicyDiskCacheDir, | |
| 177 Value::TYPE_STRING, | |
| 178 prefs::kDiskCacheDir) { | |
| 179 } | |
| 180 | |
| 181 DiskCacheDirPolicyHandler::~DiskCacheDirPolicyHandler() { | |
| 182 } | |
| 183 | |
| 184 void DiskCacheDirPolicyHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 185 PrefValueMap* prefs) { | |
| 186 PolicyErrorMap errors; | |
| 187 const Value* value = policies->Get(policy_type()); | |
| 188 if (value && CheckPolicySettings(policies, &errors)) { | |
| 189 FilePath::StringType string_value; | |
| 190 value->GetAsString(&string_value); | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
check return value.
simo
2011/09/22 11:43:26
Done.
| |
| 191 FilePath::StringType expanded_value = | |
| 192 policy::path_parser::ExpandPathVariables(string_value); | |
| 193 prefs->SetValue(prefs::kDiskCacheDir, | |
| 194 Value::CreateStringValue(expanded_value)); | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 // FileSelectionDialogsHandler | |
| 199 FileSelectionDialogsHandler::FileSelectionDialogsHandler() | |
| 200 : CheckPolicyValueType(kPolicyAllowFileSelectionDialogs, | |
| 201 Value::TYPE_BOOLEAN, | |
| 202 prefs::kAllowFileSelectionDialogs) { | |
| 203 } | |
| 204 | |
| 205 FileSelectionDialogsHandler::~FileSelectionDialogsHandler() { | |
| 206 } | |
| 207 | |
| 208 void FileSelectionDialogsHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 209 PrefValueMap* prefs) { | |
| 210 PolicyErrorMap errors; | |
| 211 const Value* value = policies->Get(policy_type()); | |
| 212 if (value && CheckPolicySettings(policies, &errors)) { | |
| 213 prefs->SetValue(prefs::kAllowFileSelectionDialogs, value->DeepCopy()); | |
| 214 // If file-selection dialogs are not allowed we forbid the user to be | |
| 215 // prompted for the download location, since this would end up in an Infobar | |
| 216 // explaining that file-selection dialogs are forbidden anyways. | |
| 217 bool allow_file_selection_dialogs = true; | |
| 218 value->GetAsBoolean(&allow_file_selection_dialogs); | |
| 219 if (!allow_file_selection_dialogs) { | |
| 220 prefs->SetValue(prefs::kPromptForDownload, | |
| 221 Value::CreateBooleanValue(false)); | |
| 222 } | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 // BookmarksPolicyHandler | |
| 227 BookmarksPolicyHandler::BookmarksPolicyHandler() | |
| 228 : CheckPolicyValueType(kPolicyBookmarkBarEnabled, | |
| 229 Value::TYPE_BOOLEAN, | |
| 230 prefs::kEnableBookmarkBar) { | |
| 231 } | |
| 232 | |
| 233 BookmarksPolicyHandler::~BookmarksPolicyHandler() { | |
| 234 } | |
| 235 | |
| 236 void BookmarksPolicyHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 237 PrefValueMap* prefs) { | |
| 238 PolicyErrorMap errors; | |
| 239 const Value* value = policies->Get(policy_type()); | |
| 240 if (value && CheckPolicySettings(policies, &errors)) { | |
| 241 prefs->SetValue(prefs::kEnableBookmarkBar, value->DeepCopy()); | |
| 242 // kShowBookmarkBar is not managed directly by a policy, but when | |
| 243 // kEnableBookmarkBar is managed, kShowBookmarkBar should be false so that | |
| 244 // the bookmarks bar either is completely disabled or only shows on the NTP. | |
| 245 // This also disables the checkbox for this preference in the prefs UI. | |
| 246 prefs->SetValue(prefs::kShowBookmarkBar, Value::CreateBooleanValue(false)); | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 // IncognitoModePolicyHandler | |
| 251 IncognitoModePolicyHandler::IncognitoModePolicyHandler() { | |
| 252 } | |
| 253 | |
| 254 IncognitoModePolicyHandler::~IncognitoModePolicyHandler() { | |
| 255 } | |
| 256 | |
| 257 bool IncognitoModePolicyHandler::CheckPolicySettings(PolicyMap* policies, | |
| 258 PolicyErrorMap* errors) { | |
| 259 int int_value = IncognitoModePrefs::ENABLED; | |
| 260 std::string message; | |
| 261 const Value* availability = policies->Get(kPolicyIncognitoModeAvailability); | |
| 262 const Value* deprecated_enabled = policies->Get(kPolicyIncognitoEnabled); | |
| 263 if (availability) { | |
| 264 if (availability->GetAsInteger(&int_value)) { | |
| 265 if (!IncognitoModePrefs::IntToAvailability(int_value, | |
| 266 &availability_enum_value_)) { | |
| 267 message = "IncognitoModeAvailability policy value is out of " | |
| 268 "range " + int_value; | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
i18n
simo
2011/09/22 11:43:26
Done.
| |
| 269 errors->AddError(kPolicyIncognitoModeAvailability, | |
| 270 Value::CreateStringValue(message)); | |
| 271 LOG(WARNING) << message; | |
| 272 return false; | |
| 273 } | |
| 274 } else { | |
| 275 message = "IncognitoModeAvailability policy value could not be parsed"; | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
i18n
simo
2011/09/22 11:43:26
Done.
| |
| 276 errors->AddError(kPolicyIncognitoModeAvailability, | |
| 277 Value::CreateStringValue(message)); | |
| 278 LOG(WARNING) << message; | |
| 279 return false; | |
| 280 } | |
| 281 } else { | |
| 282 // If kPolicyIncognitoModeAvailability is not specified, check the obsolete | |
| 283 // kPolicyIncognitoEnabled. | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
could move the deprecated_enabled lookup from line
simo
2011/09/22 11:43:26
Done.
| |
| 284 if (deprecated_enabled && | |
| 285 !deprecated_enabled->IsType(Value::TYPE_BOOLEAN)) { | |
| 286 message = "IncognitoEnabled policy value could not be parsed"; | |
| 287 errors->AddError(kPolicyIncognitoEnabled, | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
i18n
simo
2011/09/22 11:43:26
Done.
| |
| 288 Value::CreateStringValue(message)); | |
| 289 LOG(WARNING) << message; | |
| 290 return false; | |
| 291 } | |
| 292 } | |
| 293 return true; | |
| 294 } | |
| 295 | |
| 296 void IncognitoModePolicyHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 297 PrefValueMap* prefs) { | |
| 298 PolicyErrorMap errors; | |
| 299 const Value* availability = policies->Get(kPolicyIncognitoModeAvailability); | |
| 300 const Value* deprecated_enabled = policies->Get(kPolicyIncognitoEnabled); | |
| 301 if (CheckPolicySettings(policies, &errors)) { | |
| 302 if (availability) { | |
| 303 prefs->SetValue(prefs::kIncognitoModeAvailability, | |
| 304 Value::CreateIntegerValue(availability_enum_value_)); | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
Ah, keeping state in the handler prevents us from
simo
2011/09/22 11:43:26
Ah yes, true. I think adding an method isn't reall
| |
| 305 } else if (deprecated_enabled) { | |
| 306 bool enabled = true; | |
| 307 deprecated_enabled->GetAsBoolean(&enabled); | |
| 308 prefs->SetInteger(prefs::kIncognitoModeAvailability, | |
| 309 enabled ? IncognitoModePrefs::ENABLED : | |
| 310 IncognitoModePrefs::DISABLED); | |
| 311 } | |
| 312 } | |
| 313 } | |
| 314 | |
| 315 namespace { | |
| 316 | |
| 317 // Implementation of SearchTermsData just for validation. | |
| 318 class SearchTermsDataForValidation : public SearchTermsData { | |
| 319 public: | |
| 320 SearchTermsDataForValidation() {} | |
| 321 | |
| 322 // Implementation of SearchTermsData. | |
| 323 virtual std::string GoogleBaseURLValue() const { | |
| 324 return "http://www.google.com/"; | |
| 325 } | |
| 326 virtual std::string GetApplicationLocale() const { | |
| 327 return "en"; | |
| 328 } | |
| 329 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) | |
| 330 virtual string16 GetRlzParameterValue() const { | |
| 331 return string16(); | |
| 332 } | |
| 333 #endif | |
| 334 private: | |
| 335 DISALLOW_COPY_AND_ASSIGN(SearchTermsDataForValidation); | |
| 336 }; | |
| 337 | |
| 338 } // namespace | |
| 339 | |
| 340 // DefaultSearchEncodingsPolicyHandler | |
| 341 DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler() | |
| 342 : CheckPolicyValueType(kPolicyDefaultSearchProviderEncodings, | |
| 343 Value::TYPE_LIST, | |
| 344 prefs::kDefaultSearchProviderEncodings) { | |
| 345 } | |
| 346 | |
| 347 DefaultSearchEncodingsPolicyHandler::~DefaultSearchEncodingsPolicyHandler() { | |
| 348 } | |
| 349 | |
| 350 void DefaultSearchEncodingsPolicyHandler::ApplyPolicySettings( | |
| 351 PolicyMap* policies, PrefValueMap* prefs) { | |
| 352 PolicyErrorMap errors; | |
| 353 const Value* value = policies->Get(policy_type()); | |
| 354 if (value && CheckPolicySettings(policies, &errors)) { | |
| 355 const ListValue* list; | |
| 356 value->GetAsList(&list); | |
| 357 ListValue::const_iterator iter(list->begin()); | |
| 358 ListValue::const_iterator end(list->end()); | |
| 359 std::string encodings; | |
| 360 for (; iter != end; ++iter) { | |
| 361 std::string s; | |
| 362 if ((*iter)->GetAsString(&s)) { | |
| 363 if (!encodings.empty()) | |
| 364 encodings.push_back(';'); | |
| 365 encodings.append(s); | |
| 366 } else { | |
| 367 NOTREACHED(); | |
| 368 } | |
| 369 } | |
| 370 prefs->SetValue(prefs::kDefaultSearchProviderEncodings, | |
| 371 Value::CreateStringValue(encodings)); | |
| 372 } | |
| 373 } | |
| 374 | |
| 375 // DefaultSearchPolicyHandler | |
| 376 DefaultSearchPolicyHandler::DefaultSearchPolicyHandler() { | |
| 377 handlers_.push_back(new SimplePolicyHandler( | |
| 378 kPolicyDefaultSearchProviderEnabled, | |
| 379 Value::TYPE_BOOLEAN, | |
| 380 prefs::kDefaultSearchProviderEnabled)); | |
| 381 handlers_.push_back(new SimplePolicyHandler( | |
| 382 kPolicyDefaultSearchProviderName, | |
| 383 Value::TYPE_STRING, | |
| 384 prefs::kDefaultSearchProviderName)); | |
| 385 handlers_.push_back(new SimplePolicyHandler( | |
| 386 kPolicyDefaultSearchProviderKeyword, | |
| 387 Value::TYPE_STRING, | |
| 388 prefs::kDefaultSearchProviderKeyword)); | |
| 389 handlers_.push_back(new SimplePolicyHandler( | |
| 390 kPolicyDefaultSearchProviderSearchURL, | |
| 391 Value::TYPE_STRING, | |
| 392 prefs::kDefaultSearchProviderSearchURL)); | |
| 393 handlers_.push_back(new SimplePolicyHandler( | |
| 394 kPolicyDefaultSearchProviderSuggestURL, | |
| 395 Value::TYPE_STRING, | |
| 396 prefs::kDefaultSearchProviderSuggestURL)); | |
| 397 handlers_.push_back(new SimplePolicyHandler( | |
| 398 kPolicyDefaultSearchProviderInstantURL, | |
| 399 Value::TYPE_STRING, | |
| 400 prefs::kDefaultSearchProviderInstantURL)); | |
| 401 handlers_.push_back(new SimplePolicyHandler( | |
| 402 kPolicyDefaultSearchProviderIconURL, | |
| 403 Value::TYPE_STRING, | |
| 404 prefs::kDefaultSearchProviderIconURL)); | |
| 405 handlers_.push_back(new SimplePolicyHandler( | |
| 406 kPolicyDefaultSearchProviderIconURL, | |
| 407 Value::TYPE_STRING, | |
| 408 prefs::kDefaultSearchProviderIconURL)); | |
| 409 handlers_.push_back(new DefaultSearchEncodingsPolicyHandler()); | |
| 410 } | |
| 411 | |
| 412 DefaultSearchPolicyHandler::~DefaultSearchPolicyHandler() { | |
| 413 STLDeleteContainerPointers(handlers_.begin(), handlers_.end()); | |
| 414 handlers_.clear(); | |
| 415 } | |
| 416 | |
| 417 bool DefaultSearchPolicyHandler::CheckPolicySettings(PolicyMap* policies, | |
| 418 PolicyErrorMap* errors) { | |
| 419 if (!CheckIndividualPolicies(policies, errors)) | |
| 420 return false; | |
| 421 | |
| 422 if (DefaultSearchProviderIsDisabled(policies)) { | |
| 423 std::string message = "This policy will be ignored because default search " | |
| 424 "is disabled by policy."; | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
i18n
simo
2011/09/22 11:43:26
Done.
| |
| 425 errors->AddError(kPolicyDefaultSearchProviderName, | |
| 426 Value::CreateStringValue(message)); | |
| 427 errors->AddError(kPolicyDefaultSearchProviderSearchURL, | |
| 428 Value::CreateStringValue(message)); | |
| 429 errors->AddError(kPolicyDefaultSearchProviderSuggestURL, | |
| 430 Value::CreateStringValue(message)); | |
| 431 errors->AddError(kPolicyDefaultSearchProviderIconURL, | |
| 432 Value::CreateStringValue(message)); | |
| 433 errors->AddError(kPolicyDefaultSearchProviderEncodings, | |
| 434 Value::CreateStringValue(message)); | |
| 435 errors->AddError(kPolicyDefaultSearchProviderKeyword, | |
| 436 Value::CreateStringValue(message)); | |
| 437 errors->AddError(kPolicyDefaultSearchProviderInstantURL, | |
| 438 Value::CreateStringValue(message)); | |
| 439 return false; | |
| 440 } | |
| 441 | |
| 442 const Value* search_url = | |
| 443 policies->Get(kPolicyDefaultSearchProviderSearchURL); | |
| 444 if (!search_url) { | |
| 445 std::string message = "Search URL must be specified."; | |
| 446 errors->AddError(kPolicyDefaultSearchProviderSearchURL, | |
| 447 Value::CreateStringValue(message)); | |
| 448 return false; | |
| 449 } | |
| 450 if (DefaultSearchURLIsPresentAndValid(policies)) { | |
| 451 std::string message = "Invalid search URL."; | |
| 452 errors->AddError(kPolicyDefaultSearchProviderSearchURL, | |
| 453 Value::CreateStringValue(message)); | |
| 454 return false; | |
| 455 } | |
| 456 return true; | |
| 457 } | |
| 458 | |
| 459 bool DefaultSearchPolicyHandler::CheckIndividualPolicies( | |
| 460 PolicyMap* policies, PolicyErrorMap* errors) { | |
| 461 HandlerList::const_iterator handler = handlers_.begin(); | |
| 462 for ( ; handler != handlers_.end(); ++handler) { | |
| 463 if (!(*handler)->CheckPolicySettings(policies, errors)) | |
| 464 return false; | |
| 465 } | |
| 466 return true; | |
| 467 } | |
| 468 | |
| 469 bool DefaultSearchPolicyHandler::DefaultSearchProviderIsDisabled( | |
| 470 PolicyMap* policies) { | |
| 471 const Value* provider_enabled = | |
| 472 policies->Get(kPolicyDefaultSearchProviderEnabled); | |
| 473 bool enabled = true; | |
| 474 return provider_enabled && | |
| 475 provider_enabled->GetAsBoolean(&enabled) && | |
| 476 !enabled; | |
| 477 } | |
| 478 | |
| 479 bool DefaultSearchPolicyHandler::DefaultSearchURLIsPresentAndValid( | |
| 480 PolicyMap* policies) { | |
| 481 const Value* search_url = | |
| 482 policies->Get(kPolicyDefaultSearchProviderSearchURL); | |
| 483 std::string search_url_string; | |
| 484 if (search_url) { | |
| 485 std::string search_url_string; | |
| 486 search_url->GetAsString(&search_url_string); | |
| 487 SearchTermsDataForValidation search_terms_data; | |
| 488 const TemplateURLRef search_url_ref(search_url_string, 0, 0); | |
| 489 // It must support replacement (which implies it is valid). | |
| 490 return search_url_ref.SupportsReplacementUsingTermsData(search_terms_data); | |
| 491 } | |
| 492 | |
| 493 return false; | |
| 494 } | |
| 495 | |
| 496 void DefaultSearchPolicyHandler::EnsureStringPrefExists(PrefValueMap* prefs, | |
| 497 const std::string& path) { | |
| 498 std::string value; | |
| 499 if (!prefs->GetString(path, &value)) | |
| 500 prefs->SetString(path, value); | |
| 501 } | |
| 502 | |
| 503 void DefaultSearchPolicyHandler::ClearDefaultSearchPreferences( | |
| 504 PrefValueMap* prefs) { | |
| 505 prefs->RemoveValue(prefs::kDefaultSearchProviderEnabled); | |
| 506 prefs->RemoveValue(prefs::kDefaultSearchProviderName); | |
| 507 prefs->RemoveValue(prefs::kDefaultSearchProviderKeyword); | |
| 508 prefs->RemoveValue(prefs::kDefaultSearchProviderSearchURL); | |
| 509 prefs->RemoveValue(prefs::kDefaultSearchProviderSuggestURL); | |
| 510 prefs->RemoveValue(prefs::kDefaultSearchProviderInstantURL); | |
| 511 prefs->RemoveValue(prefs::kDefaultSearchProviderIconURL); | |
| 512 prefs->RemoveValue(prefs::kDefaultSearchProviderEncodings); | |
| 513 } | |
| 514 | |
| 515 void DefaultSearchPolicyHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 516 PrefValueMap* prefs) { | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
Hm, should we run CheckPolicySettings() here?
simo
2011/09/22 11:43:26
Hm well, the CheckPolicySettings() for each of the
| |
| 517 if (DefaultSearchProviderIsDisabled(policies)) { | |
| 518 // If default search is disabled, we ignore the other fields. | |
| 519 prefs->SetString(prefs::kDefaultSearchProviderName, std::string()); | |
| 520 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, std::string()); | |
| 521 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, std::string()); | |
| 522 prefs->SetString(prefs::kDefaultSearchProviderIconURL, std::string()); | |
| 523 prefs->SetString(prefs::kDefaultSearchProviderEncodings, std::string()); | |
| 524 prefs->SetString(prefs::kDefaultSearchProviderKeyword, std::string()); | |
| 525 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, std::string()); | |
| 526 return; | |
| 527 } | |
| 528 | |
| 529 const Value* search_url = | |
| 530 policies->Get(kPolicyDefaultSearchProviderSearchURL); | |
| 531 // The search URL is required. | |
| 532 if (!search_url) { | |
| 533 return; | |
| 534 } | |
| 535 | |
| 536 // Apply all default search policies. | |
| 537 PolicyErrorMap errors; | |
| 538 HandlerList::const_iterator handler = handlers_.begin(); | |
| 539 for ( ; handler != handlers_.end(); ++handler) { | |
| 540 (*handler)->ApplyPolicySettings(policies, prefs); | |
| 541 } | |
| 542 | |
| 543 // The other entries are optional. Just make sure that they are all | |
| 544 // specified via policy, so that we don't use regular prefs. | |
| 545 if (DefaultSearchURLIsPresentAndValid(policies)) { | |
| 546 EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderSuggestURL); | |
| 547 EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderIconURL); | |
| 548 EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderEncodings); | |
| 549 EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderKeyword); | |
| 550 EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderInstantURL); | |
| 551 | |
| 552 // For the name, default to the host if not specified. | |
| 553 std::string name; | |
| 554 if (!prefs->GetString(prefs::kDefaultSearchProviderName, &name) || | |
| 555 name.empty()) { | |
| 556 const Value* search_url = | |
| 557 policies->Get(kPolicyDefaultSearchProviderSearchURL); | |
| 558 std::string search_url_string; | |
| 559 search_url->GetAsString(&search_url_string); | |
| 560 prefs->SetString(prefs::kDefaultSearchProviderName, | |
| 561 GURL(search_url_string).host()); | |
| 562 } | |
| 563 | |
| 564 // And clear the IDs since these are not specified via policy. | |
| 565 prefs->SetString(prefs::kDefaultSearchProviderID, std::string()); | |
| 566 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, | |
| 567 std::string()); | |
| 568 return; | |
| 569 } | |
| 570 | |
| 571 // Required entries are not there. Remove any related entries. | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
It might be nicer to just update a private PrefVal
simo
2011/09/22 11:43:26
Done.
| |
| 572 ClearDefaultSearchPreferences(prefs); | |
| 573 } | |
| 574 | |
| 575 // ProxyPolicyHandler | |
| 576 ProxyPolicyHandler::ProxyPolicyHandler() { | |
| 577 } | |
| 578 | |
| 579 ProxyPolicyHandler::~ProxyPolicyHandler() { | |
| 580 } | |
| 581 | |
| 582 bool ProxyPolicyHandler::CheckPolicySettings(PolicyMap* policies, | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
wow > 200 lines function! Can you please break it
simo
2011/09/22 11:43:26
Done.
| |
| 583 PolicyErrorMap* errors) { | |
| 584 const Value* mode = policies->Get(kPolicyProxyMode); | |
| 585 const Value* server = policies->Get(kPolicyProxyServer); | |
| 586 const Value* server_mode = policies->Get(kPolicyProxyServerMode); | |
| 587 const Value* pac_url = policies->Get(kPolicyProxyPacUrl); | |
| 588 const Value* bypass_list = policies->Get(kPolicyProxyBypassList); | |
| 589 std::string message; | |
| 590 if ((server || pac_url || bypass_list) && !(mode || server_mode)) { | |
| 591 message = "A centrally-administered policy defines proxy setting details " | |
| 592 "without setting a proxy mode."; | |
| 593 errors->AddError(kPolicyProxyMode, Value::CreateStringValue(message)); | |
| 594 LOG(WARNING) << message; | |
|
Mattias Nissler (ping if slow)
2011/09/20 13:12:25
It seems like we want to log the errors produced b
simo
2011/09/22 11:43:26
Done.
| |
| 595 return false; | |
| 596 } | |
| 597 | |
| 598 // If there's a server mode, convert it into a mode. | |
| 599 bool ignoring_proxy_mode = mode && server_mode; | |
| 600 std::string mode_value; | |
| 601 if (mode) { | |
| 602 if (server_mode) { | |
| 603 message = "Both ProxyMode and ProxyServerMode policies defined, ignoring " | |
| 604 "ProxyMode."; | |
| 605 errors->AddError(kPolicyProxyMode, Value::CreateStringValue(message)); | |
| 606 LOG(WARNING) << message; | |
| 607 } | |
| 608 std::string mode_value; | |
| 609 if (!mode->GetAsString(&mode_value)) { | |
| 610 message = "Invalid ProxyMode value."; | |
| 611 errors->AddError(kPolicyProxyMode, Value::CreateStringValue(message)); | |
| 612 LOG(WARNING) << message; | |
| 613 return false; | |
| 614 } | |
| 615 | |
| 616 ProxyPrefs::ProxyMode mode; | |
| 617 if (!ProxyPrefs::StringToProxyMode(mode_value, &mode)) { | |
| 618 message = "ProxyMode value isn't recognized"; | |
| 619 errors->AddError(kPolicyProxyMode, Value::CreateStringValue(message)); | |
| 620 LOG(WARNING) << "A centrally-administered policy specifies a value for " | |
| 621 "the ProxyMode policy that isn't recognized."; | |
| 622 return false; | |
| 623 } | |
| 624 | |
| 625 if (mode == ProxyPrefs::MODE_PAC_SCRIPT && !pac_url) { | |
| 626 message = "Missing PAC script URL."; | |
| 627 errors->AddError(kPolicyProxyPacUrl, Value::CreateStringValue(message)); | |
| 628 LOG(WARNING) << "A centrally-administered policy specifies to use a PAC " | |
| 629 "script, but doesn't supply the PAC script URL."; | |
| 630 return false; | |
| 631 } else if (mode == ProxyPrefs::MODE_FIXED_SERVERS && !server) { | |
| 632 message = "Missing server address."; | |
| 633 errors->AddError(kPolicyProxyServer, Value::CreateStringValue(message)); | |
| 634 LOG(WARNING) << "A centrally-administered policy specifies to use a " | |
| 635 "fixed server, but doesn't supply the server address."; | |
| 636 return false; | |
| 637 } | |
| 638 } else if (server_mode) { | |
| 639 int server_mode_value; | |
| 640 if (!server_mode->GetAsInteger(&server_mode_value)) { | |
| 641 message = "Invalid ProxyServerMode value."; | |
| 642 errors->AddError(kPolicyProxyServerMode, | |
| 643 Value::CreateStringValue(message)); | |
| 644 LOG(WARNING) << message; | |
| 645 return false; | |
| 646 } | |
| 647 | |
| 648 switch (server_mode_value) { | |
| 649 case kPolicyNoProxyServerMode: | |
| 650 mode_value = ProxyPrefs::kDirectProxyModeName; | |
| 651 break; | |
| 652 case kPolicyAutoDetectProxyServerMode: | |
| 653 mode_value = ProxyPrefs::kAutoDetectProxyModeName; | |
| 654 break; | |
| 655 case kPolicyManuallyConfiguredProxyServerMode: | |
| 656 if (server && pac_url) { | |
| 657 message = "Both fixed proxy servers and a .pac url are specified."; | |
| 658 errors->AddError( | |
| 659 kPolicyProxyServer, Value::CreateStringValue(message)); | |
| 660 errors->AddError( | |
| 661 kPolicyProxyPacUrl, Value::CreateStringValue(message)); | |
| 662 LOG(WARNING) << "A centrally-administered policy dictates that both " | |
| 663 "fixed proxy servers and a .pac url should be used " | |
| 664 "for proxy configuration."; | |
| 665 return false; | |
| 666 } | |
| 667 if (!server && !pac_url) { | |
| 668 message = "Neither fixed proxy servers nor a .pac url are specified."; | |
| 669 errors->AddError( | |
| 670 kPolicyProxyServer, Value::CreateStringValue(message)); | |
| 671 errors->AddError( | |
| 672 kPolicyProxyPacUrl, Value::CreateStringValue(message)); | |
| 673 LOG(WARNING) << "A centrally-administered policy dictates that " | |
| 674 "the proxy settings should use either fixed proxy " | |
| 675 "servers or a .pac url, but specifies neither."; | |
| 676 return false; | |
| 677 } | |
| 678 if (pac_url) | |
| 679 mode_value = ProxyPrefs::kPacScriptProxyModeName; | |
| 680 else | |
| 681 mode_value = ProxyPrefs::kFixedServersProxyModeName; | |
| 682 break; | |
| 683 case kPolicyUseSystemProxyServerMode: | |
| 684 mode_value = ProxyPrefs::kSystemProxyModeName; | |
| 685 break; | |
| 686 default: | |
| 687 // TODO(simo) map server_mode_value to string. | |
| 688 message = "Invalid proxy mode " + server_mode_value; | |
| 689 errors->AddError(kPolicyProxyServer, Value::CreateStringValue(message)); | |
| 690 LOG(WARNING) << message; | |
| 691 return false; | |
| 692 } | |
| 693 } | |
| 694 | |
| 695 // If neither ProxyMode nor ProxyServerMode are specified, mode_value will be | |
| 696 // empty and the proxy shouldn't be configured at all. | |
| 697 if (mode_value.empty()) | |
| 698 return true; | |
| 699 | |
| 700 if (mode_value == ProxyPrefs::kDirectProxyModeName) { | |
| 701 if (server || pac_url || bypass_list) { | |
| 702 message = "Use of a proxy is disabled but an explicit proxy " | |
| 703 "configuration is specified."; | |
| 704 if (server) | |
| 705 errors->AddError(kPolicyProxyServer, Value::CreateStringValue(message)); | |
| 706 if (bypass_list) | |
| 707 errors->AddError( | |
| 708 kPolicyProxyBypassList, Value::CreateStringValue(message)); | |
| 709 if (pac_url) | |
| 710 errors->AddError( | |
| 711 kPolicyProxyPacUrl, Value::CreateStringValue(message)); | |
| 712 | |
| 713 LOG(WARNING) << "A centrally-administered policy disables the use of a " | |
| 714 "proxy but also specifies an explicit proxy " | |
| 715 "configuration."; | |
| 716 return false; | |
| 717 } | |
| 718 } else if (mode_value == ProxyPrefs::kAutoDetectProxyModeName) { | |
| 719 if (server || bypass_list || pac_url) { | |
| 720 message = "Proxy is set to auto configured."; | |
| 721 if (server) | |
| 722 errors->AddError(kPolicyProxyServer, Value::CreateStringValue(message)); | |
| 723 if (bypass_list) | |
| 724 errors->AddError( | |
| 725 kPolicyProxyBypassList, Value::CreateStringValue(message)); | |
| 726 if (pac_url) | |
| 727 errors->AddError( | |
| 728 kPolicyProxyPacUrl, Value::CreateStringValue(message)); | |
| 729 | |
| 730 LOG(WARNING) << "A centrally-administered policy dictates that a proxy " | |
| 731 "shall be auto configured but specifies fixed proxy " | |
| 732 "servers, a by-pass list or a .pac script URL."; | |
| 733 return false; | |
| 734 } | |
| 735 } else if (mode_value == ProxyPrefs::kPacScriptProxyModeName) { | |
| 736 if (server|| bypass_list) { | |
| 737 message = "Proxy configuration is set to use a .pac script URL."; | |
| 738 if (server) | |
| 739 errors->AddError(kPolicyProxyServer, Value::CreateStringValue(message)); | |
| 740 if (bypass_list) | |
| 741 errors->AddError( | |
| 742 kPolicyProxyBypassList, Value::CreateStringValue(message)); | |
| 743 LOG(WARNING) << "A centrally-administered policy dictates that a .pac " | |
| 744 ".pac script URL should be used for proxy configuration " | |
| 745 "configuration but also specifies policies only for " | |
| 746 "fixed proxy servers."; | |
| 747 return false; | |
| 748 } | |
| 749 } else if (mode_value == ProxyPrefs::kFixedServersProxyModeName) { | |
| 750 if (pac_url) { | |
| 751 message = "Proxy is set to use fixed proxy servers"; | |
| 752 errors->AddError( | |
| 753 kPolicyProxyPacUrl, Value::CreateStringValue(message)); | |
| 754 LOG(WARNING) << "A centrally-administered policy dictates that fixed " | |
| 755 "fixed proxy servers should be used but also specifies " | |
| 756 "a .pac script URL."; | |
| 757 return false; | |
| 758 } | |
| 759 } else if (mode_value == ProxyPrefs::kSystemProxyModeName) { | |
| 760 if (server || pac_url || bypass_list) { | |
| 761 message = "System proxy settings are set to be used but an explicit proxy" | |
| 762 "configuration is also specified."; | |
| 763 if (server) | |
| 764 errors->AddError(kPolicyProxyServer, Value::CreateStringValue(message)); | |
| 765 if (bypass_list) | |
| 766 errors->AddError( | |
| 767 kPolicyProxyBypassList, Value::CreateStringValue(message)); | |
| 768 if (pac_url) | |
| 769 errors->AddError( | |
| 770 kPolicyProxyPacUrl, Value::CreateStringValue(message)); | |
| 771 | |
| 772 LOG(WARNING) << "A centrally-administered policy dictates that the " | |
| 773 "system proxy settings should be used but also " | |
| 774 "specifies an explicit proxy configuration."; | |
| 775 return false; | |
| 776 } | |
| 777 } else { | |
| 778 // TODO (simo) map to string. | |
| 779 message = "Invalid proxy mode " + mode_value; | |
| 780 if (ignoring_proxy_mode) | |
| 781 errors->AddError(kPolicyProxyServerMode, | |
| 782 Value::CreateStringValue(message)); | |
| 783 else | |
| 784 errors->AddError(kPolicyProxyMode, Value::CreateStringValue(message)); | |
| 785 | |
| 786 LOG(WARNING) << message; | |
| 787 return false; | |
| 788 } | |
| 789 return true; | |
| 790 } | |
| 791 | |
| 792 void ProxyPolicyHandler::ApplyPolicySettings(PolicyMap* policies, | |
| 793 PrefValueMap* prefs) { | |
| 794 PolicyErrorMap errors; | |
| 795 if (!CheckPolicySettings(policies, &errors)) | |
| 796 return; | |
| 797 | |
| 798 const Value* mode = policies->Get(kPolicyProxyMode); | |
| 799 const Value* server = policies->Get(kPolicyProxyServer); | |
| 800 const Value* server_mode = policies->Get(kPolicyProxyServerMode); | |
| 801 const Value* pac_url = policies->Get(kPolicyProxyPacUrl); | |
| 802 const Value* bypass_list = policies->Get(kPolicyProxyBypassList); | |
| 803 | |
| 804 ProxyPrefs::ProxyMode proxy_mode; | |
| 805 if (mode) { | |
| 806 std::string string_mode; | |
| 807 mode->GetAsString(&string_mode); | |
| 808 ProxyPrefs::StringToProxyMode(string_mode, &proxy_mode); | |
| 809 } else if (server_mode) { | |
| 810 int int_mode = 0; | |
| 811 server_mode->GetAsInteger(&int_mode); | |
| 812 switch (int_mode) { | |
| 813 case kPolicyNoProxyServerMode: | |
| 814 proxy_mode = ProxyPrefs::MODE_DIRECT; | |
| 815 break; | |
| 816 case kPolicyAutoDetectProxyServerMode: | |
| 817 proxy_mode = ProxyPrefs::MODE_AUTO_DETECT; | |
| 818 break; | |
| 819 case kPolicyManuallyConfiguredProxyServerMode: | |
| 820 proxy_mode = ProxyPrefs::MODE_FIXED_SERVERS; | |
| 821 if (pac_url) | |
| 822 proxy_mode = ProxyPrefs::MODE_PAC_SCRIPT; | |
| 823 break; | |
| 824 case kPolicyUseSystemProxyServerMode: | |
| 825 proxy_mode = ProxyPrefs::MODE_SYSTEM; | |
| 826 break; | |
| 827 default: | |
| 828 proxy_mode = ProxyPrefs::MODE_DIRECT; | |
| 829 NOTREACHED(); | |
| 830 } | |
| 831 } else { | |
| 832 return; | |
| 833 } | |
| 834 switch (proxy_mode) { | |
| 835 case ProxyPrefs::MODE_DIRECT: | |
| 836 prefs->SetValue(prefs::kProxy, ProxyConfigDictionary::CreateDirect()); | |
| 837 break; | |
| 838 case ProxyPrefs::MODE_AUTO_DETECT: | |
| 839 prefs->SetValue(prefs::kProxy, ProxyConfigDictionary::CreateAutoDetect()); | |
| 840 break; | |
| 841 case ProxyPrefs::MODE_PAC_SCRIPT: { | |
| 842 std::string pac_url_string; | |
| 843 pac_url->GetAsString(&pac_url_string); | |
| 844 prefs->SetValue(prefs::kProxy, | |
| 845 ProxyConfigDictionary::CreatePacScript(pac_url_string, false)); | |
| 846 break; | |
| 847 } | |
| 848 case ProxyPrefs::MODE_FIXED_SERVERS: { | |
| 849 std::string proxy_server; | |
| 850 server->GetAsString(&proxy_server); | |
| 851 std::string bypass_list_string; | |
| 852 if (bypass_list) | |
| 853 bypass_list->GetAsString(&bypass_list_string); | |
| 854 prefs->SetValue(prefs::kProxy, | |
| 855 ProxyConfigDictionary::CreateFixedServers( | |
| 856 proxy_server, bypass_list_string)); | |
| 857 break; | |
| 858 } | |
| 859 case ProxyPrefs::MODE_SYSTEM: | |
| 860 prefs->SetValue(prefs::kProxy, | |
| 861 ProxyConfigDictionary::CreateSystem()); | |
| 862 break; | |
| 863 case ProxyPrefs::kModeCount: | |
| 864 NOTREACHED(); | |
| 865 } | |
| 866 } | |
| 867 | |
| 868 } // namespace policy | |
| OLD | NEW |