diff options
author | dconnelly@chromium.org <dconnelly@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-17 13:28:05 +0000 |
---|---|---|
committer | dconnelly@chromium.org <dconnelly@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-17 13:28:05 +0000 |
commit | 1be3d61abea26f5342024a10719c3acce2e8f58e (patch) | |
tree | 43623ba436c839a12f95a1299c21d396e7128dd5 | |
parent | 3901cbb959c76d8403d41af18b93f5f4612cda42 (diff) | |
download | chromium_src-1be3d61abea26f5342024a10719c3acce2e8f58e.zip chromium_src-1be3d61abea26f5342024a10719c3acce2e8f58e.tar.gz chromium_src-1be3d61abea26f5342024a10719c3acce2e8f58e.tar.bz2 |
Move DefaultSearchPolicyHandler to live in search_engines.
This facilitates the refactoring of the policy code into a layered
component.
BUG=271392
R=joaodasilva@chromium.org, pkasting@chromium.org
Review URL: https://codereview.chromium.org/27166002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@229124 0039d316-1c4b-4281-b951-d872f2087c98
14 files changed, 768 insertions, 798 deletions
diff --git a/chrome/browser/policy/DEPS b/chrome/browser/policy/DEPS index 31e99bf..844d027 100644 --- a/chrome/browser/policy/DEPS +++ b/chrome/browser/policy/DEPS @@ -50,10 +50,7 @@ specific_include_rules = { "+chrome/browser/prefs/proxy_config_dictionary.h", "+chrome/browser/prefs/proxy_prefs.h", "+chrome/browser/prefs/session_startup_pref.h", - "+chrome/browser/search_engines/search_terms_data.h", - "+chrome/browser/search_engines/template_url.h", "+chrome/common/extensions/extension.h", - "+content/public/browser/notification_service.h", ], r"configuration_policy_handler\.h": [ diff --git a/chrome/browser/policy/configuration_policy_handler.cc b/chrome/browser/policy/configuration_policy_handler.cc index a708757..093d25d 100644 --- a/chrome/browser/policy/configuration_policy_handler.cc +++ b/chrome/browser/policy/configuration_policy_handler.cc @@ -11,7 +11,6 @@ #include "base/json/json_writer.h" #include "base/logging.h" #include "base/prefs/pref_value_map.h" -#include "base/stl_util.h" #include "base/strings/string16.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" @@ -25,10 +24,7 @@ #include "chrome/browser/prefs/proxy_config_dictionary.h" #include "chrome/browser/prefs/proxy_prefs.h" #include "chrome/browser/prefs/session_startup_pref.h" -#include "chrome/browser/search_engines/search_terms_data.h" -#include "chrome/browser/search_engines/template_url.h" #include "chrome/common/extensions/extension.h" -#include "content/public/browser/notification_service.h" #include "grit/generated_resources.h" #include "policy/policy_constants.h" #include "url/gurl.h" @@ -823,263 +819,6 @@ void IncognitoModePolicyHandler::ApplyPolicySettings(const PolicyMap& policies, } -// DefaultSearchEncodingsPolicyHandler implementation -------------------------- - -DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler( - const char* pref_name) - : TypeCheckingPolicyHandler(key::kDefaultSearchProviderEncodings, - Value::TYPE_LIST), - pref_name_(pref_name) {} - -DefaultSearchEncodingsPolicyHandler::~DefaultSearchEncodingsPolicyHandler() { -} - -void DefaultSearchEncodingsPolicyHandler::ApplyPolicySettings( - const PolicyMap& policies, PrefValueMap* prefs) { - // The DefaultSearchProviderEncodings policy has type list, but the related - // preference has type string. Convert one into the other here, using - // ';' as a separator. - const Value* value = policies.GetValue(policy_name()); - const ListValue* list; - if (!value || !value->GetAsList(&list)) - return; - - ListValue::const_iterator iter(list->begin()); - ListValue::const_iterator end(list->end()); - std::vector<std::string> string_parts; - for (; iter != end; ++iter) { - std::string s; - if ((*iter)->GetAsString(&s)) { - string_parts.push_back(s); - } - } - std::string encodings = JoinString(string_parts, ';'); - prefs->SetValue(pref_name(), Value::CreateStringValue(encodings)); -} - - -// DefaultSearchPolicyHandler implementation ----------------------------------- - -DefaultSearchPolicyHandler::DefaultSearchPolicyHandler( - const char* id_pref_name, - const char* prepopulate_id_pref_name, - const PolicyToPreferenceMapEntry policy_to_pref_map[]) - : id_pref_name_(id_pref_name), - prepopulate_id_pref_name_(prepopulate_id_pref_name), - policy_to_pref_map_(policy_to_pref_map) { - for (size_t i = 0; i < DEFAULT_SEARCH_KEY_SIZE; ++i) { - const char* policy_name = policy_to_pref_map[i].policy_name; - if (policy_name == key::kDefaultSearchProviderEncodings) { - handlers_.push_back(new DefaultSearchEncodingsPolicyHandler( - policy_to_pref_map[i].preference_path)); - } else { - handlers_.push_back(new SimplePolicyHandler( - policy_name, - policy_to_pref_map[i].preference_path, - policy_to_pref_map[i].value_type)); - } - } -} - -DefaultSearchPolicyHandler::~DefaultSearchPolicyHandler() { - STLDeleteElements(&handlers_); -} - -bool DefaultSearchPolicyHandler::CheckPolicySettings(const PolicyMap& policies, - PolicyErrorMap* errors) { - if (!CheckIndividualPolicies(policies, errors)) - return false; - - if (DefaultSearchProviderIsDisabled(policies)) { - // Add an error for all specified default search policies except - // DefaultSearchProviderEnabled. - - for (std::vector<TypeCheckingPolicyHandler*>::const_iterator handler = - handlers_.begin(); - handler != handlers_.end(); ++handler) { - const char* policy_name = (*handler)->policy_name(); - if (policy_name != key::kDefaultSearchProviderEnabled && - HasDefaultSearchPolicy(policies, policy_name)) { - errors->AddError(policy_name, IDS_POLICY_DEFAULT_SEARCH_DISABLED); - } - } - return true; - } - - const Value* url; - std::string dummy; - if (DefaultSearchURLIsValid(policies, &url, &dummy) || - !AnyDefaultSearchPoliciesSpecified(policies)) - return true; - errors->AddError(key::kDefaultSearchProviderSearchURL, url ? - IDS_POLICY_INVALID_SEARCH_URL_ERROR : IDS_POLICY_NOT_SPECIFIED_ERROR); - return false; -} - -#define PREF_FOR(x) policy_to_pref_map_[x].preference_path -void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies, - PrefValueMap* prefs) { - if (DefaultSearchProviderIsDisabled(policies)) { - prefs->SetBoolean(PREF_FOR(DEFAULT_SEARCH_ENABLED), false); - - // If default search is disabled, the other fields are ignored. - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_NAME), std::string()); - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_SEARCH_URL), std::string()); - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_SUGGEST_URL), std::string()); - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_ICON_URL), std::string()); - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_ENCODINGS), std::string()); - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_KEYWORD), std::string()); - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_INSTANT_URL), std::string()); - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_NEW_TAB_URL), std::string()); - prefs->SetValue(PREF_FOR(DEFAULT_SEARCH_ALTERNATE_URLS), new ListValue()); - prefs->SetString( - PREF_FOR(DEFAULT_SEARCH_TERMS_REPLACEMENT_KEY), std::string()); - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_IMAGE_URL), std::string()); - prefs->SetString( - PREF_FOR(DEFAULT_SEARCH_SEARCH_URL_POST_PARAMS), std::string()); - prefs->SetString( - PREF_FOR(DEFAULT_SEARCH_SUGGEST_URL_POST_PARAMS), std::string()); - prefs->SetString( - PREF_FOR(DEFAULT_SEARCH_INSTANT_URL_POST_PARAMS), std::string()); - prefs->SetString( - PREF_FOR(DEFAULT_SEARCH_IMAGE_URL_POST_PARAMS), std::string()); - } 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 Value* dummy; - std::string url; - if (DefaultSearchURLIsValid(policies, &dummy, &url)) { - - for (std::vector<TypeCheckingPolicyHandler*>::const_iterator handler = - handlers_.begin(); - handler != handlers_.end(); ++handler) { - (*handler)->ApplyPolicySettings(policies, prefs); - } - - EnsureStringPrefExists(prefs, PREF_FOR(DEFAULT_SEARCH_SUGGEST_URL)); - EnsureStringPrefExists(prefs, PREF_FOR(DEFAULT_SEARCH_ICON_URL)); - EnsureStringPrefExists(prefs, PREF_FOR(DEFAULT_SEARCH_ENCODINGS)); - EnsureStringPrefExists(prefs, PREF_FOR(DEFAULT_SEARCH_KEYWORD)); - EnsureStringPrefExists(prefs, PREF_FOR(DEFAULT_SEARCH_INSTANT_URL)); - EnsureStringPrefExists(prefs, PREF_FOR(DEFAULT_SEARCH_NEW_TAB_URL)); - EnsureListPrefExists(prefs, PREF_FOR(DEFAULT_SEARCH_ALTERNATE_URLS)); - EnsureStringPrefExists( - prefs, - PREF_FOR(DEFAULT_SEARCH_TERMS_REPLACEMENT_KEY)); - EnsureStringPrefExists(prefs, PREF_FOR(DEFAULT_SEARCH_IMAGE_URL)); - EnsureStringPrefExists( - prefs, - PREF_FOR(DEFAULT_SEARCH_SEARCH_URL_POST_PARAMS)); - EnsureStringPrefExists( - prefs, - PREF_FOR(DEFAULT_SEARCH_SUGGEST_URL_POST_PARAMS)); - EnsureStringPrefExists( - prefs, - PREF_FOR(DEFAULT_SEARCH_INSTANT_URL_POST_PARAMS)); - EnsureStringPrefExists( - prefs, - PREF_FOR(DEFAULT_SEARCH_IMAGE_URL_POST_PARAMS)); - - // 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; - std::string host(GURL(url).host()); - if (host.empty()) - host = "_"; - if (!prefs->GetString(PREF_FOR(DEFAULT_SEARCH_NAME), &name) || - name.empty()) { - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_NAME), host); - } - if (!prefs->GetString(PREF_FOR(DEFAULT_SEARCH_KEYWORD), &keyword) || - keyword.empty()) { - prefs->SetString(PREF_FOR(DEFAULT_SEARCH_KEYWORD), host); - } - - // And clear the IDs since these are not specified via policy. - prefs->SetString(id_pref_name_, std::string()); - prefs->SetString(prepopulate_id_pref_name_, std::string()); - } - } - content::NotificationService::current()->Notify( - chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED, - content::NotificationService::AllSources(), - content::NotificationService::NoDetails()); -} -#undef PREF_FOR - -bool DefaultSearchPolicyHandler::CheckIndividualPolicies( - const PolicyMap& policies, - PolicyErrorMap* errors) { - for (std::vector<TypeCheckingPolicyHandler*>::const_iterator handler = - handlers_.begin(); - handler != handlers_.end(); ++handler) { - if (!(*handler)->CheckPolicySettings(policies, errors)) - return false; - } - return true; -} - -bool DefaultSearchPolicyHandler::HasDefaultSearchPolicy( - const PolicyMap& policies, - const char* policy_name) { - return policies.Get(policy_name) != NULL; -} - -bool DefaultSearchPolicyHandler::AnyDefaultSearchPoliciesSpecified( - const PolicyMap& policies) { - for (std::vector<TypeCheckingPolicyHandler*>::const_iterator handler = - handlers_.begin(); - handler != handlers_.end(); ++handler) { - if (policies.Get((*handler)->policy_name())) - return true; - } - return false; -} - -bool DefaultSearchPolicyHandler::DefaultSearchProviderIsDisabled( - const PolicyMap& policies) { - const Value* provider_enabled = - policies.GetValue(key::kDefaultSearchProviderEnabled); - bool enabled = true; - return provider_enabled && provider_enabled->GetAsBoolean(&enabled) && - !enabled; -} - -bool DefaultSearchPolicyHandler::DefaultSearchURLIsValid( - const PolicyMap& policies, - const Value** url_value, - std::string* url_string) { - *url_value = policies.GetValue(key::kDefaultSearchProviderSearchURL); - if (!*url_value || !(*url_value)->GetAsString(url_string) || - url_string->empty()) - return false; - TemplateURLData data; - data.SetURL(*url_string); - SearchTermsData search_terms_data; - return TemplateURL(NULL, data).SupportsReplacementUsingTermsData( - search_terms_data); -} - -void DefaultSearchPolicyHandler::EnsureStringPrefExists( - PrefValueMap* prefs, - const std::string& path) { - std::string value; - if (!prefs->GetString(path, &value)) - prefs->SetString(path, value); -} - -void DefaultSearchPolicyHandler::EnsureListPrefExists( - PrefValueMap* prefs, - const std::string& path) { - base::Value* value; - base::ListValue* list_value; - if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) - prefs->SetValue(path, new ListValue()); -} - - // ProxyPolicyHandler implementation ------------------------------------------- // The proxy policies have the peculiarity that they are loaded from individual diff --git a/chrome/browser/policy/configuration_policy_handler.h b/chrome/browser/policy/configuration_policy_handler.h index 0fc8755..6a642e5 100644 --- a/chrome/browser/policy/configuration_policy_handler.h +++ b/chrome/browser/policy/configuration_policy_handler.h @@ -402,106 +402,6 @@ class IncognitoModePolicyHandler : public ConfigurationPolicyHandler { DISALLOW_COPY_AND_ASSIGN(IncognitoModePolicyHandler); }; -// ConfigurationPolicyHandler for the DefaultSearchEncodings policy. -class DefaultSearchEncodingsPolicyHandler : public TypeCheckingPolicyHandler { - public: - explicit DefaultSearchEncodingsPolicyHandler(const char* pref_name); - virtual ~DefaultSearchEncodingsPolicyHandler(); - - // ConfigurationPolicyHandler methods: - virtual void ApplyPolicySettings(const PolicyMap& policies, - PrefValueMap* prefs) OVERRIDE; - - const char* pref_name() const { return pref_name_; } - - private: - const char* pref_name_; - DISALLOW_COPY_AND_ASSIGN(DefaultSearchEncodingsPolicyHandler); -}; - -enum DefaultSearchKey { - DEFAULT_SEARCH_ENABLED, - DEFAULT_SEARCH_NAME, - DEFAULT_SEARCH_KEYWORD, - DEFAULT_SEARCH_SEARCH_URL, - DEFAULT_SEARCH_SUGGEST_URL, - DEFAULT_SEARCH_INSTANT_URL, - DEFAULT_SEARCH_ICON_URL, - DEFAULT_SEARCH_ENCODINGS, - DEFAULT_SEARCH_ALTERNATE_URLS, - DEFAULT_SEARCH_TERMS_REPLACEMENT_KEY, - DEFAULT_SEARCH_IMAGE_URL, - DEFAULT_SEARCH_NEW_TAB_URL, - DEFAULT_SEARCH_SEARCH_URL_POST_PARAMS, - DEFAULT_SEARCH_SUGGEST_URL_POST_PARAMS, - DEFAULT_SEARCH_INSTANT_URL_POST_PARAMS, - DEFAULT_SEARCH_IMAGE_URL_POST_PARAMS, - // Must be last. - DEFAULT_SEARCH_KEY_SIZE, -}; - -// ConfigurationPolicyHandler for the default search policies. -class DefaultSearchPolicyHandler : public ConfigurationPolicyHandler { - public: - // Constructs a new handler for the DefaultSearch policy with the specified - // preference names. - // |id_pref_name|: Pref name for the search provider ID. - // |prepopulate_id_pref_name|: Pref name for the prepopulated provider ID. - // |policy_to_pref_map|: Defines the pref names for respective policy keys. - // Must contain exactly DEFAULT_SEARCH_KEY_SIZE entries and be ordered - // according to the DefaultSearchKey enum. - DefaultSearchPolicyHandler( - const char* id_pref_name, - const char* prepopulate_id_pref_name, - const PolicyToPreferenceMapEntry policy_to_pref_map[]); - virtual ~DefaultSearchPolicyHandler(); - - // ConfigurationPolicyHandler methods: - virtual bool CheckPolicySettings(const PolicyMap& policies, - PolicyErrorMap* errors) OVERRIDE; - virtual void ApplyPolicySettings(const PolicyMap& policies, - PrefValueMap* prefs) OVERRIDE; - - private: - // Calls |CheckPolicySettings()| on each of the handlers in |handlers_| - // and returns whether all of the calls succeeded. - bool CheckIndividualPolicies(const PolicyMap& policies, - PolicyErrorMap* errors); - - // Returns whether there is a value for |policy_name| in |policies|. - bool HasDefaultSearchPolicy(const PolicyMap& policies, - const char* policy_name); - - // Returns whether any default search policies are specified in |policies|. - bool AnyDefaultSearchPoliciesSpecified(const PolicyMap& policies); - - // Returns whether the default search provider is disabled. - bool DefaultSearchProviderIsDisabled(const PolicyMap& policies); - - // Returns whether the default search URL is set and valid. On success, both - // outparams (which must be non-NULL) are filled with the search URL. - bool DefaultSearchURLIsValid(const PolicyMap& policies, - const Value** url_value, - std::string* url_string); - - // Make sure that the |path| is present in |prefs_|. If not, set it to - // a blank string. - void EnsureStringPrefExists(PrefValueMap* prefs, const std::string& path); - - // Make sure that the |path| is present in |prefs_| and is a ListValue. If - // not, set it to an empty list. - void EnsureListPrefExists(PrefValueMap* prefs, const std::string& path); - - // The ConfigurationPolicyHandler handlers for each default search policy. - std::vector<TypeCheckingPolicyHandler*> handlers_; - - const char* id_pref_name_; - const char* prepopulate_id_pref_name_; - const PolicyToPreferenceMapEntry* policy_to_pref_map_; // weak - - DISALLOW_COPY_AND_ASSIGN(DefaultSearchPolicyHandler); -}; - // ConfigurationPolicyHandler for the proxy policies. class ProxyPolicyHandler : public ConfigurationPolicyHandler { public: diff --git a/chrome/browser/policy/configuration_policy_handler_list.cc b/chrome/browser/policy/configuration_policy_handler_list.cc index e2a5714..17d715a 100644 --- a/chrome/browser/policy/configuration_policy_handler_list.cc +++ b/chrome/browser/policy/configuration_policy_handler_list.cc @@ -13,6 +13,7 @@ #include "chrome/browser/policy/configuration_policy_handler.h" #include "chrome/browser/policy/policy_error_map.h" #include "chrome/browser/policy/policy_map.h" +#include "chrome/browser/search_engines/default_search_policy_handler.h" #include "chrome/common/pref_names.h" #include "extensions/common/manifest.h" #include "grit/generated_resources.h" @@ -34,62 +35,6 @@ namespace policy { -// List of policy types to preference names, for policies affecting the default -// search provider. -const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { - { key::kDefaultSearchProviderEnabled, - prefs::kDefaultSearchProviderEnabled, - Value::TYPE_BOOLEAN }, - { key::kDefaultSearchProviderName, - prefs::kDefaultSearchProviderName, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderKeyword, - prefs::kDefaultSearchProviderKeyword, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderSearchURL, - prefs::kDefaultSearchProviderSearchURL, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderSuggestURL, - prefs::kDefaultSearchProviderSuggestURL, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderInstantURL, - prefs::kDefaultSearchProviderInstantURL, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderIconURL, - prefs::kDefaultSearchProviderIconURL, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderEncodings, - prefs::kDefaultSearchProviderEncodings, - Value::TYPE_LIST }, - { key::kDefaultSearchProviderAlternateURLs, - prefs::kDefaultSearchProviderAlternateURLs, - Value::TYPE_LIST }, - { key::kDefaultSearchProviderSearchTermsReplacementKey, - prefs::kDefaultSearchProviderSearchTermsReplacementKey, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderImageURL, - prefs::kDefaultSearchProviderImageURL, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderNewTabURL, - prefs::kDefaultSearchProviderNewTabURL, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderSearchURLPostParams, - prefs::kDefaultSearchProviderSearchURLPostParams, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderSuggestURLPostParams, - prefs::kDefaultSearchProviderSuggestURLPostParams, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderInstantURLPostParams, - prefs::kDefaultSearchProviderInstantURLPostParams, - Value::TYPE_STRING }, - { key::kDefaultSearchProviderImageURLPostParams, - prefs::kDefaultSearchProviderImageURLPostParams, - Value::TYPE_STRING }, -}; - -COMPILE_ASSERT(DEFAULT_SEARCH_KEY_SIZE == arraysize(kDefaultSearchPolicyMap), - wrong_policy_map_size); - namespace { // List of policy types to preference names. This is used for simple policies @@ -509,10 +454,7 @@ ConfigurationPolicyHandlerList::ConfigurationPolicyHandlerList() { handlers_.push_back( new AutofillPolicyHandler(autofill::prefs::kAutofillEnabled)); - handlers_.push_back( - new DefaultSearchPolicyHandler(prefs::kDefaultSearchProviderID, - prefs::kDefaultSearchProviderPrepopulateID, - kDefaultSearchPolicyMap)); + handlers_.push_back(new DefaultSearchPolicyHandler()); handlers_.push_back(new FileSelectionDialogsHandler( prefs::kAllowFileSelectionDialogs, prefs::kPromptForDownload)); handlers_.push_back( diff --git a/chrome/browser/policy/configuration_policy_handler_list.h b/chrome/browser/policy/configuration_policy_handler_list.h index 599fd9a..8ecde65 100644 --- a/chrome/browser/policy/configuration_policy_handler_list.h +++ b/chrome/browser/policy/configuration_policy_handler_list.h @@ -18,11 +18,6 @@ class PolicyErrorMap; class PolicyMap; struct PolicyToPreferenceMapEntry; -// Declares the array of policy key to preference name mappings. Contains -// exactly DEFAULT_SEARCH_KEY entries, which are sorted according to the order -// of entries in the DefaultSearchKey enum. -extern const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[]; - // Converts policies to their corresponding preferences. Also does error // checking and cleans up policy values for displaying. class ConfigurationPolicyHandlerList { diff --git a/chrome/browser/policy/configuration_policy_handler_list_unittest.cc b/chrome/browser/policy/configuration_policy_handler_list_unittest.cc deleted file mode 100644 index c37e94f..0000000 --- a/chrome/browser/policy/configuration_policy_handler_list_unittest.cc +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/policy/configuration_policy_handler.h" -#include "chrome/browser/policy/configuration_policy_handler_list.h" -#include "policy/policy_constants.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace policy { - -TEST(DefaultSearchPolicyHandlerTest, PolicyToPreferenceMapInitialization) { - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_ENABLED].policy_name, - (const char * const)key::kDefaultSearchProviderEnabled); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_NAME].policy_name, - (const char * const)key::kDefaultSearchProviderName); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_KEYWORD].policy_name, - (const char * const)key::kDefaultSearchProviderKeyword); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_SEARCH_URL].policy_name, - (const char * const)key::kDefaultSearchProviderSearchURL); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_SUGGEST_URL].policy_name, - (const char * const)key::kDefaultSearchProviderSuggestURL); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_INSTANT_URL].policy_name, - (const char * const)key::kDefaultSearchProviderInstantURL); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_ICON_URL].policy_name, - (const char * const)key::kDefaultSearchProviderIconURL); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_ENCODINGS].policy_name, - (const char * const)key::kDefaultSearchProviderEncodings); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_ALTERNATE_URLS].policy_name, - (const char * const)key::kDefaultSearchProviderAlternateURLs); - EXPECT_EQ( - kDefaultSearchPolicyMap[DEFAULT_SEARCH_TERMS_REPLACEMENT_KEY].policy_name, - (const char * const)key::kDefaultSearchProviderSearchTermsReplacementKey); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_IMAGE_URL].policy_name, - (const char * const)key::kDefaultSearchProviderImageURL); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_NEW_TAB_URL].policy_name, - (const char * const)key::kDefaultSearchProviderNewTabURL); - EXPECT_EQ(kDefaultSearchPolicyMap[DEFAULT_SEARCH_SEARCH_URL_POST_PARAMS] - .policy_name, - (const char * const)key::kDefaultSearchProviderSearchURLPostParams); - EXPECT_EQ( - kDefaultSearchPolicyMap[DEFAULT_SEARCH_SUGGEST_URL_POST_PARAMS] - .policy_name, - (const char * const)key::kDefaultSearchProviderSuggestURLPostParams); - EXPECT_EQ( - kDefaultSearchPolicyMap[DEFAULT_SEARCH_INSTANT_URL_POST_PARAMS] - .policy_name, - (const char * const)key::kDefaultSearchProviderInstantURLPostParams); - EXPECT_EQ( - kDefaultSearchPolicyMap[DEFAULT_SEARCH_IMAGE_URL_POST_PARAMS].policy_name, - (const char * const)key::kDefaultSearchProviderImageURLPostParams); -} - -} // namespace policy diff --git a/chrome/browser/policy/configuration_policy_pref_store_unittest.cc b/chrome/browser/policy/configuration_policy_pref_store_unittest.cc index 6df3ae9..fe4abfe 100644 --- a/chrome/browser/policy/configuration_policy_pref_store_unittest.cc +++ b/chrome/browser/policy/configuration_policy_pref_store_unittest.cc @@ -2,19 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "chrome/browser/policy/configuration_policy_pref_store_unittest.h" + #include <string> #include "base/callback.h" #include "base/files/file_path.h" -#include "base/memory/ref_counted.h" -#include "base/message_loop/message_loop.h" #include "base/prefs/pref_store_observer_mock.h" #include "base/run_loop.h" #include "chrome/browser/policy/configuration_policy_handler.h" -#include "chrome/browser/policy/configuration_policy_handler_list.h" #include "chrome/browser/policy/configuration_policy_pref_store.h" #include "chrome/browser/policy/external_data_fetcher.h" -#include "chrome/browser/policy/mock_configuration_policy_provider.h" #include "chrome/browser/policy/policy_map.h" #include "chrome/browser/policy/policy_service_impl.h" #include "chrome/browser/prefs/incognito_mode_prefs.h" @@ -23,7 +21,6 @@ #include "chrome/common/pref_names.h" #include "policy/policy_constants.h" #include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" using testing::Mock; using testing::Return; @@ -46,36 +43,29 @@ class PolicyAndPref { const char* pref_name_; }; -class ConfigurationPolicyPrefStoreTest : public testing::Test { - protected: - ConfigurationPolicyPrefStoreTest() { - EXPECT_CALL(provider_, IsInitializationComplete(_)) - .WillRepeatedly(Return(false)); - provider_.Init(); - PolicyServiceImpl::Providers providers; - providers.push_back(&provider_); - policy_service_.reset(new PolicyServiceImpl(providers)); - store_ = new ConfigurationPolicyPrefStore(policy_service_.get(), - &handler_list_, - POLICY_LEVEL_MANDATORY); - } +ConfigurationPolicyPrefStoreTest::ConfigurationPolicyPrefStoreTest() { + EXPECT_CALL(provider_, IsInitializationComplete(_)) + .WillRepeatedly(Return(false)); + provider_.Init(); + PolicyServiceImpl::Providers providers; + providers.push_back(&provider_); + policy_service_.reset(new PolicyServiceImpl(providers)); + store_ = new ConfigurationPolicyPrefStore( + policy_service_.get(), &handler_list_, POLICY_LEVEL_MANDATORY); +} - virtual void TearDown() OVERRIDE { - provider_.Shutdown(); - } +ConfigurationPolicyPrefStoreTest::~ConfigurationPolicyPrefStoreTest() {} - void UpdateProviderPolicy(const PolicyMap& policy) { - provider_.UpdateChromePolicy(policy); - base::RunLoop loop; - loop.RunUntilIdle(); - } +void ConfigurationPolicyPrefStoreTest::TearDown() { + provider_.Shutdown(); +} - ConfigurationPolicyHandlerList handler_list_; - MockConfigurationPolicyProvider provider_; - scoped_ptr<PolicyServiceImpl> policy_service_; - scoped_refptr<ConfigurationPolicyPrefStore> store_; - base::MessageLoop loop_; -}; +void ConfigurationPolicyPrefStoreTest::UpdateProviderPolicy( + const PolicyMap& policy) { + provider_.UpdateChromePolicy(policy); + base::RunLoop loop; + loop.RunUntilIdle(); +} // Test cases for list-valued policy settings. class ConfigurationPolicyPrefStoreListTest @@ -622,287 +612,6 @@ TEST_F(ConfigurationPolicyPrefStoreProxyTest, ProxyInvalid) { } } -class ConfigurationPolicyPrefStoreDefaultSearchTest - : public ConfigurationPolicyPrefStoreTest { - public: - ConfigurationPolicyPrefStoreDefaultSearchTest() { - default_alternate_urls_.AppendString( - "http://www.google.com/#q={searchTerms}"); - default_alternate_urls_.AppendString( - "http://www.google.com/search#q={searchTerms}"); - } - - protected: - static const char* const kSearchURL; - static const char* const kSuggestURL; - static const char* const kIconURL; - static const char* const kName; - static const char* const kKeyword; - static const char* const kReplacementKey; - static const char* const kImageURL; - static const char* const kImageParams; - static const char* const kNewTabURL; - - // Build a default search policy by setting search-related keys in |policy| to - // reasonable values. You can update any of the keys after calling this - // method. - void BuildDefaultSearchPolicy(PolicyMap* policy); - - base::ListValue default_alternate_urls_; -}; - -const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kSearchURL = - "http://test.com/search?t={searchTerms}"; -const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kSuggestURL = - "http://test.com/sugg?={searchTerms}"; -const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kIconURL = - "http://test.com/icon.jpg"; -const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kName = - "MyName"; -const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kKeyword = - "MyKeyword"; -const char* const - ConfigurationPolicyPrefStoreDefaultSearchTest::kReplacementKey = "espv"; -const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kImageURL = - "http://test.com/searchbyimage/upload"; -const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kImageParams = - "image_content=content,image_url=http://test.com/test.png"; -const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kNewTabURL = - "http://test.com/newtab"; - -void ConfigurationPolicyPrefStoreDefaultSearchTest:: - BuildDefaultSearchPolicy(PolicyMap* policy) { - base::ListValue* encodings = new base::ListValue(); - encodings->AppendString("UTF-16"); - encodings->AppendString("UTF-8"); - policy->Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); - policy->Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, base::Value::CreateStringValue(kSearchURL), - NULL); - policy->Set(key::kDefaultSearchProviderName, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, base::Value::CreateStringValue(kName), NULL); - policy->Set(key::kDefaultSearchProviderKeyword, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, base::Value::CreateStringValue(kKeyword), - NULL); - policy->Set(key::kDefaultSearchProviderSuggestURL, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, base::Value::CreateStringValue(kSuggestURL), - NULL); - policy->Set(key::kDefaultSearchProviderIconURL, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, base::Value::CreateStringValue(kIconURL), - NULL); - policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, encodings, NULL); - policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, default_alternate_urls_.DeepCopy(), NULL); - policy->Set(key::kDefaultSearchProviderSearchTermsReplacementKey, - POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateStringValue(kReplacementKey), NULL); - policy->Set(key::kDefaultSearchProviderImageURL, - POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateStringValue(kImageURL), NULL); - policy->Set(key::kDefaultSearchProviderImageURLPostParams, - POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateStringValue(kImageParams), NULL); - policy->Set(key::kDefaultSearchProviderNewTabURL, - POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateStringValue(kNewTabURL), NULL); -} - -// Checks that if the policy for default search is valid, i.e. there's a -// search URL, that all the elements have been given proper defaults. -TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, MinimallyDefined) { - PolicyMap policy; - policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); - policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, base::Value::CreateStringValue(kSearchURL), - NULL); - UpdateProviderPolicy(policy); - - const base::Value* value = NULL; - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, &value)); - EXPECT_TRUE(base::StringValue(kSearchURL).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderName, &value)); - EXPECT_TRUE(base::StringValue("test.com").Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, &value)); - EXPECT_TRUE(base::StringValue("test.com").Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, - &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderInstantURL, - &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderAlternateURLs, - &value)); - EXPECT_TRUE(base::ListValue().Equals(value)); - - EXPECT_TRUE( - store_->GetValue(prefs::kDefaultSearchProviderSearchTermsReplacementKey, - &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue( - prefs::kDefaultSearchProviderSearchURLPostParams, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue( - prefs::kDefaultSearchProviderSuggestURLPostParams, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue( - prefs::kDefaultSearchProviderInstantURLPostParams, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue( - prefs::kDefaultSearchProviderImageURLPostParams, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue( - prefs::kDefaultSearchProviderNewTabURL, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); -} - -// Checks that for a fully defined search policy, all elements have been -// read properly. -TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, FullyDefined) { - PolicyMap policy; - BuildDefaultSearchPolicy(&policy); - UpdateProviderPolicy(policy); - - const base::Value* value = NULL; - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, &value)); - EXPECT_TRUE(base::StringValue(kSearchURL).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderName, &value)); - EXPECT_TRUE(base::StringValue(kName).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, &value)); - EXPECT_TRUE(base::StringValue(kKeyword).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, - &value)); - EXPECT_TRUE(base::StringValue(kSuggestURL).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, &value)); - EXPECT_TRUE(base::StringValue(kIconURL).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, &value)); - EXPECT_TRUE(base::StringValue("UTF-16;UTF-8").Equals(value)); - - EXPECT_TRUE(store_->GetValue( - prefs::kDefaultSearchProviderAlternateURLs, &value)); - EXPECT_TRUE(default_alternate_urls_.Equals(value)); - - EXPECT_TRUE( - store_->GetValue(prefs::kDefaultSearchProviderSearchTermsReplacementKey, - &value)); - EXPECT_TRUE(base::StringValue(kReplacementKey).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, &value)); - EXPECT_TRUE(base::StringValue(std::string(kImageURL)).Equals(value)); - - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderImageURLPostParams, - &value)); - EXPECT_TRUE(base::StringValue(std::string(kImageParams)).Equals(value)); - - EXPECT_TRUE(store_->GetValue( - prefs::kDefaultSearchProviderSearchURLPostParams, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue( - prefs::kDefaultSearchProviderSuggestURLPostParams, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); - - EXPECT_TRUE(store_->GetValue( - prefs::kDefaultSearchProviderInstantURLPostParams, &value)); - EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); -} - -// Checks that if the default search policy is missing, that no elements of the -// default search policy will be present. -TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, MissingUrl) { - PolicyMap policy; - BuildDefaultSearchPolicy(&policy); - policy.Erase(key::kDefaultSearchProviderSearchURL); - UpdateProviderPolicy(policy); - - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderName, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderAlternateURLs, - NULL)); - EXPECT_FALSE(store_->GetValue( - prefs::kDefaultSearchProviderSearchTermsReplacementKey, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, NULL)); - EXPECT_FALSE(store_->GetValue( - prefs::kDefaultSearchProviderImageURLPostParams, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderInstantURL, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderNewTabURL, NULL)); -} - -// Checks that if the default search policy is invalid, that no elements of the -// default search policy will be present. -TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, Invalid) { - PolicyMap policy; - BuildDefaultSearchPolicy(&policy); - const char* const bad_search_url = "http://test.com/noSearchTerms"; - policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, - base::Value::CreateStringValue(bad_search_url), NULL); - UpdateProviderPolicy(policy); - - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderName, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderAlternateURLs, - NULL)); - EXPECT_FALSE(store_->GetValue( - prefs::kDefaultSearchProviderSearchTermsReplacementKey, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, NULL)); - EXPECT_FALSE(store_->GetValue( - prefs::kDefaultSearchProviderImageURLPostParams, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderInstantURL, NULL)); - EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderNewTabURL, NULL)); -} - -// Checks that if the default search policy is invalid, that no elements of the -// default search policy will be present. -TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, Disabled) { - PolicyMap policy; - policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, - POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL); - UpdateProviderPolicy(policy); - - const base::Value* value = NULL; - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderEnabled, &value)); - base::FundamentalValue expected_enabled(false); - EXPECT_TRUE(base::Value::Equals(&expected_enabled, value)); - EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, &value)); - base::StringValue expected_search_url((std::string())); - EXPECT_TRUE(base::Value::Equals(&expected_search_url, value)); -} - // Tests Incognito mode availability preference setting. class ConfigurationPolicyPrefStoreIncognitoModeTest : public ConfigurationPolicyPrefStoreTest { diff --git a/chrome/browser/policy/configuration_policy_pref_store_unittest.h b/chrome/browser/policy/configuration_policy_pref_store_unittest.h new file mode 100644 index 0000000..9fdf866 --- /dev/null +++ b/chrome/browser/policy/configuration_policy_pref_store_unittest.h @@ -0,0 +1,40 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PREF_STORE_UNITTEST_H_ +#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PREF_STORE_UNITTEST_H_ + +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "base/message_loop/message_loop.h" +#include "chrome/browser/policy/configuration_policy_handler_list.h" +#include "chrome/browser/policy/mock_configuration_policy_provider.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace policy { + +class PolicyMap; +class PolicyService; +class ConfigurationPolicyPrefStore; + +class ConfigurationPolicyPrefStoreTest : public testing::Test { + protected: + ConfigurationPolicyPrefStoreTest(); + virtual ~ConfigurationPolicyPrefStoreTest(); + virtual void TearDown() OVERRIDE; + void UpdateProviderPolicy(const PolicyMap& policy); + + ConfigurationPolicyHandlerList handler_list_; + MockConfigurationPolicyProvider provider_; + scoped_ptr<PolicyService> policy_service_; + scoped_refptr<ConfigurationPolicyPrefStore> store_; + base::MessageLoop loop_; + + private: + DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyPrefStoreTest); +}; + +} // namespace policy + +#endif // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PREF_STORE_UNITTEST_H_ diff --git a/chrome/browser/search_engines/OWNERS b/chrome/browser/search_engines/OWNERS index dd79a14..09c6759 100644 --- a/chrome/browser/search_engines/OWNERS +++ b/chrome/browser/search_engines/OWNERS @@ -2,4 +2,7 @@ estade@chromium.org pkasting@chromium.org stevet@chromium.org -per-file *_android.*=yfriedman@chromium.org
\ No newline at end of file +per-file *_android.*=yfriedman@chromium.org + +per-file default_search_policy_handler*=joaodasilva@chromium.org +per-file default_search_policy_handler*=dconnelly@chromium.org diff --git a/chrome/browser/search_engines/default_search_policy_handler.cc b/chrome/browser/search_engines/default_search_policy_handler.cc new file mode 100644 index 0000000..5a5e190 --- /dev/null +++ b/chrome/browser/search_engines/default_search_policy_handler.cc @@ -0,0 +1,323 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/search_engines/default_search_policy_handler.h" + +#include "base/prefs/pref_value_map.h" +#include "base/stl_util.h" +#include "base/strings/string_util.h" +#include "chrome/browser/chrome_notification_types.h" +#include "chrome/browser/policy/policy_error_map.h" +#include "chrome/browser/policy/policy_map.h" +#include "chrome/browser/search_engines/search_terms_data.h" +#include "chrome/browser/search_engines/template_url.h" +#include "chrome/common/pref_names.h" +#include "content/public/browser/notification_service.h" +#include "grit/generated_resources.h" +#include "policy/policy_constants.h" + +namespace policy { + +// List of policy types to preference names, for policies affecting the default +// search provider. +const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { + { key::kDefaultSearchProviderEnabled, + prefs::kDefaultSearchProviderEnabled, + Value::TYPE_BOOLEAN }, + { key::kDefaultSearchProviderName, + prefs::kDefaultSearchProviderName, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderKeyword, + prefs::kDefaultSearchProviderKeyword, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderSearchURL, + prefs::kDefaultSearchProviderSearchURL, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderSuggestURL, + prefs::kDefaultSearchProviderSuggestURL, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderInstantURL, + prefs::kDefaultSearchProviderInstantURL, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderIconURL, + prefs::kDefaultSearchProviderIconURL, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderEncodings, + prefs::kDefaultSearchProviderEncodings, + Value::TYPE_LIST }, + { key::kDefaultSearchProviderAlternateURLs, + prefs::kDefaultSearchProviderAlternateURLs, + Value::TYPE_LIST }, + { key::kDefaultSearchProviderSearchTermsReplacementKey, + prefs::kDefaultSearchProviderSearchTermsReplacementKey, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderImageURL, + prefs::kDefaultSearchProviderImageURL, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderNewTabURL, + prefs::kDefaultSearchProviderNewTabURL, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderSearchURLPostParams, + prefs::kDefaultSearchProviderSearchURLPostParams, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderSuggestURLPostParams, + prefs::kDefaultSearchProviderSuggestURLPostParams, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderInstantURLPostParams, + prefs::kDefaultSearchProviderInstantURLPostParams, + Value::TYPE_STRING }, + { key::kDefaultSearchProviderImageURLPostParams, + prefs::kDefaultSearchProviderImageURLPostParams, + Value::TYPE_STRING }, +}; + +// DefaultSearchEncodingsPolicyHandler implementation -------------------------- + +DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler() + : TypeCheckingPolicyHandler(key::kDefaultSearchProviderEncodings, + Value::TYPE_LIST) {} + +DefaultSearchEncodingsPolicyHandler::~DefaultSearchEncodingsPolicyHandler() { +} + +void DefaultSearchEncodingsPolicyHandler::ApplyPolicySettings( + const PolicyMap& policies, PrefValueMap* prefs) { + // The DefaultSearchProviderEncodings policy has type list, but the related + // preference has type string. Convert one into the other here, using + // ';' as a separator. + const Value* value = policies.GetValue(policy_name()); + const ListValue* list; + if (!value || !value->GetAsList(&list)) + return; + + ListValue::const_iterator iter(list->begin()); + ListValue::const_iterator end(list->end()); + std::vector<std::string> string_parts; + for (; iter != end; ++iter) { + std::string s; + if ((*iter)->GetAsString(&s)) { + string_parts.push_back(s); + } + } + std::string encodings = JoinString(string_parts, ';'); + prefs->SetValue(prefs::kDefaultSearchProviderEncodings, + Value::CreateStringValue(encodings)); +} + + +// DefaultSearchPolicyHandler implementation ----------------------------------- + +DefaultSearchPolicyHandler::DefaultSearchPolicyHandler() { + for (size_t i = 0; i < arraysize(kDefaultSearchPolicyMap); ++i) { + const char* policy_name = kDefaultSearchPolicyMap[i].policy_name; + if (policy_name == key::kDefaultSearchProviderEncodings) { + handlers_.push_back(new DefaultSearchEncodingsPolicyHandler()); + } else { + handlers_.push_back(new SimplePolicyHandler( + policy_name, + kDefaultSearchPolicyMap[i].preference_path, + kDefaultSearchPolicyMap[i].value_type)); + } + } +} + +DefaultSearchPolicyHandler::~DefaultSearchPolicyHandler() { + STLDeleteElements(&handlers_); +} + +bool DefaultSearchPolicyHandler::CheckPolicySettings(const PolicyMap& policies, + PolicyErrorMap* errors) { + if (!CheckIndividualPolicies(policies, errors)) + return false; + + if (DefaultSearchProviderIsDisabled(policies)) { + // Add an error for all specified default search policies except + // DefaultSearchProviderEnabled. + + for (std::vector<TypeCheckingPolicyHandler*>::const_iterator handler = + handlers_.begin(); + handler != handlers_.end(); ++handler) { + const char* policy_name = (*handler)->policy_name(); + if (policy_name != key::kDefaultSearchProviderEnabled && + HasDefaultSearchPolicy(policies, policy_name)) { + errors->AddError(policy_name, IDS_POLICY_DEFAULT_SEARCH_DISABLED); + } + } + return true; + } + + const Value* url; + std::string dummy; + if (DefaultSearchURLIsValid(policies, &url, &dummy) || + !AnyDefaultSearchPoliciesSpecified(policies)) + return true; + errors->AddError(key::kDefaultSearchProviderSearchURL, url ? + IDS_POLICY_INVALID_SEARCH_URL_ERROR : IDS_POLICY_NOT_SPECIFIED_ERROR); + return false; +} + +void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies, + PrefValueMap* prefs) { + if (DefaultSearchProviderIsDisabled(policies)) { + prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, false); + + // If default search is disabled, the other fields are ignored. + prefs->SetString(prefs::kDefaultSearchProviderName, std::string()); + prefs->SetString(prefs::kDefaultSearchProviderSearchURL, std::string()); + prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, std::string()); + prefs->SetString(prefs::kDefaultSearchProviderIconURL, std::string()); + prefs->SetString(prefs::kDefaultSearchProviderEncodings, std::string()); + prefs->SetString(prefs::kDefaultSearchProviderKeyword, std::string()); + prefs->SetString(prefs::kDefaultSearchProviderInstantURL, std::string()); + prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, std::string()); + prefs->SetValue(prefs::kDefaultSearchProviderAlternateURLs, + new ListValue()); + prefs->SetString( + prefs::kDefaultSearchProviderSearchTermsReplacementKey, std::string()); + prefs->SetString(prefs::kDefaultSearchProviderImageURL, std::string()); + prefs->SetString( + prefs::kDefaultSearchProviderSearchURLPostParams, std::string()); + prefs->SetString( + prefs::kDefaultSearchProviderSuggestURLPostParams, std::string()); + prefs->SetString( + prefs::kDefaultSearchProviderInstantURLPostParams, std::string()); + prefs->SetString( + prefs::kDefaultSearchProviderImageURLPostParams, std::string()); + } 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 Value* dummy; + std::string url; + if (DefaultSearchURLIsValid(policies, &dummy, &url)) { + + for (std::vector<TypeCheckingPolicyHandler*>::const_iterator handler = + handlers_.begin(); + handler != handlers_.end(); ++handler) { + (*handler)->ApplyPolicySettings(policies, prefs); + } + + EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderSuggestURL); + EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderIconURL); + EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderEncodings); + EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderKeyword); + EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderInstantURL); + EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderNewTabURL); + EnsureListPrefExists(prefs, prefs::kDefaultSearchProviderAlternateURLs); + EnsureStringPrefExists( + prefs, + prefs::kDefaultSearchProviderSearchTermsReplacementKey); + EnsureStringPrefExists(prefs, prefs::kDefaultSearchProviderImageURL); + EnsureStringPrefExists( + prefs, + prefs::kDefaultSearchProviderSearchURLPostParams); + EnsureStringPrefExists( + prefs, + prefs::kDefaultSearchProviderSuggestURLPostParams); + EnsureStringPrefExists( + prefs, + prefs::kDefaultSearchProviderInstantURLPostParams); + EnsureStringPrefExists( + prefs, + prefs::kDefaultSearchProviderImageURLPostParams); + + // 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; + std::string host(GURL(url).host()); + if (host.empty()) + host = "_"; + if (!prefs->GetString(prefs::kDefaultSearchProviderName, &name) || + name.empty()) { + prefs->SetString(prefs::kDefaultSearchProviderName, host); + } + if (!prefs->GetString(prefs::kDefaultSearchProviderKeyword, &keyword) || + keyword.empty()) { + prefs->SetString(prefs::kDefaultSearchProviderKeyword, host); + } + + // And clear the IDs since these are not specified via policy. + prefs->SetString(prefs::kDefaultSearchProviderID, std::string()); + prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, + std::string()); + } + } + content::NotificationService::current()->Notify( + chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED, + content::NotificationService::AllSources(), + content::NotificationService::NoDetails()); +} + +bool DefaultSearchPolicyHandler::CheckIndividualPolicies( + const PolicyMap& policies, + PolicyErrorMap* errors) { + for (std::vector<TypeCheckingPolicyHandler*>::const_iterator handler = + handlers_.begin(); + handler != handlers_.end(); ++handler) { + if (!(*handler)->CheckPolicySettings(policies, errors)) + return false; + } + return true; +} + +bool DefaultSearchPolicyHandler::HasDefaultSearchPolicy( + const PolicyMap& policies, + const char* policy_name) { + return policies.Get(policy_name) != NULL; +} + +bool DefaultSearchPolicyHandler::AnyDefaultSearchPoliciesSpecified( + const PolicyMap& policies) { + for (std::vector<TypeCheckingPolicyHandler*>::const_iterator handler = + handlers_.begin(); + handler != handlers_.end(); ++handler) { + if (policies.Get((*handler)->policy_name())) + return true; + } + return false; +} + +bool DefaultSearchPolicyHandler::DefaultSearchProviderIsDisabled( + const PolicyMap& policies) { + const Value* provider_enabled = + policies.GetValue(key::kDefaultSearchProviderEnabled); + bool enabled = true; + return provider_enabled && provider_enabled->GetAsBoolean(&enabled) && + !enabled; +} + +bool DefaultSearchPolicyHandler::DefaultSearchURLIsValid( + const PolicyMap& policies, + const Value** url_value, + std::string* url_string) { + *url_value = policies.GetValue(key::kDefaultSearchProviderSearchURL); + if (!*url_value || !(*url_value)->GetAsString(url_string) || + url_string->empty()) + return false; + TemplateURLData data; + data.SetURL(*url_string); + SearchTermsData search_terms_data; + return TemplateURL(NULL, data).SupportsReplacementUsingTermsData( + search_terms_data); +} + +void DefaultSearchPolicyHandler::EnsureStringPrefExists( + PrefValueMap* prefs, + const std::string& path) { + std::string value; + if (!prefs->GetString(path, &value)) + prefs->SetString(path, value); +} + +void DefaultSearchPolicyHandler::EnsureListPrefExists( + PrefValueMap* prefs, + const std::string& path) { + base::Value* value; + base::ListValue* list_value; + if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) + prefs->SetValue(path, new ListValue()); +} + +} // namespace policy diff --git a/chrome/browser/search_engines/default_search_policy_handler.h b/chrome/browser/search_engines/default_search_policy_handler.h new file mode 100644 index 0000000..c0fe715 --- /dev/null +++ b/chrome/browser/search_engines/default_search_policy_handler.h @@ -0,0 +1,79 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_POLICY_HANDLER_H_ +#define CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_POLICY_HANDLER_H_ + +#include <vector> + +#include "chrome/browser/policy/configuration_policy_handler.h" + +namespace policy { + +// ConfigurationPolicyHandler for the DefaultSearchEncodings policy. +class DefaultSearchEncodingsPolicyHandler + : public TypeCheckingPolicyHandler { + public: + DefaultSearchEncodingsPolicyHandler(); + virtual ~DefaultSearchEncodingsPolicyHandler(); + + // ConfigurationPolicyHandler methods: + virtual void ApplyPolicySettings(const PolicyMap& policies, + PrefValueMap* prefs) OVERRIDE; + + private: + DISALLOW_COPY_AND_ASSIGN(DefaultSearchEncodingsPolicyHandler); +}; + +// ConfigurationPolicyHandler for the default search policies. +class DefaultSearchPolicyHandler : public ConfigurationPolicyHandler { + public: + DefaultSearchPolicyHandler(); + virtual ~DefaultSearchPolicyHandler(); + + // ConfigurationPolicyHandler methods: + virtual bool CheckPolicySettings(const PolicyMap& policies, + PolicyErrorMap* errors) OVERRIDE; + virtual void ApplyPolicySettings(const PolicyMap& policies, + PrefValueMap* prefs) OVERRIDE; + + private: + // Calls |CheckPolicySettings()| on each of the handlers in |handlers_| + // and returns whether all of the calls succeeded. + bool CheckIndividualPolicies(const PolicyMap& policies, + PolicyErrorMap* errors); + + // Returns whether there is a value for |policy_name| in |policies|. + bool HasDefaultSearchPolicy(const PolicyMap& policies, + const char* policy_name); + + // Returns whether any default search policies are specified in |policies|. + bool AnyDefaultSearchPoliciesSpecified(const PolicyMap& policies); + + // Returns whether the default search provider is disabled. + bool DefaultSearchProviderIsDisabled(const PolicyMap& policies); + + // Returns whether the default search URL is set and valid. On success, both + // outparams (which must be non-NULL) are filled with the search URL. + bool DefaultSearchURLIsValid(const PolicyMap& policies, + const Value** url_value, + std::string* url_string); + + // Make sure that the |path| is present in |prefs_|. If not, set it to + // a blank string. + void EnsureStringPrefExists(PrefValueMap* prefs, const std::string& path); + + // Make sure that the |path| is present in |prefs_| and is a ListValue. If + // not, set it to an empty list. + void EnsureListPrefExists(PrefValueMap* prefs, const std::string& path); + + // The ConfigurationPolicyHandler handlers for each default search policy. + std::vector<TypeCheckingPolicyHandler*> handlers_; + + DISALLOW_COPY_AND_ASSIGN(DefaultSearchPolicyHandler); +}; + +} // namespace policy + +#endif // CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_POLICY_HANDLER_H_ diff --git a/chrome/browser/search_engines/default_search_policy_handler_unittest.cc b/chrome/browser/search_engines/default_search_policy_handler_unittest.cc new file mode 100644 index 0000000..ca88043 --- /dev/null +++ b/chrome/browser/search_engines/default_search_policy_handler_unittest.cc @@ -0,0 +1,294 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/policy/configuration_policy_pref_store.h" +#include "chrome/browser/policy/configuration_policy_pref_store_unittest.h" +#include "chrome/browser/search_engines/default_search_policy_handler.h" +#include "chrome/common/pref_names.h" +#include "policy/policy_constants.h" + +namespace policy { + +class DefaultSearchPolicyHandlerTest + : public ConfigurationPolicyPrefStoreTest { + public: + DefaultSearchPolicyHandlerTest() { + default_alternate_urls_.AppendString( + "http://www.google.com/#q={searchTerms}"); + default_alternate_urls_.AppendString( + "http://www.google.com/search#q={searchTerms}"); + } + + protected: + static const char kSearchURL[]; + static const char kSuggestURL[]; + static const char kIconURL[]; + static const char kName[]; + static const char kKeyword[]; + static const char kReplacementKey[]; + static const char kImageURL[]; + static const char kImageParams[]; + static const char kNewTabURL[]; + + // Build a default search policy by setting search-related keys in |policy| to + // reasonable values. You can update any of the keys after calling this + // method. + void BuildDefaultSearchPolicy(PolicyMap* policy); + + base::ListValue default_alternate_urls_; +}; + +const char DefaultSearchPolicyHandlerTest::kSearchURL[] = + "http://test.com/search?t={searchTerms}"; +const char DefaultSearchPolicyHandlerTest::kSuggestURL[] = + "http://test.com/sugg?={searchTerms}"; +const char DefaultSearchPolicyHandlerTest::kIconURL[] = + "http://test.com/icon.jpg"; +const char DefaultSearchPolicyHandlerTest::kName[] = + "MyName"; +const char DefaultSearchPolicyHandlerTest::kKeyword[] = + "MyKeyword"; +const char DefaultSearchPolicyHandlerTest::kReplacementKey[] = + "espv"; +const char DefaultSearchPolicyHandlerTest::kImageURL[] = + "http://test.com/searchbyimage/upload"; +const char DefaultSearchPolicyHandlerTest::kImageParams[] = + "image_content=content,image_url=http://test.com/test.png"; +const char DefaultSearchPolicyHandlerTest::kNewTabURL[] = + "http://test.com/newtab"; + +void DefaultSearchPolicyHandlerTest:: + BuildDefaultSearchPolicy(PolicyMap* policy) { + base::ListValue* encodings = new base::ListValue(); + encodings->AppendString("UTF-16"); + encodings->AppendString("UTF-8"); + policy->Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); + policy->Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, base::Value::CreateStringValue(kSearchURL), + NULL); + policy->Set(key::kDefaultSearchProviderName, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, base::Value::CreateStringValue(kName), NULL); + policy->Set(key::kDefaultSearchProviderKeyword, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, base::Value::CreateStringValue(kKeyword), + NULL); + policy->Set(key::kDefaultSearchProviderSuggestURL, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, base::Value::CreateStringValue(kSuggestURL), + NULL); + policy->Set(key::kDefaultSearchProviderIconURL, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, base::Value::CreateStringValue(kIconURL), + NULL); + policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, encodings, NULL); + policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, default_alternate_urls_.DeepCopy(), NULL); + policy->Set(key::kDefaultSearchProviderSearchTermsReplacementKey, + POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, + base::Value::CreateStringValue(kReplacementKey), NULL); + policy->Set(key::kDefaultSearchProviderImageURL, + POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, + base::Value::CreateStringValue(kImageURL), NULL); + policy->Set(key::kDefaultSearchProviderImageURLPostParams, + POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, + base::Value::CreateStringValue(kImageParams), NULL); + policy->Set(key::kDefaultSearchProviderNewTabURL, + POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, + base::Value::CreateStringValue(kNewTabURL), NULL); +} + +// Checks that if the policy for default search is valid, i.e. there's a +// search URL, that all the elements have been given proper defaults. +TEST_F(DefaultSearchPolicyHandlerTest, MinimallyDefined) { + PolicyMap policy; + policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); + policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, base::Value::CreateStringValue(kSearchURL), + NULL); + UpdateProviderPolicy(policy); + + const base::Value* value = NULL; + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, &value)); + EXPECT_TRUE(base::StringValue(kSearchURL).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderName, &value)); + EXPECT_TRUE(base::StringValue("test.com").Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, &value)); + EXPECT_TRUE(base::StringValue("test.com").Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, + &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderInstantURL, + &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderAlternateURLs, + &value)); + EXPECT_TRUE(base::ListValue().Equals(value)); + + EXPECT_TRUE( + store_->GetValue(prefs::kDefaultSearchProviderSearchTermsReplacementKey, + &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue( + prefs::kDefaultSearchProviderSearchURLPostParams, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue( + prefs::kDefaultSearchProviderSuggestURLPostParams, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue( + prefs::kDefaultSearchProviderInstantURLPostParams, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue( + prefs::kDefaultSearchProviderImageURLPostParams, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue( + prefs::kDefaultSearchProviderNewTabURL, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); +} + +// Checks that for a fully defined search policy, all elements have been +// read properly. +TEST_F(DefaultSearchPolicyHandlerTest, FullyDefined) { + PolicyMap policy; + BuildDefaultSearchPolicy(&policy); + UpdateProviderPolicy(policy); + + const base::Value* value = NULL; + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, &value)); + EXPECT_TRUE(base::StringValue(kSearchURL).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderName, &value)); + EXPECT_TRUE(base::StringValue(kName).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, &value)); + EXPECT_TRUE(base::StringValue(kKeyword).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, + &value)); + EXPECT_TRUE(base::StringValue(kSuggestURL).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, &value)); + EXPECT_TRUE(base::StringValue(kIconURL).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, &value)); + EXPECT_TRUE(base::StringValue("UTF-16;UTF-8").Equals(value)); + + EXPECT_TRUE(store_->GetValue( + prefs::kDefaultSearchProviderAlternateURLs, &value)); + EXPECT_TRUE(default_alternate_urls_.Equals(value)); + + EXPECT_TRUE( + store_->GetValue(prefs::kDefaultSearchProviderSearchTermsReplacementKey, + &value)); + EXPECT_TRUE(base::StringValue(kReplacementKey).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, &value)); + EXPECT_TRUE(base::StringValue(std::string(kImageURL)).Equals(value)); + + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderImageURLPostParams, + &value)); + EXPECT_TRUE(base::StringValue(std::string(kImageParams)).Equals(value)); + + EXPECT_TRUE(store_->GetValue( + prefs::kDefaultSearchProviderSearchURLPostParams, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue( + prefs::kDefaultSearchProviderSuggestURLPostParams, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); + + EXPECT_TRUE(store_->GetValue( + prefs::kDefaultSearchProviderInstantURLPostParams, &value)); + EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); +} + +// Checks that if the default search policy is missing, that no elements of the +// default search policy will be present. +TEST_F(DefaultSearchPolicyHandlerTest, MissingUrl) { + PolicyMap policy; + BuildDefaultSearchPolicy(&policy); + policy.Erase(key::kDefaultSearchProviderSearchURL); + UpdateProviderPolicy(policy); + + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderName, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderAlternateURLs, + NULL)); + EXPECT_FALSE(store_->GetValue( + prefs::kDefaultSearchProviderSearchTermsReplacementKey, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, NULL)); + EXPECT_FALSE(store_->GetValue( + prefs::kDefaultSearchProviderImageURLPostParams, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderInstantURL, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderNewTabURL, NULL)); +} + +// Checks that if the default search policy is invalid, that no elements of the +// default search policy will be present. +TEST_F(DefaultSearchPolicyHandlerTest, Invalid) { + PolicyMap policy; + BuildDefaultSearchPolicy(&policy); + const char bad_search_url[] = "http://test.com/noSearchTerms"; + policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, + base::Value::CreateStringValue(bad_search_url), NULL); + UpdateProviderPolicy(policy); + + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderName, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderAlternateURLs, + NULL)); + EXPECT_FALSE(store_->GetValue( + prefs::kDefaultSearchProviderSearchTermsReplacementKey, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, NULL)); + EXPECT_FALSE(store_->GetValue( + prefs::kDefaultSearchProviderImageURLPostParams, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderInstantURL, NULL)); + EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderNewTabURL, NULL)); +} + +// Checks that if the default search policy is invalid, that no elements of the +// default search policy will be present. +TEST_F(DefaultSearchPolicyHandlerTest, Disabled) { + PolicyMap policy; + policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL); + UpdateProviderPolicy(policy); + + const base::Value* value = NULL; + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderEnabled, &value)); + base::FundamentalValue expected_enabled(false); + EXPECT_TRUE(base::Value::Equals(&expected_enabled, value)); + EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, &value)); + base::StringValue expected_search_url((std::string())); + EXPECT_TRUE(base::Value::Equals(&expected_search_url, value)); +} + +} // namespace policy diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 0542b6a..1205eba 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1926,6 +1926,8 @@ 'browser/search/search.h', 'browser/search/search_terms_tracker.cc', 'browser/search/search_terms_tracker.h', + 'browser/search_engines/default_search_policy_handler.cc', + 'browser/search_engines/default_search_policy_handler.h', 'browser/search_engines/search_engine_type.h', 'browser/search_engines/search_host_to_urls_map.cc', 'browser/search_engines/search_host_to_urls_map.h', diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index f14e6e4..f622204 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -1082,8 +1082,8 @@ 'browser/policy/cloud/user_policy_signin_service_unittest.cc', 'browser/policy/config_dir_policy_loader_unittest.cc', 'browser/policy/configuration_policy_handler_unittest.cc', - 'browser/policy/configuration_policy_handler_list_unittest.cc', 'browser/policy/configuration_policy_pref_store_unittest.cc', + 'browser/policy/configuration_policy_pref_store_unittest.h', 'browser/policy/configuration_policy_provider_test.cc', 'browser/policy/configuration_policy_provider_test.h', 'browser/policy/mock_policy_service.cc', @@ -1186,6 +1186,7 @@ 'browser/search/instant_unittest_base.h', 'browser/search/most_visited_iframe_source_unittest.cc', 'browser/search/search_unittest.cc', + 'browser/search_engines/default_search_policy_handler_unittest.cc', 'browser/search_engines/search_host_to_urls_map_unittest.cc', 'browser/search_engines/search_provider_install_data_unittest.cc', 'browser/search_engines/template_url_fetcher_unittest.cc', |