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..b89cec44f55aa2618186269452ac7635b6b320e9 100644 |
--- a/chrome/browser/search_engines/default_search_policy_handler.cc |
+++ b/chrome/browser/search_engines/default_search_policy_handler.cc |
@@ -6,8 +6,10 @@ |
#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/default_search_manager.h" |
#include "chrome/browser/search_engines/search_terms_data.h" |
#include "chrome/browser/search_engines/template_url.h" |
#include "chrome/common/pref_names.h" |
@@ -19,6 +21,39 @@ |
namespace policy { |
+namespace { |
+// Extracts a list from a policy value and adds it to a pref dictionary. |
+void SetListInPref(const PolicyMap& policies, |
+ const char* policy_name, |
+ const char* key, |
+ base::DictionaryValue* dict) { |
+ DCHECK(dict); |
+ const base::Value* policy_value = policies.GetValue(policy_name); |
+ const base::ListValue* policy_list = NULL; |
+ if (policy_value) { |
+ bool is_list = policy_value->GetAsList(&policy_list); |
+ DCHECK(is_list); |
+ } |
+ dict->Set(key, policy_list ? policy_list->DeepCopy() : new base::ListValue()); |
+} |
+ |
+// Extracts a string from a policy value and adds it to a pref dictionary. |
+void SetStringInPref(const PolicyMap& policies, |
+ const char* policy_name, |
+ const char* key, |
+ base::DictionaryValue* dict) { |
+ DCHECK(dict); |
+ const base::Value* policy_value = policies.GetValue(policy_name); |
+ std::string str; |
+ if (policy_value) { |
+ bool is_string = policy_value->GetAsString(&str); |
+ DCHECK(is_string); |
+ } |
+ dict->SetString(key, str); |
+} |
+ |
+} // namespace |
+ |
// List of policy types to preference names, for policies affecting the default |
// search provider. |
const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { |
@@ -72,6 +107,42 @@ 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, DefaultSearchManager::kShortName, |
+ base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderKeyword, DefaultSearchManager::kKeyword, |
+ base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderSearchURL, DefaultSearchManager::kURL, |
+ base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderSuggestURL, |
+ DefaultSearchManager::kSuggestionsURL, base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderInstantURL, DefaultSearchManager::kInstantURL, |
+ base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderIconURL, DefaultSearchManager::kFaviconURL, |
+ base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderEncodings, |
+ DefaultSearchManager::kInputEncodings, base::Value::TYPE_LIST}, |
+ {key::kDefaultSearchProviderAlternateURLs, |
+ DefaultSearchManager::kAlternateURLs, base::Value::TYPE_LIST}, |
+ {key::kDefaultSearchProviderSearchTermsReplacementKey, |
+ DefaultSearchManager::kSearchTermsReplacementKey, |
+ base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderImageURL, DefaultSearchManager::kImageURL, |
+ base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderNewTabURL, DefaultSearchManager::kNewTabURL, |
+ base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderSearchURLPostParams, |
+ DefaultSearchManager::kSearchURLPostParams, base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderSuggestURLPostParams, |
+ DefaultSearchManager::kSuggestionsURLPostParams, base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderInstantURLPostParams, |
+ DefaultSearchManager::kInstantURLPostParams, base::Value::TYPE_STRING}, |
+ {key::kDefaultSearchProviderImageURLPostParams, |
+ DefaultSearchManager::kImageURLPostParams, base::Value::TYPE_STRING}, |
+}; |
+ |
// DefaultSearchEncodingsPolicyHandler implementation -------------------------- |
DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler() |
@@ -157,8 +228,82 @@ bool DefaultSearchPolicyHandler::CheckPolicySettings(const PolicyMap& policies, |
return false; |
} |
+void DefaultSearchPolicyHandler::HandleDictionaryPref(const PolicyMap& policies, |
+ PrefValueMap* prefs) { |
+ if (DefaultSearchProviderIsDisabled(policies)) { |
+ scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
+ dict->SetBoolean(DefaultSearchManager::kDisabledByPolicy, true); |
+ DefaultSearchManager::AddPrefValueToMap(dict.release(), prefs); |
+ return; |
+ } |
+ |
+ // 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; |
+ |
+ scoped_ptr<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, |
+ policy_name, |
+ kDefaultSearchPolicyDataMap[i].preference_path, |
+ dict.get()); |
+ break; |
+ case base::Value::TYPE_LIST: |
+ SetListInPref(policies, |
+ policy_name, |
+ kDefaultSearchPolicyDataMap[i].preference_path, |
+ dict.get()); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ break; |
+ } |
+ } |
+ |
+ // Set the fields which are not specified by the policy to default values. |
+ dict->SetString(DefaultSearchManager::kID, |
+ base::Int64ToString(kInvalidTemplateURLID)); |
+ dict->SetInteger(DefaultSearchManager::kPrepopulateID, 0); |
+ dict->SetString(DefaultSearchManager::kSyncGUID, std::string()); |
+ dict->SetString(DefaultSearchManager::kOriginatingURL, std::string()); |
+ dict->SetBoolean(DefaultSearchManager::kSafeForAutoReplace, true); |
+ dict->SetDouble(DefaultSearchManager::kDateCreated, |
+ base::Time::Now().ToInternalValue()); |
+ dict->SetDouble(DefaultSearchManager::kLastModified, |
+ base::Time::Now().ToInternalValue()); |
+ dict->SetInteger(DefaultSearchManager::kUsageCount, 0); |
+ dict->SetBoolean(DefaultSearchManager::kCreatedByPolicy, true); |
+ |
+ // For the name and keyword, default to the host if not specified. If |
+ // there is no host (as is the case with file URLs of the form: |
+ // "file:///c:/..."), use "_" to guarantee that the keyword is non-empty. |
+ std::string name, keyword; |
+ dict->GetString(DefaultSearchManager::kKeyword, &keyword); |
+ dict->GetString(DefaultSearchManager::kShortName, &name); |
+ dict->GetString(DefaultSearchManager::kURL, &url); |
+ |
+ std::string host(GURL(url).host()); |
+ if (host.empty()) |
+ host = "_"; |
+ if (name.empty()) |
+ dict->SetString(DefaultSearchManager::kShortName, host); |
+ if (keyword.empty()) |
+ dict->SetString(DefaultSearchManager::kKeyword, host); |
+ |
+ DefaultSearchManager::AddPrefValueToMap(dict.release(), prefs); |
+} |
+ |
void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies, |
PrefValueMap* prefs) { |
+ HandleDictionaryPref(policies, prefs); |
+ |
if (DefaultSearchProviderIsDisabled(policies)) { |
prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, false); |