keyword_provider.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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.
4
5#include "chrome/browser/autocomplete/keyword_provider.h"
6
7#include <algorithm>
8#include <vector>
9
10#include "base/string16.h"
11#include "base/utf_string_conversions.h"
12#include "chrome/browser/autocomplete/autocomplete_match.h"
13#include "chrome/browser/extensions/extension_omnibox_api.h"
14#include "chrome/browser/extensions/extension_service.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/browser/search_engines/template_url.h"
17#include "chrome/browser/search_engines/template_url_model.h"
18#include "chrome/common/notification_details.h"
19#include "chrome/common/notification_source.h"
20#include "grit/generated_resources.h"
21#include "net/base/escape.h"
22#include "net/base/net_util.h"
23#include "ui/base/l10n/l10n_util.h"
24
25// Helper functor for Start(), for ending keyword mode unless explicitly told
26// otherwise.
27class KeywordProvider::ScopedEndExtensionKeywordMode {
28 public:
29  explicit ScopedEndExtensionKeywordMode(KeywordProvider* provider)
30      : provider_(provider) { }
31  ~ScopedEndExtensionKeywordMode() {
32    if (provider_)
33      provider_->MaybeEndExtensionKeywordMode();
34  }
35
36  void StayInKeywordMode() {
37    provider_ = NULL;
38  }
39 private:
40  KeywordProvider* provider_;
41};
42
43// static
44string16 KeywordProvider::SplitReplacementStringFromInput(
45    const string16& input,
46    bool trim_leading_whitespace) {
47  // The input may contain leading whitespace, strip it.
48  string16 trimmed_input;
49  TrimWhitespace(input, TRIM_LEADING, &trimmed_input);
50
51  // And extract the replacement string.
52  string16 remaining_input;
53  SplitKeywordFromInput(trimmed_input, trim_leading_whitespace,
54                        &remaining_input);
55  return remaining_input;
56}
57
58KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile)
59    : AutocompleteProvider(listener, profile, "Keyword"),
60      model_(NULL),
61      current_input_id_(0) {
62  // Extension suggestions always come from the original profile, since that's
63  // where extensions run. We use the input ID to distinguish whether the
64  // suggestions are meant for us.
65  registrar_.Add(this, NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY,
66                 Source<Profile>(profile->GetOriginalProfile()));
67  registrar_.Add(this,
68                 NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED,
69                 Source<Profile>(profile->GetOriginalProfile()));
70  registrar_.Add(this, NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED,
71                 Source<Profile>(profile));
72}
73
74KeywordProvider::KeywordProvider(ACProviderListener* listener,
75                                 TemplateURLModel* model)
76    : AutocompleteProvider(listener, NULL, "Keyword"),
77      model_(model),
78      current_input_id_(0) {
79}
80
81
82namespace {
83
84// Helper functor for Start(), for sorting keyword matches by quality.
85class CompareQuality {
86 public:
87  // A keyword is of higher quality when a greater fraction of it has been
88  // typed, that is, when it is shorter.
89  //
90  // TODO(pkasting): http://b/740691 Most recent and most frequent keywords are
91  // probably better rankings than the fraction of the keyword typed.  We should
92  // always put any exact matches first no matter what, since the code in
93  // Start() assumes this (and it makes sense).
94  bool operator()(const string16& keyword1,
95                  const string16& keyword2) const {
96    return keyword1.length() < keyword2.length();
97  }
98};
99
100// We need our input IDs to be unique across all profiles, so we keep a global
101// UID that each provider uses.
102static int global_input_uid_;
103
104}  // namespace
105
106// static
107const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput(
108    Profile* profile,
109    const AutocompleteInput& input,
110    string16* remaining_input) {
111  if (!input.allow_exact_keyword_match())
112    return NULL;
113
114  string16 keyword;
115  if (!ExtractKeywordFromInput(input, &keyword, remaining_input))
116    return NULL;
117
118  // Make sure the model is loaded. This is cheap and quickly bails out if
119  // the model is already loaded.
120  TemplateURLModel* model = profile->GetTemplateURLModel();
121  DCHECK(model);
122  model->Load();
123
124  const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword);
125  return TemplateURL::SupportsReplacement(template_url) ? template_url : NULL;
126}
127
128void KeywordProvider::Start(const AutocompleteInput& input,
129                            bool minimal_changes) {
130  // This object ensures we end keyword mode if we exit the function without
131  // toggling keyword mode to on.
132  ScopedEndExtensionKeywordMode keyword_mode_toggle(this);
133
134  matches_.clear();
135
136  if (!minimal_changes) {
137    done_ = true;
138
139    // Input has changed. Increment the input ID so that we can discard any
140    // stale extension suggestions that may be incoming.
141    current_input_id_ = ++global_input_uid_;
142  }
143
144  // Split user input into a keyword and some query input.
145  //
146  // We want to suggest keywords even when users have started typing URLs, on
147  // the assumption that they might not realize they no longer need to go to a
148  // site to be able to search it.  So we call CleanUserInputKeyword() to strip
149  // any initial scheme and/or "www.".  NOTE: Any heuristics or UI used to
150  // automatically/manually create keywords will need to be in sync with
151  // whatever we do here!
152  //
153  // TODO(pkasting): http://b/1112681 If someday we remember usage frequency for
154  // keywords, we might suggest keywords that haven't even been partially typed,
155  // if the user uses them enough and isn't obviously typing something else.  In
156  // this case we'd consider all input here to be query input.
157  string16 keyword, remaining_input;
158  if (!ExtractKeywordFromInput(input, &keyword, &remaining_input))
159    return;
160
161  // Make sure the model is loaded. This is cheap and quickly bails out if
162  // the model is already loaded.
163  TemplateURLModel* model = profile_ ? profile_->GetTemplateURLModel() : model_;
164  DCHECK(model);
165  model->Load();
166
167  // Get the best matches for this keyword.
168  //
169  // NOTE: We could cache the previous keywords and reuse them here in the
170  // |minimal_changes| case, but since we'd still have to recalculate their
171  // relevances and we can just recreate the results synchronously anyway, we
172  // don't bother.
173  //
174  // TODO(pkasting): http://b/893701 We should remember the user's use of a
175  // search query both from the autocomplete popup and from web pages
176  // themselves.
177  std::vector<string16> keyword_matches;
178  model->FindMatchingKeywords(keyword,
179                              !remaining_input.empty(),
180                              &keyword_matches);
181
182  // Prune any extension keywords that are disallowed in incognito mode (if
183  // we're incognito), or disabled.
184  for (std::vector<string16>::iterator i(keyword_matches.begin());
185       i != keyword_matches.end(); ) {
186    const TemplateURL* template_url(model->GetTemplateURLForKeyword(*i));
187    if (profile_ &&
188        !input.synchronous_only() && template_url->IsExtensionKeyword()) {
189      ExtensionService* service = profile_->GetExtensionService();
190      const Extension* extension = service->GetExtensionById(
191          template_url->GetExtensionId(), false);
192      bool enabled = extension && (!profile_->IsOffTheRecord() ||
193                                   service->IsIncognitoEnabled(extension));
194      if (!enabled) {
195        i = keyword_matches.erase(i);
196        continue;
197      }
198    }
199    ++i;
200  }
201  if (keyword_matches.empty())
202    return;
203  std::sort(keyword_matches.begin(), keyword_matches.end(), CompareQuality());
204
205  // Limit to one exact or three inexact matches, and mark them up for display
206  // in the autocomplete popup.
207  // Any exact match is going to be the highest quality match, and thus at the
208  // front of our vector.
209  if (keyword_matches.front() == keyword) {
210    const TemplateURL* template_url(model->GetTemplateURLForKeyword(keyword));
211    // TODO(pkasting): We should probably check that if the user explicitly
212    // typed a scheme, that scheme matches the one in |template_url|.
213    matches_.push_back(CreateAutocompleteMatch(model, keyword, input,
214                                               keyword.length(),
215                                               remaining_input, -1));
216
217    if (profile_ &&
218        !input.synchronous_only() && template_url->IsExtensionKeyword()) {
219      if (template_url->GetExtensionId() != current_keyword_extension_id_)
220        MaybeEndExtensionKeywordMode();
221      if (current_keyword_extension_id_.empty())
222        EnterExtensionKeywordMode(template_url->GetExtensionId());
223      keyword_mode_toggle.StayInKeywordMode();
224
225      ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url,
226                                                remaining_input,
227                                                &matches_[0]);
228
229      if (minimal_changes) {
230        // If the input hasn't significantly changed, we can just use the
231        // suggestions from last time. We need to readjust the relevance to
232        // ensure it is less than the main match's relevance.
233        for (size_t i = 0; i < extension_suggest_matches_.size(); ++i) {
234          matches_.push_back(extension_suggest_matches_[i]);
235          matches_.back().relevance = matches_[0].relevance - (i + 1);
236        }
237      } else {
238        extension_suggest_last_input_ = input;
239        extension_suggest_matches_.clear();
240
241        bool have_listeners = ExtensionOmniboxEventRouter::OnInputChanged(
242            profile_, template_url->GetExtensionId(),
243            UTF16ToUTF8(remaining_input), current_input_id_);
244
245        // We only have to wait for suggest results if there are actually
246        // extensions listening for input changes.
247        if (have_listeners)
248          done_ = false;
249      }
250    }
251  } else {
252    if (keyword_matches.size() > kMaxMatches) {
253      keyword_matches.erase(keyword_matches.begin() + kMaxMatches,
254                            keyword_matches.end());
255    }
256    for (std::vector<string16>::const_iterator i(keyword_matches.begin());
257         i != keyword_matches.end(); ++i) {
258      matches_.push_back(CreateAutocompleteMatch(model, *i,
259                                                 input, keyword.length(),
260                                                 remaining_input, -1));
261    }
262  }
263}
264
265void KeywordProvider::Stop() {
266  done_ = true;
267  MaybeEndExtensionKeywordMode();
268}
269
270KeywordProvider::~KeywordProvider() {}
271
272// static
273bool KeywordProvider::ExtractKeywordFromInput(const AutocompleteInput& input,
274                                              string16* keyword,
275                                              string16* remaining_input) {
276  if ((input.type() == AutocompleteInput::INVALID) ||
277      (input.type() == AutocompleteInput::FORCED_QUERY))
278    return false;
279
280  *keyword = TemplateURLModel::CleanUserInputKeyword(
281      SplitKeywordFromInput(input.text(), true, remaining_input));
282  return !keyword->empty();
283}
284
285// static
286string16 KeywordProvider::SplitKeywordFromInput(
287    const string16& input,
288    bool trim_leading_whitespace,
289    string16* remaining_input) {
290  // Find end of first token.  The AutocompleteController has trimmed leading
291  // whitespace, so we need not skip over that.
292  const size_t first_white(input.find_first_of(kWhitespaceUTF16));
293  DCHECK_NE(0U, first_white);
294  if (first_white == string16::npos)
295    return input;  // Only one token provided.
296
297  // Set |remaining_input| to everything after the first token.
298  DCHECK(remaining_input != NULL);
299  const size_t remaining_start = trim_leading_whitespace ?
300    input.find_first_not_of(kWhitespaceUTF16, first_white) : first_white + 1;
301
302  if (remaining_start < input.length())
303    remaining_input->assign(input.begin() + remaining_start, input.end());
304
305  // Return first token as keyword.
306  return input.substr(0, first_white);
307}
308
309// static
310void KeywordProvider::FillInURLAndContents(
311    const string16& remaining_input,
312    const TemplateURL* element,
313    AutocompleteMatch* match) {
314  DCHECK(!element->short_name().empty());
315  DCHECK(element->url());
316  DCHECK(element->url()->IsValid());
317  int message_id = element->IsExtensionKeyword() ?
318      IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH;
319  if (remaining_input.empty()) {
320    // Allow extension keyword providers to accept empty string input. This is
321    // useful to allow extensions to do something in the case where no input is
322    // entered.
323    if (element->url()->SupportsReplacement() &&
324        !element->IsExtensionKeyword()) {
325      // No query input; return a generic, no-destination placeholder.
326      match->contents.assign(
327          l10n_util::GetStringFUTF16(message_id,
328              element->AdjustedShortNameForLocaleDirection(),
329              l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)));
330      match->contents_class.push_back(
331          ACMatchClassification(0, ACMatchClassification::DIM));
332    } else {
333      // Keyword that has no replacement text (aka a shorthand for a URL).
334      match->destination_url = GURL(element->url()->url());
335      match->contents.assign(element->short_name());
336      AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(),
337          match->contents.length(), ACMatchClassification::NONE,
338          &match->contents_class);
339    }
340  } else {
341    // Create destination URL by escaping user input and substituting into
342    // keyword template URL.  The escaping here handles whitespace in user
343    // input, but we rely on later canonicalization functions to do more
344    // fixup to make the URL valid if necessary.
345    DCHECK(element->url()->SupportsReplacement());
346    match->destination_url = GURL(element->url()->ReplaceSearchTerms(
347        *element, remaining_input,
348        TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()));
349    std::vector<size_t> content_param_offsets;
350    match->contents.assign(l10n_util::GetStringFUTF16(message_id,
351                                                      element->short_name(),
352                                                      remaining_input,
353                                                      &content_param_offsets));
354    if (content_param_offsets.size() == 2) {
355      AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1],
356          remaining_input.length(), match->contents.length(),
357          ACMatchClassification::NONE, &match->contents_class);
358    } else {
359      // See comments on an identical NOTREACHED() in search_provider.cc.
360      NOTREACHED();
361    }
362  }
363}
364
365// static
366int KeywordProvider::CalculateRelevance(AutocompleteInput::Type type,
367                                        bool complete,
368                                        bool supports_replacement,
369                                        bool prefer_keyword,
370                                        bool allow_exact_keyword_match) {
371  if (!complete)
372    return (type == AutocompleteInput::URL) ? 700 : 450;
373  if (!supports_replacement || (allow_exact_keyword_match && prefer_keyword))
374    return 1500;
375  return (allow_exact_keyword_match && (type == AutocompleteInput::QUERY)) ?
376      1450 : 1100;
377}
378
379AutocompleteMatch KeywordProvider::CreateAutocompleteMatch(
380    TemplateURLModel* model,
381    const string16& keyword,
382    const AutocompleteInput& input,
383    size_t prefix_length,
384    const string16& remaining_input,
385    int relevance) {
386  DCHECK(model);
387  // Get keyword data from data store.
388  const TemplateURL* element(
389      model->GetTemplateURLForKeyword(keyword));
390  DCHECK(element && element->url());
391  const bool supports_replacement = element->url()->SupportsReplacement();
392
393  // Create an edit entry of "[keyword] [remaining input]".  This is helpful
394  // even when [remaining input] is empty, as the user can select the popup
395  // choice and immediately begin typing in query input.
396  const bool keyword_complete = (prefix_length == keyword.length());
397  if (relevance < 0) {
398    relevance =
399        CalculateRelevance(input.type(), keyword_complete,
400                           // When the user wants keyword matches to take
401                           // preference, score them highly regardless of
402                           // whether the input provides query text.
403                           supports_replacement, input.prefer_keyword(),
404                           input.allow_exact_keyword_match());
405  }
406  AutocompleteMatch result(this, relevance, false,
407      supports_replacement ? AutocompleteMatch::SEARCH_OTHER_ENGINE :
408                             AutocompleteMatch::HISTORY_KEYWORD);
409  result.fill_into_edit.assign(keyword);
410  if (!remaining_input.empty() || !keyword_complete || supports_replacement)
411    result.fill_into_edit.push_back(L' ');
412  result.fill_into_edit.append(remaining_input);
413  // If we wanted to set |result.inline_autocomplete_offset| correctly, we'd
414  // need CleanUserInputKeyword() to return the amount of adjustment it's made
415  // to the user's input.  Because right now inexact keyword matches can't score
416  // more highly than a "what you typed" match from one of the other providers,
417  // we just don't bother to do this, and leave inline autocompletion off.
418  result.inline_autocomplete_offset = string16::npos;
419
420  // Create destination URL and popup entry content by substituting user input
421  // into keyword templates.
422  FillInURLAndContents(remaining_input, element, &result);
423
424  if (supports_replacement)
425    result.template_url = element;
426  result.transition = PageTransition::KEYWORD;
427
428  // Create popup entry description based on the keyword name.
429  if (!element->IsExtensionKeyword()) {
430    result.description.assign(l10n_util::GetStringFUTF16(
431        IDS_AUTOCOMPLETE_KEYWORD_DESCRIPTION, keyword));
432    string16 keyword_desc(
433        l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_KEYWORD_DESCRIPTION));
434    AutocompleteMatch::ClassifyLocationInString(
435        keyword_desc.find(ASCIIToUTF16("%s")),
436        prefix_length,
437        result.description.length(),
438        ACMatchClassification::DIM,
439        &result.description_class);
440  }
441
442  return result;
443}
444
445void KeywordProvider::Observe(NotificationType type,
446                              const NotificationSource& source,
447                              const NotificationDetails& details) {
448  TemplateURLModel* model = profile_ ? profile_->GetTemplateURLModel() : model_;
449  const AutocompleteInput& input = extension_suggest_last_input_;
450
451  switch (type.value) {
452    case NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED:
453      // Input has been accepted, so we're done with this input session. Ensure
454      // we don't send the OnInputCancelled event.
455      current_keyword_extension_id_.clear();
456      return;
457
458    case NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: {
459      // It's possible to change the default suggestion while not in an editing
460      // session.
461      string16 keyword, remaining_input;
462      if (matches_.empty() || current_keyword_extension_id_.empty() ||
463          !ExtractKeywordFromInput(input, &keyword, &remaining_input))
464        return;
465
466      const TemplateURL* template_url(
467          model->GetTemplateURLForKeyword(keyword));
468      ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url,
469                                                remaining_input,
470                                                &matches_[0]);
471      listener_->OnProviderUpdate(true);
472      return;
473    }
474
475    case NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY: {
476      const ExtensionOmniboxSuggestions& suggestions =
477        *Details<ExtensionOmniboxSuggestions>(details).ptr();
478      if (suggestions.request_id != current_input_id_)
479        return;  // This is an old result. Just ignore.
480
481      string16 keyword, remaining_input;
482      if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) {
483        NOTREACHED();
484        return;
485      }
486
487      // TODO(mpcomplete): consider clamping the number of suggestions to
488      // AutocompleteProvider::kMaxMatches.
489      for (size_t i = 0; i < suggestions.suggestions.size(); ++i) {
490        const ExtensionOmniboxSuggestion& suggestion =
491            suggestions.suggestions[i];
492        // We want to order these suggestions in descending order, so start with
493        // the relevance of the first result (added synchronously in Start()),
494        // and subtract 1 for each subsequent suggestion from the extension.
495        // We know that |complete| is true, because we wouldn't get results from
496        // the extension unless the full keyword had been typed.
497        int first_relevance = CalculateRelevance(input.type(), true, true,
498            input.prefer_keyword(), input.allow_exact_keyword_match());
499        extension_suggest_matches_.push_back(CreateAutocompleteMatch(
500            model, keyword, input, keyword.length(),
501            suggestion.content, first_relevance - (i + 1)));
502
503        AutocompleteMatch* match = &extension_suggest_matches_.back();
504        match->contents.assign(suggestion.description);
505        match->contents_class = suggestion.description_styles;
506        match->description.clear();
507        match->description_class.clear();
508      }
509
510      done_ = true;
511      matches_.insert(matches_.end(), extension_suggest_matches_.begin(),
512                      extension_suggest_matches_.end());
513      listener_->OnProviderUpdate(!extension_suggest_matches_.empty());
514      return;
515    }
516
517    default:
518      NOTREACHED();
519      return;
520  }
521}
522
523void KeywordProvider::EnterExtensionKeywordMode(
524    const std::string& extension_id) {
525  DCHECK(current_keyword_extension_id_.empty());
526  current_keyword_extension_id_ = extension_id;
527
528  ExtensionOmniboxEventRouter::OnInputStarted(
529      profile_, current_keyword_extension_id_);
530}
531
532void KeywordProvider::MaybeEndExtensionKeywordMode() {
533  if (!current_keyword_extension_id_.empty()) {
534    ExtensionOmniboxEventRouter::OnInputCancelled(
535        profile_, current_keyword_extension_id_);
536
537    current_keyword_extension_id_.clear();
538  }
539}
540