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.
5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_
6 #define CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_
12 #include "base/gtest_prod_util.h"
13 #include "chrome/browser/autocomplete/autocomplete_provider.h"
14 #include "chrome/browser/history/shortcuts_backend.h"
19 class ShortcutsProviderTest;
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.
26 class ShortcutsProvider
27 : public AutocompleteProvider,
28 public history::ShortcutsBackend::ShortcutsBackendObserver {
30 ShortcutsProvider(AutocompleteProviderListener* listener, Profile* profile);
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;
37 virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE;
40 friend class ClassifyTest;
41 friend class history::ShortcutsProviderTest;
43 typedef std::multimap<char16, string16> WordMap;
45 virtual ~ShortcutsProvider();
47 // ShortcutsBackendObserver:
48 virtual void OnShortcutsLoaded() OVERRIDE;
50 // Performs the autocomplete matching and scoring.
51 void GetMatches(const AutocompleteInput& input);
53 AutocompleteMatch ShortcutToACMatch(
55 const string16& terms,
56 const history::ShortcutsBackend::Shortcut& shortcut);
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
62 static WordMap CreateWordMapForString(const string16& text);
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
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}}.
80 // |find_words| should be as constructed by CreateWordMapForString(find_text).
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,
88 const ACMatchClassifications& original_class);
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);
97 const string16& terms,
98 const history::ShortcutsBackend::Shortcut& shortcut,
101 std::string languages_;
105 #endif // CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_