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/autocomplete_controller.h" | 5 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 16 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" | 17 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" |
| 18 #include "chrome/browser/autocomplete/builtin_provider.h" | 18 #include "chrome/browser/autocomplete/builtin_provider.h" |
| 19 #include "chrome/browser/autocomplete/extension_app_provider.h" | 19 #include "chrome/browser/autocomplete/extension_app_provider.h" |
| 20 #include "chrome/browser/autocomplete/history_contents_provider.h" | 20 #include "chrome/browser/autocomplete/history_contents_provider.h" |
| 21 #include "chrome/browser/autocomplete/history_quick_provider.h" | 21 #include "chrome/browser/autocomplete/history_quick_provider.h" |
| 22 #include "chrome/browser/autocomplete/history_url_provider.h" | 22 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 23 #include "chrome/browser/autocomplete/keyword_provider.h" | |
| 24 #include "chrome/browser/autocomplete/search_provider.h" | |
| 25 #include "chrome/browser/autocomplete/shortcuts_provider.h" | 23 #include "chrome/browser/autocomplete/shortcuts_provider.h" |
| 26 #include "chrome/browser/autocomplete/zero_suggest_provider.h" | 24 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
| 27 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/search_engines/template_url.h" | 26 #include "chrome/browser/search_engines/template_url.h" |
| 29 #include "chrome/common/chrome_notification_types.h" | 27 #include "chrome/common/chrome_notification_types.h" |
| 30 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
| 31 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
| 32 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
| 33 #include "grit/theme_resources.h" | 31 #include "grit/theme_resources.h" |
| 34 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 // providers (such as SearchProvider) wait for the user to stop typing before | 67 // providers (such as SearchProvider) wait for the user to stop typing before |
| 70 // they initiate a query. | 68 // they initiate a query. |
| 71 const int kExpireTimeMS = 500; | 69 const int kExpireTimeMS = 500; |
| 72 | 70 |
| 73 } // namespace | 71 } // namespace |
| 74 | 72 |
| 75 const int AutocompleteController::kNoItemSelected = -1; | 73 const int AutocompleteController::kNoItemSelected = -1; |
| 76 | 74 |
| 77 AutocompleteController::AutocompleteController( | 75 AutocompleteController::AutocompleteController( |
| 78 Profile* profile, | 76 Profile* profile, |
| 79 AutocompleteControllerDelegate* delegate) | 77 AutocompleteControllerDelegate* delegate, |
| 78 int provider_types) | |
| 80 : delegate_(delegate), | 79 : delegate_(delegate), |
| 81 keyword_provider_(NULL), | 80 keyword_provider_(NULL), |
| 81 search_provider_(NULL), | |
| 82 zero_suggest_provider_(NULL), | 82 zero_suggest_provider_(NULL), |
| 83 done_(true), | 83 done_(true), |
| 84 in_start_(false), | 84 in_start_(false), |
| 85 in_zero_suggest_(false), | 85 in_zero_suggest_(false), |
| 86 profile_(profile) { | 86 profile_(profile) { |
| 87 search_provider_ = new SearchProvider(this, profile); | 87 // TODO(mrossetti): Permanently modify the HistoryURLProvider to not search |
| 88 providers_.push_back(search_provider_); | 88 // titles once HQP is turned on permanently. |
| 89 #if !defined(OS_ANDROID) | |
| 90 // History quick provider is enabled on all platforms other than Android. | |
| 91 bool hqp_enabled = true; | |
| 92 providers_.push_back(new HistoryQuickProvider(this, profile)); | |
| 93 // Search provider/"tab to search" is enabled on all platforms other than | |
| 94 // Android. | |
| 95 keyword_provider_ = new KeywordProvider(this, profile); | |
| 96 providers_.push_back(keyword_provider_); | |
| 97 #else | |
| 98 // TODO(mrossetti): Remove the following and permanently modify the | |
| 99 // HistoryURLProvider to not search titles once HQP is turned on permanently. | |
| 100 // TODO(jcivelli): Enable the History Quick Provider and figure out why it | 89 // TODO(jcivelli): Enable the History Quick Provider and figure out why it |
| 101 // reports the wrong results for some pages. | 90 // reports the wrong results for some pages. |
| 102 bool hqp_enabled = false; | 91 bool use_hqp = false; |
| 103 #endif // !OS_ANDROID | 92 #if !defined(OS_ANDROID) |
| 104 providers_.push_back(new HistoryURLProvider(this, profile)); | 93 // History quick provider can be used on all platforms other than Android. |
|
Peter Kasting
2012/09/11 01:38:42
Nit: I'd move this comment to just below the other
Daniel Erat
2012/09/11 02:09:22
Done.
| |
| 105 providers_.push_back(new ShortcutsProvider(this, profile)); | 94 if (provider_types & AutocompleteProvider::TYPE_HISTORY_QUICK) |
|
Peter Kasting
2012/09/11 01:38:42
Nit: Simpler:
use_hqp = (provider_types & Autoc
Daniel Erat
2012/09/11 02:09:22
Done.
| |
| 106 providers_.push_back(new HistoryContentsProvider(this, profile, hqp_enabled)); | 95 use_hqp = true; |
| 107 providers_.push_back(new BuiltinProvider(this, profile)); | 96 #endif |
| 108 providers_.push_back(new ExtensionAppProvider(this, profile)); | 97 |
| 109 // Create ZeroSuggest if its switch is present. | 98 if (provider_types & AutocompleteProvider::TYPE_BUILTIN) |
| 99 providers_.push_back(new BuiltinProvider(this, profile)); | |
| 100 if (provider_types & AutocompleteProvider::TYPE_EXTENSION_APP) | |
| 101 providers_.push_back(new ExtensionAppProvider(this, profile)); | |
| 102 if (provider_types & AutocompleteProvider::TYPE_HISTORY_CONTENTS) | |
| 103 providers_.push_back(new HistoryContentsProvider(this, profile, use_hqp)); | |
| 104 #if !defined(OS_ANDROID) | |
|
Peter Kasting
2012/09/11 01:38:42
Nit: No need for this #ifdef (handled above)
Daniel Erat
2012/09/11 02:09:22
Ah. I was thinking that we might not be compiling
| |
| 105 if (use_hqp) | |
| 106 providers_.push_back(new HistoryQuickProvider(this, profile)); | |
| 107 #endif | |
| 108 if (provider_types & AutocompleteProvider::TYPE_HISTORY_URL) | |
| 109 providers_.push_back(new HistoryURLProvider(this, profile)); | |
| 110 #if !defined(OS_ANDROID) | |
| 111 if (provider_types & AutocompleteProvider::TYPE_KEYWORD) { | |
| 112 // Search provider/"tab to search" can be used on all platforms other than | |
|
Peter Kasting
2012/09/11 01:38:42
Nit: I'd move this comment to just above the #ifde
Daniel Erat
2012/09/11 02:09:22
Done.
| |
| 113 // Android. | |
| 114 keyword_provider_ = new KeywordProvider(this, profile); | |
| 115 providers_.push_back(keyword_provider_); | |
| 116 } | |
| 117 #endif | |
| 118 if (provider_types & AutocompleteProvider::TYPE_SEARCH) { | |
| 119 search_provider_ = new SearchProvider(this, profile); | |
| 120 providers_.push_back(search_provider_); | |
| 121 } | |
| 122 if (provider_types & AutocompleteProvider::TYPE_SHORTCUTS) | |
| 123 providers_.push_back(new ShortcutsProvider(this, profile)); | |
| 124 | |
| 110 CommandLine* cl = CommandLine::ForCurrentProcess(); | 125 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 111 if (cl->HasSwitch(switches::kExperimentalZeroSuggestURLPrefix)) { | 126 if ((provider_types & AutocompleteProvider::TYPE_ZERO_SUGGEST) && |
| 127 cl->HasSwitch(switches::kExperimentalZeroSuggestURLPrefix)) { | |
| 112 zero_suggest_provider_ = new ZeroSuggestProvider(this, profile, | 128 zero_suggest_provider_ = new ZeroSuggestProvider(this, profile, |
| 113 cl->GetSwitchValueASCII(switches::kExperimentalZeroSuggestURLPrefix)); | 129 cl->GetSwitchValueASCII(switches::kExperimentalZeroSuggestURLPrefix)); |
| 114 providers_.push_back(zero_suggest_provider_); | 130 providers_.push_back(zero_suggest_provider_); |
| 115 } | 131 } |
| 132 | |
| 116 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); ++i) | 133 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); ++i) |
| 117 (*i)->AddRef(); | 134 (*i)->AddRef(); |
| 118 } | 135 } |
| 119 | 136 |
| 120 AutocompleteController::~AutocompleteController() { | 137 AutocompleteController::~AutocompleteController() { |
| 121 // The providers may have tasks outstanding that hold refs to them. We need | 138 // The providers may have tasks outstanding that hold refs to them. We need |
| 122 // to ensure they won't call us back if they outlive us. (Practically, | 139 // to ensure they won't call us back if they outlive us. (Practically, |
| 123 // calling Stop() should also cancel those tasks and make it so that we hold | 140 // calling Stop() should also cancel those tasks and make it so that we hold |
| 124 // the only refs.) We also don't want to bother notifying anyone of our | 141 // the only refs.) We also don't want to bother notifying anyone of our |
| 125 // result changes here, because the notification observer is in the midst of | 142 // result changes here, because the notification observer is in the midst of |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms( | 403 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms( |
| 387 *match->search_terms_args)); | 404 *match->search_terms_args)); |
| 388 } | 405 } |
| 389 } | 406 } |
| 390 | 407 |
| 391 void AutocompleteController::UpdateKeywordDescriptions( | 408 void AutocompleteController::UpdateKeywordDescriptions( |
| 392 AutocompleteResult* result) { | 409 AutocompleteResult* result) { |
| 393 string16 last_keyword; | 410 string16 last_keyword; |
| 394 for (AutocompleteResult::iterator i(result->begin()); i != result->end(); | 411 for (AutocompleteResult::iterator i(result->begin()); i != result->end(); |
| 395 ++i) { | 412 ++i) { |
| 396 if ((i->provider == keyword_provider_ && !i->keyword.empty()) || | 413 if ((i->provider->type() == AutocompleteProvider::TYPE_KEYWORD && |
| 397 (i->provider == search_provider_ && | 414 !i->keyword.empty()) || |
| 415 (i->provider->type() == AutocompleteProvider::TYPE_SEARCH && | |
| 398 AutocompleteMatch::IsSearchType(i->type))) { | 416 AutocompleteMatch::IsSearchType(i->type))) { |
| 399 i->description.clear(); | 417 i->description.clear(); |
| 400 i->description_class.clear(); | 418 i->description_class.clear(); |
| 401 DCHECK(!i->keyword.empty()); | 419 DCHECK(!i->keyword.empty()); |
| 402 if (i->keyword != last_keyword) { | 420 if (i->keyword != last_keyword) { |
| 403 const TemplateURL* template_url = i->GetTemplateURL(profile_); | 421 const TemplateURL* template_url = i->GetTemplateURL(profile_); |
| 404 if (template_url) { | 422 if (template_url) { |
| 405 i->description = l10n_util::GetStringFUTF16( | 423 i->description = l10n_util::GetStringFUTF16( |
| 406 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, | 424 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, |
| 407 template_url->AdjustedShortNameForLocaleDirection()); | 425 template_url->AdjustedShortNameForLocaleDirection()); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 437 } | 455 } |
| 438 done_ = true; | 456 done_ = true; |
| 439 } | 457 } |
| 440 | 458 |
| 441 void AutocompleteController::StartExpireTimer() { | 459 void AutocompleteController::StartExpireTimer() { |
| 442 if (result_.HasCopiedMatches()) | 460 if (result_.HasCopiedMatches()) |
| 443 expire_timer_.Start(FROM_HERE, | 461 expire_timer_.Start(FROM_HERE, |
| 444 base::TimeDelta::FromMilliseconds(kExpireTimeMS), | 462 base::TimeDelta::FromMilliseconds(kExpireTimeMS), |
| 445 this, &AutocompleteController::ExpireCopiedEntries); | 463 this, &AutocompleteController::ExpireCopiedEntries); |
| 446 } | 464 } |
| OLD | NEW |