diff options
-rw-r--r-- | chrome/browser/policy/configuration_policy_pref_store.cc | 163 | ||||
-rw-r--r-- | chrome/browser/policy/configuration_policy_pref_store.h | 22 | ||||
-rw-r--r-- | chrome/browser/policy/configuration_policy_pref_store_unittest.cc | 241 | ||||
-rw-r--r-- | chrome/browser/policy/configuration_policy_provider.cc | 12 | ||||
-rw-r--r-- | chrome/browser/policy/configuration_policy_store.h | 6 | ||||
-rw-r--r-- | chrome/browser/policy/mock_configuration_policy_provider.h | 3 | ||||
-rw-r--r-- | chrome/browser/policy/mock_configuration_policy_store.h | 3 | ||||
-rw-r--r-- | chrome/browser/prefs/pref_set_observer.cc | 15 | ||||
-rw-r--r-- | chrome/browser/prefs/pref_set_observer.h | 7 | ||||
-rw-r--r-- | chrome/common/policy_constants.cc | 10 | ||||
-rw-r--r-- | chrome/common/policy_constants.h | 6 | ||||
-rw-r--r-- | chrome/common/pref_names.cc | 13 | ||||
-rw-r--r-- | chrome/common/pref_names.h | 3 |
13 files changed, 451 insertions, 53 deletions
diff --git a/chrome/browser/policy/configuration_policy_pref_store.cc b/chrome/browser/policy/configuration_policy_pref_store.cc index 813b58a..421cdf7 100644 --- a/chrome/browser/policy/configuration_policy_pref_store.cc +++ b/chrome/browser/policy/configuration_policy_pref_store.cc @@ -21,6 +21,8 @@ #include "chrome/browser/policy/config_dir_policy_provider.h" #endif #include "chrome/browser/policy/dummy_configuration_policy_provider.h" +#include "chrome/browser/search_engines/search_terms_data.h" +#include "chrome/browser/search_engines/template_url.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/pref_names.h" @@ -124,6 +126,22 @@ const ConfigurationPolicyPrefStore::PolicyToPreferenceMapEntry }; const ConfigurationPolicyPrefStore::PolicyToPreferenceMapEntry + ConfigurationPolicyPrefStore::default_search_policy_map_[] = { + { Value::TYPE_STRING, kPolicyDefaultSearchProviderName, + prefs::kDefaultSearchProviderName }, + { Value::TYPE_STRING, kPolicyDefaultSearchProviderKeyword, + prefs::kDefaultSearchProviderKeyword }, + { Value::TYPE_STRING, kPolicyDefaultSearchProviderSearchURL, + prefs::kDefaultSearchProviderSearchURL }, + { Value::TYPE_STRING, kPolicyDefaultSearchProviderSuggestURL, + prefs::kDefaultSearchProviderSuggestURL }, + { Value::TYPE_STRING, kPolicyDefaultSearchProviderIconURL, + prefs::kDefaultSearchProviderIconURL }, + { Value::TYPE_STRING, kPolicyDefaultSearchProviderEncodings, + prefs::kDefaultSearchProviderEncodings }, +}; + +const ConfigurationPolicyPrefStore::PolicyToPreferenceMapEntry ConfigurationPolicyPrefStore::proxy_policy_map_[] = { { Value::TYPE_STRING, kPolicyProxyServer, prefs::kProxyServer }, { Value::TYPE_STRING, kPolicyProxyPacUrl, prefs::kProxyPacUrl }, @@ -186,8 +204,10 @@ PrefStore::PrefReadError ConfigurationPolicyPrefStore::ReadPrefs() { proxy_configuration_specified_ = false; command_line_proxy_settings_cleared_ = false; - return (provider_ == NULL || provider_->Provide(this)) ? - PrefStore::PREF_READ_ERROR_NONE : PrefStore::PREF_READ_ERROR_OTHER; + bool success = (provider_ == NULL || provider_->Provide(this)); + FinalizeDefaultSearchPolicySettings(); + return success ? PrefStore::PREF_READ_ERROR_NONE : + PrefStore::PREF_READ_ERROR_OTHER; } // static @@ -206,46 +226,52 @@ ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore() { manager->recommended_provider()); } +const ConfigurationPolicyPrefStore::PolicyToPreferenceMapEntry* +ConfigurationPolicyPrefStore::FindPolicyInMap(PolicyType policy, + const PolicyToPreferenceMapEntry* map, int table_size) { + for (int i = 0; i < table_size; ++i) { + if (map[i].policy_type == policy) + return map + i; + } + return NULL; +} + +bool ConfigurationPolicyPrefStore::RemovePreferencesOfMap( + const PolicyToPreferenceMapEntry* map, int table_size) { + bool found_one = false; + for (int i = 0; i < table_size; ++i) { + if (prefs_->Remove(map[i].preference_path, NULL)) + found_one = true; + } + return found_one; +} + bool ConfigurationPolicyPrefStore::ApplyProxyPolicy(PolicyType policy, Value* value) { bool result = false; bool warn_about_proxy_disable_config = false; bool warn_about_proxy_system_config = false; - const PolicyToPreferenceMapEntry* match_entry_ = NULL; - for (const PolicyToPreferenceMapEntry* current = proxy_policy_map_; - current != proxy_policy_map_ + arraysize(proxy_policy_map_); ++current) { - if (current->policy_type == policy) { - match_entry_ = current; - } - } + const PolicyToPreferenceMapEntry* match_entry = + FindPolicyInMap(policy, proxy_policy_map_, arraysize(proxy_policy_map_)); // When the first proxy-related policy is applied, ALL proxy-related // preferences that have been set by command-line switches must be // removed. Otherwise it's possible for a user to interfere with proxy // policy by using proxy-related switches that are related to, but not // identical, to the ones set through policy. - if ((match_entry_ || + if ((match_entry || policy == ConfigurationPolicyPrefStore::kPolicyProxyServerMode) && - !command_line_proxy_settings_cleared_) { - for (const PolicyToPreferenceMapEntry* i = proxy_policy_map_; - i != proxy_policy_map_ + arraysize(proxy_policy_map_); ++i) { - if (prefs_->Get(i->preference_path, NULL)) { - LOG(WARNING) << "proxy configuration options were specified on the" - << " command-line but will be ignored because an" - << " explicit proxy configuration has been specified" - << " through a centrally-administered policy."; - break; - } + !command_line_proxy_settings_cleared_) { + if (RemovePreferencesOfMap(proxy_policy_map_, + arraysize(proxy_policy_map_))) { + LOG(WARNING) << "proxy configuration options were specified on the" + " command-line but will be ignored because an" + " explicit proxy configuration has been specified" + " through a centrally-administered policy."; } - - // Now actually do the preference removal. - for (const PolicyToPreferenceMapEntry* current = proxy_policy_map_; - current != proxy_policy_map_ + arraysize(proxy_policy_map_); ++current) - prefs_->Remove(current->preference_path, NULL); prefs_->Remove(prefs::kNoProxyServer, NULL); prefs_->Remove(prefs::kProxyAutoDetect, NULL); - command_line_proxy_settings_cleared_ = true; } @@ -298,7 +324,7 @@ bool ConfigurationPolicyPrefStore::ApplyProxyPolicy(PolicyType policy, prefs_->Remove(current->preference_path, NULL); } } - } else if (match_entry_) { + } else if (match_entry) { // Determine if the applied proxy policy settings conflict and issue // a corresponding warning if they do. if (!proxy_configuration_specified_) { @@ -309,7 +335,7 @@ bool ConfigurationPolicyPrefStore::ApplyProxyPolicy(PolicyType policy, proxy_configuration_specified_ = true; } if (!use_system_proxy_ && !proxy_disabled_) { - prefs_->Set(match_entry_->preference_path, value); + prefs_->Set(match_entry->preference_path, value); // The ownership of value has been passed on to |prefs_|, // don't clean it up later. value = NULL; @@ -362,14 +388,13 @@ bool ConfigurationPolicyPrefStore::ApplyAutoFillPolicy(PolicyType policy, bool ConfigurationPolicyPrefStore::ApplyPolicyFromMap(PolicyType policy, Value* value, const PolicyToPreferenceMapEntry map[], int size) { - const PolicyToPreferenceMapEntry* end = map + size; - for (const PolicyToPreferenceMapEntry* current = map; - current != end; ++current) { - if (current->policy_type == policy) { - DCHECK(current->value_type == value->GetType()); - prefs_->Set(current->preference_path, value); - return true; - } + + const PolicyToPreferenceMapEntry* match_entry = + FindPolicyInMap(policy, map, size); + if (match_entry) { + DCHECK(match_entry->value_type == value->GetType()); + prefs_->Set(match_entry->preference_path, value); + return true; } return false; } @@ -384,6 +409,10 @@ void ConfigurationPolicyPrefStore::Apply(PolicyType policy, Value* value) { if (ApplyAutoFillPolicy(policy, value)) return; + if (ApplyPolicyFromMap(policy, value, default_search_policy_map_, + arraysize(default_search_policy_map_))) + return; + if (ApplyPolicyFromMap(policy, value, simple_policy_map_, arraysize(simple_policy_map_))) return; @@ -392,3 +421,65 @@ void ConfigurationPolicyPrefStore::Apply(PolicyType policy, Value* value) { NOTIMPLEMENTED(); delete value; } + +void ConfigurationPolicyPrefStore::EnsureStringPrefExists( + const std::string& path) { + std::string value; + if (!prefs_->GetString(path, &value)) + prefs_->SetString(path, value); +} + +// Implementation of SearchTermsData just for validation. +class SearchTermsDataForValidation : public SearchTermsData { + public: + SearchTermsDataForValidation() {} + + // Implementation of SearchTermsData. + virtual std::string GoogleBaseURLValue() const { + return "http://www.google.com/"; + } + virtual std::string GetApplicationLocale() const { + return "en"; + } +#if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) + virtual std::wstring GetRlzParameterValue() const { + return ""; + } +#endif + private: + DISALLOW_COPY_AND_ASSIGN(SearchTermsDataForValidation); +}; + +void ConfigurationPolicyPrefStore::FinalizeDefaultSearchPolicySettings() { + std::string search_url; + // The search URL is required. + if (prefs_->GetString(prefs::kDefaultSearchProviderSearchURL, &search_url) && + !search_url.empty()) { + SearchTermsDataForValidation search_terms_data; + TemplateURLRef search_url_ref(search_url, 0, 0); + // It must support replacement (which implies it is valid). + if (search_url_ref.SupportsReplacementUsingTermsData(search_terms_data)) { + // The other entries are optional. Just make sure that they are all + // specified via policy, so that we don't use regular prefs. + EnsureStringPrefExists(prefs::kDefaultSearchProviderSuggestURL); + EnsureStringPrefExists(prefs::kDefaultSearchProviderIconURL); + EnsureStringPrefExists(prefs::kDefaultSearchProviderEncodings); + EnsureStringPrefExists(prefs::kDefaultSearchProviderKeyword); + + // For the name, default to the host if not specified. + std::string name; + if (!prefs_->GetString(prefs::kDefaultSearchProviderName, &name)) + prefs_->SetString(prefs::kDefaultSearchProviderName, + search_url_ref.GetHost()); + + // And clear the IDs since these are not specified via policy. + prefs_->SetString(prefs::kDefaultSearchProviderID, ""); + prefs_->SetString(prefs::kDefaultSearchProviderPrepopulateID, ""); + return; + } + } + // Required entries are not there. Remove any related entries. + RemovePreferencesOfMap(default_search_policy_map_, + arraysize(default_search_policy_map_)); +} + diff --git a/chrome/browser/policy/configuration_policy_pref_store.h b/chrome/browser/policy/configuration_policy_pref_store.h index 2b56172..45d6eca 100644 --- a/chrome/browser/policy/configuration_policy_pref_store.h +++ b/chrome/browser/policy/configuration_policy_pref_store.h @@ -6,6 +6,8 @@ #define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PREF_STORE_H_ #pragma once +#include <string> + #include "base/basictypes.h" #include "base/gtest_prod_util.h" #include "base/scoped_ptr.h" @@ -52,6 +54,7 @@ class ConfigurationPolicyPrefStore : public PrefStore, static const PolicyToPreferenceMapEntry simple_policy_map_[]; static const PolicyToPreferenceMapEntry proxy_policy_map_[]; + static const PolicyToPreferenceMapEntry default_search_policy_map_[]; const CommandLine* command_line_; ConfigurationPolicyProvider* provider_; @@ -85,6 +88,15 @@ class ConfigurationPolicyPrefStore : public PrefStore, bool ApplyPolicyFromMap(PolicyType policy, Value* value, const PolicyToPreferenceMapEntry map[], int size); + // Returns the map entry that corresponds to |policy| in the map. + const PolicyToPreferenceMapEntry* FindPolicyInMap(PolicyType policy, + const PolicyToPreferenceMapEntry* map, int size); + + // Remove the preferences found in the map from |prefs_|. Returns true if + // any such preferences were found and removed. + bool RemovePreferencesOfMap(const PolicyToPreferenceMapEntry* map, + int table_size); + // Processes proxy-specific policies. Returns true if the specified policy // is a proxy-related policy. ApplyProxyPolicy assumes the ownership // of |value| in the case that the policy is proxy-specific. @@ -98,6 +110,16 @@ class ConfigurationPolicyPrefStore : public PrefStore, // handled and assumes ownership of |value| in that case. bool ApplyAutoFillPolicy(PolicyType policy, Value* value); + // Make sure that the |path| if present in |prefs_|. If not, set it to + // a blank string. + void EnsureStringPrefExists(const std::string& path); + + // If the required entries for default search are specified and valid, + // finalizes the policy-specified configuration by initializing the + // unspecified map entries. Otherwise wipes all default search related + // map entries from |prefs_|. + void FinalizeDefaultSearchPolicySettings(); + DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyPrefStore); }; diff --git a/chrome/browser/policy/configuration_policy_pref_store_unittest.cc b/chrome/browser/policy/configuration_policy_pref_store_unittest.cc index 1b9dca3..974d85f 100644 --- a/chrome/browser/policy/configuration_policy_pref_store_unittest.cc +++ b/chrome/browser/policy/configuration_policy_pref_store_unittest.cc @@ -201,6 +201,21 @@ TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyRestoreOnStartup) { ConfigurationPolicyPrefStore::kPolicyURLsToRestoreOnStartup); } +TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyDefaultSearchProvider) { + TestStringPolicy(prefs::kDefaultSearchProviderName, + ConfigurationPolicyPrefStore::kPolicyDefaultSearchProviderName); + TestStringPolicy(prefs::kDefaultSearchProviderKeyword, + ConfigurationPolicyPrefStore::kPolicyDefaultSearchProviderKeyword); + TestStringPolicy(prefs::kDefaultSearchProviderSearchURL, + ConfigurationPolicyPrefStore::kPolicyDefaultSearchProviderSearchURL); + TestStringPolicy(prefs::kDefaultSearchProviderSuggestURL, + ConfigurationPolicyPrefStore::kPolicyDefaultSearchProviderSuggestURL); + TestStringPolicy(prefs::kDefaultSearchProviderIconURL, + ConfigurationPolicyPrefStore::kPolicyDefaultSearchProviderIconURL); + TestStringPolicy(prefs::kDefaultSearchProviderEncodings, + ConfigurationPolicyPrefStore::kPolicyDefaultSearchProviderEncodings); +} + TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyAlternateErrorPagesEnabled) { TestBooleanPolicy(prefs::kAlternateErrorPagesEnabled, ConfigurationPolicyStore::kPolicyAlternateErrorPagesEnabled); @@ -247,8 +262,7 @@ TEST_F(ConfigurationPolicyPrefStoreTest, TestSettingProxyBypassList) { } TEST_F(ConfigurationPolicyPrefStoreTest, TestSettingsProxyConfig) { - FilePath unused_path(FILE_PATH_LITERAL("foo.exe")); - CommandLine command_line(unused_path); + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); command_line.AppendSwitch(switches::kNoProxyServer); command_line.AppendSwitch(switches::kProxyAutoDetect); command_line.AppendSwitchASCII(switches::kProxyPacUrl, @@ -281,8 +295,7 @@ TEST_F(ConfigurationPolicyPrefStoreTest, TestSettingsProxyConfig) { } TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfigManualOverride) { - FilePath unused_path(FILE_PATH_LITERAL("foo.exe")); - CommandLine command_line(unused_path); + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); command_line.AppendSwitch(switches::kNoProxyServer); command_line.AppendSwitch(switches::kProxyAutoDetect); command_line.AppendSwitchASCII(switches::kProxyPacUrl, @@ -321,8 +334,7 @@ TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfigManualOverride) { } TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfigNoProxy) { - FilePath unused_path(FILE_PATH_LITERAL("foo.exe")); - CommandLine command_line(unused_path); + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); scoped_ptr<MockConfigurationPolicyProvider> provider( new MockConfigurationPolicyProvider()); provider->AddPolicy(ConfigurationPolicyStore::kPolicyProxyBypassList, @@ -350,8 +362,7 @@ TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfigNoProxy) { TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfigNoProxyReversedApplyOrder) { - FilePath unused_path(FILE_PATH_LITERAL("foo.exe")); - CommandLine command_line(unused_path); + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); scoped_ptr<MockConfigurationPolicyProvider> provider( new MockConfigurationPolicyProvider()); provider->AddPolicy(ConfigurationPolicyStore::kPolicyProxyServerMode, @@ -378,8 +389,7 @@ TEST_F(ConfigurationPolicyPrefStoreTest, } TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfigAutoDetect) { - FilePath unused_path(FILE_PATH_LITERAL("foo.exe")); - CommandLine command_line(unused_path); + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); scoped_ptr<MockConfigurationPolicyProvider> provider( new MockConfigurationPolicyProvider()); provider->AddPolicy(ConfigurationPolicyStore::kPolicyProxyBypassList, @@ -409,8 +419,7 @@ TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfigAutoDetect) { } TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfiguseSystem) { - FilePath unused_path(FILE_PATH_LITERAL("foo.exe")); - CommandLine command_line(unused_path); + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); scoped_ptr<MockConfigurationPolicyProvider> provider( new MockConfigurationPolicyProvider()); provider->AddPolicy(ConfigurationPolicyStore::kPolicyProxyBypassList, @@ -437,8 +446,7 @@ TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfiguseSystem) { TEST_F(ConfigurationPolicyPrefStoreTest, TestPolicyProxyConfiguseSystemReversedApplyOrder) { - FilePath unused_path(FILE_PATH_LITERAL("foo.exe")); - CommandLine command_line(unused_path); + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); scoped_ptr<MockConfigurationPolicyProvider> provider( new MockConfigurationPolicyProvider()); provider->AddPolicy(ConfigurationPolicyStore::kPolicyProxyServerMode, @@ -462,3 +470,208 @@ TEST_F(ConfigurationPolicyPrefStoreTest, EXPECT_FALSE(store.prefs()->GetBoolean(prefs::kProxyAutoDetect, &bool_result)); } + +// 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(ConfigurationPolicyPrefStoreTest, MinimallyDefinedDefaultSearchPolicy) { + const char* search_url = "http://test.com/search?t={searchTerms}"; + scoped_ptr<MockConfigurationPolicyProvider> provider( + new MockConfigurationPolicyProvider()); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderSearchURL, + Value::CreateStringValue(search_url)); + + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); + ConfigurationPolicyPrefStore store(&command_line, provider.get()); + + EXPECT_EQ(store.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); + DictionaryValue* prefs = store.prefs(); + + std::string string_result; + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderSearchURL, + &string_result)); + EXPECT_EQ(string_result, search_url); + + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderName, + &string_result)); + EXPECT_EQ(string_result, "test.com"); + + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderKeyword, + &string_result)); + EXPECT_EQ(string_result, ""); + + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderSuggestURL, + &string_result)); + EXPECT_EQ(string_result, ""); + + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderIconURL, + &string_result)); + EXPECT_EQ(string_result, ""); + + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderEncodings, + &string_result)); + EXPECT_EQ(string_result, ""); +} + +// Checks that for a fully defined search policy, all elements have been +// read properly. +TEST_F(ConfigurationPolicyPrefStoreTest, FullyDefinedDefaultSearchPolicy) { + const char* search_url = "http://test.com/search?t={searchTerms}"; + const char* suggest_url = "http://test.com/sugg?={searchTerms}"; + const char* icon_url = "http://test.com/icon.jpg"; + const char* name = "MyName"; + const char* keyword = "MyKeyword"; + const char* encodings = "UTF-16;UTF-8"; + scoped_ptr<MockConfigurationPolicyProvider> provider( + new MockConfigurationPolicyProvider()); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderSearchURL, + Value::CreateStringValue(search_url)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderName, + Value::CreateStringValue(name)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderKeyword, + Value::CreateStringValue(keyword)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderSuggestURL, + Value::CreateStringValue(suggest_url)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderIconURL, + Value::CreateStringValue(icon_url)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderEncodings, + Value::CreateStringValue(encodings)); + + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); + ConfigurationPolicyPrefStore store(&command_line, provider.get()); + EXPECT_EQ(store.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); + DictionaryValue* prefs = store.prefs(); + + std::string result_search_url; + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderSearchURL, + &result_search_url)); + EXPECT_EQ(result_search_url, search_url); + + std::string result_name; + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderName, + &result_name)); + EXPECT_EQ(result_name, name); + + std::string result_keyword; + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderKeyword, + &result_keyword)); + EXPECT_EQ(result_keyword, keyword); + + std::string result_suggest_url; + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderSuggestURL, + &result_suggest_url)); + EXPECT_EQ(result_suggest_url, suggest_url); + + std::string result_icon_url; + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderIconURL, + &result_icon_url)); + EXPECT_EQ(result_icon_url, icon_url); + + std::string result_encodings; + EXPECT_TRUE(prefs->GetString(prefs::kDefaultSearchProviderEncodings, + &result_encodings)); + EXPECT_EQ(result_encodings, encodings); +} + +// Checks that if the default search policy is missing, that no elements of the +// default search policy will be present. +TEST_F(ConfigurationPolicyPrefStoreTest, MissingUrlDefaultSearchPolicy) { + const char* suggest_url = "http://test.com/sugg?t={searchTerms}"; + const char* icon_url = "http://test.com/icon.jpg"; + const char* name = "MyName"; + const char* keyword = "MyKeyword"; + const char* encodings = "UTF-16;UTF-8"; + scoped_ptr<MockConfigurationPolicyProvider> provider( + new MockConfigurationPolicyProvider()); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderName, + Value::CreateStringValue(name)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderKeyword, + Value::CreateStringValue(keyword)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderSuggestURL, + Value::CreateStringValue(suggest_url)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderIconURL, + Value::CreateStringValue(icon_url)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderEncodings, + Value::CreateStringValue(encodings)); + + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); + ConfigurationPolicyPrefStore store(&command_line, provider.get()); + EXPECT_EQ(store.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); + DictionaryValue* prefs = store.prefs(); + + std::string string_result; + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderSearchURL, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderName, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderKeyword, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderSuggestURL, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderIconURL, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderEncodings, + &string_result)); +} + +// Checks that if the default search policy is invalid, that no elements of the +// default search policy will be present. +TEST_F(ConfigurationPolicyPrefStoreTest, InvalidDefaultSearchPolicy) { + const char* bad_search_url = "http://test.com/noSearchTerms"; + const char* suggest_url = "http://test.com/sugg?t={searchTerms}"; + const char* icon_url = "http://test.com/icon.jpg"; + const char* name = "MyName"; + const char* keyword = "MyKeyword"; + const char* encodings = "UTF-16;UTF-8"; + scoped_ptr<MockConfigurationPolicyProvider> provider( + new MockConfigurationPolicyProvider()); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderSearchURL, + Value::CreateStringValue(bad_search_url)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderName, + Value::CreateStringValue(name)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderKeyword, + Value::CreateStringValue(keyword)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderSuggestURL, + Value::CreateStringValue(suggest_url)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderIconURL, + Value::CreateStringValue(icon_url)); + provider->AddPolicy( + ConfigurationPolicyStore::kPolicyDefaultSearchProviderEncodings, + Value::CreateStringValue(encodings)); + + CommandLine command_line(CommandLine::ARGUMENTS_ONLY); + ConfigurationPolicyPrefStore store(&command_line, provider.get()); + EXPECT_EQ(store.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); + DictionaryValue* prefs = store.prefs(); + + std::string string_result; + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderSearchURL, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderName, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderKeyword, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderSuggestURL, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderIconURL, + &string_result)); + EXPECT_FALSE(prefs->GetString(prefs::kDefaultSearchProviderEncodings, + &string_result)); +} + diff --git a/chrome/browser/policy/configuration_policy_provider.cc b/chrome/browser/policy/configuration_policy_provider.cc index b63765e..0f3d93c 100644 --- a/chrome/browser/policy/configuration_policy_provider.cc +++ b/chrome/browser/policy/configuration_policy_provider.cc @@ -28,6 +28,18 @@ const InternalPolicyValueMapEntry kPolicyValueMap[] = { Value::TYPE_INTEGER, policy::key::kRestoreOnStartup }, { ConfigurationPolicyStore::kPolicyURLsToRestoreOnStartup, Value::TYPE_LIST, policy::key::kURLsToRestoreOnStartup }, + { ConfigurationPolicyStore::kPolicyDefaultSearchProviderName, + Value::TYPE_STRING, policy::key::kDefaultSearchProviderName }, + { ConfigurationPolicyStore::kPolicyDefaultSearchProviderKeyword, + Value::TYPE_STRING, policy::key::kDefaultSearchProviderKeyword }, + { ConfigurationPolicyStore::kPolicyDefaultSearchProviderSearchURL, + Value::TYPE_STRING, policy::key::kDefaultSearchProviderSearchURL }, + { ConfigurationPolicyStore::kPolicyDefaultSearchProviderSuggestURL, + Value::TYPE_STRING, policy::key::kDefaultSearchProviderSuggestURL }, + { ConfigurationPolicyStore::kPolicyDefaultSearchProviderIconURL, + Value::TYPE_STRING, policy::key::kDefaultSearchProviderIconURL }, + { ConfigurationPolicyStore::kPolicyDefaultSearchProviderEncodings, + Value::TYPE_STRING, policy::key::kDefaultSearchProviderEncodings }, { ConfigurationPolicyStore::kPolicyProxyServerMode, Value::TYPE_INTEGER, policy::key::kProxyServerMode }, { ConfigurationPolicyStore::kPolicyProxyServer, diff --git a/chrome/browser/policy/configuration_policy_store.h b/chrome/browser/policy/configuration_policy_store.h index 05e07a0..9d226ab0 100644 --- a/chrome/browser/policy/configuration_policy_store.h +++ b/chrome/browser/policy/configuration_policy_store.h @@ -22,6 +22,12 @@ class ConfigurationPolicyStore { kPolicyHomepageIsNewTabPage, kPolicyRestoreOnStartup, kPolicyURLsToRestoreOnStartup, + kPolicyDefaultSearchProviderName, + kPolicyDefaultSearchProviderKeyword, + kPolicyDefaultSearchProviderSearchURL, + kPolicyDefaultSearchProviderSuggestURL, + kPolicyDefaultSearchProviderIconURL, + kPolicyDefaultSearchProviderEncodings, kPolicyProxyServerMode, kPolicyProxyServer, kPolicyProxyPacUrl, diff --git a/chrome/browser/policy/mock_configuration_policy_provider.h b/chrome/browser/policy/mock_configuration_policy_provider.h index 2d727467..abd77a2 100644 --- a/chrome/browser/policy/mock_configuration_policy_provider.h +++ b/chrome/browser/policy/mock_configuration_policy_provider.h @@ -23,7 +23,8 @@ class MockConfigurationPolicyProvider : public ConfigurationPolicyProvider { typedef std::map<ConfigurationPolicyStore::PolicyType, Value*> PolicyMap; void AddPolicy(ConfigurationPolicyStore::PolicyType policy, Value* value) { - policy_map_[policy] = value; + std::swap(policy_map_[policy], value); + delete value; } // ConfigurationPolicyProvider method overrides. diff --git a/chrome/browser/policy/mock_configuration_policy_store.h b/chrome/browser/policy/mock_configuration_policy_store.h index c1ab12d..fbac70d 100644 --- a/chrome/browser/policy/mock_configuration_policy_store.h +++ b/chrome/browser/policy/mock_configuration_policy_store.h @@ -25,7 +25,8 @@ class MockConfigurationPolicyStore : public ConfigurationPolicyStore { // ConfigurationPolicyStore implementation. virtual void Apply(PolicyType policy, Value* value) { - policy_map_[policy] = value; + std::swap(policy_map_[policy], value); + delete value; } private: diff --git a/chrome/browser/prefs/pref_set_observer.cc b/chrome/browser/prefs/pref_set_observer.cc index 0f41500..a4ecf6f 100644 --- a/chrome/browser/prefs/pref_set_observer.cc +++ b/chrome/browser/prefs/pref_set_observer.cc @@ -58,6 +58,21 @@ PrefSetObserver* PrefSetObserver::CreateProxyPrefSetObserver( return pref_set; } +// static +PrefSetObserver* PrefSetObserver::CreateDefaultSearchPrefSetObserver( + PrefService* pref_service, + NotificationObserver* observer) { + PrefSetObserver* pref_set = new PrefSetObserver(pref_service, observer); + pref_set->AddPref(prefs::kDefaultSearchProviderName); + pref_set->AddPref(prefs::kDefaultSearchProviderKeyword); + pref_set->AddPref(prefs::kDefaultSearchProviderSearchURL); + pref_set->AddPref(prefs::kDefaultSearchProviderSuggestURL); + pref_set->AddPref(prefs::kDefaultSearchProviderIconURL); + pref_set->AddPref(prefs::kDefaultSearchProviderEncodings); + + return pref_set; +} + void PrefSetObserver::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { diff --git a/chrome/browser/prefs/pref_set_observer.h b/chrome/browser/prefs/pref_set_observer.h index 038a3f5..beaec01 100644 --- a/chrome/browser/prefs/pref_set_observer.h +++ b/chrome/browser/prefs/pref_set_observer.h @@ -31,11 +31,16 @@ class PrefSetObserver : public NotificationObserver { // Check whether any of the observed preferences has the managed bit set. bool IsManaged(); - // Create a pref set observer for all preferences relavant to proxies. + // Create a pref set observer for all preferences relevant to proxies. static PrefSetObserver* CreateProxyPrefSetObserver( PrefService* pref_service, NotificationObserver* observer); + // Create a pref set observer for all preferences relevant to default search. + static PrefSetObserver* CreateDefaultSearchPrefSetObserver( + PrefService* pref_service, + NotificationObserver* observer); + private: // Overridden from NotificationObserver. virtual void Observe(NotificationType type, diff --git a/chrome/common/policy_constants.cc b/chrome/common/policy_constants.cc index 2fc0ba6..9624a2a 100644 --- a/chrome/common/policy_constants.cc +++ b/chrome/common/policy_constants.cc @@ -20,6 +20,16 @@ const char kHomepageLocation[] = "HomepageLocation"; const char kHomepageIsNewTabPage[] = "HomepageIsNewTabPage"; const char kRestoreOnStartup[] = "RestoreOnStartup"; const char kURLsToRestoreOnStartup[] = "RestoreOnStartupURLs"; +const char kDefaultSearchProviderName[] = "DefaultSearchProviderName"; +const char kDefaultSearchProviderKeyword[] = "DefaultSearchProviderKeyword"; +const char kDefaultSearchProviderSearchURL[] = + "DefaultSearchProviderSearchURL"; +const char kDefaultSearchProviderSuggestURL[] = + "DefaultSearchProviderSuggestURL"; +const char kDefaultSearchProviderIconURL[] = + "DefaultSearchProviderIconURL"; +const char kDefaultSearchProviderEncodings[] = + "DefaultSearchProviderEncodings"; const char kProxyServerMode[] = "ProxyServerMode"; const char kProxyServer[] = "ProxyServer"; const char kProxyPacUrl[] = "ProxyPacUrl"; diff --git a/chrome/common/policy_constants.h b/chrome/common/policy_constants.h index 252077a..b12d774 100644 --- a/chrome/common/policy_constants.h +++ b/chrome/common/policy_constants.h @@ -22,6 +22,12 @@ extern const char kHomepageLocation[]; extern const char kHomepageIsNewTabPage[]; extern const char kRestoreOnStartup[]; extern const char kURLsToRestoreOnStartup[]; +extern const char kDefaultSearchProviderName[]; +extern const char kDefaultSearchProviderKeyword[]; +extern const char kDefaultSearchProviderSearchURL[]; +extern const char kDefaultSearchProviderSuggestURL[]; +extern const char kDefaultSearchProviderIconURL[]; +extern const char kDefaultSearchProviderEncodings[]; extern const char kProxyServerMode[]; extern const char kProxyServer[]; extern const char kProxyPacUrl[]; diff --git a/chrome/common/pref_names.cc b/chrome/common/pref_names.cc index 451e283..d7031ae 100644 --- a/chrome/common/pref_names.cc +++ b/chrome/common/pref_names.cc @@ -133,9 +133,22 @@ const char kDefaultSearchProviderSearchURL[] = const char kDefaultSearchProviderSuggestURL[] = "default_search_provider.suggest_url"; +// The Fav Icon URL (as understood by TemplateURLRef) of the default search +// provider. +const char kDefaultSearchProviderIconURL[] = + "default_search_provider.icon_url"; + +// The input encoding (as understood by TemplateURLRef) supported by the default +// search provider. The various encodings are separated by ';' +const char kDefaultSearchProviderEncodings[] = + "default_search_provider.encodings"; + // The name of the default search provider. const char kDefaultSearchProviderName[] = "default_search_provider.name"; +// The keyword of the default search provider. +const char kDefaultSearchProviderKeyword[] = "default_search_provider.keyword"; + // The id of the default search provider. const char kDefaultSearchProviderID[] = "default_search_provider.id"; diff --git a/chrome/common/pref_names.h b/chrome/common/pref_names.h index 00d6992..5f7b957 100644 --- a/chrome/common/pref_names.h +++ b/chrome/common/pref_names.h @@ -54,7 +54,10 @@ extern const char kSearchSuggestEnabled[]; extern const char kCookieBehavior[]; // OBSOLETE extern const char kDefaultSearchProviderSearchURL[]; extern const char kDefaultSearchProviderSuggestURL[]; +extern const char kDefaultSearchProviderIconURL[]; +extern const char kDefaultSearchProviderEncodings[]; extern const char kDefaultSearchProviderName[]; +extern const char kDefaultSearchProviderKeyword[]; extern const char kDefaultSearchProviderID[]; extern const char kDefaultSearchProviderPrepopulateID[]; extern const char kSearchProviderOverrides[]; |