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

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

Powered by Google App Engine
This is Rietveld 408576698