OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ | 5 #ifndef COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ |
6 #define COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ | 6 #define COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 13 matching lines...) Expand all Loading... | |
24 public ShortcutsBackend::ShortcutsBackendObserver { | 24 public ShortcutsBackend::ShortcutsBackendObserver { |
25 public: | 25 public: |
26 explicit ShortcutsProvider(AutocompleteProviderClient* client); | 26 explicit ShortcutsProvider(AutocompleteProviderClient* client); |
27 | 27 |
28 // Performs the autocompletion synchronously. Since no asynch completion is | 28 // Performs the autocompletion synchronously. Since no asynch completion is |
29 // performed |minimal_changes| is ignored. | 29 // performed |minimal_changes| is ignored. |
30 void Start(const AutocompleteInput& input, bool minimal_changes) override; | 30 void Start(const AutocompleteInput& input, bool minimal_changes) override; |
31 | 31 |
32 void DeleteMatch(const AutocompleteMatch& match) override; | 32 void DeleteMatch(const AutocompleteMatch& match) override; |
33 | 33 |
34 // ShortcutMatch holds sufficient information about a single match from the | |
35 // shortcut database to allow for destination deduping and relevance sorting. | |
36 // After those stages the top matches are converted to the more heavyweight | |
37 // AutocompleteMatch struct. Avoiding constructing the larger struct for | |
38 // every such match can save significant time when there are many shortcut | |
39 // matches to process. | |
40 struct ShortcutMatch { | |
Peter Kasting
2016/04/14 23:52:33
This struct doesn't need to access any members of
Alexander Yashkin
2016/04/15 09:14:15
I moved ShortcutMatch structure and SortAndDedupMa
| |
41 ShortcutMatch(int relevance, | |
42 const GURL& stripped_destination_url, | |
43 const ShortcutsDatabase::Shortcut* shortcut); | |
44 int relevance; | |
45 GURL stripped_destination_url; | |
46 const ShortcutsDatabase::Shortcut* shortcut; | |
47 base::string16 contents; | |
48 AutocompleteMatch::Type type; | |
49 }; | |
50 | |
34 private: | 51 private: |
35 friend class ClassifyTest; | 52 friend class ClassifyTest; |
36 friend class ShortcutsProviderExtensionTest; | 53 friend class ShortcutsProviderExtensionTest; |
37 friend class ShortcutsProviderTest; | 54 friend class ShortcutsProviderTest; |
38 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, CalculateScore); | 55 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, CalculateScore); |
56 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, SortAndDedupMatches); | |
Peter Kasting
2016/04/14 23:52:34
Nit: If this is only needed because of the call to
Alexander Yashkin
2016/04/15 09:14:15
Removed.
| |
39 | 57 |
40 typedef std::multimap<base::char16, base::string16> WordMap; | 58 typedef std::multimap<base::char16, base::string16> WordMap; |
41 | 59 |
42 ~ShortcutsProvider() override; | 60 ~ShortcutsProvider() override; |
43 | 61 |
44 // ShortcutsBackendObserver: | |
45 void OnShortcutsLoaded() override; | |
46 | |
47 // Performs the autocomplete matching and scoring. | |
48 void GetMatches(const AutocompleteInput& input); | |
49 | |
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); | |
60 | |
61 // Returns a map mapping characters to groups of words from |text| that start | 62 // Returns a map mapping characters to groups of words from |text| that start |
62 // with those characters, ordered lexicographically descending so that longer | 63 // with those characters, ordered lexicographically descending so that longer |
63 // words appear before their prefixes (if any) within a particular | 64 // words appear before their prefixes (if any) within a particular |
64 // equal_range(). | 65 // equal_range(). |
65 static WordMap CreateWordMapForString(const base::string16& text); | 66 static WordMap CreateWordMapForString(const base::string16& text); |
66 | 67 |
67 // Given |text| and a corresponding base set of classifications | 68 // Given |text| and a corresponding base set of classifications |
68 // |original_class|, adds ACMatchClassification::MATCH markers for all | 69 // |original_class|, adds ACMatchClassification::MATCH markers for all |
69 // instances of the words from |find_words| within |text| and returns the | 70 // instances of the words from |find_words| within |text| and returns the |
70 // resulting classifications. (|find_text| is provided as the original string | 71 // resulting classifications. (|find_text| is provided as the original string |
(...skipping 12 matching lines...) Expand all Loading... | |
83 // |find_words| should be as constructed by CreateWordMapForString(find_text). | 84 // |find_words| should be as constructed by CreateWordMapForString(find_text). |
84 // | 85 // |
85 // |find_text| (and thus |find_words|) are expected to be lowercase. |text| | 86 // |find_text| (and thus |find_words|) are expected to be lowercase. |text| |
86 // will be lowercased in this function. | 87 // will be lowercased in this function. |
87 static ACMatchClassifications ClassifyAllMatchesInString( | 88 static ACMatchClassifications ClassifyAllMatchesInString( |
88 const base::string16& find_text, | 89 const base::string16& find_text, |
89 const WordMap& find_words, | 90 const WordMap& find_words, |
90 const base::string16& text, | 91 const base::string16& text, |
91 const ACMatchClassifications& original_class); | 92 const ACMatchClassifications& original_class); |
92 | 93 |
94 // Sorts |matches| by destination, taking into account demotions based on | |
95 // |page_classification| when resolving ties about which of several | |
96 // duplicates to keep. The matches are also deduplicated. | |
97 static void SortAndDedupMatches( | |
98 metrics::OmniboxEventProto::PageClassification page_classification, | |
99 std::vector<ShortcutMatch>* matches); | |
100 | |
101 // ShortcutsBackendObserver: | |
102 void OnShortcutsLoaded() override; | |
103 | |
104 // Performs the autocomplete matching and scoring. | |
105 void GetMatches(const AutocompleteInput& input); | |
106 | |
107 // Returns an AutocompleteMatch corresponding to |shortcut_match|. Assigns it | |
108 // |relevance| score in the process, and highlights the description and | |
109 // contents against |input|, which should be the lower-cased version of | |
110 // the user's input. |input| and |fixed_up_input_text| are used to decide | |
111 // what can be inlined. | |
112 AutocompleteMatch ShortcutMatchToACMatch( | |
113 const ShortcutMatch& shortcut_match, | |
114 const AutocompleteInput& input, | |
115 const base::string16& fixed_up_input_text, | |
116 const base::string16 term_string, | |
117 const WordMap& terms_map); | |
118 | |
93 // Returns iterator to first item in |shortcuts_map_| matching |keyword|. | 119 // Returns iterator to first item in |shortcuts_map_| matching |keyword|. |
94 // Returns shortcuts_map_.end() if there are no matches. | 120 // Returns shortcuts_map_.end() if there are no matches. |
95 ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch( | 121 ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch( |
96 const base::string16& keyword, | 122 const base::string16& keyword, |
97 ShortcutsBackend* backend); | 123 ShortcutsBackend* backend); |
98 | 124 |
99 int CalculateScore(const base::string16& terms, | 125 int CalculateScore(const base::string16& terms, |
100 const ShortcutsDatabase::Shortcut& shortcut, | 126 const ShortcutsDatabase::Shortcut& shortcut, |
101 int max_relevance); | 127 int max_relevance); |
102 | 128 |
103 // The default max relevance unless overridden by a field trial. | 129 // The default max relevance unless overridden by a field trial. |
104 static const int kShortcutsProviderDefaultMaxRelevance; | 130 static const int kShortcutsProviderDefaultMaxRelevance; |
105 | 131 |
106 AutocompleteProviderClient* client_; | 132 AutocompleteProviderClient* client_; |
107 std::string languages_; | 133 std::string languages_; |
108 bool initialized_; | 134 bool initialized_; |
109 }; | 135 }; |
110 | 136 |
111 #endif // COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ | 137 #endif // COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ |
OLD | NEW |