diff options
author | levin@chromium.org <levin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-19 05:02:54 +0000 |
---|---|---|
committer | levin@chromium.org <levin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-19 05:02:54 +0000 |
commit | 258ecdbbc548d725058a1d6f01a0c734fdc85011 (patch) | |
tree | a468a5c72c465833913ec173bfddfc37ddd6f794 | |
parent | 05b66161a39e6cd6422528cac220df275d3aa239 (diff) | |
download | chromium_src-258ecdbbc548d725058a1d6f01a0c734fdc85011.zip chromium_src-258ecdbbc548d725058a1d6f01a0c734fdc85011.tar.gz chromium_src-258ecdbbc548d725058a1d6f01a0c734fdc85011.tar.bz2 |
Add function to convert from a url to a known search engine.
This is part of the dialog redesign for this feature. We need a way to go from the url to the search engine icon and friendly name (for search engines that we know about).
BUG=38475
TEST=unit_tests --gtest_filter=TemplateURLPrep*
Review URL: http://codereview.chromium.org/3847004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63019 0039d316-1c4b-4281-b951-d872f2087c98
3 files changed, 213 insertions, 63 deletions
diff --git a/chrome/browser/search_engines/template_url_prepopulate_data.cc b/chrome/browser/search_engines/template_url_prepopulate_data.cc index 362f95b..34ec09b 100644 --- a/chrome/browser/search_engines/template_url_prepopulate_data.cc +++ b/chrome/browser/search_engines/template_url_prepopulate_data.cc @@ -15,9 +15,12 @@ #include "base/stl_util-inl.h" #include "base/utf_string_conversions.h" #include "chrome/browser/prefs/pref_service.h" +#include "chrome/browser/search_engines/search_terms_data.h" #include "chrome/browser/search_engines/template_url.h" +#include "chrome/browser/search_engines/template_url_model.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/pref_names.h" +#include "googleurl/src/gurl.h" #include "grit/theme_resources.h" #if defined(OS_WIN) @@ -2764,6 +2767,39 @@ const PrepopulatedEngine* engines_ZA[] = const PrepopulatedEngine* engines_ZW[] = { &google, &yahoo, &bing, }; + +// A list of all the engines that we know about. +const PrepopulatedEngine* kAllEngines[] = + { &abcsok, &altavista, &altavista_ar, &altavista_se, &aol, &araby, &ask, + &ask_de, &ask_es, &ask_it, &ask_nl, &ask_uk, &atlas_cz, &atlas_sk, &baidu, + &bing, &bing_ar_XA, &bing_bg_BG, &bing_cs_CZ, &bing_da_DK, &bing_de_AT, + &bing_de_CH, &bing_de_DE, &bing_el_GR, &bing_en_AU, &bing_en_CA, + &bing_en_GB, &bing_en_ID, &bing_en_IE, &bing_en_IN, &bing_en_MY, + &bing_en_NZ, &bing_en_PH, &bing_en_SG, &bing_en_US, &bing_en_XA, + &bing_en_ZA, &bing_es_AR, &bing_es_CL, &bing_es_ES, &bing_es_MX, + &bing_es_XL, &bing_et_EE, &bing_fi_FI, &bing_fr_BE, &bing_fr_CA, + &bing_fr_CH, &bing_fr_FR, &bing_he_IL, &bing_hr_HR, &bing_hu_HU, + &bing_it_IT, &bing_ja_JP, &bing_ko_KR, &bing_lt_LT, &bing_lv_LV, + &bing_nb_NO, &bing_nl_BE, &bing_nl_NL, &bing_pl_PL, &bing_pt_BR, + &bing_pt_PT, &bing_ro_RO, &bing_ru_RU, &bing_sl_SI, &bing_sk_SK, + &bing_sv_SE, &bing_th_TH, &bing_tr_TR, &bing_uk_UA, &bing_zh_CN, + &bing_zh_HK, &bing_zh_TW, ¢rum_cz, ¢rum_sk, &daum, &delfi_lt, + &delfi_lv, &diri, &eniro_fi, &eniro_se, &fonecta_02_fi, &go, &goo, + &google, &guruji, &hispavista, &in, &jabse, &jubii, &kvasir, &latne, + &leit, &libero, &mail_ru, &maktoob, &masrawy, &mynet, &najdi, &nate, + &naver, &neti, &netsprint, &nur_kz, &ok, &onet, &pogodak_ba, &pogodak_hr, + &pogodak_rs, &pogodok, &rambler, &rediff, &rednano, &sanook, &sapo, + &search_de_CH, &search_fr_CH, &seznam, &spray, &terra_ar, &terra_es, &tut, + &uol, &virgilio, &walla, &wp, &yahoo, &yahoo_ar, &yahoo_at, &yahoo_au, + &yahoo_br, &yahoo_ca, &yahoo_ch, &yahoo_cl, &yahoo_cn, &yahoo_co, + &yahoo_de, &yahoo_dk, &yahoo_es, &yahoo_fi, &yahoo_fr, &yahoo_hk, + &yahoo_id, &yahoo_in, &yahoo_it, &yahoo_jp, &yahoo_kr, &yahoo_malaysia, + &yahoo_mx, &yahoo_nl, &yahoo_no, &yahoo_nz, &yahoo_pe, &yahoo_ph, + &yahoo_qc, &yahoo_ru, &yahoo_se, &yahoo_sg, &yahoo_th, &yahoo_tw, + &yahoo_uk, &yahoo_ve, &yahoo_vn, &yamli, &yandex_ru, &yandex_ua, + &zoznam }; + + // Geographic mappings ///////////////////////////////////////////////////////// // Please refer to ISO 3166-1 for information about the two-character country @@ -3418,6 +3454,21 @@ void GetPrepopulatedTemplateFromPrefs(PrefService* prefs, } } +// The caller owns the returned TemplateURL. +TemplateURL* MakePrepopulateTemplateURLFromPrepopulateEngine( + const PrepopulatedEngine& engine) { + return MakePrepopulatedTemplateURL(engine.name, + engine.keyword, + engine.search_url, + engine.favicon_url, + engine.suggest_url, + engine.instant_url, + engine.encoding, + engine.search_engine_type, + engine.logo_id, + engine.id); +} + void GetPrepopulatedEngines(PrefService* prefs, std::vector<TemplateURL*>* t_urls, size_t* default_search_provider_index) { @@ -3432,18 +3483,8 @@ void GetPrepopulatedEngines(PrefService* prefs, size_t num_engines; GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); 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]->instant_url, - engines[i]->encoding, - engines[i]->search_engine_type, - engines[i]->logo_id, - engines[i]->id); - t_urls->push_back(turl); + t_urls->push_back( + MakePrepopulateTemplateURLFromPrepopulateEngine(*engines[i])); } } @@ -3461,4 +3502,60 @@ TemplateURL* GetPrepopulatedDefaultSearch(PrefService* prefs) { return default_search_provider; } +// Helper function for the templated function GetOriginForSearchURL. +static const std::string& ToUTF8(const std::string& str) { + return str; +} + +// Helper function for the templated function GetOriginForSearchURL. +static std::string ToUTF8(const wchar_t* str) { + return WideToUTF8(str); +} + +template<typename STR> +static GURL GetOriginForSearchURL(const STR& url_string) { + // It is much faster to parse the url without generating the search URL, so + // try that first. If it fails, fallback to the slow method. + std::string url_utf8_string(ToUTF8(url_string)); + GURL url(url_utf8_string); + if (!url.is_valid()) { + TemplateURL turl; + turl.SetURL(url_utf8_string, 0, 0); + + UIThreadSearchTermsData search_terms_data; + url = TemplateURLModel::GenerateSearchURLUsingTermsData( + &turl, search_terms_data); + } + return url.GetOrigin(); +} + +TemplateURL* GetEngineForOrigin(PrefService* prefs, const GURL& url_to_find) { + GURL origin_to_find = url_to_find.GetOrigin(); + + // Let's first try to find the url in the defaults. (In case the name + // of logo is different for the current locale versus others.) + ScopedVector<TemplateURL> loaded_urls; + size_t default_search_index; + GetPrepopulatedEngines(prefs, &loaded_urls.get(), &default_search_index); + + UIThreadSearchTermsData search_terms_data; + for (std::vector<TemplateURL*>::iterator i = loaded_urls->begin(); + i != loaded_urls->end(); ++i) { + TemplateURL* template_url = *i; + GURL engine_origin(GetOriginForSearchURL((*i)->url()->url())); + if (origin_to_find == engine_origin) { + loaded_urls.weak_erase(i); + return template_url; + } + } + + // Let's try all of known engines now. + for (size_t i = 0; i < arraysize(kAllEngines); ++i) { + GURL engine_origin(GetOriginForSearchURL(kAllEngines[i]->search_url)); + if (origin_to_find == engine_origin) + return MakePrepopulateTemplateURLFromPrepopulateEngine(*kAllEngines[i]); + } + return NULL; +} + } // 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 75a0138..14888a2 100644 --- a/chrome/browser/search_engines/template_url_prepopulate_data.h +++ b/chrome/browser/search_engines/template_url_prepopulate_data.h @@ -8,6 +8,7 @@ #include <vector> +class GURL; class PrefService; class TemplateURL; @@ -72,6 +73,11 @@ void GetPrepopulatedEngines(PrefService* prefs, // The caller owns the returned value, which may be NULL. TemplateURL* GetPrepopulatedDefaultSearch(PrefService* prefs); +// Returns a TemplateURL from the prepopulated data which has the same origin +// as the given url. The caller is responsible for deleting the returned +// TemplateURL. +TemplateURL* GetEngineForOrigin(PrefService* prefs, const GURL& url_to_find); + } // namespace TemplateURLPrepopulateData #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_PREPOPULATE_DATA_H_ 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 37f60e9..4053005 100644 --- a/chrome/browser/search_engines/template_url_prepopulate_data_unittest.cc +++ b/chrome/browser/search_engines/template_url_prepopulate_data_unittest.cc @@ -6,69 +6,73 @@ #include "base/scoped_temp_dir.h" #include "base/scoped_vector.h" #include "chrome/browser/prefs/pref_service.h" +#include "chrome/browser/search_engines/search_terms_data.h" #include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url_prepopulate_data.h" +#include "chrome/browser/search_engines/template_url_model.h" #include "chrome/common/pref_names.h" #include "chrome/test/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" typedef testing::Test TemplateURLPrepopulateDataTest; +const int kCountryIds[] = { + 'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I', + 'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q', + 'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W', + 'A'<<8|'X', 'A'<<8|'Z', 'B'<<8|'A', 'B'<<8|'B', 'B'<<8|'D', + 'B'<<8|'E', 'B'<<8|'F', 'B'<<8|'G', 'B'<<8|'H', 'B'<<8|'I', + 'B'<<8|'J', 'B'<<8|'M', 'B'<<8|'N', 'B'<<8|'O', 'B'<<8|'R', + 'B'<<8|'S', 'B'<<8|'T', 'B'<<8|'V', 'B'<<8|'W', 'B'<<8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'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R', + 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A', + 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z', + 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I', + 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E', + 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 }; + // Verifies the set of prepopulate data doesn't contain entries with duplicate // ids. TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) { - int ids[] = { 'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I', - 'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q', - 'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W', - 'A'<<8|'X', 'A'<<8|'Z', 'B'<<8|'A', 'B'<<8|'B', 'B'<<8|'D', - 'B'<<8|'E', 'B'<<8|'F', 'B'<<8|'G', 'B'<<8|'H', 'B'<<8|'I', - 'B'<<8|'J', 'B'<<8|'M', 'B'<<8|'N', 'B'<<8|'O', 'B'<<8|'R', - 'B'<<8|'S', 'B'<<8|'T', 'B'<<8|'V', 'B'<<8|'W', 'B'<<8|'Y', - 'B'<<8|'Z', 'C'<<8|'A', 'C'<<8|'C', 'C'<<8|'D', 'C'<<8|'F', - 'C'<<8|'G', 'C'<<8|'H', 'C'<<8|'I', 'C'<<8|'K', 'C'<<8|'L', - 'C'<<8|'M', 'C'<<8|'N', 'C'<<8|'O', 'C'<<8|'R', 'C'<<8|'U', - 'C'<<8|'V', 'C'<<8|'X', 'C'<<8|'Y', 'C'<<8|'Z', 'D'<<8|'E', - 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<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E', - 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 }; TestingProfile profile; - for (size_t i = 0; i < arraysize(ids); ++i) { - profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, ids[i]); + for (size_t i = 0; i < arraysize(kCountryIds); ++i) { + profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]); ScopedVector<TemplateURL> urls; size_t default_index; TemplateURLPrepopulateData::GetPrepopulatedEngines( @@ -131,3 +135,46 @@ TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { t_urls[0]->search_engine_type()); EXPECT_EQ(0, t_urls[0]->logo_id()); } + +TEST_F(TemplateURLPrepopulateDataTest, SearchEngineFromOrigin) { + UIThreadSearchTermsData search_terms_data; + std::set<GURL> unique_engines; + + { // Scoping for the profile. + TestingProfile profile; + for (size_t i = 0; i < arraysize(kCountryIds); ++i) { + profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, + kCountryIds[i]); + ScopedVector<TemplateURL> urls; + size_t default_index; + TemplateURLPrepopulateData::GetPrepopulatedEngines( + profile.GetPrefs(), &(urls.get()), &default_index); + for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) { + GURL engine_url(urls[turl_i]->url()->url()); + if (!engine_url.is_valid()) { + engine_url = TemplateURLModel::GenerateSearchURLUsingTermsData( + urls[turl_i], search_terms_data); + } + GURL origin = engine_url.GetOrigin(); + unique_engines.insert(origin); + } + } + } + + TestingProfile profile; + for (std::set<GURL>::iterator it = unique_engines.begin(); + it != unique_engines.end(); ++it) { + scoped_ptr<TemplateURL> found_url( + TemplateURLPrepopulateData::GetEngineForOrigin(profile.GetPrefs(), + *it)); + EXPECT_EQ( + TemplateURLModel::GenerateSearchURLUsingTermsData( + found_url.get(), search_terms_data).GetOrigin(), + it->GetOrigin()); + } + + GURL not_a_search_engine("http://example.com/"); + EXPECT_EQ(NULL, TemplateURLPrepopulateData::GetEngineForOrigin( + profile.GetPrefs(), + not_a_search_engine)); +} |