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 // This file contains the keyword autocomplete provider. The keyword provider 6 // is responsible for remembering/suggesting user "search keyword queries" 7 // (e.g. "imdb Godzilla") and then fixing them up into valid URLs. An 8 // instance of it gets created and managed by the autocomplete controller. 9 // KeywordProvider uses a TemplateURLService to find the set of keywords. 10 11 #ifndef CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ 12 #define CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ 13 14 #include <string> 15 16 #include "base/basictypes.h" 17 #include "base/compiler_specific.h" 18 #include "base/memory/scoped_ptr.h" 19 #include "chrome/browser/autocomplete/autocomplete_input.h" 20 #include "chrome/browser/autocomplete/autocomplete_provider.h" 21 #include "components/metrics/proto/omnibox_input_type.pb.h" 22 23 class KeywordExtensionsDelegate; 24 class Profile; 25 class TemplateURL; 26 class TemplateURLService; 27 28 // Autocomplete provider for keyword input. 29 // 30 // After construction, the autocomplete controller repeatedly calls Start() 31 // with some user input, each time expecting to receive a small set of the best 32 // matches (either synchronously or asynchronously). 33 // 34 // To construct these matches, the provider treats user input as a series of 35 // whitespace-delimited tokens and tries to match the first token as the prefix 36 // of a known "keyword". A keyword is some string that maps to a search query 37 // URL; the rest of the user's input is taken as the input to the query. For 38 // example, the keyword "bug" might map to the URL "http://b/issue?id=%s", so 39 // input like "bug 123" would become "http://b/issue?id=123". 40 // 41 // Because we do prefix matching, user input could match more than one keyword 42 // at once. (Example: the input "f jazz" matches all keywords starting with 43 // "f".) We return the best matches, up to three. 44 // 45 // The resulting matches are shown with content specified by the keyword 46 // (usually "Search [name] for %s"), description "(Keyword: [keyword])", and 47 // action "[keyword] %s". If the user has typed a (possibly partial) keyword 48 // but no search terms, the suggested result is shown greyed out, with 49 // "<enter term(s)>" as the substituted input, and does nothing when selected. 50 class KeywordProvider : public AutocompleteProvider { 51 public: 52 KeywordProvider(AutocompleteProviderListener* listener, Profile* profile); 53 // For testing. 54 KeywordProvider(AutocompleteProviderListener* listener, 55 TemplateURLService* model); 56 57 // Extracts the next whitespace-delimited token from input and returns it. 58 // Sets |remaining_input| to everything after the first token (skipping over 59 // the first intervening whitespace). 60 // If |trim_leading_whitespace| is true then leading whitespace in 61 // |*remaining_input| will be trimmed. 62 static base::string16 SplitKeywordFromInput(const base::string16& input, 63 bool trim_leading_whitespace, 64 base::string16* remaining_input); 65 66 // Returns the replacement string from the user input. The replacement 67 // string is the portion of the input that does not contain the keyword. 68 // For example, the replacement string for "b blah" is blah. 69 // If |trim_leading_whitespace| is true then leading whitespace in 70 // replacement string will be trimmed. 71 static base::string16 SplitReplacementStringFromInput( 72 const base::string16& input, 73 bool trim_leading_whitespace); 74 75 // Returns the matching substituting keyword for |input|, or NULL if there 76 // is no keyword for the specified input. If the matching keyword was found, 77 // updates |input|'s text and cursor position. 78 static const TemplateURL* GetSubstitutingTemplateURLForInput( 79 TemplateURLService* model, 80 AutocompleteInput* input); 81 82 // If |text| corresponds (in the sense of 83 // TemplateURLModel::CleanUserInputKeyword()) to an enabled, substituting 84 // keyword, returns that keyword; returns the empty string otherwise. 85 base::string16 GetKeywordForText(const base::string16& text) const; 86 87 // Creates a fully marked-up AutocompleteMatch for a specific keyword. 88 AutocompleteMatch CreateVerbatimMatch(const base::string16& text, 89 const base::string16& keyword, 90 const AutocompleteInput& input); 91 92 // AutocompleteProvider: 93 virtual void Start(const AutocompleteInput& input, 94 bool minimal_changes) OVERRIDE; 95 virtual void Stop(bool clear_cached_results) OVERRIDE; 96 97 private: 98 friend class KeywordExtensionsDelegateImpl; 99 100 virtual ~KeywordProvider(); 101 102 // Extracts the keyword from |input| into |keyword|. Any remaining characters 103 // after the keyword are placed in |remaining_input|. Returns true if |input| 104 // is valid and has a keyword. This makes use of SplitKeywordFromInput to 105 // extract the keyword and remaining string, and uses 106 // TemplateURLService::CleanUserInputKeyword to remove unnecessary characters. 107 // In general use this instead of SplitKeywordFromInput. 108 // Leading whitespace in |*remaining_input| will be trimmed. 109 static bool ExtractKeywordFromInput(const AutocompleteInput& input, 110 base::string16* keyword, 111 base::string16* remaining_input); 112 113 // Determines the relevance for some input, given its type, whether the user 114 // typed the complete keyword, and whether the user is in "prefer keyword 115 // matches" mode, and whether the keyword supports replacement. 116 // If |allow_exact_keyword_match| is false, the relevance for complete 117 // keywords that support replacements is degraded. 118 static int CalculateRelevance(metrics::OmniboxInputType::Type type, 119 bool complete, 120 bool support_replacement, 121 bool prefer_keyword, 122 bool allow_exact_keyword_match); 123 124 // Creates a fully marked-up AutocompleteMatch from the user's input. 125 // If |relevance| is negative, calculate a relevance based on heuristics. 126 AutocompleteMatch CreateAutocompleteMatch( 127 const TemplateURL* template_url, 128 const AutocompleteInput& input, 129 size_t prefix_length, 130 const base::string16& remaining_input, 131 bool allowed_to_be_default_match, 132 int relevance); 133 134 // Fills in the "destination_url" and "contents" fields of |match| with the 135 // provided user input and keyword data. 136 void FillInURLAndContents(const base::string16& remaining_input, 137 const TemplateURL* element, 138 AutocompleteMatch* match) const; 139 140 TemplateURLService* GetTemplateURLService() const; 141 142 // Model for the keywords. This is only non-null when testing, otherwise the 143 // TemplateURLService from the Profile is used. 144 TemplateURLService* model_; 145 146 // Delegate to handle the extensions-only logic for KeywordProvider. 147 // NULL when extensions are not enabled. May be NULL for tests. 148 scoped_ptr<KeywordExtensionsDelegate> extensions_delegate_; 149 150 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); 151 }; 152 153 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ 154