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 COMPONENTS_OMNIBOX_SHORTCUTS_PROVIDER_H_
6 #define COMPONENTS_OMNIBOX_SHORTCUTS_PROVIDER_H_
12 #include "base/gtest_prod_util.h"
13 #include "components/omnibox/autocomplete_provider.h"
14 #include "components/omnibox/shortcuts_backend.h"
16 class AutocompleteProviderClient
;
17 class ShortcutsProviderTest
;
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.
23 class ShortcutsProvider
: public AutocompleteProvider
,
24 public ShortcutsBackend::ShortcutsBackendObserver
{
26 explicit ShortcutsProvider(AutocompleteProviderClient
* client
);
28 // Performs the autocompletion synchronously. Since no asynch completion is
29 // performed |minimal_changes| is ignored.
30 void Start(const AutocompleteInput
& input
, bool minimal_changes
) override
;
32 void DeleteMatch(const AutocompleteMatch
& match
) override
;
35 friend class ClassifyTest
;
36 friend class ShortcutsProviderTest
;
37 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest
, CalculateScore
);
39 typedef std::multimap
<base::char16
, base::string16
> WordMap
;
41 ~ShortcutsProvider() override
;
43 // ShortcutsBackendObserver:
44 void OnShortcutsLoaded() override
;
46 // Performs the autocomplete matching and scoring.
47 void GetMatches(const AutocompleteInput
& input
);
49 // Returns an AutocompleteMatch corresponding to |shortcut|. Assigns it
50 // |relevance| score in the process, and highlights the description and
51 // contents against |input|, which should be the lower-cased version of
52 // the user's input. |input| and |fixed_up_input_text| are used to decide
53 // what can be inlined.
54 AutocompleteMatch
ShortcutToACMatch(
55 const ShortcutsDatabase::Shortcut
& shortcut
,
57 const AutocompleteInput
& input
,
58 const base::string16
& fixed_up_input_text
);
60 // Returns a map mapping characters to groups of words from |text| that start
61 // with those characters, ordered lexicographically descending so that longer
62 // words appear before their prefixes (if any) within a particular
64 static WordMap
CreateWordMapForString(const base::string16
& text
);
66 // Given |text| and a corresponding base set of classifications
67 // |original_class|, adds ACMatchClassification::MATCH markers for all
68 // instances of the words from |find_words| within |text| and returns the
69 // resulting classifications. (|find_text| is provided as the original string
70 // used to create |find_words|. This is supplied because it's common for this
71 // to be a prefix of |text|, so we can quickly check for that and mark that
72 // entire substring as a match before proceeding with the more generic
75 // For example, given the |text|
76 // "Sports and News at sports.somesite.com - visit us!" and |original_class|
77 // {{0, NONE}, {18, URL}, {37, NONE}} (marking "sports.somesite.com" as a
78 // URL), calling with |find_text| set to "sp ew" would return
79 // {{0, MATCH}, {2, NONE}, {12, MATCH}, {14, NONE}, {18, URL|MATCH},
80 // {20, URL}, {37, NONE}}.
82 // |find_words| should be as constructed by CreateWordMapForString(find_text).
84 // |find_text| (and thus |find_words|) are expected to be lowercase. |text|
85 // will be lowercased in this function.
86 static ACMatchClassifications
ClassifyAllMatchesInString(
87 const base::string16
& find_text
,
88 const WordMap
& find_words
,
89 const base::string16
& text
,
90 const ACMatchClassifications
& original_class
);
92 // Returns iterator to first item in |shortcuts_map_| matching |keyword|.
93 // Returns shortcuts_map_.end() if there are no matches.
94 ShortcutsBackend::ShortcutMap::const_iterator
FindFirstMatch(
95 const base::string16
& keyword
,
96 ShortcutsBackend
* backend
);
98 int CalculateScore(const base::string16
& terms
,
99 const ShortcutsDatabase::Shortcut
& shortcut
,
102 // The default max relevance unless overridden by a field trial.
103 static const int kShortcutsProviderDefaultMaxRelevance
;
105 AutocompleteProviderClient
* client_
;
106 std::string languages_
;
110 #endif // COMPONENTS_OMNIBOX_SHORTCUTS_PROVIDER_H_