Chromium Code Reviews| Index: chrome/browser/search_engines/default_search_policy_handler.cc |
| diff --git a/chrome/browser/search_engines/default_search_policy_handler.cc b/chrome/browser/search_engines/default_search_policy_handler.cc |
| index 05018780f290709fb9d8a183e13a7f935d0b8894..3a02671bfc978e499169c12ea126983ffcce14f1 100644 |
| --- a/chrome/browser/search_engines/default_search_policy_handler.cc |
| +++ b/chrome/browser/search_engines/default_search_policy_handler.cc |
| @@ -6,6 +6,7 @@ |
| #include "base/prefs/pref_value_map.h" |
| #include "base/stl_util.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_util.h" |
| #include "chrome/browser/chrome_notification_types.h" |
| #include "chrome/browser/search_engines/search_terms_data.h" |
| @@ -19,6 +20,37 @@ |
| namespace policy { |
| +// Keys for kDefaultSearchProviderData Pref. |
| +const char kID[] = "id"; |
| +const char kShortName[] = "short_name"; |
| +const char kKeyword[] = "keyword"; |
| +const char kPrepopulateID[] = "prepopulate_id"; |
| +const char kSyncGUID[] = "sync_guid"; |
| + |
| +const char kURL[] = "url"; |
| +const char kSuggestionsURL[] = "suggestions_url"; |
| +const char kInstantURL[] = "instant_url"; |
| +const char kImageURL[] = "image_url"; |
| +const char kNewTabURL[] = "new_tab_url"; |
| +const char kFaviconURL[] = "favicon_url"; |
| +const char kOriginatingURL[] = "originating_url"; |
| + |
| +const char kSearchURLPostParams[] = "search_url_post_params"; |
| +const char kSuggestionsURLPostParams[] = "suggestions_url_post_params"; |
| +const char kInstantURLPostParams[] = "instant_url_post_params"; |
| +const char kImageURLPostParams[] = "image_url_post_params"; |
| + |
| +const char kSafeForAutoReplace[] = "safe_for_autoreplace"; |
| +const char kInputEncodings[] = "input_encodings"; |
| + |
| +const char kDateCreated[] = "date_created"; |
| +const char kLastModified[] = "last_modified"; |
| + |
| +const char kUsageCount[] = "usage_count"; |
| +const char kAlternateURLs[] = "alternate_urls"; |
| +const char kSearchTermsReplacementKey[] = "search_terms_replacement_key"; |
| +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.
|
| + |
| // List of policy types to preference names, for policies affecting the default |
| // search provider. |
| const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { |
| @@ -72,6 +104,57 @@ const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { |
| base::Value::TYPE_STRING }, |
| }; |
| +// List of policy types to preference names, for policies affecting the default |
| +// search provider. |
| +const PolicyToPreferenceMapEntry kDefaultSearchPolicyDataMap[] = { |
| + { key::kDefaultSearchProviderName, |
| + kShortName, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderKeyword, |
| + kKeyword, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderSearchURL, |
| + kURL, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderSuggestURL, |
| + kSuggestionsURL, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderInstantURL, |
| + kInstantURL, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderIconURL, |
| + kFaviconURL, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderEncodings, |
| + kInputEncodings, |
| + base::Value::TYPE_LIST }, |
| + { key::kDefaultSearchProviderAlternateURLs, |
| + kAlternateURLs, |
| + base::Value::TYPE_LIST }, |
| + { key::kDefaultSearchProviderSearchTermsReplacementKey, |
| + kSearchTermsReplacementKey, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderImageURL, |
| + kImageURL, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderNewTabURL, |
| + kNewTabURL, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderSearchURLPostParams, |
| + kSearchURLPostParams, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderSuggestURLPostParams, |
| + kSuggestionsURLPostParams, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderInstantURLPostParams, |
| + kInstantURLPostParams, |
| + base::Value::TYPE_STRING }, |
| + { key::kDefaultSearchProviderImageURLPostParams, |
| + kImageURLPostParams, |
| + base::Value::TYPE_STRING }, |
| +}; |
| + |
| + |
|
gab
2014/04/15 20:52:33
nit: rm extra empty line
Cait (Slow)
2014/04/16 21:14:11
Done.
|
| // DefaultSearchEncodingsPolicyHandler implementation -------------------------- |
| DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler() |
| @@ -157,8 +240,87 @@ bool DefaultSearchPolicyHandler::CheckPolicySettings(const PolicyMap& policies, |
| return false; |
| } |
| +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.
|
| + const char* policy_name, const char* key) { |
| + const base::Value* value = policies.GetValue(policy_name); |
| + const base::ListValue* list = new base::ListValue(); |
| + if (value) |
| + 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.
|
| + dict->Set(key, list->DeepCopy()); |
| +} |
| + |
| +void SetStringInPref(const PolicyMap& policies, base::DictionaryValue* dict, |
| + const char* policy_name, const char* key) { |
| + const base::Value* value = policies.GetValue(policy_name); |
| + 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.
|
| + if (value) |
| + value->GetAsString(&str); |
|
gab
2014/04/15 20:52:33
DCHECK return value.
Cait (Slow)
2014/04/16 21:14:11
Done.
|
| + dict->SetString(key, str); |
| +} |
| + |
| +void DefaultSearchPolicyHandler::HandleDictionaryPref(const PolicyMap& policies, |
| + PrefValueMap* prefs) { |
| + TemplateURLData data; |
| + if (DefaultSearchProviderIsDisabled(policies)) { |
| + // Set data to default values. |
|
gab
2014/04/15 20:52:33
TODO?
Cait (Slow)
2014/04/16 21:14:11
Done.
|
| + } else { |
| + // The search URL is required. The other entries are optional. Just make |
| + // sure that they are all specified via policy, so that the regular prefs |
| + // aren't used. |
| + const base::Value* dummy; |
| + std::string url; |
| + if (!DefaultSearchURLIsValid(policies, &dummy, &url)) |
| + return; |
| + |
| + base::DictionaryValue* dict = new base::DictionaryValue; |
| + for (size_t i = 0; i < arraysize(kDefaultSearchPolicyDataMap); ++i) { |
| + const char* policy_name = kDefaultSearchPolicyDataMap[i].policy_name; |
| + switch (kDefaultSearchPolicyDataMap[i].value_type) { |
| + case base::Value::TYPE_STRING: |
| + SetStringInPref(policies, dict, policy_name, |
| + kDefaultSearchPolicyDataMap[i].preference_path); |
| + break; |
| + case base::Value::TYPE_LIST: |
| + SetListInPref(policies, dict, policy_name, |
| + kDefaultSearchPolicyDataMap[i].preference_path); |
| + break; |
| + } |
|
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.
|
| + } |
| + dict->SetString(kID, base::Int64ToString(kInvalidTemplateURLID)); |
| + dict->SetInteger(kPrepopulateID, 0); |
| + dict->SetString(kSyncGUID, std::string()); |
| + 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
|
| + |
| + dict->SetBoolean(kSafeForAutoReplace, true); |
| + |
| + dict->SetDouble(kDateCreated, base::Time::Now().ToInternalValue()); |
| + dict->SetDouble(kLastModified, base::Time::Now().ToInternalValue()); |
| + dict->SetInteger(kUsageCount, 0); |
| + dict->SetBoolean(kCreatedByPolicy, true); |
| + |
| + // For the name and keyword, default to the host if not specified. If |
| + // there is no host (file: URLs? Not sure), use "_" to guarantee that the |
| + // keyword is non-empty. |
| + std::string name, keyword; |
| + dict->GetString(kKeyword, &keyword); |
| + dict->GetString(kShortName, &name); |
| + dict->GetString(kURL, &url); |
| + std::string host(GURL(url).host()); |
| + if (host.empty()) |
| + host = "_"; |
| + if (name.empty()) |
| + dict->SetString(kShortName, host); |
| + if (keyword.empty()) |
| + dict->SetString(kKeyword, host); |
| + |
| + prefs->SetValue(prefs::kDefaultSearchProviderData, dict); |
| + } |
| +} |
| + |
| void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies, |
| PrefValueMap* prefs) { |
| + HandleDictionaryPref(policies, prefs); |
| + |
| if (DefaultSearchProviderIsDisabled(policies)) { |
| prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, false); |