OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/strings/string16.h" | 10 #include "base/strings/string16.h" |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 | 210 |
211 return keyword; | 211 return keyword; |
212 } | 212 } |
213 | 213 |
214 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( | 214 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( |
215 const string16& text, | 215 const string16& text, |
216 const string16& keyword, | 216 const string16& keyword, |
217 const AutocompleteInput& input) { | 217 const AutocompleteInput& input) { |
218 return CreateAutocompleteMatch( | 218 return CreateAutocompleteMatch( |
219 GetTemplateURLService()->GetTemplateURLForKeyword(keyword), input, | 219 GetTemplateURLService()->GetTemplateURLForKeyword(keyword), input, |
220 keyword.length(), SplitReplacementStringFromInput(text, true), 0); | 220 keyword.length(), SplitReplacementStringFromInput(text, true), true, 0); |
msw
2013/07/23 21:55:33
It's not obvious why this file has true or false s
Mark P
2013/07/26 16:48:13
Sure. I'll clarify in this comment; you can decid
msw
2013/07/26 20:04:49
I think your comments are insightful enough that t
Mark P
2013/07/26 23:55:24
Added comments in those other three areas as reque
| |
221 } | 221 } |
222 | 222 |
223 void KeywordProvider::Start(const AutocompleteInput& input, | 223 void KeywordProvider::Start(const AutocompleteInput& input, |
224 bool minimal_changes) { | 224 bool minimal_changes) { |
225 // This object ensures we end keyword mode if we exit the function without | 225 // This object ensures we end keyword mode if we exit the function without |
226 // toggling keyword mode to on. | 226 // toggling keyword mode to on. |
227 ScopedEndExtensionKeywordMode keyword_mode_toggle(this); | 227 ScopedEndExtensionKeywordMode keyword_mode_toggle(this); |
228 | 228 |
229 matches_.clear(); | 229 matches_.clear(); |
230 | 230 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 // this is an extension keyword. If |remaining_input| is a | 312 // this is an extension keyword. If |remaining_input| is a |
313 // non-empty non-extension keyword (i.e., a regular keyword that | 313 // non-empty non-extension keyword (i.e., a regular keyword that |
314 // supports replacement and that has extra text following it), | 314 // supports replacement and that has extra text following it), |
315 // then SearchProvider creates the exact (a.k.a. verbatim) match. | 315 // then SearchProvider creates the exact (a.k.a. verbatim) match. |
316 if (!remaining_input.empty() && !is_extension_keyword) | 316 if (!remaining_input.empty() && !is_extension_keyword) |
317 return; | 317 return; |
318 | 318 |
319 // TODO(pkasting): We should probably check that if the user explicitly | 319 // TODO(pkasting): We should probably check that if the user explicitly |
320 // typed a scheme, that scheme matches the one in |template_url|. | 320 // typed a scheme, that scheme matches the one in |template_url|. |
321 matches_.push_back(CreateAutocompleteMatch( | 321 matches_.push_back(CreateAutocompleteMatch( |
322 template_url, input, keyword.length(), remaining_input, -1)); | 322 template_url, input, keyword.length(), remaining_input, true, -1)); |
323 | 323 |
324 if (profile_ && is_extension_keyword) { | 324 if (profile_ && is_extension_keyword) { |
325 if (input.matches_requested() == AutocompleteInput::ALL_MATCHES) { | 325 if (input.matches_requested() == AutocompleteInput::ALL_MATCHES) { |
326 if (template_url->GetExtensionId() != current_keyword_extension_id_) | 326 if (template_url->GetExtensionId() != current_keyword_extension_id_) |
327 MaybeEndExtensionKeywordMode(); | 327 MaybeEndExtensionKeywordMode(); |
328 if (current_keyword_extension_id_.empty()) | 328 if (current_keyword_extension_id_.empty()) |
329 EnterExtensionKeywordMode(template_url->GetExtensionId()); | 329 EnterExtensionKeywordMode(template_url->GetExtensionId()); |
330 keyword_mode_toggle.StayInKeywordMode(); | 330 keyword_mode_toggle.StayInKeywordMode(); |
331 } | 331 } |
332 | 332 |
(...skipping 25 matching lines...) Expand all Loading... | |
358 if (have_listeners) | 358 if (have_listeners) |
359 done_ = false; | 359 done_ = false; |
360 } | 360 } |
361 } | 361 } |
362 } else { | 362 } else { |
363 if (matches.size() > kMaxMatches) | 363 if (matches.size() > kMaxMatches) |
364 matches.erase(matches.begin() + kMaxMatches, matches.end()); | 364 matches.erase(matches.begin() + kMaxMatches, matches.end()); |
365 for (TemplateURLService::TemplateURLVector::const_iterator i( | 365 for (TemplateURLService::TemplateURLVector::const_iterator i( |
366 matches.begin()); i != matches.end(); ++i) { | 366 matches.begin()); i != matches.end(); ++i) { |
367 matches_.push_back(CreateAutocompleteMatch( | 367 matches_.push_back(CreateAutocompleteMatch( |
368 *i, input, keyword.length(), remaining_input, -1)); | 368 *i, input, keyword.length(), remaining_input, false, -1)); |
369 } | 369 } |
370 } | 370 } |
371 } | 371 } |
372 | 372 |
373 void KeywordProvider::Stop(bool clear_cached_results) { | 373 void KeywordProvider::Stop(bool clear_cached_results) { |
374 done_ = true; | 374 done_ = true; |
375 MaybeEndExtensionKeywordMode(); | 375 MaybeEndExtensionKeywordMode(); |
376 } | 376 } |
377 | 377 |
378 KeywordProvider::~KeywordProvider() {} | 378 KeywordProvider::~KeywordProvider() {} |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 return 1500; | 413 return 1500; |
414 return (allow_exact_keyword_match && (type == AutocompleteInput::QUERY)) ? | 414 return (allow_exact_keyword_match && (type == AutocompleteInput::QUERY)) ? |
415 1450 : 1100; | 415 1450 : 1100; |
416 } | 416 } |
417 | 417 |
418 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( | 418 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( |
419 const TemplateURL* template_url, | 419 const TemplateURL* template_url, |
420 const AutocompleteInput& input, | 420 const AutocompleteInput& input, |
421 size_t prefix_length, | 421 size_t prefix_length, |
422 const string16& remaining_input, | 422 const string16& remaining_input, |
423 bool allowed_to_be_default_match, | |
423 int relevance) { | 424 int relevance) { |
424 DCHECK(template_url); | 425 DCHECK(template_url); |
425 const bool supports_replacement = | 426 const bool supports_replacement = |
426 template_url->url_ref().SupportsReplacement(); | 427 template_url->url_ref().SupportsReplacement(); |
427 | 428 |
428 // Create an edit entry of "[keyword] [remaining input]". This is helpful | 429 // Create an edit entry of "[keyword] [remaining input]". This is helpful |
429 // even when [remaining input] is empty, as the user can select the popup | 430 // even when [remaining input] is empty, as the user can select the popup |
430 // choice and immediately begin typing in query input. | 431 // choice and immediately begin typing in query input. |
431 const string16& keyword = template_url->keyword(); | 432 const string16& keyword = template_url->keyword(); |
432 const bool keyword_complete = (prefix_length == keyword.length()); | 433 const bool keyword_complete = (prefix_length == keyword.length()); |
433 if (relevance < 0) { | 434 if (relevance < 0) { |
434 relevance = | 435 relevance = |
435 CalculateRelevance(input.type(), keyword_complete, | 436 CalculateRelevance(input.type(), keyword_complete, |
436 // When the user wants keyword matches to take | 437 // When the user wants keyword matches to take |
437 // preference, score them highly regardless of | 438 // preference, score them highly regardless of |
438 // whether the input provides query text. | 439 // whether the input provides query text. |
439 supports_replacement, input.prefer_keyword(), | 440 supports_replacement, input.prefer_keyword(), |
440 input.allow_exact_keyword_match()); | 441 input.allow_exact_keyword_match()); |
441 } | 442 } |
442 AutocompleteMatch match(this, relevance, false, | 443 AutocompleteMatch match(this, relevance, false, |
443 supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE : | 444 supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE : |
444 AutocompleteMatchType::HISTORY_KEYWORD); | 445 AutocompleteMatchType::HISTORY_KEYWORD); |
446 match.allowed_to_be_default_match = allowed_to_be_default_match; | |
445 match.fill_into_edit = keyword; | 447 match.fill_into_edit = keyword; |
446 if (!remaining_input.empty() || !keyword_complete || supports_replacement) | 448 if (!remaining_input.empty() || !keyword_complete || supports_replacement) |
447 match.fill_into_edit.push_back(L' '); | 449 match.fill_into_edit.push_back(L' '); |
448 match.fill_into_edit.append(remaining_input); | 450 match.fill_into_edit.append(remaining_input); |
449 // If we wanted to set |result.inline_autocompletion| correctly, we'd need | 451 // If we wanted to set |result.inline_autocompletion| correctly, we'd need |
450 // CleanUserInputKeyword() to return the amount of adjustment it's made to | 452 // CleanUserInputKeyword() to return the amount of adjustment it's made to |
451 // the user's input. Because right now inexact keyword matches can't score | 453 // the user's input. Because right now inexact keyword matches can't score |
452 // more highly than a "what you typed" match from one of the other providers, | 454 // more highly than a "what you typed" match from one of the other providers, |
453 // we just don't bother to do this, and leave inline autocompletion off. | 455 // we just don't bother to do this, and leave inline autocompletion off. |
454 | 456 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
566 *suggestions.suggest_results[i]; | 568 *suggestions.suggest_results[i]; |
567 // We want to order these suggestions in descending order, so start with | 569 // We want to order these suggestions in descending order, so start with |
568 // the relevance of the first result (added synchronously in Start()), | 570 // the relevance of the first result (added synchronously in Start()), |
569 // and subtract 1 for each subsequent suggestion from the extension. | 571 // and subtract 1 for each subsequent suggestion from the extension. |
570 // We know that |complete| is true, because we wouldn't get results from | 572 // We know that |complete| is true, because we wouldn't get results from |
571 // the extension unless the full keyword had been typed. | 573 // the extension unless the full keyword had been typed. |
572 int first_relevance = CalculateRelevance(input.type(), true, true, | 574 int first_relevance = CalculateRelevance(input.type(), true, true, |
573 input.prefer_keyword(), input.allow_exact_keyword_match()); | 575 input.prefer_keyword(), input.allow_exact_keyword_match()); |
574 extension_suggest_matches_.push_back(CreateAutocompleteMatch( | 576 extension_suggest_matches_.push_back(CreateAutocompleteMatch( |
575 template_url, input, keyword.length(), | 577 template_url, input, keyword.length(), |
576 UTF8ToUTF16(suggestion.content), first_relevance - (i + 1))); | 578 UTF8ToUTF16(suggestion.content), false, first_relevance - (i + 1))); |
577 | 579 |
578 AutocompleteMatch* match = &extension_suggest_matches_.back(); | 580 AutocompleteMatch* match = &extension_suggest_matches_.back(); |
579 match->contents.assign(UTF8ToUTF16(suggestion.description)); | 581 match->contents.assign(UTF8ToUTF16(suggestion.description)); |
580 match->contents_class = | 582 match->contents_class = |
581 extensions::StyleTypesToACMatchClassifications(suggestion); | 583 extensions::StyleTypesToACMatchClassifications(suggestion); |
582 match->description.clear(); | 584 match->description.clear(); |
583 match->description_class.clear(); | 585 match->description_class.clear(); |
584 } | 586 } |
585 | 587 |
586 done_ = true; | 588 done_ = true; |
(...skipping 29 matching lines...) Expand all Loading... | |
616 } | 618 } |
617 | 619 |
618 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 620 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
619 if (!current_keyword_extension_id_.empty()) { | 621 if (!current_keyword_extension_id_.empty()) { |
620 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( | 622 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( |
621 profile_, current_keyword_extension_id_); | 623 profile_, current_keyword_extension_id_); |
622 | 624 |
623 current_keyword_extension_id_.clear(); | 625 current_keyword_extension_id_.clear(); |
624 } | 626 } |
625 } | 627 } |
OLD | NEW |