Commit 623de74f authored by pkasting@chromium.org's avatar pkasting@chromium.org

Clean up TemplateURL prepopulate data:

* Replace a pair of overlapping functions in the API with two distinct accessors for relevant pieces of information about prepopulated URLs. One key side effect here is that we no longer return TemplateURL*s from these, which will make the upcoming refactoring changes slightly cleaner. 
* Make the implementation of these a bit more robust w.r.t. determining if a provided URL corresponds to Google. This will also be important later as we'll want to be able to match a hand-coded "google.co.uk" entry against an auto-generated "google.de" entry (or similar). 
* Remove a bunch of string conversions, as well as a lot of passing of raw C-style string pointers. 
* Remove using statements. 
* Remove two obscure engines which have the same hostname as another, different engine. These cause problems when we ask for the name or type of an engine because the answer we get back is basically indeterminate. I don't think either of these is very important (they lived at slots 4 and 5 in their respective countries) so just gut them. 

BUG=none 
TEST=none
Review URL: https://chromiumcodereview.appspot.com/9705021

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126746 0039d316-1c4b-4281-b951-d872f2087c98
parent 004b3746
......@@ -1415,6 +1415,10 @@ are declared in build/common.gypi.
<ph name="ENGINE_NAME">$1<ex>Google</ex></ph> (Default)
</message>
<message name="IDS_UNKNOWN_SEARCH_ENGINE_NAME" desc="The name given to a search engine for which we can't deduce any information (even a hostname)">
Unknown engine
</message>
<message name="IDS_TIME_SECS_DEFAULT"
desc="This is necessary for every language. This is the default for all the numbers NOT covered by special cases (singular, dual/two, few, many) some languages need. For CJK, Vietnamese, Turkish and Kannada, this is the only string necessary. For languages with singular-plural distinction, this is the generic plural. For Lithuanian, NUMBER_DEFAULT is 11 .. 19.">
<ph name="NUMBER_DEFAULT"><ex>37</ex>#</ph> secs
......
......@@ -44,33 +44,11 @@ bool HasQueryParameter(const std::string& str) {
return false;
}
// True if |url| is an HTTP[S] request with host "[www.]google.<TLD>".
// True if |url| is an HTTP[S] request with host "[www.]google.<TLD>" and no
// explicit port.
bool IsGoogleDomainUrl(const GURL& url) {
if (!url.is_valid())
return false;
// Make sure the scheme is valid.
if (!url.SchemeIs("http") && !url.SchemeIs("https"))
return false;
// Make sure port is default for the respective scheme.
if (!url.port().empty())
return false;
// Accept only valid TLD.
size_t tld_length = net::RegistryControlledDomainService::GetRegistryLength(
url, false);
if (tld_length == 0 || tld_length == std::string::npos)
return false;
// We only accept "[www.]google." in front of the TLD.
std::string host = url.host();
host = host.substr(0, host.length() - tld_length);
if (!LowerCaseEqualsASCII(host, "www.google.") &&
!LowerCaseEqualsASCII(host, "google."))
return false;
return true;
return url.is_valid() && (url.SchemeIs("http") || url.SchemeIs("https")) &&
url.port().empty() && google_util::IsGoogleHostname(url.host());
}
} // anonymous namespace
......@@ -164,6 +142,16 @@ bool GetReactivationBrand(std::string* brand) {
#endif
bool IsGoogleHostname(const std::string& host) {
size_t tld_length =
net::RegistryControlledDomainService::GetRegistryLength(host, false);
if ((tld_length == 0) || (tld_length == std::string::npos))
return false;
std::string host_minus_tld(host, 0, host.length() - tld_length);
return LowerCaseEqualsASCII(host_minus_tld, "www.google.") ||
LowerCaseEqualsASCII(host_minus_tld, "google.");
}
bool IsGoogleHomePageUrl(const std::string& url) {
GURL original_url(url);
......
......@@ -38,6 +38,9 @@ bool GetBrand(std::string* brand);
// install. Returns false if the information is not available.
bool GetReactivationBrand(std::string* brand);
// True if |host| is "[www.]google.<TLD>" with a valid TLD.
bool IsGoogleHostname(const std::string& host);
// True if |url| represents a valid Google home page URL.
bool IsGoogleHomePageUrl(const std::string& url);
......
......@@ -5,7 +5,6 @@
#include "chrome/browser/protector/histograms.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/search_engines/search_engine_type.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
......@@ -42,16 +41,10 @@ const char kProtectorHistogramStartupSettingsTimeout[] =
const int kProtectorMaxSearchProviderID = SEARCH_ENGINE_MAX;
int GetSearchProviderHistogramID(const TemplateURL* turl) {
if (!turl || !turl->url())
return SEARCH_ENGINE_NONE;
scoped_ptr<TemplateURL> prepopulated_url(
TemplateURLPrepopulateData::FindPrepopulatedEngine(turl->url()->url()));
if (prepopulated_url.get())
return static_cast<int>(prepopulated_url->search_engine_type());
// If |turl| is not among the prepopulated providers, return
// SEARCH_ENGINE_OTHER as well.
return SEARCH_ENGINE_OTHER;
int GetSearchProviderHistogramID(const TemplateURL* t_url) {
return (t_url && t_url->url()) ?
TemplateURLPrepopulateData::GetEngineType(t_url->url()->url()) :
SEARCH_ENGINE_NONE;
}
} // namespace protector
......@@ -10,6 +10,9 @@
#include <string>
#include <vector>
#include "base/string16.h"
#include "chrome/browser/search_engines/search_engine_type.h"
class GURL;
class PrefService;
class TemplateURL;
......@@ -35,18 +38,22 @@ void GetPrepopulatedEngines(PrefService* prefs,
// Returns the default search provider specified by the prepopulate data.
// The caller owns the returned value, which may be NULL.
// If |prefs| is NULL, search provider overrides from preferences are not used.
// If |prefs| is NULL, any search provider overrides from the preferences are
// not used.
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);
// Both the next two functions use same-origin checks unless the |url| is a
// Google seach URL, in which case we'll identify any valid Google hostname, or
// the unsubstituted Google prepopulate URL, as "Google".
// Returns the short name for the matching engine, or url.host() if no engines
// match. If no engines match and the |url| can't be converted to a valid GURL,
// returns the string in IDS_UNKNOWN_SEARCH_ENGINE_NAME.
string16 GetEngineName(const std::string& url);
// Returns the prepopulated search provider whose search URL origin matches the
// origin of |search_url| or NULL if none is found. The caller is responsible
// for deleting the returned TemplateURL.
TemplateURL* FindPrepopulatedEngine(const std::string& search_url);
// Returns the type of the matching engine, or SEARCH_ENGINE_OTHER if no engines
// match.
SearchEngineType GetEngineType(const std::string& url);
} // namespace TemplateURLPrepopulateData
......
......@@ -6,7 +6,6 @@
#include "base/memory/scoped_vector.h"
#include "base/scoped_temp_dir.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/search_engines/search_engine_type.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_service.h"
......@@ -14,7 +13,9 @@
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service.h"
#include "chrome/test/base/testing_profile.h"
#include "grit/generated_resources.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
typedef testing::Test TemplateURLPrepopulateDataTest;
......@@ -71,14 +72,14 @@ const int kCountryIds[] = {
// Verifies the set of prepopulate data doesn't contain entries with duplicate
// ids.
TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) {
TEST(TemplateURLPrepopulateDataTest, UniqueIDs) {
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);
TemplateURLPrepopulateData::GetPrepopulatedEngines(profile.GetPrefs(),
&urls.get(), &default_index);
std::set<int> unique_ids;
for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) {
ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id()) ==
......@@ -90,11 +91,11 @@ TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) {
// Verifies that default search providers from the preferences file
// override the built-in ones.
TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) {
TestingPrefService prefs;
TemplateURLPrepopulateData::RegisterUserPrefs(&prefs);
prefs.SetUserPref(prefs::kSearchProviderOverridesVersion,
Value::CreateIntegerValue(1));
TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) {
TestingProfile profile;
TestingPrefService* prefs = profile.GetTestingPrefService();
prefs->SetUserPref(prefs::kSearchProviderOverridesVersion,
Value::CreateIntegerValue(1));
ListValue* overrides = new ListValue;
DictionaryValue* entry = new DictionaryValue;
entry->SetString("name", "foo");
......@@ -107,15 +108,15 @@ TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) {
entry->SetInteger("search_engine_type", 1);
entry->SetInteger("id", 1001);
overrides->Append(entry);
prefs.SetUserPref(prefs::kSearchProviderOverrides, overrides);
prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides);
int version = TemplateURLPrepopulateData::GetDataVersion(&prefs);
int version = TemplateURLPrepopulateData::GetDataVersion(prefs);
EXPECT_EQ(1, version);
ScopedVector<TemplateURL> t_urls;
size_t default_index;
TemplateURLPrepopulateData::GetPrepopulatedEngines(
&prefs, &(t_urls.get()), &default_index);
TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, &t_urls.get(),
&default_index);
ASSERT_EQ(1u, t_urls.size());
EXPECT_EQ(ASCIIToUTF16("foo"), t_urls[0]->short_name());
......@@ -124,53 +125,31 @@ TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) {
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());
EXPECT_EQ(SEARCH_ENGINE_GOOGLE, t_urls[0]->search_engine_type());
}
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 = TemplateURLService::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(
TemplateURLService::GenerateSearchURLUsingTermsData(
found_url.get(), search_terms_data).GetOrigin(),
it->GetOrigin());
}
TEST(TemplateURLPrepopulateDataTest, GetEngineName) {
EXPECT_EQ(ASCIIToUTF16("Atlas"),
TemplateURLPrepopulateData::GetEngineName("http://search.atlas.cz/"));
EXPECT_EQ(ASCIIToUTF16("Google"),
TemplateURLPrepopulateData::GetEngineName("http://www.google.com/"));
EXPECT_EQ(ASCIIToUTF16("example.com"),
TemplateURLPrepopulateData::GetEngineName("http://example.com/"));
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_UNKNOWN_SEARCH_ENGINE_NAME),
TemplateURLPrepopulateData::GetEngineName("!@#"));
}
GURL not_a_search_engine("http://example.com/");
EXPECT_EQ(NULL, TemplateURLPrepopulateData::GetEngineForOrigin(
profile.GetPrefs(),
not_a_search_engine));
TEST(TemplateURLPrepopulateDataTest, GetEngineTypeBasic) {
EXPECT_EQ(SEARCH_ENGINE_OTHER,
TemplateURLPrepopulateData::GetEngineType("http://example.com/"));
EXPECT_EQ(SEARCH_ENGINE_ASK,
TemplateURLPrepopulateData::GetEngineType("http://www.ask.com/"));
EXPECT_EQ(SEARCH_ENGINE_OTHER,
TemplateURLPrepopulateData::GetEngineType("http://search.atlas.cz/"));
EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
TemplateURLPrepopulateData::GetEngineType("http://www.google.com/"));
}
TEST_F(TemplateURLPrepopulateDataTest, FindPrepopulatedEngine) {
TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeAdvanced) {
// Google URLs in different forms.
const char* kGoogleURLs[] = {
// Original with google:baseURL:
......@@ -189,13 +168,9 @@ TEST_F(TemplateURLPrepopulateDataTest, FindPrepopulatedEngine) {
"{google:instantFieldTrialGroupParameter}"
"sourceid=chrome&ie={inputEncoding}&q={searchTerms}"
};
scoped_ptr<TemplateURL> t_url;
for (size_t i = 0; i < arraysize(kGoogleURLs); ++i) {
t_url.reset(
TemplateURLPrepopulateData::FindPrepopulatedEngine(kGoogleURLs[i]));
ASSERT_TRUE(t_url.get());
// Google's prepopulated ID is 1.
EXPECT_EQ(1, t_url->prepopulate_id());
EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
TemplateURLPrepopulateData::GetEngineType(kGoogleURLs[i]));
}
// Non-Google URLs.
const char* kYahooURLs[] = {
......@@ -204,17 +179,13 @@ TEST_F(TemplateURLPrepopulateDataTest, FindPrepopulatedEngine) {
"http://search.yahoo.com/search?p={searchTerms}"
};
for (size_t i = 0; i < arraysize(kYahooURLs); ++i) {
t_url.reset(
TemplateURLPrepopulateData::FindPrepopulatedEngine(kYahooURLs[i]));
ASSERT_TRUE(t_url.get());
// Yahoo!'s prepopulated ID is 2.
EXPECT_EQ(2, t_url->prepopulate_id());
EXPECT_EQ(SEARCH_ENGINE_YAHOO,
TemplateURLPrepopulateData::GetEngineType(kYahooURLs[i]));
}
// Search URL for which no prepopulated search provider exists.
std::string kExampleSearchURL = "http://example.net/search?q={searchTerms}";
EXPECT_FALSE(TemplateURLPrepopulateData::FindPrepopulatedEngine(
kExampleSearchURL));
// Invalid search URL.
EXPECT_FALSE(TemplateURLPrepopulateData::FindPrepopulatedEngine(
"invalid:search:url"));
EXPECT_EQ(SEARCH_ENGINE_OTHER,
TemplateURLPrepopulateData::GetEngineType(kExampleSearchURL));
EXPECT_EQ(SEARCH_ENGINE_OTHER,
TemplateURLPrepopulateData::GetEngineType("invalid:search:url"));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment