| 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. |
| 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 24 matching lines...) Expand all Loading... |
| 357 // extensions listening for input changes. | 364 // extensions listening for input changes. |
| 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) { |
| 374 // These inexact (prefix) keyword matches probably could be allowed |
| 375 // to be the default match but in practice they're always outranked |
| 376 // by a verbatim match (a query from SearchProvider from non-URL-like |
| 377 // inputs or a URL-what-you-typed from HistoryURLProvider for URL-like |
| 378 // inputs). We set allowed_to_be_default_match to false to mirror |
| 379 // this implicit consequence of the relevance scoring code. |
| 367 matches_.push_back(CreateAutocompleteMatch( | 380 matches_.push_back(CreateAutocompleteMatch( |
| 368 *i, input, keyword.length(), remaining_input, -1)); | 381 *i, input, keyword.length(), remaining_input, false, -1)); |
| 369 } | 382 } |
| 370 } | 383 } |
| 371 } | 384 } |
| 372 | 385 |
| 373 void KeywordProvider::Stop(bool clear_cached_results) { | 386 void KeywordProvider::Stop(bool clear_cached_results) { |
| 374 done_ = true; | 387 done_ = true; |
| 375 MaybeEndExtensionKeywordMode(); | 388 MaybeEndExtensionKeywordMode(); |
| 376 } | 389 } |
| 377 | 390 |
| 378 KeywordProvider::~KeywordProvider() {} | 391 KeywordProvider::~KeywordProvider() {} |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 return 1500; | 426 return 1500; |
| 414 return (allow_exact_keyword_match && (type == AutocompleteInput::QUERY)) ? | 427 return (allow_exact_keyword_match && (type == AutocompleteInput::QUERY)) ? |
| 415 1450 : 1100; | 428 1450 : 1100; |
| 416 } | 429 } |
| 417 | 430 |
| 418 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( | 431 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( |
| 419 const TemplateURL* template_url, | 432 const TemplateURL* template_url, |
| 420 const AutocompleteInput& input, | 433 const AutocompleteInput& input, |
| 421 size_t prefix_length, | 434 size_t prefix_length, |
| 422 const string16& remaining_input, | 435 const string16& remaining_input, |
| 436 bool allowed_to_be_default_match, |
| 423 int relevance) { | 437 int relevance) { |
| 424 DCHECK(template_url); | 438 DCHECK(template_url); |
| 425 const bool supports_replacement = | 439 const bool supports_replacement = |
| 426 template_url->url_ref().SupportsReplacement(); | 440 template_url->url_ref().SupportsReplacement(); |
| 427 | 441 |
| 428 // Create an edit entry of "[keyword] [remaining input]". This is helpful | 442 // 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 | 443 // even when [remaining input] is empty, as the user can select the popup |
| 430 // choice and immediately begin typing in query input. | 444 // choice and immediately begin typing in query input. |
| 431 const string16& keyword = template_url->keyword(); | 445 const string16& keyword = template_url->keyword(); |
| 432 const bool keyword_complete = (prefix_length == keyword.length()); | 446 const bool keyword_complete = (prefix_length == keyword.length()); |
| 433 if (relevance < 0) { | 447 if (relevance < 0) { |
| 434 relevance = | 448 relevance = |
| 435 CalculateRelevance(input.type(), keyword_complete, | 449 CalculateRelevance(input.type(), keyword_complete, |
| 436 // When the user wants keyword matches to take | 450 // When the user wants keyword matches to take |
| 437 // preference, score them highly regardless of | 451 // preference, score them highly regardless of |
| 438 // whether the input provides query text. | 452 // whether the input provides query text. |
| 439 supports_replacement, input.prefer_keyword(), | 453 supports_replacement, input.prefer_keyword(), |
| 440 input.allow_exact_keyword_match()); | 454 input.allow_exact_keyword_match()); |
| 441 } | 455 } |
| 442 AutocompleteMatch match(this, relevance, false, | 456 AutocompleteMatch match(this, relevance, false, |
| 443 supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE : | 457 supports_replacement ? AutocompleteMatchType::SEARCH_OTHER_ENGINE : |
| 444 AutocompleteMatchType::HISTORY_KEYWORD); | 458 AutocompleteMatchType::HISTORY_KEYWORD); |
| 459 match.allowed_to_be_default_match = allowed_to_be_default_match; |
| 445 match.fill_into_edit = keyword; | 460 match.fill_into_edit = keyword; |
| 446 if (!remaining_input.empty() || !keyword_complete || supports_replacement) | 461 if (!remaining_input.empty() || !keyword_complete || supports_replacement) |
| 447 match.fill_into_edit.push_back(L' '); | 462 match.fill_into_edit.push_back(L' '); |
| 448 match.fill_into_edit.append(remaining_input); | 463 match.fill_into_edit.append(remaining_input); |
| 449 // If we wanted to set |result.inline_autocompletion| correctly, we'd need | 464 // If we wanted to set |result.inline_autocompletion| correctly, we'd need |
| 450 // CleanUserInputKeyword() to return the amount of adjustment it's made to | 465 // 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 | 466 // 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, | 467 // 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. | 468 // we just don't bother to do this, and leave inline autocompletion off. |
| 454 | 469 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 model->GetTemplateURLForKeyword(keyword); | 575 model->GetTemplateURLForKeyword(keyword); |
| 561 | 576 |
| 562 // TODO(mpcomplete): consider clamping the number of suggestions to | 577 // TODO(mpcomplete): consider clamping the number of suggestions to |
| 563 // AutocompleteProvider::kMaxMatches. | 578 // AutocompleteProvider::kMaxMatches. |
| 564 for (size_t i = 0; i < suggestions.suggest_results.size(); ++i) { | 579 for (size_t i = 0; i < suggestions.suggest_results.size(); ++i) { |
| 565 const omnibox_api::SuggestResult& suggestion = | 580 const omnibox_api::SuggestResult& suggestion = |
| 566 *suggestions.suggest_results[i]; | 581 *suggestions.suggest_results[i]; |
| 567 // We want to order these suggestions in descending order, so start with | 582 // We want to order these suggestions in descending order, so start with |
| 568 // the relevance of the first result (added synchronously in Start()), | 583 // the relevance of the first result (added synchronously in Start()), |
| 569 // and subtract 1 for each subsequent suggestion from the extension. | 584 // and subtract 1 for each subsequent suggestion from the extension. |
| 570 // We know that |complete| is true, because we wouldn't get results from | 585 // We recompute the first match's relevance; we know that |complete| |
| 571 // the extension unless the full keyword had been typed. | 586 // is true, because we wouldn't get results from the extension unless |
| 587 // the full keyword had been typed. |
| 572 int first_relevance = CalculateRelevance(input.type(), true, true, | 588 int first_relevance = CalculateRelevance(input.type(), true, true, |
| 573 input.prefer_keyword(), input.allow_exact_keyword_match()); | 589 input.prefer_keyword(), input.allow_exact_keyword_match()); |
| 590 // Since these matches score less than the first match, they should |
| 591 // never become the default match. Mark them as such. |
| 574 extension_suggest_matches_.push_back(CreateAutocompleteMatch( | 592 extension_suggest_matches_.push_back(CreateAutocompleteMatch( |
| 575 template_url, input, keyword.length(), | 593 template_url, input, keyword.length(), |
| 576 UTF8ToUTF16(suggestion.content), first_relevance - (i + 1))); | 594 UTF8ToUTF16(suggestion.content), false, first_relevance - (i + 1))); |
| 577 | 595 |
| 578 AutocompleteMatch* match = &extension_suggest_matches_.back(); | 596 AutocompleteMatch* match = &extension_suggest_matches_.back(); |
| 579 match->contents.assign(UTF8ToUTF16(suggestion.description)); | 597 match->contents.assign(UTF8ToUTF16(suggestion.description)); |
| 580 match->contents_class = | 598 match->contents_class = |
| 581 extensions::StyleTypesToACMatchClassifications(suggestion); | 599 extensions::StyleTypesToACMatchClassifications(suggestion); |
| 582 match->description.clear(); | 600 match->description.clear(); |
| 583 match->description_class.clear(); | 601 match->description_class.clear(); |
| 584 } | 602 } |
| 585 | 603 |
| 586 done_ = true; | 604 done_ = true; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 616 } | 634 } |
| 617 | 635 |
| 618 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 636 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
| 619 if (!current_keyword_extension_id_.empty()) { | 637 if (!current_keyword_extension_id_.empty()) { |
| 620 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( | 638 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( |
| 621 profile_, current_keyword_extension_id_); | 639 profile_, current_keyword_extension_id_); |
| 622 | 640 |
| 623 current_keyword_extension_id_.clear(); | 641 current_keyword_extension_id_.clear(); |
| 624 } | 642 } |
| 625 } | 643 } |
| OLD | NEW |