[Audio Player] Cleanup the code along with closure compiler
[chromium-blink-merge.git] / components / omnibox / keyword_provider.cc
blob13b2710637b3a60039d237bed48112b304caae41
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 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_;