Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3235)

Unified Diff: chrome/browser/autocomplete/keyword_provider.cc

Issue 5607003: Make chrome.omnibox.setDefaultSuggestion work when called inside (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nits Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_omnibox_api.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/autocomplete/keyword_provider.cc
diff --git a/chrome/browser/autocomplete/keyword_provider.cc b/chrome/browser/autocomplete/keyword_provider.cc
index 7c974b9b79d75b0f1551e3c9d360048aa8c91995..fd3607eb7b6492492dacfa18954cd871477f94d5 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(
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_omnibox_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698