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 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 } else { | 358 } else { |
359 // See comments on an identical NOTREACHED() in search_provider.cc. | 359 // See comments on an identical NOTREACHED() in search_provider.cc. |
360 NOTREACHED(); | 360 NOTREACHED(); |
361 } | 361 } |
362 } | 362 } |
363 } | 363 } |
364 | 364 |
365 // static | 365 // static |
366 int KeywordProvider::CalculateRelevance(AutocompleteInput::Type type, | 366 int KeywordProvider::CalculateRelevance(AutocompleteInput::Type type, |
367 bool complete, | 367 bool complete, |
368 bool no_query_text_needed, | 368 bool supports_replacement, |
| 369 bool prefer_keyword, |
369 bool allow_exact_keyword_match) { | 370 bool allow_exact_keyword_match) { |
370 if (!complete) | 371 if (!complete) |
371 return (type == AutocompleteInput::URL) ? 700 : 450; | 372 return (type == AutocompleteInput::URL) ? 700 : 450; |
372 if (!allow_exact_keyword_match) | 373 if (!supports_replacement || (allow_exact_keyword_match && prefer_keyword)) |
373 return 1100; | |
374 if (no_query_text_needed) | |
375 return 1500; | 374 return 1500; |
376 return (type == AutocompleteInput::QUERY) ? 1450 : 1100; | 375 return (allow_exact_keyword_match && (type == AutocompleteInput::QUERY)) ? |
| 376 1450 : 1100; |
377 } | 377 } |
378 | 378 |
379 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( | 379 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch( |
380 TemplateURLModel* model, | 380 TemplateURLModel* model, |
381 const string16& keyword, | 381 const string16& keyword, |
382 const AutocompleteInput& input, | 382 const AutocompleteInput& input, |
383 size_t prefix_length, | 383 size_t prefix_length, |
384 const string16& remaining_input, | 384 const string16& remaining_input, |
385 int relevance) { | 385 int relevance) { |
386 DCHECK(model); | 386 DCHECK(model); |
387 // Get keyword data from data store. | 387 // Get keyword data from data store. |
388 const TemplateURL* element( | 388 const TemplateURL* element( |
389 model->GetTemplateURLForKeyword(keyword)); | 389 model->GetTemplateURLForKeyword(keyword)); |
390 DCHECK(element && element->url()); | 390 DCHECK(element && element->url()); |
391 const bool supports_replacement = element->url()->SupportsReplacement(); | 391 const bool supports_replacement = element->url()->SupportsReplacement(); |
392 | 392 |
393 // Create an edit entry of "[keyword] [remaining input]". This is helpful | 393 // Create an edit entry of "[keyword] [remaining input]". This is helpful |
394 // 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 |
395 // choice and immediately begin typing in query input. | 395 // choice and immediately begin typing in query input. |
396 const bool keyword_complete = (prefix_length == keyword.length()); | 396 const bool keyword_complete = (prefix_length == keyword.length()); |
397 if (relevance < 0) { | 397 if (relevance < 0) { |
398 relevance = | 398 relevance = |
399 CalculateRelevance(input.type(), keyword_complete, | 399 CalculateRelevance(input.type(), keyword_complete, |
400 // When the user wants keyword matches to take | 400 // When the user wants keyword matches to take |
401 // preference, score them highly regardless of | 401 // preference, score them highly regardless of |
402 // whether the input provides query text. | 402 // whether the input provides query text. |
403 input.prefer_keyword() || !supports_replacement, | 403 supports_replacement, input.prefer_keyword(), |
404 input.allow_exact_keyword_match()); | 404 input.allow_exact_keyword_match()); |
405 } | 405 } |
406 AutocompleteMatch result(this, relevance, false, | 406 AutocompleteMatch result(this, relevance, false, |
407 supports_replacement ? AutocompleteMatch::SEARCH_OTHER_ENGINE : | 407 supports_replacement ? AutocompleteMatch::SEARCH_OTHER_ENGINE : |
408 AutocompleteMatch::HISTORY_KEYWORD); | 408 AutocompleteMatch::HISTORY_KEYWORD); |
409 result.fill_into_edit.assign(keyword); | 409 result.fill_into_edit.assign(keyword); |
410 if (!remaining_input.empty() || !keyword_complete || supports_replacement) | 410 if (!remaining_input.empty() || !keyword_complete || supports_replacement) |
411 result.fill_into_edit.push_back(L' '); | 411 result.fill_into_edit.push_back(L' '); |
412 result.fill_into_edit.append(remaining_input); | 412 result.fill_into_edit.append(remaining_input); |
413 // 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 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 // TODO(mpcomplete): consider clamping the number of suggestions to | 487 // TODO(mpcomplete): consider clamping the number of suggestions to |
488 // AutocompleteProvider::kMaxMatches. | 488 // AutocompleteProvider::kMaxMatches. |
489 for (size_t i = 0; i < suggestions.suggestions.size(); ++i) { | 489 for (size_t i = 0; i < suggestions.suggestions.size(); ++i) { |
490 const ExtensionOmniboxSuggestion& suggestion = | 490 const ExtensionOmniboxSuggestion& suggestion = |
491 suggestions.suggestions[i]; | 491 suggestions.suggestions[i]; |
492 // 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 |
493 // the relevance of the first result (added synchronously in Start()), | 493 // the relevance of the first result (added synchronously in Start()), |
494 // and subtract 1 for each subsequent suggestion from the extension. | 494 // and subtract 1 for each subsequent suggestion from the extension. |
495 // 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 |
496 // the extension unless the full keyword had been typed. | 496 // the extension unless the full keyword had been typed. |
497 int first_relevance = CalculateRelevance(input.type(), true, | 497 int first_relevance = CalculateRelevance(input.type(), true, true, |
498 input.prefer_keyword(), input.allow_exact_keyword_match()); | 498 input.prefer_keyword(), input.allow_exact_keyword_match()); |
499 extension_suggest_matches_.push_back(CreateAutocompleteMatch( | 499 extension_suggest_matches_.push_back(CreateAutocompleteMatch( |
500 model, keyword, input, keyword.length(), | 500 model, keyword, input, keyword.length(), |
501 suggestion.content, first_relevance - (i + 1))); | 501 suggestion.content, first_relevance - (i + 1))); |
502 | 502 |
503 AutocompleteMatch* match = &extension_suggest_matches_.back(); | 503 AutocompleteMatch* match = &extension_suggest_matches_.back(); |
504 match->contents.assign(suggestion.description); | 504 match->contents.assign(suggestion.description); |
505 match->contents_class = suggestion.description_styles; | 505 match->contents_class = suggestion.description_styles; |
506 match->description.clear(); | 506 match->description.clear(); |
507 match->description_class.clear(); | 507 match->description_class.clear(); |
(...skipping 22 matching lines...) Expand all Loading... |
530 } | 530 } |
531 | 531 |
532 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 532 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
533 if (!current_keyword_extension_id_.empty()) { | 533 if (!current_keyword_extension_id_.empty()) { |
534 ExtensionOmniboxEventRouter::OnInputCancelled( | 534 ExtensionOmniboxEventRouter::OnInputCancelled( |
535 profile_, current_keyword_extension_id_); | 535 profile_, current_keyword_extension_id_); |
536 | 536 |
537 current_keyword_extension_id_.clear(); | 537 current_keyword_extension_id_.clear(); |
538 } | 538 } |
539 } | 539 } |
OLD | NEW |