diff options
author | cpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-13 21:57:18 +0000 |
---|---|---|
committer | cpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-13 21:57:18 +0000 |
commit | af4d76441db0a6c35b4dc32a0d7fc4cca6e31453 (patch) | |
tree | 1ddd5dc943ed7664b0da217a465ef60728cffa0c /chrome/browser/search_engines | |
parent | 1222d024d22984bf567e5cc3f36bf767225d8822 (diff) | |
download | chromium_src-af4d76441db0a6c35b4dc32a0d7fc4cca6e31453.zip chromium_src-af4d76441db0a6c35b4dc32a0d7fc4cca6e31453.tar.gz chromium_src-af4d76441db0a6c35b4dc32a0d7fc4cca6e31453.tar.bz2 |
Allow the default search providers to be specified by the preferences files,
overriding the built-in set.
- Per locale providers not allowed.
BUG=47440
TEST=unittest included, for manual testing see bug.
Review URL: http://codereview.chromium.org/2974001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52228 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/search_engines')
4 files changed, 157 insertions, 32 deletions
diff --git a/chrome/browser/search_engines/template_url_model.cc b/chrome/browser/search_engines/template_url_model.cc index bbb07fc..ede0ea0 100644 --- a/chrome/browser/search_engines/template_url_model.cc +++ b/chrome/browser/search_engines/template_url_model.cc @@ -600,7 +600,7 @@ void TemplateURLModel::OnWebDataServiceRequestDone( keyword_result.keywords.end()); const int resource_keyword_version = - TemplateURLPrepopulateData::GetDataVersion(); + TemplateURLPrepopulateData::GetDataVersion(GetPrefs()); if (keyword_result.builtin_keyword_version != resource_keyword_version) { // There should never be duplicate TemplateURLs. We had a bug such that // duplicate TemplateURLs existed for one locale. As such we invoke diff --git a/chrome/browser/search_engines/template_url_prepopulate_data.cc b/chrome/browser/search_engines/template_url_prepopulate_data.cc index 2c971325..67a5342 100644 --- a/chrome/browser/search_engines/template_url_prepopulate_data.cc +++ b/chrome/browser/search_engines/template_url_prepopulate_data.cc @@ -62,6 +62,9 @@ struct PrepopulatedEngine { // 33, 34, 36, 39, 42, 43, 47, 48, 49, 50, 52, 53, 56, 58, 60, 61, 64, 65, // 66, 70, 74, 78, 79, 80, 81, 84, 86, 88, 91, 92, 93, 94, 95, 96, 97, 98, // 102+ + // + // IDs > 1000 are reserved for distribution custom engines. + // // NOTE: CHANGE THE ABOVE NUMBERS IF YOU ADD A NEW ENGINE; ID conflicts = bad! const int id; }; @@ -2790,44 +2793,120 @@ namespace TemplateURLPrepopulateData { void RegisterUserPrefs(PrefService* prefs) { prefs->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown); - + prefs->RegisterListPref(prefs::kSearchProviderOverrides); + prefs->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); // Obsolete pref, for migration. prefs->RegisterIntegerPref(prefs::kGeoIDAtInstall, -1); } -int GetDataVersion() { - return 28; // Increment this if you change the above data in ways that mean - // users with existing data should get a new version. +int GetDataVersion(PrefService* prefs) { + // Increment this if you change the above data in ways that mean users with + // existing data should get a new version. + const int kCurrentDataVersion = 28; + if (!prefs) + return kCurrentDataVersion; + // If a version number exist in the preferences file, it overrides the + // version of the built-in data. + int version = + prefs->GetInteger(prefs::kSearchProviderOverridesVersion); + return (version >= 0) ? version : kCurrentDataVersion; +} + +TemplateURL* MakePrepopulatedTemplateURL(const wchar_t* name, + const wchar_t* keyword, + const wchar_t* search_url, + const char* favicon_url, + const wchar_t* suggest_url, + const char* encoding, + int id) { + TemplateURL* new_turl = new TemplateURL(); + new_turl->SetURL(WideToUTF8(search_url), 0, 0); + if (favicon_url) + new_turl->SetFavIconURL(GURL(favicon_url)); + if (suggest_url) + new_turl->SetSuggestionsURL(WideToUTF8(suggest_url), 0, 0); + new_turl->set_short_name(name); + if (keyword == NULL) + new_turl->set_autogenerate_keyword(true); + else + new_turl->set_keyword(keyword); + new_turl->set_show_in_default_list(true); + new_turl->set_safe_for_autoreplace(true); + new_turl->set_date_created(Time()); + std::vector<std::string> turl_encodings; + turl_encodings.push_back(encoding); + new_turl->set_input_encodings(turl_encodings); + new_turl->set_prepopulate_id(id); + return new_turl; +} + +void GetPrepopulatedTemplatefromPrefs(PrefService* prefs, + std::vector<TemplateURL*>* t_urls) { + const ListValue* list = + prefs->GetList(prefs::kSearchProviderOverrides); + if (!list) + return; + + std::wstring name; + std::wstring keyword; + std::wstring search_url; + std::wstring suggest_url; + std::string favicon_url; + std::string encoding; + int id; + + size_t num_engines = list->GetSize(); + for (size_t i = 0; i != num_engines; ++i) { + Value* val; + DictionaryValue* engine; + list->GetDictionary(i, &engine); + if (engine->Get(L"name", &val) && val->GetAsString(&name) && + engine->Get(L"keyword", &val) && val->GetAsString(&keyword) && + engine->Get(L"search_url", &val) && val->GetAsString(&search_url) && + engine->Get(L"suggest_url", &val) && val->GetAsString(&suggest_url) && + engine->Get(L"favicon_url", &val) && val->GetAsString(&favicon_url) && + engine->Get(L"encoding", &val) && val->GetAsString(&encoding) && + engine->Get(L"id", &val) && val->GetAsInteger(&id)) { + // These next fields are not allowed to be empty. + if (search_url.empty() || favicon_url.empty() || encoding.empty()) + return; + } else { + // Got a parsing error. No big deal. + continue; + } + t_urls->push_back(MakePrepopulatedTemplateURL(name.c_str(), + keyword.c_str(), + search_url.c_str(), + favicon_url.c_str(), + suggest_url.c_str(), + encoding.c_str(), + id)); + } } 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. + *default_search_provider_index = 0; + GetPrepopulatedTemplatefromPrefs(prefs, t_urls); + if (!t_urls->empty()) + return; + const PrepopulatedEngine** engines; size_t num_engines; GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); - *default_search_provider_index = 0; - - for (size_t i = 0; i < num_engines; ++i) { - TemplateURL* new_turl = new TemplateURL(); - new_turl->SetURL(WideToUTF8(engines[i]->search_url), 0, 0); - if (engines[i]->favicon_url) - new_turl->SetFavIconURL(GURL(engines[i]->favicon_url)); - if (engines[i]->suggest_url) - new_turl->SetSuggestionsURL(WideToUTF8(engines[i]->suggest_url), 0, 0); - new_turl->set_short_name(engines[i]->name); - if (engines[i]->keyword == NULL) - new_turl->set_autogenerate_keyword(true); - else - new_turl->set_keyword(engines[i]->keyword); - new_turl->set_show_in_default_list(true); - new_turl->set_safe_for_autoreplace(true); - new_turl->set_date_created(Time()); - std::vector<std::string> turl_encodings; - turl_encodings.push_back(engines[i]->encoding); - new_turl->set_input_encodings(turl_encodings); - new_turl->set_prepopulate_id(engines[i]->id); - t_urls->push_back(new_turl); + for (size_t i = 0; i != num_engines; ++i) { + TemplateURL* turl = + MakePrepopulatedTemplateURL(engines[i]->name, + engines[i]->keyword, + engines[i]->search_url, + engines[i]->favicon_url, + engines[i]->suggest_url, + engines[i]->encoding, + engines[i]->id); + t_urls->push_back(turl); } } @@ -2860,6 +2939,4 @@ SearchEngineType GetSearchEngineType(const TemplateURL* search_engine) { } } - - } // namespace TemplateURLPrepopulateData diff --git a/chrome/browser/search_engines/template_url_prepopulate_data.h b/chrome/browser/search_engines/template_url_prepopulate_data.h index c0a7adb..4191a92 100644 --- a/chrome/browser/search_engines/template_url_prepopulate_data.h +++ b/chrome/browser/search_engines/template_url_prepopulate_data.h @@ -32,8 +32,9 @@ enum SearchEngineType { void RegisterUserPrefs(PrefService* prefs); // Returns the current version of the prepopulate data, so callers can know when -// they need to re-merge. -int GetDataVersion(); +// they need to re-merge. If the prepopulate data comes from the preferences +// file then it returns the version specified there. +int GetDataVersion(PrefService* prefs); // Loads the set of TemplateURLs from the prepopulate data. Ownership of the // TemplateURLs is passed to the caller. On return, diff --git a/chrome/browser/search_engines/template_url_prepopulate_data_unittest.cc b/chrome/browser/search_engines/template_url_prepopulate_data_unittest.cc index a5ede32..003c64b 100644 --- a/chrome/browser/search_engines/template_url_prepopulate_data_unittest.cc +++ b/chrome/browser/search_engines/template_url_prepopulate_data_unittest.cc @@ -1,7 +1,7 @@ // Copyright (c) 2010 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 "base/scoped_temp_dir.h" #include "base/scoped_vector.h" #include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url_prepopulate_data.h" @@ -78,3 +78,50 @@ TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) { } } } + +// Verifies that default search providers from the preferences file +// override the built-in ones. +TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { + const char pref_data[] = + "{ " + " \"search_provider_overrides_version\":1," + " \"search_provider_overrides\": [" + " { \"name\":\"foo\"," + " \"keyword\":\"fook\"," + " \"search_url\":\"http://foo.com/s?q={searchTerms}\"," + " \"favicon_url\":\"http://foi.com/favicon.ico\"," + " \"suggest_url\":\"\"," + " \"encoding\":\"UTF-8\"," + " \"id\":1001" + " }" + " ]" + "}"; + + ScopedTempDir temp_dir; + ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); + FilePath preferences_file = temp_dir.path().AppendASCII("Preferences"); + file_util::WriteFile(preferences_file, pref_data, sizeof(pref_data)); + + scoped_ptr<PrefService> prefs(new PrefService(new PrefValueStore( + NULL, new JsonPrefStore(preferences_file, + ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE)), + NULL, NULL))); + + TemplateURLPrepopulateData::RegisterUserPrefs(prefs.get()); + + int version = TemplateURLPrepopulateData::GetDataVersion(prefs.get()); + EXPECT_EQ(1, version); + + std::vector<TemplateURL*> t_urls; + size_t default_index; + TemplateURLPrepopulateData::GetPrepopulatedEngines( + prefs.get(), &t_urls, &default_index); + + ASSERT_EQ(1u, t_urls.size()); + EXPECT_EQ(L"foo", t_urls[0]->short_name()); + EXPECT_EQ(L"fook", t_urls[0]->keyword()); + EXPECT_EQ("foo.com", t_urls[0]->url()->GetHost()); + EXPECT_EQ("foi.com", t_urls[0]->GetFavIconURL().host()); + EXPECT_EQ(1u, t_urls[0]->input_encodings().size()); + EXPECT_EQ(1001, t_urls[0]->prepopulate_id()); +} |