autocomplete_match.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef COMPONENTS_OMNIBOX_AUTOCOMPLETE_MATCH_H_ 65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define COMPONENTS_OMNIBOX_AUTOCOMPLETE_MATCH_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_match_type.h" 14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url.h" 151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/base/page_transition_types.h" 16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AutocompleteProvider; 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURL; 20116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass TemplateURLService; 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Time; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace base 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 26effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochconst char kACMatchPropertyInputText[] = "input text"; 27effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochconst char kACMatchPropertyContentsPrefix[] = "match contents prefix"; 28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochconst char kACMatchPropertyContentsStartIndex[] = "match contents start index"; 29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// AutocompleteMatch ---------------------------------------------------------- 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A single result line with classified spans. The autocomplete popup displays 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the 'contents' and the 'description' (the description is optional) in the 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// autocomplete dropdown, and fills in 'fill_into_edit' into the textbox when 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that line is selected. fill_into_edit may be the same as 'description' for 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// things like URLs, but may be different for searches or other providers. For 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// example, a search result may say "Search for asdf" as the description, but 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "asdf" should appear in the box. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct AutocompleteMatch { 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Autocomplete matches contain strings that are classified according to a 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // separate vector of styles. This vector associates flags with particular 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // string segments, and must be in sorted order. All text must be associated 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // with some kind of classification. Even if a match has no distinct 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // segments, its vector should contain an entry at offset 0 with no flags. 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Example: The user typed "goog" 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // http://www.google.com/ Google 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ^ ^ ^ ^ ^ 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 0, | 15, | 4, 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 11,match 0,match 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This structure holds the classification information for each span. 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) struct ACMatchClassification { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The values in here are not mutually exclusive -- use them like a 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // bitfield. This also means we use "int" instead of this enum type when 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // passing the values around, so the compiler doesn't complain. 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum Style { 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NONE = 0, 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) URL = 1 << 0, // A URL 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MATCH = 1 << 1, // A match for the user's search term 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DIM = 1 << 2, // "Helper text" 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACMatchClassification(size_t offset, int style) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : offset(offset), 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) style(style) { 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Offset within the string that this classification starts 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t offset; 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int style; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef std::vector<ACMatchClassification> ACMatchClassifications; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Type used by providers to attach additional, optional information to 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // an AutocompleteMatch. 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef std::map<std::string, std::string> AdditionalInfo; 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The type of this match. 8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) typedef AutocompleteMatchType::Type Type; 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Null-terminated array of characters that are not valid within |contents| 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and |description| strings. 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static const base::char16 kInvalidChars[]; 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteMatch(); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteMatch(AutocompleteProvider* provider, 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int relevance, 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool deletable, 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Type type); 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteMatch(const AutocompleteMatch& match); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~AutocompleteMatch(); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Converts |type| to a string representation. Used in logging and debugging. 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteMatch& operator=(const AutocompleteMatch& match); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Converts |type| to a resource identifier for the appropriate icon for this 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // type to show in the completion popup. 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static int TypeToIcon(Type type); 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Comparison function for determining when one match is better than another. 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool MoreRelevant(const AutocompleteMatch& elem1, 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const AutocompleteMatch& elem2); 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 107c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch // Comparison function for removing matches with duplicate destinations. 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Destinations are compared using |stripped_destination_url|. Pairs of 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // matches with empty destinations are treated as differing, since empty 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // destinations are expected for non-navigable matches. 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool DestinationsEqual(const AutocompleteMatch& elem1, 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const AutocompleteMatch& elem2); 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Helper functions for classes creating matches: 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Fills in the classifications for |text|, using |style| as the base style 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and marking the first instance of |find_text| as a match. (This match 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // will also not be dimmed, if |style| has DIM set.) 118a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) static void ClassifyMatchInString(const base::string16& find_text, 119a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& text, 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int style, 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACMatchClassifications* classifications); 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Similar to ClassifyMatchInString(), but for cases where the range to mark 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // as matching is already known (avoids calling find()). This can be helpful 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // when find() would be misleading (e.g. you want to mark the second match in 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a string instead of the first). 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void ClassifyLocationInString(size_t match_location, 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t match_length, 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t overall_length, 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int style, 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACMatchClassifications* classifications); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a new vector of classifications containing the merged contents of 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |classifications1| and |classifications2|. 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static ACMatchClassifications MergeClassifications( 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ACMatchClassifications& classifications1, 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ACMatchClassifications& classifications2); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Converts classifications to and from a serialized string representation 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // (using comma-separated integers to sequentially list positions and styles). 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static std::string ClassificationsToString( 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ACMatchClassifications& classifications); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static ACMatchClassifications ClassificationsFromString( 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& serialized_classifications); 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Adds a classification to the end of |classifications| iff its style is 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // different from the last existing classification. |offset| must be larger 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // than the offset of the last classification in |classifications|. 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void AddLastClassificationIfNecessary( 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACMatchClassifications* classifications, 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t offset, 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int style); 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Removes invalid characters from |text|. Should be called on strings coming 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // from external sources (such as extensions) before assigning to |contents| 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // or |description|. 157a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) static base::string16 SanitizeString(const base::string16& text); 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Convenience function to check if |type| is a search (as opposed to a URL or 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // an extension). 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsSearchType(Type type); 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 163effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // Convenience function to check if |type| is a special search suggest type - 164effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // like entity, personalized, profile or postfix. 165effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch static bool IsSpecializedSearchType(Type type); 166effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 167116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // A static version GetTemplateURL() that takes the match's keyword and 168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // match's hostname as parameters. In short, returns the TemplateURL 169116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // associated with |keyword| if it exists; otherwise returns the TemplateURL 170116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // associated with |host| if it exists. 171116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static TemplateURL* GetTemplateURLWithKeyword( 172116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch TemplateURLService* template_url_service, 173116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::string16& keyword, 174116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& host); 175116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 176116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Returns |url| altered by stripping off "www.", converting https protocol 177116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // to http, and stripping excess query parameters. These conversions are 178116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // merely to allow comparisons to remove likely duplicates; these URLs are 179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // not used as actual destination URLs. If |template_url_service| is not 180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // NULL, it is used to get a template URL corresponding to this match. If 181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // the match's keyword is known, it can be passed in. Otherwise, it can be 182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // left empty and the template URL (if any) is determined from the 183116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // destination's hostname. The template URL is used to strip off query args 184116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // other than the search terms themselves that would otherwise prevent doing 185116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // proper deduping. 186116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static GURL GURLToStrippedGURL(const GURL& url, 187116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch TemplateURLService* template_url_service, 188116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::string16& keyword); 189116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Computes the stripped destination URL (via GURLToStrippedGURL()) and 191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // stores the result in |stripped_destination_url|. 192116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ComputeStrippedDestinationURL(TemplateURLService* template_url_service); 193116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 194116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Sets |allowed_to_be_default_match| to true if this match is effectively 195116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // the URL-what-you-typed match (i.e., would be dupped against the UWYT 196116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // match when AutocompleteResult merges matches). |canonical_input_url| is 197116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // the AutocompleteInput interpreted as a URL (i.e., 198116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // AutocompleteInput::canonicalized_url()). 199116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void EnsureUWYTIsAllowedToBeDefault(const GURL& canonical_input_url, 200116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch TemplateURLService* template_url_service); 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Gets data relevant to whether there should be any special keyword-related 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // UI shown for this match. If this match represents a selected keyword, i.e. 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the UI should be "in keyword mode", |keyword| will be set to the keyword 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and |is_keyword_hint| will be set to false. If this match has a non-NULL 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |associated_keyword|, i.e. we should show a "Press [tab] to search ___" 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // hint and allow the user to toggle into keyword mode, |keyword| will be set 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to the associated keyword and |is_keyword_hint| will be set to true. Note 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // that only one of these states can be in effect at once. In all other 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // cases, |keyword| will be cleared, even when our member variable |keyword| 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is non-empty -- such as with non-substituting keywords or matches that 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // represent searches using the default search engine. See also 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // GetSubstitutingExplicitlyInvokedKeyword(). 214116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void GetKeywordUIState(TemplateURLService* template_url_service, 215a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16* keyword, 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool* is_keyword_hint) const; 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns |keyword|, but only if it represents a substituting keyword that 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the user has explicitly invoked. If for example this match represents a 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // search with the default search engine (and the user didn't explicitly 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // invoke its keyword), this returns the empty string. The result is that 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // this function returns a non-empty string in the same cases as when the UI 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // should show up as being "in keyword mode". 224a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 GetSubstitutingExplicitlyInvokedKeyword( 225116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch TemplateURLService* template_url_service) const; 2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the TemplateURL associated with this match. This may be NULL if 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the match has no keyword OR if the keyword no longer corresponds to a valid 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TemplateURL. See comments on |keyword| below. 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If |allow_fallback_to_destination_host| is true and the keyword does 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // not map to a valid TemplateURL, we'll then check for a TemplateURL that 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // corresponds to the destination_url's hostname. 233116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch TemplateURL* GetTemplateURL(TemplateURLService* template_url_service, 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool allow_fallback_to_destination_host) const; 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Adds optional information to the |additional_info| dictionary. 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void RecordAdditionalInfo(const std::string& property, 2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& value); 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void RecordAdditionalInfo(const std::string& property, int value); 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void RecordAdditionalInfo(const std::string& property, 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::Time& value); 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 243eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Returns the value recorded for |property| in the |additional_info| 244eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // dictionary. Returns the empty string if no such value exists. 245eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string GetAdditionalInfo(const std::string& property) const; 246eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 2472385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch // Returns whether this match is a "verbatim" match: a URL navigation directly 2482385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch // to the user's input, a search for the user's input with the default search 2492385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch // engine, or a "keyword mode" search for the query portion of the user's 2502385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch // input. Note that rare or unusual types that could be considered verbatim, 2512385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch // such as keyword engine matches or extension-provided matches, aren't 2522385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch // detected by this IsVerbatimType, as the user will not be able to infer 2532385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch // what will happen when he or she presses enter in those cases if the match 2542385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch // is not shown. 2552385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch bool IsVerbatimType() const; 2562385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch 257a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Returns whether this match or any duplicate of this match can be deleted. 258a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // This is used to decide whether we should call DeleteMatch(). 259a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) bool SupportsDeletion() const; 260a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The provider of this match, used to remember which provider the user had 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // selected when the input changes. This may be NULL, in which case there is 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // no provider (or memory of the user's selection). 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AutocompleteProvider* provider; 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The relevance of this match. See table in autocomplete.h for scores 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // returned by various providers. This is used to rank matches among all 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // responding providers, so different providers must be carefully tuned to 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // supply matches with appropriate relevance. 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(pkasting): http://b/1111299 This should be calculated algorithmically, 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // rather than being a fairly fixed value defined by the table above. 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int relevance; 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // How many times this result was typed in / selected from the omnibox. 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Only set for some providers and result_types. If it is not set, 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // its value is -1. At the time of writing this comment, it is only 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // set for matches from HistoryURL and HistoryQuickProvider. 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int typed_count; 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // True if the user should be able to delete this match. 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool deletable; 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This string is loaded into the location bar when the item is selected 2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // by pressing the arrow keys. This may be different than a URL, for example, 2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // for search suggestions, this would just be the search terms. 287a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 fill_into_edit; 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 289ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // The inline autocompletion to display after the user's typing in the 290ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // omnibox, if this match becomes the default match. It may be empty. 291a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 inline_autocompletion; 2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 293c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // If false, the omnibox should prevent this match from being the 294c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // default match. Providers should set this to true only if the 295c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // user's input, plus any inline autocompletion on this match, would 296c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // lead the user to expect a navigation to this match's destination. 297c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // For example, with input "foo", a search for "bar" or navigation 298c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // to "bar.com" should not set this flag; a navigation to "foo.com" 299c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // should only set this flag if ".com" will be inline autocompleted; 300c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // and a navigation to "foo/" (an intranet host) or search for "foo" 301c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // should set this flag. 302c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch bool allowed_to_be_default_match; 303c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch 3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The URL to actually load when the autocomplete item is selected. This URL 3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // should be canonical so we can compare URLs with strcmp to avoid dupes. 3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // It may be empty if there is no possible navigation. 3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL destination_url; 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The destination URL with "www." stripped off for better dupe finding. 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL stripped_destination_url; 3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The main text displayed in the address bar dropdown. 313a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 contents; 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACMatchClassifications contents_class; 3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Additional helper text for each entry, such as a title or description. 317a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 description; 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACMatchClassifications description_class; 3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 320cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // A rich-format version of the display for the dropdown. 321cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::string16 answer_contents; 322cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::string16 answer_type; 323cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The transition type to use when the user opens this match. By default 3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // this is TYPED. Providers whose matches do not look like URLs should set 3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // it to GENERATED. 3271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ui::PageTransition transition; 3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // True when this match is the "what you typed" match from the history 3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // system. 3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_history_what_you_typed_match; 3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Type of this match. 3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Type type; 3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Set with a keyword provider match if this match can show a keyword hint. 3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // For example, if this is a SearchProvider match for "www.amazon.com", 3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |associated_keyword| could be a KeywordProvider match for "amazon.com". 3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<AutocompleteMatch> associated_keyword; 3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The keyword of the TemplateURL the match originated from. This is nonempty 3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // for both explicit "keyword mode" matches as well as matches for the default 3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // search provider (so, any match for which we're doing substitution); it 3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // doesn't imply (alone) that the UI is going to show a keyword hint or 3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // keyword mode. For that, see GetKeywordUIState() or 3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // GetSubstitutingExplicitlyInvokedKeyword(). 3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // CAUTION: The TemplateURL associated with this keyword may be deleted or 3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // modified while the AutocompleteMatch is alive. This means anyone who 3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // accesses it must perform any necessary sanity checks before blindly using 3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // it! 352a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 keyword; 3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // True if this match is from a previous result. 3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool from_previous; 3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Optional search terms args. If present, 3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AutocompleteController::UpdateAssistedQueryStats() will incorporate this 3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // data with additional data it calculates and pass the completed struct to 3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TemplateURLRef::ReplaceSearchTerms() to reset the match's |destination_url| 3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // after the complete set of matches in the AutocompleteResult has been chosen 3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and sorted. Most providers will leave this as NULL, which will cause the 3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AutocompleteController to do no additional transformations. 3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<TemplateURLRef::SearchTermsArgs> search_terms_args; 3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Information dictionary into which each provider can optionally record a 3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // property and associated value and which is presented in chrome://omnibox. 3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AdditionalInfo additional_info; 3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // A list of matches culled during de-duplication process, retained to 371a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // ensure if a match is deleted, the duplicates are deleted as well. 372a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) std::vector<AutocompleteMatch> duplicate_matches; 373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NDEBUG 3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Does a data integrity check on this match. 3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Validate() const; 3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Checks one text/classifications pair for valid values. 3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void ValidateClassifications( 380a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& text, 3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ACMatchClassifications& classifications) const; 3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef AutocompleteMatch::ACMatchClassification ACMatchClassification; 3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::vector<ACMatchClassification> ACMatchClassifications; 3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::vector<AutocompleteMatch> ACMatches; 3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif // COMPONENTS_OMNIBOX_AUTOCOMPLETE_MATCH_H_ 390