diff options
author | dbeam@chromium.org <dbeam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-04 06:49:58 +0000 |
---|---|---|
committer | dbeam@chromium.org <dbeam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-04 06:49:58 +0000 |
commit | d154026cca5ac89e3b9b93acf41296efff5dc5c7 (patch) | |
tree | 9323bf6460d6aaa9cc8510aba349203964f544c7 /chrome/browser | |
parent | b6a0c59e8bb89b56ef158f640b091b0c809443a4 (diff) | |
download | chromium_src-d154026cca5ac89e3b9b93acf41296efff5dc5c7.zip chromium_src-d154026cca5ac89e3b9b93acf41296efff5dc5c7.tar.gz chromium_src-d154026cca5ac89e3b9b93acf41296efff5dc5c7.tar.bz2 |
Revert 130431 - Move the URL string from TemplateURLRef onto the owning TemplateURL. This will make it easier to move the data members of TemplateURL into a new class later.
This changes the accessors for TemplateURL's TemplateURLRefs. There are now separate accessors for the URLs as strings and as TemplateURLRefs, and the latter have changed to returning a const ref, meaning they no longer return NULL when the corresponding URL string is empty. This makes a number of callers clearer.
BUG=none
TEST=none
Review URL: https://chromiumcodereview.appspot.com/9968016
TBR=pkasting@chromium.org
Review URL: https://chromiumcodereview.appspot.com/9965143
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@130566 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
45 files changed, 754 insertions, 679 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit.cc b/chrome/browser/autocomplete/autocomplete_edit.cc index abb1d3d..c5c9ff9 100644 --- a/chrome/browser/autocomplete/autocomplete_edit.cc +++ b/chrome/browser/autocomplete/autocomplete_edit.cc @@ -495,7 +495,8 @@ void AutocompleteEditModel::AcceptInput(WindowOpenDisposition disposition, } const TemplateURL* template_url = match.GetTemplateURL(); - if (template_url && template_url->url_ref().HasGoogleBaseURLs()) { + if (template_url && template_url->url() && + template_url->url()->HasGoogleBaseURLs()) { GoogleURLTracker::GoogleURLSearchCommitted(); #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) // TODO(pastarmovj): Remove these metrics once we have proven that (close diff --git a/chrome/browser/autocomplete/keyword_provider.cc b/chrome/browser/autocomplete/keyword_provider.cc index 98ed031..68e7e9d 100644 --- a/chrome/browser/autocomplete/keyword_provider.cc +++ b/chrome/browser/autocomplete/keyword_provider.cc @@ -364,15 +364,16 @@ void KeywordProvider::FillInURLAndContents( const TemplateURL* element, AutocompleteMatch* match) { DCHECK(!element->short_name().empty()); - const TemplateURLRef& element_ref = element->url_ref(); - DCHECK(element_ref.IsValid()); + const TemplateURLRef* element_ref = element->url(); + DCHECK(element_ref); + DCHECK(element_ref->IsValid()); int message_id = element->IsExtensionKeyword() ? IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH; if (remaining_input.empty()) { // Allow extension keyword providers to accept empty string input. This is // useful to allow extensions to do something in the case where no input is // entered. - if (element_ref.SupportsReplacement() && !element->IsExtensionKeyword()) { + if (element_ref->SupportsReplacement() && !element->IsExtensionKeyword()) { // No query input; return a generic, no-destination placeholder. match->contents.assign( l10n_util::GetStringFUTF16(message_id, @@ -382,7 +383,7 @@ void KeywordProvider::FillInURLAndContents( ACMatchClassification(0, ACMatchClassification::DIM)); } else { // Keyword that has no replacement text (aka a shorthand for a URL). - match->destination_url = GURL(element->url()); + match->destination_url = GURL(element_ref->url()); match->contents.assign(element->short_name()); AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(), match->contents.length(), ACMatchClassification::NONE, @@ -393,10 +394,10 @@ void KeywordProvider::FillInURLAndContents( // keyword template URL. The escaping here handles whitespace in user // input, but we rely on later canonicalization functions to do more // fixup to make the URL valid if necessary. - DCHECK(element_ref.SupportsReplacement()); - match->destination_url = GURL(element_ref.ReplaceSearchTermsUsingProfile( - profile, remaining_input, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, - string16())); + DCHECK(element_ref->SupportsReplacement()); + match->destination_url = GURL(element_ref-> + ReplaceSearchTermsUsingProfile(profile, remaining_input, + TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); std::vector<size_t> content_param_offsets; match->contents.assign(l10n_util::GetStringFUTF16(message_id, element->short_name(), @@ -438,8 +439,8 @@ AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( // Get keyword data from data store. const TemplateURL* element( model->GetTemplateURLForKeyword(keyword)); - DCHECK(element && !element->url().empty()); - const bool supports_replacement = element->url_ref().SupportsReplacement(); + DCHECK(element && element->url()); + const bool supports_replacement = element->url()->SupportsReplacement(); // Create an edit entry of "[keyword] [remaining input]". This is helpful // even when [remaining input] is empty, as the user can select the popup diff --git a/chrome/browser/autocomplete/search_provider.cc b/chrome/browser/autocomplete/search_provider.cc index 078947c..3770635 100644 --- a/chrome/browser/autocomplete/search_provider.cc +++ b/chrome/browser/autocomplete/search_provider.cc @@ -455,10 +455,10 @@ content::URLFetcher* SearchProvider::CreateSuggestFetcher( int id, const TemplateURL& provider, const string16& text) { - const TemplateURLRef& suggestions_url = provider.suggestions_url_ref(); - DCHECK(suggestions_url.SupportsReplacement()); + const TemplateURLRef* const suggestions_url = provider.suggestions_url(); + DCHECK(suggestions_url->SupportsReplacement()); content::URLFetcher* fetcher = content::URLFetcher::Create(id, - GURL(suggestions_url.ReplaceSearchTermsUsingProfile( + GURL(suggestions_url->ReplaceSearchTermsUsingProfile( profile_, text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), content::URLFetcher::GET, this); @@ -901,9 +901,9 @@ void SearchProvider::AddMatchToMap(const string16& query_string, input_text)) match.inline_autocomplete_offset = search_start + input_text.length(); - const TemplateURLRef& search_url = provider.url_ref(); - DCHECK(search_url.SupportsReplacement()); - match.destination_url = GURL(search_url.ReplaceSearchTermsUsingProfile( + const TemplateURLRef* const search_url = provider.url(); + DCHECK(search_url->SupportsReplacement()); + match.destination_url = GURL(search_url->ReplaceSearchTermsUsingProfile( profile_, query_string, accepted_suggestion, input_text)); // Search results don't look like URLs. diff --git a/chrome/browser/autocomplete/search_provider.h b/chrome/browser/autocomplete/search_provider.h index 9c5d26a..e3c0a9c 100644 --- a/chrome/browser/autocomplete/search_provider.h +++ b/chrome/browser/autocomplete/search_provider.h @@ -120,8 +120,7 @@ class SearchProvider : public AutocompleteProvider, // Returns true if the keyword provider is valid and has a valid suggest // url. bool valid_suggest_for_keyword_provider() const { - return keyword_provider_ && - !cached_keyword_provider_.suggestions_url().empty(); + return keyword_provider_ && cached_keyword_provider_.suggestions_url(); } // Returns true of the default provider is valid. @@ -130,8 +129,7 @@ class SearchProvider : public AutocompleteProvider, // Returns true if the default provider is valid and has a valid suggest // url. bool valid_suggest_for_default_provider() const { - return default_provider_ && - !cached_default_provider_.suggestions_url().empty(); + return default_provider_ && cached_default_provider_.suggestions_url(); } // Returns true if |from_keyword_provider| is true, or diff --git a/chrome/browser/autocomplete/search_provider_unittest.cc b/chrome/browser/autocomplete/search_provider_unittest.cc index d10d466..cadcc7f 100644 --- a/chrome/browser/autocomplete/search_provider_unittest.cc +++ b/chrome/browser/autocomplete/search_provider_unittest.cc @@ -199,7 +199,7 @@ void SearchProviderTest::QueryForInputAndSetWYTMatch( return; ASSERT_GE(provider_->matches().size(), 1u); EXPECT_TRUE(FindMatchWithDestination(GURL( - default_t_url_->url_ref().ReplaceSearchTerms(text, + default_t_url_->url()->ReplaceSearchTerms(text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), wyt_match)); } @@ -215,7 +215,7 @@ GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, int visit_count) { HistoryService* history = profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); - GURL search(t_url->url_ref().ReplaceSearchTerms(term, + GURL search(t_url->url()->ReplaceSearchTerms(term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); static base::Time last_added_time; last_added_time = std::max(base::Time::Now(), @@ -262,7 +262,7 @@ TEST_F(SearchProviderTest, QueryDefaultProvider) { ASSERT_TRUE(fetcher); // And the URL matches what we expected. - GURL expected_url(default_t_url_->suggestions_url_ref().ReplaceSearchTerms( + GURL expected_url(default_t_url_->suggestions_url()->ReplaceSearchTerms( term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(fetcher->GetOriginalURL() == expected_url); @@ -283,7 +283,7 @@ TEST_F(SearchProviderTest, QueryDefaultProvider) { AutocompleteMatch wyt_match; EXPECT_TRUE(FindMatchWithDestination( - GURL(default_t_url_->url_ref().ReplaceSearchTerms(term, + GURL(default_t_url_->url()->ReplaceSearchTerms(term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), &wyt_match)); EXPECT_TRUE(wyt_match.description.empty()); @@ -322,7 +322,7 @@ TEST_F(SearchProviderTest, QueryKeywordProvider) { ASSERT_TRUE(keyword_fetcher); // And the URL matches what we expected. - GURL expected_url(keyword_t_url_->suggestions_url_ref().ReplaceSearchTerms( + GURL expected_url(keyword_t_url_->suggestions_url()->ReplaceSearchTerms( term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(keyword_fetcher->GetOriginalURL() == expected_url); @@ -391,7 +391,7 @@ TEST_F(SearchProviderTest, FinalizeInstantQuery) { // There should be two matches, one for what you typed, the other for // 'foobar'. EXPECT_EQ(2u, provider_->matches().size()); - GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( + GURL instant_url(default_t_url_->url()->ReplaceSearchTerms( ASCIIToUTF16("foobar"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); AutocompleteMatch instant_match; @@ -403,7 +403,7 @@ TEST_F(SearchProviderTest, FinalizeInstantQuery) { // Make sure the what you typed match has no description. AutocompleteMatch wyt_match; EXPECT_TRUE(FindMatchWithDestination( - GURL(default_t_url_->url_ref().ReplaceSearchTerms(ASCIIToUTF16("foo"), + GURL(default_t_url_->url()->ReplaceSearchTerms(ASCIIToUTF16("foo"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), &wyt_match)); EXPECT_TRUE(wyt_match.description.empty()); @@ -425,7 +425,7 @@ TEST_F(SearchProviderTest, RememberInstantQuery) { // There should be two matches, one for what you typed, the other for // 'foobar'. EXPECT_EQ(2u, provider_->matches().size()); - GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( + GURL instant_url(default_t_url_->url()->ReplaceSearchTerms( ASCIIToUTF16("foobar"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); AutocompleteMatch instant_match; diff --git a/chrome/browser/automation/testing_automation_provider.cc b/chrome/browser/automation/testing_automation_provider.cc index ee53ec73..eb24beb 100644 --- a/chrome/browser/automation/testing_automation_provider.cc +++ b/chrome/browser/automation/testing_automation_provider.cc @@ -3371,14 +3371,14 @@ void TestingAutomationProvider::GetSearchEngineInfo( search_engine->SetBoolean("in_default_list", (*it)->ShowInDefaultList()); search_engine->SetBoolean("is_default", (*it) == url_model->GetDefaultSearchProvider()); - search_engine->SetBoolean("is_valid", (*it)->url_ref().IsValid()); + search_engine->SetBoolean("is_valid", (*it)->url()->IsValid()); search_engine->SetBoolean("supports_replacement", - (*it)->url_ref().SupportsReplacement()); - search_engine->SetString("url", (*it)->url()); - search_engine->SetString("host", (*it)->url_ref().GetHost()); - search_engine->SetString("path", (*it)->url_ref().GetPath()); + (*it)->url()->SupportsReplacement()); + search_engine->SetString("url", (*it)->url()->url()); + search_engine->SetString("host", (*it)->url()->GetHost()); + search_engine->SetString("path", (*it)->url()->GetPath()); search_engine->SetString("display_url", - UTF16ToUTF8((*it)->url_ref().DisplayURL())); + UTF16ToUTF8((*it)->url()->DisplayURL())); search_engines->Append(search_engine); } return_value->Set("search_engines", search_engines); diff --git a/chrome/browser/chrome_browser_main.cc b/chrome/browser/chrome_browser_main.cc index 135b1c8..97de7c0 100644 --- a/chrome/browser/chrome_browser_main.cc +++ b/chrome/browser/chrome_browser_main.cc @@ -1886,8 +1886,9 @@ void ChromeBrowserMainParts::PostMainMessageLoopRun() { // The default engine can be NULL if the administrator has disabled // default search. SearchEngineType search_engine_type = - TemplateURLPrepopulateData::GetEngineType(default_search_engine ? - default_search_engine->url() : std::string()); + TemplateURLPrepopulateData::GetEngineType( + (default_search_engine && default_search_engine->url()) ? + default_search_engine->url()->url() : std::string()); // Record the search engine chosen. UMA_HISTOGRAM_ENUMERATION("Chrome.SearchSelectExempt", search_engine_type, SEARCH_ENGINE_MAX); diff --git a/chrome/browser/importer/firefox_importer_unittest.cc b/chrome/browser/importer/firefox_importer_unittest.cc index e684209..cba48a5 100644 --- a/chrome/browser/importer/firefox_importer_unittest.cc +++ b/chrome/browser/importer/firefox_importer_unittest.cc @@ -191,7 +191,7 @@ class FirefoxObserver : public ProfileWriter, for (size_t j = 0; j < arraysize(kFirefox2Keywords); ++j) { if (template_urls[i]->keyword() == WideToUTF16Hack(kFirefox2Keywords[j].keyword)) { - EXPECT_EQ(kFirefox2Keywords[j].url, template_urls[i]->url()); + EXPECT_EQ(kFirefox2Keywords[j].url, template_urls[i]->url()->url()); found = true; break; } @@ -341,7 +341,7 @@ class Firefox3Observer : public ProfileWriter, for (size_t j = 0; j < arraysize(kFirefox3Keywords); ++j) { if (template_urls[i]->keyword() == WideToUTF16Hack(kFirefox3Keywords[j].keyword)) { - EXPECT_EQ(kFirefox3Keywords[j].url, template_urls[i]->url()); + EXPECT_EQ(kFirefox3Keywords[j].url, template_urls[i]->url()->url()); found = true; break; } diff --git a/chrome/browser/importer/firefox_importer_utils.cc b/chrome/browser/importer/firefox_importer_utils.cc index 5904940..db7e48c 100644 --- a/chrome/browser/importer/firefox_importer_utils.cc +++ b/chrome/browser/importer/firefox_importer_utils.cc @@ -203,11 +203,11 @@ void ParseSearchEnginesFromXMLFiles(const std::vector<FilePath>& xml_files, TemplateURL* template_url = TemplateURLParser::Parse(NULL, content.data(), content.length(), ¶m_filter); if (template_url) { - SearchEnginesMap::iterator iter = - search_engine_for_url.find(template_url->url()); + std::string url = template_url->url()->url(); + SearchEnginesMap::iterator iter = search_engine_for_url.find(url); if (iter == search_engine_for_url.end()) { iter = search_engine_for_url.insert( - std::make_pair(template_url->url(), template_url)).first; + std::make_pair(url, template_url)).first; } else { // We have already found a search engine with the same URL. We give // priority to the latest one found, as GetSearchEnginesXMLFiles() diff --git a/chrome/browser/importer/profile_import_process_messages.h b/chrome/browser/importer/profile_import_process_messages.h index ff9407a..6af66fc 100644 --- a/chrome/browser/importer/profile_import_process_messages.h +++ b/chrome/browser/importer/profile_import_process_messages.h @@ -199,9 +199,13 @@ struct ParamTraits<TemplateURL*> { typedef TemplateURL* param_type; static void Write(Message* m, const param_type& p) { WriteParam(m, p->short_name()); - WriteParam(m, p->url()); - WriteParam(m, p->suggestions_url()); - WriteParam(m, p->instant_url()); + if (p->suggestions_url()) { + WriteParam(m, true); + WriteParam(m, p->suggestions_url()->url()); + } else { + WriteParam(m, false); + } + WriteParam(m, p->url()->url()); WriteParam(m, p->originating_url()); WriteParam(m, p->keyword()); WriteParam(m, p->autogenerate_keyword()); @@ -215,10 +219,12 @@ struct ParamTraits<TemplateURL*> { WriteParam(m, p->prepopulate_id()); } static bool Read(const Message* m, PickleIterator* iter, param_type* p) { + *p = NULL; + string16 short_name; - std::string url; + bool includes_suggestions_url; std::string suggestions_url; - std::string instant_url; + std::string url; GURL originating_url; string16 keyword; bool autogenerate_keyword; @@ -229,36 +235,47 @@ struct ParamTraits<TemplateURL*> { base::Time last_modified; int usage_count; int prepopulate_id; - if (!ReadParam(m, iter, &short_name) || - !ReadParam(m, iter, &url) || - !ReadParam(m, iter, &suggestions_url) || - !ReadParam(m, iter, &instant_url) || + + if (!ReadParam(m, iter, &short_name)) + return false; + + if (!ReadParam(m, iter, &includes_suggestions_url)) + return false; + if (includes_suggestions_url) { + if (!ReadParam(m, iter, &suggestions_url)) + return false; + } + + if (!ReadParam(m, iter, &url) || !ReadParam(m, iter, &originating_url) || !ReadParam(m, iter, &keyword) || !ReadParam(m, iter, &autogenerate_keyword) || !ReadParam(m, iter, &show_in_default_list) || - !ReadParam(m, iter, &safe_for_autoreplace) || - !ReadParam(m, iter, &favicon_url) || + !ReadParam(m, iter, &safe_for_autoreplace)) + return false; + + scoped_ptr<TemplateURL> turl(new TemplateURL()); + if (!ReadParam(m, iter, &favicon_url) || !ReadParam(m, iter, &date_created) || !ReadParam(m, iter, &last_modified) || !ReadParam(m, iter, &usage_count) || !ReadParam(m, iter, &prepopulate_id)) return false; - *p = new TemplateURL(); - (*p)->set_short_name(short_name); - (*p)->SetURL(url); - (*p)->SetSuggestionsURL(suggestions_url); - (*p)->SetInstantURL(suggestions_url); - (*p)->set_originating_url(originating_url); - (*p)->set_keyword(keyword); - (*p)->set_autogenerate_keyword(autogenerate_keyword); - (*p)->set_show_in_default_list(show_in_default_list); - (*p)->set_safe_for_autoreplace(safe_for_autoreplace); - (*p)->set_favicon_url(favicon_url); - (*p)->set_date_created(date_created); - (*p)->set_last_modified(last_modified); - (*p)->set_usage_count(usage_count); - (*p)->SetPrepopulateId(prepopulate_id); + + turl->set_short_name(short_name); + turl->SetSuggestionsURL(suggestions_url); + turl->SetURL(url); + turl->set_originating_url(originating_url); + turl->set_keyword(keyword); + turl->set_autogenerate_keyword(autogenerate_keyword); + turl->set_show_in_default_list(show_in_default_list); + turl->set_safe_for_autoreplace(safe_for_autoreplace); + turl->set_favicon_url(favicon_url); + turl->set_date_created(date_created); + turl->set_last_modified(last_modified); + turl->set_usage_count(usage_count); + turl->SetPrepopulateId(prepopulate_id); + *p = turl.release(); return true; } static void Log(const param_type& p, std::string* l) { diff --git a/chrome/browser/importer/profile_writer.cc b/chrome/browser/importer/profile_writer.cc index bf81c3bc..2102067 100644 --- a/chrome/browser/importer/profile_writer.cc +++ b/chrome/browser/importer/profile_writer.cc @@ -255,13 +255,13 @@ static std::string HostPathKeyForURL(const GURL& url) { // the TemplateURL is invalid. static std::string BuildHostPathKey(const TemplateURL* t_url, bool try_url_if_invalid) { - if (!t_url->url().empty()) { - if (try_url_if_invalid && !t_url->url_ref().IsValid()) - return HostPathKeyForURL(GURL(t_url->url())); + if (t_url->url()) { + if (try_url_if_invalid && !t_url->url()->IsValid()) + return HostPathKeyForURL(GURL(t_url->url()->url())); - if (t_url->url_ref().SupportsReplacement()) { + if (t_url->url()->SupportsReplacement()) { return HostPathKeyForURL(GURL( - t_url->url_ref().ReplaceSearchTerms(ASCIIToUTF16("x"), + t_url->url()->ReplaceSearchTerms(ASCIIToUTF16("x"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()))); } } @@ -317,7 +317,7 @@ void ProfileWriter::AddKeywords(ScopedVector<TemplateURL> template_urls, continue; // Only add valid TemplateURLs to the model. - if (!(*i)->url().empty() && (*i)->url_ref().IsValid()) { + if ((*i)->url() && (*i)->url()->IsValid()) { model->Add(*i); // Takes ownership. *i = NULL; // Prevent the vector from deleting *i later. } diff --git a/chrome/browser/instant/instant_browsertest.cc b/chrome/browser/instant/instant_browsertest.cc index 1c96356..12e4f22 100644 --- a/chrome/browser/instant/instant_browsertest.cc +++ b/chrome/browser/instant/instant_browsertest.cc @@ -282,8 +282,9 @@ IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(OnChangeEvent)) { TemplateURLServiceFactory::GetForProfile(browser()->profile())-> GetDefaultSearchProvider(); EXPECT_TRUE(default_turl); - EXPECT_EQ(default_turl->url_ref().ReplaceSearchTerms(ASCIIToUTF16("defghi"), - 0, string16()), loader()->url().spec()); + EXPECT_TRUE(default_turl->url()); + EXPECT_EQ(default_turl->url()->ReplaceSearchTerms(ASCIIToUTF16("defghi"), 0, + string16()), loader()->url().spec()); // Check that the value is reflected and onchange is called. EXPECT_EQ("true 0 0 1 true d false def false 3 3", diff --git a/chrome/browser/instant/instant_controller.cc b/chrome/browser/instant/instant_controller.cc index a13a193..ef6ec95 100644 --- a/chrome/browser/instant/instant_controller.cc +++ b/chrome/browser/instant/instant_controller.cc @@ -498,8 +498,8 @@ void InstantController::UpdateLoader(const TemplateURL* template_url, // Returns true if |template_url| is a valid TemplateURL for use by instant. bool InstantController::IsValidInstantTemplateURL( const TemplateURL* template_url) { - return template_url && template_url->id() && - template_url->instant_url_ref().SupportsReplacement() && + return template_url && template_url->instant_url() && template_url->id() && + template_url->instant_url()->SupportsReplacement() && !IsBlacklistedFromInstant(template_url->id()); } diff --git a/chrome/browser/instant/instant_loader.cc b/chrome/browser/instant/instant_loader.cc index e6feafe..5c22dba 100644 --- a/chrome/browser/instant/instant_loader.cc +++ b/chrome/browser/instant/instant_loader.cc @@ -1127,9 +1127,8 @@ void InstantLoader::LoadInstantURL(TabContentsWrapper* tab_contents, // functionality so that embeded tags (like {google:baseURL}) are escaped // correctly. // TODO(sky): having to use a replaceable url is a bit of a hack here. - GURL instant_url( - template_url->instant_url_ref().ReplaceSearchTermsUsingProfile( - tab_contents->profile(), string16(), -1, string16())); + GURL instant_url(template_url->instant_url()->ReplaceSearchTermsUsingProfile( + tab_contents->profile(), string16(), -1, string16())); CommandLine* cl = CommandLine::ForCurrentProcess(); if (cl->HasSwitch(switches::kInstantURL)) instant_url = GURL(cl->GetSwitchValueASCII(switches::kInstantURL)); diff --git a/chrome/browser/omnibox_search_hint.cc b/chrome/browser/omnibox_search_hint.cc index f9802eb..09f4610 100644 --- a/chrome/browser/omnibox_search_hint.cc +++ b/chrome/browser/omnibox_search_hint.cc @@ -190,7 +190,7 @@ void OmniboxSearchHint::Observe(int type, if (!default_provider) return; - if (default_provider->url_ref().GetHost() == entry->GetURL().host()) + if (default_provider->url()->GetHost() == entry->GetURL().host()) ShowInfoBar(); } else if (type == chrome::NOTIFICATION_OMNIBOX_OPENED_URL) { AutocompleteLog* log = content::Details<AutocompleteLog>(details).ptr(); diff --git a/chrome/browser/policy/configuration_policy_handler.cc b/chrome/browser/policy/configuration_policy_handler.cc index 238f5df..6f65bf9 100644 --- a/chrome/browser/policy/configuration_policy_handler.cc +++ b/chrome/browser/policy/configuration_policy_handler.cc @@ -598,9 +598,9 @@ bool DefaultSearchPolicyHandler::DefaultSearchURLIsValid( if (!*url_value || !(*url_value)->GetAsString(url_string)) return false; TemplateURL t_url; - t_url.SetURL(*url_string); SearchTermsData search_terms_data; - return t_url.url_ref().SupportsReplacementUsingTermsData(search_terms_data); + return TemplateURLRef(&t_url, *url_string).SupportsReplacementUsingTermsData( + search_terms_data); } void DefaultSearchPolicyHandler::EnsureStringPrefExists( diff --git a/chrome/browser/protector/default_search_provider_change.cc b/chrome/browser/protector/default_search_provider_change.cc index 28df6ed..7c8c019 100644 --- a/chrome/browser/protector/default_search_provider_change.cc +++ b/chrome/browser/protector/default_search_provider_change.cc @@ -54,11 +54,13 @@ class TemplateURLIsSame { return false; return url->short_name() == other_->short_name() && AreKeywordsSame(url, other_) && - url->url() == other_->url() && - url->suggestions_url() == other_->suggestions_url() && - url->instant_url() == other_->instant_url() && - url->safe_for_autoreplace() == other_->safe_for_autoreplace() && + TemplateURLRef::SameUrlRefs(url->url(), other_->url()) && + TemplateURLRef::SameUrlRefs(url->suggestions_url(), + other_->suggestions_url()) && + TemplateURLRef::SameUrlRefs(url->instant_url(), + other_->instant_url()) && url->favicon_url() == other_->favicon_url() && + url->safe_for_autoreplace() == other_->safe_for_autoreplace() && url->show_in_default_list() == other_->show_in_default_list() && url->input_encodings() == other_->input_encodings() && url->prepopulate_id() == other_->prepopulate_id(); diff --git a/chrome/browser/protector/default_search_provider_change_browsertest.cc b/chrome/browser/protector/default_search_provider_change_browsertest.cc index 5a50ce5..2494919 100644 --- a/chrome/browser/protector/default_search_provider_change_browsertest.cc +++ b/chrome/browser/protector/default_search_provider_change_browsertest.cc @@ -79,7 +79,7 @@ class DefaultSearchProviderChangeTest : public InProcessBrowserTest { turl_service_->GetTemplateURLs(); for (TemplateURLService::TemplateURLVector::const_iterator it = urls.begin(); it != urls.end(); ++it) { - if ((*it)->url() == search_url) + if ((*it)->url()->url() == search_url) return *it; } return NULL; @@ -257,7 +257,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupInvalid) { AddAndSetDefault(current_url); // Prepopulated default search must exist. - ASSERT_TRUE(FindTemplateURL(prepopulated_url_->url())); + ASSERT_TRUE(FindTemplateURL(prepopulated_url_->url()->url())); scoped_ptr<BaseSettingChange> change( CreateDefaultSearchProviderChange(current_url, NULL)); @@ -265,7 +265,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupInvalid) { ASSERT_TRUE(change->Init(browser()->profile())); // Verify that the prepopulated default search is active. - EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), turl_service_->GetDefaultSearchProvider()); // Verify histograms. @@ -286,7 +286,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupInvalid) { // Verify that search engine settings are opened by Discard. ExpectSettingsOpened(chrome::kSearchEnginesSubPage); change->Discard(browser()); - EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), turl_service_->GetDefaultSearchProvider()); // Verify that Apply switches back to |current_url|. @@ -310,7 +310,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, AddAndSetDefault(current_url); const TemplateURL* prepopulated_default = - FindTemplateURL(prepopulated_url_->url()); + FindTemplateURL(prepopulated_url_->url()->url()); // Prepopulated default search must exist, remove it. ASSERT_TRUE(prepopulated_default); turl_service_->Remove(prepopulated_default); @@ -321,7 +321,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, ASSERT_TRUE(change->Init(browser()->profile())); // Verify that the prepopulated default search is active. - EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), turl_service_->GetDefaultSearchProvider()); // Verify histograms. @@ -344,7 +344,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, // Verify that search engine settings are opened by Discard. ExpectSettingsOpened(chrome::kSearchEnginesSubPage); change->Discard(browser()); - EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), turl_service_->GetDefaultSearchProvider()); // Verify that Apply switches back to |current_url|. @@ -410,7 +410,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, ASSERT_TRUE(change->Init(browser()->profile())); // Verify that the prepopulated default search is active. - EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), turl_service_->GetDefaultSearchProvider()); // Verify histograms. @@ -430,7 +430,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, // Verify that search engine settings are opened by Discard. ExpectSettingsOpened(chrome::kSearchEnginesSubPage); change->Discard(browser()); - EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), turl_service_->GetDefaultSearchProvider()); } @@ -444,7 +444,7 @@ IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, // Verify that current search provider is same as the prepopulated default. ASSERT_TRUE(current_url); - ASSERT_EQ(current_url, FindTemplateURL(prepopulated_url_->url())); + ASSERT_EQ(current_url, FindTemplateURL(prepopulated_url_->url()->url())); scoped_ptr<BaseSettingChange> change( CreateDefaultSearchProviderChange(current_url, NULL)); diff --git a/chrome/browser/protector/histograms.cc b/chrome/browser/protector/histograms.cc index 0418d89..c0ba393 100644 --- a/chrome/browser/protector/histograms.cc +++ b/chrome/browser/protector/histograms.cc @@ -45,8 +45,8 @@ const char kProtectorHistogramStartupSettingsTimeout[] = const int kProtectorMaxSearchProviderID = SEARCH_ENGINE_MAX; int GetSearchProviderHistogramID(const TemplateURL* t_url) { - return t_url ? - TemplateURLPrepopulateData::GetEngineType(t_url->url()) : + return (t_url && t_url->url()) ? + TemplateURLPrepopulateData::GetEngineType(t_url->url()->url()) : SEARCH_ENGINE_NONE; } diff --git a/chrome/browser/search_engines/search_host_to_urls_map.cc b/chrome/browser/search_engines/search_host_to_urls_map.cc index 3810f06..af8257e 100644 --- a/chrome/browser/search_engines/search_host_to_urls_map.cc +++ b/chrome/browser/search_engines/search_host_to_urls_map.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -88,8 +88,9 @@ void SearchHostToURLsMap::UpdateGoogleBaseURLs( for (TemplateURLSet::const_iterator url_set_iterator = urls.begin(); url_set_iterator != urls.end(); ++url_set_iterator) { const TemplateURL* t_url = *url_set_iterator; - if (t_url->url_ref().HasGoogleBaseURLs() || - t_url->suggestions_url_ref().HasGoogleBaseURLs()) { + if ((t_url->url() && t_url->url()->HasGoogleBaseURLs()) || + (t_url->suggestions_url() && + t_url->suggestions_url()->HasGoogleBaseURLs())) { t_urls_using_base_url.push_back(t_url); } } diff --git a/chrome/browser/search_engines/template_url.cc b/chrome/browser/search_engines/template_url.cc index 1cb143d..504d757 100644 --- a/chrome/browser/search_engines/template_url.cc +++ b/chrome/browser/search_engines/template_url.cc @@ -86,26 +86,21 @@ static const char kOutputEncodingType[] = "UTF-8"; // TemplateURLRef ------------------------------------------------------------- -TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type) +TemplateURLRef::TemplateURLRef(TemplateURL* owner) : owner_(owner), - type_(type), - parsed_(false), - valid_(false), - supports_replacements_(false), prepopulated_(false) { DCHECK(owner_); + Set(std::string()); } -TemplateURLRef::~TemplateURLRef() { +TemplateURLRef::TemplateURLRef(TemplateURL* owner, const std::string& url) + : owner_(owner), + prepopulated_(false) { + DCHECK(owner_); + Set(url); } -std::string TemplateURLRef::GetURL() const { - switch (type_) { - case SEARCH: return owner_->url(); - case SUGGEST: return owner_->suggestions_url(); - case INSTANT: return owner_->instant_url(); - default: NOTREACHED(); return std::string(); - } +TemplateURLRef::~TemplateURLRef() { } bool TemplateURLRef::SupportsReplacement() const { @@ -292,15 +287,19 @@ bool TemplateURLRef::IsValidUsingTermsData( string16 TemplateURLRef::DisplayURL() const { ParseIfNecessary(); - string16 result(UTF8ToUTF16(GetURL())); - if (valid_ && !replacements_.empty()) { - ReplaceSubstringsAfterOffset(&result, 0, - ASCIIToUTF16(kSearchTermsParameterFull), - ASCIIToUTF16(kDisplaySearchTerms)); - ReplaceSubstringsAfterOffset(&result, 0, - ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull), - ASCIIToUTF16(kDisplayUnescapedSearchTerms)); - } + if (!valid_ || replacements_.empty()) + return UTF8ToUTF16(url_); + + string16 result = UTF8ToUTF16(url_); + ReplaceSubstringsAfterOffset(&result, 0, + ASCIIToUTF16(kSearchTermsParameterFull), + ASCIIToUTF16(kDisplaySearchTerms)); + + ReplaceSubstringsAfterOffset( + &result, 0, + ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull), + ASCIIToUTF16(kDisplayUnescapedSearchTerms)); + return result; } @@ -369,6 +368,12 @@ bool TemplateURLRef::HasGoogleBaseURLs() const { return false; } +// static +bool TemplateURLRef::SameUrlRefs(const TemplateURLRef* ref1, + const TemplateURLRef* ref2) { + return ref1 == ref2 || (ref1 && ref2 && ref1->url() == ref2->url()); +} + void TemplateURLRef::CollectRLZMetrics() const { #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) ParseIfNecessary(); @@ -399,6 +404,11 @@ void TemplateURLRef::InvalidateCachedValues() const { replacements_.clear(); } +void TemplateURLRef::Set(const std::string& url) { + url_ = url; + InvalidateCachedValues(); +} + bool TemplateURLRef::ParseParameter(size_t start, size_t end, std::string* url, @@ -508,7 +518,7 @@ void TemplateURLRef::ParseIfNecessaryUsingTermsData( const SearchTermsData& search_terms_data) const { if (!parsed_) { parsed_ = true; - parsed_url_ = ParseURL(GetURL(), &replacements_, &valid_); + parsed_url_ = ParseURL(url_, &replacements_, &valid_); supports_replacements_ = false; if (valid_) { bool has_only_one_search_term = false; @@ -534,7 +544,7 @@ void TemplateURLRef::ParseIfNecessaryUsingTermsData( void TemplateURLRef::ParseHostAndSearchTermKey( const SearchTermsData& search_terms_data) const { - std::string url_string(GetURL()); + std::string url_string = url_; ReplaceSubstringsAfterOffset(&url_string, 0, kGoogleBaseURLParameterFull, search_terms_data.GoogleBaseURLValue()); @@ -573,7 +583,10 @@ void TemplateURLRef::ParseHostAndSearchTermKey( // TemplateURL ---------------------------------------------------------------- TemplateURL::TemplateURL() - : autogenerate_keyword_(false), + : url_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), + suggestions_url_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), + instant_url_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), + autogenerate_keyword_(false), keyword_generated_(false), show_in_default_list_(false), safe_for_autoreplace_(false), @@ -583,19 +596,14 @@ TemplateURL::TemplateURL() created_by_policy_(false), usage_count_(0), prepopulate_id_(0), - sync_guid_(guid::GenerateGUID()), - url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), TemplateURLRef::SEARCH), - suggestions_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - TemplateURLRef::SUGGEST), - instant_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - TemplateURLRef::INSTANT) { + sync_guid_(guid::GenerateGUID()) { } TemplateURL::TemplateURL(const TemplateURL& other) : short_name_(other.short_name_), - url_(other.url_), - suggestions_url_(other.suggestions_url_), - instant_url_(other.instant_url_), + url_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), + suggestions_url_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), + instant_url_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), originating_url_(other.originating_url_), keyword_(other.keyword_), autogenerate_keyword_(other.autogenerate_keyword_), @@ -609,12 +617,7 @@ TemplateURL::TemplateURL(const TemplateURL& other) last_modified_(other.last_modified_), created_by_policy_(other.created_by_policy_), usage_count_(other.usage_count_), - sync_guid_(other.sync_guid_), - url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), TemplateURLRef::SEARCH), - suggestions_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - TemplateURLRef::SUGGEST), - instant_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - TemplateURLRef::INSTANT) { + sync_guid_(other.sync_guid_) { CopyURLRefs(other); } @@ -623,9 +626,7 @@ TemplateURL& TemplateURL::operator=(const TemplateURL& other) { return *this; short_name_ = other.short_name_; - url_ = other.url_; - suggestions_url_ = other.suggestions_url_; - instant_url_ = other.instant_url_; + CopyURLRefs(other); originating_url_ = other.originating_url_; keyword_ = other.keyword_; autogenerate_keyword_ = other.autogenerate_keyword_; @@ -640,7 +641,6 @@ TemplateURL& TemplateURL::operator=(const TemplateURL& other) { created_by_policy_ = other.created_by_policy_; usage_count_ = other.usage_count_; sync_guid_ = other.sync_guid_; - CopyURLRefs(other); return *this; } @@ -670,18 +670,15 @@ string16 TemplateURL::AdjustedShortNameForLocaleDirection() const { } void TemplateURL::SetURL(const std::string& url) { - url_ = url; - url_ref_.InvalidateCachedValues(); + url_.Set(url); } void TemplateURL::SetSuggestionsURL(const std::string& url) { - suggestions_url_ = url; - suggestions_url_ref_.InvalidateCachedValues(); + suggestions_url_.Set(url); } void TemplateURL::SetInstantURL(const std::string& url) { - instant_url_ = url; - instant_url_ref_.InvalidateCachedValues(); + instant_url_.Set(url); } void TemplateURL::set_keyword(const string16& keyword) { @@ -706,28 +703,27 @@ void TemplateURL::EnsureKeyword() const { } bool TemplateURL::ShowInDefaultList() const { - return show_in_default_list() && url_ref_.SupportsReplacement(); + return show_in_default_list() && url() && url()->SupportsReplacement(); } void TemplateURL::CopyURLRefs(const TemplateURL& other) { - url_ref_.InvalidateCachedValues(); - suggestions_url_ref_.InvalidateCachedValues(); - instant_url_ref_.InvalidateCachedValues(); + suggestions_url_.Set(other.suggestions_url_.url_); + url_.Set(other.url_.url_); + instant_url_.Set(other.instant_url_.url_); SetPrepopulateId(other.prepopulate_id_); } void TemplateURL::SetPrepopulateId(int id) { prepopulate_id_ = id; const bool prepopulated = id > 0; - suggestions_url_ref_.prepopulated_ = prepopulated; - url_ref_.prepopulated_ = prepopulated; - instant_url_ref_.prepopulated_ = prepopulated; + suggestions_url_.prepopulated_ = prepopulated; + url_.prepopulated_ = prepopulated; + instant_url_.prepopulated_ = prepopulated; } void TemplateURL::InvalidateCachedValues() const { - url_ref_.InvalidateCachedValues(); - suggestions_url_ref_.InvalidateCachedValues(); - instant_url_ref_.InvalidateCachedValues(); + url_.InvalidateCachedValues(); + suggestions_url_.InvalidateCachedValues(); if (autogenerate_keyword_) { keyword_.clear(); keyword_generated_ = false; @@ -741,14 +737,14 @@ bool TemplateURL::SupportsReplacement() const { bool TemplateURL::SupportsReplacementUsingTermsData( const SearchTermsData& search_terms_data) const { - return url_ref_.SupportsReplacementUsingTermsData(search_terms_data); + return url_.SupportsReplacementUsingTermsData(search_terms_data); } std::string TemplateURL::GetExtensionId() const { DCHECK(IsExtensionKeyword()); - return GURL(url_).host(); + return GURL(url_.url()).host(); } bool TemplateURL::IsExtensionKeyword() const { - return GURL(url_).SchemeIs(chrome::kExtensionScheme); + return GURL(url_.url()).SchemeIs(chrome::kExtensionScheme); } diff --git a/chrome/browser/search_engines/template_url.h b/chrome/browser/search_engines/template_url.h index 78889a6..d7ef4a5 100644 --- a/chrome/browser/search_engines/template_url.h +++ b/chrome/browser/search_engines/template_url.h @@ -50,20 +50,10 @@ class TemplateURLRef { NO_SUGGESTIONS_AVAILABLE = -2, }; - // Which kind of URL within our owner we are. This allows us to get at the - // correct string field. - enum Type { - SEARCH, - SUGGEST, - INSTANT, - }; - - TemplateURLRef(TemplateURL* owner, Type type); + explicit TemplateURLRef(TemplateURL* owner); + TemplateURLRef(TemplateURL* owner, const std::string& url); ~TemplateURLRef(); - // Returns the raw URL. None of the parameters will have been replaced. - std::string GetURL() const; - // Returns true if this URL supports replacement. bool SupportsReplacement() const; @@ -99,6 +89,9 @@ class TemplateURLRef { const string16& original_query_for_suggestion, const SearchTermsData& search_terms_data) const; + // Returns the raw URL. None of the parameters will have been replaced. + const std::string& url() const { return url_; } + // Returns true if the TemplateURLRef is valid. An invalid TemplateURLRef is // one that contains unknown terms, or invalid characters. bool IsValid() const; @@ -130,6 +123,10 @@ class TemplateURLRef { // {google:baseURL} or {google:baseSuggestURL}. bool HasGoogleBaseURLs() const; + // Returns true if both refs are NULL or have the same values. + static bool SameUrlRefs(const TemplateURLRef* ref1, + const TemplateURLRef* ref2); + // Collects metrics whether searches through Google are sent with RLZ string. void CollectRLZMetrics() const; @@ -175,6 +172,9 @@ class TemplateURLRef { // method invalidates any cached values. void InvalidateCachedValues() const; + // Resets the url. + void Set(const std::string& url); + // Parses the parameter in url at the specified offset. start/end specify the // range of the parameter in the url, including the braces. If the parameter // is valid, url is updated to reflect the appropriate parameter. If @@ -212,10 +212,11 @@ class TemplateURLRef { const SearchTermsData& search_terms_data) const; // The TemplateURL that contains us. This should outlive us. - TemplateURL* const owner_; + TemplateURL* owner_; - // What kind of URL we are. - const Type type_; + // The raw URL. Where as this contains all the terms (such as {searchTerms}), + // parsed_url_ has them all stripped out. + std::string url_; // Whether the URL has been parsed. mutable bool parsed_; @@ -266,22 +267,38 @@ class TemplateURL { short_name_ = short_name; } const string16& short_name() const { return short_name_; } + // An accessor for the short_name, but adjusted so it can be appropriately // displayed even if it is LTR and the UI is RTL. string16 AdjustedShortNameForLocaleDirection() const; - // Parameterized URL for providing the results. + // Parameterized URL for providing the results. This may be NULL. + // Be sure and check the resulting TemplateURLRef for SupportsReplacement + // before using. void SetURL(const std::string& url); - const std::string& url() const { return url_; } + // Returns the TemplateURLRef that may be used for search results. This + // returns NULL if a url element was not specified. + const TemplateURLRef* url() const { + return url_.url().empty() ? NULL : &url_; + } // URL providing JSON results. This is typically used to provide suggestions - // as you type. + // as your type. If NULL, this url does not support suggestions. + // Be sure and check the resulting TemplateURLRef for SupportsReplacement + // before using. void SetSuggestionsURL(const std::string& url); - const std::string& suggestions_url() const { return suggestions_url_; } + const TemplateURLRef* suggestions_url() const { + return suggestions_url_.url().empty() ? NULL : &suggestions_url_; + } - // Parameterized URL for instant results. + // Parameterized URL for instant results. This may be NULL. Be sure and check + // the resulting TemplateURLRef for SupportsReplacement before using. void SetInstantURL(const std::string& url); - const std::string& instant_url() const { return instant_url_; } + // Returns the TemplateURLRef that may be used for search results. This + // returns NULL if a url element was not specified. + const TemplateURLRef* instant_url() const { + return instant_url_.url().empty() ? NULL : &instant_url_; + } // URL to the OSD file this came from. May be empty. void set_originating_url(const GURL& url) { @@ -397,12 +414,6 @@ class TemplateURL { const std::string& sync_guid() const { return sync_guid_; } void set_sync_guid(const std::string& guid) { sync_guid_ = guid; } - const TemplateURLRef& url_ref() const { return url_ref_; } - const TemplateURLRef& suggestions_url_ref() const { - return suggestions_url_ref_; - } - const TemplateURLRef& instant_url_ref() const { return instant_url_ref_; } - // Returns true if |url| supports replacement. bool SupportsReplacement() const; @@ -434,9 +445,9 @@ class TemplateURL { void set_id(TemplateURLID id) { id_ = id; } string16 short_name_; - std::string url_; - std::string suggestions_url_; - std::string instant_url_; + TemplateURLRef url_; + TemplateURLRef suggestions_url_; + TemplateURLRef instant_url_; GURL originating_url_; mutable string16 keyword_; bool autogenerate_keyword_; // If this is set, |keyword_| holds the cached @@ -459,10 +470,6 @@ class TemplateURL { // that have been associated with Sync. std::string sync_guid_; - TemplateURLRef url_ref_; - TemplateURLRef suggestions_url_ref_; - TemplateURLRef instant_url_ref_; - // TODO(sky): Add date last parsed OSD file. }; diff --git a/chrome/browser/search_engines/template_url_fetcher.cc b/chrome/browser/search_engines/template_url_fetcher.cc index ac1ff53..11e7fc1 100644 --- a/chrome/browser/search_engines/template_url_fetcher.cc +++ b/chrome/browser/search_engines/template_url_fetcher.cc @@ -149,7 +149,7 @@ void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete( template_url_.reset(TemplateURLParser::Parse(fetcher_->profile(), data.data(), data.length(), NULL)); - if (!template_url_.get() || !template_url_->url_ref().SupportsReplacement()) { + if (!template_url_.get() || !template_url_->url()->SupportsReplacement()) { fetcher_->RequestCompleted(this); // WARNING: RequestCompleted deletes us. return; @@ -183,7 +183,7 @@ void TemplateURLFetcher::RequestDelegate::AddSearchProvider() { DCHECK(model->loaded()); const TemplateURL* existing_url = NULL; - if (model->CanReplaceKeyword(keyword_, GURL(template_url_->url()), + if (model->CanReplaceKeyword(keyword_, GURL(template_url_->url()->url()), &existing_url)) { if (existing_url) model->Remove(existing_url); diff --git a/chrome/browser/search_engines/template_url_fetcher_unittest.cc b/chrome/browser/search_engines/template_url_fetcher_unittest.cc index 324e337..d9f2167 100644 --- a/chrome/browser/search_engines/template_url_fetcher_unittest.cc +++ b/chrome/browser/search_engines/template_url_fetcher_unittest.cc @@ -180,7 +180,7 @@ TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) { keyword); ASSERT_TRUE(t_url); EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), - t_url->url_ref().DisplayURL()); + t_url->url()->DisplayURL()); EXPECT_TRUE(t_url->safe_for_autoreplace()); } @@ -246,7 +246,7 @@ TEST_F(TemplateURLFetcherTest, BasicExplicitTest) { ASSERT_TRUE(last_callback_template_url_.get()); EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), - last_callback_template_url_->url_ref().DisplayURL()); + last_callback_template_url_->url()->DisplayURL()); EXPECT_EQ(ASCIIToUTF16("example.com"), last_callback_template_url_->keyword()); EXPECT_FALSE(last_callback_template_url_->safe_for_autoreplace()); @@ -279,7 +279,7 @@ TEST_F(TemplateURLFetcherTest, ExplicitBeforeLoadTest) { ASSERT_TRUE(last_callback_template_url_.get()); EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), - last_callback_template_url_->url_ref().DisplayURL()); + last_callback_template_url_->url()->DisplayURL()); EXPECT_EQ(ASCIIToUTF16("example.com"), last_callback_template_url_->keyword()); EXPECT_FALSE(last_callback_template_url_->safe_for_autoreplace()); diff --git a/chrome/browser/search_engines/template_url_parser.cc b/chrome/browser/search_engines/template_url_parser.cc index 974c771..c2f7ea2 100644 --- a/chrome/browser/search_engines/template_url_parser.cc +++ b/chrome/browser/search_engines/template_url_parser.cc @@ -94,13 +94,14 @@ void AppendParamToQuery(const std::string& key, query->append(value); } -// Returns true if |url| is empty or is a valid URL with a scheme of HTTP[S]. -bool IsHTTPRef(const std::string& url) { - if (url.empty()) +// Returns true if the ref is null, or the url wrapped by ref is +// valid with a spec of http/https. +bool IsHTTPRef(const TemplateURLRef* ref) { + if (ref == NULL) return true; - GURL gurl(url); - return (gurl.is_valid() && (gurl.SchemeIs(chrome::kHttpScheme) || - gurl.SchemeIs(chrome::kHttpsScheme))); + GURL url(ref->url()); + return (url.is_valid() && (url.SchemeIs(chrome::kHttpScheme) || + url.SchemeIs(chrome::kHttpsScheme))); } } // namespace @@ -298,7 +299,7 @@ TemplateURL* TemplateURLParsingContext::GetTemplateURL(Profile* profile) { // If the image was a data URL, use the favicon from the search URL instead. // (see TODO inEndElementImpl()). - GURL url(url_->url()); + GURL url(url_->url()->url()); if (derive_image_from_url_ && url_->favicon_url().is_empty()) url_->set_favicon_url(TemplateURL::GenerateFaviconURL(url)); @@ -413,10 +414,11 @@ void TemplateURLParsingContext::ProcessURLParams() { if (!parameter_filter_ && extra_params_.empty()) return; - GURL url(is_suggest_url_ ? url_->suggestions_url() : url_->url()); - if (url.is_empty()) + const TemplateURLRef* t_url_ref = + is_suggest_url_ ? url_->suggestions_url() : url_->url(); + if (!t_url_ref) return; - + GURL url(t_url_ref->url()); // If there is a parameter filter, parse the existing URL and remove any // unwanted parameter. std::string new_query; diff --git a/chrome/browser/search_engines/template_url_parser_unittest.cc b/chrome/browser/search_engines/template_url_parser_unittest.cc index 2ad1ae1..e57a444 100644 --- a/chrome/browser/search_engines/template_url_parser_unittest.cc +++ b/chrome/browser/search_engines/template_url_parser_unittest.cc @@ -135,9 +135,10 @@ TEST_F(TemplateURLParserTest, TestDictionary) { EXPECT_EQ(ASCIIToUTF16("Dictionary.com"), template_url_->short_name()); EXPECT_EQ(GURL("http://cache.lexico.com/g/d/favicon.ico"), template_url_->favicon_url()); - EXPECT_TRUE(template_url_->url_ref().SupportsReplacement()); + ASSERT_FALSE(template_url_->url() == NULL); + EXPECT_TRUE(template_url_->url()->SupportsReplacement()); EXPECT_EQ("http://dictionary.reference.com/browse/{searchTerms}?r=75", - template_url_->url()); + template_url_->url()->url()); } TEST_F(TemplateURLParserTest, TestMSDN) { @@ -148,10 +149,11 @@ TEST_F(TemplateURLParserTest, TestMSDN) { EXPECT_EQ(ASCIIToUTF16("Search \" MSDN"), template_url_->short_name()); EXPECT_EQ(GURL("http://search.msdn.microsoft.com/search/favicon.ico"), template_url_->favicon_url()); - EXPECT_TRUE(template_url_->url_ref().SupportsReplacement()); + ASSERT_FALSE(template_url_->url() == NULL); + EXPECT_TRUE(template_url_->url()->SupportsReplacement()); EXPECT_EQ("http://search.msdn.microsoft.com/search/default.aspx?" "Query={searchTerms}&brand=msdn&locale=en-US", - template_url_->url()); + template_url_->url()->url()); } TEST_F(TemplateURLParserTest, TestWikipedia) { @@ -162,14 +164,16 @@ TEST_F(TemplateURLParserTest, TestWikipedia) { EXPECT_EQ(ASCIIToUTF16("Wikipedia (English)"), template_url_->short_name()); EXPECT_EQ(GURL("http://en.wikipedia.org/favicon.ico"), template_url_->favicon_url()); - EXPECT_TRUE(template_url_->url_ref().SupportsReplacement()); + ASSERT_FALSE(template_url_->url() == NULL); + EXPECT_TRUE(template_url_->url()->SupportsReplacement()); EXPECT_EQ("http://en.wikipedia.org/w/index.php?" "title=Special:Search&search={searchTerms}", - template_url_->url()); - EXPECT_TRUE(template_url_->suggestions_url_ref().SupportsReplacement()); + template_url_->url()->url()); + ASSERT_FALSE(template_url_->suggestions_url() == NULL); + EXPECT_TRUE(template_url_->suggestions_url()->SupportsReplacement()); EXPECT_EQ("http://en.wikipedia.org/w/api.php?" "action=opensearch&search={searchTerms}", - template_url_->suggestions_url()); + template_url_->suggestions_url()->url()); ASSERT_EQ(2U, template_url_->input_encodings().size()); EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]); EXPECT_EQ("Shift_JIS", template_url_->input_encodings()[1]); @@ -190,11 +194,12 @@ TEST_F(TemplateURLParserTest, TestFirefoxEbay) { ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_ebay.xml", &filter)); ASSERT_TRUE(template_url_.get()); EXPECT_EQ(ASCIIToUTF16("eBay"), template_url_->short_name()); - EXPECT_TRUE(template_url_->url_ref().SupportsReplacement()); + ASSERT_FALSE(template_url_->url() == NULL); + EXPECT_TRUE(template_url_->url()->SupportsReplacement()); EXPECT_EQ("http://search.ebay.com/search/search.dll?query={searchTerms}&" "MfcISAPICommand=GetResult&ht=1&srchdesc=n&maxRecordsReturned=300&" "maxRecordsPerPage=50&SortProperty=MetaEndSort", - template_url_->url()); + template_url_->url()->url()); ASSERT_EQ(1U, template_url_->input_encodings().size()); EXPECT_EQ("ISO-8859-1", template_url_->input_encodings()[0]); EXPECT_EQ(GURL("http://search.ebay.com/favicon.ico"), @@ -209,9 +214,10 @@ TEST_F(TemplateURLParserTest, TestFirefoxWebster) { ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_webster.xml", &filter)); ASSERT_TRUE(template_url_.get()); EXPECT_EQ(ASCIIToUTF16("Webster"), template_url_->short_name()); - EXPECT_TRUE(template_url_->url_ref().SupportsReplacement()); + ASSERT_FALSE(template_url_->url() == NULL); + EXPECT_TRUE(template_url_->url()->SupportsReplacement()); EXPECT_EQ("http://www.webster.com/cgi-bin/dictionary?va={searchTerms}", - template_url_->url()); + template_url_->url()->url()); ASSERT_EQ(1U, template_url_->input_encodings().size()); EXPECT_EQ("ISO-8859-1", template_url_->input_encodings()[0]); EXPECT_EQ(GURL("http://www.webster.com/favicon.ico"), @@ -226,12 +232,13 @@ TEST_F(TemplateURLParserTest, TestFirefoxYahoo) { ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_yahoo.xml", &filter)); ASSERT_TRUE(template_url_.get()); EXPECT_EQ(ASCIIToUTF16("Yahoo"), template_url_->short_name()); - EXPECT_TRUE(template_url_->url_ref().SupportsReplacement()); + ASSERT_FALSE(template_url_->url() == NULL); + EXPECT_TRUE(template_url_->url()->SupportsReplacement()); EXPECT_EQ("http://ff.search.yahoo.com/gossip?" "output=fxjson&command={searchTerms}", - template_url_->suggestions_url()); + template_url_->suggestions_url()->url()); EXPECT_EQ("http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8", - template_url_->url()); + template_url_->url()->url()); ASSERT_EQ(1U, template_url_->input_encodings().size()); EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]); EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"), @@ -248,10 +255,11 @@ TEST_F(TemplateURLParserTest, TestPostSuggestion) { ASSERT_NO_FATAL_FAILURE(ParseFile("post_suggestion.xml", &filter)); ASSERT_TRUE(template_url_.get()); EXPECT_EQ(ASCIIToUTF16("Yahoo"), template_url_->short_name()); - EXPECT_TRUE(template_url_->url_ref().SupportsReplacement()); - EXPECT_TRUE(template_url_->suggestions_url().empty()); + ASSERT_FALSE(template_url_->url() == NULL); + EXPECT_TRUE(template_url_->url()->SupportsReplacement()); + EXPECT_TRUE(template_url_->suggestions_url() == NULL); EXPECT_EQ("http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8", - template_url_->url()); + template_url_->url()->url()); ASSERT_EQ(1U, template_url_->input_encodings().size()); EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]); EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"), 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 ee91e85..3d53787 100644 --- a/chrome/browser/search_engines/template_url_prepopulate_data_unittest.cc +++ b/chrome/browser/search_engines/template_url_prepopulate_data_unittest.cc @@ -120,7 +120,7 @@ TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { ASSERT_EQ(1u, t_urls.size()); EXPECT_EQ(ASCIIToUTF16("foo"), t_urls[0]->short_name()); EXPECT_EQ(ASCIIToUTF16("fook"), t_urls[0]->keyword()); - EXPECT_EQ("foo.com", t_urls[0]->url_ref().GetHost()); + EXPECT_EQ("foo.com", t_urls[0]->url()->GetHost()); EXPECT_EQ("foi.com", t_urls[0]->favicon_url().host()); EXPECT_EQ(1u, t_urls[0]->input_encodings().size()); EXPECT_EQ(1001, t_urls[0]->prepopulate_id()); diff --git a/chrome/browser/search_engines/template_url_service.cc b/chrome/browser/search_engines/template_url_service.cc index 30987d8..00c3a60 100644 --- a/chrome/browser/search_engines/template_url_service.cc +++ b/chrome/browser/search_engines/template_url_service.cc @@ -71,9 +71,9 @@ bool TemplateURLsHaveSamePrefs(const TemplateURL* url1, NULL != url2 && url1->short_name() == url2->short_name() && url1->keyword() == url2->keyword() && - url1->url() == url2->url() && - url1->suggestions_url() == url2->suggestions_url() && - url1->instant_url() == url2->instant_url() && + TemplateURLRef::SameUrlRefs(url1->url(), url2->url()) && + TemplateURLRef::SameUrlRefs(url1->suggestions_url(), + url2->suggestions_url()) && url1->favicon_url() == url2->favicon_url() && url1->safe_for_autoreplace() == url2->safe_for_autoreplace() && url1->show_in_default_list() == url2->show_in_default_list() && @@ -219,16 +219,16 @@ GURL TemplateURLService::GenerateSearchURLUsingTermsData( const TemplateURL* t_url, const SearchTermsData& search_terms_data) { DCHECK(t_url); - const TemplateURLRef& search_ref = t_url->url_ref(); + const TemplateURLRef* search_ref = t_url->url(); // Extension keywords don't have host-based search URLs. - if (!search_ref.IsValidUsingTermsData(search_terms_data) || + if (!search_ref || !search_ref->IsValidUsingTermsData(search_terms_data) || t_url->IsExtensionKeyword()) return GURL(); - if (!search_ref.SupportsReplacementUsingTermsData(search_terms_data)) - return GURL(t_url->url()); + if (!search_ref->SupportsReplacementUsingTermsData(search_terms_data)) + return GURL(search_ref->url()); - return GURL(search_ref.ReplaceSearchTermsUsingTermsData( + return GURL(search_ref->ReplaceSearchTermsUsingTermsData( ASCIIToUTF16(kReplacementTerm), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(), search_terms_data)); } @@ -285,8 +285,8 @@ void TemplateURLService::FindMatchingKeywords( // Return vector of matching keywords. for (KeywordToTemplateMap::const_iterator i(match_range.first); i != match_range.second; ++i) { - DCHECK(!i->second->url().empty()); - if (!support_replacement_only || i->second->url_ref().SupportsReplacement()) + DCHECK(i->second->url()); + if (!support_replacement_only || i->second->url()->SupportsReplacement()) matches->push_back(i->first); } } @@ -398,8 +398,7 @@ const TemplateURL* TemplateURLService::GetTemplateURLForExtension( const Extension* extension) const { for (TemplateURLVector::const_iterator i = template_urls_.begin(); i != template_urls_.end(); ++i) { - if ((*i)->IsExtensionKeyword() && - ((*i)->url_ref().GetHost() == extension->id())) + if ((*i)->IsExtensionKeyword() && (*i)->url()->GetHost() == extension->id()) return *i; } @@ -425,7 +424,9 @@ void TemplateURLService::ResetTemplateURL(const TemplateURL* url, TemplateURL new_url(*url); new_url.set_short_name(title); new_url.set_keyword(keyword); - if (new_url.url() != search_url) { + if ((new_url.url() && search_url.empty()) || + (!new_url.url() && !search_url.empty()) || + (new_url.url() && new_url.url()->url() != search_url)) { new_url.SetURL(search_url); // The urls have changed, reset the favicon url. new_url.set_favicon_url(GURL()); @@ -438,7 +439,9 @@ void TemplateURLService::ResetTemplateURL(const TemplateURL* url, bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { return url != GetDefaultSearchProvider() && - url->url_ref().SupportsReplacement() && !is_default_search_managed(); + url->url() && + url->url()->SupportsReplacement() && + !is_default_search_managed(); } void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { @@ -974,16 +977,18 @@ SyncData TemplateURLService::CreateSyncDataFromTemplateURL( se_specifics->set_short_name(UTF16ToUTF8(turl.short_name())); se_specifics->set_keyword(UTF16ToUTF8(turl.keyword())); se_specifics->set_favicon_url(turl.favicon_url().spec()); - se_specifics->set_url(turl.url()); + se_specifics->set_url(turl.url() ? turl.url()->url() : std::string()); se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace()); se_specifics->set_originating_url(turl.originating_url().spec()); se_specifics->set_date_created(turl.date_created().ToInternalValue()); se_specifics->set_input_encodings(JoinString(turl.input_encodings(), ';')); se_specifics->set_show_in_default_list(turl.show_in_default_list()); - se_specifics->set_suggestions_url(turl.suggestions_url()); + se_specifics->set_suggestions_url(turl.suggestions_url() ? + turl.suggestions_url()->url() : std::string()); se_specifics->set_prepopulate_id(turl.prepopulate_id()); se_specifics->set_autogenerate_keyword(turl.autogenerate_keyword()); - se_specifics->set_instant_url(turl.instant_url()); + se_specifics->set_instant_url(turl.instant_url() ? + turl.instant_url()->url() : std::string()); se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); se_specifics->set_sync_guid(turl.sync_guid()); return SyncData::CreateLocalData(se_specifics->sync_guid(), @@ -1070,12 +1075,15 @@ void TemplateURLService::Init(const Initializer* initializers, // Request a server check for the correct Google URL if Google is the // default search engine, not in headless mode and not in Chrome Frame. - if (initial_default_search_provider_.get() && - initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) { - scoped_ptr<base::Environment> env(base::Environment::Create()); - if (!env->HasVar(env_vars::kHeadless) && - !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) - GoogleURLTracker::RequestServerCheck(); + if (initial_default_search_provider_.get()) { + const TemplateURLRef* default_provider_ref = + initial_default_search_provider_->url(); + if (default_provider_ref && default_provider_ref->HasGoogleBaseURLs()) { + scoped_ptr<base::Environment> env(base::Environment::Create()); + if (!env->HasVar(env_vars::kHeadless) && + !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) + GoogleURLTracker::RequestServerCheck(); + } } } @@ -1180,9 +1188,12 @@ void TemplateURLService::SaveDefaultSearchProviderToPrefs( std::string prepopulate_id; if (t_url) { enabled = true; - search_url = t_url->url(); - suggest_url = t_url->suggestions_url(); - instant_url = t_url->instant_url(); + if (t_url->url()) + search_url = t_url->url()->url(); + if (t_url->suggestions_url()) + suggest_url = t_url->suggestions_url()->url(); + if (t_url->instant_url()) + instant_url = t_url->instant_url()->url(); GURL icon_gurl = t_url->favicon_url(); if (!icon_gurl.is_empty()) icon_url = icon_gurl.spec(); @@ -1347,7 +1358,7 @@ void TemplateURLService::UpdateKeywordSearchTermsForURL( for (TemplateURLSet::const_iterator i = urls_for_host->begin(); i != urls_for_host->end(); ++i) { - const TemplateURLRef& search_ref = (*i)->url_ref(); + const TemplateURLRef* search_ref = (*i)->url(); // Count the URL against a TemplateURL if the host and path of the // visited URL match that of the TemplateURL as well as the search term's @@ -1358,8 +1369,8 @@ void TemplateURLService::UpdateKeywordSearchTermsForURL( // particular, GetHost returns an empty string if search_ref doesn't support // replacement or isn't valid for use in keyword search terms. - if (search_ref.GetHost() == row.url().host() && - search_ref.GetPath() == path) { + if (search_ref && search_ref->GetHost() == row.url().host() && + search_ref->GetPath() == path) { if (!built_terms && !BuildQueryTerms(row.url(), &query_terms)) { // No query terms. No need to continue with the rest of the // TemplateURLs. @@ -1376,11 +1387,11 @@ void TemplateURLService::UpdateKeywordSearchTermsForURL( } QueryTerms::iterator terms_iterator = - query_terms.find(search_ref.GetSearchTermKey()); + query_terms.find(search_ref->GetSearchTermKey()); if (terms_iterator != query_terms.end() && !terms_iterator->second.empty()) { SetKeywordSearchTermsForURL(*i, row.url(), - search_ref.SearchTermToString16(terms_iterator->second)); + search_ref->SearchTermToString16(terms_iterator->second)); } } } @@ -1450,8 +1461,9 @@ void TemplateURLService::GoogleBaseURLChanged() { bool something_changed = false; for (size_t i = 0; i < template_urls_.size(); ++i) { const TemplateURL* t_url = template_urls_[i]; - if (t_url->url_ref().HasGoogleBaseURLs() || - t_url->suggestions_url_ref().HasGoogleBaseURLs()) { + if ((t_url->url() && t_url->url()->HasGoogleBaseURLs()) || + (t_url->suggestions_url() && + t_url->suggestions_url()->HasGoogleBaseURLs())) { RemoveFromKeywordMapByPointer(t_url); t_url->InvalidateCachedValues(); if (!t_url->keyword().empty()) @@ -1573,7 +1585,8 @@ void TemplateURLService::SetDefaultSearchProviderNoNotify( if (service_.get()) service_.get()->UpdateKeyword(*url); - if (url->url_ref().HasGoogleBaseURLs()) { + const TemplateURLRef* url_ref = url->url(); + if (url_ref && url_ref->HasGoogleBaseURLs()) { GoogleURLTracker::RequestServerCheck(); #if defined(ENABLE_RLZ) // Needs to be evaluated. See http://crbug.com/62328. @@ -1729,8 +1742,8 @@ string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const { return turl.keyword(); // First, try to return the generated keyword for the TemplateURL. - GURL gurl(turl.url()); - string16 keyword_candidate = GenerateKeyword(gurl, true); + string16 keyword_candidate = GenerateKeyword( + turl.url() ? GURL(turl.url()->url()) : GURL(), true); if (!GetTemplateURLForKeyword(keyword_candidate) && !keyword_candidate.empty()) { return keyword_candidate; @@ -1741,7 +1754,7 @@ string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const { // keyword and let the user do what they will after our attempt. keyword_candidate = turl.keyword(); do { - keyword_candidate.append(ASCIIToUTF16("_")); + keyword_candidate.append(UTF8ToUTF16("_")); } while (GetTemplateURLForKeyword(keyword_candidate)); return keyword_candidate; @@ -1785,8 +1798,8 @@ const TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( if (!existing_turl) return NULL; - if (!existing_turl->url().empty() && - existing_turl->url() == sync_turl.url()) { + if (existing_turl->url() && sync_turl.url() && + existing_turl->url()->url() == sync_turl.url()->url()) { return existing_turl; } return NULL; diff --git a/chrome/browser/search_engines/template_url_service_sync_unittest.cc b/chrome/browser/search_engines/template_url_service_sync_unittest.cc index 8179cea..3bad1bc 100644 --- a/chrome/browser/search_engines/template_url_service_sync_unittest.cc +++ b/chrome/browser/search_engines/template_url_service_sync_unittest.cc @@ -82,28 +82,37 @@ void RemoveManagedDefaultSearchPreferences(TemplateURLService* turl_service, pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID); } - -// TestChangeProcessor -------------------------------------------------------- - // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed // back up to Sync. class TestChangeProcessor : public SyncChangeProcessor { public: - TestChangeProcessor(); - virtual ~TestChangeProcessor(); + TestChangeProcessor() : erroneous_(false) { + } + virtual ~TestChangeProcessor() { } // Store a copy of all the changes passed in so we can examine them later. virtual SyncError ProcessSyncChanges( const tracked_objects::Location& from_here, - const SyncChangeList& change_list) OVERRIDE; + const SyncChangeList& change_list) { + if (erroneous_) + return SyncError(FROM_HERE, "Some error.", syncable::SEARCH_ENGINES); + + change_map_.erase(change_map_.begin(), change_map_.end()); + for (SyncChangeList::const_iterator iter = change_list.begin(); + iter != change_list.end(); ++iter) { + change_map_[GetGUID(iter->sync_data())] = *iter; + } - bool contains_guid(const std::string& guid) const { - return change_map_.count(guid) != 0; + return SyncError(); } - SyncChange change_for_guid(const std::string& guid) const { - DCHECK(contains_guid(guid)); - return change_map_.find(guid)->second; + bool ContainsGUID(const std::string& guid) { + return change_map_.find(guid) != change_map_.end(); + } + + SyncChange GetChangeByGUID(const std::string& guid) { + DCHECK(ContainsGUID(guid)); + return change_map_[guid]; } int change_list_size() { return change_map_.size(); } @@ -118,37 +127,20 @@ class TestChangeProcessor : public SyncChangeProcessor { DISALLOW_COPY_AND_ASSIGN(TestChangeProcessor); }; -TestChangeProcessor::TestChangeProcessor() : erroneous_(false) { -} - -TestChangeProcessor::~TestChangeProcessor() { -} - -SyncError TestChangeProcessor::ProcessSyncChanges( - const tracked_objects::Location& from_here, - const SyncChangeList& change_list) { - if (erroneous_) - return SyncError(FROM_HERE, "Some error.", syncable::SEARCH_ENGINES); - - change_map_.erase(change_map_.begin(), change_map_.end()); - for (SyncChangeList::const_iterator iter = change_list.begin(); - iter != change_list.end(); ++iter) - change_map_[GetGUID(iter->sync_data())] = *iter; - return SyncError(); -} - - -// SyncChangeProcessorDelegate ------------------------------------------------ - class SyncChangeProcessorDelegate : public SyncChangeProcessor { public: - explicit SyncChangeProcessorDelegate(SyncChangeProcessor* recipient); - virtual ~SyncChangeProcessorDelegate(); + explicit SyncChangeProcessorDelegate(SyncChangeProcessor* recipient) + : recipient_(recipient) { + DCHECK(recipient_); + } + virtual ~SyncChangeProcessorDelegate() {} // SyncChangeProcessor implementation. virtual SyncError ProcessSyncChanges( const tracked_objects::Location& from_here, - const SyncChangeList& change_list) OVERRIDE; + const SyncChangeList& change_list) OVERRIDE { + return recipient_->ProcessSyncChanges(from_here, change_list); + } private: // The recipient of all sync changes. @@ -157,33 +149,29 @@ class SyncChangeProcessorDelegate : public SyncChangeProcessor { DISALLOW_COPY_AND_ASSIGN(SyncChangeProcessorDelegate); }; -SyncChangeProcessorDelegate::SyncChangeProcessorDelegate( - SyncChangeProcessor* recipient) - : recipient_(recipient) { - DCHECK(recipient_); -} - -SyncChangeProcessorDelegate::~SyncChangeProcessorDelegate() { -} - -SyncError SyncChangeProcessorDelegate::ProcessSyncChanges( - const tracked_objects::Location& from_here, - const SyncChangeList& change_list) { - return recipient_->ProcessSyncChanges(from_here, change_list); -} - -} // namespace - - -// TemplateURLServiceSyncTest ------------------------------------------------- - class TemplateURLServiceSyncTest : public testing::Test { public: typedef TemplateURLService::SyncDataMap SyncDataMap; - TemplateURLServiceSyncTest(); + TemplateURLServiceSyncTest() + : sync_processor_(new TestChangeProcessor), + sync_processor_delegate_(new SyncChangeProcessorDelegate( + sync_processor_.get())) {} + + virtual void SetUp() { + profile_a_.reset(new TestingProfile); + TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( + profile_a_.get()); + model_a_.reset(new TemplateURLService(profile_a_.get())); + model_a_->Load(); + profile_b_.reset(new TestingProfile); + TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( + profile_b_.get()); + model_b_.reset(new TemplateURLService(profile_b_.get())); + model_b_->Load(); + } - virtual void SetUp() OVERRIDE; + virtual void TearDown() { } TemplateURLService* model() { return model_a_.get(); } // For readability, we redefine an accessor for Model A for use in tests that @@ -191,38 +179,121 @@ class TemplateURLServiceSyncTest : public testing::Test { TemplateURLService* model_a() { return model_a_.get(); } TemplateURLService* model_b() { return model_b_.get(); } TestChangeProcessor* processor() { return sync_processor_.get(); } - scoped_ptr<SyncChangeProcessor> PassProcessor(); + scoped_ptr<SyncChangeProcessor> PassProcessor() { + return sync_processor_delegate_.PassAs<SyncChangeProcessor>(); + } // Create a TemplateURL with some test values. The caller owns the returned // TemplateURL*. TemplateURL* CreateTestTemplateURL(const string16& keyword, + const std::string& url) const { + return CreateTestTemplateURL(keyword, url, std::string()); + } + + TemplateURL* CreateTestTemplateURL(const string16& keyword, + const std::string& url, + const std::string& guid) const { + return CreateTestTemplateURL(keyword, url, guid, 100); + } + + TemplateURL* CreateTestTemplateURL(const string16& keyword, const std::string& url, - const std::string& guid = std::string(), - time_t last_mod = 100, - bool created_by_policy = false) const; + const std::string& guid, + time_t last_mod) const { + return CreateTestTemplateURL(keyword, url, guid, last_mod, false); + } + + TemplateURL* CreateTestTemplateURL(const string16& keyword, + const std::string& url, + const std::string& guid, + time_t last_mod, + bool created_by_policy) const { + TemplateURL* turl = new TemplateURL(); + turl->set_short_name(ASCIIToUTF16("unittest")); + turl->set_keyword(keyword); + turl->set_safe_for_autoreplace(true); + turl->set_date_created(Time::FromTimeT(100)); + turl->set_last_modified(Time::FromTimeT(last_mod)); + turl->set_created_by_policy(created_by_policy); + turl->SetPrepopulateId(999999); + if (!guid.empty()) + turl->set_sync_guid(guid); + turl->SetURL(url); + turl->set_favicon_url(GURL("http://favicon.url")); + return turl; + } // Verifies the two TemplateURLs are equal. // TODO(stevet): Share this with TemplateURLServiceTest. void AssertEquals(const TemplateURL& expected, - const TemplateURL& actual) const; + const TemplateURL& actual) const { + ASSERT_TRUE(TemplateURLRef::SameUrlRefs(expected.url(), actual.url())); + ASSERT_TRUE(TemplateURLRef::SameUrlRefs(expected.suggestions_url(), + actual.suggestions_url())); + ASSERT_EQ(expected.keyword(), actual.keyword()); + ASSERT_EQ(expected.short_name(), actual.short_name()); + ASSERT_EQ(JoinString(expected.input_encodings(), ';'), + JoinString(actual.input_encodings(), ';')); + ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); + ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); + ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); + ASSERT_TRUE(expected.date_created() == actual.date_created()); + ASSERT_TRUE(expected.last_modified() == actual.last_modified()); + } // Expect that two SyncDataLists have equal contents, in terms of the // sync_guid, keyword, and url fields. void AssertEquals(const SyncDataList& data1, - const SyncDataList& data2) const; + const SyncDataList& data2) const { + SyncDataMap map1 = TemplateURLService::CreateGUIDToSyncDataMap(data1); + SyncDataMap map2 = TemplateURLService::CreateGUIDToSyncDataMap(data2); + + for (SyncDataMap::const_iterator iter1 = map1.begin(); + iter1 != map1.end(); iter1++) { + SyncDataMap::iterator iter2 = map2.find(iter1->first); + if (iter2 != map2.end()) { + ASSERT_EQ(GetKeyword(iter1->second), GetKeyword(iter2->second)); + ASSERT_EQ(GetURL(iter1->second), GetURL(iter2->second)); + map2.erase(iter2); + } + } + EXPECT_EQ(0U, map2.size()); + } // Convenience helper for creating SyncChanges. Takes ownership of |turl|. SyncChange CreateTestSyncChange(SyncChange::SyncChangeType type, - TemplateURL* turl) const; + TemplateURL* turl) const { + // We take control of the TemplateURL so make sure it's cleaned up after + // we create data out of it. + scoped_ptr<TemplateURL> scoped_turl(turl); + return SyncChange( + type, TemplateURLService::CreateSyncDataFromTemplateURL(*scoped_turl)); + } // Helper that creates some initial sync data. We cheat a little by specifying // GUIDs for easy identification later. We also make the last_modified times // slightly older than CreateTestTemplateURL's default, to test conflict // resolution. - SyncDataList CreateInitialSyncData() const; + SyncDataList CreateInitialSyncData() const { + SyncDataList list; + + scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key1"), + "http://key1.com", "key1", 90)); + list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); + turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", + "key2", 90)); + list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); + turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", + "key3", 90)); + list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); + + return list; + } // Syntactic sugar. - TemplateURL* Deserialize(const SyncData& sync_data); + TemplateURL* Deserialize(const SyncData& sync_data) { + return TemplateURLService::CreateTemplateURLFromSyncData(sync_data); + } protected: // We keep two TemplateURLServices to test syncing between them. @@ -238,114 +309,7 @@ class TemplateURLServiceSyncTest : public testing::Test { DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceSyncTest); }; -TemplateURLServiceSyncTest::TemplateURLServiceSyncTest() - : sync_processor_(new TestChangeProcessor), - sync_processor_delegate_(new SyncChangeProcessorDelegate( - sync_processor_.get())) { -} - -void TemplateURLServiceSyncTest::SetUp() { - profile_a_.reset(new TestingProfile); - TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( - profile_a_.get()); - model_a_.reset(new TemplateURLService(profile_a_.get())); - model_a_->Load(); - profile_b_.reset(new TestingProfile); - TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( - profile_b_.get()); - model_b_.reset(new TemplateURLService(profile_b_.get())); - model_b_->Load(); -} - -scoped_ptr<SyncChangeProcessor> TemplateURLServiceSyncTest::PassProcessor() { - return sync_processor_delegate_.PassAs<SyncChangeProcessor>(); -} - -TemplateURL* TemplateURLServiceSyncTest::CreateTestTemplateURL( - const string16& keyword, - const std::string& url, - const std::string& guid, - time_t last_mod, - bool created_by_policy) const { - TemplateURL* turl = new TemplateURL(); - turl->set_short_name(ASCIIToUTF16("unittest")); - turl->set_keyword(keyword); - turl->set_safe_for_autoreplace(true); - turl->set_date_created(Time::FromTimeT(100)); - turl->set_last_modified(Time::FromTimeT(last_mod)); - turl->set_created_by_policy(created_by_policy); - turl->SetPrepopulateId(999999); - if (!guid.empty()) - turl->set_sync_guid(guid); - turl->SetURL(url); - turl->set_favicon_url(GURL("http://favicon.url")); - return turl; -} - -void TemplateURLServiceSyncTest::AssertEquals(const TemplateURL& expected, - const TemplateURL& actual) const { - ASSERT_EQ(expected.short_name(), actual.short_name()); - ASSERT_EQ(expected.url(), actual.url()); - ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); - ASSERT_EQ(expected.keyword(), actual.keyword()); - ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); - ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); - ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); - ASSERT_EQ(expected.input_encodings(), actual.input_encodings()); - ASSERT_EQ(expected.date_created(), actual.date_created()); - ASSERT_EQ(expected.last_modified(), actual.last_modified()); -} - -void TemplateURLServiceSyncTest::AssertEquals(const SyncDataList& data1, - const SyncDataList& data2) const { - SyncDataMap map1 = TemplateURLService::CreateGUIDToSyncDataMap(data1); - SyncDataMap map2 = TemplateURLService::CreateGUIDToSyncDataMap(data2); - - for (SyncDataMap::const_iterator iter1 = map1.begin(); - iter1 != map1.end(); iter1++) { - SyncDataMap::iterator iter2 = map2.find(iter1->first); - if (iter2 != map2.end()) { - ASSERT_EQ(GetKeyword(iter1->second), GetKeyword(iter2->second)); - ASSERT_EQ(GetURL(iter1->second), GetURL(iter2->second)); - map2.erase(iter2); - } - } - EXPECT_EQ(0U, map2.size()); -} - -SyncChange TemplateURLServiceSyncTest::CreateTestSyncChange( - SyncChange::SyncChangeType type, - TemplateURL* turl) const { - // We take control of the TemplateURL so make sure it's cleaned up after - // we create data out of it. - scoped_ptr<TemplateURL> scoped_turl(turl); - return SyncChange(type, - TemplateURLService::CreateSyncDataFromTemplateURL(*scoped_turl)); -} - -SyncDataList TemplateURLServiceSyncTest::CreateInitialSyncData() const { - SyncDataList list; - - scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key1"), - "http://key1.com", "key1", 90)); - list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); - turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", - "key2", 90)); - list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); - turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", - "key3", 90)); - list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); - - return list; -} - -TemplateURL* TemplateURLServiceSyncTest::Deserialize( - const SyncData& sync_data) { - return TemplateURLService::CreateTemplateURLFromSyncData(sync_data); -} - - -// Actual tests --------------------------------------------------------------- +} // namespace TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) { // Create a TemplateURL and convert it into a sync specific type. @@ -550,7 +514,7 @@ TEST_F(TemplateURLServiceSyncTest, FindDuplicateOfSyncTemplateURL) { model()->FindDuplicateOfSyncTemplateURL(*sync_turl); ASSERT_TRUE(dupe_turl); EXPECT_EQ(dupe_turl->keyword(), sync_turl->keyword()); - EXPECT_EQ(dupe_turl->url(), sync_turl->url()); + EXPECT_EQ(dupe_turl->url()->url(), sync_turl->url()->url()); } TEST_F(TemplateURLServiceSyncTest, MergeSyncAndLocalURLDuplicates) { @@ -633,9 +597,9 @@ TEST_F(TemplateURLServiceSyncTest, MergeInAllNewData) { EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com"))); // Ensure that Sync received the expected changes. EXPECT_EQ(3, processor()->change_list_size()); - EXPECT_TRUE(processor()->contains_guid("abc")); - EXPECT_TRUE(processor()->contains_guid("def")); - EXPECT_TRUE(processor()->contains_guid("xyz")); + EXPECT_TRUE(processor()->ContainsGUID("abc")); + EXPECT_TRUE(processor()->ContainsGUID("def")); + EXPECT_TRUE(processor()->ContainsGUID("xyz")); } TEST_F(TemplateURLServiceSyncTest, MergeSyncIsTheSame) { @@ -690,12 +654,12 @@ TEST_F(TemplateURLServiceSyncTest, MergeUpdateFromSync) { // Check that the first replaced the initial Google TemplateURL. EXPECT_EQ(turl1, model()->GetTemplateURLForGUID("abc")); - EXPECT_EQ("http://google.ca", turl1->url()); + EXPECT_EQ("http://google.ca", turl1->url()->url()); // Check that the second produced an upstream update to the Bing TemplateURL. EXPECT_EQ(1, processor()->change_list_size()); - ASSERT_TRUE(processor()->contains_guid("xyz")); - SyncChange change = processor()->change_for_guid("xyz"); + ASSERT_TRUE(processor()->ContainsGUID("xyz")); + SyncChange change = processor()->GetChangeByGUID("xyz"); EXPECT_TRUE(change.change_type() == SyncChange::ACTION_UPDATE); EXPECT_EQ("http://bing.com", GetURL(change.sync_data())); } @@ -726,8 +690,8 @@ TEST_F(TemplateURLServiceSyncTest, MergeAddFromOlderSyncData) { // update. The local copy should have received the sync data's GUID. EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); // Check changes for the UPDATE. - ASSERT_TRUE(processor()->contains_guid("key1")); - SyncChange key1_change = processor()->change_for_guid("key1"); + ASSERT_TRUE(processor()->ContainsGUID("key1")); + SyncChange key1_change = processor()->GetChangeByGUID("key1"); EXPECT_EQ(SyncChange::ACTION_UPDATE, key1_change.change_type()); EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa")); @@ -739,8 +703,8 @@ TEST_F(TemplateURLServiceSyncTest, MergeAddFromOlderSyncData) { EXPECT_EQ(ASCIIToUTF16("key2"), key2->keyword()); EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); // Check changes for the UPDATE. - ASSERT_TRUE(processor()->contains_guid("key2")); - SyncChange key2_change = processor()->change_for_guid("key2"); + ASSERT_TRUE(processor()->ContainsGUID("key2")); + SyncChange key2_change = processor()->GetChangeByGUID("key2"); EXPECT_EQ(SyncChange::ACTION_UPDATE, key2_change.change_type()); EXPECT_EQ("key2.com", GetKeyword(key2_change.sync_data())); @@ -752,12 +716,12 @@ TEST_F(TemplateURLServiceSyncTest, MergeAddFromOlderSyncData) { // Two UPDATEs and two ADDs. EXPECT_EQ(4, processor()->change_list_size()); // Two ADDs should be pushed up to Sync. - ASSERT_TRUE(processor()->contains_guid("bbb")); + ASSERT_TRUE(processor()->ContainsGUID("bbb")); EXPECT_EQ(SyncChange::ACTION_ADD, - processor()->change_for_guid("bbb").change_type()); - ASSERT_TRUE(processor()->contains_guid("ccc")); + processor()->GetChangeByGUID("bbb").change_type()); + ASSERT_TRUE(processor()->ContainsGUID("ccc")); EXPECT_EQ(SyncChange::ACTION_ADD, - processor()->change_for_guid("ccc").change_type()); + processor()->GetChangeByGUID("ccc").change_type()); } TEST_F(TemplateURLServiceSyncTest, MergeAddFromNewerSyncData) { @@ -804,12 +768,12 @@ TEST_F(TemplateURLServiceSyncTest, MergeAddFromNewerSyncData) { // Two ADDs. EXPECT_EQ(2, processor()->change_list_size()); // Two ADDs should be pushed up to Sync. - ASSERT_TRUE(processor()->contains_guid("bbb")); + ASSERT_TRUE(processor()->ContainsGUID("bbb")); EXPECT_EQ(SyncChange::ACTION_ADD, - processor()->change_for_guid("bbb").change_type()); - ASSERT_TRUE(processor()->contains_guid("ccc")); + processor()->GetChangeByGUID("bbb").change_type()); + ASSERT_TRUE(processor()->ContainsGUID("ccc")); EXPECT_EQ(SyncChange::ACTION_ADD, - processor()->change_for_guid("ccc").change_type()); + processor()->GetChangeByGUID("ccc").change_type()); } TEST_F(TemplateURLServiceSyncTest, ProcessChangesEmptyModel) { @@ -859,7 +823,7 @@ TEST_F(TemplateURLServiceSyncTest, ProcessChangesNoConflicts) { const TemplateURL* turl = model()->GetTemplateURLForGUID("key2"); EXPECT_TRUE(turl); EXPECT_EQ(ASCIIToUTF16("newkeyword"), turl->keyword()); - EXPECT_EQ("http://new.com", turl->url()); + EXPECT_EQ("http://new.com", turl->url()->url()); EXPECT_FALSE(model()->GetTemplateURLForGUID("key3")); EXPECT_TRUE(model()->GetTemplateURLForGUID("key4")); } @@ -937,12 +901,12 @@ TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsLocalWins) { EXPECT_EQ(model()->GetTemplateURLForGUID("key3"), model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3"))); - ASSERT_TRUE(processor()->contains_guid("aaa")); + ASSERT_TRUE(processor()->ContainsGUID("aaa")); EXPECT_EQ(SyncChange::ACTION_UPDATE, - processor()->change_for_guid("aaa").change_type()); - ASSERT_TRUE(processor()->contains_guid("key1")); + processor()->GetChangeByGUID("aaa").change_type()); + ASSERT_TRUE(processor()->ContainsGUID("key1")); EXPECT_EQ(SyncChange::ACTION_UPDATE, - processor()->change_for_guid("key1").change_type()); + processor()->GetChangeByGUID("key1").change_type()); } TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) { @@ -956,8 +920,8 @@ TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) { CreateTestTemplateURL(ASCIIToUTF16("baidu"), "http://baidu.cn", "new"); model()->Add(new_turl); EXPECT_EQ(1, processor()->change_list_size()); - ASSERT_TRUE(processor()->contains_guid("new")); - SyncChange change = processor()->change_for_guid("new"); + ASSERT_TRUE(processor()->ContainsGUID("new")); + SyncChange change = processor()->GetChangeByGUID("new"); EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type()); EXPECT_EQ("baidu", GetKeyword(change.sync_data())); EXPECT_EQ("http://baidu.cn", GetURL(change.sync_data())); @@ -965,10 +929,10 @@ TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) { // Change a keyword. const TemplateURL* existing_turl = model()->GetTemplateURLForGUID("key1"); model()->ResetTemplateURL(existing_turl, existing_turl->short_name(), - ASCIIToUTF16("k"), existing_turl->url()); + ASCIIToUTF16("k"), existing_turl->url()->url()); EXPECT_EQ(1, processor()->change_list_size()); - ASSERT_TRUE(processor()->contains_guid("key1")); - change = processor()->change_for_guid("key1"); + ASSERT_TRUE(processor()->ContainsGUID("key1")); + change = processor()->GetChangeByGUID("key1"); EXPECT_EQ(SyncChange::ACTION_UPDATE, change.change_type()); EXPECT_EQ("k", GetKeyword(change.sync_data())); @@ -976,8 +940,8 @@ TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) { existing_turl = model()->GetTemplateURLForGUID("key2"); model()->Remove(existing_turl); EXPECT_EQ(1, processor()->change_list_size()); - ASSERT_TRUE(processor()->contains_guid("key2")); - change = processor()->change_for_guid("key2"); + ASSERT_TRUE(processor()->ContainsGUID("key2")); + change = processor()->GetChangeByGUID("key2"); EXPECT_EQ(SyncChange::ACTION_DELETE, change.change_type()); } diff --git a/chrome/browser/search_engines/template_url_service_unittest.cc b/chrome/browser/search_engines/template_url_service_unittest.cc index 040a812..25b8509 100644 --- a/chrome/browser/search_engines/template_url_service_unittest.cc +++ b/chrome/browser/search_engines/template_url_service_unittest.cc @@ -258,17 +258,19 @@ TemplateURL* TemplateURLServiceTest::AddKeywordWithDate( void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected, const TemplateURL& actual) { - ASSERT_EQ(expected.short_name(), actual.short_name()); - ASSERT_EQ(expected.url(), actual.url()); - ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); + ASSERT_TRUE(TemplateURLRef::SameUrlRefs(expected.url(), actual.url())); + ASSERT_TRUE(TemplateURLRef::SameUrlRefs(expected.suggestions_url(), + actual.suggestions_url())); ASSERT_EQ(expected.keyword(), actual.keyword()); - ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); - ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); + ASSERT_EQ(expected.short_name(), actual.short_name()); + ASSERT_EQ(JoinString(expected.input_encodings(), ';'), + JoinString(actual.input_encodings(), ';')); ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); + ASSERT_EQ(expected.id(), actual.id()); + ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); + ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); ASSERT_EQ(expected.date_created(), actual.date_created()); ASSERT_EQ(expected.last_modified(), actual.last_modified()); - ASSERT_EQ(expected.input_encodings(), actual.input_encodings()); - ASSERT_EQ(expected.id(), actual.id()); ASSERT_EQ(expected.sync_guid(), actual.sync_guid()); } @@ -276,14 +278,16 @@ void TemplateURLServiceTest::ExpectSimilar(const TemplateURL* expected, const TemplateURL* actual) { ASSERT_TRUE(expected != NULL); ASSERT_TRUE(actual != NULL); - EXPECT_EQ(expected->short_name(), actual->short_name()); - EXPECT_EQ(expected->url(), actual->url()); - EXPECT_EQ(expected->suggestions_url(), actual->suggestions_url()); + EXPECT_TRUE(TemplateURLRef::SameUrlRefs(expected->url(), actual->url())); + EXPECT_TRUE(TemplateURLRef::SameUrlRefs(expected->suggestions_url(), + actual->suggestions_url())); EXPECT_EQ(expected->keyword(), actual->keyword()); - EXPECT_EQ(expected->show_in_default_list(), actual->show_in_default_list()); - EXPECT_EQ(expected->safe_for_autoreplace(), actual->safe_for_autoreplace()); + EXPECT_EQ(expected->short_name(), actual->short_name()); + EXPECT_EQ(JoinString(expected->input_encodings(), ';'), + JoinString(actual->input_encodings(), ';')); EXPECT_EQ(expected->favicon_url(), actual->favicon_url()); - EXPECT_EQ(expected->input_encodings(), actual->input_encodings()); + EXPECT_EQ(expected->safe_for_autoreplace(), actual->safe_for_autoreplace()); + EXPECT_EQ(expected->show_in_default_list(), actual->show_in_default_list()); } void TemplateURLServiceTest::SetManagedDefaultSearchPreferences( @@ -339,7 +343,7 @@ TemplateURL* TemplateURLServiceTest::CreateReplaceablePreloadedTemplateURL( TemplateURL* t_url = CreatePreloadedTemplateURL(safe_for_autoreplace, prepopulated_urls[prepopulated_index]->prepopulate_id()); *prepopulated_display_url = - prepopulated_urls[prepopulated_index]->url_ref().DisplayURL(); + prepopulated_urls[prepopulated_index]->url()->DisplayURL(); return t_url; } @@ -349,7 +353,7 @@ void TemplateURLServiceTest::TestLoadUpdatingPreloadedURL( TemplateURL* t_url = CreateReplaceablePreloadedTemplateURL(false, index_offset_from_default, &prepopulated_url); - string16 original_url = t_url->url_ref().DisplayURL(); + string16 original_url = t_url->url()->DisplayURL(); ASSERT_NE(prepopulated_url, original_url); // Then add it to the model and save it all. @@ -358,14 +362,14 @@ void TemplateURLServiceTest::TestLoadUpdatingPreloadedURL( const TemplateURL* keyword_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); ASSERT_EQ(t_url, keyword_url); - ASSERT_EQ(original_url, keyword_url->url_ref().DisplayURL()); + ASSERT_EQ(original_url, keyword_url->url()->DisplayURL()); test_util_.BlockTillServiceProcessesRequests(); // Now reload the model and verify that the merge updates the url. test_util_.ResetModel(true); keyword_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); ASSERT_TRUE(keyword_url != NULL); - ASSERT_EQ(prepopulated_url, keyword_url->url_ref().DisplayURL()); + ASSERT_EQ(prepopulated_url, keyword_url->url()->DisplayURL()); // Wait for any saves to finish. test_util_.BlockTillServiceProcessesRequests(); @@ -374,7 +378,7 @@ void TemplateURLServiceTest::TestLoadUpdatingPreloadedURL( test_util_.ResetModel(true); keyword_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); ASSERT_TRUE(keyword_url != NULL); - ASSERT_EQ(prepopulated_url, keyword_url->url_ref().DisplayURL()); + ASSERT_EQ(prepopulated_url, keyword_url->url()->DisplayURL()); } void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { @@ -442,7 +446,7 @@ TEST_F(TemplateURLServiceTest, AddUpdateRemove) { "c"); ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name()); ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword()); - ASSERT_EQ("c", loaded_url->url()); + ASSERT_EQ("c", loaded_url->url()->url()); ASSERT_FALSE(loaded_url->safe_for_autoreplace()); ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), NULL)); @@ -649,7 +653,7 @@ TEST_F(TemplateURLServiceTest, Reset) { model()->ResetTemplateURL(t_url, new_short_name, new_keyword, new_url); ASSERT_EQ(new_short_name, t_url->short_name()); ASSERT_EQ(new_keyword, t_url->keyword()); - ASSERT_EQ(new_url, t_url->url()); + ASSERT_EQ(new_url, t_url->url()->url()); // Make sure the mappings in the model were updated. ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword)); @@ -734,7 +738,7 @@ TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) { // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should // no longer be replaceable. model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), - t_url->url()); + t_url->url()->url()); ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL("http://foo2"), NULL)); @@ -755,7 +759,7 @@ TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) { // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should // no longer be replaceable. model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), - t_url->url()); + t_url->url()->url()); ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), GURL("http://foo.com"), NULL)); @@ -797,9 +801,12 @@ TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) { // value are persisted to prefs. const TemplateURL* default_turl = model()->GetDefaultSearchProvider(); ASSERT_TRUE(default_turl); - ASSERT_EQ("http://url", default_turl->url()); - ASSERT_EQ("http://url2", default_turl->suggestions_url()); - EXPECT_EQ("http://instant", default_turl->instant_url()); + ASSERT_TRUE(default_turl->url()); + ASSERT_EQ("http://url", default_turl->url()->url()); + ASSERT_TRUE(default_turl->suggestions_url()); + ASSERT_EQ("http://url2", default_turl->suggestions_url()->url()); + ASSERT_TRUE(default_turl->instant_url()); + EXPECT_EQ("http://instant", default_turl->instant_url()->url()); ASSERT_EQ(ASCIIToUTF16("a"), default_turl->short_name()); ASSERT_EQ(id, default_turl->id()); @@ -917,7 +924,7 @@ TEST_F(TemplateURLServiceTest, ChangeGoogleBaseValue) { "{google:baseURL}?q={searchTerms}", "http://sugg1", "http://icon1", "UTF-8;UTF-16", "name", false, Time(), Time()); ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com")); - EXPECT_EQ("google.com", t_url->url_ref().GetHost()); + EXPECT_EQ("google.com", t_url->url()->GetHost()); EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword()); // Change the Google base url. @@ -928,9 +935,9 @@ TEST_F(TemplateURLServiceTest, ChangeGoogleBaseValue) { // Make sure the host->TemplateURL map was updated appropriately. ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.co.uk")); EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL); - EXPECT_EQ("google.co.uk", t_url->url_ref().GetHost()); + EXPECT_EQ("google.co.uk", t_url->url()->GetHost()); EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); - EXPECT_EQ("http://google.co.uk/?q=x", t_url->url_ref().ReplaceSearchTerms( + EXPECT_EQ("http://google.co.uk/?q=x", t_url->url()->ReplaceSearchTerms( ASCIIToUTF16("x"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); } @@ -969,7 +976,7 @@ TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { HistoryService* history = test_util_.profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); history->AddPage( - GURL(t_url->url_ref().ReplaceSearchTerms(ASCIIToUTF16("blah"), + GURL(t_url->url()->ReplaceSearchTerms(ASCIIToUTF16("blah"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), NULL, 0, GURL(), content::PAGE_TRANSITION_KEYWORD, history::RedirectList(), history::SOURCE_BROWSED, false); diff --git a/chrome/browser/search_engines/template_url_unittest.cc b/chrome/browser/search_engines/template_url_unittest.cc index 479f306..fa1baf6 100644 --- a/chrome/browser/search_engines/template_url_unittest.cc +++ b/chrome/browser/search_engines/template_url_unittest.cc @@ -65,9 +65,9 @@ TEST_F(TemplateURLTest, Defaults) { } TEST_F(TemplateURLTest, TestValidWithComplete) { - TemplateURL url; - url.SetURL("{searchTerms}"); - EXPECT_TRUE(url.url_ref().IsValid()); + TemplateURL t_url; + TemplateURLRef ref(&t_url, "{searchTerms}"); + EXPECT_TRUE(ref.IsValid()); } TEST_F(TemplateURLTest, URLRefTestSearchTerms) { @@ -88,68 +88,72 @@ TEST_F(TemplateURLTest, URLRefTestSearchTerms) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { const SearchTermsCase& value = search_term_cases[i]; - TemplateURL url; - url.SetURL(value.url); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - std::string result = url.url_ref().ReplaceSearchTerms(value.terms, + TemplateURL t_url; + TemplateURLRef ref(&t_url, value.url); + ASSERT_TRUE(ref.IsValid()); + + ASSERT_TRUE(ref.SupportsReplacement()); + std::string result = ref.ReplaceSearchTerms(value.terms, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()); - EXPECT_EQ(value.output, result); + ASSERT_EQ(value.output, result); GURL result_url(result); - EXPECT_EQ(value.valid_url, result_url.is_valid()); + ASSERT_EQ(value.valid_url, result_url.is_valid()); } } TEST_F(TemplateURLTest, URLRefTestCount) { - TemplateURL url; - url.SetURL("http://foo{searchTerms}{count?}"); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), + TemplateURL t_url; + TemplateURLRef ref(&t_url, "http://foo{searchTerms}{count?}"); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("X"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); EXPECT_EQ("http://foox/", result.spec()); } TEST_F(TemplateURLTest, URLRefTestCount2) { - TemplateURL url; - url.SetURL("http://foo{searchTerms}{count}"); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), + TemplateURL t_url; + TemplateURLRef ref(&t_url, "http://foo{searchTerms}{count}"); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("X"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); EXPECT_EQ("http://foox10/", result.spec()); } TEST_F(TemplateURLTest, URLRefTestIndices) { - TemplateURL url; - url.SetURL("http://foo{searchTerms}x{startIndex?}y{startPage?}"); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), + TemplateURL t_url; + TemplateURLRef ref(&t_url, + "http://foo{searchTerms}x{startIndex?}y{startPage?}"); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("X"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); EXPECT_EQ("http://fooxxy/", result.spec()); } TEST_F(TemplateURLTest, URLRefTestIndices2) { - TemplateURL url; - url.SetURL("http://foo{searchTerms}x{startIndex}y{startPage}"); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), + TemplateURL t_url; + TemplateURLRef ref(&t_url, + "http://foo{searchTerms}x{startIndex}y{startPage}"); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("X"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); EXPECT_EQ("http://fooxx1y1/", result.spec()); } TEST_F(TemplateURLTest, URLRefTestEncoding) { - TemplateURL url; - url.SetURL("http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a"); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), + TemplateURL t_url; + TemplateURLRef ref(&t_url, + "http://foo{searchTerms}x{inputEncoding?}y{outputEncoding?}a"); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("X"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); EXPECT_EQ("http://fooxxutf-8ya/", result.spec()); @@ -158,39 +162,41 @@ TEST_F(TemplateURLTest, URLRefTestEncoding) { // Test that setting the prepopulate ID from TemplateURL causes the stored // TemplateURLRef to handle parsing the URL parameters differently. TEST_F(TemplateURLTest, SetPrepopulatedAndParse) { - TemplateURL url; - url.SetURL("http://foo{fhqwhgads}"); + TemplateURL t_url; + t_url.SetURL("http://foo{fhqwhgads}"); TemplateURLRef::Replacements replacements; bool valid = false; EXPECT_EQ("http://foo{fhqwhgads}", - url.url_ref().ParseURL("http://foo{fhqwhgads}", &replacements, &valid)); + t_url.url()->ParseURL("http://foo{fhqwhgads}", &replacements, &valid)); EXPECT_TRUE(replacements.empty()); EXPECT_TRUE(valid); - url.SetPrepopulateId(123); + t_url.SetPrepopulateId(123); EXPECT_EQ("http://foo", - url.url_ref().ParseURL("http://foo{fhqwhgads}", &replacements, &valid)); + t_url.url()->ParseURL("http://foo{fhqwhgads}", &replacements, &valid)); EXPECT_TRUE(replacements.empty()); EXPECT_TRUE(valid); } TEST_F(TemplateURLTest, InputEncodingBeforeSearchTerm) { - TemplateURL url; - url.SetURL("http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b"); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), + TemplateURL t_url; + TemplateURLRef ref(&t_url, + "http://foox{inputEncoding?}a{searchTerms}y{outputEncoding?}b"); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("X"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); EXPECT_EQ("http://fooxutf-8axyb/", result.spec()); } TEST_F(TemplateURLTest, URLRefTestEncoding2) { - TemplateURL url; - url.SetURL("http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a"); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), + TemplateURL t_url; + TemplateURLRef ref(&t_url, + "http://foo{searchTerms}x{inputEncoding}y{outputEncoding}a"); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("X"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); EXPECT_EQ("http://fooxxutf-8yutf-8a/", result.spec()); @@ -209,13 +215,14 @@ TEST_F(TemplateURLTest, URLRefTestSearchTermsUsingTermsData) { }; TestSearchTermsData search_terms_data("http://example.com/e/"); - TemplateURL url; + TemplateURL t_url; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { const SearchTermsCase& value = search_term_cases[i]; - url.SetURL(value.url); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - GURL result(url.url_ref().ReplaceSearchTermsUsingTermsData(value.terms, + TemplateURLRef ref(&t_url, value.url); + ASSERT_TRUE(ref.IsValid()); + + ASSERT_TRUE(ref.SupportsReplacement()); + GURL result(ref.ReplaceSearchTermsUsingTermsData(value.terms, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16(), search_terms_data)); ASSERT_TRUE(result.is_valid()); @@ -244,15 +251,18 @@ TEST_F(TemplateURLTest, URLRefTermToWide) { }; // Set one input encoding: big-5. This is so we can test fallback to UTF-8. - TemplateURL url; - url.SetURL("http://foo?q={searchTerms}"); - url.add_input_encoding("big-5"); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); + TemplateURL t_url; + std::vector<std::string> encodings; + encodings.push_back("big-5"); + t_url.set_input_encodings(encodings); + + TemplateURLRef ref(&t_url, "http://foo?q={searchTerms}"); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(to_wide_cases); i++) { EXPECT_EQ(to_wide_cases[i].expected_decoded_term, - url.url_ref().SearchTermToString16( - to_wide_cases[i].encoded_search_term)); + ref.SearchTermToString16(to_wide_cases[i].encoded_search_term)); } } @@ -271,11 +281,11 @@ TEST_F(TemplateURLTest, DisplayURLToURLRef) { ASCIIToUTF16("http://foo%s{language}") }, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { - TemplateURL url; - url.SetURL(test_data[i].url); - EXPECT_EQ(test_data[i].expected_result, url.url_ref().DisplayURL()); + TemplateURL t_url; + TemplateURLRef ref(&t_url, test_data[i].url); + EXPECT_EQ(test_data[i].expected_result, ref.DisplayURL()); EXPECT_EQ(test_data[i].url, - TemplateURLRef::DisplayURLToURLRef(url.url_ref().DisplayURL())); + TemplateURLRef::DisplayURLToURLRef(ref.DisplayURL())); } } @@ -309,16 +319,16 @@ TEST_F(TemplateURLTest, ReplaceSearchTerms) { { "http://foo/{inputEncoding}a{language}a{searchTerms}a", "http://foo/UTF-8a{language}aXa" }, }; - TemplateURL url; - url.add_input_encoding("UTF-8"); + TemplateURL turl; + turl.add_input_encoding("UTF-8"); for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { - url.SetURL(test_data[i].url); - EXPECT_TRUE(url.url_ref().IsValid()); - EXPECT_TRUE(url.url_ref().SupportsReplacement()); + TemplateURLRef ref(&turl, test_data[i].url); + EXPECT_TRUE(ref.IsValid()); + EXPECT_TRUE(ref.SupportsReplacement()); std::string expected_result = test_data[i].expected_result; ReplaceSubstringsAfterOffset(&expected_result, 0, "{language}", g_browser_process->GetApplicationLocale()); - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("X"), + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("X"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); EXPECT_EQ(expected_result, result.spec()); @@ -349,10 +359,10 @@ TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { "http://foo/%82%A0%20%82%A2/bar"}, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { - TemplateURL url; - url.SetURL(test_data[i].url); - url.add_input_encoding(test_data[i].encoding); - GURL result(url.url_ref().ReplaceSearchTerms(test_data[i].search_term, + TemplateURL turl; + turl.add_input_encoding(test_data[i].encoding); + TemplateURLRef ref(&turl, test_data[i].url); + GURL result(ref.ReplaceSearchTerms(test_data[i].search_term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); EXPECT_EQ(test_data[i].expected_result, result.spec()); @@ -376,14 +386,14 @@ TEST_F(TemplateURLTest, Suggestions) { { 0, string16(), "http://bar/foo?aq=0&oq=&q=foobar" }, { 1, ASCIIToUTF16("foo"), "http://bar/foo?aq=1&oq=foo&q=foobar" }, }; - TemplateURL url; - url.add_input_encoding("UTF-8"); - url.SetURL("http://bar/foo?{google:acceptedSuggestion}" + TemplateURL turl; + turl.add_input_encoding("UTF-8"); + TemplateURLRef ref(&turl, "http://bar/foo?{google:acceptedSuggestion}" "{google:originalQueryForSuggestion}q={searchTerms}"); - ASSERT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("foobar"), + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("foobar"), test_data[i].accepted_suggestion, test_data[i].original_query_for_suggestion)); ASSERT_TRUE(result.is_valid()); @@ -402,11 +412,11 @@ TEST_F(TemplateURLTest, RLZ) { } #endif - TemplateURL url; - url.SetURL("http://bar/?{google:RLZ}{searchTerms}"); - EXPECT_TRUE(url.url_ref().IsValid()); - ASSERT_TRUE(url.url_ref().SupportsReplacement()); - GURL result(url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("x"), + TemplateURL t_url; + TemplateURLRef ref(&t_url, "http://bar/?{google:RLZ}{searchTerms}"); + ASSERT_TRUE(ref.IsValid()); + ASSERT_TRUE(ref.SupportsReplacement()); + GURL result(ref.ReplaceSearchTerms(ASCIIToUTF16("x"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); ASSERT_TRUE(result.is_valid()); std::string expected_url = "http://bar/?"; @@ -445,11 +455,11 @@ TEST_F(TemplateURLTest, HostAndSearchTermKey) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { - TemplateURL url; - url.SetURL(data[i].url); - EXPECT_EQ(data[i].host, url.url_ref().GetHost()); - EXPECT_EQ(data[i].path, url.url_ref().GetPath()); - EXPECT_EQ(data[i].search_term_key, url.url_ref().GetSearchTermKey()); + TemplateURL t_url; + t_url.SetURL(data[i].url); + EXPECT_EQ(data[i].host, t_url.url()->GetHost()); + EXPECT_EQ(data[i].path, t_url.url()->GetPath()); + EXPECT_EQ(data[i].search_term_key, t_url.url()->GetSearchTermKey()); } } @@ -470,25 +480,25 @@ TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { } TEST_F(TemplateURLTest, Keyword) { - TemplateURL url; - url.SetURL("http://www.google.com/search"); - EXPECT_FALSE(url.autogenerate_keyword()); - url.set_keyword(ASCIIToUTF16("foo")); - EXPECT_EQ(ASCIIToUTF16("foo"), url.keyword()); - url.set_autogenerate_keyword(true); - EXPECT_TRUE(url.autogenerate_keyword()); - EXPECT_EQ(ASCIIToUTF16("google.com"), url.keyword()); - url.set_keyword(ASCIIToUTF16("foo")); - EXPECT_FALSE(url.autogenerate_keyword()); - EXPECT_EQ(ASCIIToUTF16("foo"), url.keyword()); + TemplateURL t_url; + t_url.SetURL("http://www.google.com/search"); + EXPECT_FALSE(t_url.autogenerate_keyword()); + t_url.set_keyword(ASCIIToUTF16("foo")); + EXPECT_EQ(ASCIIToUTF16("foo"), t_url.keyword()); + t_url.set_autogenerate_keyword(true); + EXPECT_TRUE(t_url.autogenerate_keyword()); + EXPECT_EQ(ASCIIToUTF16("google.com"), t_url.keyword()); + t_url.set_keyword(ASCIIToUTF16("foo")); + EXPECT_FALSE(t_url.autogenerate_keyword()); + EXPECT_EQ(ASCIIToUTF16("foo"), t_url.keyword()); } TEST_F(TemplateURLTest, ParseParameterKnown) { std::string parsed_url("{searchTerms}"); - TemplateURL url; - url.SetURL(parsed_url); + TemplateURL t_url; + TemplateURLRef url_ref(&t_url, parsed_url); TemplateURLRef::Replacements replacements; - EXPECT_TRUE(url.url_ref().ParseParameter(0, 12, &parsed_url, &replacements)); + EXPECT_TRUE(url_ref.ParseParameter(0, 12, &parsed_url, &replacements)); EXPECT_EQ(std::string(), parsed_url); ASSERT_EQ(1U, replacements.size()); EXPECT_EQ(0U, replacements[0].index); @@ -497,61 +507,62 @@ TEST_F(TemplateURLTest, ParseParameterKnown) { TEST_F(TemplateURLTest, ParseParameterUnknown) { std::string parsed_url("{fhqwhgads}"); - TemplateURL url; - url.SetURL(parsed_url); + TemplateURL t_url; + TemplateURLRef url_ref(&t_url, parsed_url); TemplateURLRef::Replacements replacements; // By default, TemplateURLRef should not consider itself prepopulated. // Therefore we should not replace the unknown parameter. - EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); + EXPECT_FALSE(url_ref.ParseParameter(0, 10, &parsed_url, &replacements)); EXPECT_EQ("{fhqwhgads}", parsed_url); EXPECT_TRUE(replacements.empty()); // If the TemplateURLRef is prepopulated, we should remove unknown parameters. parsed_url = "{fhqwhgads}"; - url.SetPrepopulateId(1); - EXPECT_FALSE(url.url_ref().ParseParameter(0, 10, &parsed_url, &replacements)); + url_ref.prepopulated_ = true; + EXPECT_FALSE(url_ref.ParseParameter(0, 10, &parsed_url, &replacements)); EXPECT_EQ(std::string(), parsed_url); EXPECT_TRUE(replacements.empty()); } TEST_F(TemplateURLTest, ParseURLEmpty) { - TemplateURL url; + TemplateURL t_url; + TemplateURLRef url_ref(&t_url); TemplateURLRef::Replacements replacements; bool valid = false; EXPECT_EQ(std::string(), - url.url_ref().ParseURL(std::string(), &replacements, &valid)); + url_ref.ParseURL(std::string(), &replacements, &valid)); EXPECT_TRUE(replacements.empty()); EXPECT_TRUE(valid); } TEST_F(TemplateURLTest, ParseURLNoTemplateEnd) { - TemplateURL url; - url.SetURL("{"); + TemplateURL t_url; + TemplateURLRef url_ref(&t_url, "{"); TemplateURLRef::Replacements replacements; bool valid = false; - EXPECT_EQ(std::string(), url.url_ref().ParseURL("{", &replacements, &valid)); + EXPECT_EQ(std::string(), url_ref.ParseURL("{", &replacements, &valid)); EXPECT_TRUE(replacements.empty()); EXPECT_FALSE(valid); } TEST_F(TemplateURLTest, ParseURLNoKnownParameters) { - TemplateURL url; - url.SetURL("{}"); + TemplateURL t_url; + TemplateURLRef url_ref(&t_url, "{}"); TemplateURLRef::Replacements replacements; bool valid = false; - EXPECT_EQ("{}", url.url_ref().ParseURL("{}", &replacements, &valid)); + EXPECT_EQ("{}", url_ref.ParseURL("{}", &replacements, &valid)); EXPECT_TRUE(replacements.empty()); EXPECT_TRUE(valid); } TEST_F(TemplateURLTest, ParseURLTwoParameters) { - TemplateURL url; - url.SetURL("{}{{%s}}"); + TemplateURL t_url; + TemplateURLRef url_ref(&t_url, "{}{{%s}}"); TemplateURLRef::Replacements replacements; bool valid = false; EXPECT_EQ("{}{}", - url.url_ref().ParseURL("{}{{searchTerms}}", &replacements, &valid)); + url_ref.ParseURL("{}{{searchTerms}}", &replacements, &valid)); ASSERT_EQ(1U, replacements.size()); EXPECT_EQ(3U, replacements[0].index); EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); @@ -559,12 +570,11 @@ TEST_F(TemplateURLTest, ParseURLTwoParameters) { } TEST_F(TemplateURLTest, ParseURLNestedParameter) { - TemplateURL url; - url.SetURL("{%s"); + TemplateURL t_url; + TemplateURLRef url_ref(&t_url, "{%s"); TemplateURLRef::Replacements replacements; bool valid = false; - EXPECT_EQ("{", - url.url_ref().ParseURL("{{searchTerms}", &replacements, &valid)); + EXPECT_EQ("{", url_ref.ParseURL("{{searchTerms}", &replacements, &valid)); ASSERT_EQ(1U, replacements.size()); EXPECT_EQ(1U, replacements[0].index); EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); diff --git a/chrome/browser/sync/test/integration/search_engines_helper.cc b/chrome/browser/sync/test/integration/search_engines_helper.cc index cb1c6c5..16261b3 100644 --- a/chrome/browser/sync/test/integration/search_engines_helper.cc +++ b/chrome/browser/sync/test/integration/search_engines_helper.cc @@ -47,17 +47,26 @@ GUIDToTURLMap CreateGUIDToTURLMap(TemplateURLService* service) { std::string GetTURLInfoString(const TemplateURL* turl) { DCHECK(turl); - return "TemplateURL: shortname: " + UTF16ToASCII(turl->short_name()) + - " keyword: " + UTF16ToASCII(turl->keyword()) + " url: " + turl->url(); + std::string shortname = UTF16ToASCII(turl->short_name()); + std::string keyword = UTF16ToASCII(turl->keyword()); + return StringPrintf("TemplateURL: shortname: %s keyword: %s url: %s", + shortname.c_str(), keyword.c_str(), + (turl->url() ? turl->url()->url().c_str() : "NULL")); } bool TURLsMatch(const TemplateURL* turl1, const TemplateURL* turl2) { CHECK(turl1); CHECK(turl2); - bool result = (turl1->url() == turl2->url()) && - (turl1->keyword() == turl2->keyword()) && - (turl1->short_name() == turl2->short_name()); + // Either both TemplateURLRefs are NULL or they're both valid and have the + // same raw URL value. + bool urls_match = ((!turl1->url() && !turl1->url()) || + (turl1->url() && turl2->url() && + turl1->url()->url() == turl2->url()->url())); + + // Compare all major fields. + bool result = (urls_match && turl1->keyword() == turl2->keyword() && + turl1->short_name() == turl2->short_name()); // Print some useful debug info. if (!result) { @@ -139,7 +148,7 @@ bool ServicesMatch(int profile_a, int profile_b) { << default_b->keyword(); return false; } else { - LOG(INFO) << "A had default with URL: " << default_a->url() + LOG(INFO) << "A had default with URL: " << default_a->url()->url() << " and keyword: " << default_a->keyword(); } diff --git a/chrome/browser/sync/test/integration/two_client_search_engines_sync_test.cc b/chrome/browser/sync/test/integration/two_client_search_engines_sync_test.cc index 30e7a57..892f067 100644 --- a/chrome/browser/sync/test/integration/two_client_search_engines_sync_test.cc +++ b/chrome/browser/sync/test/integration/two_client_search_engines_sync_test.cc @@ -153,8 +153,10 @@ IN_PROC_BROWSER_TEST_F(TwoClientSearchEnginesSyncTest, ConflictKeyword) { const TemplateURL* turl = GetServiceForProfile(1)->GetTemplateURLForKeyword( ASCIIToUTF16("test1")); EXPECT_TRUE(turl); - GetServiceForProfile(1)->ResetTemplateURL(turl, turl->short_name(), - ASCIIToUTF16("test0"), turl->url()); + GetServiceForProfile(1)->ResetTemplateURL(turl, + turl->short_name(), + ASCIIToUTF16("test0"), + turl->url()->url()); ASSERT_TRUE(AwaitQuiescence()); ASSERT_TRUE(AllServicesMatch()); diff --git a/chrome/browser/ui/browser_init.cc b/chrome/browser/ui/browser_init.cc index a14e95ee..1bae449 100644 --- a/chrome/browser/ui/browser_init.cc +++ b/chrome/browser/ui/browser_init.cc @@ -1631,11 +1631,11 @@ std::vector<GURL> BrowserInit::GetURLsFromCommandLine( const TemplateURL* default_provider = TemplateURLServiceFactory::GetForProfile(profile)-> GetDefaultSearchProvider(); - if (default_provider && !default_provider->url().empty()) { - const TemplateURLRef& search_url = default_provider->url_ref(); - DCHECK(search_url.SupportsReplacement()); + if (default_provider && default_provider->url()) { + const TemplateURLRef* search_url = default_provider->url(); + DCHECK(search_url->SupportsReplacement()); string16 search_term = param.LossyDisplayName().substr(2); - urls.push_back(GURL(search_url.ReplaceSearchTermsUsingProfile( + urls.push_back(GURL(search_url->ReplaceSearchTermsUsingProfile( profile, search_term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()))); continue; diff --git a/chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.mm b/chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.mm index e1c15db..1fa63bf6 100644 --- a/chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.mm +++ b/chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -90,7 +90,7 @@ void ShiftOriginY(NSView* view, CGFloat amount) { [keywordField_ setStringValue: base::SysUTF16ToNSString(templateURL_->keyword())]; [urlField_ setStringValue: - base::SysUTF16ToNSString(templateURL_->url_ref().DisplayURL())]; + base::SysUTF16ToNSString(templateURL_->url()->DisplayURL())]; [urlField_ setEnabled:(templateURL_->prepopulate_id() == 0)]; } // When creating a new keyword, this will mark the fields as "invalid" and diff --git a/chrome/browser/ui/gtk/edit_search_engine_dialog.cc b/chrome/browser/ui/gtk/edit_search_engine_dialog.cc index 1cb853c8..b40d46a 100644 --- a/chrome/browser/ui/gtk/edit_search_engine_dialog.cc +++ b/chrome/browser/ui/gtk/edit_search_engine_dialog.cc @@ -27,6 +27,10 @@ namespace { +std::string GetDisplayURL(const TemplateURL& turl) { + return turl.url() ? UTF16ToUTF8(turl.url()->DisplayURL()) : std::string(); +} + // Forces text to lowercase when connected to an editable's "insert-text" // signal. (Like views Textfield::STYLE_LOWERCASE.) void LowercaseInsertTextHandler(GtkEditable *editable, const gchar *text, @@ -149,8 +153,7 @@ void EditSearchEngineDialog::Init(GtkWindow* parent_window, Profile* profile) { UTF16ToUTF8(controller_->template_url()->keyword()).c_str()); gtk_entry_set_text( GTK_ENTRY(url_entry_), - UTF16ToUTF8(controller_->template_url()->url_ref().DisplayURL()). - c_str()); + GetDisplayURL(*controller_->template_url()).c_str()); // We don't allow users to edit prepopulated URLs. gtk_editable_set_editable( GTK_EDITABLE(url_entry_), diff --git a/chrome/browser/ui/search_engines/edit_search_engine_controller.cc b/chrome/browser/ui/search_engines/edit_search_engine_controller.cc index db46f3e..dbf9ae4 100644 --- a/chrome/browser/ui/search_engines/edit_search_engine_controller.cc +++ b/chrome/browser/ui/search_engines/edit_search_engine_controller.cc @@ -42,8 +42,7 @@ bool EditSearchEngineController::IsURLValid( // TemplateURL owner because |template_url_| might be NULL and we can't call // TemplateURLRef::IsValid() when its owner is NULL. TemplateURL t_url; - t_url.SetURL(url); - const TemplateURLRef& template_ref = t_url.url_ref(); + TemplateURLRef template_ref(&t_url, url); if (!template_ref.IsValid()) return false; @@ -136,7 +135,7 @@ std::string EditSearchEngineController::GetFixedUpURL( // we need to replace the search terms before testing for the scheme. TemplateURL t_url; t_url.SetURL(url); - std::string expanded_url(t_url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("x"), + std::string expanded_url(t_url.url()->ReplaceSearchTerms(ASCIIToUTF16("x"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); url_parse::Parsed parts; std::string scheme(URLFixerUpper::SegmentURL(expanded_url, &parts)); diff --git a/chrome/browser/ui/search_engines/keyword_editor_controller.cc b/chrome/browser/ui/search_engines/keyword_editor_controller.cc index 5ede231..9640e5e 100644 --- a/chrome/browser/ui/search_engines/keyword_editor_controller.cc +++ b/chrome/browser/ui/search_engines/keyword_editor_controller.cc @@ -63,7 +63,10 @@ void KeywordEditorController::ModifyTemplateURL(const TemplateURL* template_url, // Don't do anything if the entry didn't change. if ((template_url->short_name() == title) && - (template_url->keyword() == keyword) && (template_url->url() == url)) + (template_url->keyword() == keyword) && + ((url.empty() && !template_url->url()) || + (!url.empty() && template_url->url() && + template_url->url()->url() == url))) return; table_model_->ModifyTemplateURL(index, title, keyword, url); diff --git a/chrome/browser/ui/search_engines/keyword_editor_controller_unittest.cc b/chrome/browser/ui/search_engines/keyword_editor_controller_unittest.cc index 90c7af8..70ba2ac 100644 --- a/chrome/browser/ui/search_engines/keyword_editor_controller_unittest.cc +++ b/chrome/browser/ui/search_engines/keyword_editor_controller_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -142,7 +142,8 @@ TEST_F(KeywordEditorControllerTest, Add) { const TemplateURL* turl = model_->GetTemplateURLs()[0]; EXPECT_EQ(ASCIIToUTF16("a"), turl->short_name()); EXPECT_EQ(ASCIIToUTF16("b"), turl->keyword()); - EXPECT_EQ("http://c", turl->url()); + ASSERT_TRUE(turl->url() != NULL); + EXPECT_EQ("http://c", turl->url()->url()); } // Tests modifying a TemplateURL. @@ -158,7 +159,8 @@ TEST_F(KeywordEditorControllerTest, Modify) { VerifyChangeCount(0, 1, 0, 0); EXPECT_EQ(ASCIIToUTF16("a1"), turl->short_name()); EXPECT_EQ(ASCIIToUTF16("b1"), turl->keyword()); - EXPECT_EQ("http://c1", turl->url()); + ASSERT_TRUE(turl->url() != NULL); + EXPECT_EQ("http://c1", turl->url()->url()); } // Tests making a TemplateURL the default search provider. @@ -197,7 +199,7 @@ TEST_F(KeywordEditorControllerTest, CannotSetDefaultWhileManaged) { EXPECT_TRUE(controller_->CanMakeDefault(turl1)); EXPECT_TRUE(controller_->CanMakeDefault(turl2)); - SimulateDefaultSearchIsManaged(turl2->url()); + SimulateDefaultSearchIsManaged(turl2->url()->url()); EXPECT_TRUE(model_->is_default_search_managed()); EXPECT_FALSE(controller_->CanMakeDefault(turl1)); @@ -223,7 +225,7 @@ TEST_F(KeywordEditorControllerTest, EditManagedDefault) { // Simulate setting a managed default. This will add another template URL to // the model. - SimulateDefaultSearchIsManaged(turl2->url()); + SimulateDefaultSearchIsManaged(turl2->url()->url()); EXPECT_TRUE(model_->is_default_search_managed()); EXPECT_TRUE(controller_->CanEdit(turl1)); EXPECT_TRUE(controller_->CanEdit(turl2)); diff --git a/chrome/browser/ui/search_engines/template_url_table_model.cc b/chrome/browser/ui/search_engines/template_url_table_model.cc index 5ad90f3..08af8c6 100644 --- a/chrome/browser/ui/search_engines/template_url_table_model.cc +++ b/chrome/browser/ui/search_engines/template_url_table_model.cc @@ -82,8 +82,8 @@ class ModelEntry { GURL favicon_url = template_url()->favicon_url(); if (!favicon_url.is_valid()) { // The favicon url isn't always set. Guess at one here. - if (template_url_->url_ref().IsValid()) { - GURL url(template_url_->url()); + if (template_url_->url() && template_url_->url()->IsValid()) { + GURL url(template_url_->url()->url()); if (url.is_valid()) favicon_url = TemplateURL::GenerateFaviconURL(url); } diff --git a/chrome/browser/ui/views/edit_search_engine_dialog.cc b/chrome/browser/ui/views/edit_search_engine_dialog.cc index e87a917..e4a75d6 100644 --- a/chrome/browser/ui/views/edit_search_engine_dialog.cc +++ b/chrome/browser/ui/views/edit_search_engine_dialog.cc @@ -28,6 +28,14 @@ using views::ImageView; using views::Textfield; +namespace { +// Converts a URL as understood by TemplateURL to one appropriate for display +// to the user. +string16 GetDisplayURL(const TemplateURL& turl) { + return turl.url() ? turl.url()->DisplayURL() : string16(); +} +} // namespace + namespace browser { void EditSearchEngine(gfx::NativeWindow parent, @@ -121,8 +129,8 @@ void EditSearchEngineDialog::Init() { title_tf_ = CreateTextfield(controller_->template_url()->short_name(), false); keyword_tf_ = CreateTextfield(controller_->template_url()->keyword(), true); - url_tf_ = CreateTextfield( - controller_->template_url()->url_ref().DisplayURL(), false); + url_tf_ = CreateTextfield(GetDisplayURL(*controller_->template_url()), + false); // We don't allow users to edit prepopulate URLs. This is done as // occasionally we need to update the URL of prepopulated TemplateURLs. url_tf_->SetReadOnly(controller_->template_url()->prepopulate_id() != 0); diff --git a/chrome/browser/ui/webui/omnibox/omnibox_ui_handler.cc b/chrome/browser/ui/webui/omnibox/omnibox_ui_handler.cc index 12f34c57..dbe0786 100644 --- a/chrome/browser/ui/webui/omnibox/omnibox_ui_handler.cc +++ b/chrome/browser/ui/webui/omnibox/omnibox_ui_handler.cc @@ -116,8 +116,10 @@ void OmniboxUIHandler::AddResultToDictionary(const std::string& prefix, it->is_history_what_you_typed_match); output->SetString(item_prefix + ".type", AutocompleteMatch::TypeToString(it->type)); - if (it->template_url != NULL) - output->SetString(item_prefix + ".template_url", it->template_url->url()); + if ((it->template_url != NULL) && (it->template_url->url() != NULL)) { + output->SetString(item_prefix + ".template_url", + it->template_url->url()->url()); + } output->SetBoolean(item_prefix + ".starred", it->starred); output->SetBoolean(item_prefix + ".from_previous", it->from_previous); } diff --git a/chrome/browser/ui/webui/options2/search_engine_manager_handler2.cc b/chrome/browser/ui/webui/options2/search_engine_manager_handler2.cc index 1f74fc3..8b8b417 100644 --- a/chrome/browser/ui/webui/options2/search_engine_manager_handler2.cc +++ b/chrome/browser/ui/webui/options2/search_engine_manager_handler2.cc @@ -196,7 +196,7 @@ base::DictionaryValue* SearchEngineManagerHandler::CreateDictionaryForEngine( index, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)); dict->SetString("keyword", table_model->GetText( index, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)); - dict->SetString("url", template_url->url_ref().DisplayURL()); + dict->SetString("url", template_url->url()->DisplayURL()); dict->SetBoolean("urlLocked", template_url->prepopulate_id() > 0); GURL icon_url = template_url->favicon_url(); if (icon_url.is_valid()) diff --git a/chrome/browser/webdata/keyword_table.cc b/chrome/browser/webdata/keyword_table.cc index c8cd84f..879601f 100644 --- a/chrome/browser/webdata/keyword_table.cc +++ b/chrome/browser/webdata/keyword_table.cc @@ -61,7 +61,8 @@ void BindURLToStatement(const TemplateURL& url, s->BindString(starting_column + 2, url.favicon_url().is_valid() ? history::HistoryDatabase::GURLToDatabaseURL(url.favicon_url()) : std::string()); - s->BindString(starting_column + 3, url.url()); + s->BindString(starting_column + 3, + url.url() ? url.url()->url() : std::string()); s->BindBool(starting_column + 4, url.safe_for_autoreplace()); s->BindString(starting_column + 5, url.originating_url().is_valid() ? history::HistoryDatabase::GURLToDatabaseURL(url.originating_url()) : @@ -70,12 +71,14 @@ void BindURLToStatement(const TemplateURL& url, s->BindInt(starting_column + 7, url.usage_count()); s->BindString(starting_column + 8, JoinString(url.input_encodings(), ';')); s->BindBool(starting_column + 9, url.show_in_default_list()); - s->BindString(starting_column + 10, url.suggestions_url()); + s->BindString(starting_column + 10, + url.suggestions_url() ? url.suggestions_url()->url() : std::string()); s->BindInt(starting_column + 11, url.prepopulate_id()); s->BindInt(starting_column + 12, url.autogenerate_keyword() ? 1 : 0); s->BindInt(starting_column + 13, 0); s->BindBool(starting_column + 14, url.created_by_policy()); - s->BindString(starting_column + 15, url.instant_url()); + s->BindString(starting_column + 15, + url.instant_url() ? url.instant_url()->url() : std::string()); s->BindInt64(starting_column + 16, url.last_modified().ToTimeT()); s->BindString(starting_column + 17, url.sync_guid()); } diff --git a/chrome/browser/webdata/keyword_table_unittest.cc b/chrome/browser/webdata/keyword_table_unittest.cc index dd4a379..4e4a2c5 100644 --- a/chrome/browser/webdata/keyword_table_unittest.cc +++ b/chrome/browser/webdata/keyword_table_unittest.cc @@ -100,8 +100,11 @@ TEST_F(KeywordTableTest, Keywords) { EXPECT_EQ(keyword.created_by_policy(), restored_keyword->created_by_policy()); EXPECT_EQ(keyword.usage_count(), restored_keyword->usage_count()); EXPECT_EQ(keyword.prepopulate_id(), restored_keyword->prepopulate_id()); - EXPECT_EQ(keyword.url(), restored_keyword->url()); - EXPECT_EQ(keyword.instant_url(), restored_keyword->instant_url()); + ASSERT_TRUE(restored_keyword->url()); + EXPECT_EQ(keyword.url()->url(), restored_keyword->url()->url()); + ASSERT_TRUE(restored_keyword->instant_url()); + EXPECT_EQ(keyword.instant_url()->url(), + restored_keyword->instant_url()->url()); EXPECT_EQ(keyword.favicon_url(), restored_keyword->favicon_url()); EXPECT_TRUE(keyword_table->RemoveKeyword(restored_keyword->id())); @@ -178,10 +181,13 @@ TEST_F(KeywordTableTest, DefaultSearchProviderBackup) { EXPECT_EQ(keyword.short_name(), backup_url->short_name()); EXPECT_EQ(keyword.keyword(), backup_url->keyword()); EXPECT_EQ(keyword.favicon_url(), backup_url->favicon_url()); - EXPECT_EQ(keyword.url(), backup_url->url()); + ASSERT_TRUE(backup_url->url()); + EXPECT_EQ(keyword.url()->url(), backup_url->url()->url()); EXPECT_EQ(keyword.safe_for_autoreplace(), backup_url->safe_for_autoreplace()); EXPECT_EQ(keyword.show_in_default_list(), backup_url->show_in_default_list()); - EXPECT_EQ(keyword.suggestions_url(), backup_url->suggestions_url()); + ASSERT_TRUE(backup_url->suggestions_url()); + EXPECT_EQ(keyword.suggestions_url()->url(), + backup_url->suggestions_url()->url()); EXPECT_FALSE(keyword_table->DidDefaultSearchProviderChange()); // Change the actual setting. @@ -195,10 +201,13 @@ TEST_F(KeywordTableTest, DefaultSearchProviderBackup) { EXPECT_EQ(keyword.short_name(), backup_url->short_name()); EXPECT_EQ(keyword.keyword(), backup_url->keyword()); EXPECT_EQ(keyword.favicon_url(), backup_url->favicon_url()); - EXPECT_EQ(keyword.url(), backup_url->url()); + ASSERT_TRUE(backup_url->url()); + EXPECT_EQ(keyword.url()->url(), backup_url->url()->url()); EXPECT_EQ(keyword.safe_for_autoreplace(), backup_url->safe_for_autoreplace()); EXPECT_EQ(keyword.show_in_default_list(), backup_url->show_in_default_list()); - EXPECT_EQ(keyword.suggestions_url(), backup_url->suggestions_url()); + ASSERT_TRUE(backup_url->suggestions_url()); + EXPECT_EQ(keyword.suggestions_url()->url(), + backup_url->suggestions_url()->url()); EXPECT_TRUE(keyword_table->DidDefaultSearchProviderChange()); // Change the backup. @@ -234,10 +243,13 @@ TEST_F(KeywordTableTest, DefaultSearchProviderBackup) { EXPECT_EQ(keyword.short_name(), backup_url->short_name()); EXPECT_EQ(keyword.keyword(), backup_url->keyword()); EXPECT_EQ(keyword.favicon_url(), backup_url->favicon_url()); - EXPECT_EQ(keyword.url(), backup_url->url()); + ASSERT_TRUE(backup_url->url()); + EXPECT_EQ(keyword.url()->url(), backup_url->url()->url()); EXPECT_EQ(keyword.safe_for_autoreplace(), backup_url->safe_for_autoreplace()); EXPECT_EQ(keyword.show_in_default_list(), backup_url->show_in_default_list()); - EXPECT_EQ(keyword.suggestions_url(), backup_url->suggestions_url()); + ASSERT_TRUE(backup_url->suggestions_url()); + EXPECT_EQ(keyword.suggestions_url()->url(), + backup_url->suggestions_url()->url()); EXPECT_TRUE(keyword_table->DidDefaultSearchProviderChange()); // Change keywords backup. @@ -375,9 +387,13 @@ TEST_F(KeywordTableTest, UpdateKeyword) { EXPECT_EQ(keyword.input_encodings(), restored_keyword->input_encodings()); EXPECT_EQ(keyword.id(), restored_keyword->id()); EXPECT_EQ(keyword.prepopulate_id(), restored_keyword->prepopulate_id()); - EXPECT_EQ(keyword.suggestions_url(), restored_keyword->suggestions_url()); + ASSERT_TRUE(restored_keyword->suggestions_url()); + EXPECT_EQ(keyword.suggestions_url()->url(), + restored_keyword->suggestions_url()->url()); + ASSERT_TRUE(restored_keyword->instant_url()); EXPECT_EQ(keyword.favicon_url(), restored_keyword->favicon_url()); - EXPECT_EQ(keyword.instant_url(), restored_keyword->instant_url()); + EXPECT_EQ(keyword.instant_url()->url(), + restored_keyword->instant_url()->url()); STLDeleteElements(&keywords); } |