Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: chrome/browser/policy/configuration_policy_handler.cc

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

Powered by Google App Engine
This is Rietveld 408576698