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

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

Powered by Google App Engine
This is Rietveld 408576698