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 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 // into keyword templates. | 433 // into keyword templates. |
434 FillInURLAndContents(profile_, remaining_input, element, &result); | 434 FillInURLAndContents(profile_, remaining_input, element, &result); |
435 | 435 |
436 if (supports_replacement) | 436 if (supports_replacement) |
437 result.template_url = element; | 437 result.template_url = element; |
438 result.transition = content::PAGE_TRANSITION_KEYWORD; | 438 result.transition = content::PAGE_TRANSITION_KEYWORD; |
439 | 439 |
440 return result; | 440 return result; |
441 } | 441 } |
442 | 442 |
| 443 AutocompleteMatch KeywordProvider::CreateAutocompleteMatchFromSuggestion( |
| 444 TemplateURLService* model, |
| 445 const string16& keyword, |
| 446 const AutocompleteInput& input, |
| 447 const ExtensionOmniboxSuggestion& suggestion, |
| 448 int relevance) { |
| 449 AutocompleteMatch match = CreateAutocompleteMatch( |
| 450 model, keyword, input, keyword.length(), |
| 451 suggestion.content, relevance); |
| 452 match.contents = AutocompleteMatch::SanitizeString(suggestion.description); |
| 453 match.contents_class = suggestion.description_styles; |
| 454 match.description.clear(); |
| 455 match.description_class.clear(); |
| 456 return match; |
| 457 } |
| 458 |
443 void KeywordProvider::Observe(int type, | 459 void KeywordProvider::Observe(int type, |
444 const content::NotificationSource& source, | 460 const content::NotificationSource& source, |
445 const content::NotificationDetails& details) { | 461 const content::NotificationDetails& details) { |
446 TemplateURLService* model = | 462 TemplateURLService* model = |
447 profile_ ? TemplateURLServiceFactory::GetForProfile(profile_) : model_; | 463 profile_ ? TemplateURLServiceFactory::GetForProfile(profile_) : model_; |
448 const AutocompleteInput& input = extension_suggest_last_input_; | 464 const AutocompleteInput& input = extension_suggest_last_input_; |
449 | 465 |
450 switch (type) { | 466 switch (type) { |
451 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED: | 467 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED: |
452 // Input has been accepted, so we're done with this input session. Ensure | 468 // Input has been accepted, so we're done with this input session. Ensure |
(...skipping 28 matching lines...) Expand all Loading... |
481 | 497 |
482 string16 keyword, remaining_input; | 498 string16 keyword, remaining_input; |
483 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { | 499 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { |
484 NOTREACHED(); | 500 NOTREACHED(); |
485 return; | 501 return; |
486 } | 502 } |
487 | 503 |
488 // TODO(mpcomplete): consider clamping the number of suggestions to | 504 // TODO(mpcomplete): consider clamping the number of suggestions to |
489 // AutocompleteProvider::kMaxMatches. | 505 // AutocompleteProvider::kMaxMatches. |
490 for (size_t i = 0; i < suggestions.suggestions.size(); ++i) { | 506 for (size_t i = 0; i < suggestions.suggestions.size(); ++i) { |
491 const ExtensionOmniboxSuggestion& suggestion = | |
492 suggestions.suggestions[i]; | |
493 // We want to order these suggestions in descending order, so start with | 507 // We want to order these suggestions in descending order, so start with |
494 // the relevance of the first result (added synchronously in Start()), | 508 // the relevance of the first result (added synchronously in Start()), |
495 // and subtract 1 for each subsequent suggestion from the extension. | 509 // and subtract 1 for each subsequent suggestion from the extension. |
496 // We know that |complete| is true, because we wouldn't get results from | 510 // We know that |complete| is true, because we wouldn't get results from |
497 // the extension unless the full keyword had been typed. | 511 // the extension unless the full keyword had been typed. |
498 int first_relevance = CalculateRelevance(input.type(), true, true, | 512 int first_relevance = CalculateRelevance(input.type(), true, true, |
499 input.prefer_keyword(), input.allow_exact_keyword_match()); | 513 input.prefer_keyword(), input.allow_exact_keyword_match()); |
500 extension_suggest_matches_.push_back(CreateAutocompleteMatch( | 514 int relevance = first_relevance - (i + 1); |
501 model, keyword, input, keyword.length(), | 515 extension_suggest_matches_.push_back( |
502 suggestion.content, first_relevance - (i + 1))); | 516 CreateAutocompleteMatchFromSuggestion(model, |
503 | 517 keyword, |
504 AutocompleteMatch* match = &extension_suggest_matches_.back(); | 518 input, |
505 match->contents.assign(suggestion.description); | 519 suggestions.suggestions[i], |
506 match->contents_class = suggestion.description_styles; | 520 relevance)); |
507 match->description.clear(); | |
508 match->description_class.clear(); | |
509 } | 521 } |
510 | 522 |
511 done_ = true; | 523 done_ = true; |
512 matches_.insert(matches_.end(), extension_suggest_matches_.begin(), | 524 matches_.insert(matches_.end(), extension_suggest_matches_.begin(), |
513 extension_suggest_matches_.end()); | 525 extension_suggest_matches_.end()); |
514 listener_->OnProviderUpdate(!extension_suggest_matches_.empty()); | 526 listener_->OnProviderUpdate(!extension_suggest_matches_.empty()); |
515 return; | 527 return; |
516 } | 528 } |
517 | 529 |
518 default: | 530 default: |
(...skipping 12 matching lines...) Expand all Loading... |
531 } | 543 } |
532 | 544 |
533 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 545 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
534 if (!current_keyword_extension_id_.empty()) { | 546 if (!current_keyword_extension_id_.empty()) { |
535 ExtensionOmniboxEventRouter::OnInputCancelled( | 547 ExtensionOmniboxEventRouter::OnInputCancelled( |
536 profile_, current_keyword_extension_id_); | 548 profile_, current_keyword_extension_id_); |
537 | 549 |
538 current_keyword_extension_id_.clear(); | 550 current_keyword_extension_id_.clear(); |
539 } | 551 } |
540 } | 552 } |
OLD | NEW |