summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authormpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-07 00:02:31 +0000
committermpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-07 00:02:31 +0000
commitf5277dd89040b05ca84eb883ba6dde173aaf453b (patch)
tree6d152eb3e0e2c9286fb956c0377046310b710896 /chrome
parent80564b4ea5e7630f27e8ea2990c933eb313f92c5 (diff)
downloadchromium_src-f5277dd89040b05ca84eb883ba6dde173aaf453b.zip
chromium_src-f5277dd89040b05ca84eb883ba6dde173aaf453b.tar.gz
chromium_src-f5277dd89040b05ca84eb883ba6dde173aaf453b.tar.bz2
Make chrome.omnibox.setDefaultSuggestion work when called inside
onInputChanged. BUG=64538 TEST=Use the chrome_search sample extension and make sure the first suggestion updates in sync with what you type. Review URL: http://codereview.chromium.org/5607003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@68411 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/autocomplete/keyword_provider.cc115
-rw-r--r--chrome/browser/extensions/extension_omnibox_api.cc5
-rw-r--r--chrome/common/notification_type.h4
3 files changed, 79 insertions, 45 deletions
diff --git a/chrome/browser/autocomplete/keyword_provider.cc b/chrome/browser/autocomplete/keyword_provider.cc
index 7c974b9..fd3607e 100644
--- a/chrome/browser/autocomplete/keyword_provider.cc
+++ b/chrome/browser/autocomplete/keyword_provider.cc
@@ -61,6 +61,9 @@ KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile)
// suggestions are meant for us.
registrar_.Add(this, NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY,
Source<Profile>(profile->GetOriginalProfile()));
+ registrar_.Add(this,
+ NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED,
+ Source<Profile>(profile->GetOriginalProfile()));
registrar_.Add(this, NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED,
Source<Profile>(profile));
}
@@ -433,56 +436,78 @@ AutocompleteMatch KeywordProvider::CreateAutocompleteMatch(
void KeywordProvider::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
- if (type == NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED) {
- // Input has been accepted, so we're done with this input session. Ensure
- // we don't send the OnInputCancelled event.
- current_keyword_extension_id_.clear();
- return;
- }
+ TemplateURLModel* model = profile_ ? profile_->GetTemplateURLModel() : model_;
+ const AutocompleteInput& input = extension_suggest_last_input_;
- // TODO(mpcomplete): consider clamping the number of suggestions to
- // AutocompleteProvider::kMaxMatches.
- DCHECK(type == NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY);
+ switch (type.value) {
+ case NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED:
+ // Input has been accepted, so we're done with this input session. Ensure
+ // we don't send the OnInputCancelled event.
+ current_keyword_extension_id_.clear();
+ return;
+
+ case NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: {
+ // It's possible to change the default suggestion while not in an editing
+ // session.
+ std::wstring keyword, remaining_input;
+ if (matches_.empty() || current_keyword_extension_id_.empty() ||
+ !ExtractKeywordFromInput(input, &keyword, &remaining_input))
+ return;
+
+ const TemplateURL* template_url(model->GetTemplateURLForKeyword(keyword));
+ ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url,
+ WideToUTF16(remaining_input),
+ &matches_[0]);
+ listener_->OnProviderUpdate(true);
+ return;
+ }
- const ExtensionOmniboxSuggestions& suggestions =
- *Details<ExtensionOmniboxSuggestions>(details).ptr();
- if (suggestions.request_id != current_input_id_)
- return; // This is an old result. Just ignore.
+ case NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY: {
+ const ExtensionOmniboxSuggestions& suggestions =
+ *Details<ExtensionOmniboxSuggestions>(details).ptr();
+ if (suggestions.request_id != current_input_id_)
+ return; // This is an old result. Just ignore.
- const AutocompleteInput& input = extension_suggest_last_input_;
- std::wstring keyword, remaining_input;
- if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) {
- NOTREACHED();
- return;
- }
+ std::wstring keyword, remaining_input;
+ if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) {
+ NOTREACHED();
+ return;
+ }
- TemplateURLModel* model =
- profile_ ? profile_->GetTemplateURLModel() : model_;
-
- for (size_t i = 0; i < suggestions.suggestions.size(); ++i) {
- const ExtensionOmniboxSuggestion& suggestion = suggestions.suggestions[i];
- // We want to order these suggestions in descending order, so start with
- // the relevance of the first result (added synchronously in Start()),
- // and subtract 1 for each subsequent suggestion from the extension.
- // We know that |complete| is true, because we wouldn't get results from
- // the extension unless the full keyword had been typed.
- int first_relevance = CalculateRelevance(input.type(), true,
- input.prefer_keyword(), input.allow_exact_keyword_match());
- extension_suggest_matches_.push_back(CreateAutocompleteMatch(
- model, keyword, input, keyword.length(),
- UTF16ToWide(suggestion.content), first_relevance - (i + 1)));
-
- AutocompleteMatch* match = &extension_suggest_matches_.back();
- match->contents.assign(UTF16ToWide(suggestion.description));
- match->contents_class = suggestion.description_styles;
- match->description.clear();
- match->description_class.clear();
- }
+ // TODO(mpcomplete): consider clamping the number of suggestions to
+ // AutocompleteProvider::kMaxMatches.
+ for (size_t i = 0; i < suggestions.suggestions.size(); ++i) {
+ const ExtensionOmniboxSuggestion& suggestion =
+ suggestions.suggestions[i];
+ // We want to order these suggestions in descending order, so start with
+ // the relevance of the first result (added synchronously in Start()),
+ // and subtract 1 for each subsequent suggestion from the extension.
+ // We know that |complete| is true, because we wouldn't get results from
+ // the extension unless the full keyword had been typed.
+ int first_relevance = CalculateRelevance(input.type(), true,
+ input.prefer_keyword(), input.allow_exact_keyword_match());
+ extension_suggest_matches_.push_back(CreateAutocompleteMatch(
+ model, keyword, input, keyword.length(),
+ UTF16ToWide(suggestion.content), first_relevance - (i + 1)));
+
+ AutocompleteMatch* match = &extension_suggest_matches_.back();
+ match->contents.assign(UTF16ToWide(suggestion.description));
+ match->contents_class = suggestion.description_styles;
+ match->description.clear();
+ match->description_class.clear();
+ }
- done_ = true;
- matches_.insert(matches_.end(), extension_suggest_matches_.begin(),
- extension_suggest_matches_.end());
- listener_->OnProviderUpdate(!extension_suggest_matches_.empty());
+ done_ = true;
+ matches_.insert(matches_.end(), extension_suggest_matches_.begin(),
+ extension_suggest_matches_.end());
+ listener_->OnProviderUpdate(!extension_suggest_matches_.empty());
+ return;
+ }
+
+ default:
+ NOTREACHED();
+ return;
+ }
}
void KeywordProvider::EnterExtensionKeywordMode(
diff --git a/chrome/browser/extensions/extension_omnibox_api.cc b/chrome/browser/extensions/extension_omnibox_api.cc
index ef97d22..8492788 100644
--- a/chrome/browser/extensions/extension_omnibox_api.cc
+++ b/chrome/browser/extensions/extension_omnibox_api.cc
@@ -164,6 +164,11 @@ bool OmniboxSetDefaultSuggestionFunction::RunImpl() {
profile_->GetExtensionsService()->GetPropertyBag(GetExtension()),
suggestion);
+ NotificationService::current()->Notify(
+ NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED,
+ Source<Profile>(profile_),
+ NotificationService::NoDetails());
+
return true;
}
diff --git a/chrome/common/notification_type.h b/chrome/common/notification_type.h
index 3adc3ff..c7b3c35 100644
--- a/chrome/common/notification_type.h
+++ b/chrome/common/notification_type.h
@@ -955,6 +955,10 @@ class NotificationType {
// session. The source is the profile.
EXTENSION_OMNIBOX_INPUT_ENTERED,
+ // Sent when an omnibox extension has updated the default suggestion. The
+ // source is the profile.
+ EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED,
+
// Sent when an extension changes a preference value. The source is the
// profile, and the details are an ExtensionPrefStore::ExtensionPrefDetails
// object.