| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 TemplateURLModel to find the set of keywords. | 9 // KeywordProvider uses a TemplateURLModel to find the set of keywords. |
| 10 // | 10 // |
| 11 // For more information on the autocomplete system in general, including how | 11 // For more information on the autocomplete system in general, including how |
| 12 // the autocomplete controller and autocomplete providers work, see | 12 // the autocomplete controller and autocomplete providers work, see |
| 13 // chrome/browser/autocomplete.h. | 13 // chrome/browser/autocomplete.h. |
| 14 | 14 |
| 15 #ifndef CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 15 #ifndef CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
| 16 #define CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 16 #define CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
| 17 | 17 |
| 18 #include <string> | 18 #include <string> |
| 19 | 19 |
| 20 #include "chrome/browser/autocomplete/autocomplete.h" | 20 #include "chrome/browser/autocomplete/autocomplete.h" |
| 21 #include "chrome/common/notification_registrar.h" |
| 21 | 22 |
| 22 class Profile; | 23 class Profile; |
| 23 class TemplateURL; | 24 class TemplateURL; |
| 24 class TemplateURLModel; | 25 class TemplateURLModel; |
| 25 | 26 |
| 26 // Autocomplete provider for keyword input. | 27 // Autocomplete provider for keyword input. |
| 27 // | 28 // |
| 28 // After construction, the autocomplete controller repeatedly calls Start() | 29 // After construction, the autocomplete controller repeatedly calls Start() |
| 29 // with some user input, each time expecting to receive a small set of the best | 30 // with some user input, each time expecting to receive a small set of the best |
| 30 // matches (either synchronously or asynchronously). | 31 // matches (either synchronously or asynchronously). |
| 31 // | 32 // |
| 32 // To construct these matches, the provider treats user input as a series of | 33 // To construct these matches, the provider treats user input as a series of |
| 33 // whitespace-delimited tokens and tries to match the first token as the prefix | 34 // whitespace-delimited tokens and tries to match the first token as the prefix |
| 34 // of a known "keyword". A keyword is some string that maps to a search query | 35 // of a known "keyword". A keyword is some string that maps to a search query |
| 35 // URL; the rest of the user's input is taken as the input to the query. For | 36 // URL; the rest of the user's input is taken as the input to the query. For |
| 36 // example, the keyword "bug" might map to the URL "http://b/issue?id=%s", so | 37 // example, the keyword "bug" might map to the URL "http://b/issue?id=%s", so |
| 37 // input like "bug 123" would become "http://b/issue?id=123". | 38 // input like "bug 123" would become "http://b/issue?id=123". |
| 38 // | 39 // |
| 39 // Because we do prefix matching, user input could match more than one keyword | 40 // Because we do prefix matching, user input could match more than one keyword |
| 40 // at once. (Example: the input "f jazz" matches all keywords starting with | 41 // at once. (Example: the input "f jazz" matches all keywords starting with |
| 41 // "f".) We return the best matches, up to three. | 42 // "f".) We return the best matches, up to three. |
| 42 // | 43 // |
| 43 // The resulting matches are shown with content specified by the keyword | 44 // The resulting matches are shown with content specified by the keyword |
| 44 // (usually "Search [name] for %s"), description "(Keyword: [keyword])", and | 45 // (usually "Search [name] for %s"), description "(Keyword: [keyword])", and |
| 45 // action "[keyword] %s". If the user has typed a (possibly partial) keyword | 46 // action "[keyword] %s". If the user has typed a (possibly partial) keyword |
| 46 // but no search terms, the suggested result is shown greyed out, with | 47 // but no search terms, the suggested result is shown greyed out, with |
| 47 // "<enter term(s)>" as the substituted input, and does nothing when selected. | 48 // "<enter term(s)>" as the substituted input, and does nothing when selected. |
| 48 class KeywordProvider : public AutocompleteProvider { | 49 class KeywordProvider : |
| 50 public AutocompleteProvider, |
| 51 public NotificationObserver { |
| 49 public: | 52 public: |
| 50 KeywordProvider(ACProviderListener* listener, Profile* profile); | 53 KeywordProvider(ACProviderListener* listener, Profile* profile); |
| 51 // For testing. | 54 // For testing. |
| 52 KeywordProvider(ACProviderListener* listener, TemplateURLModel* model); | 55 KeywordProvider(ACProviderListener* listener, TemplateURLModel* model); |
| 53 | 56 |
| 54 // Returns the replacement string from the user input. The replacement | 57 // Returns the replacement string from the user input. The replacement |
| 55 // string is the portion of the input that does not contain the keyword. | 58 // string is the portion of the input that does not contain the keyword. |
| 56 // For example, the replacement string for "b blah" is blah. | 59 // For example, the replacement string for "b blah" is blah. |
| 57 static std::wstring SplitReplacementStringFromInput( | 60 static std::wstring SplitReplacementStringFromInput( |
| 58 const std::wstring& input); | 61 const std::wstring& input); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 | 99 |
| 97 // Determines the relevance for some input, given its type, whether the user | 100 // Determines the relevance for some input, given its type, whether the user |
| 98 // typed the complete keyword, and whether the keyword needs query text (true | 101 // typed the complete keyword, and whether the keyword needs query text (true |
| 99 // if the keyword supports replacement and the user isn't in "prefer keyword | 102 // if the keyword supports replacement and the user isn't in "prefer keyword |
| 100 // matches" mode). | 103 // matches" mode). |
| 101 static int CalculateRelevance(AutocompleteInput::Type type, | 104 static int CalculateRelevance(AutocompleteInput::Type type, |
| 102 bool complete, | 105 bool complete, |
| 103 bool no_query_text_needed); | 106 bool no_query_text_needed); |
| 104 | 107 |
| 105 // Creates a fully marked-up AutocompleteMatch from the user's input. | 108 // Creates a fully marked-up AutocompleteMatch from the user's input. |
| 109 // If |relevance| is negative, calculate a relevance based on heuristics. |
| 106 AutocompleteMatch CreateAutocompleteMatch( | 110 AutocompleteMatch CreateAutocompleteMatch( |
| 107 TemplateURLModel* model, | 111 TemplateURLModel* model, |
| 108 const std::wstring keyword, | 112 const std::wstring keyword, |
| 109 const AutocompleteInput& input, | 113 const AutocompleteInput& input, |
| 110 size_t prefix_length, | 114 size_t prefix_length, |
| 111 const std::wstring& remaining_input); | 115 const std::wstring& remaining_input, |
| 116 int relevance); |
| 117 |
| 118 // NotificationObserver interface. |
| 119 void Observe(NotificationType type, |
| 120 const NotificationSource& source, |
| 121 const NotificationDetails& details); |
| 112 | 122 |
| 113 // Model for the keywords. This is only non-null when testing, otherwise the | 123 // Model for the keywords. This is only non-null when testing, otherwise the |
| 114 // TemplateURLModel from the Profile is used. | 124 // TemplateURLModel from the Profile is used. |
| 115 TemplateURLModel* model_; | 125 TemplateURLModel* model_; |
| 116 | 126 |
| 127 // Identifies the current input state. This is incremented each time the |
| 128 // autocomplete edit's input changes in any way. It is used to tell whether |
| 129 // suggest results from the extension are current. |
| 130 int current_input_id_; |
| 131 |
| 132 // The input state at the time we last asked the extension for suggest |
| 133 // results. |
| 134 AutocompleteInput extension_suggest_last_input_; |
| 135 |
| 136 // We remember the last suggestions we've received from the extension in case |
| 137 // we need to reset our matches without asking the extension again. |
| 138 std::vector<AutocompleteMatch> extension_suggest_matches_; |
| 139 |
| 140 NotificationRegistrar registrar_; |
| 141 |
| 117 DISALLOW_EVIL_CONSTRUCTORS(KeywordProvider); | 142 DISALLOW_EVIL_CONSTRUCTORS(KeywordProvider); |
| 118 }; | 143 }; |
| 119 | 144 |
| 120 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 145 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
| OLD | NEW |