| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // This file contains the keyword autocomplete provider. The keyword provider | 5 // This file contains the keyword autocomplete provider. The keyword provider |
| 6 // is responsible for remembering/suggesting user "search keyword queries" | 6 // is responsible for remembering/suggesting user "search keyword queries" |
| 7 // (e.g. "imdb Godzilla") and then fixing them up into valid URLs. An | 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. | 8 // instance of it gets created and managed by the autocomplete controller. |
| 9 // KeywordProvider uses a TemplateURLService to find the set of keywords. | 9 // KeywordProvider uses a TemplateURLService to find the set of keywords. |
| 10 // | 10 // |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 // action "[keyword] %s". If the user has typed a (possibly partial) keyword | 48 // action "[keyword] %s". If the user has typed a (possibly partial) keyword |
| 49 // but no search terms, the suggested result is shown greyed out, with | 49 // but no search terms, the suggested result is shown greyed out, with |
| 50 // "<enter term(s)>" as the substituted input, and does nothing when selected. | 50 // "<enter term(s)>" as the substituted input, and does nothing when selected. |
| 51 class KeywordProvider : public AutocompleteProvider, | 51 class KeywordProvider : public AutocompleteProvider, |
| 52 public content::NotificationObserver { | 52 public content::NotificationObserver { |
| 53 public: | 53 public: |
| 54 KeywordProvider(ACProviderListener* listener, Profile* profile); | 54 KeywordProvider(ACProviderListener* listener, Profile* profile); |
| 55 // For testing. | 55 // For testing. |
| 56 KeywordProvider(ACProviderListener* listener, TemplateURLService* model); | 56 KeywordProvider(ACProviderListener* listener, TemplateURLService* model); |
| 57 | 57 |
| 58 // Extracts the next whitespace-delimited token from input and returns it. |
| 59 // Sets |remaining_input| to everything after the first token (skipping over |
| 60 // the first intervening whitespace). |
| 61 // If |trim_leading_whitespace| is true then leading whitespace in |
| 62 // |*remaining_input| will be trimmed. |
| 63 static string16 SplitKeywordFromInput(const string16& input, |
| 64 bool trim_leading_whitespace, |
| 65 string16* remaining_input); |
| 66 |
| 58 // Returns the replacement string from the user input. The replacement | 67 // Returns the replacement string from the user input. The replacement |
| 59 // string is the portion of the input that does not contain the keyword. | 68 // string is the portion of the input that does not contain the keyword. |
| 60 // For example, the replacement string for "b blah" is blah. | 69 // For example, the replacement string for "b blah" is blah. |
| 61 // If |trim_leading_whitespace| is true then leading whitespace in | 70 // If |trim_leading_whitespace| is true then leading whitespace in |
| 62 // replacement string will be trimmed. | 71 // replacement string will be trimmed. |
| 63 static string16 SplitReplacementStringFromInput( | 72 static string16 SplitReplacementStringFromInput(const string16& input, |
| 64 const string16& input, | 73 bool trim_leading_whitespace); |
| 65 bool trim_leading_whitespace); | |
| 66 | 74 |
| 67 // Returns the matching substituting keyword for |input|, or NULL if there | 75 // Returns the matching substituting keyword for |input|, or NULL if there |
| 68 // is no keyword for the specified input. | 76 // is no keyword for the specified input. |
| 69 static const TemplateURL* GetSubstitutingTemplateURLForInput( | 77 static const TemplateURL* GetSubstitutingTemplateURLForInput( |
| 70 Profile* profile, | 78 Profile* profile, |
| 71 const AutocompleteInput& input, | 79 const AutocompleteInput& input, |
| 72 string16* remaining_input); | 80 string16* remaining_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 string16 GetKeywordForText(const string16& text) const; |
| 86 |
| 87 // Creates a fully marked-up AutocompleteMatch for a specific keyword. |
| 88 AutocompleteMatch CreateAutocompleteMatch(const string16& text, |
| 89 const string16& keyword, |
| 90 const AutocompleteInput& input); |
| 73 | 91 |
| 74 // AutocompleteProvider | 92 // AutocompleteProvider |
| 75 virtual void Start(const AutocompleteInput& input, | 93 virtual void Start(const AutocompleteInput& input, |
| 76 bool minimal_changes) OVERRIDE; | 94 bool minimal_changes) OVERRIDE; |
| 77 virtual void Stop() OVERRIDE; | 95 virtual void Stop() OVERRIDE; |
| 78 | 96 |
| 79 private: | 97 private: |
| 80 class ScopedEndExtensionKeywordMode; | 98 class ScopedEndExtensionKeywordMode; |
| 81 friend class ScopedEndExtensionKeywordMode; | 99 friend class ScopedEndExtensionKeywordMode; |
| 82 | 100 |
| 83 virtual ~KeywordProvider(); | 101 virtual ~KeywordProvider(); |
| 84 | 102 |
| 85 // Extracts the keyword from |input| into |keyword|. Any remaining characters | 103 // Extracts the keyword from |input| into |keyword|. Any remaining characters |
| 86 // after the keyword are placed in |remaining_input|. Returns true if |input| | 104 // after the keyword are placed in |remaining_input|. Returns true if |input| |
| 87 // is valid and has a keyword. This makes use of SplitKeywordFromInput to | 105 // is valid and has a keyword. This makes use of SplitKeywordFromInput to |
| 88 // extract the keyword and remaining string, and uses | 106 // extract the keyword and remaining string, and uses |
| 89 // TemplateURLService::CleanUserInputKeyword to remove unnecessary characters. | 107 // TemplateURLService::CleanUserInputKeyword to remove unnecessary characters. |
| 90 // In general use this instead of SplitKeywordFromInput. | 108 // In general use this instead of SplitKeywordFromInput. |
| 91 // Leading whitespace in |*remaining_input| will be trimmed. | 109 // Leading whitespace in |*remaining_input| will be trimmed. |
| 92 static bool ExtractKeywordFromInput(const AutocompleteInput& input, | 110 static bool ExtractKeywordFromInput(const AutocompleteInput& input, |
| 93 string16* keyword, | 111 string16* keyword, |
| 94 string16* remaining_input); | 112 string16* remaining_input); |
| 95 | 113 |
| 96 // Extracts the next whitespace-delimited token from input and returns it. | |
| 97 // Sets |remaining_input| to everything after the first token (skipping over | |
| 98 // the first intervening whitespace). | |
| 99 // If |trim_leading_whitespace| is true then leading whitespace in | |
| 100 // |*remaining_input| will be trimmed. | |
| 101 static string16 SplitKeywordFromInput(const string16& input, | |
| 102 bool trim_leading_whitespace, | |
| 103 string16* remaining_input); | |
| 104 | |
| 105 // Fills in the "destination_url" and "contents" fields of |match| with the | 114 // Fills in the "destination_url" and "contents" fields of |match| with the |
| 106 // provided user input and keyword data. | 115 // provided user input and keyword data. |
| 107 static void FillInURLAndContents( | 116 static void FillInURLAndContents(Profile* profile, |
| 108 Profile* profile, | 117 const string16& remaining_input, |
| 109 const string16& remaining_input, | 118 const TemplateURL* element, |
| 110 const TemplateURL* element, | 119 AutocompleteMatch* match); |
| 111 AutocompleteMatch* match); | |
| 112 | 120 |
| 113 // Determines the relevance for some input, given its type, whether the user | 121 // 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 | 122 // typed the complete keyword, and whether the user is in "prefer keyword |
| 115 // matches" mode, and whether the keyword supports replacement. | 123 // matches" mode, and whether the keyword supports replacement. |
| 116 // If |allow_exact_keyword_match| is false, the relevance for complete | 124 // If |allow_exact_keyword_match| is false, the relevance for complete |
| 117 // keywords that support replacements is degraded. | 125 // keywords that support replacements is degraded. |
| 118 static int CalculateRelevance(AutocompleteInput::Type type, | 126 static int CalculateRelevance(AutocompleteInput::Type type, |
| 119 bool complete, | 127 bool complete, |
| 120 bool support_replacement, | 128 bool support_replacement, |
| 121 bool prefer_keyword, | 129 bool prefer_keyword, |
| 122 bool allow_exact_keyword_match); | 130 bool allow_exact_keyword_match); |
| 123 | 131 |
| 124 // Creates a fully marked-up AutocompleteMatch from the user's input. | 132 // Creates a fully marked-up AutocompleteMatch from the user's input. |
| 125 // If |relevance| is negative, calculate a relevance based on heuristics. | 133 // If |relevance| is negative, calculate a relevance based on heuristics. |
| 126 AutocompleteMatch CreateAutocompleteMatch( | 134 AutocompleteMatch CreateAutocompleteMatch(TemplateURLService* model, |
| 127 TemplateURLService* model, | 135 const string16& keyword, |
| 128 const string16& keyword, | 136 const AutocompleteInput& input, |
| 129 const AutocompleteInput& input, | 137 size_t prefix_length, |
| 130 size_t prefix_length, | 138 const string16& remaining_input, |
| 131 const string16& remaining_input, | 139 int relevance); |
| 132 int relevance); | |
| 133 | 140 |
| 134 void EnterExtensionKeywordMode(const std::string& extension_id); | 141 void EnterExtensionKeywordMode(const std::string& extension_id); |
| 135 void MaybeEndExtensionKeywordMode(); | 142 void MaybeEndExtensionKeywordMode(); |
| 136 | 143 |
| 137 // content::NotificationObserver interface. | 144 // content::NotificationObserver interface. |
| 138 virtual void Observe(int type, | 145 virtual void Observe(int type, |
| 139 const content::NotificationSource& source, | 146 const content::NotificationSource& source, |
| 140 const content::NotificationDetails& details) OVERRIDE; | 147 const content::NotificationDetails& details) OVERRIDE; |
| 141 | 148 |
| 149 TemplateURLService* GetTemplateURLService() const; |
| 150 |
| 142 // Model for the keywords. This is only non-null when testing, otherwise the | 151 // Model for the keywords. This is only non-null when testing, otherwise the |
| 143 // TemplateURLService from the Profile is used. | 152 // TemplateURLService from the Profile is used. |
| 144 TemplateURLService* model_; | 153 TemplateURLService* model_; |
| 145 | 154 |
| 146 // Identifies the current input state. This is incremented each time the | 155 // Identifies the current input state. This is incremented each time the |
| 147 // autocomplete edit's input changes in any way. It is used to tell whether | 156 // autocomplete edit's input changes in any way. It is used to tell whether |
| 148 // suggest results from the extension are current. | 157 // suggest results from the extension are current. |
| 149 int current_input_id_; | 158 int current_input_id_; |
| 150 | 159 |
| 151 // The input state at the time we last asked the extension for suggest | 160 // The input state at the time we last asked the extension for suggest |
| 152 // results. | 161 // results. |
| 153 AutocompleteInput extension_suggest_last_input_; | 162 AutocompleteInput extension_suggest_last_input_; |
| 154 | 163 |
| 155 // We remember the last suggestions we've received from the extension in case | 164 // We remember the last suggestions we've received from the extension in case |
| 156 // we need to reset our matches without asking the extension again. | 165 // we need to reset our matches without asking the extension again. |
| 157 std::vector<AutocompleteMatch> extension_suggest_matches_; | 166 std::vector<AutocompleteMatch> extension_suggest_matches_; |
| 158 | 167 |
| 159 // If non-empty, holds the ID of the extension whose keyword is currently in | 168 // If non-empty, holds the ID of the extension whose keyword is currently in |
| 160 // the URL bar while the autocomplete popup is open. | 169 // the URL bar while the autocomplete popup is open. |
| 161 std::string current_keyword_extension_id_; | 170 std::string current_keyword_extension_id_; |
| 162 | 171 |
| 163 content::NotificationRegistrar registrar_; | 172 content::NotificationRegistrar registrar_; |
| 164 | 173 |
| 165 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); | 174 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); |
| 166 }; | 175 }; |
| 167 | 176 |
| 168 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 177 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
| OLD | NEW |