1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_
6#define CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_
7
8#include <map>
9#include <set>
10#include <string>
11
12#include "base/gtest_prod_util.h"
13#include "chrome/browser/autocomplete/shortcuts_backend.h"
14#include "components/omnibox/autocomplete_provider.h"
15
16class Profile;
17class ShortcutsProviderTest;
18
19// Provider of recently autocompleted links. Provides autocomplete suggestions
20// from previously selected suggestions. The more often a user selects a
21// suggestion for a given search term the higher will be that suggestion's
22// ranking for future uses of that search term.
23class ShortcutsProvider
24    : public AutocompleteProvider,
25      public ShortcutsBackend::ShortcutsBackendObserver {
26 public:
27  explicit ShortcutsProvider(Profile* profile);
28
29  // Performs the autocompletion synchronously. Since no asynch completion is
30  // performed |minimal_changes| is ignored.
31  virtual void Start(const AutocompleteInput& input,
32                     bool minimal_changes) OVERRIDE;
33
34  virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE;
35
36 private:
37  friend class ClassifyTest;
38  friend class ShortcutsProviderTest;
39  FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, CalculateScore);
40
41  typedef std::multimap<base::char16, base::string16> WordMap;
42
43  virtual ~ShortcutsProvider();
44
45  // ShortcutsBackendObserver:
46  virtual void OnShortcutsLoaded() OVERRIDE;
47
48  // Performs the autocomplete matching and scoring.
49  void GetMatches(const AutocompleteInput& input);
50
51  // Returns an AutocompleteMatch corresponding to |shortcut|. Assigns it
52  // |relevance| score in the process, and highlights the description and
53  // contents against |input|, which should be the lower-cased version of
54  // the user's input. |input| and |fixed_up_input_text| are used to decide
55  // what can be inlined.
56  AutocompleteMatch ShortcutToACMatch(
57      const history::ShortcutsDatabase::Shortcut& shortcut,
58      int relevance,
59      const AutocompleteInput& input,
60      const base::string16& fixed_up_input_text);
61
62  // Returns a map mapping characters to groups of words from |text| that start
63  // with those characters, ordered lexicographically descending so that longer
64  // words appear before their prefixes (if any) within a particular
65  // equal_range().
66  static WordMap CreateWordMapForString(const base::string16& text);
67
68  // Given |text| and a corresponding base set of classifications
69  // |original_class|, adds ACMatchClassification::MATCH markers for all
70  // instances of the words from |find_words| within |text| and returns the
71  // resulting classifications.  (|find_text| is provided as the original string
72  // used to create |find_words|.  This is supplied because it's common for this
73  // to be a prefix of |text|, so we can quickly check for that and mark that
74  // entire substring as a match before proceeding with the more generic
75  // algorithm.)
76  //
77  // For example, given the |text|
78  // "Sports and News at sports.somesite.com - visit us!" and |original_class|
79  // {{0, NONE}, {18, URL}, {37, NONE}} (marking "sports.somesite.com" as a
80  // URL), calling with |find_text| set to "sp ew" would return
81  // {{0, MATCH}, {2, NONE}, {12, MATCH}, {14, NONE}, {18, URL|MATCH},
82  // {20, URL}, {37, NONE}}.
83  //
84  // |find_words| should be as constructed by CreateWordMapForString(find_text).
85  //
86  // |find_text| (and thus |find_words|) are expected to be lowercase.  |text|
87  // will be lowercased in this function.
88  static ACMatchClassifications ClassifyAllMatchesInString(
89      const base::string16& find_text,
90      const WordMap& find_words,
91      const base::string16& text,
92      const ACMatchClassifications& original_class);
93
94  // Returns iterator to first item in |shortcuts_map_| matching |keyword|.
95  // Returns shortcuts_map_.end() if there are no matches.
96  ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch(
97      const base::string16& keyword,
98      ShortcutsBackend* backend);
99
100  int CalculateScore(const base::string16& terms,
101                     const history::ShortcutsDatabase::Shortcut& shortcut,
102                     int max_relevance);
103
104  // The default max relevance unless overridden by a field trial.
105  static const int kShortcutsProviderDefaultMaxRelevance;
106
107  Profile* profile_;
108  std::string languages_;
109  bool initialized_;
110};
111
112#endif  // CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_
113