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

Side by Side Diff: chrome/browser/autocomplete/keyword_provider.cc

Issue 6731036: Enabled pressing TAB to cycle through the Omnibox results. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 8 years, 11 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 24 matching lines...) Expand all
35 provider_->MaybeEndExtensionKeywordMode(); 35 provider_->MaybeEndExtensionKeywordMode();
36 } 36 }
37 37
38 void StayInKeywordMode() { 38 void StayInKeywordMode() {
39 provider_ = NULL; 39 provider_ = NULL;
40 } 40 }
41 private: 41 private:
42 KeywordProvider* provider_; 42 KeywordProvider* provider_;
43 }; 43 };
44 44
45 // static
46 string16 KeywordProvider::SplitReplacementStringFromInput(
47 const string16& input,
48 bool trim_leading_whitespace) {
49 // The input may contain leading whitespace, strip it.
50 string16 trimmed_input;
51 TrimWhitespace(input, TRIM_LEADING, &trimmed_input);
52
53 // And extract the replacement string.
54 string16 remaining_input;
55 SplitKeywordFromInput(trimmed_input, trim_leading_whitespace,
56 &remaining_input);
57 return remaining_input;
58 }
59
60 KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile) 45 KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile)
61 : AutocompleteProvider(listener, profile, "Keyword"), 46 : AutocompleteProvider(listener, profile, "Keyword"),
62 model_(NULL), 47 model_(NULL),
63 current_input_id_(0) { 48 current_input_id_(0) {
64 // Extension suggestions always come from the original profile, since that's 49 // Extension suggestions always come from the original profile, since that's
65 // where extensions run. We use the input ID to distinguish whether the 50 // where extensions run. We use the input ID to distinguish whether the
66 // suggestions are meant for us. 51 // suggestions are meant for us.
67 registrar_.Add(this, 52 registrar_.Add(this,
68 chrome::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY, 53 chrome::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY,
69 content::Source<Profile>(profile->GetOriginalProfile())); 54 content::Source<Profile>(profile->GetOriginalProfile()));
(...skipping 30 matching lines...) Expand all
100 } 85 }
101 }; 86 };
102 87
103 // We need our input IDs to be unique across all profiles, so we keep a global 88 // We need our input IDs to be unique across all profiles, so we keep a global
104 // UID that each provider uses. 89 // UID that each provider uses.
105 static int global_input_uid_; 90 static int global_input_uid_;
106 91
107 } // namespace 92 } // namespace
108 93
109 // static 94 // static
95 string16 KeywordProvider::SplitKeywordFromInput(
96 const string16& input,
97 bool trim_leading_whitespace,
98 string16* remaining_input) {
99 // Find end of first token. The AutocompleteController has trimmed leading
100 // whitespace, so we need not skip over that.
101 const size_t first_white(input.find_first_of(kWhitespaceUTF16));
102 DCHECK_NE(0U, first_white);
103 if (first_white == string16::npos)
104 return input; // Only one token provided.
105
106 // Set |remaining_input| to everything after the first token.
107 DCHECK(remaining_input != NULL);
108 const size_t remaining_start = trim_leading_whitespace ?
109 input.find_first_not_of(kWhitespaceUTF16, first_white) : first_white + 1;
110
111 if (remaining_start < input.length())
112 remaining_input->assign(input.begin() + remaining_start, input.end());
113
114 // Return first token as keyword.
115 return input.substr(0, first_white);
116 }
117
118 // static
119 string16 KeywordProvider::SplitReplacementStringFromInput(
120 const string16& input,
121 bool trim_leading_whitespace) {
122 // The input may contain leading whitespace, strip it.
123 string16 trimmed_input;
124 TrimWhitespace(input, TRIM_LEADING, &trimmed_input);
125
126 // And extract the replacement string.
127 string16 remaining_input;
128 SplitKeywordFromInput(trimmed_input, trim_leading_whitespace,
129 &remaining_input);
130 return remaining_input;
131 }
132
133 // static
110 const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput( 134 const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput(
111 Profile* profile, 135 Profile* profile,
112 const AutocompleteInput& input, 136 const AutocompleteInput& input,
113 string16* remaining_input) { 137 string16* remaining_input) {
114 if (!input.allow_exact_keyword_match()) 138 if (!input.allow_exact_keyword_match())
115 return NULL; 139 return NULL;
116 140
117 string16 keyword; 141 string16 keyword;
118 if (!ExtractKeywordFromInput(input, &keyword, remaining_input)) 142 if (!ExtractKeywordFromInput(input, &keyword, remaining_input))
119 return NULL; 143 return NULL;
120 144
121 // Make sure the model is loaded. This is cheap and quickly bails out if 145 // Make sure the model is loaded. This is cheap and quickly bails out if
122 // the model is already loaded. 146 // the model is already loaded.
123 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(profile); 147 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(profile);
124 DCHECK(model); 148 DCHECK(model);
125 model->Load(); 149 model->Load();
126 150
127 const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword); 151 const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword);
128 return TemplateURL::SupportsReplacement(template_url) ? template_url : NULL; 152 return TemplateURL::SupportsReplacement(template_url) ? template_url : NULL;
129 } 153 }
130 154
155 string16 KeywordProvider::GetKeywordForText(
156 const string16& text) const {
157 const string16 keyword(TemplateURLService::CleanUserInputKeyword(text));
158
159 if (keyword.empty())
160 return keyword;
161
162 TemplateURLService* url_service = GetTemplateURLService();
163 if (!url_service)
164 return string16();
165
166 // Don't provide a keyword if it doesn't support replacement.
167 const TemplateURL* const template_url =
168 url_service->GetTemplateURLForKeyword(keyword);
169 if (!TemplateURL::SupportsReplacement(template_url))
170 return string16();
171
172 // Don't provide a keyword for inactive/disabled extension keywords.
173 if (template_url->IsExtensionKeyword()) {
174 const Extension* extension = profile_->GetExtensionService()->
175 GetExtensionById(template_url->GetExtensionId(), false);
176 if (!extension ||
177 (profile_->IsOffTheRecord() &&
178 !profile_->GetExtensionService()->IsIncognitoEnabled(extension->id())))
179 return string16();
180 }
181
182 return keyword;
183 }
184
185 AutocompleteMatch KeywordProvider::CreateAutocompleteMatch(
186 const string16& text,
187 const string16& keyword,
188 const AutocompleteInput& input) {
189 return CreateAutocompleteMatch(GetTemplateURLService(), keyword, input,
190 keyword.size(), SplitReplacementStringFromInput(text, true), 0);
191 }
192
131 void KeywordProvider::Start(const AutocompleteInput& input, 193 void KeywordProvider::Start(const AutocompleteInput& input,
132 bool minimal_changes) { 194 bool minimal_changes) {
133 // This object ensures we end keyword mode if we exit the function without 195 // This object ensures we end keyword mode if we exit the function without
134 // toggling keyword mode to on. 196 // toggling keyword mode to on.
135 ScopedEndExtensionKeywordMode keyword_mode_toggle(this); 197 ScopedEndExtensionKeywordMode keyword_mode_toggle(this);
136 198
137 matches_.clear(); 199 matches_.clear();
138 200
139 if (!minimal_changes) { 201 if (!minimal_changes) {
140 done_ = true; 202 done_ = true;
(...skipping 13 matching lines...) Expand all
154 // whatever we do here! 216 // whatever we do here!
155 // 217 //
156 // TODO(pkasting): http://b/1112681 If someday we remember usage frequency for 218 // TODO(pkasting): http://b/1112681 If someday we remember usage frequency for
157 // keywords, we might suggest keywords that haven't even been partially typed, 219 // keywords, we might suggest keywords that haven't even been partially typed,
158 // if the user uses them enough and isn't obviously typing something else. In 220 // if the user uses them enough and isn't obviously typing something else. In
159 // this case we'd consider all input here to be query input. 221 // this case we'd consider all input here to be query input.
160 string16 keyword, remaining_input; 222 string16 keyword, remaining_input;
161 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) 223 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input))
162 return; 224 return;
163 225
164 // Make sure the model is loaded. This is cheap and quickly bails out if 226 TemplateURLService* model = GetTemplateURLService();
165 // the model is already loaded.
166 TemplateURLService* model =
167 profile_ ?
168 TemplateURLServiceFactory::GetForProfile(profile_) :
169 model_;
170 DCHECK(model);
171 model->Load();
172 227
173 // Get the best matches for this keyword. 228 // Get the best matches for this keyword.
174 // 229 //
175 // NOTE: We could cache the previous keywords and reuse them here in the 230 // NOTE: We could cache the previous keywords and reuse them here in the
176 // |minimal_changes| case, but since we'd still have to recalculate their 231 // |minimal_changes| case, but since we'd still have to recalculate their
177 // relevances and we can just recreate the results synchronously anyway, we 232 // relevances and we can just recreate the results synchronously anyway, we
178 // don't bother. 233 // don't bother.
179 // 234 //
180 // TODO(pkasting): http://b/893701 We should remember the user's use of a 235 // TODO(pkasting): http://b/893701 We should remember the user's use of a
181 // search query both from the autocomplete popup and from web pages 236 // search query both from the autocomplete popup and from web pages
182 // themselves. 237 // themselves.
183 std::vector<string16> keyword_matches; 238 std::vector<string16> keyword_matches;
184 model->FindMatchingKeywords(keyword, 239 model->FindMatchingKeywords(keyword,
185 !remaining_input.empty(), 240 !remaining_input.empty(),
186 &keyword_matches); 241 &keyword_matches);
187 242
188 // Prune any extension keywords that are disallowed in incognito mode (if
189 // we're incognito), or disabled.
190 for (std::vector<string16>::iterator i(keyword_matches.begin()); 243 for (std::vector<string16>::iterator i(keyword_matches.begin());
191 i != keyword_matches.end(); ) { 244 i != keyword_matches.end(); ) {
192 const TemplateURL* template_url(model->GetTemplateURLForKeyword(*i)); 245 const TemplateURL* template_url(model->GetTemplateURLForKeyword(*i));
246
247 // Prune any extension keywords that are disallowed in incognito mode (if
248 // we're incognito), or disabled.
193 if (profile_ && 249 if (profile_ &&
194 input.matches_requested() == AutocompleteInput::ALL_MATCHES && 250 input.matches_requested() == AutocompleteInput::ALL_MATCHES &&
195 template_url->IsExtensionKeyword()) { 251 template_url->IsExtensionKeyword()) {
196 ExtensionService* service = profile_->GetExtensionService(); 252 ExtensionService* service = profile_->GetExtensionService();
197 const Extension* extension = service->GetExtensionById( 253 const Extension* extension = service->GetExtensionById(
198 template_url->GetExtensionId(), false); 254 template_url->GetExtensionId(), false);
199 bool enabled = 255 bool enabled =
200 extension && (!profile_->IsOffTheRecord() || 256 extension && (!profile_->IsOffTheRecord() ||
201 service->IsIncognitoEnabled(extension->id())); 257 service->IsIncognitoEnabled(extension->id()));
202 if (!enabled) { 258 if (!enabled) {
203 i = keyword_matches.erase(i); 259 i = keyword_matches.erase(i);
204 continue; 260 continue;
205 } 261 }
206 } 262 }
263
264 // Prune any substituting keywords if there is no substitution.
265 if (TemplateURL::SupportsReplacement(template_url) &&
266 !input.allow_exact_keyword_match()) {
267 i = keyword_matches.erase(i);
268 continue;
269 }
270
207 ++i; 271 ++i;
208 } 272 }
209 if (keyword_matches.empty()) 273 if (keyword_matches.empty())
210 return; 274 return;
211 std::sort(keyword_matches.begin(), keyword_matches.end(), CompareQuality()); 275 std::sort(keyword_matches.begin(), keyword_matches.end(), CompareQuality());
212 276
213 // Limit to one exact or three inexact matches, and mark them up for display 277 // Limit to one exact or three inexact matches, and mark them up for display
214 // in the autocomplete popup. 278 // in the autocomplete popup.
215 // Any exact match is going to be the highest quality match, and thus at the 279 // Any exact match is going to be the highest quality match, and thus at the
216 // front of our vector. 280 // front of our vector.
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 return false; 351 return false;
288 352
289 string16 trimmed_input; 353 string16 trimmed_input;
290 TrimWhitespace(input.text(), TRIM_TRAILING, &trimmed_input); 354 TrimWhitespace(input.text(), TRIM_TRAILING, &trimmed_input);
291 *keyword = TemplateURLService::CleanUserInputKeyword( 355 *keyword = TemplateURLService::CleanUserInputKeyword(
292 SplitKeywordFromInput(trimmed_input, true, remaining_input)); 356 SplitKeywordFromInput(trimmed_input, true, remaining_input));
293 return !keyword->empty(); 357 return !keyword->empty();
294 } 358 }
295 359
296 // static 360 // static
297 string16 KeywordProvider::SplitKeywordFromInput(
298 const string16& input,
299 bool trim_leading_whitespace,
300 string16* remaining_input) {
301 // Find end of first token. The AutocompleteController has trimmed leading
302 // whitespace, so we need not skip over that.
303 const size_t first_white(input.find_first_of(kWhitespaceUTF16));
304 DCHECK_NE(0U, first_white);
305 if (first_white == string16::npos)
306 return input; // Only one token provided.
307
308 // Set |remaining_input| to everything after the first token.
309 DCHECK(remaining_input != NULL);
310 const size_t remaining_start = trim_leading_whitespace ?
311 input.find_first_not_of(kWhitespaceUTF16, first_white) : first_white + 1;
312
313 if (remaining_start < input.length())
314 remaining_input->assign(input.begin() + remaining_start, input.end());
315
316 // Return first token as keyword.
317 return input.substr(0, first_white);
318 }
319
320 // static
321 void KeywordProvider::FillInURLAndContents( 361 void KeywordProvider::FillInURLAndContents(
322 Profile* profile, 362 Profile* profile,
323 const string16& remaining_input, 363 const string16& remaining_input,
324 const TemplateURL* element, 364 const TemplateURL* element,
325 AutocompleteMatch* match) { 365 AutocompleteMatch* match) {
326 DCHECK(!element->short_name().empty()); 366 DCHECK(!element->short_name().empty());
327 DCHECK(element->url()); 367 DCHECK(element->url());
328 DCHECK(element->url()->IsValid()); 368 DCHECK(element->url()->IsValid());
329 int message_id = element->IsExtensionKeyword() ? 369 int message_id = element->IsExtensionKeyword() ?
330 IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH; 370 IDS_EXTENSION_KEYWORD_COMMAND : IDS_KEYWORD_SEARCH;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 const bool keyword_complete = (prefix_length == keyword.length()); 448 const bool keyword_complete = (prefix_length == keyword.length());
409 if (relevance < 0) { 449 if (relevance < 0) {
410 relevance = 450 relevance =
411 CalculateRelevance(input.type(), keyword_complete, 451 CalculateRelevance(input.type(), keyword_complete,
412 // When the user wants keyword matches to take 452 // When the user wants keyword matches to take
413 // preference, score them highly regardless of 453 // preference, score them highly regardless of
414 // whether the input provides query text. 454 // whether the input provides query text.
415 supports_replacement, input.prefer_keyword(), 455 supports_replacement, input.prefer_keyword(),
416 input.allow_exact_keyword_match()); 456 input.allow_exact_keyword_match());
417 } 457 }
418 AutocompleteMatch result(this, relevance, false, 458 AutocompleteMatch match(this, relevance, false,
419 supports_replacement ? AutocompleteMatch::SEARCH_OTHER_ENGINE : 459 supports_replacement ? AutocompleteMatch::SEARCH_OTHER_ENGINE :
420 AutocompleteMatch::HISTORY_KEYWORD); 460 AutocompleteMatch::HISTORY_KEYWORD);
421 result.fill_into_edit.assign(keyword); 461 match.fill_into_edit.assign(keyword);
422 if (!remaining_input.empty() || !keyword_complete || supports_replacement) 462 if (!remaining_input.empty() || !keyword_complete || supports_replacement)
423 result.fill_into_edit.push_back(L' '); 463 match.fill_into_edit.push_back(L' ');
424 result.fill_into_edit.append(remaining_input); 464 match.fill_into_edit.append(remaining_input);
425 // If we wanted to set |result.inline_autocomplete_offset| correctly, we'd 465 // If we wanted to set |result.inline_autocomplete_offset| correctly, we'd
426 // need CleanUserInputKeyword() to return the amount of adjustment it's made 466 // need CleanUserInputKeyword() to return the amount of adjustment it's made
427 // to the user's input. Because right now inexact keyword matches can't score 467 // to the user's input. Because right now inexact keyword matches can't score
428 // more highly than a "what you typed" match from one of the other providers, 468 // more highly than a "what you typed" match from one of the other providers,
429 // we just don't bother to do this, and leave inline autocompletion off. 469 // we just don't bother to do this, and leave inline autocompletion off.
430 result.inline_autocomplete_offset = string16::npos; 470 match.inline_autocomplete_offset = string16::npos;
431 471
432 // Create destination URL and popup entry content by substituting user input 472 // Create destination URL and popup entry content by substituting user input
433 // into keyword templates. 473 // into keyword templates.
434 FillInURLAndContents(profile_, remaining_input, element, &result); 474 FillInURLAndContents(profile_, remaining_input, element, &match);
435 475
436 if (supports_replacement) 476 if (supports_replacement)
437 result.template_url = element; 477 match.template_url = element;
438 result.transition = content::PAGE_TRANSITION_KEYWORD; 478 match.keyword = keyword;
479 match.transition = content::PAGE_TRANSITION_KEYWORD;
439 480
440 return result; 481 return match;
441 } 482 }
442 483
443 void KeywordProvider::Observe(int type, 484 void KeywordProvider::Observe(int type,
444 const content::NotificationSource& source, 485 const content::NotificationSource& source,
445 const content::NotificationDetails& details) { 486 const content::NotificationDetails& details) {
446 TemplateURLService* model = 487 TemplateURLService* model = GetTemplateURLService();
447 profile_ ? TemplateURLServiceFactory::GetForProfile(profile_) : model_;
448 const AutocompleteInput& input = extension_suggest_last_input_; 488 const AutocompleteInput& input = extension_suggest_last_input_;
449 489
450 switch (type) { 490 switch (type) {
451 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED: 491 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED:
452 // Input has been accepted, so we're done with this input session. Ensure 492 // Input has been accepted, so we're done with this input session. Ensure
453 // we don't send the OnInputCancelled event, or handle any more stray 493 // we don't send the OnInputCancelled event, or handle any more stray
454 // suggestions_ready events. 494 // suggestions_ready events.
455 current_keyword_extension_id_.clear(); 495 current_keyword_extension_id_.clear();
456 current_input_id_ = 0; 496 current_input_id_ = 0;
457 return; 497 return;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 listener_->OnProviderUpdate(!extension_suggest_matches_.empty()); 554 listener_->OnProviderUpdate(!extension_suggest_matches_.empty());
515 return; 555 return;
516 } 556 }
517 557
518 default: 558 default:
519 NOTREACHED(); 559 NOTREACHED();
520 return; 560 return;
521 } 561 }
522 } 562 }
523 563
564 TemplateURLService* KeywordProvider::GetTemplateURLService() const {
565 TemplateURLService* service = profile_ ?
566 TemplateURLServiceFactory::GetForProfile(profile_) : model_;
567 // Make sure the model is loaded. This is cheap and quickly bails out if
568 // the model is already loaded.
569 DCHECK(service);
570 service->Load();
571 return service;
572 }
573
524 void KeywordProvider::EnterExtensionKeywordMode( 574 void KeywordProvider::EnterExtensionKeywordMode(
525 const std::string& extension_id) { 575 const std::string& extension_id) {
526 DCHECK(current_keyword_extension_id_.empty()); 576 DCHECK(current_keyword_extension_id_.empty());
527 current_keyword_extension_id_ = extension_id; 577 current_keyword_extension_id_ = extension_id;
528 578
529 ExtensionOmniboxEventRouter::OnInputStarted( 579 ExtensionOmniboxEventRouter::OnInputStarted(
530 profile_, current_keyword_extension_id_); 580 profile_, current_keyword_extension_id_);
531 } 581 }
532 582
533 void KeywordProvider::MaybeEndExtensionKeywordMode() { 583 void KeywordProvider::MaybeEndExtensionKeywordMode() {
534 if (!current_keyword_extension_id_.empty()) { 584 if (!current_keyword_extension_id_.empty()) {
535 ExtensionOmniboxEventRouter::OnInputCancelled( 585 ExtensionOmniboxEventRouter::OnInputCancelled(
536 profile_, current_keyword_extension_id_); 586 profile_, current_keyword_extension_id_);
537 587
538 current_keyword_extension_id_.clear(); 588 current_keyword_extension_id_.clear();
539 } 589 }
540 } 590 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698