| Index: chrome/browser/autocomplete/keyword_provider.cc
|
| ===================================================================
|
| --- chrome/browser/autocomplete/keyword_provider.cc (revision 208572)
|
| +++ chrome/browser/autocomplete/keyword_provider.cc (working copy)
|
| @@ -86,9 +86,8 @@
|
| // probably better rankings than the fraction of the keyword typed. We should
|
| // always put any exact matches first no matter what, since the code in
|
| // Start() assumes this (and it makes sense).
|
| - bool operator()(const string16& keyword1,
|
| - const string16& keyword2) const {
|
| - return keyword1.length() < keyword2.length();
|
| + bool operator()(const TemplateURL* t_url1, const TemplateURL* t_url2) const {
|
| + return t_url1->keyword().length() < t_url2->keyword().length();
|
| }
|
| };
|
|
|
| @@ -216,8 +215,9 @@
|
| const string16& text,
|
| const string16& keyword,
|
| const AutocompleteInput& input) {
|
| - return CreateAutocompleteMatch(GetTemplateURLService(), keyword, input,
|
| - keyword.size(), SplitReplacementStringFromInput(text, true), 0);
|
| + return CreateAutocompleteMatch(
|
| + GetTemplateURLService()->GetTemplateURLForKeyword(keyword), input,
|
| + keyword.length(), SplitReplacementStringFromInput(text, true), 0);
|
| }
|
|
|
| void KeywordProvider::Start(const AutocompleteInput& input,
|
| @@ -253,8 +253,6 @@
|
| if (!ExtractKeywordFromInput(input, &keyword, &remaining_input))
|
| return;
|
|
|
| - TemplateURLService* model = GetTemplateURLService();
|
| -
|
| // Get the best matches for this keyword.
|
| //
|
| // NOTE: We could cache the previous keywords and reuse them here in the
|
| @@ -265,50 +263,47 @@
|
| // TODO(pkasting): http://b/893701 We should remember the user's use of a
|
| // search query both from the autocomplete popup and from web pages
|
| // themselves.
|
| - std::vector<string16> keyword_matches;
|
| - model->FindMatchingKeywords(keyword,
|
| - !remaining_input.empty(),
|
| - &keyword_matches);
|
| + TemplateURLService::TemplateURLVector matches;
|
| + GetTemplateURLService()->FindMatchingKeywords(
|
| + keyword, !remaining_input.empty(), &matches);
|
|
|
| - for (std::vector<string16>::iterator i(keyword_matches.begin());
|
| - i != keyword_matches.end(); ) {
|
| - const TemplateURL* template_url = model->GetTemplateURLForKeyword(*i);
|
| -
|
| + for (TemplateURLService::TemplateURLVector::iterator i(matches.begin());
|
| + i != matches.end(); ) {
|
| // Prune any extension keywords that are disallowed in incognito mode (if
|
| // we're incognito), or disabled.
|
| - if (profile_ && template_url->IsExtensionKeyword()) {
|
| + if (profile_ && (*i)->IsExtensionKeyword()) {
|
| ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
|
| extension_service();
|
| - const extensions::Extension* extension = service->GetExtensionById(
|
| - template_url->GetExtensionId(), false);
|
| + const extensions::Extension* extension =
|
| + service->GetExtensionById((*i)->GetExtensionId(), false);
|
| bool enabled =
|
| extension && (!profile_->IsOffTheRecord() ||
|
| service->IsIncognitoEnabled(extension->id()));
|
| if (!enabled) {
|
| - i = keyword_matches.erase(i);
|
| + i = matches.erase(i);
|
| continue;
|
| }
|
| }
|
|
|
| // Prune any substituting keywords if there is no substitution.
|
| - if (template_url->SupportsReplacement() && remaining_input.empty() &&
|
| + if ((*i)->SupportsReplacement() && remaining_input.empty() &&
|
| !input.allow_exact_keyword_match()) {
|
| - i = keyword_matches.erase(i);
|
| + i = matches.erase(i);
|
| continue;
|
| }
|
|
|
| ++i;
|
| }
|
| - if (keyword_matches.empty())
|
| + if (matches.empty())
|
| return;
|
| - std::sort(keyword_matches.begin(), keyword_matches.end(), CompareQuality());
|
| + std::sort(matches.begin(), matches.end(), CompareQuality());
|
|
|
| // Limit to one exact or three inexact matches, and mark them up for display
|
| // in the autocomplete popup.
|
| // Any exact match is going to be the highest quality match, and thus at the
|
| // front of our vector.
|
| - if (keyword_matches.front() == keyword) {
|
| - const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword);
|
| + if (matches.front()->keyword() == keyword) {
|
| + const TemplateURL* template_url = matches.front();
|
| const bool is_extension_keyword = template_url->IsExtensionKeyword();
|
|
|
| // Only create an exact match if |remaining_input| is empty or if
|
| @@ -321,9 +316,8 @@
|
|
|
| // TODO(pkasting): We should probably check that if the user explicitly
|
| // typed a scheme, that scheme matches the one in |template_url|.
|
| - matches_.push_back(CreateAutocompleteMatch(model, keyword, input,
|
| - keyword.length(),
|
| - remaining_input, -1));
|
| + matches_.push_back(CreateAutocompleteMatch(
|
| + template_url, input, keyword.length(), remaining_input, -1));
|
|
|
| if (profile_ && is_extension_keyword) {
|
| if (input.matches_requested() == AutocompleteInput::ALL_MATCHES) {
|
| @@ -364,16 +358,13 @@
|
| }
|
| }
|
| } else {
|
| - if (keyword_matches.size() > kMaxMatches) {
|
| - keyword_matches.erase(keyword_matches.begin() + kMaxMatches,
|
| - keyword_matches.end());
|
| + if (matches.size() > kMaxMatches)
|
| + matches.erase(matches.begin() + kMaxMatches, matches.end());
|
| + for (TemplateURLService::TemplateURLVector::const_iterator i(
|
| + matches.begin()); i != matches.end(); ++i) {
|
| + matches_.push_back(CreateAutocompleteMatch(
|
| + *i, input, keyword.length(), remaining_input, -1));
|
| }
|
| - for (std::vector<string16>::const_iterator i(keyword_matches.begin());
|
| - i != keyword_matches.end(); ++i) {
|
| - matches_.push_back(CreateAutocompleteMatch(model, *i,
|
| - input, keyword.length(),
|
| - remaining_input, -1));
|
| - }
|
| }
|
| }
|
|
|
| @@ -400,60 +391,6 @@
|
| }
|
|
|
| // static
|
| -void KeywordProvider::FillInURLAndContents(
|
| - const string16& remaining_input,
|
| - const TemplateURL* element,
|
| - AutocompleteMatch* match) {
|
| - DCHECK(!element->short_name().empty());
|
| - const TemplateURLRef& element_ref = element->url_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()) {
|
| - // No query input; return a generic, no-destination placeholder.
|
| - match->contents.assign(
|
| - l10n_util::GetStringFUTF16(message_id,
|
| - element->AdjustedShortNameForLocaleDirection(),
|
| - l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)));
|
| - match->contents_class.push_back(
|
| - ACMatchClassification(0, ACMatchClassification::DIM));
|
| - } else {
|
| - // Keyword that has no replacement text (aka a shorthand for a URL).
|
| - match->destination_url = GURL(element->url());
|
| - match->contents.assign(element->short_name());
|
| - AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(),
|
| - match->contents.length(), ACMatchClassification::NONE,
|
| - &match->contents_class);
|
| - }
|
| - } else {
|
| - // Create destination URL by escaping user input and substituting into
|
| - // 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.ReplaceSearchTerms(
|
| - TemplateURLRef::SearchTermsArgs(remaining_input)));
|
| - std::vector<size_t> content_param_offsets;
|
| - match->contents.assign(l10n_util::GetStringFUTF16(message_id,
|
| - element->short_name(),
|
| - remaining_input,
|
| - &content_param_offsets));
|
| - if (content_param_offsets.size() == 2) {
|
| - AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1],
|
| - remaining_input.length(), match->contents.length(),
|
| - ACMatchClassification::NONE, &match->contents_class);
|
| - } else {
|
| - // See comments on an identical NOTREACHED() in search_provider.cc.
|
| - NOTREACHED();
|
| - }
|
| - }
|
| -}
|
| -
|
| -// static
|
| int KeywordProvider::CalculateRelevance(AutocompleteInput::Type type,
|
| bool complete,
|
| bool supports_replacement,
|
| @@ -477,22 +414,20 @@
|
| }
|
|
|
| AutocompleteMatch KeywordProvider::CreateAutocompleteMatch(
|
| - TemplateURLService* model,
|
| - const string16& keyword,
|
| + const TemplateURL* template_url,
|
| const AutocompleteInput& input,
|
| size_t prefix_length,
|
| const string16& remaining_input,
|
| int relevance) {
|
| - DCHECK(model);
|
| - // Get keyword data from data store.
|
| - TemplateURL* element = model->GetTemplateURLForKeyword(keyword);
|
| - DCHECK(element);
|
| - const bool supports_replacement = element->url_ref().SupportsReplacement();
|
| + DCHECK(template_url);
|
| + const bool supports_replacement =
|
| + template_url->url_ref().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
|
| // choice and immediately begin typing in query input.
|
| - const bool keyword_complete = (prefix_length == keyword.length());
|
| + const bool keyword_complete =
|
| + (prefix_length == template_url->keyword().length());
|
| if (relevance < 0) {
|
| relevance =
|
| CalculateRelevance(input.type(), keyword_complete,
|
| @@ -505,7 +440,7 @@
|
| AutocompleteMatch match(this, relevance, false,
|
| supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE :
|
| AutocompleteMatchType::HISTORY_KEYWORD);
|
| - match.fill_into_edit.assign(keyword);
|
| + match.fill_into_edit = template_url->keyword();
|
| if (!remaining_input.empty() || !keyword_complete || supports_replacement)
|
| match.fill_into_edit.push_back(L' ');
|
| match.fill_into_edit.append(remaining_input);
|
| @@ -518,14 +453,69 @@
|
|
|
| // Create destination URL and popup entry content by substituting user input
|
| // into keyword templates.
|
| - FillInURLAndContents(remaining_input, element, &match);
|
| + FillInURLAndContents(remaining_input, template_url, &match);
|
|
|
| - match.keyword = keyword;
|
| + match.keyword = template_url->keyword();
|
| match.transition = content::PAGE_TRANSITION_KEYWORD;
|
|
|
| return match;
|
| }
|
|
|
| +void KeywordProvider::FillInURLAndContents(const string16& remaining_input,
|
| + const TemplateURL* element,
|
| + AutocompleteMatch* match) const {
|
| + DCHECK(!element->short_name().empty());
|
| + const TemplateURLRef& element_ref = element->url_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()) {
|
| + // No query input; return a generic, no-destination placeholder.
|
| + match->contents.assign(
|
| + l10n_util::GetStringFUTF16(message_id,
|
| + element->AdjustedShortNameForLocaleDirection(),
|
| + l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)));
|
| + match->contents_class.push_back(
|
| + ACMatchClassification(0, ACMatchClassification::DIM));
|
| + } else {
|
| + // Keyword that has no replacement text (aka a shorthand for a URL).
|
| + match->destination_url = GURL(element->url());
|
| + match->contents.assign(element->short_name());
|
| + AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(),
|
| + match->contents.length(), ACMatchClassification::NONE,
|
| + &match->contents_class);
|
| + }
|
| + } else {
|
| + // Create destination URL by escaping user input and substituting into
|
| + // 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());
|
| + TemplateURLRef::SearchTermsArgs search_terms_args(remaining_input);
|
| + search_terms_args.append_extra_query_params =
|
| + element == GetTemplateURLService()->GetDefaultSearchProvider();
|
| + match->destination_url =
|
| + GURL(element_ref.ReplaceSearchTerms(search_terms_args));
|
| + std::vector<size_t> content_param_offsets;
|
| + match->contents.assign(l10n_util::GetStringFUTF16(message_id,
|
| + element->short_name(),
|
| + remaining_input,
|
| + &content_param_offsets));
|
| + if (content_param_offsets.size() == 2) {
|
| + AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1],
|
| + remaining_input.length(), match->contents.length(),
|
| + ACMatchClassification::NONE, &match->contents_class);
|
| + } else {
|
| + // See comments on an identical NOTREACHED() in search_provider.cc.
|
| + NOTREACHED();
|
| + }
|
| + }
|
| +}
|
| +
|
| void KeywordProvider::Observe(int type,
|
| const content::NotificationSource& source,
|
| const content::NotificationDetails& details) {
|
| @@ -585,7 +575,7 @@
|
| int first_relevance = CalculateRelevance(input.type(), true, true,
|
| input.prefer_keyword(), input.allow_exact_keyword_match());
|
| extension_suggest_matches_.push_back(CreateAutocompleteMatch(
|
| - model, keyword, input, keyword.length(),
|
| + model->GetTemplateURLForKeyword(keyword), input, keyword.length(),
|
| UTF8ToUTF16(suggestion.content), first_relevance - (i + 1)));
|
|
|
| AutocompleteMatch* match = &extension_suggest_matches_.back();
|
|
|