[email protected] | 56e2654 | 2012-02-11 00:38:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 5 | #ifndef COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ |
| 6 | #define COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 7 | |
[email protected] | 645b448 | 2012-07-25 22:59:08 | [diff] [blame] | 8 | #include <map> |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "base/gtest_prod_util.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 13 | #include "components/omnibox/browser/autocomplete_provider.h" |
| 14 | #include "components/omnibox/browser/shortcuts_backend.h" |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 15 | |
blundell | fb1215ee | 2015-06-19 20:18:12 | [diff] [blame] | 16 | class AutocompleteProviderClient; |
[email protected] | 1cef2cce | 2013-10-14 20:41:57 | [diff] [blame] | 17 | class ShortcutsProviderTest; |
[email protected] | 1cef2cce | 2013-10-14 20:41:57 | [diff] [blame] | 18 | |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 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. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 23 | class ShortcutsProvider : public AutocompleteProvider, |
| 24 | public ShortcutsBackend::ShortcutsBackendObserver { |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 25 | public: |
blundell | fb1215ee | 2015-06-19 20:18:12 | [diff] [blame] | 26 | explicit ShortcutsProvider(AutocompleteProviderClient* client); |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 27 | |
| 28 | // Performs the autocompletion synchronously. Since no asynch completion is |
| 29 | // performed |minimal_changes| is ignored. |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 30 | void Start(const AutocompleteInput& input, bool minimal_changes) override; |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 31 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 32 | void DeleteMatch(const AutocompleteMatch& match) override; |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 33 | |
| 34 | private: |
[email protected] | 645b448 | 2012-07-25 22:59:08 | [diff] [blame] | 35 | friend class ClassifyTest; |
rohitrao | b617549 | 2016-02-03 16:37:55 | [diff] [blame] | 36 | friend class ShortcutsProviderExtensionTest; |
[email protected] | f9dac2c7 | 2014-03-19 00:13:43 | [diff] [blame] | 37 | friend class ShortcutsProviderTest; |
[email protected] | f13a213 | 2014-04-22 08:13:52 | [diff] [blame] | 38 | FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, CalculateScore); |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 39 | |
[email protected] | b6775d78 | 2013-12-25 20:04:53 | [diff] [blame] | 40 | typedef std::multimap<base::char16, base::string16> WordMap; |
[email protected] | 645b448 | 2012-07-25 22:59:08 | [diff] [blame] | 41 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 42 | ~ShortcutsProvider() override; |
[email protected] | 649d1c0 | 2012-04-27 02:56:21 | [diff] [blame] | 43 | |
[email protected] | 645b448 | 2012-07-25 22:59:08 | [diff] [blame] | 44 | // Returns a map mapping characters to groups of words from |text| that start |
| 45 | // with those characters, ordered lexicographically descending so that longer |
| 46 | // words appear before their prefixes (if any) within a particular |
| 47 | // equal_range(). |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 48 | static WordMap CreateWordMapForString(const base::string16& text); |
[email protected] | 645b448 | 2012-07-25 22:59:08 | [diff] [blame] | 49 | |
Gheorghe Comanici | 15b9b104 | 2017-10-13 17:38:44 | [diff] [blame] | 50 | // Finds all instances of the words from |find_words| within |text|, adds |
| 51 | // classifications to |original_class| according to the logic described below, |
| 52 | // and returns the result. |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 53 | // |
Gheorghe Comanici | 15b9b104 | 2017-10-13 17:38:44 | [diff] [blame] | 54 | // - if |text_is_search_query| is false, the function adds |
| 55 | // ACMatchClassification::MATCH markers for all such instances. |
| 56 | // |
| 57 | // For example, given the |text| |
| 58 | // "Sports and News at sports.somesite.com - visit us!" and |original_class| |
| 59 | // {{0, NONE}, {18, URL}, {37, NONE}} (marking "sports.somesite.com" as a |
| 60 | // URL), calling with |find_text| set to "sp ew" would return |
| 61 | // {{0, MATCH}, {2, NONE}, {12, MATCH}, {14, NONE}, {18, URL|MATCH}, |
| 62 | // {20, URL}, {37, NONE}}. |
| 63 | // |
| 64 | // |
| 65 | // - if |text_is_search_query| is true, applies the same logic, but uses |
| 66 | // NONE for the matching text and MATCH for the non-matching text. This is |
| 67 | // done to mimic the behavior of SearchProvider which decorates matches |
| 68 | // according to the approach used by Google Suggest. |
| 69 | // |
| 70 | // For example, given that |text| corresponds to a search query "panama |
| 71 | // canal" and |original class| is {{0, NONE}}, calling with |find_text| set |
| 72 | // to "canal" would return {{0,MATCH}, {7, NONE}}. |
| 73 | // |
| 74 | // |find_text| is provided as the original string used to create |
| 75 | // |find_words|. This is supplied because it's common for this to be a prefix |
| 76 | // of |text|, so we can quickly check for that and mark that entire substring |
| 77 | // as a match before proceeding with the more generic algorithm. |
[email protected] | 645b448 | 2012-07-25 22:59:08 | [diff] [blame] | 78 | // |
| 79 | // |find_words| should be as constructed by CreateWordMapForString(find_text). |
| 80 | // |
| 81 | // |find_text| (and thus |find_words|) are expected to be lowercase. |text| |
| 82 | // will be lowercased in this function. |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 83 | static ACMatchClassifications ClassifyAllMatchesInString( |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 84 | const base::string16& find_text, |
[email protected] | 645b448 | 2012-07-25 22:59:08 | [diff] [blame] | 85 | const WordMap& find_words, |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 86 | const base::string16& text, |
Gheorghe Comanici | 15b9b104 | 2017-10-13 17:38:44 | [diff] [blame] | 87 | const bool text_is_search_query, |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 88 | const ACMatchClassifications& original_class); |
| 89 | |
a-v-y | d3baa3e | 2016-04-27 07:24:16 | [diff] [blame] | 90 | // ShortcutsBackendObserver: |
| 91 | void OnShortcutsLoaded() override; |
| 92 | |
| 93 | // Performs the autocomplete matching and scoring. |
| 94 | void GetMatches(const AutocompleteInput& input); |
| 95 | |
| 96 | // Returns an AutocompleteMatch corresponding to |shortcut|. Assigns it |
| 97 | // |relevance| score in the process, and highlights the description and |
| 98 | // contents against |input|, which should be the lower-cased version of |
| 99 | // the user's input. |input| and |fixed_up_input_text| are used to decide |
| 100 | // what can be inlined. |
| 101 | AutocompleteMatch ShortcutToACMatch( |
| 102 | const ShortcutsDatabase::Shortcut& shortcut, |
| 103 | int relevance, |
| 104 | const AutocompleteInput& input, |
| 105 | const base::string16& fixed_up_input_text, |
| 106 | const base::string16 term_string, |
| 107 | const WordMap& terms_map); |
| 108 | |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 109 | // Returns iterator to first item in |shortcuts_map_| matching |keyword|. |
| 110 | // Returns shortcuts_map_.end() if there are no matches. |
[email protected] | f9dac2c7 | 2014-03-19 00:13:43 | [diff] [blame] | 111 | ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch( |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 112 | const base::string16& keyword, |
[email protected] | f9dac2c7 | 2014-03-19 00:13:43 | [diff] [blame] | 113 | ShortcutsBackend* backend); |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 114 | |
[email protected] | f9dac2c7 | 2014-03-19 00:13:43 | [diff] [blame] | 115 | int CalculateScore(const base::string16& terms, |
sdefresne | 4946258f | 2015-02-27 20:10:45 | [diff] [blame] | 116 | const ShortcutsDatabase::Shortcut& shortcut, |
[email protected] | f9dac2c7 | 2014-03-19 00:13:43 | [diff] [blame] | 117 | int max_relevance); |
[email protected] | 56e2654 | 2012-02-11 00:38:59 | [diff] [blame] | 118 | |
[email protected] | f13a213 | 2014-04-22 08:13:52 | [diff] [blame] | 119 | // The default max relevance unless overridden by a field trial. |
| 120 | static const int kShortcutsProviderDefaultMaxRelevance; |
| 121 | |
blundell | fb1215ee | 2015-06-19 20:18:12 | [diff] [blame] | 122 | AutocompleteProviderClient* client_; |
[email protected] | 9c8242a7 | 2011-09-17 00:44:51 | [diff] [blame] | 123 | bool initialized_; |
[email protected] | 0ebad8d | 2011-06-29 18:13:57 | [diff] [blame] | 124 | }; |
| 125 | |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 126 | #endif // COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_ |