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