Convert events_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / components / omnibox / keyword_provider.cc
blobc7d8a81c3185d1c24573836f468d1e7f9109be2a
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/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/autocomplete_match.h"
15 #include "components/omnibox/autocomplete_provider_client.h"
16 #include "components/omnibox/autocomplete_provider_listener.h"
17 #include "components/omnibox/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, true)) {
143 int offset = input->text().length() - input->cursor_position();
144 // The cursor should never be past the last character or before the
145 // first character.
146 DCHECK_GE(offset, 0);
147 DCHECK_LE(offset, static_cast<int>(input->text().length()));
148 if (offset <= 0) {
149 // Normalize the cursor to be exactly after the last character.
150 cursor_position = remaining_input.length();
151 } else {
152 // If somehow the cursor was before the remaining text, set it to 0,
153 // otherwise adjust it relative to the remaining text.
154 cursor_position = offset > static_cast<int>(remaining_input.length()) ?
155 0u : remaining_input.length() - offset;
158 input->UpdateText(remaining_input, cursor_position, input->parts());
159 return template_url;
162 return NULL;
165 base::string16 KeywordProvider::GetKeywordForText(
166 const base::string16& text) const {
167 const base::string16 keyword(TemplateURLService::CleanUserInputKeyword(text));
169 if (keyword.empty())
170 return keyword;
172 TemplateURLService* url_service = GetTemplateURLService();
173 if (!url_service)
174 return base::string16();
176 // Don't provide a keyword if it doesn't support replacement.
177 const TemplateURL* const template_url =
178 url_service->GetTemplateURLForKeyword(keyword);
179 if (!template_url ||
180 !template_url->SupportsReplacement(url_service->search_terms_data()))
181 return base::string16();
183 // Don't provide a keyword for inactive/disabled extension keywords.
184 if ((template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
185 extensions_delegate_ &&
186 !extensions_delegate_->IsEnabledExtension(template_url->GetExtensionId()))
187 return base::string16();
189 return keyword;
192 AutocompleteMatch KeywordProvider::CreateVerbatimMatch(
193 const base::string16& text,
194 const base::string16& keyword,
195 const AutocompleteInput& input) {
196 // A verbatim match is allowed to be the default match.
197 return CreateAutocompleteMatch(
198 GetTemplateURLService()->GetTemplateURLForKeyword(keyword), input,
199 keyword.length(), SplitReplacementStringFromInput(text, true), true, 0);
202 void KeywordProvider::Start(const AutocompleteInput& input,
203 bool minimal_changes) {
204 // This object ensures we end keyword mode if we exit the function without
205 // toggling keyword mode to on.
206 ScopedEndExtensionKeywordMode keyword_mode_toggle(extensions_delegate_.get());
208 matches_.clear();
210 if (!minimal_changes) {
211 done_ = true;
213 // Input has changed. Increment the input ID so that we can discard any
214 // stale extension suggestions that may be incoming.
215 if (extensions_delegate_)
216 extensions_delegate_->IncrementInputId();
219 if (input.from_omnibox_focus())
220 return;
222 // Split user input into a keyword and some query input.
224 // We want to suggest keywords even when users have started typing URLs, on
225 // the assumption that they might not realize they no longer need to go to a
226 // site to be able to search it. So we call CleanUserInputKeyword() to strip
227 // any initial scheme and/or "www.". NOTE: Any heuristics or UI used to
228 // automatically/manually create keywords will need to be in sync with
229 // whatever we do here!
231 // TODO(pkasting): http://crbug/347744 If someday we remember usage frequency
232 // for keywords, we might suggest keywords that haven't even been partially
233 // typed, if the user uses them enough and isn't obviously typing something
234 // else. In this case we'd consider all input here to be query input.
235 base::string16 keyword, remaining_input;
236 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input))
237 return;
239 // Get the best matches for this keyword.
241 // NOTE: We could cache the previous keywords and reuse them here in the
242 // |minimal_changes| case, but since we'd still have to recalculate their
243 // relevances and we can just recreate the results synchronously anyway, we
244 // don't bother.
245 TemplateURLService::TemplateURLVector matches;
246 GetTemplateURLService()->FindMatchingKeywords(
247 keyword, !remaining_input.empty(), &matches);
249 for (TemplateURLService::TemplateURLVector::iterator i(matches.begin());
250 i != matches.end(); ) {
251 const TemplateURL* template_url = *i;
253 // Prune any extension keywords that are disallowed in incognito mode (if
254 // we're incognito), or disabled.
255 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION &&
256 extensions_delegate_ &&
257 !extensions_delegate_->IsEnabledExtension(
258 template_url->GetExtensionId())) {
259 i = matches.erase(i);
260 continue;
263 // Prune any substituting keywords if there is no substitution.
264 if (template_url->SupportsReplacement(
265 GetTemplateURLService()->search_terms_data()) &&
266 remaining_input.empty() &&
267 !input.allow_exact_keyword_match()) {
268 i = matches.erase(i);
269 continue;
272 ++i;
274 if (matches.empty())
275 return;
276 std::sort(matches.begin(), matches.end(), CompareQuality());
278 // Limit to one exact or three inexact matches, and mark them up for display
279 // in the autocomplete popup.
280 // Any exact match is going to be the highest quality match, and thus at the
281 // front of our vector.
282 if (matches.front()->keyword() == keyword) {
283 const TemplateURL* template_url = matches.front();
284 const bool is_extension_keyword =
285 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION;
287 // Only create an exact match if |remaining_input| is empty or if
288 // this is an extension keyword. If |remaining_input| is a
289 // non-empty non-extension keyword (i.e., a regular keyword that
290 // supports replacement and that has extra text following it),
291 // then SearchProvider creates the exact (a.k.a. verbatim) match.
292 if (!remaining_input.empty() && !is_extension_keyword)
293 return;
295 // TODO(pkasting): We should probably check that if the user explicitly
296 // typed a scheme, that scheme matches the one in |template_url|.
298 // When creating an exact match (either for the keyword itself, no
299 // remaining query or an extension keyword, possibly with remaining
300 // input), allow the match to be the default match.
301 matches_.push_back(CreateAutocompleteMatch(
302 template_url, input, keyword.length(), remaining_input, true, -1));
304 if (is_extension_keyword && extensions_delegate_) {
305 if (extensions_delegate_->Start(input, minimal_changes, template_url,
306 remaining_input))
307 keyword_mode_toggle.StayInKeywordMode();
309 } else {
310 if (matches.size() > kMaxMatches)
311 matches.erase(matches.begin() + kMaxMatches, matches.end());
312 for (TemplateURLService::TemplateURLVector::const_iterator i(
313 matches.begin()); i != matches.end(); ++i) {
314 matches_.push_back(CreateAutocompleteMatch(
315 *i, input, keyword.length(), remaining_input, false, -1));
320 void KeywordProvider::Stop(bool clear_cached_results,
321 bool due_to_user_inactivity) {
322 done_ = true;
323 // Only end an extension's request if the user did something to explicitly
324 // cancel it; mere inactivity shouldn't terminate long-running extension
325 // operations since the user likely explicitly requested them.
326 if (extensions_delegate_ && !due_to_user_inactivity)
327 extensions_delegate_->MaybeEndExtensionKeywordMode();
330 KeywordProvider::~KeywordProvider() {}
332 // static
333 bool KeywordProvider::ExtractKeywordFromInput(const AutocompleteInput& input,
334 base::string16* keyword,
335 base::string16* remaining_input) {
336 if ((input.type() == metrics::OmniboxInputType::INVALID) ||
337 (input.type() == metrics::OmniboxInputType::FORCED_QUERY))
338 return false;
340 *keyword = TemplateURLService::CleanUserInputKeyword(
341 SplitKeywordFromInput(input.text(), true, remaining_input));
342 return !keyword->empty();
345 // static
346 int KeywordProvider::CalculateRelevance(metrics::OmniboxInputType::Type type,
347 bool complete,
348 bool supports_replacement,
349 bool prefer_keyword,
350 bool allow_exact_keyword_match) {
351 // This function is responsible for scoring suggestions of keywords
352 // themselves and the suggestion of the verbatim query on an
353 // extension keyword. SearchProvider::CalculateRelevanceForKeywordVerbatim()
354 // scores verbatim query suggestions for non-extension keywords.
355 // These two functions are currently in sync, but there's no reason
356 // we couldn't decide in the future to score verbatim matches
357 // differently for extension and non-extension keywords. If you
358 // make such a change, however, you should update this comment to
359 // describe it, so it's clear why the functions diverge.
360 if (!complete)
361 return (type == metrics::OmniboxInputType::URL) ? 700 : 450;
362 if (!supports_replacement || (allow_exact_keyword_match && prefer_keyword))
363 return 1500;
364 return (allow_exact_keyword_match &&
365 (type == metrics::OmniboxInputType::QUERY)) ?
366 1450 : 1100;
369 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch(
370 const TemplateURL* template_url,
371 const AutocompleteInput& input,
372 size_t prefix_length,
373 const base::string16& remaining_input,
374 bool allowed_to_be_default_match,
375 int relevance) {
376 DCHECK(template_url);
377 const bool supports_replacement =
378 template_url->url_ref().SupportsReplacement(
379 GetTemplateURLService()->search_terms_data());
381 // Create an edit entry of "[keyword] [remaining input]". This is helpful
382 // even when [remaining input] is empty, as the user can select the popup
383 // choice and immediately begin typing in query input.
384 const base::string16& keyword = template_url->keyword();
385 const bool keyword_complete = (prefix_length == keyword.length());
386 if (relevance < 0) {
387 relevance =
388 CalculateRelevance(input.type(), keyword_complete,
389 // When the user wants keyword matches to take
390 // preference, score them highly regardless of
391 // whether the input provides query text.
392 supports_replacement, input.prefer_keyword(),
393 input.allow_exact_keyword_match());
395 AutocompleteMatch match(this, relevance, false,
396 supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE :
397 AutocompleteMatchType::HISTORY_KEYWORD);
398 match.allowed_to_be_default_match = allowed_to_be_default_match;
399 match.fill_into_edit = keyword;
400 if (!remaining_input.empty() || supports_replacement)
401 match.fill_into_edit.push_back(L' ');
402 match.fill_into_edit.append(remaining_input);
403 // If we wanted to set |result.inline_autocompletion| correctly, we'd need
404 // CleanUserInputKeyword() to return the amount of adjustment it's made to
405 // the user's input. Because right now inexact keyword matches can't score
406 // more highly than a "what you typed" match from one of the other providers,
407 // we just don't bother to do this, and leave inline autocompletion off.
409 // Create destination URL and popup entry content by substituting user input
410 // into keyword templates.
411 FillInURLAndContents(remaining_input, template_url, &match);
413 match.keyword = keyword;
414 match.transition = ui::PAGE_TRANSITION_KEYWORD;
416 return match;
419 void KeywordProvider::FillInURLAndContents(
420 const base::string16& remaining_input,
421 const TemplateURL* element,
422 AutocompleteMatch* match) const {
423 DCHECK(!element->short_name().empty());
424 const TemplateURLRef& element_ref = element->url_ref();
425 DCHECK(element_ref.IsValid(GetTemplateURLService()->search_terms_data()));
426 int message_id = (element->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) ?
427 IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH;
428 if (remaining_input.empty()) {
429 // Allow extension keyword providers to accept empty string input. This is
430 // useful to allow extensions to do something in the case where no input is
431 // entered.
432 if (element_ref.SupportsReplacement(
433 GetTemplateURLService()->search_terms_data()) &&
434 (element->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) {
435 // No query input; return a generic, no-destination placeholder.
436 match->contents.assign(
437 l10n_util::GetStringFUTF16(message_id,
438 element->AdjustedShortNameForLocaleDirection(),
439 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)));
440 match->contents_class.push_back(
441 ACMatchClassification(0, ACMatchClassification::DIM));
442 } else {
443 // Keyword that has no replacement text (aka a shorthand for a URL).
444 match->destination_url = GURL(element->url());
445 match->contents.assign(element->short_name());
446 AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(),
447 match->contents.length(), ACMatchClassification::NONE,
448 &match->contents_class);
450 } else {
451 // Create destination URL by escaping user input and substituting into
452 // keyword template URL. The escaping here handles whitespace in user
453 // input, but we rely on later canonicalization functions to do more
454 // fixup to make the URL valid if necessary.
455 DCHECK(element_ref.SupportsReplacement(
456 GetTemplateURLService()->search_terms_data()));
457 TemplateURLRef::SearchTermsArgs search_terms_args(remaining_input);
458 search_terms_args.append_extra_query_params =
459 element == GetTemplateURLService()->GetDefaultSearchProvider();
460 match->destination_url = GURL(element_ref.ReplaceSearchTerms(
461 search_terms_args, GetTemplateURLService()->search_terms_data()));
462 std::vector<size_t> content_param_offsets;
463 match->contents.assign(l10n_util::GetStringFUTF16(message_id,
464 element->short_name(),
465 remaining_input,
466 &content_param_offsets));
467 DCHECK_EQ(2U, content_param_offsets.size());
468 AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1],
469 remaining_input.length(), match->contents.length(),
470 ACMatchClassification::NONE, &match->contents_class);
474 TemplateURLService* KeywordProvider::GetTemplateURLService() const {
475 // Make sure the model is loaded. This is cheap and quickly bails out if
476 // the model is already loaded.
477 model_->Load();
478 return model_;