| OLD | NEW |
| 1 // Copyright (c) 2010 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 // |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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, TemplateURLModel* model); | 56 KeywordProvider(ACProviderListener* listener, TemplateURLModel* model); |
| 57 | 57 |
| 58 // Returns the replacement string from the user input. The replacement | 58 // Returns the replacement string from the user input. The replacement |
| 59 // string is the portion of the input that does not contain the keyword. | 59 // string is the portion of the input that does not contain the keyword. |
| 60 // For example, the replacement string for "b blah" is blah. | 60 // For example, the replacement string for "b blah" is blah. |
| 61 // If |trim_leading_whitespace| is true then leading whitespace in | 61 // If |trim_leading_whitespace| is true then leading whitespace in |
| 62 // replacement string will be trimmed. | 62 // replacement string will be trimmed. |
| 63 static string16 SplitReplacementStringFromInput( | 63 static std::wstring SplitReplacementStringFromInput( |
| 64 const string16& input, | 64 const std::wstring& input, |
| 65 bool trim_leading_whitespace); | 65 bool trim_leading_whitespace); |
| 66 | 66 |
| 67 // Returns the matching substituting keyword for |input|, or NULL if there | 67 // Returns the matching substituting keyword for |input|, or NULL if there |
| 68 // is no keyword for the specified input. | 68 // is no keyword for the specified input. |
| 69 static const TemplateURL* GetSubstitutingTemplateURLForInput( | 69 static const TemplateURL* GetSubstitutingTemplateURLForInput( |
| 70 Profile* profile, | 70 Profile* profile, |
| 71 const AutocompleteInput& input, | 71 const AutocompleteInput& input, |
| 72 string16* remaining_input); | 72 std::wstring* remaining_input); |
| 73 | 73 |
| 74 // AutocompleteProvider | 74 // AutocompleteProvider |
| 75 virtual void Start(const AutocompleteInput& input, bool minimal_changes); | 75 virtual void Start(const AutocompleteInput& input, bool minimal_changes); |
| 76 virtual void Stop(); | 76 virtual void Stop(); |
| 77 | 77 |
| 78 private: | 78 private: |
| 79 class ScopedEndExtensionKeywordMode; | 79 class ScopedEndExtensionKeywordMode; |
| 80 friend class ScopedEndExtensionKeywordMode; | 80 friend class ScopedEndExtensionKeywordMode; |
| 81 | 81 |
| 82 virtual ~KeywordProvider(); | 82 virtual ~KeywordProvider(); |
| 83 | 83 |
| 84 // Extracts the keyword from |input| into |keyword|. Any remaining characters | 84 // Extracts the keyword from |input| into |keyword|. Any remaining characters |
| 85 // after the keyword are placed in |remaining_input|. Returns true if |input| | 85 // after the keyword are placed in |remaining_input|. Returns true if |input| |
| 86 // is valid and has a keyword. This makes use of SplitKeywordFromInput to | 86 // is valid and has a keyword. This makes use of SplitKeywordFromInput to |
| 87 // extract the keyword and remaining string, and uses | 87 // extract the keyword and remaining string, and uses |
| 88 // TemplateURLModel::CleanUserInputKeyword to remove unnecessary characters. | 88 // TemplateURLModel::CleanUserInputKeyword to remove unnecessary characters. |
| 89 // In general use this instead of SplitKeywordFromInput. | 89 // In general use this instead of SplitKeywordFromInput. |
| 90 // Leading whitespace in |*remaining_input| will be trimmed. | 90 // Leading whitespace in |*remaining_input| will be trimmed. |
| 91 static bool ExtractKeywordFromInput(const AutocompleteInput& input, | 91 static bool ExtractKeywordFromInput(const AutocompleteInput& input, |
| 92 string16* keyword, | 92 std::wstring* keyword, |
| 93 string16* remaining_input); | 93 std::wstring* remaining_input); |
| 94 | 94 |
| 95 // Extracts the next whitespace-delimited token from input and returns it. | 95 // Extracts the next whitespace-delimited token from input and returns it. |
| 96 // Sets |remaining_input| to everything after the first token (skipping over | 96 // Sets |remaining_input| to everything after the first token (skipping over |
| 97 // the first intervening whitespace). | 97 // the first intervening whitespace). |
| 98 // If |trim_leading_whitespace| is true then leading whitespace in | 98 // If |trim_leading_whitespace| is true then leading whitespace in |
| 99 // |*remaining_input| will be trimmed. | 99 // |*remaining_input| will be trimmed. |
| 100 static string16 SplitKeywordFromInput(const string16& input, | 100 static std::wstring SplitKeywordFromInput(const std::wstring& input, |
| 101 bool trim_leading_whitespace, | 101 bool trim_leading_whitespace, |
| 102 string16* remaining_input); | 102 std::wstring* remaining_input); |
| 103 | 103 |
| 104 // Fills in the "destination_url" and "contents" fields of |match| with the | 104 // Fills in the "destination_url" and "contents" fields of |match| with the |
| 105 // provided user input and keyword data. | 105 // provided user input and keyword data. |
| 106 static void FillInURLAndContents( | 106 static void FillInURLAndContents( |
| 107 const string16& remaining_input, | 107 const std::wstring& remaining_input, |
| 108 const TemplateURL* element, | 108 const TemplateURL* element, |
| 109 AutocompleteMatch* match); | 109 AutocompleteMatch* match); |
| 110 | 110 |
| 111 // Determines the relevance for some input, given its type, whether the user | 111 // Determines the relevance for some input, given its type, whether the user |
| 112 // typed the complete keyword, and whether the keyword needs query text (true | 112 // typed the complete keyword, and whether the keyword needs query text (true |
| 113 // if the keyword supports replacement and the user isn't in "prefer keyword | 113 // if the keyword supports replacement and the user isn't in "prefer keyword |
| 114 // matches" mode). | 114 // matches" mode). |
| 115 // If |allow_exact_keyword_match| is false, the relevance for complete | 115 // If |allow_exact_keyword_match| is false, the relevance for complete |
| 116 // keywords is degraded. | 116 // keywords is degraded. |
| 117 static int CalculateRelevance(AutocompleteInput::Type type, | 117 static int CalculateRelevance(AutocompleteInput::Type type, |
| 118 bool complete, | 118 bool complete, |
| 119 bool no_query_text_needed, | 119 bool no_query_text_needed, |
| 120 bool allow_exact_keyword_match); | 120 bool allow_exact_keyword_match); |
| 121 | 121 |
| 122 // Creates a fully marked-up AutocompleteMatch from the user's input. | 122 // Creates a fully marked-up AutocompleteMatch from the user's input. |
| 123 // If |relevance| is negative, calculate a relevance based on heuristics. | 123 // If |relevance| is negative, calculate a relevance based on heuristics. |
| 124 AutocompleteMatch CreateAutocompleteMatch( | 124 AutocompleteMatch CreateAutocompleteMatch( |
| 125 TemplateURLModel* model, | 125 TemplateURLModel* model, |
| 126 const string16& keyword, | 126 const std::wstring& keyword, |
| 127 const AutocompleteInput& input, | 127 const AutocompleteInput& input, |
| 128 size_t prefix_length, | 128 size_t prefix_length, |
| 129 const string16& remaining_input, | 129 const std::wstring& remaining_input, |
| 130 int relevance); | 130 int relevance); |
| 131 | 131 |
| 132 void EnterExtensionKeywordMode(const std::string& extension_id); | 132 void EnterExtensionKeywordMode(const std::string& extension_id); |
| 133 void MaybeEndExtensionKeywordMode(); | 133 void MaybeEndExtensionKeywordMode(); |
| 134 | 134 |
| 135 // NotificationObserver interface. | 135 // NotificationObserver interface. |
| 136 virtual void Observe(NotificationType type, | 136 virtual void Observe(NotificationType type, |
| 137 const NotificationSource& source, | 137 const NotificationSource& source, |
| 138 const NotificationDetails& details); | 138 const NotificationDetails& details); |
| 139 | 139 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 157 // If non-empty, holds the ID of the extension whose keyword is currently in | 157 // If non-empty, holds the ID of the extension whose keyword is currently in |
| 158 // the URL bar while the autocomplete popup is open. | 158 // the URL bar while the autocomplete popup is open. |
| 159 std::string current_keyword_extension_id_; | 159 std::string current_keyword_extension_id_; |
| 160 | 160 |
| 161 NotificationRegistrar registrar_; | 161 NotificationRegistrar registrar_; |
| 162 | 162 |
| 163 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); | 163 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); |
| 164 }; | 164 }; |
| 165 | 165 |
| 166 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 166 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
| OLD | NEW |