diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-04 14:54:20 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-04 14:54:20 +0000 |
commit | fca94f27c89ce98e74459ebb6a761e07b63cfe7a (patch) | |
tree | 3bce85bf61d7dfbed2327da8585a798b7e7855b6 /chrome | |
parent | b4876084f3ba252966eb3768f34ab5508ddce1f2 (diff) | |
download | chromium_src-fca94f27c89ce98e74459ebb6a761e07b63cfe7a.zip chromium_src-fca94f27c89ce98e74459ebb6a761e07b63cfe7a.tar.gz chromium_src-fca94f27c89ce98e74459ebb6a761e07b63cfe7a.tar.bz2 |
Lands http://codereview.chromium.org/3364005/show for jeanluc:
Disable editing and setting of the default search provider if the
provider is managed via group policy.
BUG=49306
TEST=KeywordEditorControllerTest*
Review URL: http://codereview.chromium.org/3296007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58581 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
7 files changed, 94 insertions, 5 deletions
diff --git a/chrome/browser/search_engines/keyword_editor_controller.cc b/chrome/browser/search_engines/keyword_editor_controller.cc index 23c50aa..f40fdca 100644 --- a/chrome/browser/search_engines/keyword_editor_controller.cc +++ b/chrome/browser/search_engines/keyword_editor_controller.cc @@ -78,10 +78,16 @@ void KeywordEditorController::ModifyTemplateURL(const TemplateURL* template_url, profile_); } +bool KeywordEditorController::CanEdit(const TemplateURL* url) const { + return !url_model()->IsDefaultSearchManaged() || + url != url_model()->GetDefaultSearchProvider(); +} + bool KeywordEditorController::CanMakeDefault(const TemplateURL* url) const { return (url != url_model()->GetDefaultSearchProvider() && url->url() && - url->url()->SupportsReplacement()); + url->url()->SupportsReplacement() && + !url_model()->IsDefaultSearchManaged()); } bool KeywordEditorController::CanRemove(const TemplateURL* url) const { diff --git a/chrome/browser/search_engines/keyword_editor_controller.h b/chrome/browser/search_engines/keyword_editor_controller.h index fd30bf4..a1642e4 100644 --- a/chrome/browser/search_engines/keyword_editor_controller.h +++ b/chrome/browser/search_engines/keyword_editor_controller.h @@ -39,6 +39,9 @@ class KeywordEditorController { const string16& keyword, const std::string& url); + // Return true if the given |url| can be edited. + bool CanEdit(const TemplateURL* url) const; + // Return true if the given |url| can be made the default. bool CanMakeDefault(const TemplateURL* url) const; diff --git a/chrome/browser/search_engines/keyword_editor_controller_unittest.cc b/chrome/browser/search_engines/keyword_editor_controller_unittest.cc index eabf291..ca817ec 100644 --- a/chrome/browser/search_engines/keyword_editor_controller_unittest.cc +++ b/chrome/browser/search_engines/keyword_editor_controller_unittest.cc @@ -10,6 +10,8 @@ #include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url_model.h" #include "chrome/browser/search_engines/template_url_table_model.h" +#include "chrome/common/pref_names.h" +#include "chrome/test/testing_pref_service.h" #include "chrome/test/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" @@ -60,6 +62,14 @@ class KeywordEditorControllerTest : public testing::Test, removed_count_ = 0; } + void SimulateDefaultSearchIsManaged(const TemplateURL* turl) { + ASSERT_TRUE(turl->url() != NULL); + model_->SetDefaultSearchProvider(turl); + TestingPrefService* service = profile_->GetTestingPrefService(); + service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL, + Value::CreateStringValue(turl->url()->url())); + } + TemplateURLTableModel* table_model() const { return controller_->table_model(); } @@ -112,7 +122,7 @@ TEST_F(KeywordEditorControllerTest, Add) { const TemplateURL* turl = model_->GetTemplateURLs()[0]; EXPECT_EQ(L"a", turl->short_name()); EXPECT_EQ(L"b", turl->keyword()); - EXPECT_TRUE(turl->url() != NULL); + ASSERT_TRUE(turl->url() != NULL); EXPECT_EQ("http://c", turl->url()->url()); } @@ -129,7 +139,7 @@ TEST_F(KeywordEditorControllerTest, Modify) { VerifyChangeCount(0, 1, 0, 0); EXPECT_EQ(L"a1", turl->short_name()); EXPECT_EQ(L"b1", turl->keyword()); - EXPECT_TRUE(turl->url() != NULL); + ASSERT_TRUE(turl->url() != NULL); EXPECT_EQ("http://c1", turl->url()->url()); } @@ -152,6 +162,50 @@ TEST_F(KeywordEditorControllerTest, MakeDefault) { EXPECT_EQ(-1, new_default); } +// Tests that a TemplateURL can't be made the default if the default search +// provider is managed via policy. +TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) { + controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); + controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); + ClearChangeCount(); + + const TemplateURL* turl1 = model_->GetTemplateURLForKeyword(L"b"); + ASSERT_TRUE(turl1 != NULL); + const TemplateURL* turl2 = model_->GetTemplateURLForKeyword(L"b1"); + ASSERT_TRUE(turl2 != NULL); + + EXPECT_TRUE(controller_->CanMakeDefault(turl1)); + EXPECT_TRUE(controller_->CanMakeDefault(turl2)); + + SimulateDefaultSearchIsManaged(turl2); + EXPECT_TRUE(model_->IsDefaultSearchManaged()); + + EXPECT_FALSE(controller_->CanMakeDefault(turl1)); + EXPECT_FALSE(controller_->CanMakeDefault(turl2)); +} + +// Tests that a TemplateURL can't be edited if it is the managed default search +// provider. +TEST_F(KeywordEditorControllerTest, EditManagedDefault) { + controller_->AddTemplateURL(kA, kB, "http://c{searchTerms}"); + controller_->AddTemplateURL(kA1, kB1, "http://d{searchTerms}"); + ClearChangeCount(); + + const TemplateURL* turl1 = model_->GetTemplateURLForKeyword(L"b"); + ASSERT_TRUE(turl1 != NULL); + const TemplateURL* turl2 = model_->GetTemplateURLForKeyword(L"b1"); + ASSERT_TRUE(turl2 != NULL); + + EXPECT_TRUE(controller_->CanEdit(turl1)); + EXPECT_TRUE(controller_->CanEdit(turl2)); + + SimulateDefaultSearchIsManaged(turl2); + EXPECT_TRUE(model_->IsDefaultSearchManaged()); + + EXPECT_TRUE(controller_->CanEdit(turl1)); + EXPECT_FALSE(controller_->CanEdit(turl2)); +} + TEST_F(KeywordEditorControllerTest, MakeDefaultNoWebData) { // Simulate a failure to load Web Data. Init(true); diff --git a/chrome/browser/search_engines/template_url_model.cc b/chrome/browser/search_engines/template_url_model.cc index 2df8ad0..9396935 100644 --- a/chrome/browser/search_engines/template_url_model.cc +++ b/chrome/browser/search_engines/template_url_model.cc @@ -464,6 +464,15 @@ const TemplateURL* TemplateURLModel::GetDefaultSearchProvider() { return prefs_default_search_provider_.get(); } +bool TemplateURLModel::IsDefaultSearchManaged() { + PrefService* prefs = GetPrefs(); + if (!prefs) + return false; + const PrefService::Preference* pref = + prefs->FindPreference(prefs::kDefaultSearchProviderSearchURL); + return pref && pref->IsManaged(); +} + void TemplateURLModel::AddObserver(TemplateURLModelObserver* observer) { model_observers_.AddObserver(observer); } diff --git a/chrome/browser/search_engines/template_url_model.h b/chrome/browser/search_engines/template_url_model.h index ce7ebb4..44b5029 100644 --- a/chrome/browser/search_engines/template_url_model.h +++ b/chrome/browser/search_engines/template_url_model.h @@ -174,6 +174,9 @@ class TemplateURLModel : public WebDataServiceConsumer, // NOTE: At least in unittest mode, this may return NULL. const TemplateURL* GetDefaultSearchProvider(); + // Returns true if the default search is managed through group policy. + bool IsDefaultSearchManaged(); + // Observers used to listen for changes to the model. // TemplateURLModel does NOT delete the observers when deleted. void AddObserver(TemplateURLModelObserver* observer); diff --git a/chrome/browser/search_engines/template_url_model_unittest.cc b/chrome/browser/search_engines/template_url_model_unittest.cc index 44665d8..32cee09 100644 --- a/chrome/browser/search_engines/template_url_model_unittest.cc +++ b/chrome/browser/search_engines/template_url_model_unittest.cc @@ -19,6 +19,8 @@ #include "chrome/browser/search_engines/template_url_model_observer.h" #include "chrome/browser/search_engines/template_url_prepopulate_data.h" #include "chrome/browser/webdata/web_database.h" +#include "chrome/common/pref_names.h" +#include "chrome/test/testing_pref_service.h" #include "chrome/test/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" @@ -1111,3 +1113,15 @@ TEST_F(TemplateURLModelTest, FailedInit) { ASSERT_TRUE(model_->GetDefaultSearchProvider()); } + +// Verifies that if the default search URL preference is managed, we report +// the default search as managed. +TEST_F(TemplateURLModelTest, ReportDefaultSearchIsManaged) { + TestingPrefService* service = profile_->GetTestingPrefService(); + service->RegisterStringPref(prefs::kDefaultSearchProviderSearchURL, + std::string()); + service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL, + Value::CreateStringValue("http://test.com/{searchTerms}")); + ASSERT_TRUE(model_->IsDefaultSearchManaged()); +} + diff --git a/chrome/browser/search_engines/template_url_prepopulate_data.cc b/chrome/browser/search_engines/template_url_prepopulate_data.cc index 5d44957..f66735d 100644 --- a/chrome/browser/search_engines/template_url_prepopulate_data.cc +++ b/chrome/browser/search_engines/template_url_prepopulate_data.cc @@ -3240,8 +3240,8 @@ void GetPrepopulatedTemplatefromPrefs(PrefService* prefs, void GetPrepopulatedEngines(PrefService* prefs, std::vector<TemplateURL*>* t_urls, size_t* default_search_provider_index) { - // If there if there is a set of search engines in the preferences - // file, it overrides the built-in set. + // If there is a set of search engines in the preferences file, it overrides + // the built-in set. *default_search_provider_index = 0; GetPrepopulatedTemplatefromPrefs(prefs, t_urls); if (!t_urls->empty()) |