OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search_engines/default_search_policy_handler.h" | 5 #include "chrome/browser/search_engines/default_search_policy_handler.h" |
6 | 6 |
7 #include "base/prefs/pref_value_map.h" | 7 #include "base/prefs/pref_value_map.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/strings/string_number_conversions.h" | |
9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/search_engines/search_terms_data.h" | 12 #include "chrome/browser/search_engines/search_terms_data.h" |
12 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
14 #include "components/policy/core/browser/policy_error_map.h" | 15 #include "components/policy/core/browser/policy_error_map.h" |
15 #include "components/policy/core/common/policy_map.h" | 16 #include "components/policy/core/common/policy_map.h" |
16 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
17 #include "grit/component_strings.h" | 18 #include "grit/component_strings.h" |
18 #include "policy/policy_constants.h" | 19 #include "policy/policy_constants.h" |
19 | 20 |
20 namespace policy { | 21 namespace policy { |
21 | 22 |
23 // Keys for kDefaultSearchProviderData Pref. | |
24 const char kID[] = "id"; | |
25 const char kShortName[] = "short_name"; | |
26 const char kKeyword[] = "keyword"; | |
27 const char kPrepopulateID[] = "prepopulate_id"; | |
28 const char kSyncGUID[] = "sync_guid"; | |
29 | |
30 const char kURL[] = "url"; | |
31 const char kSuggestionsURL[] = "suggestions_url"; | |
32 const char kInstantURL[] = "instant_url"; | |
33 const char kImageURL[] = "image_url"; | |
34 const char kNewTabURL[] = "new_tab_url"; | |
35 const char kFaviconURL[] = "favicon_url"; | |
36 const char kOriginatingURL[] = "originating_url"; | |
37 | |
38 const char kSearchURLPostParams[] = "search_url_post_params"; | |
39 const char kSuggestionsURLPostParams[] = "suggestions_url_post_params"; | |
40 const char kInstantURLPostParams[] = "instant_url_post_params"; | |
41 const char kImageURLPostParams[] = "image_url_post_params"; | |
42 | |
43 const char kSafeForAutoReplace[] = "safe_for_autoreplace"; | |
44 const char kInputEncodings[] = "input_encodings"; | |
45 | |
46 const char kDateCreated[] = "date_created"; | |
47 const char kLastModified[] = "last_modified"; | |
48 | |
49 const char kUsageCount[] = "usage_count"; | |
50 const char kAlternateURLs[] = "alternate_urls"; | |
51 const char kSearchTermsReplacementKey[] = "search_terms_replacement_key"; | |
52 const char kCreatedByPolicy[] = "created_by_policy"; | |
gab
2014/04/15 20:52:33
We should avoid duplicating these again here (IIRC
Cait (Slow)
2014/04/16 21:14:11
Done.
| |
53 | |
22 // List of policy types to preference names, for policies affecting the default | 54 // List of policy types to preference names, for policies affecting the default |
23 // search provider. | 55 // search provider. |
24 const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { | 56 const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { |
25 { key::kDefaultSearchProviderEnabled, | 57 { key::kDefaultSearchProviderEnabled, |
26 prefs::kDefaultSearchProviderEnabled, | 58 prefs::kDefaultSearchProviderEnabled, |
27 base::Value::TYPE_BOOLEAN }, | 59 base::Value::TYPE_BOOLEAN }, |
28 { key::kDefaultSearchProviderName, | 60 { key::kDefaultSearchProviderName, |
29 prefs::kDefaultSearchProviderName, | 61 prefs::kDefaultSearchProviderName, |
30 base::Value::TYPE_STRING }, | 62 base::Value::TYPE_STRING }, |
31 { key::kDefaultSearchProviderKeyword, | 63 { key::kDefaultSearchProviderKeyword, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 prefs::kDefaultSearchProviderSuggestURLPostParams, | 97 prefs::kDefaultSearchProviderSuggestURLPostParams, |
66 base::Value::TYPE_STRING }, | 98 base::Value::TYPE_STRING }, |
67 { key::kDefaultSearchProviderInstantURLPostParams, | 99 { key::kDefaultSearchProviderInstantURLPostParams, |
68 prefs::kDefaultSearchProviderInstantURLPostParams, | 100 prefs::kDefaultSearchProviderInstantURLPostParams, |
69 base::Value::TYPE_STRING }, | 101 base::Value::TYPE_STRING }, |
70 { key::kDefaultSearchProviderImageURLPostParams, | 102 { key::kDefaultSearchProviderImageURLPostParams, |
71 prefs::kDefaultSearchProviderImageURLPostParams, | 103 prefs::kDefaultSearchProviderImageURLPostParams, |
72 base::Value::TYPE_STRING }, | 104 base::Value::TYPE_STRING }, |
73 }; | 105 }; |
74 | 106 |
107 // List of policy types to preference names, for policies affecting the default | |
108 // search provider. | |
109 const PolicyToPreferenceMapEntry kDefaultSearchPolicyDataMap[] = { | |
110 { key::kDefaultSearchProviderName, | |
111 kShortName, | |
112 base::Value::TYPE_STRING }, | |
113 { key::kDefaultSearchProviderKeyword, | |
114 kKeyword, | |
115 base::Value::TYPE_STRING }, | |
116 { key::kDefaultSearchProviderSearchURL, | |
117 kURL, | |
118 base::Value::TYPE_STRING }, | |
119 { key::kDefaultSearchProviderSuggestURL, | |
120 kSuggestionsURL, | |
121 base::Value::TYPE_STRING }, | |
122 { key::kDefaultSearchProviderInstantURL, | |
123 kInstantURL, | |
124 base::Value::TYPE_STRING }, | |
125 { key::kDefaultSearchProviderIconURL, | |
126 kFaviconURL, | |
127 base::Value::TYPE_STRING }, | |
128 { key::kDefaultSearchProviderEncodings, | |
129 kInputEncodings, | |
130 base::Value::TYPE_LIST }, | |
131 { key::kDefaultSearchProviderAlternateURLs, | |
132 kAlternateURLs, | |
133 base::Value::TYPE_LIST }, | |
134 { key::kDefaultSearchProviderSearchTermsReplacementKey, | |
135 kSearchTermsReplacementKey, | |
136 base::Value::TYPE_STRING }, | |
137 { key::kDefaultSearchProviderImageURL, | |
138 kImageURL, | |
139 base::Value::TYPE_STRING }, | |
140 { key::kDefaultSearchProviderNewTabURL, | |
141 kNewTabURL, | |
142 base::Value::TYPE_STRING }, | |
143 { key::kDefaultSearchProviderSearchURLPostParams, | |
144 kSearchURLPostParams, | |
145 base::Value::TYPE_STRING }, | |
146 { key::kDefaultSearchProviderSuggestURLPostParams, | |
147 kSuggestionsURLPostParams, | |
148 base::Value::TYPE_STRING }, | |
149 { key::kDefaultSearchProviderInstantURLPostParams, | |
150 kInstantURLPostParams, | |
151 base::Value::TYPE_STRING }, | |
152 { key::kDefaultSearchProviderImageURLPostParams, | |
153 kImageURLPostParams, | |
154 base::Value::TYPE_STRING }, | |
155 }; | |
156 | |
157 | |
gab
2014/04/15 20:52:33
nit: rm extra empty line
Cait (Slow)
2014/04/16 21:14:11
Done.
| |
75 // DefaultSearchEncodingsPolicyHandler implementation -------------------------- | 158 // DefaultSearchEncodingsPolicyHandler implementation -------------------------- |
76 | 159 |
77 DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler() | 160 DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler() |
78 : TypeCheckingPolicyHandler(key::kDefaultSearchProviderEncodings, | 161 : TypeCheckingPolicyHandler(key::kDefaultSearchProviderEncodings, |
79 base::Value::TYPE_LIST) {} | 162 base::Value::TYPE_LIST) {} |
80 | 163 |
81 DefaultSearchEncodingsPolicyHandler::~DefaultSearchEncodingsPolicyHandler() { | 164 DefaultSearchEncodingsPolicyHandler::~DefaultSearchEncodingsPolicyHandler() { |
82 } | 165 } |
83 | 166 |
84 void DefaultSearchEncodingsPolicyHandler::ApplyPolicySettings( | 167 void DefaultSearchEncodingsPolicyHandler::ApplyPolicySettings( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 const base::Value* url; | 233 const base::Value* url; |
151 std::string dummy; | 234 std::string dummy; |
152 if (DefaultSearchURLIsValid(policies, &url, &dummy) || | 235 if (DefaultSearchURLIsValid(policies, &url, &dummy) || |
153 !AnyDefaultSearchPoliciesSpecified(policies)) | 236 !AnyDefaultSearchPoliciesSpecified(policies)) |
154 return true; | 237 return true; |
155 errors->AddError(key::kDefaultSearchProviderSearchURL, url ? | 238 errors->AddError(key::kDefaultSearchProviderSearchURL, url ? |
156 IDS_POLICY_INVALID_SEARCH_URL_ERROR : IDS_POLICY_NOT_SPECIFIED_ERROR); | 239 IDS_POLICY_INVALID_SEARCH_URL_ERROR : IDS_POLICY_NOT_SPECIFIED_ERROR); |
157 return false; | 240 return false; |
158 } | 241 } |
159 | 242 |
243 void SetListInPref(const PolicyMap& policies, base::DictionaryValue* dict, | |
gab
2014/04/15 20:52:33
out-param last
Cait (Slow)
2014/04/16 21:14:11
Done.
| |
244 const char* policy_name, const char* key) { | |
245 const base::Value* value = policies.GetValue(policy_name); | |
246 const base::ListValue* list = new base::ListValue(); | |
247 if (value) | |
248 value->GetAsList(&list); | |
gab
2014/04/15 20:52:33
I would prefer to check that this is indeed a list
Cait (Slow)
2014/04/16 21:14:11
Done.
| |
249 dict->Set(key, list->DeepCopy()); | |
250 } | |
251 | |
252 void SetStringInPref(const PolicyMap& policies, base::DictionaryValue* dict, | |
253 const char* policy_name, const char* key) { | |
254 const base::Value* value = policies.GetValue(policy_name); | |
255 std::string str = std::string(); | |
gab
2014/04/15 20:52:33
No need to explicitly initialize:
std::string str
Cait (Slow)
2014/04/16 21:14:11
Done.
| |
256 if (value) | |
257 value->GetAsString(&str); | |
gab
2014/04/15 20:52:33
DCHECK return value.
Cait (Slow)
2014/04/16 21:14:11
Done.
| |
258 dict->SetString(key, str); | |
259 } | |
260 | |
261 void DefaultSearchPolicyHandler::HandleDictionaryPref(const PolicyMap& policies, | |
262 PrefValueMap* prefs) { | |
263 TemplateURLData data; | |
264 if (DefaultSearchProviderIsDisabled(policies)) { | |
265 // Set data to default values. | |
gab
2014/04/15 20:52:33
TODO?
Cait (Slow)
2014/04/16 21:14:11
Done.
| |
266 } else { | |
267 // The search URL is required. The other entries are optional. Just make | |
268 // sure that they are all specified via policy, so that the regular prefs | |
269 // aren't used. | |
270 const base::Value* dummy; | |
271 std::string url; | |
272 if (!DefaultSearchURLIsValid(policies, &dummy, &url)) | |
273 return; | |
274 | |
275 base::DictionaryValue* dict = new base::DictionaryValue; | |
276 for (size_t i = 0; i < arraysize(kDefaultSearchPolicyDataMap); ++i) { | |
277 const char* policy_name = kDefaultSearchPolicyDataMap[i].policy_name; | |
278 switch (kDefaultSearchPolicyDataMap[i].value_type) { | |
279 case base::Value::TYPE_STRING: | |
280 SetStringInPref(policies, dict, policy_name, | |
281 kDefaultSearchPolicyDataMap[i].preference_path); | |
282 break; | |
283 case base::Value::TYPE_LIST: | |
284 SetListInPref(policies, dict, policy_name, | |
285 kDefaultSearchPolicyDataMap[i].preference_path); | |
286 break; | |
287 } | |
gab
2014/04/15 20:52:33
Clang will complain about not all cases being hand
Cait (Slow)
2014/04/16 21:14:11
Done.
| |
288 } | |
289 dict->SetString(kID, base::Int64ToString(kInvalidTemplateURLID)); | |
290 dict->SetInteger(kPrepopulateID, 0); | |
291 dict->SetString(kSyncGUID, std::string()); | |
292 dict->SetString(kOriginatingURL, std::string()); | |
gab
2014/04/15 20:52:33
Add comments where necessary above/below for the r
Cait (Slow)
2014/04/16 21:14:11
These may go away if we decide there's no point in
| |
293 | |
294 dict->SetBoolean(kSafeForAutoReplace, true); | |
295 | |
296 dict->SetDouble(kDateCreated, base::Time::Now().ToInternalValue()); | |
297 dict->SetDouble(kLastModified, base::Time::Now().ToInternalValue()); | |
298 dict->SetInteger(kUsageCount, 0); | |
299 dict->SetBoolean(kCreatedByPolicy, true); | |
300 | |
301 // For the name and keyword, default to the host if not specified. If | |
302 // there is no host (file: URLs? Not sure), use "_" to guarantee that the | |
303 // keyword is non-empty. | |
304 std::string name, keyword; | |
305 dict->GetString(kKeyword, &keyword); | |
306 dict->GetString(kShortName, &name); | |
307 dict->GetString(kURL, &url); | |
308 std::string host(GURL(url).host()); | |
309 if (host.empty()) | |
310 host = "_"; | |
311 if (name.empty()) | |
312 dict->SetString(kShortName, host); | |
313 if (keyword.empty()) | |
314 dict->SetString(kKeyword, host); | |
315 | |
316 prefs->SetValue(prefs::kDefaultSearchProviderData, dict); | |
317 } | |
318 } | |
319 | |
160 void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies, | 320 void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies, |
161 PrefValueMap* prefs) { | 321 PrefValueMap* prefs) { |
322 HandleDictionaryPref(policies, prefs); | |
323 | |
162 if (DefaultSearchProviderIsDisabled(policies)) { | 324 if (DefaultSearchProviderIsDisabled(policies)) { |
163 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, false); | 325 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, false); |
164 | 326 |
165 // If default search is disabled, the other fields are ignored. | 327 // If default search is disabled, the other fields are ignored. |
166 prefs->SetString(prefs::kDefaultSearchProviderName, std::string()); | 328 prefs->SetString(prefs::kDefaultSearchProviderName, std::string()); |
167 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, std::string()); | 329 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, std::string()); |
168 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, std::string()); | 330 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, std::string()); |
169 prefs->SetString(prefs::kDefaultSearchProviderIconURL, std::string()); | 331 prefs->SetString(prefs::kDefaultSearchProviderIconURL, std::string()); |
170 prefs->SetString(prefs::kDefaultSearchProviderEncodings, std::string()); | 332 prefs->SetString(prefs::kDefaultSearchProviderEncodings, std::string()); |
171 prefs->SetString(prefs::kDefaultSearchProviderKeyword, std::string()); | 333 prefs->SetString(prefs::kDefaultSearchProviderKeyword, std::string()); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 void DefaultSearchPolicyHandler::EnsureListPrefExists( | 476 void DefaultSearchPolicyHandler::EnsureListPrefExists( |
315 PrefValueMap* prefs, | 477 PrefValueMap* prefs, |
316 const std::string& path) { | 478 const std::string& path) { |
317 base::Value* value; | 479 base::Value* value; |
318 base::ListValue* list_value; | 480 base::ListValue* list_value; |
319 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) | 481 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) |
320 prefs->SetValue(path, new base::ListValue()); | 482 prefs->SetValue(path, new base::ListValue()); |
321 } | 483 } |
322 | 484 |
323 } // namespace policy | 485 } // namespace policy |
OLD | NEW |