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> |
11 | 11 |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "components/omnibox/browser/autocomplete_provider.h" | 13 #include "components/omnibox/browser/autocomplete_provider.h" |
14 #include "components/omnibox/browser/shortcuts_backend.h" | 14 #include "components/omnibox/browser/shortcuts_backend.h" |
15 | 15 |
16 class AutocompleteProviderClient; | 16 class AutocompleteProviderClient; |
17 class ShortcutsProviderTest; | 17 class ShortcutsProviderTest; |
18 struct ShortcutMatch; | |
18 | 19 |
19 // Provider of recently autocompleted links. Provides autocomplete suggestions | 20 // Provider of recently autocompleted links. Provides autocomplete suggestions |
20 // from previously selected suggestions. The more often a user selects a | 21 // 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 // suggestion for a given search term the higher will be that suggestion's |
22 // ranking for future uses of that search term. | 23 // ranking for future uses of that search term. |
23 class ShortcutsProvider : public AutocompleteProvider, | 24 class ShortcutsProvider : public AutocompleteProvider, |
24 public ShortcutsBackend::ShortcutsBackendObserver { | 25 public ShortcutsBackend::ShortcutsBackendObserver { |
25 public: | 26 public: |
26 explicit ShortcutsProvider(AutocompleteProviderClient* client); | 27 explicit ShortcutsProvider(AutocompleteProviderClient* client); |
27 | 28 |
28 // Performs the autocompletion synchronously. Since no asynch completion is | 29 // Performs the autocompletion synchronously. Since no asynch completion is |
29 // performed |minimal_changes| is ignored. | 30 // performed |minimal_changes| is ignored. |
30 void Start(const AutocompleteInput& input, bool minimal_changes) override; | 31 void Start(const AutocompleteInput& input, bool minimal_changes) override; |
31 | 32 |
32 void DeleteMatch(const AutocompleteMatch& match) override; | 33 void DeleteMatch(const AutocompleteMatch& match) override; |
33 | 34 |
34 private: | 35 private: |
35 friend class ClassifyTest; | 36 friend class ClassifyTest; |
36 friend class ShortcutsProviderExtensionTest; | 37 friend class ShortcutsProviderExtensionTest; |
37 friend class ShortcutsProviderTest; | 38 friend class ShortcutsProviderTest; |
38 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, CalculateScore); | 39 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, CalculateScore); |
40 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, | |
41 DedupShortcutMatchesByDestination); | |
39 | 42 |
40 typedef std::multimap<base::char16, base::string16> WordMap; | 43 typedef std::multimap<base::char16, base::string16> WordMap; |
41 | 44 |
42 ~ShortcutsProvider() override; | 45 ~ShortcutsProvider() override; |
43 | 46 |
44 // ShortcutsBackendObserver: | 47 // ShortcutsBackendObserver: |
45 void OnShortcutsLoaded() override; | 48 void OnShortcutsLoaded() override; |
46 | 49 |
47 // Performs the autocomplete matching and scoring. | 50 // Performs the autocomplete matching and scoring. |
48 void GetMatches(const AutocompleteInput& input); | 51 void GetMatches(const AutocompleteInput& input); |
49 | 52 |
50 // Returns an AutocompleteMatch corresponding to |shortcut|. Assigns it | 53 // Returns an AutocompleteMatch corresponding to |shortcut|. Assigns it |
51 // |relevance| score in the process, and highlights the description and | 54 // |relevance| score in the process, and highlights the description and |
52 // contents against |input|, which should be the lower-cased version of | 55 // 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 | 56 // the user's input. |input| and |fixed_up_input_text| are used to decide |
54 // what can be inlined. | 57 // what can be inlined. |
55 AutocompleteMatch ShortcutToACMatch( | 58 AutocompleteMatch ShortcutMatchToACMatch( |
56 const ShortcutsDatabase::Shortcut& shortcut, | 59 const ShortcutMatch& shortcut_match, |
57 int relevance, | |
58 const AutocompleteInput& input, | 60 const AutocompleteInput& input, |
59 const base::string16& fixed_up_input_text); | 61 const base::string16& fixed_up_input_text, |
62 const base::string16 term_string, | |
63 const WordMap& terms_map); | |
60 | 64 |
61 // Returns a map mapping characters to groups of words from |text| that start | 65 // Returns a map mapping characters to groups of words from |text| that start |
62 // with those characters, ordered lexicographically descending so that longer | 66 // with those characters, ordered lexicographically descending so that longer |
63 // words appear before their prefixes (if any) within a particular | 67 // words appear before their prefixes (if any) within a particular |
64 // equal_range(). | 68 // equal_range(). |
65 static WordMap CreateWordMapForString(const base::string16& text); | 69 static WordMap CreateWordMapForString(const base::string16& text); |
66 | 70 |
67 // Given |text| and a corresponding base set of classifications | 71 // Given |text| and a corresponding base set of classifications |
68 // |original_class|, adds ACMatchClassification::MATCH markers for all | 72 // |original_class|, adds ACMatchClassification::MATCH markers for all |
69 // instances of the words from |find_words| within |text| and returns the | 73 // instances of the words from |find_words| within |text| and returns the |
(...skipping 23 matching lines...) Expand all Loading... | |
93 // Returns iterator to first item in |shortcuts_map_| matching |keyword|. | 97 // Returns iterator to first item in |shortcuts_map_| matching |keyword|. |
94 // Returns shortcuts_map_.end() if there are no matches. | 98 // Returns shortcuts_map_.end() if there are no matches. |
95 ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch( | 99 ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch( |
96 const base::string16& keyword, | 100 const base::string16& keyword, |
97 ShortcutsBackend* backend); | 101 ShortcutsBackend* backend); |
98 | 102 |
99 int CalculateScore(const base::string16& terms, | 103 int CalculateScore(const base::string16& terms, |
100 const ShortcutsDatabase::Shortcut& shortcut, | 104 const ShortcutsDatabase::Shortcut& shortcut, |
101 int max_relevance); | 105 int max_relevance); |
102 | 106 |
107 // Function removes duplicates from vector of ShortcutMatch objects. | |
108 // Deduplication algorithm is the same as used in | |
109 // AutocompleteResult::DedupMatchesByDestination. | |
110 // Matches with higher relevance are left from duplicates. | |
Peter Kasting
2016/04/12 23:29:50
Nit: Let's use a similar comment to what's in Auto
Alexander Yashkin
2016/04/13 09:29:36
Done, thanks.
| |
111 static void DedupShortcutMatchesByDestination( | |
112 metrics::OmniboxEventProto::PageClassification page_classification, | |
113 std::vector<ShortcutMatch>* matches); | |
114 | |
103 // The default max relevance unless overridden by a field trial. | 115 // The default max relevance unless overridden by a field trial. |
104 static const int kShortcutsProviderDefaultMaxRelevance; | 116 static const int kShortcutsProviderDefaultMaxRelevance; |
105 | 117 |
106 AutocompleteProviderClient* client_; | 118 AutocompleteProviderClient* client_; |
107 std::string languages_; | 119 std::string languages_; |
108 bool initialized_; | 120 bool initialized_; |
109 }; | 121 }; |
110 | 122 |
111 #endif // COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ | 123 #endif // COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ |
OLD | NEW |