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