Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / keyword_provider.cc
blob8992f02f8717f4d795f9b93b76d03ee8866442e9
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/omnibox/browser/keyword_provider.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "components/metrics/proto/omnibox_input_type.pb.h"
14 #include "components/omnibox/browser/autocomplete_match.h"
15 #include "components/omnibox/browser/autocomplete_provider_client.h"
16 #include "components/omnibox/browser/autocomplete_provider_listener.h"
17 #include "components/omnibox/browser/keyword_extensions_delegate.h"
18 #include "components/search_engines/template_url.h"
19 #include "components/search_engines/template_url_service.h"
20 #include "grit/components_strings.h"
21 #include "net/base/escape.h"
22 #include "net/base/net_util.h"
23 #include "ui/base/l10n/l10n_util.h"
25 namespace {
27 // Helper functor for Start(), for sorting keyword matches by quality.
28 class CompareQuality {
29 public:
30 // A keyword is of higher quality when a greater fraction of it has been
31 // typed, that is, when it is shorter.
33 // TODO(pkasting): Most recent and most frequent keywords are probably
34 // better rankings than the fraction of the keyword typed. We should
35 // always put any exact matches first no matter what, since the code in
36 // Start() assumes this (and it makes sense).
37 bool operator()(const TemplateURL* t_url1, const TemplateURL* t_url2) const {
38 return t_url1->keyword().length() < t_url2->keyword().length();
42 // Helper for KeywordProvider::Start(), for ending keyword mode unless
43 // explicitly told otherwise.
44 class ScopedEndExtensionKeywordMode {
45 public:
46 explicit ScopedEndExtensionKeywordMode(KeywordExtensionsDelegate* delegate);
47 ~ScopedEndExtensionKeywordMode();
49 void StayInKeywordMode();
51 private:
52 KeywordExtensionsDelegate* delegate_;
54 DISALLOW_COPY_AND_ASSIGN(ScopedEndExtensionKeywordMode);
57 ScopedEndExtensionKeywordMode::ScopedEndExtensionKeywordMode(
58 KeywordExtensionsDelegate* delegate)
59 : delegate_(delegate) {
62 ScopedEndExtensionKeywordMode::~ScopedEndExtensionKeywordMode() {
63 if (delegate_)
64 delegate_->MaybeEndExtensionKeywordMode();
67 void ScopedEndExtensionKeywordMode::StayInKeywordMode() {
68 delegate_ = NULL;
71 } // namespace
73 KeywordProvider::KeywordProvider(AutocompleteProviderClient* client,
74 AutocompleteProviderListener* listener)
75 : AutocompleteProvider(AutocompleteProvider::TYPE_KEYWORD),
76 listener_(listener),
77 model_(client->GetTemplateURLService()),
78 extensions_delegate_(client->GetKeywordExtensionsDelegate(this)) {
81 // static
82 base::string16 KeywordProvider::SplitKeywordFromInput(
83 const base::string16& input,
84 bool trim_leading_whitespace,
85 base::string16* remaining_input) {
86 // Find end of first token. The AutocompleteController has trimmed leading
87 // whitespace, so we need not skip over that.
88 const size_t first_white(input.find_first_of(base::kWhitespaceUTF16));
89 DCHECK_NE(0U, first_white);
90 if (first_white == base::string16::npos)
91 return input; // Only one token provided.
93 // Set |remaining_input| to everything after the first token.
94 DCHECK(remaining_input != NULL);
95 const size_t remaining_start = trim_leading_whitespace ?
96 input.find_first_not_of(base::kWhitespaceUTF16, first_white) :
97 first_white + 1;
99 if (remaining_start < input.length())
100 remaining_input->assign(input.begin() + remaining_start, input.end());
102 // Return first token as keyword.
103 return input.substr(0, first_white);
106 // static
107 base::string16 KeywordProvider::SplitReplacementStringFromInput(
108 const base::string16& input,
109 bool trim_leading_whitespace) {
110 // The input may contain leading whitespace, strip it.
111 base::string16 trimmed_input;
112 base::TrimWhitespace(input, base::TRIM_LEADING, &trimmed_input);
114 // And extract the replacement string.
115 base::string16 remaining_input;
116 SplitKeywordFromInput(trimmed_input, trim_leading_whitespace,
117 &remaining_input);
118 return remaining_input;
121 // static
122 const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput(
123 TemplateURLService* model,
124 AutocompleteInput* input) {
125 if (!input->allow_exact_keyword_match())
126 return NULL;
128 base::string16 keyword, remaining_input;
129 if (!ExtractKeywordFromInput(*input, &keyword, &remaining_input))
130 return NULL;
132 DCHECK(model);
133 const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword);
134 if (template_url &&
135 template_url->SupportsReplacement(model->search_terms_data())) {
136 // Adjust cursor position iff it was set before, otherwise leave it as is.
137 size_t cursor_position = base::string16::npos;
138 // The adjustment assumes that the keyword was stripped from the beginning
139 // of the original input.
140 if (input->cursor_position() != base::string16::npos &&
141 !remaining_input.empty() &&
142 base::EndsWith(input->text(), remaining_input,
143 base::CompareCase::SENSITIVE)) {
144 int offset = input->text().length() - input->cursor_position();
145 // The cursor should never be past the last character or before the
146 // first character.
147 DCHECK_GE(offset, 0);
148 DCHECK_LE(offset, static_cast<int>(input->text().length()));
149 if (offset <= 0) {
150 // Normalize the cursor to be exactly after the last character.
151 cursor_position = remaining_input.length();
152 } else {
153 // If somehow the cursor was before the remaining text, set it to 0,
154 // otherwise adjust it relative to the remaining text.
155 cursor_position = offset > static_cast<int>(remaining_input.length()) ?
156 0u : remaining_input.length() - offset;
159 input->UpdateText(remaining_input, cursor_position, input->parts());
160 return template_url;
163 return NULL;
166 base::string16 KeywordProvider::GetKeywordForText(
167 const base::string16& text) const {
168 const base::string16 keyword(TemplateURLService::CleanUserInputKeyword(text));
170 if (keyword.empty())
171 return keyword;
173 TemplateURLService* url_service = GetTemplateURLService();
174 if (!url_service)
175 return base::string16();
177 // Don't provide a keyword if it doesn't support replacement.
178 const TemplateURL* const template_url =
179 url_service->GetTemplateURLForKeyword(keyword);
180 if (!template_url ||
181 !template_url->SupportsReplacement(url_service->search_terms_data()))
182 return base::string16();
184 // Don't provide a keyword for inactive/disabled extension keywords.
185 if ((template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
186 extensions_delegate_ &&
187 !extensions_delegate_->IsEnabledExtension(template_url->GetExtensionId()))
188 return base::string16();
190 return keyword;
193 AutocompleteMatch KeywordProvider::CreateVerbatimMatch(
194 const base::string16& text,
195 const base::string16& keyword,
196 const AutocompleteInput& input) {
197 // A verbatim match is allowed to be the default match.
198 return CreateAutocompleteMatch(
199 GetTemplateURLService()->GetTemplateURLForKeyword(keyword), input,
200 keyword.length(), SplitReplacementStringFromInput(text, true), true, 0);
203 void KeywordProvider::Start(const AutocompleteInput& input,
204 bool minimal_changes) {
205 // This object ensures we end keyword mode if we exit the function without
206 // toggling keyword mode to on.
207 ScopedEndExtensionKeywordMode keyword_mode_toggle(extensions_delegate_.get());
209 matches_.clear();
211 if (!minimal_changes) {
212 done_ = true;
214 // Input has changed. Increment the input ID so that we can discard any
215 // stale extension suggestions that may be incoming.
216 if (extensions_delegate_)
217 extensions_delegate_->IncrementInputId();
220 if (input.from_omnibox_focus())
221 return;
223 // Split user input into a keyword and some query input.
225 // We want to suggest keywords even when users have started typing URLs, on
226 // the assumption that they might not realize they no longer need to go to a
227 // site to be able to search it. So we call CleanUserInputKeyword() to strip
228 // any initial scheme and/or "www.". NOTE: Any heuristics or UI used to
229 // automatically/manually create keywords will need to be in sync with
230 // whatever we do here!
232 // TODO(pkasting): http://crbug/347744 If someday we remember usage frequency
233 // for keywords, we might suggest keywords that haven't even been partially
234 // typed, if the user uses them enough and isn't obviously typing something
235 // else. In this case we'd consider all input here to be query input.
236 base::string16 keyword, remaining_input;
237 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input))
238 return;
240 // Get the best matches for this keyword.
242 // NOTE: We could cache the previous keywords and reuse them here in the
243 // |minimal_changes| case, but since we'd still have to recalculate their
244 // relevances and we can just recreate the results synchronously anyway, we
245 // don't bother.
246 TemplateURLService::TemplateURLVector matches;
247 GetTemplateURLService()->FindMatchingKeywords(
248 keyword, !remaining_input.empty(), &matches);
250 for (TemplateURLService::TemplateURLVector::iterator i(matches.begin());
251 i != matches.end(); ) {
252 const TemplateURL* template_url = *i;
254 // Prune any extension keywords that are disallowed in incognito mode (if
255 // we're incognito), or disabled.
256 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION &&
257 extensions_delegate_ &&
258 !extensions_delegate_->IsEnabledExtension(
259 template_url->GetExtensionId())) {
260 i = matches.erase(i);
261 continue;
264 // Prune any substituting keywords if there is no substitution.
265 if (template_url->SupportsReplacement(
266 GetTemplateURLService()->search_terms_data()) &&
267 remaining_input.empty() &&
268 !input.allow_exact_keyword_match()) {
269 i = matches.erase(i);
270 continue;
273 ++i;
275 if (matches.empty())
276 return;
277 std::sort(matches.begin(), matches.end(), CompareQuality());
279 // Limit to one exact or three inexact matches, and mark them up for display
280 // in the autocomplete popup.
281 // Any exact match is going to be the highest quality match, and thus at the
282 // front of our vector.
283 if (matches.front()->keyword() == keyword) {
284 const TemplateURL* template_url = matches.front();
285 const bool is_extension_keyword =
286 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION;
288 // Only create an exact match if |remaining_input| is empty or if
289 // this is an extension keyword. If |remaining_input| is a
290 // non-empty non-extension keyword (i.e., a regular keyword that
291 // supports replacement and that has extra text following it),
292 // then SearchProvider creates the exact (a.k.a. verbatim) match.
293 if (!remaining_input.empty() && !is_extension_keyword)
294 return;
296 // TODO(pkasting): We should probably check that if the user explicitly
297 // typed a scheme, that scheme matches the one in |template_url|.
299 // When creating an exact match (either for the keyword itself, no
300 // remaining query or an extension keyword, possibly with remaining
301 // input), allow the match to be the default match.
302 matches_.push_back(CreateAutocompleteMatch(
303 template_url, input, keyword.length(), remaining_input, true, -1));
305 if (is_extension_keyword && extensions_delegate_) {
306 if (extensions_delegate_->Start(input, minimal_changes, template_url,
307 remaining_input))
308 keyword_mode_toggle.StayInKeywordMode();
310 } else {
311 if (matches.size() > kMaxMatches)
312 matches.erase(matches.begin() + kMaxMatches, matches.end());
313 for (TemplateURLService::TemplateURLVector::const_iterator i(
314 matches.begin()); i != matches.end(); ++i) {
315 matches_.push_back(CreateAutocompleteMatch(
316 *i, input, keyword.length(), remaining_input, false, -1));
321 void KeywordProvider::Stop(bool clear_cached_results,
322 bool due_to_user_inactivity) {
323 done_ = true;
324 // Only end an extension's request if the user did something to explicitly
325 // cancel it; mere inactivity shouldn't terminate long-running extension
326 // operations since the user likely explicitly requested them.
327 if (extensions_delegate_ && !due_to_user_inactivity)
328 extensions_delegate_->MaybeEndExtensionKeywordMode();
331 KeywordProvider::~KeywordProvider() {}
333 // static
334 bool KeywordProvider::ExtractKeywordFromInput(const AutocompleteInput& input,
335 base::string16* keyword,
336 base::string16* remaining_input) {
337 if ((input.type() == metrics::OmniboxInputType::INVALID) ||
338 (input.type() == metrics::OmniboxInputType::FORCED_QUERY))
339 return false;
341 *keyword = TemplateURLService::CleanUserInputKeyword(
342 SplitKeywordFromInput(input.text(), true, remaining_input));
343 return !keyword->empty();
346 // static
347 int KeywordProvider::CalculateRelevance(metrics::OmniboxInputType::Type type,
348 bool complete,
349 bool supports_replacement,
350 bool prefer_keyword,
351 bool allow_exact_keyword_match) {
352 // This function is responsible for scoring suggestions of keywords
353 // themselves and the suggestion of the verbatim query on an
354 // extension keyword. SearchProvider::CalculateRelevanceForKeywordVerbatim()
355 // scores verbatim query suggestions for non-extension keywords.
356 // These two functions are currently in sync, but there's no reason
357 // we couldn't decide in the future to score verbatim matches
358 // differently for extension and non-extension keywords. If you
359 // make such a change, however, you should update this comment to
360 // describe it, so it's clear why the functions diverge.
361 if (!complete)
362 return (type == metrics::OmniboxInputType::URL) ? 700 : 450;
363 if (!supports_replacement || (allow_exact_keyword_match && prefer_keyword))
364 return 1500;
365 return (allow_exact_keyword_match &&
366 (type == metrics::OmniboxInputType::QUERY)) ?
367 1450 : 1100;
370 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch(
371 const TemplateURL* template_url,
372 const AutocompleteInput& input,
373 size_t prefix_length,
374 const base::string16& remaining_input,
375 bool allowed_to_be_default_match,
376 int relevance) {
377 DCHECK(template_url);
378 const bool supports_replacement =
379 template_url->url_ref().SupportsReplacement(
380 GetTemplateURLService()->search_terms_data());
382 // Create an edit entry of "[keyword] [remaining input]". This is helpful
383 // even when [remaining input] is empty, as the user can select the popup
384 // choice and immediately begin typing in query input.
385 const base::string16& keyword = template_url->keyword();
386 const bool keyword_complete = (prefix_length == keyword.length());
387 if (relevance < 0) {
388 relevance =
389 CalculateRelevance(input.type(), keyword_complete,
390 // When the user wants keyword matches to take
391 // preference, score them highly regardless of
392 // whether the input provides query text.
393 supports_replacement, input.prefer_keyword(),
394 input.allow_exact_keyword_match());
396 AutocompleteMatch match(this, relevance, false,
397 supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE :
398 AutocompleteMatchType::HISTORY_KEYWORD);
399 match.allowed_to_be_default_match = allowed_to_be_default_match;
400 match.fill_into_edit = keyword;
401 if (!remaining_input.empty() || supports_replacement)
402 match.fill_into_edit.push_back(L' ');
403 match.fill_into_edit.append(remaining_input);
404 // If we wanted to set |result.inline_autocompletion| correctly, we'd need
405 // CleanUserInputKeyword() to return the amount of adjustment it's made to
406 // the user's input. Because right now inexact keyword matches can't score
407 // more highly than a "what you typed" match from one of the other providers,
408 // we just don't bother to do this, and leave inline autocompletion off.
410 // Create destination URL and popup entry content by substituting user input
411 // into keyword templates.
412 FillInURLAndContents(remaining_input, template_url, &match);
414 match.keyword = keyword;
415 match.transition = ui::PAGE_TRANSITION_KEYWORD;
417 return match;
420 void KeywordProvider::FillInURLAndContents(
421 const base::string16& remaining_input,
422 const TemplateURL* element,
423 AutocompleteMatch* match) const {
424 DCHECK(!element->short_name().empty());
425 const TemplateURLRef& element_ref = element->url_ref();
426 DCHECK(element_ref.IsValid(GetTemplateURLService()->search_terms_data()));
427 int message_id = (element->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) ?
428 IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH;
429 if (remaining_input.empty()) {
430 // Allow extension keyword providers to accept empty string input. This is
431 // useful to allow extensions to do something in the case where no input is
432 // entered.
433 if (element_ref.SupportsReplacement(
434 GetTemplateURLService()->search_terms_data()) &&
435 (element->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) {
436 // No query input; return a generic, no-destination placeholder.
437 match->contents.assign(
438 l10n_util::GetStringFUTF16(message_id,
439 element->AdjustedShortNameForLocaleDirection(),
440 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)));
441 match->contents_class.push_back(
442 ACMatchClassification(0, ACMatchClassification::DIM));
443 } else {
444 // Keyword that has no replacement text (aka a shorthand for a URL).
445 match->destination_url = GURL(element->url());
446 match->contents.assign(element->short_name());
447 AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(),
448 match->contents.length(), ACMatchClassification::NONE,
449 &match->contents_class);
451 } else {
452 // Create destination URL by escaping user input and substituting into
453 // keyword template URL. The escaping here handles whitespace in user
454 // input, but we rely on later canonicalization functions to do more
455 // fixup to make the URL valid if necessary.
456 DCHECK(element_ref.SupportsReplacement(
457 GetTemplateURLService()->search_terms_data()));
458 TemplateURLRef::SearchTermsArgs search_terms_args(remaining_input);
459 search_terms_args.append_extra_query_params =
460 element == GetTemplateURLService()->GetDefaultSearchProvider();
461 match->destination_url = GURL(element_ref.ReplaceSearchTerms(
462 search_terms_args, GetTemplateURLService()->search_terms_data()));
463 std::vector<size_t> content_param_offsets;
464 match->contents.assign(l10n_util::GetStringFUTF16(message_id,
465 element->short_name(),
466 remaining_input,
467 &content_param_offsets));
468 DCHECK_EQ(2U, content_param_offsets.size());
469 AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1],
470 remaining_input.length(), match->contents.length(),
471 ACMatchClassification::NONE, &match->contents_class);
475 TemplateURLService* KeywordProvider::GetTemplateURLService() const {
476 // Make sure the model is loaded. This is cheap and quickly bails out if
477 // the model is already loaded.
478 model_->Load();
479 return model_;