autocomplete_match.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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_AUTOCOMPLETE_MATCH_H_ 6#define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_MATCH_H_ 7#pragma once 8 9#include <vector> 10#include <string> 11 12#include "chrome/common/page_transition_types.h" 13#include "googleurl/src/gurl.h" 14 15class AutocompleteProvider; 16class PageTransition; 17class TemplateURL; 18 19// AutocompleteMatch ---------------------------------------------------------- 20 21// A single result line with classified spans. The autocomplete popup displays 22// the 'contents' and the 'description' (the description is optional) in the 23// autocomplete dropdown, and fills in 'fill_into_edit' into the textbox when 24// that line is selected. fill_into_edit may be the same as 'description' for 25// things like URLs, but may be different for searches or other providers. For 26// example, a search result may say "Search for asdf" as the description, but 27// "asdf" should appear in the box. 28struct AutocompleteMatch { 29 // Autocomplete matches contain strings that are classified according to a 30 // separate vector of styles. This vector associates flags with particular 31 // string segments, and must be in sorted order. All text must be associated 32 // with some kind of classification. Even if a match has no distinct 33 // segments, its vector should contain an entry at offset 0 with no flags. 34 // 35 // Example: The user typed "goog" 36 // http://www.google.com/ Google 37 // ^ ^ ^ ^ ^ 38 // 0, | 15, | 4, 39 // 11,match 0,match 40 // 41 // This structure holds the classification information for each span. 42 struct ACMatchClassification { 43 // The values in here are not mutually exclusive -- use them like a 44 // bitfield. This also means we use "int" instead of this enum type when 45 // passing the values around, so the compiler doesn't complain. 46 enum Style { 47 NONE = 0, 48 URL = 1 << 0, // A URL 49 MATCH = 1 << 1, // A match for the user's search term 50 DIM = 1 << 2, // "Helper text" 51 }; 52 53 ACMatchClassification(size_t offset, int style) 54 : offset(offset), 55 style(style) { 56 } 57 58 // Offset within the string that this classification starts 59 size_t offset; 60 61 int style; 62 }; 63 64 typedef std::vector<ACMatchClassification> ACMatchClassifications; 65 66 // The type of this match. 67 enum Type { 68 URL_WHAT_YOU_TYPED = 0, // The input as a URL. 69 HISTORY_URL, // A past page whose URL contains the input. 70 HISTORY_TITLE, // A past page whose title contains the input. 71 HISTORY_BODY, // A past page whose body contains the input. 72 HISTORY_KEYWORD, // A past page whose keyword contains the input. 73 NAVSUGGEST, // A suggested URL. 74 SEARCH_WHAT_YOU_TYPED, // The input as a search query (with the default 75 // engine). 76 SEARCH_HISTORY, // A past search (with the default engine) 77 // containing the input. 78 SEARCH_SUGGEST, // A suggested search (with the default engine). 79 SEARCH_OTHER_ENGINE, // A search with a non-default engine. 80 NUM_TYPES, 81 }; 82 83 AutocompleteMatch(); 84 AutocompleteMatch(AutocompleteProvider* provider, 85 int relevance, 86 bool deletable, 87 Type type); 88 ~AutocompleteMatch(); 89 90 // Converts |type| to a string representation. Used in logging. 91 static std::string TypeToString(Type type); 92 93 // Converts |type| to a resource identifier for the appropriate icon for this 94 // type. 95 static int TypeToIcon(Type type); 96 97 // Comparison function for determining when one match is better than another. 98 static bool MoreRelevant(const AutocompleteMatch& elem1, 99 const AutocompleteMatch& elem2); 100 101 // Comparison functions for removing matches with duplicate destinations. 102 static bool DestinationSortFunc(const AutocompleteMatch& elem1, 103 const AutocompleteMatch& elem2); 104 static bool DestinationsEqual(const AutocompleteMatch& elem1, 105 const AutocompleteMatch& elem2); 106 107 // Helper functions for classes creating matches: 108 // Fills in the classifications for |text|, using |style| as the base style 109 // and marking the first instance of |find_text| as a match. (This match 110 // will also not be dimmed, if |style| has DIM set.) 111 static void ClassifyMatchInString(const string16& find_text, 112 const string16& text, 113 int style, 114 ACMatchClassifications* classifications); 115 116 // Similar to ClassifyMatchInString(), but for cases where the range to mark 117 // as matching is already known (avoids calling find()). This can be helpful 118 // when find() would be misleading (e.g. you want to mark the second match in 119 // a string instead of the first). 120 static void ClassifyLocationInString(size_t match_location, 121 size_t match_length, 122 size_t overall_length, 123 int style, 124 ACMatchClassifications* classifications); 125 126 // The provider of this match, used to remember which provider the user had 127 // selected when the input changes. This may be NULL, in which case there is 128 // no provider (or memory of the user's selection). 129 AutocompleteProvider* provider; 130 131 // The relevance of this match. See table above for scores returned by 132 // various providers. This is used to rank matches among all responding 133 // providers, so different providers must be carefully tuned to supply 134 // matches with appropriate relevance. 135 // 136 // TODO(pkasting): http://b/1111299 This should be calculated algorithmically, 137 // rather than being a fairly fixed value defined by the table above. 138 int relevance; 139 140 // True if the user should be able to delete this match. 141 bool deletable; 142 143 // This string is loaded into the location bar when the item is selected 144 // by pressing the arrow keys. This may be different than a URL, for example, 145 // for search suggestions, this would just be the search terms. 146 string16 fill_into_edit; 147 148 // The position within fill_into_edit from which we'll display the inline 149 // autocomplete string. This will be string16::npos if this match should 150 // not be inline autocompleted. 151 size_t inline_autocomplete_offset; 152 153 // The URL to actually load when the autocomplete item is selected. This URL 154 // should be canonical so we can compare URLs with strcmp to avoid dupes. 155 // It may be empty if there is no possible navigation. 156 GURL destination_url; 157 158 // The main text displayed in the address bar dropdown. 159 string16 contents; 160 ACMatchClassifications contents_class; 161 162 // Additional helper text for each entry, such as a title or description. 163 string16 description; 164 ACMatchClassifications description_class; 165 166 // The transition type to use when the user opens this match. By default 167 // this is TYPED. Providers whose matches do not look like URLs should set 168 // it to GENERATED. 169 PageTransition::Type transition; 170 171 // True when this match is the "what you typed" match from the history 172 // system. 173 bool is_history_what_you_typed_match; 174 175 // Type of this match. 176 Type type; 177 178 // If this match corresponds to a keyword, this is the TemplateURL the 179 // keyword was obtained from. 180 const TemplateURL* template_url; 181 182 // True if the user has starred the destination URL. 183 bool starred; 184 185 // True if this match is from a previous result. 186 bool from_previous; 187 188#ifndef NDEBUG 189 // Does a data integrity check on this match. 190 void Validate() const; 191 192 // Checks one text/classifications pair for valid values. 193 void ValidateClassifications( 194 const string16& text, 195 const ACMatchClassifications& classifications) const; 196#endif 197}; 198 199typedef AutocompleteMatch::ACMatchClassification ACMatchClassification; 200typedef std::vector<ACMatchClassification> ACMatchClassifications; 201 202#endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_MATCH_H_ 203