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. 4 5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_ 6 #define CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 12 #include "base/gtest_prod_util.h" 13 #include "chrome/browser/autocomplete/autocomplete_provider.h" 14 #include "chrome/browser/autocomplete/url_prefix.h" 15 #include "chrome/browser/history/shortcuts_backend.h" 16 17 class Profile; 18 19 namespace history { 20 class ShortcutsProviderTest; 21 } 22 23 // Provider of recently autocompleted links. Provides autocomplete suggestions 24 // from previously selected suggestions. The more often a user selects a 25 // suggestion for a given search term the higher will be that suggestion's 26 // ranking for future uses of that search term. 27 class ShortcutsProvider 28 : public AutocompleteProvider, 29 public history::ShortcutsBackend::ShortcutsBackendObserver { 30 public: 31 ShortcutsProvider(AutocompleteProviderListener* listener, Profile* profile); 32 33 // Performs the autocompletion synchronously. Since no asynch completion is 34 // performed |minimal_changes| is ignored. 35 virtual void Start(const AutocompleteInput& input, 36 bool minimal_changes) OVERRIDE; 37 38 virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE; 39 40 private: 41 friend class ClassifyTest; 42 friend class history::ShortcutsProviderTest; 43 44 typedef std::multimap<char16, base::string16> WordMap; 45 46 virtual ~ShortcutsProvider(); 47 48 // ShortcutsBackendObserver: 49 virtual void OnShortcutsLoaded() OVERRIDE; 50 51 // Performs the autocomplete matching and scoring. 52 void GetMatches(const AutocompleteInput& input); 53 54 // Returns an AutocompleteMatch corresponding to |shortcut|. Assigns it 55 // |relevance| score in the process, and highlights the description and 56 // contents against |term_string|, which should be the lower-cased version 57 // of the user's input. |term_string| and |fixed_up_term_string| are used 58 // to decide what can be inlined. If |prevent_inline_autocomplete|, no 59 // matches with inline completions will be allowed to be the default match. 60 AutocompleteMatch ShortcutToACMatch( 61 const history::ShortcutsBackend::Shortcut& shortcut, 62 int relevance, 63 const base::string16& term_string, 64 const base::string16& fixed_up_term_string, 65 const bool prevent_inline_autocomplete); 66 67 // Returns a map mapping characters to groups of words from |text| that start 68 // with those characters, ordered lexicographically descending so that longer 69 // words appear before their prefixes (if any) within a particular 70 // equal_range(). 71 static WordMap CreateWordMapForString(const base::string16& text); 72 73 // Given |text| and a corresponding base set of classifications 74 // |original_class|, adds ACMatchClassification::MATCH markers for all 75 // instances of the words from |find_words| within |text| and returns the 76 // resulting classifications. (|find_text| is provided as the original string 77 // used to create |find_words|. This is supplied because it's common for this 78 // to be a prefix of |text|, so we can quickly check for that and mark that 79 // entire substring as a match before proceeding with the more generic 80 // algorithm.) 81 // 82 // For example, given the |text| 83 // "Sports and News at sports.somesite.com - visit us!" and |original_class| 84 // {{0, NONE}, {18, URL}, {37, NONE}} (marking "sports.somesite.com" as a 85 // URL), calling with |find_text| set to "sp ew" would return 86 // {{0, MATCH}, {2, NONE}, {12, MATCH}, {14, NONE}, {18, URL|MATCH}, 87 // {20, URL}, {37, NONE}}. 88 // 89 // |find_words| should be as constructed by CreateWordMapForString(find_text). 90 // 91 // |find_text| (and thus |find_words|) are expected to be lowercase. |text| 92 // will be lowercased in this function. 93 static ACMatchClassifications ClassifyAllMatchesInString( 94 const base::string16& find_text, 95 const WordMap& find_words, 96 const base::string16& text, 97 const ACMatchClassifications& original_class); 98 99 // Returns iterator to first item in |shortcuts_map_| matching |keyword|. 100 // Returns shortcuts_map_.end() if there are no matches. 101 history::ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch( 102 const base::string16& keyword, 103 history::ShortcutsBackend* backend); 104 105 int CalculateScore( 106 const base::string16& terms, 107 const history::ShortcutsBackend::Shortcut& shortcut, 108 int max_relevance); 109 110 std::string languages_; 111 bool initialized_; 112 }; 113 114 #endif // CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_PROVIDER_H_ 115