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

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: Nits and rebase on ToT 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 // Extracts a list from a policy value and adds it to a pref dictionary.
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 // Extracts a string from a policy value and adds 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(dict.release(), prefs);
237 return;
238 }
239
240 // The search URL is required. The other entries are optional. Just make
241 // sure that they are all specified via policy, so that the regular prefs
242 // aren't used.
243 const base::Value* dummy;
244 std::string url;
245 if (!DefaultSearchURLIsValid(policies, &dummy, &url))
246 return;
247
248 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
249 for (size_t i = 0; i < arraysize(kDefaultSearchPolicyDataMap); ++i) {
250 const char* policy_name = kDefaultSearchPolicyDataMap[i].policy_name;
251 switch (kDefaultSearchPolicyDataMap[i].value_type) {
252 case base::Value::TYPE_STRING:
253 SetStringInPref(policies,
254 policy_name,
255 kDefaultSearchPolicyDataMap[i].preference_path,
256 dict.get());
257 break;
258 case base::Value::TYPE_LIST:
259 SetListInPref(policies,
260 policy_name,
261 kDefaultSearchPolicyDataMap[i].preference_path,
262 dict.get());
263 break;
264 default:
265 NOTREACHED();
266 break;
267 }
268 }
269
270 // Set the fields which are not specified by the policy to default values.
271 dict->SetString(DefaultSearchManager::kID,
272 base::Int64ToString(kInvalidTemplateURLID));
273 dict->SetInteger(DefaultSearchManager::kPrepopulateID, 0);
274 dict->SetString(DefaultSearchManager::kSyncGUID, std::string());
275 dict->SetString(DefaultSearchManager::kOriginatingURL, std::string());
276 dict->SetBoolean(DefaultSearchManager::kSafeForAutoReplace, true);
277 dict->SetDouble(DefaultSearchManager::kDateCreated,
278 base::Time::Now().ToInternalValue());
279 dict->SetDouble(DefaultSearchManager::kLastModified,
280 base::Time::Now().ToInternalValue());
281 dict->SetInteger(DefaultSearchManager::kUsageCount, 0);
282 dict->SetBoolean(DefaultSearchManager::kCreatedByPolicy, true);
283
284 // For the name and keyword, default to the host if not specified. If
285 // there is no host (as is the case with file URLs of the form:
286 // "file:///c:/..."), use "_" to guarantee that the keyword is non-empty.
287 std::string name, keyword;
288 dict->GetString(DefaultSearchManager::kKeyword, &keyword);
289 dict->GetString(DefaultSearchManager::kShortName, &name);
290 dict->GetString(DefaultSearchManager::kURL, &url);
291
292 std::string host(GURL(url).host());
293 if (host.empty())
294 host = "_";
295 if (name.empty())
296 dict->SetString(DefaultSearchManager::kShortName, host);
297 if (keyword.empty())
298 dict->SetString(DefaultSearchManager::kKeyword, host);
299
300 DefaultSearchManager::AddPrefValueToMap(dict.release(), prefs);
301 }
302
160 void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies, 303 void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
161 PrefValueMap* prefs) { 304 PrefValueMap* prefs) {
305 HandleDictionaryPref(policies, prefs);
306
162 if (DefaultSearchProviderIsDisabled(policies)) { 307 if (DefaultSearchProviderIsDisabled(policies)) {
163 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, false); 308 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, false);
164 309
165 // If default search is disabled, the other fields are ignored. 310 // If default search is disabled, the other fields are ignored.
166 prefs->SetString(prefs::kDefaultSearchProviderName, std::string()); 311 prefs->SetString(prefs::kDefaultSearchProviderName, std::string());
167 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, std::string()); 312 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, std::string());
168 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, std::string()); 313 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, std::string());
169 prefs->SetString(prefs::kDefaultSearchProviderIconURL, std::string()); 314 prefs->SetString(prefs::kDefaultSearchProviderIconURL, std::string());
170 prefs->SetString(prefs::kDefaultSearchProviderEncodings, std::string()); 315 prefs->SetString(prefs::kDefaultSearchProviderEncodings, std::string());
171 prefs->SetString(prefs::kDefaultSearchProviderKeyword, std::string()); 316 prefs->SetString(prefs::kDefaultSearchProviderKeyword, std::string());
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 void DefaultSearchPolicyHandler::EnsureListPrefExists( 459 void DefaultSearchPolicyHandler::EnsureListPrefExists(
315 PrefValueMap* prefs, 460 PrefValueMap* prefs,
316 const std::string& path) { 461 const std::string& path) {
317 base::Value* value; 462 base::Value* value;
318 base::ListValue* list_value; 463 base::ListValue* list_value;
319 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) 464 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value))
320 prefs->SetValue(path, new base::ListValue()); 465 prefs->SetValue(path, new base::ListValue());
321 } 466 }
322 467
323 } // namespace policy 468 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698