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