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 #include "chrome/browser/autocomplete/keyword_provider.h" | 5 #include "chrome/browser/autocomplete/keyword_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/string16.h" | 10 #include "base/string16.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 } | 34 } |
35 | 35 |
36 void StayInKeywordMode() { | 36 void StayInKeywordMode() { |
37 provider_ = NULL; | 37 provider_ = NULL; |
38 } | 38 } |
39 private: | 39 private: |
40 KeywordProvider* provider_; | 40 KeywordProvider* provider_; |
41 }; | 41 }; |
42 | 42 |
43 // static | 43 // static |
44 std::wstring KeywordProvider::SplitReplacementStringFromInput( | 44 string16 KeywordProvider::SplitReplacementStringFromInput( |
45 const std::wstring& input, | 45 const string16& input, |
46 bool trim_leading_whitespace) { | 46 bool trim_leading_whitespace) { |
47 // The input may contain leading whitespace, strip it. | 47 // The input may contain leading whitespace, strip it. |
48 std::wstring trimmed_input; | 48 string16 trimmed_input; |
49 TrimWhitespace(input, TRIM_LEADING, &trimmed_input); | 49 TrimWhitespace(input, TRIM_LEADING, &trimmed_input); |
50 | 50 |
51 // And extract the replacement string. | 51 // And extract the replacement string. |
52 std::wstring remaining_input; | 52 string16 remaining_input; |
53 SplitKeywordFromInput(trimmed_input, trim_leading_whitespace, | 53 SplitKeywordFromInput(trimmed_input, trim_leading_whitespace, |
54 &remaining_input); | 54 &remaining_input); |
55 return remaining_input; | 55 return remaining_input; |
56 } | 56 } |
57 | 57 |
58 KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile) | 58 KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile) |
59 : AutocompleteProvider(listener, profile, "Keyword"), | 59 : AutocompleteProvider(listener, profile, "Keyword"), |
60 model_(NULL), | 60 model_(NULL), |
61 current_input_id_(0) { | 61 current_input_id_(0) { |
62 // Extension suggestions always come from the original profile, since that's | 62 // Extension suggestions always come from the original profile, since that's |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // We need our input IDs to be unique across all profiles, so we keep a global | 100 // We need our input IDs to be unique across all profiles, so we keep a global |
101 // UID that each provider uses. | 101 // UID that each provider uses. |
102 static int global_input_uid_; | 102 static int global_input_uid_; |
103 | 103 |
104 } // namespace | 104 } // namespace |
105 | 105 |
106 // static | 106 // static |
107 const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput( | 107 const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput( |
108 Profile* profile, | 108 Profile* profile, |
109 const AutocompleteInput& input, | 109 const AutocompleteInput& input, |
110 std::wstring* remaining_input) { | 110 string16* remaining_input) { |
111 if (!input.allow_exact_keyword_match()) | 111 if (!input.allow_exact_keyword_match()) |
112 return NULL; | 112 return NULL; |
113 | 113 |
114 std::wstring keyword; | 114 string16 keyword; |
115 if (!ExtractKeywordFromInput(input, &keyword, remaining_input)) | 115 if (!ExtractKeywordFromInput(input, &keyword, remaining_input)) |
116 return NULL; | 116 return NULL; |
117 | 117 |
118 // Make sure the model is loaded. This is cheap and quickly bails out if | 118 // Make sure the model is loaded. This is cheap and quickly bails out if |
119 // the model is already loaded. | 119 // the model is already loaded. |
120 TemplateURLModel* model = profile->GetTemplateURLModel(); | 120 TemplateURLModel* model = profile->GetTemplateURLModel(); |
121 DCHECK(model); | 121 DCHECK(model); |
122 model->Load(); | 122 model->Load(); |
123 | 123 |
124 const TemplateURL* template_url = | 124 const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword); |
125 model->GetTemplateURLForKeyword(WideToUTF16Hack(keyword)); | |
126 return TemplateURL::SupportsReplacement(template_url) ? template_url : NULL; | 125 return TemplateURL::SupportsReplacement(template_url) ? template_url : NULL; |
127 } | 126 } |
128 | 127 |
129 void KeywordProvider::Start(const AutocompleteInput& input, | 128 void KeywordProvider::Start(const AutocompleteInput& input, |
130 bool minimal_changes) { | 129 bool minimal_changes) { |
131 // This object ensures we end keyword mode if we exit the function without | 130 // This object ensures we end keyword mode if we exit the function without |
132 // toggling keyword mode to on. | 131 // toggling keyword mode to on. |
133 ScopedEndExtensionKeywordMode keyword_mode_toggle(this); | 132 ScopedEndExtensionKeywordMode keyword_mode_toggle(this); |
134 | 133 |
135 matches_.clear(); | 134 matches_.clear(); |
(...skipping 12 matching lines...) Expand all Loading... |
148 // the assumption that they might not realize they no longer need to go to a | 147 // the assumption that they might not realize they no longer need to go to a |
149 // site to be able to search it. So we call CleanUserInputKeyword() to strip | 148 // site to be able to search it. So we call CleanUserInputKeyword() to strip |
150 // any initial scheme and/or "www.". NOTE: Any heuristics or UI used to | 149 // any initial scheme and/or "www.". NOTE: Any heuristics or UI used to |
151 // automatically/manually create keywords will need to be in sync with | 150 // automatically/manually create keywords will need to be in sync with |
152 // whatever we do here! | 151 // whatever we do here! |
153 // | 152 // |
154 // TODO(pkasting): http://b/1112681 If someday we remember usage frequency for | 153 // TODO(pkasting): http://b/1112681 If someday we remember usage frequency for |
155 // keywords, we might suggest keywords that haven't even been partially typed, | 154 // keywords, we might suggest keywords that haven't even been partially typed, |
156 // if the user uses them enough and isn't obviously typing something else. In | 155 // if the user uses them enough and isn't obviously typing something else. In |
157 // this case we'd consider all input here to be query input. | 156 // this case we'd consider all input here to be query input. |
158 std::wstring keyword, remaining_input; | 157 string16 keyword, remaining_input; |
159 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) | 158 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) |
160 return; | 159 return; |
161 | 160 |
162 // Make sure the model is loaded. This is cheap and quickly bails out if | 161 // Make sure the model is loaded. This is cheap and quickly bails out if |
163 // the model is already loaded. | 162 // the model is already loaded. |
164 TemplateURLModel* model = profile_ ? profile_->GetTemplateURLModel() : model_; | 163 TemplateURLModel* model = profile_ ? profile_->GetTemplateURLModel() : model_; |
165 DCHECK(model); | 164 DCHECK(model); |
166 model->Load(); | 165 model->Load(); |
167 | 166 |
168 // Get the best matches for this keyword. | 167 // Get the best matches for this keyword. |
169 // | 168 // |
170 // NOTE: We could cache the previous keywords and reuse them here in the | 169 // NOTE: We could cache the previous keywords and reuse them here in the |
171 // |minimal_changes| case, but since we'd still have to recalculate their | 170 // |minimal_changes| case, but since we'd still have to recalculate their |
172 // relevances and we can just recreate the results synchronously anyway, we | 171 // relevances and we can just recreate the results synchronously anyway, we |
173 // don't bother. | 172 // don't bother. |
174 // | 173 // |
175 // TODO(pkasting): http://b/893701 We should remember the user's use of a | 174 // TODO(pkasting): http://b/893701 We should remember the user's use of a |
176 // search query both from the autocomplete popup and from web pages | 175 // search query both from the autocomplete popup and from web pages |
177 // themselves. | 176 // themselves. |
178 std::vector<string16> keyword_matches; | 177 std::vector<string16> keyword_matches; |
179 model->FindMatchingKeywords(WideToUTF16Hack(keyword), | 178 model->FindMatchingKeywords(keyword, |
180 !remaining_input.empty(), | 179 !remaining_input.empty(), |
181 &keyword_matches); | 180 &keyword_matches); |
182 | 181 |
183 // Prune any extension keywords that are disallowed in incognito mode (if | 182 // Prune any extension keywords that are disallowed in incognito mode (if |
184 // we're incognito), or disabled. | 183 // we're incognito), or disabled. |
185 for (std::vector<string16>::iterator i(keyword_matches.begin()); | 184 for (std::vector<string16>::iterator i(keyword_matches.begin()); |
186 i != keyword_matches.end(); ) { | 185 i != keyword_matches.end(); ) { |
187 const TemplateURL* template_url(model->GetTemplateURLForKeyword(*i)); | 186 const TemplateURL* template_url(model->GetTemplateURLForKeyword(*i)); |
188 if (profile_ && | 187 if (profile_ && |
189 !input.synchronous_only() && template_url->IsExtensionKeyword()) { | 188 !input.synchronous_only() && template_url->IsExtensionKeyword()) { |
(...skipping 10 matching lines...) Expand all Loading... |
200 ++i; | 199 ++i; |
201 } | 200 } |
202 if (keyword_matches.empty()) | 201 if (keyword_matches.empty()) |
203 return; | 202 return; |
204 std::sort(keyword_matches.begin(), keyword_matches.end(), CompareQuality()); | 203 std::sort(keyword_matches.begin(), keyword_matches.end(), CompareQuality()); |
205 | 204 |
206 // Limit to one exact or three inexact matches, and mark them up for display | 205 // Limit to one exact or three inexact matches, and mark them up for display |
207 // in the autocomplete popup. | 206 // in the autocomplete popup. |
208 // Any exact match is going to be the highest quality match, and thus at the | 207 // Any exact match is going to be the highest quality match, and thus at the |
209 // front of our vector. | 208 // front of our vector. |
210 if (keyword_matches.front() == WideToUTF16Hack(keyword)) { | 209 if (keyword_matches.front() == keyword) { |
211 const TemplateURL* template_url( | 210 const TemplateURL* template_url(model->GetTemplateURLForKeyword(keyword)); |
212 model->GetTemplateURLForKeyword(WideToUTF16Hack(keyword))); | |
213 // TODO(pkasting): We should probably check that if the user explicitly | 211 // TODO(pkasting): We should probably check that if the user explicitly |
214 // typed a scheme, that scheme matches the one in |template_url|. | 212 // typed a scheme, that scheme matches the one in |template_url|. |
215 matches_.push_back(CreateAutocompleteMatch(model, keyword, input, | 213 matches_.push_back(CreateAutocompleteMatch(model, keyword, input, |
216 keyword.length(), | 214 keyword.length(), |
217 remaining_input, -1)); | 215 remaining_input, -1)); |
218 | 216 |
219 if (profile_ && | 217 if (profile_ && |
220 !input.synchronous_only() && template_url->IsExtensionKeyword()) { | 218 !input.synchronous_only() && template_url->IsExtensionKeyword()) { |
221 if (template_url->GetExtensionId() != current_keyword_extension_id_) | 219 if (template_url->GetExtensionId() != current_keyword_extension_id_) |
222 MaybeEndExtensionKeywordMode(); | 220 MaybeEndExtensionKeywordMode(); |
223 if (current_keyword_extension_id_.empty()) | 221 if (current_keyword_extension_id_.empty()) |
224 EnterExtensionKeywordMode(template_url->GetExtensionId()); | 222 EnterExtensionKeywordMode(template_url->GetExtensionId()); |
225 keyword_mode_toggle.StayInKeywordMode(); | 223 keyword_mode_toggle.StayInKeywordMode(); |
226 | 224 |
227 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, | 225 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, |
228 WideToUTF16(remaining_input), | 226 remaining_input, |
229 &matches_[0]); | 227 &matches_[0]); |
230 | 228 |
231 if (minimal_changes) { | 229 if (minimal_changes) { |
232 // If the input hasn't significantly changed, we can just use the | 230 // If the input hasn't significantly changed, we can just use the |
233 // suggestions from last time. We need to readjust the relevance to | 231 // suggestions from last time. We need to readjust the relevance to |
234 // ensure it is less than the main match's relevance. | 232 // ensure it is less than the main match's relevance. |
235 for (size_t i = 0; i < extension_suggest_matches_.size(); ++i) { | 233 for (size_t i = 0; i < extension_suggest_matches_.size(); ++i) { |
236 matches_.push_back(extension_suggest_matches_[i]); | 234 matches_.push_back(extension_suggest_matches_[i]); |
237 matches_.back().relevance = matches_[0].relevance - (i + 1); | 235 matches_.back().relevance = matches_[0].relevance - (i + 1); |
238 } | 236 } |
239 } else { | 237 } else { |
240 extension_suggest_last_input_ = input; | 238 extension_suggest_last_input_ = input; |
241 extension_suggest_matches_.clear(); | 239 extension_suggest_matches_.clear(); |
242 | 240 |
243 bool have_listeners = ExtensionOmniboxEventRouter::OnInputChanged( | 241 bool have_listeners = ExtensionOmniboxEventRouter::OnInputChanged( |
244 profile_, template_url->GetExtensionId(), | 242 profile_, template_url->GetExtensionId(), |
245 WideToUTF8(remaining_input), current_input_id_); | 243 UTF16ToUTF8(remaining_input), current_input_id_); |
246 | 244 |
247 // We only have to wait for suggest results if there are actually | 245 // We only have to wait for suggest results if there are actually |
248 // extensions listening for input changes. | 246 // extensions listening for input changes. |
249 if (have_listeners) | 247 if (have_listeners) |
250 done_ = false; | 248 done_ = false; |
251 } | 249 } |
252 } | 250 } |
253 } else { | 251 } else { |
254 if (keyword_matches.size() > kMaxMatches) { | 252 if (keyword_matches.size() > kMaxMatches) { |
255 keyword_matches.erase(keyword_matches.begin() + kMaxMatches, | 253 keyword_matches.erase(keyword_matches.begin() + kMaxMatches, |
256 keyword_matches.end()); | 254 keyword_matches.end()); |
257 } | 255 } |
258 for (std::vector<string16>::const_iterator i(keyword_matches.begin()); | 256 for (std::vector<string16>::const_iterator i(keyword_matches.begin()); |
259 i != keyword_matches.end(); ++i) { | 257 i != keyword_matches.end(); ++i) { |
260 matches_.push_back(CreateAutocompleteMatch(model, UTF16ToWideHack(*i), | 258 matches_.push_back(CreateAutocompleteMatch(model, *i, |
261 input, keyword.length(), | 259 input, keyword.length(), |
262 remaining_input, -1)); | 260 remaining_input, -1)); |
263 } | 261 } |
264 } | 262 } |
265 } | 263 } |
266 | 264 |
267 void KeywordProvider::Stop() { | 265 void KeywordProvider::Stop() { |
268 done_ = true; | 266 done_ = true; |
269 MaybeEndExtensionKeywordMode(); | 267 MaybeEndExtensionKeywordMode(); |
270 } | 268 } |
271 | 269 |
272 KeywordProvider::~KeywordProvider() {} | 270 KeywordProvider::~KeywordProvider() {} |
273 | 271 |
274 // static | 272 // static |
275 bool KeywordProvider::ExtractKeywordFromInput(const AutocompleteInput& input, | 273 bool KeywordProvider::ExtractKeywordFromInput(const AutocompleteInput& input, |
276 std::wstring* keyword, | 274 string16* keyword, |
277 std::wstring* remaining_input) { | 275 string16* remaining_input) { |
278 if ((input.type() == AutocompleteInput::INVALID) || | 276 if ((input.type() == AutocompleteInput::INVALID) || |
279 (input.type() == AutocompleteInput::FORCED_QUERY)) | 277 (input.type() == AutocompleteInput::FORCED_QUERY)) |
280 return false; | 278 return false; |
281 | 279 |
282 *keyword = | 280 *keyword = TemplateURLModel::CleanUserInputKeyword( |
283 UTF16ToWideHack(TemplateURLModel::CleanUserInputKeyword(WideToUTF16Hack( | 281 SplitKeywordFromInput(input.text(), true, remaining_input)); |
284 SplitKeywordFromInput(input.text(), true, remaining_input)))); | |
285 return !keyword->empty(); | 282 return !keyword->empty(); |
286 } | 283 } |
287 | 284 |
288 // static | 285 // static |
289 std::wstring KeywordProvider::SplitKeywordFromInput( | 286 string16 KeywordProvider::SplitKeywordFromInput( |
290 const std::wstring& input, | 287 const string16& input, |
291 bool trim_leading_whitespace, | 288 bool trim_leading_whitespace, |
292 std::wstring* remaining_input) { | 289 string16* remaining_input) { |
293 // Find end of first token. The AutocompleteController has trimmed leading | 290 // Find end of first token. The AutocompleteController has trimmed leading |
294 // whitespace, so we need not skip over that. | 291 // whitespace, so we need not skip over that. |
295 const size_t first_white(input.find_first_of(kWhitespaceWide)); | 292 const size_t first_white(input.find_first_of(kWhitespaceUTF16)); |
296 DCHECK_NE(0U, first_white); | 293 DCHECK_NE(0U, first_white); |
297 if (first_white == std::wstring::npos) | 294 if (first_white == string16::npos) |
298 return input; // Only one token provided. | 295 return input; // Only one token provided. |
299 | 296 |
300 // Set |remaining_input| to everything after the first token. | 297 // Set |remaining_input| to everything after the first token. |
301 DCHECK(remaining_input != NULL); | 298 DCHECK(remaining_input != NULL); |
302 const size_t remaining_start = trim_leading_whitespace ? | 299 const size_t remaining_start = trim_leading_whitespace ? |
303 input.find_first_not_of(kWhitespaceWide, first_white) : first_white + 1; | 300 input.find_first_not_of(kWhitespaceUTF16, first_white) : first_white + 1; |
304 | 301 |
305 if (remaining_start < input.length()) | 302 if (remaining_start < input.length()) |
306 remaining_input->assign(input.begin() + remaining_start, input.end()); | 303 remaining_input->assign(input.begin() + remaining_start, input.end()); |
307 | 304 |
308 // Return first token as keyword. | 305 // Return first token as keyword. |
309 return input.substr(0, first_white); | 306 return input.substr(0, first_white); |
310 } | 307 } |
311 | 308 |
312 // static | 309 // static |
313 void KeywordProvider::FillInURLAndContents( | 310 void KeywordProvider::FillInURLAndContents( |
314 const std::wstring& remaining_input, | 311 const string16& remaining_input, |
315 const TemplateURL* element, | 312 const TemplateURL* element, |
316 AutocompleteMatch* match) { | 313 AutocompleteMatch* match) { |
317 DCHECK(!element->short_name().empty()); | 314 DCHECK(!element->short_name().empty()); |
318 DCHECK(element->url()); | 315 DCHECK(element->url()); |
319 DCHECK(element->url()->IsValid()); | 316 DCHECK(element->url()->IsValid()); |
320 int message_id = element->IsExtensionKeyword() ? | 317 int message_id = element->IsExtensionKeyword() ? |
321 IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH; | 318 IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH; |
322 if (remaining_input.empty()) { | 319 if (remaining_input.empty()) { |
323 // Allow extension keyword providers to accept empty string input. This is | 320 // Allow extension keyword providers to accept empty string input. This is |
324 // useful to allow extensions to do something in the case where no input is | 321 // useful to allow extensions to do something in the case where no input is |
325 // entered. | 322 // entered. |
326 if (element->url()->SupportsReplacement() && | 323 if (element->url()->SupportsReplacement() && |
327 !element->IsExtensionKeyword()) { | 324 !element->IsExtensionKeyword()) { |
328 // No query input; return a generic, no-destination placeholder. | 325 // No query input; return a generic, no-destination placeholder. |
329 match->contents.assign(UTF16ToWideHack( | 326 match->contents.assign( |
330 l10n_util::GetStringFUTF16(message_id, | 327 l10n_util::GetStringFUTF16(message_id, |
331 element->AdjustedShortNameForLocaleDirection(), | 328 element->AdjustedShortNameForLocaleDirection(), |
332 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)))); | 329 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE))); |
333 match->contents_class.push_back( | 330 match->contents_class.push_back( |
334 ACMatchClassification(0, ACMatchClassification::DIM)); | 331 ACMatchClassification(0, ACMatchClassification::DIM)); |
335 } else { | 332 } else { |
336 // Keyword that has no replacement text (aka a shorthand for a URL). | 333 // Keyword that has no replacement text (aka a shorthand for a URL). |
337 match->destination_url = GURL(element->url()->url()); | 334 match->destination_url = GURL(element->url()->url()); |
338 match->contents.assign(UTF16ToWideHack(element->short_name())); | 335 match->contents.assign(element->short_name()); |
339 AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(), | 336 AutocompleteMatch::ClassifyLocationInString(0, match->contents.length(), |
340 match->contents.length(), ACMatchClassification::NONE, | 337 match->contents.length(), ACMatchClassification::NONE, |
341 &match->contents_class); | 338 &match->contents_class); |
342 } | 339 } |
343 } else { | 340 } else { |
344 // Create destination URL by escaping user input and substituting into | 341 // Create destination URL by escaping user input and substituting into |
345 // keyword template URL. The escaping here handles whitespace in user | 342 // keyword template URL. The escaping here handles whitespace in user |
346 // input, but we rely on later canonicalization functions to do more | 343 // input, but we rely on later canonicalization functions to do more |
347 // fixup to make the URL valid if necessary. | 344 // fixup to make the URL valid if necessary. |
348 DCHECK(element->url()->SupportsReplacement()); | 345 DCHECK(element->url()->SupportsReplacement()); |
349 match->destination_url = GURL(element->url()->ReplaceSearchTerms( | 346 match->destination_url = GURL(element->url()->ReplaceSearchTerms( |
350 *element, WideToUTF16Hack(remaining_input), | 347 *element, remaining_input, |
351 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 348 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
352 std::vector<size_t> content_param_offsets; | 349 std::vector<size_t> content_param_offsets; |
353 match->contents.assign(UTF16ToWideHack( | 350 match->contents.assign(l10n_util::GetStringFUTF16(message_id, |
354 l10n_util::GetStringFUTF16(message_id, | 351 element->short_name(), |
355 element->short_name(), | 352 remaining_input, |
356 WideToUTF16Hack(remaining_input), | 353 &content_param_offsets)); |
357 &content_param_offsets))); | |
358 if (content_param_offsets.size() == 2) { | 354 if (content_param_offsets.size() == 2) { |
359 AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1], | 355 AutocompleteMatch::ClassifyLocationInString(content_param_offsets[1], |
360 remaining_input.length(), match->contents.length(), | 356 remaining_input.length(), match->contents.length(), |
361 ACMatchClassification::NONE, &match->contents_class); | 357 ACMatchClassification::NONE, &match->contents_class); |
362 } else { | 358 } else { |
363 // See comments on an identical NOTREACHED() in search_provider.cc. | 359 // See comments on an identical NOTREACHED() in search_provider.cc. |
364 NOTREACHED(); | 360 NOTREACHED(); |
365 } | 361 } |
366 } | 362 } |
367 } | 363 } |
368 | 364 |
369 // static | 365 // static |
370 int KeywordProvider::CalculateRelevance(AutocompleteInput::Type type, | 366 int KeywordProvider::CalculateRelevance(AutocompleteInput::Type type, |
371 bool complete, | 367 bool complete, |
372 bool no_query_text_needed, | 368 bool no_query_text_needed, |
373 bool allow_exact_keyword_match) { | 369 bool allow_exact_keyword_match) { |
374 if (!complete) | 370 if (!complete) |
375 return (type == AutocompleteInput::URL) ? 700 : 450; | 371 return (type == AutocompleteInput::URL) ? 700 : 450; |
376 if (!allow_exact_keyword_match) | 372 if (!allow_exact_keyword_match) |
377 return 1100; | 373 return 1100; |
378 if (no_query_text_needed) | 374 if (no_query_text_needed) |
379 return 1500; | 375 return 1500; |
380 return (type == AutocompleteInput::QUERY) ? 1450 : 1100; | 376 return (type == AutocompleteInput::QUERY) ? 1450 : 1100; |
381 } | 377 } |
382 | 378 |
383 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( | 379 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( |
384 TemplateURLModel* model, | 380 TemplateURLModel* model, |
385 const std::wstring& keyword, | 381 const string16& keyword, |
386 const AutocompleteInput& input, | 382 const AutocompleteInput& input, |
387 size_t prefix_length, | 383 size_t prefix_length, |
388 const std::wstring& remaining_input, | 384 const string16& remaining_input, |
389 int relevance) { | 385 int relevance) { |
390 DCHECK(model); | 386 DCHECK(model); |
391 // Get keyword data from data store. | 387 // Get keyword data from data store. |
392 const TemplateURL* element( | 388 const TemplateURL* element( |
393 model->GetTemplateURLForKeyword(WideToUTF16Hack(keyword))); | 389 model->GetTemplateURLForKeyword(keyword)); |
394 DCHECK(element && element->url()); | 390 DCHECK(element && element->url()); |
395 const bool supports_replacement = element->url()->SupportsReplacement(); | 391 const bool supports_replacement = element->url()->SupportsReplacement(); |
396 | 392 |
397 // Create an edit entry of "[keyword] [remaining input]". This is helpful | 393 // Create an edit entry of "[keyword] [remaining input]". This is helpful |
398 // even when [remaining input] is empty, as the user can select the popup | 394 // even when [remaining input] is empty, as the user can select the popup |
399 // choice and immediately begin typing in query input. | 395 // choice and immediately begin typing in query input. |
400 const bool keyword_complete = (prefix_length == keyword.length()); | 396 const bool keyword_complete = (prefix_length == keyword.length()); |
401 if (relevance < 0) { | 397 if (relevance < 0) { |
402 relevance = | 398 relevance = |
403 CalculateRelevance(input.type(), keyword_complete, | 399 CalculateRelevance(input.type(), keyword_complete, |
404 // When the user wants keyword matches to take | 400 // When the user wants keyword matches to take |
405 // preference, score them highly regardless of | 401 // preference, score them highly regardless of |
406 // whether the input provides query text. | 402 // whether the input provides query text. |
407 input.prefer_keyword() || !supports_replacement, | 403 input.prefer_keyword() || !supports_replacement, |
408 input.allow_exact_keyword_match()); | 404 input.allow_exact_keyword_match()); |
409 } | 405 } |
410 AutocompleteMatch result(this, relevance, false, | 406 AutocompleteMatch result(this, relevance, false, |
411 supports_replacement ? AutocompleteMatch::SEARCH_OTHER_ENGINE : | 407 supports_replacement ? AutocompleteMatch::SEARCH_OTHER_ENGINE : |
412 AutocompleteMatch::HISTORY_KEYWORD); | 408 AutocompleteMatch::HISTORY_KEYWORD); |
413 result.fill_into_edit.assign(keyword); | 409 result.fill_into_edit.assign(keyword); |
414 if (!remaining_input.empty() || !keyword_complete || supports_replacement) | 410 if (!remaining_input.empty() || !keyword_complete || supports_replacement) |
415 result.fill_into_edit.push_back(L' '); | 411 result.fill_into_edit.push_back(L' '); |
416 result.fill_into_edit.append(remaining_input); | 412 result.fill_into_edit.append(remaining_input); |
417 // If we wanted to set |result.inline_autocomplete_offset| correctly, we'd | 413 // If we wanted to set |result.inline_autocomplete_offset| correctly, we'd |
418 // need CleanUserInputKeyword() to return the amount of adjustment it's made | 414 // need CleanUserInputKeyword() to return the amount of adjustment it's made |
419 // to the user's input. Because right now inexact keyword matches can't score | 415 // to the user's input. Because right now inexact keyword matches can't score |
420 // more highly than a "what you typed" match from one of the other providers, | 416 // more highly than a "what you typed" match from one of the other providers, |
421 // we just don't bother to do this, and leave inline autocompletion off. | 417 // we just don't bother to do this, and leave inline autocompletion off. |
422 result.inline_autocomplete_offset = std::wstring::npos; | 418 result.inline_autocomplete_offset = string16::npos; |
423 | 419 |
424 // Create destination URL and popup entry content by substituting user input | 420 // Create destination URL and popup entry content by substituting user input |
425 // into keyword templates. | 421 // into keyword templates. |
426 FillInURLAndContents(remaining_input, element, &result); | 422 FillInURLAndContents(remaining_input, element, &result); |
427 | 423 |
428 if (supports_replacement) | 424 if (supports_replacement) |
429 result.template_url = element; | 425 result.template_url = element; |
430 result.transition = PageTransition::KEYWORD; | 426 result.transition = PageTransition::KEYWORD; |
431 | 427 |
432 // Create popup entry description based on the keyword name. | 428 // Create popup entry description based on the keyword name. |
433 if (!element->IsExtensionKeyword()) { | 429 if (!element->IsExtensionKeyword()) { |
434 result.description.assign(UTF16ToWideHack(l10n_util::GetStringFUTF16( | 430 result.description.assign(l10n_util::GetStringFUTF16( |
435 IDS_AUTOCOMPLETE_KEYWORD_DESCRIPTION, WideToUTF16Hack(keyword)))); | 431 IDS_AUTOCOMPLETE_KEYWORD_DESCRIPTION, keyword)); |
436 string16 keyword_desc( | 432 string16 keyword_desc( |
437 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_KEYWORD_DESCRIPTION)); | 433 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_KEYWORD_DESCRIPTION)); |
438 AutocompleteMatch::ClassifyLocationInString( | 434 AutocompleteMatch::ClassifyLocationInString( |
439 keyword_desc.find(ASCIIToUTF16("%s")), | 435 keyword_desc.find(ASCIIToUTF16("%s")), |
440 prefix_length, | 436 prefix_length, |
441 result.description.length(), | 437 result.description.length(), |
442 ACMatchClassification::DIM, | 438 ACMatchClassification::DIM, |
443 &result.description_class); | 439 &result.description_class); |
444 } | 440 } |
445 | 441 |
446 return result; | 442 return result; |
447 } | 443 } |
448 | 444 |
449 void KeywordProvider::Observe(NotificationType type, | 445 void KeywordProvider::Observe(NotificationType type, |
450 const NotificationSource& source, | 446 const NotificationSource& source, |
451 const NotificationDetails& details) { | 447 const NotificationDetails& details) { |
452 TemplateURLModel* model = profile_ ? profile_->GetTemplateURLModel() : model_; | 448 TemplateURLModel* model = profile_ ? profile_->GetTemplateURLModel() : model_; |
453 const AutocompleteInput& input = extension_suggest_last_input_; | 449 const AutocompleteInput& input = extension_suggest_last_input_; |
454 | 450 |
455 switch (type.value) { | 451 switch (type.value) { |
456 case NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED: | 452 case NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED: |
457 // Input has been accepted, so we're done with this input session. Ensure | 453 // Input has been accepted, so we're done with this input session. Ensure |
458 // we don't send the OnInputCancelled event. | 454 // we don't send the OnInputCancelled event. |
459 current_keyword_extension_id_.clear(); | 455 current_keyword_extension_id_.clear(); |
460 return; | 456 return; |
461 | 457 |
462 case NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: { | 458 case NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: { |
463 // It's possible to change the default suggestion while not in an editing | 459 // It's possible to change the default suggestion while not in an editing |
464 // session. | 460 // session. |
465 std::wstring keyword, remaining_input; | 461 string16 keyword, remaining_input; |
466 if (matches_.empty() || current_keyword_extension_id_.empty() || | 462 if (matches_.empty() || current_keyword_extension_id_.empty() || |
467 !ExtractKeywordFromInput(input, &keyword, &remaining_input)) | 463 !ExtractKeywordFromInput(input, &keyword, &remaining_input)) |
468 return; | 464 return; |
469 | 465 |
470 const TemplateURL* template_url( | 466 const TemplateURL* template_url( |
471 model->GetTemplateURLForKeyword(WideToUTF16Hack(keyword))); | 467 model->GetTemplateURLForKeyword(keyword)); |
472 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, | 468 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, |
473 WideToUTF16(remaining_input), | 469 remaining_input, |
474 &matches_[0]); | 470 &matches_[0]); |
475 listener_->OnProviderUpdate(true); | 471 listener_->OnProviderUpdate(true); |
476 return; | 472 return; |
477 } | 473 } |
478 | 474 |
479 case NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY: { | 475 case NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY: { |
480 const ExtensionOmniboxSuggestions& suggestions = | 476 const ExtensionOmniboxSuggestions& suggestions = |
481 *Details<ExtensionOmniboxSuggestions>(details).ptr(); | 477 *Details<ExtensionOmniboxSuggestions>(details).ptr(); |
482 if (suggestions.request_id != current_input_id_) | 478 if (suggestions.request_id != current_input_id_) |
483 return; // This is an old result. Just ignore. | 479 return; // This is an old result. Just ignore. |
484 | 480 |
485 std::wstring keyword, remaining_input; | 481 string16 keyword, remaining_input; |
486 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { | 482 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { |
487 NOTREACHED(); | 483 NOTREACHED(); |
488 return; | 484 return; |
489 } | 485 } |
490 | 486 |
491 // TODO(mpcomplete): consider clamping the number of suggestions to | 487 // TODO(mpcomplete): consider clamping the number of suggestions to |
492 // AutocompleteProvider::kMaxMatches. | 488 // AutocompleteProvider::kMaxMatches. |
493 for (size_t i = 0; i < suggestions.suggestions.size(); ++i) { | 489 for (size_t i = 0; i < suggestions.suggestions.size(); ++i) { |
494 const ExtensionOmniboxSuggestion& suggestion = | 490 const ExtensionOmniboxSuggestion& suggestion = |
495 suggestions.suggestions[i]; | 491 suggestions.suggestions[i]; |
496 // We want to order these suggestions in descending order, so start with | 492 // We want to order these suggestions in descending order, so start with |
497 // the relevance of the first result (added synchronously in Start()), | 493 // the relevance of the first result (added synchronously in Start()), |
498 // and subtract 1 for each subsequent suggestion from the extension. | 494 // and subtract 1 for each subsequent suggestion from the extension. |
499 // We know that |complete| is true, because we wouldn't get results from | 495 // We know that |complete| is true, because we wouldn't get results from |
500 // the extension unless the full keyword had been typed. | 496 // the extension unless the full keyword had been typed. |
501 int first_relevance = CalculateRelevance(input.type(), true, | 497 int first_relevance = CalculateRelevance(input.type(), true, |
502 input.prefer_keyword(), input.allow_exact_keyword_match()); | 498 input.prefer_keyword(), input.allow_exact_keyword_match()); |
503 extension_suggest_matches_.push_back(CreateAutocompleteMatch( | 499 extension_suggest_matches_.push_back(CreateAutocompleteMatch( |
504 model, keyword, input, keyword.length(), | 500 model, keyword, input, keyword.length(), |
505 UTF16ToWide(suggestion.content), first_relevance - (i + 1))); | 501 suggestion.content, first_relevance - (i + 1))); |
506 | 502 |
507 AutocompleteMatch* match = &extension_suggest_matches_.back(); | 503 AutocompleteMatch* match = &extension_suggest_matches_.back(); |
508 match->contents.assign(UTF16ToWide(suggestion.description)); | 504 match->contents.assign(suggestion.description); |
509 match->contents_class = suggestion.description_styles; | 505 match->contents_class = suggestion.description_styles; |
510 match->description.clear(); | 506 match->description.clear(); |
511 match->description_class.clear(); | 507 match->description_class.clear(); |
512 } | 508 } |
513 | 509 |
514 done_ = true; | 510 done_ = true; |
515 matches_.insert(matches_.end(), extension_suggest_matches_.begin(), | 511 matches_.insert(matches_.end(), extension_suggest_matches_.begin(), |
516 extension_suggest_matches_.end()); | 512 extension_suggest_matches_.end()); |
517 listener_->OnProviderUpdate(!extension_suggest_matches_.empty()); | 513 listener_->OnProviderUpdate(!extension_suggest_matches_.empty()); |
518 return; | 514 return; |
(...skipping 15 matching lines...) Expand all Loading... |
534 } | 530 } |
535 | 531 |
536 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 532 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
537 if (!current_keyword_extension_id_.empty()) { | 533 if (!current_keyword_extension_id_.empty()) { |
538 ExtensionOmniboxEventRouter::OnInputCancelled( | 534 ExtensionOmniboxEventRouter::OnInputCancelled( |
539 profile_, current_keyword_extension_id_); | 535 profile_, current_keyword_extension_id_); |
540 | 536 |
541 current_keyword_extension_id_.clear(); | 537 current_keyword_extension_id_.clear(); |
542 } | 538 } |
543 } | 539 } |
OLD | NEW |