| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/autocomplete/autocomplete_controller.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/format_macros.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/metrics/histogram.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/strings/stringprintf.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" | |
| 17 #include "chrome/browser/autocomplete/builtin_provider.h" | |
| 18 #include "chrome/browser/autocomplete/zero_suggest_provider.h" | |
| 19 #include "components/omnibox/bookmark_provider.h" | |
| 20 #include "components/omnibox/history_quick_provider.h" | |
| 21 #include "components/omnibox/history_url_provider.h" | |
| 22 #include "components/omnibox/keyword_provider.h" | |
| 23 #include "components/omnibox/omnibox_field_trial.h" | |
| 24 #include "components/omnibox/search_provider.h" | |
| 25 #include "components/omnibox/shortcuts_provider.h" | |
| 26 #include "components/search_engines/template_url.h" | |
| 27 #include "components/search_engines/template_url_service.h" | |
| 28 #include "grit/components_strings.h" | |
| 29 #include "ui/base/l10n/l10n_util.h" | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // Converts the given match to a type (and possibly subtype) based on the AQS | |
| 34 // specification. For more details, see | |
| 35 // http://goto.google.com/binary-clients-logging. | |
| 36 void AutocompleteMatchToAssistedQuery( | |
| 37 const AutocompleteMatch::Type& match, | |
| 38 const AutocompleteProvider* provider, | |
| 39 size_t* type, | |
| 40 size_t* subtype) { | |
| 41 // This type indicates a native chrome suggestion. | |
| 42 *type = 69; | |
| 43 // Default value, indicating no subtype. | |
| 44 *subtype = base::string16::npos; | |
| 45 | |
| 46 // If provider is TYPE_ZERO_SUGGEST, set the subtype accordingly. | |
| 47 // Type will be set in the switch statement below where we'll enter one of | |
| 48 // SEARCH_SUGGEST or NAVSUGGEST. This subtype indicates context-aware zero | |
| 49 // suggest. | |
| 50 if (provider && | |
| 51 (provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) && | |
| 52 (match != AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED)) { | |
| 53 DCHECK((match == AutocompleteMatchType::SEARCH_SUGGEST) || | |
| 54 (match == AutocompleteMatchType::NAVSUGGEST)); | |
| 55 *subtype = 66; | |
| 56 } | |
| 57 | |
| 58 switch (match) { | |
| 59 case AutocompleteMatchType::SEARCH_SUGGEST: { | |
| 60 // Do not set subtype here; subtype may have been set above. | |
| 61 *type = 0; | |
| 62 return; | |
| 63 } | |
| 64 case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY: { | |
| 65 *subtype = 46; | |
| 66 return; | |
| 67 } | |
| 68 case AutocompleteMatchType::SEARCH_SUGGEST_TAIL: { | |
| 69 *subtype = 33; | |
| 70 return; | |
| 71 } | |
| 72 case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED: { | |
| 73 *subtype = 39; | |
| 74 return; | |
| 75 } | |
| 76 case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE: { | |
| 77 *subtype = 44; | |
| 78 return; | |
| 79 } | |
| 80 case AutocompleteMatchType::NAVSUGGEST: { | |
| 81 // Do not set subtype here; subtype may have been set above. | |
| 82 *type = 5; | |
| 83 return; | |
| 84 } | |
| 85 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED: { | |
| 86 *subtype = 57; | |
| 87 return; | |
| 88 } | |
| 89 case AutocompleteMatchType::URL_WHAT_YOU_TYPED: { | |
| 90 *subtype = 58; | |
| 91 return; | |
| 92 } | |
| 93 case AutocompleteMatchType::SEARCH_HISTORY: { | |
| 94 *subtype = 59; | |
| 95 return; | |
| 96 } | |
| 97 case AutocompleteMatchType::HISTORY_URL: { | |
| 98 *subtype = 60; | |
| 99 return; | |
| 100 } | |
| 101 case AutocompleteMatchType::HISTORY_TITLE: { | |
| 102 *subtype = 61; | |
| 103 return; | |
| 104 } | |
| 105 case AutocompleteMatchType::HISTORY_BODY: { | |
| 106 *subtype = 62; | |
| 107 return; | |
| 108 } | |
| 109 case AutocompleteMatchType::HISTORY_KEYWORD: { | |
| 110 *subtype = 63; | |
| 111 return; | |
| 112 } | |
| 113 case AutocompleteMatchType::BOOKMARK_TITLE: { | |
| 114 *subtype = 65; | |
| 115 return; | |
| 116 } | |
| 117 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED: { | |
| 118 *subtype = 39; | |
| 119 return; | |
| 120 } | |
| 121 default: { | |
| 122 // This value indicates a native chrome suggestion with no named subtype | |
| 123 // (yet). | |
| 124 *subtype = 64; | |
| 125 } | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 // Appends available autocompletion of the given type, subtype, and number to | |
| 130 // the existing available autocompletions string, encoding according to the | |
| 131 // spec. | |
| 132 void AppendAvailableAutocompletion(size_t type, | |
| 133 size_t subtype, | |
| 134 int count, | |
| 135 std::string* autocompletions) { | |
| 136 if (!autocompletions->empty()) | |
| 137 autocompletions->append("j"); | |
| 138 base::StringAppendF(autocompletions, "%" PRIuS, type); | |
| 139 // Subtype is optional - base::string16::npos indicates no subtype. | |
| 140 if (subtype != base::string16::npos) | |
| 141 base::StringAppendF(autocompletions, "i%" PRIuS, subtype); | |
| 142 if (count > 1) | |
| 143 base::StringAppendF(autocompletions, "l%d", count); | |
| 144 } | |
| 145 | |
| 146 // Returns whether the autocompletion is trivial enough that we consider it | |
| 147 // an autocompletion for which the omnibox autocompletion code did not add | |
| 148 // any value. | |
| 149 bool IsTrivialAutocompletion(const AutocompleteMatch& match) { | |
| 150 return match.type == AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED || | |
| 151 match.type == AutocompleteMatchType::URL_WHAT_YOU_TYPED || | |
| 152 match.type == AutocompleteMatchType::SEARCH_OTHER_ENGINE; | |
| 153 } | |
| 154 | |
| 155 // Whether this autocomplete match type supports custom descriptions. | |
| 156 bool AutocompleteMatchHasCustomDescription(const AutocompleteMatch& match) { | |
| 157 return match.type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || | |
| 158 match.type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE; | |
| 159 } | |
| 160 | |
| 161 } // namespace | |
| 162 | |
| 163 AutocompleteController::AutocompleteController( | |
| 164 scoped_ptr<AutocompleteProviderClient> provider_client, | |
| 165 AutocompleteControllerDelegate* delegate, | |
| 166 int provider_types) | |
| 167 : delegate_(delegate), | |
| 168 provider_client_(provider_client.Pass()), | |
| 169 history_url_provider_(NULL), | |
| 170 keyword_provider_(NULL), | |
| 171 search_provider_(NULL), | |
| 172 zero_suggest_provider_(NULL), | |
| 173 stop_timer_duration_(OmniboxFieldTrial::StopTimerFieldTrialDuration()), | |
| 174 done_(true), | |
| 175 in_start_(false), | |
| 176 template_url_service_(provider_client_->GetTemplateURLService()) { | |
| 177 provider_types &= ~OmniboxFieldTrial::GetDisabledProviderTypes(); | |
| 178 if (provider_types & AutocompleteProvider::TYPE_BOOKMARK) | |
| 179 providers_.push_back(new BookmarkProvider(provider_client_.get())); | |
| 180 if (provider_types & AutocompleteProvider::TYPE_BUILTIN) | |
| 181 providers_.push_back(new BuiltinProvider(provider_client_.get())); | |
| 182 if (provider_types & AutocompleteProvider::TYPE_HISTORY_QUICK) | |
| 183 providers_.push_back(new HistoryQuickProvider(provider_client_.get())); | |
| 184 if (provider_types & AutocompleteProvider::TYPE_HISTORY_URL) { | |
| 185 history_url_provider_ = | |
| 186 new HistoryURLProvider(provider_client_.get(), this); | |
| 187 providers_.push_back(history_url_provider_); | |
| 188 } | |
| 189 // "Tab to search" can be used on all platforms other than Android. | |
| 190 #if !defined(OS_ANDROID) | |
| 191 if (provider_types & AutocompleteProvider::TYPE_KEYWORD) { | |
| 192 keyword_provider_ = new KeywordProvider(provider_client_.get(), this); | |
| 193 providers_.push_back(keyword_provider_); | |
| 194 } | |
| 195 #endif | |
| 196 if (provider_types & AutocompleteProvider::TYPE_SEARCH) { | |
| 197 search_provider_ = new SearchProvider(provider_client_.get(), this); | |
| 198 providers_.push_back(search_provider_); | |
| 199 } | |
| 200 if (provider_types & AutocompleteProvider::TYPE_SHORTCUTS) | |
| 201 providers_.push_back(new ShortcutsProvider(provider_client_.get())); | |
| 202 if (provider_types & AutocompleteProvider::TYPE_ZERO_SUGGEST) { | |
| 203 zero_suggest_provider_ = | |
| 204 ZeroSuggestProvider::Create(provider_client_.get(), this); | |
| 205 if (zero_suggest_provider_) | |
| 206 providers_.push_back(zero_suggest_provider_); | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 AutocompleteController::~AutocompleteController() { | |
| 211 // The providers may have tasks outstanding that hold refs to them. We need | |
| 212 // to ensure they won't call us back if they outlive us. (Practically, | |
| 213 // calling Stop() should also cancel those tasks and make it so that we hold | |
| 214 // the only refs.) We also don't want to bother notifying anyone of our | |
| 215 // result changes here, because the notification observer is in the midst of | |
| 216 // shutdown too, so we don't ask Stop() to clear |result_| (and notify). | |
| 217 result_.Reset(); // Not really necessary. | |
| 218 Stop(false); | |
| 219 } | |
| 220 | |
| 221 void AutocompleteController::Start(const AutocompleteInput& input) { | |
| 222 const base::string16 old_input_text(input_.text()); | |
| 223 const bool old_want_asynchronous_matches = input_.want_asynchronous_matches(); | |
| 224 const bool old_from_omnibox_focus = input_.from_omnibox_focus(); | |
| 225 input_ = input; | |
| 226 | |
| 227 // See if we can avoid rerunning autocomplete when the query hasn't changed | |
| 228 // much. When the user presses or releases the ctrl key, the desired_tld | |
| 229 // changes, and when the user finishes an IME composition, inline autocomplete | |
| 230 // may no longer be prevented. In both these cases the text itself hasn't | |
| 231 // changed since the last query, and some providers can do much less work (and | |
| 232 // get matches back more quickly). Taking advantage of this reduces flicker. | |
| 233 // | |
| 234 // NOTE: This comes after constructing |input_| above since that construction | |
| 235 // can change the text string (e.g. by stripping off a leading '?'). | |
| 236 const bool minimal_changes = | |
| 237 (input_.text() == old_input_text) && | |
| 238 (input_.want_asynchronous_matches() == old_want_asynchronous_matches) && | |
| 239 (input.from_omnibox_focus() == old_from_omnibox_focus); | |
| 240 | |
| 241 expire_timer_.Stop(); | |
| 242 stop_timer_.Stop(); | |
| 243 | |
| 244 // Start the new query. | |
| 245 in_start_ = true; | |
| 246 base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 247 for (Providers::iterator i(providers_.begin()); i != providers_.end(); ++i) { | |
| 248 // TODO(mpearson): Remove timing code once bug 178705 is resolved. | |
| 249 base::TimeTicks provider_start_time = base::TimeTicks::Now(); | |
| 250 (*i)->Start(input_, minimal_changes); | |
| 251 if (!input.want_asynchronous_matches()) | |
| 252 DCHECK((*i)->done()); | |
| 253 base::TimeTicks provider_end_time = base::TimeTicks::Now(); | |
| 254 std::string name = std::string("Omnibox.ProviderTime2.") + (*i)->GetName(); | |
| 255 base::HistogramBase* counter = base::Histogram::FactoryGet( | |
| 256 name, 1, 5000, 20, base::Histogram::kUmaTargetedHistogramFlag); | |
| 257 counter->Add(static_cast<int>( | |
| 258 (provider_end_time - provider_start_time).InMilliseconds())); | |
| 259 } | |
| 260 if (input.want_asynchronous_matches() && (input.text().length() < 6)) { | |
| 261 base::TimeTicks end_time = base::TimeTicks::Now(); | |
| 262 std::string name = | |
| 263 "Omnibox.QueryTime2." + base::IntToString(input.text().length()); | |
| 264 base::HistogramBase* counter = base::Histogram::FactoryGet( | |
| 265 name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag); | |
| 266 counter->Add(static_cast<int>((end_time - start_time).InMilliseconds())); | |
| 267 } | |
| 268 in_start_ = false; | |
| 269 CheckIfDone(); | |
| 270 // The second true forces saying the default match has changed. | |
| 271 // This triggers the edit model to update things such as the inline | |
| 272 // autocomplete state. In particular, if the user has typed a key | |
| 273 // since the last notification, and we're now re-running | |
| 274 // autocomplete, then we need to update the inline autocompletion | |
| 275 // even if the current match is for the same URL as the last run's | |
| 276 // default match. Likewise, the controller doesn't know what's | |
| 277 // happened in the edit since the last time it ran autocomplete. | |
| 278 // The user might have selected all the text and hit delete, then | |
| 279 // typed a new character. The selection and delete won't send any | |
| 280 // signals to the controller so it doesn't realize that anything was | |
| 281 // cleared or changed. Even if the default match hasn't changed, we | |
| 282 // need the edit model to update the display. | |
| 283 UpdateResult(false, true); | |
| 284 | |
| 285 if (!done_) { | |
| 286 StartExpireTimer(); | |
| 287 StartStopTimer(); | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 void AutocompleteController::Stop(bool clear_result) { | |
| 292 StopHelper(clear_result, false); | |
| 293 } | |
| 294 | |
| 295 void AutocompleteController::DeleteMatch(const AutocompleteMatch& match) { | |
| 296 DCHECK(match.SupportsDeletion()); | |
| 297 | |
| 298 // Delete duplicate matches attached to the main match first. | |
| 299 for (ACMatches::const_iterator it(match.duplicate_matches.begin()); | |
| 300 it != match.duplicate_matches.end(); ++it) { | |
| 301 if (it->deletable) | |
| 302 it->provider->DeleteMatch(*it); | |
| 303 } | |
| 304 | |
| 305 if (match.deletable) | |
| 306 match.provider->DeleteMatch(match); | |
| 307 | |
| 308 OnProviderUpdate(true); | |
| 309 | |
| 310 // If we're not done, we might attempt to redisplay the deleted match. Make | |
| 311 // sure we aren't displaying it by removing any old entries. | |
| 312 ExpireCopiedEntries(); | |
| 313 } | |
| 314 | |
| 315 void AutocompleteController::ExpireCopiedEntries() { | |
| 316 // The first true makes UpdateResult() clear out the results and | |
| 317 // regenerate them, thus ensuring that no results from the previous | |
| 318 // result set remain. | |
| 319 UpdateResult(true, false); | |
| 320 } | |
| 321 | |
| 322 void AutocompleteController::OnProviderUpdate(bool updated_matches) { | |
| 323 CheckIfDone(); | |
| 324 // Multiple providers may provide synchronous results, so we only update the | |
| 325 // results if we're not in Start(). | |
| 326 if (!in_start_ && (updated_matches || done_)) | |
| 327 UpdateResult(false, false); | |
| 328 } | |
| 329 | |
| 330 void AutocompleteController::AddProvidersInfo( | |
| 331 ProvidersInfo* provider_info) const { | |
| 332 provider_info->clear(); | |
| 333 for (Providers::const_iterator i(providers_.begin()); i != providers_.end(); | |
| 334 ++i) { | |
| 335 // Add per-provider info, if any. | |
| 336 (*i)->AddProviderInfo(provider_info); | |
| 337 | |
| 338 // This is also a good place to put code to add info that you want to | |
| 339 // add for every provider. | |
| 340 } | |
| 341 } | |
| 342 | |
| 343 void AutocompleteController::ResetSession() { | |
| 344 for (Providers::const_iterator i(providers_.begin()); i != providers_.end(); | |
| 345 ++i) | |
| 346 (*i)->ResetSession(); | |
| 347 } | |
| 348 | |
| 349 void AutocompleteController::UpdateMatchDestinationURLWithQueryFormulationTime( | |
| 350 base::TimeDelta query_formulation_time, | |
| 351 AutocompleteMatch* match) const { | |
| 352 if (!match->search_terms_args.get() || | |
| 353 match->search_terms_args->assisted_query_stats.empty()) | |
| 354 return; | |
| 355 | |
| 356 // Append the query formulation time (time from when the user first typed a | |
| 357 // character into the omnibox to when the user selected a query) and whether | |
| 358 // a field trial has triggered to the AQS parameter. | |
| 359 TemplateURLRef::SearchTermsArgs search_terms_args(*match->search_terms_args); | |
| 360 search_terms_args.assisted_query_stats += base::StringPrintf( | |
| 361 ".%" PRId64 "j%dj%d", | |
| 362 query_formulation_time.InMilliseconds(), | |
| 363 (search_provider_ && | |
| 364 search_provider_->field_trial_triggered_in_session()) || | |
| 365 (zero_suggest_provider_ && | |
| 366 zero_suggest_provider_->field_trial_triggered_in_session()), | |
| 367 input_.current_page_classification()); | |
| 368 UpdateMatchDestinationURL(search_terms_args, match); | |
| 369 } | |
| 370 | |
| 371 void AutocompleteController::UpdateMatchDestinationURL( | |
| 372 const TemplateURLRef::SearchTermsArgs& search_terms_args, | |
| 373 AutocompleteMatch* match) const { | |
| 374 TemplateURL* template_url = match->GetTemplateURL( | |
| 375 template_url_service_, false); | |
| 376 if (!template_url) | |
| 377 return; | |
| 378 | |
| 379 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms( | |
| 380 search_terms_args, template_url_service_->search_terms_data())); | |
| 381 } | |
| 382 | |
| 383 void AutocompleteController::UpdateResult( | |
| 384 bool regenerate_result, | |
| 385 bool force_notify_default_match_changed) { | |
| 386 const bool last_default_was_valid = result_.default_match() != result_.end(); | |
| 387 // The following three variables are only set and used if | |
| 388 // |last_default_was_valid|. | |
| 389 base::string16 last_default_fill_into_edit, last_default_keyword, | |
| 390 last_default_associated_keyword; | |
| 391 if (last_default_was_valid) { | |
| 392 last_default_fill_into_edit = result_.default_match()->fill_into_edit; | |
| 393 last_default_keyword = result_.default_match()->keyword; | |
| 394 if (result_.default_match()->associated_keyword != NULL) | |
| 395 last_default_associated_keyword = | |
| 396 result_.default_match()->associated_keyword->keyword; | |
| 397 } | |
| 398 | |
| 399 if (regenerate_result) | |
| 400 result_.Reset(); | |
| 401 | |
| 402 AutocompleteResult last_result; | |
| 403 last_result.Swap(&result_); | |
| 404 | |
| 405 for (Providers::const_iterator i(providers_.begin()); | |
| 406 i != providers_.end(); ++i) | |
| 407 result_.AppendMatches(input_, (*i)->matches()); | |
| 408 | |
| 409 // Sort the matches and trim to a small number of "best" matches. | |
| 410 result_.SortAndCull(input_, template_url_service_); | |
| 411 | |
| 412 // Need to validate before invoking CopyOldMatches as the old matches are not | |
| 413 // valid against the current input. | |
| 414 #ifndef NDEBUG | |
| 415 result_.Validate(); | |
| 416 #endif | |
| 417 | |
| 418 if (!done_) { | |
| 419 // This conditional needs to match the conditional in Start that invokes | |
| 420 // StartExpireTimer. | |
| 421 result_.CopyOldMatches(input_, last_result, template_url_service_); | |
| 422 } | |
| 423 | |
| 424 UpdateKeywordDescriptions(&result_); | |
| 425 UpdateAssociatedKeywords(&result_); | |
| 426 UpdateAssistedQueryStats(&result_); | |
| 427 if (search_provider_) | |
| 428 search_provider_->RegisterDisplayedAnswers(result_); | |
| 429 | |
| 430 const bool default_is_valid = result_.default_match() != result_.end(); | |
| 431 base::string16 default_associated_keyword; | |
| 432 if (default_is_valid && | |
| 433 (result_.default_match()->associated_keyword != NULL)) { | |
| 434 default_associated_keyword = | |
| 435 result_.default_match()->associated_keyword->keyword; | |
| 436 } | |
| 437 // We've gotten async results. Send notification that the default match | |
| 438 // updated if fill_into_edit, associated_keyword, or keyword differ. (The | |
| 439 // second can change if we've just started Chrome and the keyword database | |
| 440 // finishes loading while processing this request. The third can change | |
| 441 // if we swapped from interpreting the input as a search--which gets | |
| 442 // labeled with the default search provider's keyword--to a URL.) | |
| 443 // We don't check the URL as that may change for the default match | |
| 444 // even though the fill into edit hasn't changed (see SearchProvider | |
| 445 // for one case of this). | |
| 446 const bool notify_default_match = | |
| 447 (last_default_was_valid != default_is_valid) || | |
| 448 (last_default_was_valid && | |
| 449 ((result_.default_match()->fill_into_edit != | |
| 450 last_default_fill_into_edit) || | |
| 451 (default_associated_keyword != last_default_associated_keyword) || | |
| 452 (result_.default_match()->keyword != last_default_keyword))); | |
| 453 if (notify_default_match) | |
| 454 last_time_default_match_changed_ = base::TimeTicks::Now(); | |
| 455 | |
| 456 NotifyChanged(force_notify_default_match_changed || notify_default_match); | |
| 457 } | |
| 458 | |
| 459 void AutocompleteController::UpdateAssociatedKeywords( | |
| 460 AutocompleteResult* result) { | |
| 461 if (!keyword_provider_) | |
| 462 return; | |
| 463 | |
| 464 // Determine if the user's input is an exact keyword match. | |
| 465 base::string16 exact_keyword = keyword_provider_->GetKeywordForText( | |
| 466 TemplateURLService::CleanUserInputKeyword(input_.text())); | |
| 467 | |
| 468 std::set<base::string16> keywords; | |
| 469 for (ACMatches::iterator match(result->begin()); match != result->end(); | |
| 470 ++match) { | |
| 471 base::string16 keyword( | |
| 472 match->GetSubstitutingExplicitlyInvokedKeyword(template_url_service_)); | |
| 473 if (!keyword.empty()) { | |
| 474 keywords.insert(keyword); | |
| 475 continue; | |
| 476 } | |
| 477 | |
| 478 // When the user has typed an exact keyword, we want tab-to-search on the | |
| 479 // default match to select that keyword, even if the match | |
| 480 // inline-autocompletes to a different keyword. (This prevents inline | |
| 481 // autocompletions from blocking a user's attempts to use an explicitly-set | |
| 482 // keyword of their own creation.) So use |exact_keyword| if it's | |
| 483 // available. | |
| 484 if (!exact_keyword.empty() && !keywords.count(exact_keyword)) { | |
| 485 keywords.insert(exact_keyword); | |
| 486 match->associated_keyword.reset(new AutocompleteMatch( | |
| 487 keyword_provider_->CreateVerbatimMatch(exact_keyword, | |
| 488 exact_keyword, input_))); | |
| 489 continue; | |
| 490 } | |
| 491 | |
| 492 // Otherwise, set a match's associated keyword based on the match's | |
| 493 // fill_into_edit, which should take inline autocompletions into account. | |
| 494 keyword = keyword_provider_->GetKeywordForText(match->fill_into_edit); | |
| 495 | |
| 496 // Only add the keyword if the match does not have a duplicate keyword with | |
| 497 // a more relevant match. | |
| 498 if (!keyword.empty() && !keywords.count(keyword)) { | |
| 499 keywords.insert(keyword); | |
| 500 match->associated_keyword.reset(new AutocompleteMatch( | |
| 501 keyword_provider_->CreateVerbatimMatch(match->fill_into_edit, | |
| 502 keyword, input_))); | |
| 503 } else { | |
| 504 match->associated_keyword.reset(); | |
| 505 } | |
| 506 } | |
| 507 } | |
| 508 | |
| 509 void AutocompleteController::UpdateKeywordDescriptions( | |
| 510 AutocompleteResult* result) { | |
| 511 base::string16 last_keyword; | |
| 512 for (AutocompleteResult::iterator i(result->begin()); i != result->end(); | |
| 513 ++i) { | |
| 514 if (AutocompleteMatch::IsSearchType(i->type)) { | |
| 515 if (AutocompleteMatchHasCustomDescription(*i)) | |
| 516 continue; | |
| 517 i->description.clear(); | |
| 518 i->description_class.clear(); | |
| 519 DCHECK(!i->keyword.empty()); | |
| 520 if (i->keyword != last_keyword) { | |
| 521 const TemplateURL* template_url = | |
| 522 i->GetTemplateURL(template_url_service_, false); | |
| 523 if (template_url) { | |
| 524 // For extension keywords, just make the description the extension | |
| 525 // name -- don't assume that the normal search keyword description is | |
| 526 // applicable. | |
| 527 i->description = template_url->AdjustedShortNameForLocaleDirection(); | |
| 528 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) { | |
| 529 i->description = l10n_util::GetStringFUTF16( | |
| 530 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, i->description); | |
| 531 } | |
| 532 i->description_class.push_back( | |
| 533 ACMatchClassification(0, ACMatchClassification::DIM)); | |
| 534 } | |
| 535 last_keyword = i->keyword; | |
| 536 } | |
| 537 } else { | |
| 538 last_keyword.clear(); | |
| 539 } | |
| 540 } | |
| 541 } | |
| 542 | |
| 543 void AutocompleteController::UpdateAssistedQueryStats( | |
| 544 AutocompleteResult* result) { | |
| 545 if (result->empty()) | |
| 546 return; | |
| 547 | |
| 548 // Build the impressions string (the AQS part after "."). | |
| 549 std::string autocompletions; | |
| 550 int count = 0; | |
| 551 size_t last_type = base::string16::npos; | |
| 552 size_t last_subtype = base::string16::npos; | |
| 553 for (ACMatches::iterator match(result->begin()); match != result->end(); | |
| 554 ++match) { | |
| 555 size_t type = base::string16::npos; | |
| 556 size_t subtype = base::string16::npos; | |
| 557 AutocompleteMatchToAssistedQuery( | |
| 558 match->type, match->provider, &type, &subtype); | |
| 559 if (last_type != base::string16::npos && | |
| 560 (type != last_type || subtype != last_subtype)) { | |
| 561 AppendAvailableAutocompletion( | |
| 562 last_type, last_subtype, count, &autocompletions); | |
| 563 count = 1; | |
| 564 } else { | |
| 565 count++; | |
| 566 } | |
| 567 last_type = type; | |
| 568 last_subtype = subtype; | |
| 569 } | |
| 570 AppendAvailableAutocompletion( | |
| 571 last_type, last_subtype, count, &autocompletions); | |
| 572 // Go over all matches and set AQS if the match supports it. | |
| 573 for (size_t index = 0; index < result->size(); ++index) { | |
| 574 AutocompleteMatch* match = result->match_at(index); | |
| 575 const TemplateURL* template_url = | |
| 576 match->GetTemplateURL(template_url_service_, false); | |
| 577 if (!template_url || !match->search_terms_args.get()) | |
| 578 continue; | |
| 579 std::string selected_index; | |
| 580 // Prevent trivial suggestions from getting credit for being selected. | |
| 581 if (!IsTrivialAutocompletion(*match)) | |
| 582 selected_index = base::StringPrintf("%" PRIuS, index); | |
| 583 match->search_terms_args->assisted_query_stats = | |
| 584 base::StringPrintf("chrome.%s.%s", | |
| 585 selected_index.c_str(), | |
| 586 autocompletions.c_str()); | |
| 587 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms( | |
| 588 *match->search_terms_args, template_url_service_->search_terms_data())); | |
| 589 } | |
| 590 } | |
| 591 | |
| 592 void AutocompleteController::NotifyChanged(bool notify_default_match) { | |
| 593 if (delegate_) | |
| 594 delegate_->OnResultChanged(notify_default_match); | |
| 595 if (done_) | |
| 596 provider_client_->OnAutocompleteControllerResultReady(this); | |
| 597 } | |
| 598 | |
| 599 void AutocompleteController::CheckIfDone() { | |
| 600 for (Providers::const_iterator i(providers_.begin()); i != providers_.end(); | |
| 601 ++i) { | |
| 602 if (!(*i)->done()) { | |
| 603 done_ = false; | |
| 604 return; | |
| 605 } | |
| 606 } | |
| 607 done_ = true; | |
| 608 } | |
| 609 | |
| 610 void AutocompleteController::StartExpireTimer() { | |
| 611 // Amount of time (in ms) between when the user stops typing and | |
| 612 // when we remove any copied entries. We do this from the time the | |
| 613 // user stopped typing as some providers (such as SearchProvider) | |
| 614 // wait for the user to stop typing before they initiate a query. | |
| 615 const int kExpireTimeMS = 500; | |
| 616 | |
| 617 if (result_.HasCopiedMatches()) | |
| 618 expire_timer_.Start(FROM_HERE, | |
| 619 base::TimeDelta::FromMilliseconds(kExpireTimeMS), | |
| 620 this, &AutocompleteController::ExpireCopiedEntries); | |
| 621 } | |
| 622 | |
| 623 void AutocompleteController::StartStopTimer() { | |
| 624 stop_timer_.Start(FROM_HERE, | |
| 625 stop_timer_duration_, | |
| 626 base::Bind(&AutocompleteController::StopHelper, | |
| 627 base::Unretained(this), | |
| 628 false, true)); | |
| 629 } | |
| 630 | |
| 631 void AutocompleteController::StopHelper(bool clear_result, | |
| 632 bool due_to_user_inactivity) { | |
| 633 for (Providers::const_iterator i(providers_.begin()); i != providers_.end(); | |
| 634 ++i) { | |
| 635 (*i)->Stop(clear_result, due_to_user_inactivity); | |
| 636 } | |
| 637 | |
| 638 expire_timer_.Stop(); | |
| 639 stop_timer_.Stop(); | |
| 640 done_ = true; | |
| 641 if (clear_result && !result_.empty()) { | |
| 642 result_.Reset(); | |
| 643 // NOTE: We pass in false since we're trying to only clear the popup, not | |
| 644 // touch the edit... this is all a mess and should be cleaned up :( | |
| 645 NotifyChanged(false); | |
| 646 } | |
| 647 } | |
| OLD | NEW |