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