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 | |
67 // Returns the replacement string from the user input. The replacement | 58 // Returns the replacement string from the user input. The replacement |
68 // 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. |
69 // For example, the replacement string for "b blah" is blah. | 60 // For example, the replacement string for "b blah" is blah. |
70 // If |trim_leading_whitespace| is true then leading whitespace in | 61 // If |trim_leading_whitespace| is true then leading whitespace in |
71 // replacement string will be trimmed. | 62 // replacement string will be trimmed. |
72 static string16 SplitReplacementStringFromInput( | 63 static string16 SplitReplacementStringFromInput( |
73 const string16& input, | 64 const string16& input, |
74 bool trim_leading_whitespace); | 65 bool trim_leading_whitespace); |
75 | 66 |
76 // Returns the matching substituting keyword for |input|, or NULL if there | 67 // Returns the matching substituting keyword for |input|, or NULL if there |
77 // is no keyword for the specified input. | 68 // is no keyword for the specified input. |
78 static const TemplateURL* GetSubstitutingTemplateURLForInput( | 69 static const TemplateURL* GetSubstitutingTemplateURLForInput( |
79 Profile* profile, | 70 Profile* profile, |
80 const AutocompleteInput& input, | 71 const AutocompleteInput& input, |
81 string16* remaining_input); | 72 string16* remaining_input); |
82 | 73 |
83 // If |text| corresponds (in the sense of | |
84 // TemplateURLModel::CleanUserInputKeyword()) to an enabled, substituting | |
85 // keyword, returns that keyword; returns the empty string otherwise. | |
86 string16 GetKeywordForText(const string16& text) const; | |
87 | |
88 // Creates a fully marked-up AutocompleteMatch for a specific keyword. | |
89 AutocompleteMatch CreateAutocompleteMatch( | |
90 const string16& text, | |
91 const string16& keyword, | |
92 const AutocompleteInput& input); | |
93 | |
94 // AutocompleteProvider | 74 // AutocompleteProvider |
95 virtual void Start(const AutocompleteInput& input, | 75 virtual void Start(const AutocompleteInput& input, |
96 bool minimal_changes) OVERRIDE; | 76 bool minimal_changes) OVERRIDE; |
97 virtual void Stop() OVERRIDE; | 77 virtual void Stop() OVERRIDE; |
98 | 78 |
99 private: | 79 private: |
100 class ScopedEndExtensionKeywordMode; | 80 class ScopedEndExtensionKeywordMode; |
101 friend class ScopedEndExtensionKeywordMode; | 81 friend class ScopedEndExtensionKeywordMode; |
102 | 82 |
103 virtual ~KeywordProvider(); | 83 virtual ~KeywordProvider(); |
104 | 84 |
105 // Extracts the keyword from |input| into |keyword|. Any remaining characters | 85 // Extracts the keyword from |input| into |keyword|. Any remaining characters |
106 // after the keyword are placed in |remaining_input|. Returns true if |input| | 86 // after the keyword are placed in |remaining_input|. Returns true if |input| |
107 // is valid and has a keyword. This makes use of SplitKeywordFromInput to | 87 // is valid and has a keyword. This makes use of SplitKeywordFromInput to |
108 // extract the keyword and remaining string, and uses | 88 // extract the keyword and remaining string, and uses |
109 // TemplateURLService::CleanUserInputKeyword to remove unnecessary characters. | 89 // TemplateURLService::CleanUserInputKeyword to remove unnecessary characters. |
110 // In general use this instead of SplitKeywordFromInput. | 90 // In general use this instead of SplitKeywordFromInput. |
111 // Leading whitespace in |*remaining_input| will be trimmed. | 91 // Leading whitespace in |*remaining_input| will be trimmed. |
112 static bool ExtractKeywordFromInput(const AutocompleteInput& input, | 92 static bool ExtractKeywordFromInput(const AutocompleteInput& input, |
113 string16* keyword, | 93 string16* keyword, |
114 string16* remaining_input); | 94 string16* remaining_input); |
115 | 95 |
| 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 |
116 // Fills in the "destination_url" and "contents" fields of |match| with the | 105 // Fills in the "destination_url" and "contents" fields of |match| with the |
117 // provided user input and keyword data. | 106 // provided user input and keyword data. |
118 static void FillInURLAndContents( | 107 static void FillInURLAndContents( |
119 Profile* profile, | 108 Profile* profile, |
120 const string16& remaining_input, | 109 const string16& remaining_input, |
121 const TemplateURL* element, | 110 const TemplateURL* element, |
122 AutocompleteMatch* match); | 111 AutocompleteMatch* match); |
123 | 112 |
124 // Determines the relevance for some input, given its type, whether the user | 113 // Determines the relevance for some input, given its type, whether the user |
125 // typed the complete keyword, and whether the user is in "prefer keyword | 114 // typed the complete keyword, and whether the user is in "prefer keyword |
(...skipping 17 matching lines...) Expand all Loading... |
143 int relevance); | 132 int relevance); |
144 | 133 |
145 void EnterExtensionKeywordMode(const std::string& extension_id); | 134 void EnterExtensionKeywordMode(const std::string& extension_id); |
146 void MaybeEndExtensionKeywordMode(); | 135 void MaybeEndExtensionKeywordMode(); |
147 | 136 |
148 // content::NotificationObserver interface. | 137 // content::NotificationObserver interface. |
149 virtual void Observe(int type, | 138 virtual void Observe(int type, |
150 const content::NotificationSource& source, | 139 const content::NotificationSource& source, |
151 const content::NotificationDetails& details) OVERRIDE; | 140 const content::NotificationDetails& details) OVERRIDE; |
152 | 141 |
153 TemplateURLService* GetTemplateURLService() const; | |
154 | |
155 // Model for the keywords. This is only non-null when testing, otherwise the | 142 // Model for the keywords. This is only non-null when testing, otherwise the |
156 // TemplateURLService from the Profile is used. | 143 // TemplateURLService from the Profile is used. |
157 TemplateURLService* model_; | 144 TemplateURLService* model_; |
158 | 145 |
159 // Identifies the current input state. This is incremented each time the | 146 // Identifies the current input state. This is incremented each time the |
160 // autocomplete edit's input changes in any way. It is used to tell whether | 147 // autocomplete edit's input changes in any way. It is used to tell whether |
161 // suggest results from the extension are current. | 148 // suggest results from the extension are current. |
162 int current_input_id_; | 149 int current_input_id_; |
163 | 150 |
164 // The input state at the time we last asked the extension for suggest | 151 // The input state at the time we last asked the extension for suggest |
165 // results. | 152 // results. |
166 AutocompleteInput extension_suggest_last_input_; | 153 AutocompleteInput extension_suggest_last_input_; |
167 | 154 |
168 // We remember the last suggestions we've received from the extension in case | 155 // We remember the last suggestions we've received from the extension in case |
169 // we need to reset our matches without asking the extension again. | 156 // we need to reset our matches without asking the extension again. |
170 std::vector<AutocompleteMatch> extension_suggest_matches_; | 157 std::vector<AutocompleteMatch> extension_suggest_matches_; |
171 | 158 |
172 // If non-empty, holds the ID of the extension whose keyword is currently in | 159 // If non-empty, holds the ID of the extension whose keyword is currently in |
173 // the URL bar while the autocomplete popup is open. | 160 // the URL bar while the autocomplete popup is open. |
174 std::string current_keyword_extension_id_; | 161 std::string current_keyword_extension_id_; |
175 | 162 |
176 content::NotificationRegistrar registrar_; | 163 content::NotificationRegistrar registrar_; |
177 | 164 |
178 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); | 165 DISALLOW_COPY_AND_ASSIGN(KeywordProvider); |
179 }; | 166 }; |
180 | 167 |
181 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ | 168 #endif // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_ |
OLD | NEW |