| 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" |
| 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/extension_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 "content/common/notification_details.h" | 20 #include "content/common/notification_details.h" |
| 20 #include "content/common/notification_source.h" | 21 #include "content/common/notification_source.h" |
| 21 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
| 22 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
| 23 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 25 | 26 |
| 26 // Helper functor for Start(), for ending keyword mode unless explicitly told | 27 // Helper functor for Start(), for ending keyword mode unless explicitly told |
| 27 // otherwise. | 28 // otherwise. |
| 28 class KeywordProvider::ScopedEndExtensionKeywordMode { | 29 class KeywordProvider::ScopedEndExtensionKeywordMode { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 56 return remaining_input; | 57 return remaining_input; |
| 57 } | 58 } |
| 58 | 59 |
| 59 KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile) | 60 KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile) |
| 60 : AutocompleteProvider(listener, profile, "Keyword"), | 61 : AutocompleteProvider(listener, profile, "Keyword"), |
| 61 model_(NULL), | 62 model_(NULL), |
| 62 current_input_id_(0) { | 63 current_input_id_(0) { |
| 63 // Extension suggestions always come from the original profile, since that's | 64 // Extension suggestions always come from the original profile, since that's |
| 64 // where extensions run. We use the input ID to distinguish whether the | 65 // where extensions run. We use the input ID to distinguish whether the |
| 65 // suggestions are meant for us. | 66 // suggestions are meant for us. |
| 66 registrar_.Add(this, NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY, | 67 registrar_.Add(this, chrome::EXTENSION_OMNIBOX_SUGGESTIONS_READY, |
| 67 Source<Profile>(profile->GetOriginalProfile())); | 68 Source<Profile>(profile->GetOriginalProfile())); |
| 68 registrar_.Add(this, | 69 registrar_.Add(this, |
| 69 NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED, | 70 chrome::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED, |
| 70 Source<Profile>(profile->GetOriginalProfile())); | 71 Source<Profile>(profile->GetOriginalProfile())); |
| 71 registrar_.Add(this, NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED, | 72 registrar_.Add(this, chrome::EXTENSION_OMNIBOX_INPUT_ENTERED, |
| 72 Source<Profile>(profile)); | 73 Source<Profile>(profile)); |
| 73 } | 74 } |
| 74 | 75 |
| 75 KeywordProvider::KeywordProvider(ACProviderListener* listener, | 76 KeywordProvider::KeywordProvider(ACProviderListener* listener, |
| 76 TemplateURLService* model) | 77 TemplateURLService* model) |
| 77 : AutocompleteProvider(listener, NULL, "Keyword"), | 78 : AutocompleteProvider(listener, NULL, "Keyword"), |
| 78 model_(model), | 79 model_(model), |
| 79 current_input_id_(0) { | 80 current_input_id_(0) { |
| 80 } | 81 } |
| 81 | 82 |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 } | 453 } |
| 453 | 454 |
| 454 void KeywordProvider::Observe(NotificationType type, | 455 void KeywordProvider::Observe(NotificationType type, |
| 455 const NotificationSource& source, | 456 const NotificationSource& source, |
| 456 const NotificationDetails& details) { | 457 const NotificationDetails& details) { |
| 457 TemplateURLService* model = | 458 TemplateURLService* model = |
| 458 profile_ ? TemplateURLServiceFactory::GetForProfile(profile_) : model_; | 459 profile_ ? TemplateURLServiceFactory::GetForProfile(profile_) : model_; |
| 459 const AutocompleteInput& input = extension_suggest_last_input_; | 460 const AutocompleteInput& input = extension_suggest_last_input_; |
| 460 | 461 |
| 461 switch (type.value) { | 462 switch (type.value) { |
| 462 case NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED: | 463 case chrome::EXTENSION_OMNIBOX_INPUT_ENTERED: |
| 463 // Input has been accepted, so we're done with this input session. Ensure | 464 // Input has been accepted, so we're done with this input session. Ensure |
| 464 // we don't send the OnInputCancelled event. | 465 // we don't send the OnInputCancelled event. |
| 465 current_keyword_extension_id_.clear(); | 466 current_keyword_extension_id_.clear(); |
| 466 return; | 467 return; |
| 467 | 468 |
| 468 case NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: { | 469 case chrome::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: { |
| 469 // It's possible to change the default suggestion while not in an editing | 470 // It's possible to change the default suggestion while not in an editing |
| 470 // session. | 471 // session. |
| 471 string16 keyword, remaining_input; | 472 string16 keyword, remaining_input; |
| 472 if (matches_.empty() || current_keyword_extension_id_.empty() || | 473 if (matches_.empty() || current_keyword_extension_id_.empty() || |
| 473 !ExtractKeywordFromInput(input, &keyword, &remaining_input)) | 474 !ExtractKeywordFromInput(input, &keyword, &remaining_input)) |
| 474 return; | 475 return; |
| 475 | 476 |
| 476 const TemplateURL* template_url( | 477 const TemplateURL* template_url( |
| 477 model->GetTemplateURLForKeyword(keyword)); | 478 model->GetTemplateURLForKeyword(keyword)); |
| 478 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, | 479 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, |
| 479 remaining_input, | 480 remaining_input, |
| 480 &matches_[0]); | 481 &matches_[0]); |
| 481 listener_->OnProviderUpdate(true); | 482 listener_->OnProviderUpdate(true); |
| 482 return; | 483 return; |
| 483 } | 484 } |
| 484 | 485 |
| 485 case NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY: { | 486 case chrome::EXTENSION_OMNIBOX_SUGGESTIONS_READY: { |
| 486 const ExtensionOmniboxSuggestions& suggestions = | 487 const ExtensionOmniboxSuggestions& suggestions = |
| 487 *Details<ExtensionOmniboxSuggestions>(details).ptr(); | 488 *Details<ExtensionOmniboxSuggestions>(details).ptr(); |
| 488 if (suggestions.request_id != current_input_id_) | 489 if (suggestions.request_id != current_input_id_) |
| 489 return; // This is an old result. Just ignore. | 490 return; // This is an old result. Just ignore. |
| 490 | 491 |
| 491 string16 keyword, remaining_input; | 492 string16 keyword, remaining_input; |
| 492 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { | 493 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { |
| 493 NOTREACHED(); | 494 NOTREACHED(); |
| 494 return; | 495 return; |
| 495 } | 496 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 } | 541 } |
| 541 | 542 |
| 542 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 543 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
| 543 if (!current_keyword_extension_id_.empty()) { | 544 if (!current_keyword_extension_id_.empty()) { |
| 544 ExtensionOmniboxEventRouter::OnInputCancelled( | 545 ExtensionOmniboxEventRouter::OnInputCancelled( |
| 545 profile_, current_keyword_extension_id_); | 546 profile_, current_keyword_extension_id_); |
| 546 | 547 |
| 547 current_keyword_extension_id_.clear(); | 548 current_keyword_extension_id_.clear(); |
| 548 } | 549 } |
| 549 } | 550 } |
| OLD | NEW |