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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 return string16(); | 208 return string16(); |
209 } | 209 } |
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 // This enclosing function is only called by omnibox_controller.cc to create | |
219 // a what-you-typed match on the keyword input. It's okay to allow this | |
220 // match to be the default match. | |
H Fung
2013/08/08 20:10:36
I'm not sure if it's worth changing the function n
Mark P
2013/08/08 22:06:10
A reasonable idea. I think it makes things cleare
Peter Kasting
2013/08/09 21:59:34
Does not seem changed?
Mark P
2013/08/09 22:24:11
Already did; you didn't review the most recent pat
| |
218 return CreateAutocompleteMatch( | 221 return CreateAutocompleteMatch( |
219 GetTemplateURLService()->GetTemplateURLForKeyword(keyword), input, | 222 GetTemplateURLService()->GetTemplateURLForKeyword(keyword), input, |
220 keyword.length(), SplitReplacementStringFromInput(text, true), 0); | 223 keyword.length(), SplitReplacementStringFromInput(text, true), true, 0); |
221 } | 224 } |
222 | 225 |
223 void KeywordProvider::Start(const AutocompleteInput& input, | 226 void KeywordProvider::Start(const AutocompleteInput& input, |
224 bool minimal_changes) { | 227 bool minimal_changes) { |
225 // This object ensures we end keyword mode if we exit the function without | 228 // This object ensures we end keyword mode if we exit the function without |
226 // toggling keyword mode to on. | 229 // toggling keyword mode to on. |
227 ScopedEndExtensionKeywordMode keyword_mode_toggle(this); | 230 ScopedEndExtensionKeywordMode keyword_mode_toggle(this); |
228 | 231 |
229 matches_.clear(); | 232 matches_.clear(); |
230 | 233 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
311 // Only create an exact match if |remaining_input| is empty or if | 314 // Only create an exact match if |remaining_input| is empty or if |
312 // this is an extension keyword. If |remaining_input| is a | 315 // this is an extension keyword. If |remaining_input| is a |
313 // non-empty non-extension keyword (i.e., a regular keyword that | 316 // non-empty non-extension keyword (i.e., a regular keyword that |
314 // supports replacement and that has extra text following it), | 317 // supports replacement and that has extra text following it), |
315 // then SearchProvider creates the exact (a.k.a. verbatim) match. | 318 // then SearchProvider creates the exact (a.k.a. verbatim) match. |
316 if (!remaining_input.empty() && !is_extension_keyword) | 319 if (!remaining_input.empty() && !is_extension_keyword) |
317 return; | 320 return; |
318 | 321 |
319 // TODO(pkasting): We should probably check that if the user explicitly | 322 // TODO(pkasting): We should probably check that if the user explicitly |
320 // typed a scheme, that scheme matches the one in |template_url|. | 323 // typed a scheme, that scheme matches the one in |template_url|. |
324 | |
325 // When creating an exact match (either for the keyword itself, no | |
326 // remaining query or an extension keyword, possibly with remaining | |
327 // input), allow the match to be the default match. | |
321 matches_.push_back(CreateAutocompleteMatch( | 328 matches_.push_back(CreateAutocompleteMatch( |
322 template_url, input, keyword.length(), remaining_input, -1)); | 329 template_url, input, keyword.length(), remaining_input, true, -1)); |
323 | 330 |
324 if (profile_ && is_extension_keyword) { | 331 if (profile_ && is_extension_keyword) { |
325 if (input.matches_requested() == AutocompleteInput::ALL_MATCHES) { | 332 if (input.matches_requested() == AutocompleteInput::ALL_MATCHES) { |
326 if (template_url->GetExtensionId() != current_keyword_extension_id_) | 333 if (template_url->GetExtensionId() != current_keyword_extension_id_) |
327 MaybeEndExtensionKeywordMode(); | 334 MaybeEndExtensionKeywordMode(); |
328 if (current_keyword_extension_id_.empty()) | 335 if (current_keyword_extension_id_.empty()) |
329 EnterExtensionKeywordMode(template_url->GetExtensionId()); | 336 EnterExtensionKeywordMode(template_url->GetExtensionId()); |
330 keyword_mode_toggle.StayInKeywordMode(); | 337 keyword_mode_toggle.StayInKeywordMode(); |
331 } | 338 } |
332 | 339 |
(...skipping 25 matching lines...) Expand all Loading... | |
358 if (have_listeners) | 365 if (have_listeners) |
359 done_ = false; | 366 done_ = false; |
360 } | 367 } |
361 } | 368 } |
362 } else { | 369 } else { |
363 if (matches.size() > kMaxMatches) | 370 if (matches.size() > kMaxMatches) |
364 matches.erase(matches.begin() + kMaxMatches, matches.end()); | 371 matches.erase(matches.begin() + kMaxMatches, matches.end()); |
365 for (TemplateURLService::TemplateURLVector::const_iterator i( | 372 for (TemplateURLService::TemplateURLVector::const_iterator i( |
366 matches.begin()); i != matches.end(); ++i) { | 373 matches.begin()); i != matches.end(); ++i) { |
367 matches_.push_back(CreateAutocompleteMatch( | 374 matches_.push_back(CreateAutocompleteMatch( |
368 *i, input, keyword.length(), remaining_input, -1)); | 375 *i, input, keyword.length(), remaining_input, false, -1)); |
369 } | 376 } |
370 } | 377 } |
371 } | 378 } |
372 | 379 |
373 void KeywordProvider::Stop(bool clear_cached_results) { | 380 void KeywordProvider::Stop(bool clear_cached_results) { |
374 done_ = true; | 381 done_ = true; |
375 MaybeEndExtensionKeywordMode(); | 382 MaybeEndExtensionKeywordMode(); |
376 } | 383 } |
377 | 384 |
378 KeywordProvider::~KeywordProvider() {} | 385 KeywordProvider::~KeywordProvider() {} |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 return 1500; | 420 return 1500; |
414 return (allow_exact_keyword_match && (type == AutocompleteInput::QUERY)) ? | 421 return (allow_exact_keyword_match && (type == AutocompleteInput::QUERY)) ? |
415 1450 : 1100; | 422 1450 : 1100; |
416 } | 423 } |
417 | 424 |
418 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( | 425 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( |
419 const TemplateURL* template_url, | 426 const TemplateURL* template_url, |
420 const AutocompleteInput& input, | 427 const AutocompleteInput& input, |
421 size_t prefix_length, | 428 size_t prefix_length, |
422 const string16& remaining_input, | 429 const string16& remaining_input, |
430 bool allowed_to_be_default_match, | |
423 int relevance) { | 431 int relevance) { |
424 DCHECK(template_url); | 432 DCHECK(template_url); |
425 const bool supports_replacement = | 433 const bool supports_replacement = |
426 template_url->url_ref().SupportsReplacement(); | 434 template_url->url_ref().SupportsReplacement(); |
427 | 435 |
428 // Create an edit entry of "[keyword] [remaining input]". This is helpful | 436 // 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 | 437 // even when [remaining input] is empty, as the user can select the popup |
430 // choice and immediately begin typing in query input. | 438 // choice and immediately begin typing in query input. |
431 const string16& keyword = template_url->keyword(); | 439 const string16& keyword = template_url->keyword(); |
432 const bool keyword_complete = (prefix_length == keyword.length()); | 440 const bool keyword_complete = (prefix_length == keyword.length()); |
433 if (relevance < 0) { | 441 if (relevance < 0) { |
434 relevance = | 442 relevance = |
435 CalculateRelevance(input.type(), keyword_complete, | 443 CalculateRelevance(input.type(), keyword_complete, |
436 // When the user wants keyword matches to take | 444 // When the user wants keyword matches to take |
437 // preference, score them highly regardless of | 445 // preference, score them highly regardless of |
438 // whether the input provides query text. | 446 // whether the input provides query text. |
439 supports_replacement, input.prefer_keyword(), | 447 supports_replacement, input.prefer_keyword(), |
440 input.allow_exact_keyword_match()); | 448 input.allow_exact_keyword_match()); |
441 } | 449 } |
442 AutocompleteMatch match(this, relevance, false, | 450 AutocompleteMatch match(this, relevance, false, |
443 supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE : | 451 supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE : |
444 AutocompleteMatchType::HISTORY_KEYWORD); | 452 AutocompleteMatchType::HISTORY_KEYWORD); |
453 match.allowed_to_be_default_match = allowed_to_be_default_match; | |
445 match.fill_into_edit = keyword; | 454 match.fill_into_edit = keyword; |
446 if (!remaining_input.empty() || !keyword_complete || supports_replacement) | 455 if (!remaining_input.empty() || !keyword_complete || supports_replacement) |
447 match.fill_into_edit.push_back(L' '); | 456 match.fill_into_edit.push_back(L' '); |
448 match.fill_into_edit.append(remaining_input); | 457 match.fill_into_edit.append(remaining_input); |
449 // If we wanted to set |result.inline_autocompletion| correctly, we'd need | 458 // If we wanted to set |result.inline_autocompletion| correctly, we'd need |
450 // CleanUserInputKeyword() to return the amount of adjustment it's made to | 459 // 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 | 460 // 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, | 461 // 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. | 462 // we just don't bother to do this, and leave inline autocompletion off. |
454 | 463 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
560 model->GetTemplateURLForKeyword(keyword); | 569 model->GetTemplateURLForKeyword(keyword); |
561 | 570 |
562 // TODO(mpcomplete): consider clamping the number of suggestions to | 571 // TODO(mpcomplete): consider clamping the number of suggestions to |
563 // AutocompleteProvider::kMaxMatches. | 572 // AutocompleteProvider::kMaxMatches. |
564 for (size_t i = 0; i < suggestions.suggest_results.size(); ++i) { | 573 for (size_t i = 0; i < suggestions.suggest_results.size(); ++i) { |
565 const omnibox_api::SuggestResult& suggestion = | 574 const omnibox_api::SuggestResult& suggestion = |
566 *suggestions.suggest_results[i]; | 575 *suggestions.suggest_results[i]; |
567 // We want to order these suggestions in descending order, so start with | 576 // We want to order these suggestions in descending order, so start with |
568 // the relevance of the first result (added synchronously in Start()), | 577 // the relevance of the first result (added synchronously in Start()), |
569 // and subtract 1 for each subsequent suggestion from the extension. | 578 // and subtract 1 for each subsequent suggestion from the extension. |
570 // We know that |complete| is true, because we wouldn't get results from | 579 // We recompute the first match's relevance; we know that |complete| |
571 // the extension unless the full keyword had been typed. | 580 // is true, because we wouldn't get results from the extension unless |
581 // the full keyword had been typed. | |
572 int first_relevance = CalculateRelevance(input.type(), true, true, | 582 int first_relevance = CalculateRelevance(input.type(), true, true, |
573 input.prefer_keyword(), input.allow_exact_keyword_match()); | 583 input.prefer_keyword(), input.allow_exact_keyword_match()); |
584 // Because these matches are async, we should never let them become the | |
585 // default match, lest we introduce race conditions in the omnibox user | |
586 // interaction. | |
574 extension_suggest_matches_.push_back(CreateAutocompleteMatch( | 587 extension_suggest_matches_.push_back(CreateAutocompleteMatch( |
575 template_url, input, keyword.length(), | 588 template_url, input, keyword.length(), |
576 UTF8ToUTF16(suggestion.content), first_relevance - (i + 1))); | 589 UTF8ToUTF16(suggestion.content), false, first_relevance - (i + 1))); |
577 | 590 |
578 AutocompleteMatch* match = &extension_suggest_matches_.back(); | 591 AutocompleteMatch* match = &extension_suggest_matches_.back(); |
579 match->contents.assign(UTF8ToUTF16(suggestion.description)); | 592 match->contents.assign(UTF8ToUTF16(suggestion.description)); |
580 match->contents_class = | 593 match->contents_class = |
581 extensions::StyleTypesToACMatchClassifications(suggestion); | 594 extensions::StyleTypesToACMatchClassifications(suggestion); |
582 match->description.clear(); | 595 match->description.clear(); |
583 match->description_class.clear(); | 596 match->description_class.clear(); |
584 } | 597 } |
585 | 598 |
586 done_ = true; | 599 done_ = true; |
(...skipping 29 matching lines...) Expand all Loading... | |
616 } | 629 } |
617 | 630 |
618 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 631 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
619 if (!current_keyword_extension_id_.empty()) { | 632 if (!current_keyword_extension_id_.empty()) { |
620 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( | 633 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( |
621 profile_, current_keyword_extension_id_); | 634 profile_, current_keyword_extension_id_); |
622 | 635 |
623 current_keyword_extension_id_.clear(); | 636 current_keyword_extension_id_.clear(); |
624 } | 637 } |
625 } | 638 } |
OLD | NEW |