[Chromoting] Add app_remoting/internal to .gitignore
[chromium-blink-merge.git] / components / omnibox / keyword_provider.cc
blob25807773a61ab932d7116e6a20c58e8b8426ab8c
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_listener.h"
16 #include "components/omnibox/keyword_extensions_delegate.h"
17 #include "components/search_engines/template_url.h"
18 #include "components/search_engines/template_url_service.h"
19 #include "grit/components_strings.h"
20 #include "net/base/escape.h"
21 #include "net/base/net_util.h"
22 #include "ui/base/l10n/l10n_util.h"
24 namespace {
26 // Helper functor for Start(), for sorting keyword matches by quality.
27 class CompareQuality {
28 public:
29 // A keyword is of higher quality when a greater fraction of it has been
30 // typed, that is, when it is shorter.
32 // TODO(pkasting): Most recent and most frequent keywords are probably
33 // better rankings than the fraction of the keyword typed. We should
34 // always put any exact matches first no matter what, since the code in
35 // Start() assumes this (and it makes sense).
36 bool operator()(const TemplateURL* t_url1, const TemplateURL* t_url2) const {
37 return t_url1->keyword().length() < t_url2->keyword().length();
41 // Helper for KeywordProvider::Start(), for ending keyword mode unless
42 // explicitly told otherwise.
43 class ScopedEndExtensionKeywordMode {
44 public:
45 explicit ScopedEndExtensionKeywordMode(KeywordExtensionsDelegate* delegate);
46 ~ScopedEndExtensionKeywordMode();
48 void StayInKeywordMode();
50 private:
51 KeywordExtensionsDelegate* delegate_;
53 DISALLOW_COPY_AND_ASSIGN(ScopedEndExtensionKeywordMode);
56 ScopedEndExtensionKeywordMode::ScopedEndExtensionKeywordMode(
57 KeywordExtensionsDelegate* delegate)
58 : delegate_(delegate) {
61 ScopedEndExtensionKeywordMode::~ScopedEndExtensionKeywordMode() {
62 if (delegate_)
63 delegate_->MaybeEndExtensionKeywordMode();
66 void ScopedEndExtensionKeywordMode::StayInKeywordMode() {
67 delegate_ = NULL;
70 } // namespace
72 KeywordProvider::KeywordProvider(
73 AutocompleteProviderListener* listener,
74 TemplateURLService* model)
75 : AutocompleteProvider(AutocompleteProvider::TYPE_KEYWORD),
76 listener_(listener),
77 model_(model) {
80 // static
81 base::string16 KeywordProvider::SplitKeywordFromInput(
82 const base::string16& input,
83 bool trim_leading_whitespace,
84 base::string16* remaining_input) {
85 // Find end of first token. The AutocompleteController has trimmed leading
86 // whitespace, so we need not skip over that.
87 const size_t first_white(input.find_first_of(base::kWhitespaceUTF16));
88 DCHECK_NE(0U, first_white);
89 if (first_white == base::string16::npos)
90 return input; // Only one token provided.
92 // Set |remaining_input| to everything after the first token.
93 DCHECK(remaining_input != NULL);
94 const size_t remaining_start = trim_leading_whitespace ?
95 input.find_first_not_of(base::kWhitespaceUTF16, first_white) :
96 first_white + 1;
98 if (remaining_start < input.length())
99 remaining_input->assign(input.begin() + remaining_start, input.end());
101 // Return first token as keyword.
102 return input.substr(0, first_white);
105 // static
106 base::string16 KeywordProvider::SplitReplacementStringFromInput(
107 const base::string16& input,
108 bool trim_leading_whitespace) {
109 // The input may contain leading whitespace, strip it.
110 base::string16 trimmed_input;
111 base::TrimWhitespace(input, base::TRIM_LEADING, &trimmed_input);
113 // And extract the replacement string.
114 base::string16 remaining_input;
115 SplitKeywordFromInput(trimmed_input, trim_leading_whitespace,
116 &remaining_input);
117 return remaining_input;
120 // static
121 const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput(
122 TemplateURLService* model,
123 AutocompleteInput* input) {
124 if (!input->allow_exact_keyword_match())
125 return NULL;
127 base::string16 keyword, remaining_input;
128 if (!ExtractKeywordFromInput(*input, &keyword, &remaining_input))
129 return NULL;
131 DCHECK(model);
132 const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword);
133 if (template_url &&
134 template_url->SupportsReplacement(model->search_terms_data())) {
135 // Adjust cursor position iff it was set before, otherwise leave it as is.
136 size_t cursor_position = base::string16::npos;
137 // The adjustment assumes that the keyword was stripped from the beginning
138 // of the original input.
139 if (input->cursor_position() != base::string16::npos &&
140 !remaining_input.empty() &&
141 EndsWith(input->text(), remaining_input, true)) {
142 int offset = input->text().length() - input->cursor_position();
143 // The cursor should never be past the last character or before the
144 // first character.
145 DCHECK_GE(offset, 0);
146 DCHECK_LE(offset, static_cast<int>(input->text().length()));
147 if (offset <= 0) {
148 // Normalize the cursor to be exactly after the last character.
149 cursor_position = remaining_input.length();
150 } else {
151 // If somehow the cursor was before the remaining text, set it to 0,
152 // otherwise adjust it relative to the remaining text.
153 cursor_position = offset > static_cast<int>(remaining_input.length()) ?
154 0u : remaining_input.length() - offset;
157 input->UpdateText(remaining_input, cursor_position, input->parts());
158 return template_url;
161 return NULL;
164 base::string16 KeywordProvider::GetKeywordForText(
165 const base::string16& text) const {
166 const base::string16 keyword(TemplateURLService::CleanUserInputKeyword(text));
168 if (keyword.empty())
169 return keyword;
171 TemplateURLService* url_service = GetTemplateURLService();
172 if (!url_service)
173 return base::string16();
175 // Don't provide a keyword if it doesn't support replacement.
176 const TemplateURL* const template_url =
177 url_service->GetTemplateURLForKeyword(keyword);
178 if (!template_url ||
179 !template_url->SupportsReplacement(url_service->search_terms_data()))
180 return base::string16();
182 // Don't provide a keyword for inactive/disabled extension keywords.
183 if ((template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
184 extensions_delegate_ &&
185 !extensions_delegate_->IsEnabledExtension(template_url->GetExtensionId()))
186 return base::string16();
188 return keyword;
191 AutocompleteMatch KeywordProvider::CreateVerbatimMatch(
192 const base::string16& text,
193 const base::string16& keyword,
194 const AutocompleteInput& input) {
195 // A verbatim match is allowed to be the default match.
196 return CreateAutocompleteMatch(
197 GetTemplateURLService()->GetTemplateURLForKeyword(keyword), input,
198 keyword.length(), SplitReplacementStringFromInput(text, true), true, 0);
201 void KeywordProvider::Start(const AutocompleteInput& input,
202 bool minimal_changes,
203 bool called_due_to_focus) {
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 (called_due_to_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 done_ = true;
322 if (extensions_delegate_)
323 extensions_delegate_->MaybeEndExtensionKeywordMode();
326 KeywordProvider::~KeywordProvider() {}
328 // static
329 bool KeywordProvider::ExtractKeywordFromInput(const AutocompleteInput& input,
330 base::string16* keyword,
331 base::string16* remaining_input) {
332 if ((input.type() == metrics::OmniboxInputType::INVALID) ||
333 (input.type() == metrics::OmniboxInputType::FORCED_QUERY))
334 return false;
336 *keyword = TemplateURLService::CleanUserInputKeyword(
337 SplitKeywordFromInput(input.text(), true, remaining_input));
338 return !keyword->empty();
341 // static
342 int KeywordProvider::CalculateRelevance(metrics::OmniboxInputType::Type type,
343 bool complete,
344 bool supports_replacement,
345 bool prefer_keyword,
346 bool allow_exact_keyword_match) {
347 // This function is responsible for scoring suggestions of keywords
348 // themselves and the suggestion of the verbatim query on an
349 // extension keyword. SearchProvider::CalculateRelevanceForKeywordVerbatim()
350 // scores verbatim query suggestions for non-extension keywords.
351 // These two functions are currently in sync, but there's no reason
352 // we couldn't decide in the future to score verbatim matches
353 // differently for extension and non-extension keywords. If you
354 // make such a change, however, you should update this comment to
355 // describe it, so it's clear why the functions diverge.
356 if (!complete)
357 return (type == metrics::OmniboxInputType::URL) ? 700 : 450;
358 if (!supports_replacement || (allow_exact_keyword_match && prefer_keyword))
359 return 1500;
360 return (allow_exact_keyword_match &&
361 (type == metrics::OmniboxInputType::QUERY)) ?
362 1450 : 1100;
365 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch(
366 const TemplateURL* template_url,
367 const AutocompleteInput& input,
368 size_t prefix_length,
369 const base::string16& remaining_input,
370 bool allowed_to_be_default_match,
371 int relevance) {
372 DCHECK(template_url);
373 const bool supports_replacement =
374 template_url->url_ref().SupportsReplacement(
375 GetTemplateURLService()->search_terms_data());
377 // Create an edit entry of "[keyword] [remaining input]". This is helpful
378 // even when [remaining input] is empty, as the user can select the popup
379 // choice and immediately begin typing in query input.
380 const base::string16& keyword = template_url->keyword();
381 const bool keyword_complete = (prefix_length == keyword.length());
382 if (relevance < 0) {
383 relevance =
384 CalculateRelevance(input.type(), keyword_complete,
385 // When the user wants keyword matches to take
386 // preference, score them highly regardless of
387 // whether the input provides query text.
388 supports_replacement, input.prefer_keyword(),
389 input.allow_exact_keyword_match());
391 AutocompleteMatch match(this, relevance, false,
392 supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE :
393 AutocompleteMatchType::HISTORY_KEYWORD);
394 match.allowed_to_be_default_match = allowed_to_be_default_match;
395 match.fill_into_edit = keyword;
396 if (!remaining_input.empty() || supports_replacement)
397 match.fill_into_edit.push_back(L' ');
398 match.fill_into_edit.append(remaining_input);
399 // If we wanted to set |result.inline_autocompletion| correctly, we'd need
400 // CleanUserInputKeyword() to return the amount of adjustment it's made to
401 // the user's input. Because right now inexact keyword matches can't score
402 // more highly than a "what you typed" match from one of the other providers,
403 // we just don't bother to do this, and leave inline autocompletion off.
405 // Create destination URL and popup entry content by substituting user input
406 // into keyword templates.
407 FillInURLAndContents(remaining_input, template_url, &match);
409 match.keyword = keyword;
410 match.transition = ui::PAGE_TRANSITION_KEYWORD;
412 return match;
415 void KeywordProvider::FillInURLAndContents(
416 const base::string16& remaining_input,
417 const TemplateURL* element,
418 AutocompleteMatch* match) const {
419 DCHECK(!element->short_name().empty());
420 const TemplateURLRef& element_ref = element->url_ref();
421 DCHECK(element_ref.IsValid(GetTemplateURLService()->search_terms_data()));
422 int message_id = (element->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) ?
423 IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH;
424 if (remaining_input.empty()) {
425 // Allow extension keyword providers to accept empty string input. This is
426 // useful to allow extensions to do something in the case where no input is
427 // entered.
428 if (element_ref.SupportsReplacement(
429 GetTemplateURLService()->search_terms_data()) &&
430 (element->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) {
431 // No query input; return a generic, no-destination placeholder.
432 match->contents.assign(
433 l10n_util::GetStringFUTF16(message_id,
434 element->AdjustedShortNameForLocaleDirection(),
435 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)));
436 match->contents_class.push_back(
437 ACMatchClassification(0, ACMatchClassification::DIM));
438 } else {
439 // Keyword that has no replacement text (aka a shorthand for a URL).
440 match->destination_url = GURL(element->url());
441 match->contents.assign(element->short_name());
442 AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(),
443 match->contents.length(), ACMatchClassification::NONE,
444 &match->contents_class);
446 } else {
447 // Create destination URL by escaping user input and substituting into
448 // keyword template URL. The escaping here handles whitespace in user
449 // input, but we rely on later canonicalization functions to do more
450 // fixup to make the URL valid if necessary.
451 DCHECK(element_ref.SupportsReplacement(
452 GetTemplateURLService()->search_terms_data()));
453 TemplateURLRef::SearchTermsArgs search_terms_args(remaining_input);
454 search_terms_args.append_extra_query_params =
455 element == GetTemplateURLService()->GetDefaultSearchProvider();
456 match->destination_url = GURL(element_ref.ReplaceSearchTerms(
457 search_terms_args, GetTemplateURLService()->search_terms_data()));
458 std::vector<size_t> content_param_offsets;
459 match->contents.assign(l10n_util::GetStringFUTF16(message_id,
460 element->short_name(),
461 remaining_input,
462 &content_param_offsets));
463 DCHECK_EQ(2U, content_param_offsets.size());
464 AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1],
465 remaining_input.length(), match->contents.length(),
466 ACMatchClassification::NONE, &match->contents_class);
470 TemplateURLService* KeywordProvider::GetTemplateURLService() const {
471 // Make sure the model is loaded. This is cheap and quickly bails out if
472 // the model is already loaded.
473 model_->Load();
474 return model_;