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