autocomplete_match.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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 OPEN_HISTORY_PAGE, // A synthetic result that opens the history page 81 // to search for the input. 82 NUM_TYPES, 83 }; 84 85 AutocompleteMatch(); 86 AutocompleteMatch(AutocompleteProvider* provider, 87 int relevance, 88 bool deletable, 89 Type type); 90 ~AutocompleteMatch(); 91 92 // Converts |type| to a string representation. Used in logging. 93 static std::string TypeToString(Type type); 94 95 // Converts |type| to a resource identifier for the appropriate icon for this 96 // type. 97 static int TypeToIcon(Type type); 98 99 // Comparison function for determining when one match is better than another. 100 static bool MoreRelevant(const AutocompleteMatch& elem1, 101 const AutocompleteMatch& elem2); 102 103 // Comparison functions for removing matches with duplicate destinations. 104 static bool DestinationSortFunc(const AutocompleteMatch& elem1, 105 const AutocompleteMatch& elem2); 106 static bool DestinationsEqual(const AutocompleteMatch& elem1, 107 const AutocompleteMatch& elem2); 108 109 // Helper functions for classes creating matches: 110 // Fills in the classifications for |text|, using |style| as the base style 111 // and marking the first instance of |find_text| as a match. (This match 112 // will also not be dimmed, if |style| has DIM set.) 113 static void ClassifyMatchInString(const std::wstring& find_text, 114 const std::wstring& text, 115 int style, 116 ACMatchClassifications* classifications); 117 118 // Similar to ClassifyMatchInString(), but for cases where the range to mark 119 // as matching is already known (avoids calling find()). This can be helpful 120 // when find() would be misleading (e.g. you want to mark the second match in 121 // a string instead of the first). 122 static void ClassifyLocationInString(size_t match_location, 123 size_t match_length, 124 size_t overall_length, 125 int style, 126 ACMatchClassifications* classifications); 127 128 // The provider of this match, used to remember which provider the user had 129 // selected when the input changes. This may be NULL, in which case there is 130 // no provider (or memory of the user's selection). 131 AutocompleteProvider* provider; 132 133 // The relevance of this match. See table above for scores returned by 134 // various providers. This is used to rank matches among all responding 135 // providers, so different providers must be carefully tuned to supply 136 // matches with appropriate relevance. 137 // 138 // If the relevance is negative, it will only be displayed if there are not 139 // enough non-negative items in all the providers to max out the popup. In 140 // this case, the relevance of the additional items will be inverted so they 141 // can be mixed in with the rest of the relevances. This allows a provider 142 // to group its matches, having the added items appear intermixed with its 143 // other matches. 144 // 145 // TODO(pkasting): http://b/1111299 This should be calculated algorithmically, 146 // rather than being a fairly fixed value defined by the table above. 147 int relevance; 148 149 // True if the user should be able to delete this match. 150 bool deletable; 151 152 // This string is loaded into the location bar when the item is selected 153 // by pressing the arrow keys. This may be different than a URL, for example, 154 // for search suggestions, this would just be the search terms. 155 std::wstring fill_into_edit; 156 157 // The position within fill_into_edit from which we'll display the inline 158 // autocomplete string. This will be std::wstring::npos if this match should 159 // not be inline autocompleted. 160 size_t inline_autocomplete_offset; 161 162 // The URL to actually load when the autocomplete item is selected. This URL 163 // should be canonical so we can compare URLs with strcmp to avoid dupes. 164 // It may be empty if there is no possible navigation. 165 GURL destination_url; 166 167 // The main text displayed in the address bar dropdown. 168 std::wstring contents; 169 ACMatchClassifications contents_class; 170 171 // Additional helper text for each entry, such as a title or description. 172 std::wstring description; 173 ACMatchClassifications description_class; 174 175 // The transition type to use when the user opens this match. By default 176 // this is TYPED. Providers whose matches do not look like URLs should set 177 // it to GENERATED. 178 PageTransition::Type transition; 179 180 // True when this match is the "what you typed" match from the history 181 // system. 182 bool is_history_what_you_typed_match; 183 184 // Type of this match. 185 Type type; 186 187 // If this match corresponds to a keyword, this is the TemplateURL the 188 // keyword was obtained from. 189 const TemplateURL* template_url; 190 191 // True if the user has starred the destination URL. 192 bool starred; 193 194#ifndef NDEBUG 195 // Does a data integrity check on this match. 196 void Validate() const; 197 198 // Checks one text/classifications pair for valid values. 199 void ValidateClassifications( 200 const std::wstring& text, 201 const ACMatchClassifications& classifications) const; 202#endif 203}; 204 205typedef AutocompleteMatch::ACMatchClassification ACMatchClassification; 206typedef std::vector<ACMatchClassification> ACMatchClassifications; 207 208#endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_MATCH_H_ 209