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