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/string16.h" | 10 #include "base/string16.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 13 #include "chrome/browser/extensions/extension_omnibox_api.h" | 13 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/search_engines/template_url.h" | 16 #include "chrome/browser/search_engines/template_url.h" |
| 17 #include "chrome/browser/search_engines/template_url_service.h" | 17 #include "chrome/browser/search_engines/template_url_service.h" |
| 18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 19 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| 20 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
| 21 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 22 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
| 23 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 | 287 |
| 288 if (profile_ && template_url->IsExtensionKeyword()) { | 288 if (profile_ && template_url->IsExtensionKeyword()) { |
| 289 if (input.matches_requested() == AutocompleteInput::ALL_MATCHES) { | 289 if (input.matches_requested() == AutocompleteInput::ALL_MATCHES) { |
| 290 if (template_url->GetExtensionId() != current_keyword_extension_id_) | 290 if (template_url->GetExtensionId() != current_keyword_extension_id_) |
| 291 MaybeEndExtensionKeywordMode(); | 291 MaybeEndExtensionKeywordMode(); |
| 292 if (current_keyword_extension_id_.empty()) | 292 if (current_keyword_extension_id_.empty()) |
| 293 EnterExtensionKeywordMode(template_url->GetExtensionId()); | 293 EnterExtensionKeywordMode(template_url->GetExtensionId()); |
| 294 keyword_mode_toggle.StayInKeywordMode(); | 294 keyword_mode_toggle.StayInKeywordMode(); |
| 295 } | 295 } |
| 296 | 296 |
| 297 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, | 297 extensions::ApplyDefaultSuggestionForExtensionKeyword( |
| 298 remaining_input, | 298 profile_, template_url, |
|
Matt Perry
2012/05/01 01:23:21
wrapped lines should have a 4-space indent: http:/
vabr (Chromium)
2012/05/01 02:06:50
Done.
| |
| 299 &matches_[0]); | 299 remaining_input, |
| 300 &matches_[0]); | |
| 300 | 301 |
| 301 if (minimal_changes && | 302 if (minimal_changes && |
| 302 (input.matches_requested() != AutocompleteInput::BEST_MATCH)) { | 303 (input.matches_requested() != AutocompleteInput::BEST_MATCH)) { |
| 303 // If the input hasn't significantly changed, we can just use the | 304 // If the input hasn't significantly changed, we can just use the |
| 304 // suggestions from last time. We need to readjust the relevance to | 305 // suggestions from last time. We need to readjust the relevance to |
| 305 // ensure it is less than the main match's relevance. | 306 // ensure it is less than the main match's relevance. |
| 306 for (size_t i = 0; i < extension_suggest_matches_.size(); ++i) { | 307 for (size_t i = 0; i < extension_suggest_matches_.size(); ++i) { |
| 307 matches_.push_back(extension_suggest_matches_[i]); | 308 matches_.push_back(extension_suggest_matches_[i]); |
| 308 matches_.back().relevance = matches_[0].relevance - (i + 1); | 309 matches_.back().relevance = matches_[0].relevance - (i + 1); |
| 309 } | 310 } |
| 310 } else if (input.matches_requested() == AutocompleteInput::ALL_MATCHES) { | 311 } else if (input.matches_requested() == AutocompleteInput::ALL_MATCHES) { |
| 311 extension_suggest_last_input_ = input; | 312 extension_suggest_last_input_ = input; |
| 312 extension_suggest_matches_.clear(); | 313 extension_suggest_matches_.clear(); |
| 313 | 314 |
| 314 bool have_listeners = ExtensionOmniboxEventRouter::OnInputChanged( | 315 bool have_listeners = |
| 315 profile_, template_url->GetExtensionId(), | 316 extensions::ExtensionOmniboxEventRouter::OnInputChanged( |
| 316 UTF16ToUTF8(remaining_input), current_input_id_); | 317 profile_, template_url->GetExtensionId(), |
| 318 UTF16ToUTF8(remaining_input), current_input_id_); | |
| 317 | 319 |
| 318 // We only have to wait for suggest results if there are actually | 320 // We only have to wait for suggest results if there are actually |
| 319 // extensions listening for input changes. | 321 // extensions listening for input changes. |
| 320 if (have_listeners) | 322 if (have_listeners) |
| 321 done_ = false; | 323 done_ = false; |
| 322 } | 324 } |
| 323 } | 325 } |
| 324 } else { | 326 } else { |
| 325 if (keyword_matches.size() > kMaxMatches) { | 327 if (keyword_matches.size() > kMaxMatches) { |
| 326 keyword_matches.erase(keyword_matches.begin() + kMaxMatches, | 328 keyword_matches.erase(keyword_matches.begin() + kMaxMatches, |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 497 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: { | 499 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: { |
| 498 // It's possible to change the default suggestion while not in an editing | 500 // It's possible to change the default suggestion while not in an editing |
| 499 // session. | 501 // session. |
| 500 string16 keyword, remaining_input; | 502 string16 keyword, remaining_input; |
| 501 if (matches_.empty() || current_keyword_extension_id_.empty() || | 503 if (matches_.empty() || current_keyword_extension_id_.empty() || |
| 502 !ExtractKeywordFromInput(input, &keyword, &remaining_input)) | 504 !ExtractKeywordFromInput(input, &keyword, &remaining_input)) |
| 503 return; | 505 return; |
| 504 | 506 |
| 505 const TemplateURL* template_url( | 507 const TemplateURL* template_url( |
| 506 model->GetTemplateURLForKeyword(keyword)); | 508 model->GetTemplateURLForKeyword(keyword)); |
| 507 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, | 509 extensions::ApplyDefaultSuggestionForExtensionKeyword( |
| 508 remaining_input, | 510 profile_, template_url, |
| 509 &matches_[0]); | 511 remaining_input, |
| 512 &matches_[0]); | |
| 510 listener_->OnProviderUpdate(true); | 513 listener_->OnProviderUpdate(true); |
| 511 return; | 514 return; |
| 512 } | 515 } |
| 513 | 516 |
| 514 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY: { | 517 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY: { |
| 515 const ExtensionOmniboxSuggestions& suggestions = | 518 const extensions::ExtensionOmniboxSuggestions& suggestions = |
| 516 *content::Details<ExtensionOmniboxSuggestions>(details).ptr(); | 519 *content::Details< |
| 520 extensions::ExtensionOmniboxSuggestions>(details).ptr(); | |
| 517 if (suggestions.request_id != current_input_id_) | 521 if (suggestions.request_id != current_input_id_) |
| 518 return; // This is an old result. Just ignore. | 522 return; // This is an old result. Just ignore. |
| 519 | 523 |
| 520 string16 keyword, remaining_input; | 524 string16 keyword, remaining_input; |
| 521 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { | 525 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { |
| 522 NOTREACHED(); | 526 NOTREACHED(); |
| 523 return; | 527 return; |
| 524 } | 528 } |
| 525 | 529 |
| 526 // TODO(mpcomplete): consider clamping the number of suggestions to | 530 // TODO(mpcomplete): consider clamping the number of suggestions to |
| 527 // AutocompleteProvider::kMaxMatches. | 531 // AutocompleteProvider::kMaxMatches. |
| 528 for (size_t i = 0; i < suggestions.suggestions.size(); ++i) { | 532 for (size_t i = 0; i < suggestions.suggestions.size(); ++i) { |
| 529 const ExtensionOmniboxSuggestion& suggestion = | 533 const extensions::ExtensionOmniboxSuggestion& suggestion = |
| 530 suggestions.suggestions[i]; | 534 suggestions.suggestions[i]; |
| 531 // We want to order these suggestions in descending order, so start with | 535 // We want to order these suggestions in descending order, so start with |
| 532 // the relevance of the first result (added synchronously in Start()), | 536 // the relevance of the first result (added synchronously in Start()), |
| 533 // and subtract 1 for each subsequent suggestion from the extension. | 537 // and subtract 1 for each subsequent suggestion from the extension. |
| 534 // We know that |complete| is true, because we wouldn't get results from | 538 // We know that |complete| is true, because we wouldn't get results from |
| 535 // the extension unless the full keyword had been typed. | 539 // the extension unless the full keyword had been typed. |
| 536 int first_relevance = CalculateRelevance(input.type(), true, true, | 540 int first_relevance = CalculateRelevance(input.type(), true, true, |
| 537 input.prefer_keyword(), input.allow_exact_keyword_match()); | 541 input.prefer_keyword(), input.allow_exact_keyword_match()); |
| 538 extension_suggest_matches_.push_back(CreateAutocompleteMatch( | 542 extension_suggest_matches_.push_back(CreateAutocompleteMatch( |
| 539 model, keyword, input, keyword.length(), | 543 model, keyword, input, keyword.length(), |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 567 DCHECK(service); | 571 DCHECK(service); |
| 568 service->Load(); | 572 service->Load(); |
| 569 return service; | 573 return service; |
| 570 } | 574 } |
| 571 | 575 |
| 572 void KeywordProvider::EnterExtensionKeywordMode( | 576 void KeywordProvider::EnterExtensionKeywordMode( |
| 573 const std::string& extension_id) { | 577 const std::string& extension_id) { |
| 574 DCHECK(current_keyword_extension_id_.empty()); | 578 DCHECK(current_keyword_extension_id_.empty()); |
| 575 current_keyword_extension_id_ = extension_id; | 579 current_keyword_extension_id_ = extension_id; |
| 576 | 580 |
| 577 ExtensionOmniboxEventRouter::OnInputStarted( | 581 extensions::ExtensionOmniboxEventRouter::OnInputStarted( |
| 578 profile_, current_keyword_extension_id_); | 582 profile_, current_keyword_extension_id_); |
| 579 } | 583 } |
| 580 | 584 |
| 581 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 585 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
| 582 if (!current_keyword_extension_id_.empty()) { | 586 if (!current_keyword_extension_id_.empty()) { |
| 583 ExtensionOmniboxEventRouter::OnInputCancelled( | 587 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( |
| 584 profile_, current_keyword_extension_id_); | 588 profile_, current_keyword_extension_id_); |
| 585 | 589 |
| 586 current_keyword_extension_id_.clear(); | 590 current_keyword_extension_id_.clear(); |
| 587 } | 591 } |
| 588 } | 592 } |
| OLD | NEW |