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

Side by Side Diff: chrome/browser/search_engines/default_search_policy_handler.cc

Issue 237653002: Import policy data into default search dictionary pref. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase on dict pref change Created 6 years, 7 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
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"
12 #include "chrome/browser/search_engines/default_search_manager.h"
11 #include "chrome/browser/search_engines/search_terms_data.h" 13 #include "chrome/browser/search_engines/search_terms_data.h"
12 #include "chrome/browser/search_engines/template_url.h" 14 #include "chrome/browser/search_engines/template_url.h"
13 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
14 #include "components/policy/core/browser/policy_error_map.h" 16 #include "components/policy/core/browser/policy_error_map.h"
15 #include "components/policy/core/common/policy_map.h" 17 #include "components/policy/core/common/policy_map.h"
16 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
17 #include "grit/component_strings.h" 19 #include "grit/component_strings.h"
18 #include "policy/policy_constants.h" 20 #include "policy/policy_constants.h"
19 21
20 namespace policy { 22 namespace policy {
21 23
24 namespace {
25 // Extract a list from a policy value and add it to a pref dictionary.
Peter Kasting 2014/04/29 01:47:32 Nit: Extract -> Extracts (and similar with "add")
Cait (Slow) 2014/04/29 17:14:51 Done.
26 void SetListInPref(const PolicyMap& policies,
27 const char* policy_name,
28 const char* key,
29 base::DictionaryValue* dict) {
30 DCHECK(dict);
31 const base::Value* policy_value = policies.GetValue(policy_name);
32 const base::ListValue* policy_list = NULL;
33 if (policy_value) {
34 bool is_list = policy_value->GetAsList(&policy_list);
35 DCHECK(is_list);
36 }
37 dict->Set(key, policy_list ? policy_list->DeepCopy() : new base::ListValue());
38 }
39
40 // Extract a string from a policy value and add it to a pref dictionary.
41 void SetStringInPref(const PolicyMap& policies,
42 const char* policy_name,
43 const char* key,
44 base::DictionaryValue* dict) {
45 DCHECK(dict);
46 const base::Value* policy_value = policies.GetValue(policy_name);
47 std::string str;
48 if (policy_value) {
49 bool is_string = policy_value->GetAsString(&str);
50 DCHECK(is_string);
51 }
52 dict->SetString(key, str);
53 }
54
55 } // namespace
56
22 // List of policy types to preference names, for policies affecting the default 57 // List of policy types to preference names, for policies affecting the default
23 // search provider. 58 // search provider.
24 const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { 59 const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = {
25 { key::kDefaultSearchProviderEnabled, 60 { key::kDefaultSearchProviderEnabled,
26 prefs::kDefaultSearchProviderEnabled, 61 prefs::kDefaultSearchProviderEnabled,
27 base::Value::TYPE_BOOLEAN }, 62 base::Value::TYPE_BOOLEAN },
28 { key::kDefaultSearchProviderName, 63 { key::kDefaultSearchProviderName,
29 prefs::kDefaultSearchProviderName, 64 prefs::kDefaultSearchProviderName,
30 base::Value::TYPE_STRING }, 65 base::Value::TYPE_STRING },
31 { key::kDefaultSearchProviderKeyword, 66 { key::kDefaultSearchProviderKeyword,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 prefs::kDefaultSearchProviderSuggestURLPostParams, 100 prefs::kDefaultSearchProviderSuggestURLPostParams,
66 base::Value::TYPE_STRING }, 101 base::Value::TYPE_STRING },
67 { key::kDefaultSearchProviderInstantURLPostParams, 102 { key::kDefaultSearchProviderInstantURLPostParams,
68 prefs::kDefaultSearchProviderInstantURLPostParams, 103 prefs::kDefaultSearchProviderInstantURLPostParams,
69 base::Value::TYPE_STRING }, 104 base::Value::TYPE_STRING },
70 { key::kDefaultSearchProviderImageURLPostParams, 105 { key::kDefaultSearchProviderImageURLPostParams,
71 prefs::kDefaultSearchProviderImageURLPostParams, 106 prefs::kDefaultSearchProviderImageURLPostParams,
72 base::Value::TYPE_STRING }, 107 base::Value::TYPE_STRING },
73 }; 108 };
74 109
110 // List of policy types to preference names, for policies affecting the default
111 // search provider.
112 const PolicyToPreferenceMapEntry kDefaultSearchPolicyDataMap[] = {
113 {key::kDefaultSearchProviderName, DefaultSearchManager::kShortName,
114 base::Value::TYPE_STRING},
115 {key::kDefaultSearchProviderKeyword, DefaultSearchManager::kKeyword,
116 base::Value::TYPE_STRING},
117 {key::kDefaultSearchProviderSearchURL, DefaultSearchManager::kURL,
118 base::Value::TYPE_STRING},
119 {key::kDefaultSearchProviderSuggestURL,
120 DefaultSearchManager::kSuggestionsURL, base::Value::TYPE_STRING},
121 {key::kDefaultSearchProviderInstantURL, DefaultSearchManager::kInstantURL,
122 base::Value::TYPE_STRING},
123 {key::kDefaultSearchProviderIconURL, DefaultSearchManager::kFaviconURL,
124 base::Value::TYPE_STRING},
125 {key::kDefaultSearchProviderEncodings,
126 DefaultSearchManager::kInputEncodings, base::Value::TYPE_LIST},
127 {key::kDefaultSearchProviderAlternateURLs,
128 DefaultSearchManager::kAlternateURLs, base::Value::TYPE_LIST},
129 {key::kDefaultSearchProviderSearchTermsReplacementKey,
130 DefaultSearchManager::kSearchTermsReplacementKey,
131 base::Value::TYPE_STRING},
132 {key::kDefaultSearchProviderImageURL, DefaultSearchManager::kImageURL,
133 base::Value::TYPE_STRING},
134 {key::kDefaultSearchProviderNewTabURL, DefaultSearchManager::kNewTabURL,
135 base::Value::TYPE_STRING},
136 {key::kDefaultSearchProviderSearchURLPostParams,
137 DefaultSearchManager::kSearchURLPostParams, base::Value::TYPE_STRING},
138 {key::kDefaultSearchProviderSuggestURLPostParams,
139 DefaultSearchManager::kSuggestionsURLPostParams, base::Value::TYPE_STRING},
140 {key::kDefaultSearchProviderInstantURLPostParams,
141 DefaultSearchManager::kInstantURLPostParams, base::Value::TYPE_STRING},
142 {key::kDefaultSearchProviderImageURLPostParams,
143 DefaultSearchManager::kImageURLPostParams, base::Value::TYPE_STRING},
144 };
145
75 // DefaultSearchEncodingsPolicyHandler implementation -------------------------- 146 // DefaultSearchEncodingsPolicyHandler implementation --------------------------
76 147
77 DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler() 148 DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler()
78 : TypeCheckingPolicyHandler(key::kDefaultSearchProviderEncodings, 149 : TypeCheckingPolicyHandler(key::kDefaultSearchProviderEncodings,
79 base::Value::TYPE_LIST) {} 150 base::Value::TYPE_LIST) {}
80 151
81 DefaultSearchEncodingsPolicyHandler::~DefaultSearchEncodingsPolicyHandler() { 152 DefaultSearchEncodingsPolicyHandler::~DefaultSearchEncodingsPolicyHandler() {
82 } 153 }
83 154
84 void DefaultSearchEncodingsPolicyHandler::ApplyPolicySettings( 155 void DefaultSearchEncodingsPolicyHandler::ApplyPolicySettings(
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 const base::Value* url; 221 const base::Value* url;
151 std::string dummy; 222 std::string dummy;
152 if (DefaultSearchURLIsValid(policies, &url, &dummy) || 223 if (DefaultSearchURLIsValid(policies, &url, &dummy) ||
153 !AnyDefaultSearchPoliciesSpecified(policies)) 224 !AnyDefaultSearchPoliciesSpecified(policies))
154 return true; 225 return true;
155 errors->AddError(key::kDefaultSearchProviderSearchURL, url ? 226 errors->AddError(key::kDefaultSearchProviderSearchURL, url ?
156 IDS_POLICY_INVALID_SEARCH_URL_ERROR : IDS_POLICY_NOT_SPECIFIED_ERROR); 227 IDS_POLICY_INVALID_SEARCH_URL_ERROR : IDS_POLICY_NOT_SPECIFIED_ERROR);
157 return false; 228 return false;
158 } 229 }
159 230
231 void DefaultSearchPolicyHandler::HandleDictionaryPref(const PolicyMap& policies,
232 PrefValueMap* prefs) {
233 if (DefaultSearchProviderIsDisabled(policies)) {
234 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
235 dict->SetBoolean(DefaultSearchManager::kDisabledByPolicy, true);
236 DefaultSearchManager::AddPrefValueToMap(prefs, dict.release());
Peter Kasting 2014/04/29 01:47:32 Nit: Early return and remove "else" below, to avoi
Cait (Slow) 2014/04/29 17:14:51 Done.
237 } else {
238 // The search URL is required. The other entries are optional. Just make
239 // sure that they are all specified via policy, so that the regular prefs
240 // aren't used.
241 const base::Value* dummy;
242 std::string url;
243 if (!DefaultSearchURLIsValid(policies, &dummy, &url))
244 return;
245
246 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
247 for (size_t i = 0; i < arraysize(kDefaultSearchPolicyDataMap); ++i) {
248 const char* policy_name = kDefaultSearchPolicyDataMap[i].policy_name;
249 switch (kDefaultSearchPolicyDataMap[i].value_type) {
250 case base::Value::TYPE_STRING:
251 SetStringInPref(policies,
252 policy_name,
253 kDefaultSearchPolicyDataMap[i].preference_path,
254 dict);
255 break;
256 case base::Value::TYPE_LIST:
257 SetListInPref(policies,
258 policy_name,
259 kDefaultSearchPolicyDataMap[i].preference_path,
260 dict);
261 break;
262 default:
263 NOTREACHED();
264 break;
265 }
266 }
267
268 // Set the fields which are not specified by the policy to default values.
269 dict->SetString(DefaultSearchManager::kID,
270 base::Int64ToString(kInvalidTemplateURLID));
271 dict->SetInteger(DefaultSearchManager::kPrepopulateID, 0);
272 dict->SetString(DefaultSearchManager::kSyncGUID, std::string());
273 dict->SetString(DefaultSearchManager::kOriginatingURL, std::string());
274 dict->SetBoolean(DefaultSearchManager::kSafeForAutoReplace, true);
275 dict->SetDouble(DefaultSearchManager::kDateCreated,
276 base::Time::Now().ToInternalValue());
277 dict->SetDouble(DefaultSearchManager::kLastModified,
278 base::Time::Now().ToInternalValue());
279 dict->SetInteger(DefaultSearchManager::kUsageCount, 0);
280 dict->SetBoolean(DefaultSearchManager::kCreatedByPolicy, true);
281
282 // For the name and keyword, default to the host if not specified. If
283 // there is no host (file: URLs? Not sure), use "_" to guarantee that the
Peter Kasting 2014/04/29 01:47:32 Right, I believe file: URLs won't have a host. Yo
Cait (Slow) 2014/04/29 17:14:51 Done.
284 // keyword is non-empty.
285 std::string name, keyword;
286 dict->GetString(DefaultSearchManager::kKeyword, &keyword);
287 dict->GetString(DefaultSearchManager::kShortName, &name);
288 dict->GetString(DefaultSearchManager::kURL, &url);
289
290 std::string host(GURL(url).host());
291 if (host.empty())
292 host = "_";
293 if (name.empty())
294 dict->SetString(DefaultSearchManager::kShortName, host);
295 if (keyword.empty())
296 dict->SetString(DefaultSearchManager::kKeyword, host);
297
298 DefaultSearchManager::AddPrefValueToMap(prefs, dict.release());
299 }
300 }
301
160 void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies, 302 void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
161 PrefValueMap* prefs) { 303 PrefValueMap* prefs) {
304 HandleDictionaryPref(policies, prefs);
305
162 if (DefaultSearchProviderIsDisabled(policies)) { 306 if (DefaultSearchProviderIsDisabled(policies)) {
163 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, false); 307 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, false);
164 308
165 // If default search is disabled, the other fields are ignored. 309 // If default search is disabled, the other fields are ignored.
166 prefs->SetString(prefs::kDefaultSearchProviderName, std::string()); 310 prefs->SetString(prefs::kDefaultSearchProviderName, std::string());
167 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, std::string()); 311 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, std::string());
168 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, std::string()); 312 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, std::string());
169 prefs->SetString(prefs::kDefaultSearchProviderIconURL, std::string()); 313 prefs->SetString(prefs::kDefaultSearchProviderIconURL, std::string());
170 prefs->SetString(prefs::kDefaultSearchProviderEncodings, std::string()); 314 prefs->SetString(prefs::kDefaultSearchProviderEncodings, std::string());
171 prefs->SetString(prefs::kDefaultSearchProviderKeyword, std::string()); 315 prefs->SetString(prefs::kDefaultSearchProviderKeyword, std::string());
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 void DefaultSearchPolicyHandler::EnsureListPrefExists( 458 void DefaultSearchPolicyHandler::EnsureListPrefExists(
315 PrefValueMap* prefs, 459 PrefValueMap* prefs,
316 const std::string& path) { 460 const std::string& path) {
317 base::Value* value; 461 base::Value* value;
318 base::ListValue* list_value; 462 base::ListValue* list_value;
319 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) 463 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value))
320 prefs->SetValue(path, new base::ListValue()); 464 prefs->SetValue(path, new base::ListValue());
321 } 465 }
322 466
323 } // namespace policy 467 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698