| 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/format_macros.h" | 11 #include "base/format_macros.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 13 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 14 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 15 #include "base/time.h" | 16 #include "base/time.h" |
| 16 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" | 17 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" |
| 17 #include "chrome/browser/autocomplete/builtin_provider.h" | 18 #include "chrome/browser/autocomplete/builtin_provider.h" |
| 18 #include "chrome/browser/autocomplete/extension_app_provider.h" | 19 #include "chrome/browser/autocomplete/extension_app_provider.h" |
| 19 #include "chrome/browser/autocomplete/history_contents_provider.h" | 20 #include "chrome/browser/autocomplete/history_contents_provider.h" |
| 20 #include "chrome/browser/autocomplete/history_quick_provider.h" | 21 #include "chrome/browser/autocomplete/history_quick_provider.h" |
| 21 #include "chrome/browser/autocomplete/history_url_provider.h" | 22 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 22 #include "chrome/browser/autocomplete/keyword_provider.h" | 23 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 23 #include "chrome/browser/autocomplete/search_provider.h" | 24 #include "chrome/browser/autocomplete/search_provider.h" |
| 24 #include "chrome/browser/autocomplete/shortcuts_provider.h" | 25 #include "chrome/browser/autocomplete/shortcuts_provider.h" |
| 26 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/search_engines/template_url.h" | 28 #include "chrome/browser/search_engines/template_url.h" |
| 27 #include "chrome/common/chrome_notification_types.h" | 29 #include "chrome/common/chrome_notification_types.h" |
| 30 #include "chrome/common/chrome_switches.h" |
| 28 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
| 29 #include "grit/generated_resources.h" | 32 #include "grit/generated_resources.h" |
| 30 #include "grit/theme_resources.h" | 33 #include "grit/theme_resources.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
| 32 | 35 |
| 33 namespace { | 36 namespace { |
| 34 | 37 |
| 35 // Converts the given type to an integer based on the AQS specification. | 38 // Converts the given type to an integer based on the AQS specification. |
| 36 // For more details, See http://goto.google.com/binary-clients-logging . | 39 // For more details, See http://goto.google.com/binary-clients-logging . |
| 37 int AutocompleteMatchToAssistedQueryType(const AutocompleteMatch::Type& type) { | 40 int AutocompleteMatchToAssistedQueryType(const AutocompleteMatch::Type& type) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 72 |
| 70 } // namespace | 73 } // namespace |
| 71 | 74 |
| 72 const int AutocompleteController::kNoItemSelected = -1; | 75 const int AutocompleteController::kNoItemSelected = -1; |
| 73 | 76 |
| 74 AutocompleteController::AutocompleteController( | 77 AutocompleteController::AutocompleteController( |
| 75 Profile* profile, | 78 Profile* profile, |
| 76 AutocompleteControllerDelegate* delegate) | 79 AutocompleteControllerDelegate* delegate) |
| 77 : delegate_(delegate), | 80 : delegate_(delegate), |
| 78 keyword_provider_(NULL), | 81 keyword_provider_(NULL), |
| 82 zero_suggest_provider_(NULL), |
| 79 done_(true), | 83 done_(true), |
| 80 in_start_(false), | 84 in_start_(false), |
| 85 in_zero_suggest_(false), |
| 81 profile_(profile) { | 86 profile_(profile) { |
| 82 search_provider_ = new SearchProvider(this, profile); | 87 search_provider_ = new SearchProvider(this, profile); |
| 83 providers_.push_back(search_provider_); | 88 providers_.push_back(search_provider_); |
| 84 #if !defined(OS_ANDROID) | 89 #if !defined(OS_ANDROID) |
| 85 // History quick provider is enabled on all platforms other than Android. | 90 // History quick provider is enabled on all platforms other than Android. |
| 86 bool hqp_enabled = true; | 91 bool hqp_enabled = true; |
| 87 providers_.push_back(new HistoryQuickProvider(this, profile)); | 92 providers_.push_back(new HistoryQuickProvider(this, profile)); |
| 88 // Search provider/"tab to search" is enabled on all platforms other than | 93 // Search provider/"tab to search" is enabled on all platforms other than |
| 89 // Android. | 94 // Android. |
| 90 keyword_provider_ = new KeywordProvider(this, profile); | 95 keyword_provider_ = new KeywordProvider(this, profile); |
| 91 providers_.push_back(keyword_provider_); | 96 providers_.push_back(keyword_provider_); |
| 92 #else | 97 #else |
| 93 // TODO(mrossetti): Remove the following and permanently modify the | 98 // TODO(mrossetti): Remove the following and permanently modify the |
| 94 // HistoryURLProvider to not search titles once HQP is turned on permanently. | 99 // HistoryURLProvider to not search titles once HQP is turned on permanently. |
| 95 // TODO(jcivelli): Enable the History Quick Provider and figure out why it | 100 // TODO(jcivelli): Enable the History Quick Provider and figure out why it |
| 96 // reports the wrong results for some pages. | 101 // reports the wrong results for some pages. |
| 97 bool hqp_enabled = false; | 102 bool hqp_enabled = false; |
| 98 #endif // !OS_ANDROID | 103 #endif // !OS_ANDROID |
| 99 providers_.push_back(new HistoryURLProvider(this, profile)); | 104 providers_.push_back(new HistoryURLProvider(this, profile)); |
| 100 providers_.push_back(new ShortcutsProvider(this, profile)); | 105 providers_.push_back(new ShortcutsProvider(this, profile)); |
| 101 providers_.push_back(new HistoryContentsProvider(this, profile, hqp_enabled)); | 106 providers_.push_back(new HistoryContentsProvider(this, profile, hqp_enabled)); |
| 102 providers_.push_back(new BuiltinProvider(this, profile)); | 107 providers_.push_back(new BuiltinProvider(this, profile)); |
| 103 providers_.push_back(new ExtensionAppProvider(this, profile)); | 108 providers_.push_back(new ExtensionAppProvider(this, profile)); |
| 109 // Create ZeroSuggest if its switch is present. |
| 110 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 111 if (cl->HasSwitch(switches::kExperimentalZeroSuggestURLPrefix)) { |
| 112 zero_suggest_provider_ = new ZeroSuggestProvider(this, profile, |
| 113 cl->GetSwitchValueASCII(switches::kExperimentalZeroSuggestURLPrefix)); |
| 114 providers_.push_back(zero_suggest_provider_); |
| 115 } |
| 104 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); ++i) | 116 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); ++i) |
| 105 (*i)->AddRef(); | 117 (*i)->AddRef(); |
| 106 } | 118 } |
| 107 | 119 |
| 108 AutocompleteController::~AutocompleteController() { | 120 AutocompleteController::~AutocompleteController() { |
| 109 // The providers may have tasks outstanding that hold refs to them. We need | 121 // The providers may have tasks outstanding that hold refs to them. We need |
| 110 // to ensure they won't call us back if they outlive us. (Practically, | 122 // to ensure they won't call us back if they outlive us. (Practically, |
| 111 // calling Stop() should also cancel those tasks and make it so that we hold | 123 // calling Stop() should also cancel those tasks and make it so that we hold |
| 112 // the only refs.) We also don't want to bother notifying anyone of our | 124 // the only refs.) We also don't want to bother notifying anyone of our |
| 113 // result changes here, because the notification observer is in the midst of | 125 // result changes here, because the notification observer is in the midst of |
| (...skipping 28 matching lines...) Expand all Loading... |
| 142 // get matches back more quickly). Taking advantage of this reduces flicker. | 154 // get matches back more quickly). Taking advantage of this reduces flicker. |
| 143 // | 155 // |
| 144 // NOTE: This comes after constructing |input_| above since that construction | 156 // NOTE: This comes after constructing |input_| above since that construction |
| 145 // can change the text string (e.g. by stripping off a leading '?'). | 157 // can change the text string (e.g. by stripping off a leading '?'). |
| 146 const bool minimal_changes = (input_.text() == old_input_text) && | 158 const bool minimal_changes = (input_.text() == old_input_text) && |
| 147 (input_.matches_requested() == old_matches_requested); | 159 (input_.matches_requested() == old_matches_requested); |
| 148 | 160 |
| 149 expire_timer_.Stop(); | 161 expire_timer_.Stop(); |
| 150 | 162 |
| 151 // Start the new query. | 163 // Start the new query. |
| 164 in_zero_suggest_ = false; |
| 152 in_start_ = true; | 165 in_start_ = true; |
| 153 base::TimeTicks start_time = base::TimeTicks::Now(); | 166 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 154 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); | 167 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); |
| 155 ++i) { | 168 ++i) { |
| 156 (*i)->Start(input_, minimal_changes); | 169 (*i)->Start(input_, minimal_changes); |
| 157 if (matches_requested != AutocompleteInput::ALL_MATCHES) | 170 if (matches_requested != AutocompleteInput::ALL_MATCHES) |
| 158 DCHECK((*i)->done()); | 171 DCHECK((*i)->done()); |
| 159 } | 172 } |
| 160 if (matches_requested == AutocompleteInput::ALL_MATCHES && | 173 if (matches_requested == AutocompleteInput::ALL_MATCHES && |
| 161 (text.length() < 6)) { | 174 (text.length() < 6)) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 182 expire_timer_.Stop(); | 195 expire_timer_.Stop(); |
| 183 done_ = true; | 196 done_ = true; |
| 184 if (clear_result && !result_.empty()) { | 197 if (clear_result && !result_.empty()) { |
| 185 result_.Reset(); | 198 result_.Reset(); |
| 186 // NOTE: We pass in false since we're trying to only clear the popup, not | 199 // NOTE: We pass in false since we're trying to only clear the popup, not |
| 187 // touch the edit... this is all a mess and should be cleaned up :( | 200 // touch the edit... this is all a mess and should be cleaned up :( |
| 188 NotifyChanged(false); | 201 NotifyChanged(false); |
| 189 } | 202 } |
| 190 } | 203 } |
| 191 | 204 |
| 205 void AutocompleteController::StartZeroSuggest( |
| 206 const GURL& url, |
| 207 const string16& user_text) { |
| 208 if (zero_suggest_provider_ != NULL) { |
| 209 DCHECK(!in_start_); // We should not be already running a query. |
| 210 in_zero_suggest_ = true; |
| 211 zero_suggest_provider_->StartZeroSuggest(url, user_text); |
| 212 } |
| 213 } |
| 214 |
| 215 void AutocompleteController::StopZeroSuggest() { |
| 216 if (zero_suggest_provider_ != NULL) { |
| 217 DCHECK(!in_start_); // We should not be already running a query. |
| 218 zero_suggest_provider_->Stop(false); |
| 219 } |
| 220 } |
| 221 |
| 192 void AutocompleteController::DeleteMatch(const AutocompleteMatch& match) { | 222 void AutocompleteController::DeleteMatch(const AutocompleteMatch& match) { |
| 193 DCHECK(match.deletable); | 223 DCHECK(match.deletable); |
| 194 match.provider->DeleteMatch(match); // This may synchronously call back to | 224 match.provider->DeleteMatch(match); // This may synchronously call back to |
| 195 // OnProviderUpdate(). | 225 // OnProviderUpdate(). |
| 196 // If DeleteMatch resulted in a callback to OnProviderUpdate and we're | 226 // If DeleteMatch resulted in a callback to OnProviderUpdate and we're |
| 197 // not done, we might attempt to redisplay the deleted match. Make sure | 227 // not done, we might attempt to redisplay the deleted match. Make sure |
| 198 // we aren't displaying it by removing any old entries. | 228 // we aren't displaying it by removing any old entries. |
| 199 ExpireCopiedEntries(); | 229 ExpireCopiedEntries(); |
| 200 } | 230 } |
| 201 | 231 |
| 202 void AutocompleteController::ExpireCopiedEntries() { | 232 void AutocompleteController::ExpireCopiedEntries() { |
| 203 // Clear out the results. This ensures no results from the previous result set | 233 // Clear out the results. This ensures no results from the previous result set |
| 204 // are copied over. | 234 // are copied over. |
| 205 result_.Reset(); | 235 result_.Reset(); |
| 206 // We allow matches from the previous result set to starve out matches from | 236 // We allow matches from the previous result set to starve out matches from |
| 207 // the new result set. This means in order to expire matches we have to query | 237 // the new result set. This means in order to expire matches we have to query |
| 208 // the providers again. | 238 // the providers again. |
| 209 UpdateResult(false); | 239 UpdateResult(false); |
| 210 } | 240 } |
| 211 | 241 |
| 212 void AutocompleteController::OnProviderUpdate(bool updated_matches) { | 242 void AutocompleteController::OnProviderUpdate(bool updated_matches) { |
| 213 CheckIfDone(); | 243 if (in_zero_suggest_) { |
| 214 // Multiple providers may provide synchronous results, so we only update the | 244 // We got ZeroSuggest results before Start(). Show only those results, |
| 215 // results if we're not in Start(). | 245 // because results from other providers are stale. |
| 216 if (!in_start_ && (updated_matches || done_)) | 246 result_.Reset(); |
| 217 UpdateResult(false); | 247 result_.AppendMatches(zero_suggest_provider_->matches()); |
| 248 result_.SortAndCull(input_); |
| 249 NotifyChanged(true); |
| 250 } else { |
| 251 CheckIfDone(); |
| 252 // Multiple providers may provide synchronous results, so we only update the |
| 253 // results if we're not in Start(). |
| 254 if (!in_start_ && (updated_matches || done_)) |
| 255 UpdateResult(false); |
| 256 } |
| 218 } | 257 } |
| 219 | 258 |
| 220 void AutocompleteController::AddProvidersInfo( | 259 void AutocompleteController::AddProvidersInfo( |
| 221 ProvidersInfo* provider_info) const { | 260 ProvidersInfo* provider_info) const { |
| 222 provider_info->clear(); | 261 provider_info->clear(); |
| 223 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end(); | 262 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end(); |
| 224 ++i) { | 263 ++i) { |
| 225 // Add per-provider info, if any. | 264 // Add per-provider info, if any. |
| 226 (*i)->AddProviderInfo(provider_info); | 265 (*i)->AddProviderInfo(provider_info); |
| 227 | 266 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 } | 439 } |
| 401 done_ = true; | 440 done_ = true; |
| 402 } | 441 } |
| 403 | 442 |
| 404 void AutocompleteController::StartExpireTimer() { | 443 void AutocompleteController::StartExpireTimer() { |
| 405 if (result_.HasCopiedMatches()) | 444 if (result_.HasCopiedMatches()) |
| 406 expire_timer_.Start(FROM_HERE, | 445 expire_timer_.Start(FROM_HERE, |
| 407 base::TimeDelta::FromMilliseconds(kExpireTimeMS), | 446 base::TimeDelta::FromMilliseconds(kExpireTimeMS), |
| 408 this, &AutocompleteController::ExpireCopiedEntries); | 447 this, &AutocompleteController::ExpireCopiedEntries); |
| 409 } | 448 } |
| OLD | NEW |