keyword_provider.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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  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    if (profile_ &&
181        !input.synchronous_only() && template_url->IsExtensionKeyword()) {
182      // If this extension keyword is disabled, make sure we don't add any
183      // matches (including the synchronous one below).
184      ExtensionsService* service = profile_->GetExtensionsService();
185      Extension* extension = service->GetExtensionById(
186          template_url->GetExtensionId(), false);
187      bool enabled = extension && (!profile_->IsOffTheRecord() ||
188                                   service->IsIncognitoEnabled(extension));
189      if (!enabled)
190        return;
191
192      if (extension->id() != current_keyword_extension_id_)
193        MaybeEndExtensionKeywordMode();
194      if (current_keyword_extension_id_.empty())
195        EnterExtensionKeywordMode(extension->id());
196      keyword_mode_toggle.StayInKeywordMode();
197
198      if (minimal_changes) {
199        // If the input hasn't significantly changed, we can just use the
200        // suggestions from last time. We need to readjust the relevance to
201        // ensure it is less than the main match's relevance.
202        for (size_t i = 0; i < extension_suggest_matches_.size(); ++i) {
203          matches_.push_back(extension_suggest_matches_[i]);
204          matches_.back().relevance = matches_[0].relevance - (i + 1);
205        }
206      } else {
207        extension_suggest_last_input_ = input;
208        extension_suggest_matches_.clear();
209
210        bool have_listeners = ExtensionOmniboxEventRouter::OnInputChanged(
211            profile_, template_url->GetExtensionId(),
212            WideToUTF8(remaining_input), current_input_id_);
213
214        // We only have to wait for suggest results if there are actually
215        // extensions listening for input changes.
216        if (have_listeners)
217          done_ = false;
218      }
219    }
220
221    matches_.push_back(CreateAutocompleteMatch(model, keyword, input,
222                                               keyword.length(),
223                                               remaining_input, -1));
224  } else {
225    if (keyword_matches.size() > kMaxMatches) {
226      keyword_matches.erase(keyword_matches.begin() + kMaxMatches,
227                            keyword_matches.end());
228    }
229    for (std::vector<std::wstring>::const_iterator i(keyword_matches.begin());
230         i != keyword_matches.end(); ++i) {
231      matches_.push_back(CreateAutocompleteMatch(model, *i, input,
232                                                 keyword.length(),
233                                                 remaining_input, -1));
234    }
235  }
236}
237
238void KeywordProvider::Stop() {
239  done_ = true;
240  MaybeEndExtensionKeywordMode();
241}
242
243// static
244bool KeywordProvider::ExtractKeywordFromInput(const AutocompleteInput& input,
245                                              std::wstring* keyword,
246                                              std::wstring* remaining_input) {
247  if ((input.type() == AutocompleteInput::INVALID) ||
248      (input.type() == AutocompleteInput::FORCED_QUERY))
249    return false;
250
251  *keyword = TemplateURLModel::CleanUserInputKeyword(
252      SplitKeywordFromInput(input.text(), remaining_input));
253  return !keyword->empty();
254}
255
256// static
257std::wstring KeywordProvider::SplitKeywordFromInput(
258    const std::wstring& input,
259    std::wstring* remaining_input) {
260  // Find end of first token.  The AutocompleteController has trimmed leading
261  // whitespace, so we need not skip over that.
262  const size_t first_white(input.find_first_of(kWhitespaceWide));
263  DCHECK_NE(0U, first_white);
264  if (first_white == std::wstring::npos)
265    return input;  // Only one token provided.
266
267  // Set |remaining_input| to everything after the first token.
268  DCHECK(remaining_input != NULL);
269  const size_t first_nonwhite(input.find_first_not_of(kWhitespaceWide,
270                                                      first_white));
271  if (first_nonwhite != std::wstring::npos)
272    remaining_input->assign(input.begin() + first_nonwhite, input.end());
273
274  // Return first token as keyword.
275  return input.substr(0, first_white);
276}
277
278// static
279void KeywordProvider::FillInURLAndContents(
280    const std::wstring& remaining_input,
281    const TemplateURL* element,
282    AutocompleteMatch* match) {
283  DCHECK(!element->short_name().empty());
284  DCHECK(element->url());
285  DCHECK(element->url()->IsValid());
286  int message_id = element->IsExtensionKeyword() ?
287      IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH;
288  if (remaining_input.empty()) {
289    if (element->url()->SupportsReplacement()) {
290      // No query input; return a generic, no-destination placeholder.
291      match->contents.assign(l10n_util::GetStringF(message_id,
292          element->AdjustedShortNameForLocaleDirection(),
293          l10n_util::GetString(IDS_EMPTY_KEYWORD_VALUE)));
294      match->contents_class.push_back(
295          ACMatchClassification(0, ACMatchClassification::DIM));
296    } else {
297      // Keyword that has no replacement text (aka a shorthand for a URL).
298      match->destination_url = GURL(element->url()->url());
299      match->contents.assign(element->short_name());
300      AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(),
301          match->contents.length(), ACMatchClassification::NONE,
302          &match->contents_class);
303    }
304  } else {
305    // Create destination URL by escaping user input and substituting into
306    // keyword template URL.  The escaping here handles whitespace in user
307    // input, but we rely on later canonicalization functions to do more
308    // fixup to make the URL valid if necessary.
309    DCHECK(element->url()->SupportsReplacement());
310    match->destination_url = GURL(element->url()->ReplaceSearchTerms(
311      *element, remaining_input, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
312      std::wstring()));
313    std::vector<size_t> content_param_offsets;
314    match->contents.assign(l10n_util::GetStringF(message_id,
315                                                 element->short_name(),
316                                                 remaining_input,
317                                                 &content_param_offsets));
318    if (content_param_offsets.size() == 2) {
319      AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1],
320          remaining_input.length(), match->contents.length(),
321          ACMatchClassification::NONE, &match->contents_class);
322    } else {
323      // See comments on an identical NOTREACHED() in search_provider.cc.
324      NOTREACHED();
325    }
326  }
327}
328
329// static
330int KeywordProvider::CalculateRelevance(AutocompleteInput::Type type,
331                                        bool complete,
332                                        bool no_query_text_needed) {
333  if (!complete)
334    return (type == AutocompleteInput::URL) ? 700 : 450;
335  if (no_query_text_needed)
336    return 1500;
337  return (type == AutocompleteInput::QUERY) ? 1450 : 1100;
338}
339
340AutocompleteMatch KeywordProvider::CreateAutocompleteMatch(
341    TemplateURLModel* model,
342    const std::wstring keyword,
343    const AutocompleteInput& input,
344    size_t prefix_length,
345    const std::wstring& remaining_input,
346    int relevance) {
347  DCHECK(model);
348  // Get keyword data from data store.
349  const TemplateURL* element(model->GetTemplateURLForKeyword(keyword));
350  DCHECK(element && element->url());
351  const bool supports_replacement = element->url()->SupportsReplacement();
352
353  // Create an edit entry of "[keyword] [remaining input]".  This is helpful
354  // even when [remaining input] is empty, as the user can select the popup
355  // choice and immediately begin typing in query input.
356  const bool keyword_complete = (prefix_length == keyword.length());
357  if (relevance < 0) {
358    relevance =
359        CalculateRelevance(input.type(), keyword_complete,
360                           // When the user wants keyword matches to take
361                           // preference, score them highly regardless of
362                           // whether the input provides query text.
363                           input.prefer_keyword() || !supports_replacement);
364  }
365  AutocompleteMatch result(this, relevance, false,
366      supports_replacement ? AutocompleteMatch::SEARCH_OTHER_ENGINE :
367                             AutocompleteMatch::HISTORY_KEYWORD);
368  result.fill_into_edit.assign(keyword);
369  if (!remaining_input.empty() || !keyword_complete || supports_replacement)
370    result.fill_into_edit.push_back(L' ');
371  result.fill_into_edit.append(remaining_input);
372  if (!input.prevent_inline_autocomplete() &&
373      (keyword_complete || remaining_input.empty()))
374    result.inline_autocomplete_offset = input.text().length();
375
376  // Create destination URL and popup entry content by substituting user input
377  // into keyword templates.
378  FillInURLAndContents(remaining_input, element, &result);
379
380  if (supports_replacement)
381    result.template_url = element;
382  result.transition = PageTransition::KEYWORD;
383
384  // Create popup entry description based on the keyword name.
385  if (!element->IsExtensionKeyword()) {
386    result.description.assign(l10n_util::GetStringF(
387        IDS_AUTOCOMPLETE_KEYWORD_DESCRIPTION, keyword));
388    static const std::wstring kKeywordDesc(
389        l10n_util::GetString(IDS_AUTOCOMPLETE_KEYWORD_DESCRIPTION));
390    AutocompleteMatch::ClassifyLocationInString(kKeywordDesc.find(L"%s"),
391                                                prefix_length,
392                                                result.description.length(),
393                                                ACMatchClassification::DIM,
394                                                &result.description_class);
395  }
396
397  return result;
398}
399
400void KeywordProvider::Observe(NotificationType type,
401                              const NotificationSource& source,
402                              const NotificationDetails& details) {
403  if (type == NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED) {
404    // Input has been accepted, so we're done with this input session. Ensure
405    // we don't send the OnInputCancelled event.
406    current_keyword_extension_id_.clear();
407    return;
408  }
409
410  // TODO(mpcomplete): consider clamping the number of suggestions to
411  // AutocompleteProvider::kMaxMatches.
412  DCHECK(type == NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY);
413
414  const ExtensionOmniboxSuggestions& suggestions =
415      *Details<ExtensionOmniboxSuggestions>(details).ptr();
416  if (suggestions.request_id != current_input_id_)
417    return;  // This is an old result. Just ignore.
418
419  const AutocompleteInput& input = extension_suggest_last_input_;
420  std::wstring keyword, remaining_input;
421  if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) {
422    NOTREACHED();
423    return;
424  }
425
426  TemplateURLModel* model =
427      profile_ ? profile_->GetTemplateURLModel() : model_;
428
429  for (size_t i = 0; i < suggestions.suggestions.size(); ++i) {
430    const ExtensionOmniboxSuggestion& suggestion = suggestions.suggestions[i];
431    // We want to order these suggestions in descending order, so start with
432    // the relevance of the first result (added synchronously in Start()),
433    // and subtract 1 for each subsequent suggestion from the extension.
434    // We know that |complete| is true, because we wouldn't get results from
435    // the extension unless the full keyword had been typed.
436    int first_relevance =
437        CalculateRelevance(input.type(), true, input.prefer_keyword());
438    extension_suggest_matches_.push_back(CreateAutocompleteMatch(
439        model, keyword, input, keyword.length(),
440        UTF16ToWide(suggestion.content), first_relevance - (i + 1)));
441
442    AutocompleteMatch* match = &extension_suggest_matches_.back();
443    match->contents.assign(UTF16ToWide(suggestion.description));
444    match->contents_class = suggestion.description_styles;
445    match->description.clear();
446    match->description_class.clear();
447  }
448
449  done_ = true;
450  matches_.insert(matches_.end(), extension_suggest_matches_.begin(),
451                  extension_suggest_matches_.end());
452  listener_->OnProviderUpdate(!extension_suggest_matches_.empty());
453}
454
455void KeywordProvider::EnterExtensionKeywordMode(
456    const std::string& extension_id) {
457  DCHECK(current_keyword_extension_id_.empty());
458  current_keyword_extension_id_ = extension_id;
459
460  ExtensionOmniboxEventRouter::OnInputStarted(
461      profile_, current_keyword_extension_id_);
462}
463
464void KeywordProvider::MaybeEndExtensionKeywordMode() {
465  if (!current_keyword_extension_id_.empty()) {
466    ExtensionOmniboxEventRouter::OnInputCancelled(
467        profile_, current_keyword_extension_id_);
468
469    current_keyword_extension_id_.clear();
470  }
471}
472