| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 213 |
| 214 void SearchProvider::ClearInstantSuggestion() { | 214 void SearchProvider::ClearInstantSuggestion() { |
| 215 default_provider_suggestion_ = InstantSuggestion(); | 215 default_provider_suggestion_ = InstantSuggestion(); |
| 216 if (done_ || instant_finalized_) | 216 if (done_ || instant_finalized_) |
| 217 return; | 217 return; |
| 218 instant_finalized_ = true; | 218 instant_finalized_ = true; |
| 219 UpdateMatches(); | 219 UpdateMatches(); |
| 220 listener_->OnProviderUpdate(true); | 220 listener_->OnProviderUpdate(true); |
| 221 } | 221 } |
| 222 | 222 |
| 223 bool SearchProvider::GetSearchWhatYouTypedMatch(const string16& query, |
| 224 const string16& original_query, |
| 225 int accepted_suggestion, |
| 226 bool is_keyword, |
| 227 AutocompleteMatch* match) { |
| 228 // Bail out now if we don't actually have a valid provider. |
| 229 match->keyword = is_keyword ? providers_.keyword_provider() : |
| 230 providers_.default_provider(); |
| 231 const TemplateURL* provider_url = match->GetTemplateURL(profile_, false); |
| 232 if (provider_url == NULL) |
| 233 return false; |
| 234 |
| 235 match->provider = this; |
| 236 match->contents = query; |
| 237 // We're dealing with the "default search" result which has no completion. |
| 238 match->contents_class.push_back( |
| 239 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 240 |
| 241 if (is_keyword) { |
| 242 match->fill_into_edit = match->keyword + char16(' ') + query; |
| 243 match->transition = content::PAGE_TRANSITION_KEYWORD; |
| 244 match->type = AutocompleteMatch::SEARCH_OTHER_ENGINE; |
| 245 } else { |
| 246 match->fill_into_edit = query; |
| 247 match->transition = content::PAGE_TRANSITION_GENERATED; |
| 248 match->type = AutocompleteMatch::SEARCH_WHAT_YOU_TYPED; |
| 249 } |
| 250 |
| 251 const TemplateURLRef& search_url = provider_url->url_ref(); |
| 252 DCHECK(search_url.SupportsReplacement()); |
| 253 |
| 254 match->search_terms_args.reset(new TemplateURLRef::SearchTermsArgs(query)); |
| 255 match->search_terms_args->original_query = original_query; |
| 256 match->search_terms_args->accepted_suggestion = accepted_suggestion; |
| 257 |
| 258 // This is the destination URL sans assisted query stats. This must be set so |
| 259 // the AutocompleteController can properly de-dupe; the controller will |
| 260 // eventually overwrite it before it reaches the user. |
| 261 match->destination_url = |
| 262 GURL(search_url.ReplaceSearchTerms(*match->search_terms_args.get())); |
| 263 |
| 264 return true; |
| 265 } |
| 266 |
| 223 void SearchProvider::Start(const AutocompleteInput& input, | 267 void SearchProvider::Start(const AutocompleteInput& input, |
| 224 bool minimal_changes) { | 268 bool minimal_changes) { |
| 225 // Do our best to load the model as early as possible. This will reduce | 269 // Do our best to load the model as early as possible. This will reduce |
| 226 // odds of having the model not ready when really needed (a non-empty input). | 270 // odds of having the model not ready when really needed (a non-empty input). |
| 227 TemplateURLService* model = providers_.template_url_service(); | 271 TemplateURLService* model = providers_.template_url_service(); |
| 228 DCHECK(model); | 272 DCHECK(model); |
| 229 model->Load(); | 273 model->Load(); |
| 230 | 274 |
| 231 matches_.clear(); | 275 matches_.clear(); |
| 232 field_trial_triggered_ = false; | 276 field_trial_triggered_ = false; |
| (...skipping 1085 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1318 return std::max(0, base_score - score_discount); | 1362 return std::max(0, base_score - score_discount); |
| 1319 } | 1363 } |
| 1320 | 1364 |
| 1321 void SearchProvider::AddMatchToMap(const string16& query_string, | 1365 void SearchProvider::AddMatchToMap(const string16& query_string, |
| 1322 const string16& input_text, | 1366 const string16& input_text, |
| 1323 int relevance, | 1367 int relevance, |
| 1324 AutocompleteMatch::Type type, | 1368 AutocompleteMatch::Type type, |
| 1325 int accepted_suggestion, | 1369 int accepted_suggestion, |
| 1326 bool is_keyword, | 1370 bool is_keyword, |
| 1327 MatchMap* map) { | 1371 MatchMap* map) { |
| 1328 AutocompleteMatch match(this, relevance, false, type); | 1372 AutocompleteMatch match; |
| 1329 std::vector<size_t> content_param_offsets; | 1373 // We start with a "what you typed" match (even if it's not the correct type) |
| 1330 // Bail out now if we don't actually have a valid provider. | 1374 // so that we get the benefit of common fields being filled in. We override |
| 1331 match.keyword = is_keyword ? | 1375 // fields below if the "what you typed" values for them are inappropriate. |
| 1332 providers_.keyword_provider() : providers_.default_provider(); | 1376 if (!GetSearchWhatYouTypedMatch(query_string, input_text, accepted_suggestion, |
| 1333 const TemplateURL* provider_url = match.GetTemplateURL(profile_, false); | 1377 is_keyword, &match)) |
| 1334 if (provider_url == NULL) | |
| 1335 return; | 1378 return; |
| 1336 | 1379 |
| 1337 match.contents.assign(query_string); | 1380 match.relevance = relevance; |
| 1381 match.type = type; |
| 1382 |
| 1338 // We do intra-string highlighting for suggestions - the suggested segment | 1383 // We do intra-string highlighting for suggestions - the suggested segment |
| 1339 // will be highlighted, e.g. for input_text = "you" the suggestion may be | 1384 // will be highlighted, e.g. for input_text = "you" the suggestion may be |
| 1340 // "youtube", so we'll bold the "tube" section: you*tube*. | 1385 // "youtube", so we'll bold the "tube" section: you*tube*. |
| 1341 if (input_text != query_string) { | 1386 if (input_text != query_string) { |
| 1387 match.contents_class.clear(); |
| 1342 size_t input_position = match.contents.find(input_text); | 1388 size_t input_position = match.contents.find(input_text); |
| 1343 if (input_position == string16::npos) { | 1389 if (input_position == string16::npos) { |
| 1344 // The input text is not a substring of the query string, e.g. input | 1390 // The input text is not a substring of the query string, e.g. input |
| 1345 // text is "slasdot" and the query string is "slashdot", so we bold the | 1391 // text is "slasdot" and the query string is "slashdot", so we bold the |
| 1346 // whole thing. | 1392 // whole thing. |
| 1347 match.contents_class.push_back( | 1393 match.contents_class.push_back( |
| 1348 ACMatchClassification(0, ACMatchClassification::MATCH)); | 1394 ACMatchClassification(0, ACMatchClassification::MATCH)); |
| 1349 } else { | 1395 } else { |
| 1350 // TODO(beng): ACMatchClassification::MATCH now seems to just mean | 1396 // TODO(beng): ACMatchClassification::MATCH now seems to just mean |
| 1351 // "bold" this. Consider modifying the terminology. | 1397 // "bold" this. Consider modifying the terminology. |
| 1352 // We don't iterate over the string here annotating all matches because | 1398 // We don't iterate over the string here annotating all matches because |
| 1353 // it looks odd to have every occurrence of a substring that may be as | 1399 // it looks odd to have every occurrence of a substring that may be as |
| 1354 // short as a single character highlighted in a query suggestion result, | 1400 // short as a single character highlighted in a query suggestion result, |
| 1355 // e.g. for input text "s" and query string "southwest airlines", it | 1401 // e.g. for input text "s" and query string "southwest airlines", it |
| 1356 // looks odd if both the first and last s are highlighted. | 1402 // looks odd if both the first and last s are highlighted. |
| 1357 if (input_position != 0) { | 1403 if (input_position != 0) { |
| 1358 match.contents_class.push_back( | 1404 match.contents_class.push_back( |
| 1359 ACMatchClassification(0, ACMatchClassification::NONE)); | 1405 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 1360 } | 1406 } |
| 1361 match.contents_class.push_back( | 1407 match.contents_class.push_back( |
| 1362 ACMatchClassification(input_position, ACMatchClassification::DIM)); | 1408 ACMatchClassification(input_position, ACMatchClassification::DIM)); |
| 1363 size_t next_fragment_position = input_position + input_text.length(); | 1409 size_t next_fragment_position = input_position + input_text.length(); |
| 1364 if (next_fragment_position < query_string.length()) { | 1410 if (next_fragment_position < query_string.length()) { |
| 1365 match.contents_class.push_back( | 1411 match.contents_class.push_back( |
| 1366 ACMatchClassification(next_fragment_position, | 1412 ACMatchClassification(next_fragment_position, |
| 1367 ACMatchClassification::NONE)); | 1413 ACMatchClassification::NONE)); |
| 1368 } | 1414 } |
| 1369 } | 1415 } |
| 1370 } else { | |
| 1371 // Otherwise, we're dealing with the "default search" result which has no | |
| 1372 // completion. | |
| 1373 match.contents_class.push_back( | |
| 1374 ACMatchClassification(0, ACMatchClassification::NONE)); | |
| 1375 } | 1416 } |
| 1376 | 1417 |
| 1377 // When the user forced a query, we need to make sure all the fill_into_edit | 1418 // When the user forced a query, we need to make sure all the fill_into_edit |
| 1378 // values preserve that property. Otherwise, if the user starts editing a | 1419 // values preserve that property. Otherwise, if the user starts editing a |
| 1379 // suggestion, non-Search results will suddenly appear. | 1420 // suggestion, non-Search results will suddenly appear. |
| 1421 match.fill_into_edit.clear(); |
| 1380 if (input_.type() == AutocompleteInput::FORCED_QUERY) | 1422 if (input_.type() == AutocompleteInput::FORCED_QUERY) |
| 1381 match.fill_into_edit.assign(ASCIIToUTF16("?")); | 1423 match.fill_into_edit.assign(ASCIIToUTF16("?")); |
| 1382 if (is_keyword) | 1424 if (is_keyword) |
| 1383 match.fill_into_edit.append(match.keyword + char16(' ')); | 1425 match.fill_into_edit.append(match.keyword + char16(' ')); |
| 1384 if (!input_.prevent_inline_autocomplete() && | 1426 if (!input_.prevent_inline_autocomplete() && |
| 1385 StartsWith(query_string, input_text, false)) { | 1427 StartsWith(query_string, input_text, false)) { |
| 1386 match.inline_autocomplete_offset = | 1428 match.inline_autocomplete_offset = |
| 1387 match.fill_into_edit.length() + input_text.length(); | 1429 match.fill_into_edit.length() + input_text.length(); |
| 1388 } | 1430 } |
| 1389 match.fill_into_edit.append(query_string); | 1431 match.fill_into_edit.append(query_string); |
| 1390 | 1432 |
| 1391 const TemplateURLRef& search_url = provider_url->url_ref(); | |
| 1392 DCHECK(search_url.SupportsReplacement()); | |
| 1393 match.search_terms_args.reset( | |
| 1394 new TemplateURLRef::SearchTermsArgs(query_string)); | |
| 1395 match.search_terms_args->original_query = input_text; | |
| 1396 match.search_terms_args->accepted_suggestion = accepted_suggestion; | |
| 1397 // This is the destination URL sans assisted query stats. This must be set | |
| 1398 // so the AutocompleteController can properly de-dupe; the controller will | |
| 1399 // eventually overwrite it before it reaches the user. | |
| 1400 match.destination_url = | |
| 1401 GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get())); | |
| 1402 | |
| 1403 // Search results don't look like URLs. | |
| 1404 match.transition = is_keyword ? | |
| 1405 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED; | |
| 1406 | |
| 1407 // Try to add |match| to |map|. If a match for |query_string| is already in | 1433 // Try to add |match| to |map|. If a match for |query_string| is already in |
| 1408 // |map|, replace it if |match| is more relevant. | 1434 // |map|, replace it if |match| is more relevant. |
| 1409 // NOTE: Keep this ToLower() call in sync with url_database.cc. | 1435 // NOTE: Keep this ToLower() call in sync with url_database.cc. |
| 1410 const std::pair<MatchMap::iterator, bool> i = map->insert( | 1436 const std::pair<MatchMap::iterator, bool> i = map->insert( |
| 1411 std::pair<string16, AutocompleteMatch>( | 1437 std::pair<string16, AutocompleteMatch>( |
| 1412 base::i18n::ToLower(query_string), match)); | 1438 base::i18n::ToLower(query_string), match)); |
| 1413 // NOTE: We purposefully do a direct relevance comparison here instead of | 1439 // NOTE: We purposefully do a direct relevance comparison here instead of |
| 1414 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added | 1440 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added |
| 1415 // first" rather than "items alphabetically first" when the scores are equal. | 1441 // first" rather than "items alphabetically first" when the scores are equal. |
| 1416 // The only case this matters is when a user has results with the same score | 1442 // The only case this matters is when a user has results with the same score |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1514 it->set_relevance(max_query_relevance); | 1540 it->set_relevance(max_query_relevance); |
| 1515 } | 1541 } |
| 1516 } | 1542 } |
| 1517 | 1543 |
| 1518 void SearchProvider::UpdateDone() { | 1544 void SearchProvider::UpdateDone() { |
| 1519 // We're done when the timer isn't running, there are no suggest queries | 1545 // We're done when the timer isn't running, there are no suggest queries |
| 1520 // pending, and we're not waiting on Instant. | 1546 // pending, and we're not waiting on Instant. |
| 1521 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) && | 1547 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) && |
| 1522 (instant_finalized_ || !chrome::IsInstantEnabled(profile_))); | 1548 (instant_finalized_ || !chrome::IsInstantEnabled(profile_))); |
| 1523 } | 1549 } |
| OLD | NEW |