Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/template_url_service.h" | 5 #include "components/search_engines/template_url_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/guid.h" | 13 #include "base/guid.h" |
| 14 #include "base/i18n/case_conversion.h" | 14 #include "base/i18n/case_conversion.h" |
| 15 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
| 18 #include "base/profiler/scoped_tracker.h" | 18 #include "base/profiler/scoped_tracker.h" |
| 19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/time/default_clock.h" | 23 #include "base/time/default_clock.h" |
| 24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "components/omnibox/browser/omnibox_field_trial.h" | |
| 25 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
| 26 #include "components/rappor/rappor_service.h" | 27 #include "components/rappor/rappor_service.h" |
| 27 #include "components/search_engines/search_engines_pref_names.h" | 28 #include "components/search_engines/search_engines_pref_names.h" |
| 28 #include "components/search_engines/search_host_to_urls_map.h" | 29 #include "components/search_engines/search_host_to_urls_map.h" |
| 29 #include "components/search_engines/search_terms_data.h" | 30 #include "components/search_engines/search_terms_data.h" |
| 30 #include "components/search_engines/template_url.h" | 31 #include "components/search_engines/template_url.h" |
| 31 #include "components/search_engines/template_url_prepopulate_data.h" | 32 #include "components/search_engines/template_url_prepopulate_data.h" |
| 32 #include "components/search_engines/template_url_service_client.h" | 33 #include "components/search_engines/template_url_service_client.h" |
| 33 #include "components/search_engines/template_url_service_observer.h" | 34 #include "components/search_engines/template_url_service_observer.h" |
| 34 #include "components/search_engines/util.h" | 35 #include "components/search_engines/util.h" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 int num_dupes = 0; | 143 int num_dupes = 0; |
| 143 for (std::map<std::string, int>::const_iterator it = duplicates.begin(); | 144 for (std::map<std::string, int>::const_iterator it = duplicates.begin(); |
| 144 it != duplicates.end(); ++it) { | 145 it != duplicates.end(); ++it) { |
| 145 if (it->second > 1) | 146 if (it->second > 1) |
| 146 num_dupes++; | 147 num_dupes++; |
| 147 } | 148 } |
| 148 | 149 |
| 149 UMA_HISTOGRAM_COUNTS_100("Search.SearchEngineDuplicateCounts", num_dupes); | 150 UMA_HISTOGRAM_COUNTS_100("Search.SearchEngineDuplicateCounts", num_dupes); |
| 150 } | 151 } |
| 151 | 152 |
| 153 // Returns the length of the registry portion of a hostname. e.g., | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: e.g. -> E.g., or just write "For example," (2
Mark P
2015/11/11 07:41:00
Did both.
| |
| 154 // www.google.co.uk will return 5 (=length of co.uk). | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: (=length -> (the length (2 places)
Mark P
2015/11/11 07:41:00
Did both.
| |
| 155 size_t GetRegistryLength(const base::string16& host) { | |
| 156 return net::registry_controlled_domains::GetRegistryLength( | |
| 157 base::UTF16ToUTF8(host), | |
| 158 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, | |
| 159 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | |
| 160 } | |
| 161 | |
| 162 // Returns the domain name (including registry) of a hostname. e.g., | |
| 163 // www.google.co.uk will return google.co.uk. | |
| 164 base::string16 GetDomainAndRegistry(const base::string16& host) { | |
| 165 return base::UTF8ToUTF16( | |
| 166 net::registry_controlled_domains::GetDomainAndRegistry( | |
| 167 base::UTF16ToUTF8(host), | |
| 168 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); | |
| 169 } | |
| 170 | |
| 171 // Returns the length of the important part of the |keyword|, assumed to be | |
| 172 // associated with the TemplateURL. For non-auto-generated template URLs, | |
| 173 // this is simply the length of the keyword. For auto-generated keyword URLs, | |
| 174 // it is can be less. For instance, for the keyword google.co.uk, this can | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: it is -> it
Mark P
2015/11/11 07:41:00
I removed the relevant sentence. This suggestion
| |
| 175 // return 6 (=length of google). | |
| 176 size_t GetEffectiveKeywordLength(const base::string16& keyword, | |
| 177 const TemplateURL* turl) { | |
| 178 if (!turl->safe_for_autoreplace() || | |
|
Peter Kasting
2015/11/09 23:11:15
I don't really think checking for this is right, c
Mark P
2015/11/11 07:41:00
Removed the auto-generated restriction and revised
| |
| 179 OmniboxFieldTrial::KeywordRequiresRegistry()) | |
| 180 return keyword.length(); | |
| 181 const size_t registry_length = GetRegistryLength(keyword); | |
| 182 // If there's a registry, subtract the length of the registry portion of the | |
| 183 // keyword (e.g., co.uk) and an addition one for the dot that preceeds the | |
| 184 // registry. | |
| 185 return keyword.length() - ((registry_length > 0) ? (registry_length + 1) : 0); | |
|
Peter Kasting
2015/11/09 23:11:15
Make sure this doesn't return -1 if you feed in th
Mark P
2015/11/11 07:41:00
Good point. Fixed.
| |
| 186 } | |
| 187 | |
| 152 } // namespace | 188 } // namespace |
| 153 | 189 |
| 154 | 190 |
| 155 // TemplateURLService::LessWithPrefix ----------------------------------------- | 191 // TemplateURLService::LessWithPrefix ----------------------------------------- |
| 156 | 192 |
| 157 class TemplateURLService::LessWithPrefix { | 193 class TemplateURLService::LessWithPrefix { |
| 158 public: | 194 public: |
| 159 // We want to find the set of keywords that begin with a prefix. The STL | 195 // We want to find the set of keywords that begin with a prefix. The STL |
| 160 // algorithms will return the set of elements that are "equal to" the | 196 // algorithms will return the set of elements that are "equal to" the |
| 161 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When | 197 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When |
| 162 // cmp() is the typical std::less<>, this results in lexicographic equality; | 198 // cmp() is the typical std::less<>, this results in lexicographic equality; |
| 163 // we need to extend this to mark a prefix as "not less than" a keyword it | 199 // we need to extend this to mark a prefix as "not less than" a keyword it |
| 164 // begins, which will cause the desired elements to be considered "equal to" | 200 // begins, which will cause the desired elements to be considered "equal to" |
| 165 // the prefix. Note: this is still a strict weak ordering, as required by | 201 // the prefix. Note: this is still a strict weak ordering, as required by |
| 166 // equal_range() (though I will not prove that here). | 202 // equal_range() (though I will not prove that here). |
| 167 // | 203 // |
| 168 // Unfortunately the calling convention is not "prefix and element" but | 204 // Unfortunately the calling convention is not "prefix and element" but |
| 169 // rather "two elements", so we pass the prefix as a fake "element" which has | 205 // rather "two elements", so we pass the prefix as a fake "element" which has |
| 170 // a NULL KeywordDataElement pointer. | 206 // a NULL KeywordDataElement pointer. |
| 171 bool operator()(const KeywordToTemplateMap::value_type& elem1, | 207 bool operator()(const KeywordToTemplateMap::value_type& elem1, |
| 172 const KeywordToTemplateMap::value_type& elem2) const { | 208 const KeywordToTemplateMap::value_type& elem2) const { |
| 173 return (elem1.second == NULL) ? | 209 return (elem1.second.first == NULL) ? |
| 174 (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) : | 210 (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) : |
| 175 (elem1.first < elem2.first); | 211 (elem1.first < elem2.first); |
| 176 } | 212 } |
| 177 }; | 213 }; |
| 178 | 214 |
| 179 | 215 |
| 180 // TemplateURLService --------------------------------------------------------- | 216 // TemplateURLService --------------------------------------------------------- |
| 181 | 217 |
| 182 TemplateURLService::TemplateURLService( | 218 TemplateURLService::TemplateURLService( |
| 183 PrefService* prefs, | 219 PrefService* prefs, |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 // that may interfere with search queries). An easy heuristic for this is | 376 // that may interfere with search queries). An easy heuristic for this is |
| 341 // whether the user has a TemplateURL that has been manually modified (e.g., | 377 // whether the user has a TemplateURL that has been manually modified (e.g., |
| 342 // renamed) connected to the same host. | 378 // renamed) connected to the same host. |
| 343 return !url.is_valid() || url.host().empty() || | 379 return !url.is_valid() || url.host().empty() || |
| 344 CanAddAutogeneratedKeywordForHost(url.host()); | 380 CanAddAutogeneratedKeywordForHost(url.host()); |
| 345 } | 381 } |
| 346 | 382 |
| 347 void TemplateURLService::FindMatchingKeywords( | 383 void TemplateURLService::FindMatchingKeywords( |
| 348 const base::string16& prefix, | 384 const base::string16& prefix, |
| 349 bool support_replacement_only, | 385 bool support_replacement_only, |
| 350 TemplateURLVector* matches) { | 386 TemplateURLMatchesVector* matches) { |
| 351 // Sanity check args. | 387 FindMatchingKeywordsHelper( |
| 352 if (prefix.empty()) | 388 keyword_to_template_map_, prefix, support_replacement_only, matches); |
| 353 return; | 389 } |
| 354 DCHECK(matches != NULL); | |
| 355 DCHECK(matches->empty()); // The code for exact matches assumes this. | |
| 356 | 390 |
| 357 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair | 391 void TemplateURLService::FindMatchingDomainKeywords( |
| 358 TemplateURL* const kNullTemplateURL = NULL; | 392 const base::string16& prefix, |
| 359 | 393 bool support_replacement_only, |
| 360 // Find matching keyword range. Searches the element map for keywords | 394 TemplateURLMatchesVector* matches) { |
| 361 // beginning with |prefix| and stores the endpoints of the resulting set in | 395 FindMatchingKeywordsHelper( |
| 362 // |match_range|. | 396 keyword_domain_to_template_map_, prefix, support_replacement_only, |
| 363 const std::pair<KeywordToTemplateMap::const_iterator, | 397 matches); |
| 364 KeywordToTemplateMap::const_iterator> match_range( | |
| 365 std::equal_range( | |
| 366 keyword_to_template_map_.begin(), keyword_to_template_map_.end(), | |
| 367 KeywordToTemplateMap::value_type(prefix, kNullTemplateURL), | |
| 368 LessWithPrefix())); | |
| 369 | |
| 370 // Return vector of matching keywords. | |
| 371 for (KeywordToTemplateMap::const_iterator i(match_range.first); | |
| 372 i != match_range.second; ++i) { | |
| 373 if (!support_replacement_only || | |
| 374 i->second->url_ref().SupportsReplacement(search_terms_data())) | |
| 375 matches->push_back(i->second); | |
| 376 } | |
| 377 } | 398 } |
| 378 | 399 |
| 379 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( | 400 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( |
| 380 const base::string16& keyword) { | 401 const base::string16& keyword) { |
| 381 KeywordToTemplateMap::const_iterator elem( | 402 KeywordToTemplateMap::const_iterator elem( |
| 382 keyword_to_template_map_.find(keyword)); | 403 keyword_to_template_map_.find(keyword)); |
| 383 if (elem != keyword_to_template_map_.end()) | 404 if (elem != keyword_to_template_map_.end()) |
| 384 return elem->second; | 405 return elem->second.first; |
| 385 return (!loaded_ && | 406 return (!loaded_ && |
| 386 initial_default_search_provider_.get() && | 407 initial_default_search_provider_.get() && |
| 387 (initial_default_search_provider_->keyword() == keyword)) ? | 408 (initial_default_search_provider_->keyword() == keyword)) ? |
| 388 initial_default_search_provider_.get() : NULL; | 409 initial_default_search_provider_.get() : NULL; |
| 389 } | 410 } |
| 390 | 411 |
| 391 TemplateURL* TemplateURLService::GetTemplateURLForGUID( | 412 TemplateURL* TemplateURLService::GetTemplateURLForGUID( |
| 392 const std::string& sync_guid) { | 413 const std::string& sync_guid) { |
| 393 GUIDToTemplateMap::const_iterator elem(guid_to_template_map_.find(sync_guid)); | 414 GUIDToTemplateMap::const_iterator elem(guid_to_template_map_.find(sync_guid)); |
| 394 if (elem != guid_to_template_map_.end()) | 415 if (elem != guid_to_template_map_.end()) |
| (...skipping 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1407 } | 1428 } |
| 1408 | 1429 |
| 1409 // Request a server check for the correct Google URL if Google is the | 1430 // Request a server check for the correct Google URL if Google is the |
| 1410 // default search engine. | 1431 // default search engine. |
| 1411 RequestGoogleURLTrackerServerCheckIfNecessary(); | 1432 RequestGoogleURLTrackerServerCheckIfNecessary(); |
| 1412 } | 1433 } |
| 1413 | 1434 |
| 1414 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { | 1435 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { |
| 1415 const base::string16& keyword = template_url->keyword(); | 1436 const base::string16& keyword = template_url->keyword(); |
| 1416 DCHECK_NE(0U, keyword_to_template_map_.count(keyword)); | 1437 DCHECK_NE(0U, keyword_to_template_map_.count(keyword)); |
| 1417 if (keyword_to_template_map_[keyword] == template_url) { | 1438 if (keyword_to_template_map_[keyword].first == template_url) { |
| 1418 // We need to check whether the keyword can now be provided by another | 1439 // We need to check whether the keyword can now be provided by another |
| 1419 // TemplateURL. See the comments in AddToMaps() for more information on | 1440 // TemplateURL. See the comments in AddToMaps() for more information on |
| 1420 // extension keywords and how they can coexist with non-extension keywords. | 1441 // extension keywords and how they can coexist with non-extension keywords. |
| 1421 // In the case of more than one extension, we use the most recently | 1442 // In the case of more than one extension, we use the most recently |
| 1422 // installed (which will be the most recently added, which will have the | 1443 // installed (which will be the most recently added, which will have the |
| 1423 // highest ID). | 1444 // highest ID). |
| 1424 TemplateURL* best_fallback = NULL; | 1445 TemplateURL* best_fallback = NULL; |
| 1425 for (TemplateURLVector::const_iterator i(template_urls_.begin()); | 1446 for (TemplateURLVector::const_iterator i(template_urls_.begin()); |
| 1426 i != template_urls_.end(); ++i) { | 1447 i != template_urls_.end(); ++i) { |
| 1427 TemplateURL* turl = *i; | 1448 TemplateURL* turl = *i; |
| 1428 // This next statement relies on the fact that there can only be one | 1449 // This next statement relies on the fact that there can only be one |
| 1429 // non-Omnibox API TemplateURL with a given keyword. | 1450 // non-Omnibox API TemplateURL with a given keyword. |
| 1430 if ((turl != template_url) && (turl->keyword() == keyword) && | 1451 if ((turl != template_url) && (turl->keyword() == keyword) && |
| 1431 (!best_fallback || | 1452 (!best_fallback || |
| 1432 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) || | 1453 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) || |
| 1433 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && | 1454 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && |
| 1434 (turl->id() > best_fallback->id())))) | 1455 (turl->id() > best_fallback->id())))) |
| 1435 best_fallback = turl; | 1456 best_fallback = turl; |
| 1436 } | 1457 } |
| 1437 if (best_fallback) | 1458 RemoveFromDomainMap(template_url); |
| 1438 keyword_to_template_map_[keyword] = best_fallback; | 1459 if (best_fallback) { |
| 1439 else | 1460 keyword_to_template_map_[keyword] = |
| 1461 std::make_pair(best_fallback, | |
| 1462 GetEffectiveKeywordLength(keyword, best_fallback)); | |
| 1463 AddToDomainMap(best_fallback); | |
|
Peter Kasting
2015/11/09 23:11:15
You do this sort of "add to |keyword_to_template_m
Mark P
2015/11/12 20:13:03
Good idea. I added the helper and it looks a lot
| |
| 1464 } else { | |
| 1440 keyword_to_template_map_.erase(keyword); | 1465 keyword_to_template_map_.erase(keyword); |
| 1466 } | |
| 1441 } | 1467 } |
| 1442 | 1468 |
| 1443 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) | 1469 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) |
| 1444 return; | 1470 return; |
| 1445 | 1471 |
| 1446 if (!template_url->sync_guid().empty()) | 1472 if (!template_url->sync_guid().empty()) |
| 1447 guid_to_template_map_.erase(template_url->sync_guid()); | 1473 guid_to_template_map_.erase(template_url->sync_guid()); |
| 1448 // |provider_map_| is only initialized after loading has completed. | 1474 // |provider_map_| is only initialized after loading has completed. |
| 1449 if (loaded_) { | 1475 if (loaded_) { |
| 1450 provider_map_->Remove(template_url); | 1476 provider_map_->Remove(template_url); |
| 1451 } | 1477 } |
| 1452 } | 1478 } |
| 1453 | 1479 |
| 1454 void TemplateURLService::AddToMaps(TemplateURL* template_url) { | 1480 void TemplateURLService::AddToMaps(TemplateURL* template_url) { |
| 1455 bool template_url_is_omnibox_api = | 1481 bool template_url_is_omnibox_api = |
| 1456 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; | 1482 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; |
| 1457 const base::string16& keyword = template_url->keyword(); | 1483 const base::string16& keyword = template_url->keyword(); |
| 1458 KeywordToTemplateMap::const_iterator i = | 1484 KeywordToTemplateMap::const_iterator i = |
| 1459 keyword_to_template_map_.find(keyword); | 1485 keyword_to_template_map_.find(keyword); |
| 1460 if (i == keyword_to_template_map_.end()) { | 1486 if (i == keyword_to_template_map_.end()) { |
| 1461 keyword_to_template_map_[keyword] = template_url; | 1487 keyword_to_template_map_[keyword] = |
| 1488 std::make_pair(template_url, | |
| 1489 GetEffectiveKeywordLength(keyword, template_url)); | |
| 1490 AddToDomainMap(template_url); | |
| 1462 } else { | 1491 } else { |
| 1463 const TemplateURL* existing_url = i->second; | 1492 const TemplateURL* existing_url = i->second.first; |
| 1464 // We should only have overlapping keywords when at least one comes from | 1493 // We should only have overlapping keywords when at least one comes from |
| 1465 // an extension. In that case, the ranking order is: | 1494 // an extension. In that case, the ranking order is: |
| 1466 // Manually-modified keywords > extension keywords > replaceable keywords | 1495 // Manually-modified keywords > extension keywords > replaceable keywords |
| 1467 // When there are multiple extensions, the last-added wins. | 1496 // When there are multiple extensions, the last-added wins. |
| 1468 bool existing_url_is_omnibox_api = | 1497 bool existing_url_is_omnibox_api = |
| 1469 existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; | 1498 existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; |
| 1470 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api); | 1499 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api); |
| 1471 if (existing_url_is_omnibox_api ? | 1500 if (existing_url_is_omnibox_api ? |
| 1472 !CanReplace(template_url) : CanReplace(existing_url)) | 1501 !CanReplace(template_url) : CanReplace(existing_url)) { |
| 1473 keyword_to_template_map_[keyword] = template_url; | 1502 RemoveFromDomainMap(existing_url); |
| 1503 keyword_to_template_map_[keyword] = | |
| 1504 std::make_pair(template_url, | |
| 1505 GetEffectiveKeywordLength(keyword, template_url)); | |
| 1506 AddToDomainMap(template_url); | |
| 1507 } | |
| 1474 } | 1508 } |
| 1475 | 1509 |
| 1476 if (template_url_is_omnibox_api) | 1510 if (template_url_is_omnibox_api) |
| 1477 return; | 1511 return; |
| 1478 | 1512 |
| 1479 if (!template_url->sync_guid().empty()) | 1513 if (!template_url->sync_guid().empty()) |
| 1480 guid_to_template_map_[template_url->sync_guid()] = template_url; | 1514 guid_to_template_map_[template_url->sync_guid()] = template_url; |
| 1481 // |provider_map_| is only initialized after loading has completed. | 1515 // |provider_map_| is only initialized after loading has completed. |
| 1482 if (loaded_) | 1516 if (loaded_) |
| 1483 provider_map_->Add(template_url, search_terms_data()); | 1517 provider_map_->Add(template_url, search_terms_data()); |
| 1484 } | 1518 } |
| 1485 | 1519 |
| 1520 void TemplateURLService::RemoveFromDomainMap(const TemplateURL* template_url) { | |
| 1521 const base::string16 domain = GetDomainAndRegistry(template_url->keyword()); | |
| 1522 if (domain.empty()) | |
| 1523 return; | |
| 1524 | |
| 1525 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: We no longer compile with VS2010, so let's re
Mark P
2015/11/11 07:41:01
Now unneeded in this location.
Done in the other l
| |
| 1526 TemplateURL* const kNullTemplateURL = NULL; | |
| 1527 | |
| 1528 // Find all entries with the same domain name. | |
| 1529 const std::pair<KeywordDomainToTemplateMap::const_iterator, | |
| 1530 KeywordDomainToTemplateMap::const_iterator> match_range( | |
| 1531 std::equal_range( | |
| 1532 keyword_domain_to_template_map_.begin(), | |
| 1533 keyword_domain_to_template_map_.end(), | |
| 1534 KeywordDomainToTemplateMap::value_type( | |
| 1535 domain, std::make_pair(kNullTemplateURL, 0)))); | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: I feel like it should be possible to replace
Mark P
2015/11/11 07:41:00
That's no shorter.
Also, I usually see make_pair
| |
| 1536 | |
| 1537 // Delete the template URL in question. | |
| 1538 for (KeywordDomainToTemplateMap::const_iterator it(match_range.first); | |
| 1539 it != match_range.second; ++it) { | |
| 1540 if (it->second.first == template_url) | |
| 1541 keyword_domain_to_template_map_.erase(it); | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: Prefer std::erase(std::remove_if()) with a la
Mark P
2015/11/11 07:41:00
I have simplified this in a different way that may
| |
| 1542 } | |
| 1543 } | |
| 1544 | |
| 1545 void TemplateURLService::AddToDomainMap(TemplateURL* template_url) { | |
| 1546 // If this TemplateURL was auto-generated, consider adding it to the domain | |
| 1547 // map. | |
| 1548 if (template_url->safe_for_autoreplace()) { | |
| 1549 const base::string16 domain = GetDomainAndRegistry(template_url->keyword()); | |
| 1550 // Only bother adding an entry to the domain map if its key in the domain | |
| 1551 // map would be different from the key in the regular map. | |
| 1552 if (domain != template_url->keyword()) { | |
| 1553 keyword_domain_to_template_map_.insert(std::make_pair( | |
| 1554 domain, | |
| 1555 std::make_pair(template_url, | |
| 1556 GetEffectiveKeywordLength(domain, template_url)))); | |
| 1557 } | |
| 1558 } | |
| 1559 } | |
| 1560 | |
| 1486 // Helper for partition() call in next function. | 1561 // Helper for partition() call in next function. |
| 1487 bool HasValidID(TemplateURL* t_url) { | 1562 bool HasValidID(TemplateURL* t_url) { |
| 1488 return t_url->id() != kInvalidTemplateURLID; | 1563 return t_url->id() != kInvalidTemplateURLID; |
| 1489 } | 1564 } |
| 1490 | 1565 |
| 1491 void TemplateURLService::SetTemplateURLs(TemplateURLVector* urls) { | 1566 void TemplateURLService::SetTemplateURLs(TemplateURLVector* urls) { |
| 1492 // Partition the URLs first, instead of implementing the loops below by simply | 1567 // Partition the URLs first, instead of implementing the loops below by simply |
| 1493 // scanning the input twice. While it's not supposed to happen normally, it's | 1568 // scanning the input twice. While it's not supposed to happen normally, it's |
| 1494 // possible for corrupt databases to return multiple entries with the same | 1569 // possible for corrupt databases to return multiple entries with the same |
| 1495 // keyword. In this case, the first loop may delete the first entry when | 1570 // keyword. In this case, the first loop may delete the first entry when |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1587 const TemplateURL& new_values) { | 1662 const TemplateURL& new_values) { |
| 1588 DCHECK(existing_turl); | 1663 DCHECK(existing_turl); |
| 1589 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == | 1664 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == |
| 1590 template_urls_.end()) | 1665 template_urls_.end()) |
| 1591 return false; | 1666 return false; |
| 1592 | 1667 |
| 1593 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType()); | 1668 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType()); |
| 1594 | 1669 |
| 1595 base::string16 old_keyword(existing_turl->keyword()); | 1670 base::string16 old_keyword(existing_turl->keyword()); |
| 1596 keyword_to_template_map_.erase(old_keyword); | 1671 keyword_to_template_map_.erase(old_keyword); |
| 1672 RemoveFromDomainMap(existing_turl); | |
| 1597 if (!existing_turl->sync_guid().empty()) | 1673 if (!existing_turl->sync_guid().empty()) |
| 1598 guid_to_template_map_.erase(existing_turl->sync_guid()); | 1674 guid_to_template_map_.erase(existing_turl->sync_guid()); |
| 1599 | 1675 |
| 1600 // |provider_map_| is only initialized after loading has completed. | 1676 // |provider_map_| is only initialized after loading has completed. |
| 1601 if (loaded_) | 1677 if (loaded_) |
| 1602 provider_map_->Remove(existing_turl); | 1678 provider_map_->Remove(existing_turl); |
| 1603 | 1679 |
| 1604 TemplateURLID previous_id = existing_turl->id(); | 1680 TemplateURLID previous_id = existing_turl->id(); |
| 1605 existing_turl->CopyFrom(new_values); | 1681 existing_turl->CopyFrom(new_values); |
| 1606 existing_turl->data_.id = previous_id; | 1682 existing_turl->data_.id = previous_id; |
| 1607 | 1683 |
| 1608 if (loaded_) { | 1684 if (loaded_) { |
| 1609 provider_map_->Add(existing_turl, search_terms_data()); | 1685 provider_map_->Add(existing_turl, search_terms_data()); |
| 1610 } | 1686 } |
| 1611 | 1687 |
| 1612 const base::string16& keyword = existing_turl->keyword(); | 1688 const base::string16& keyword = existing_turl->keyword(); |
| 1613 KeywordToTemplateMap::const_iterator i = | 1689 KeywordToTemplateMap::const_iterator i = |
| 1614 keyword_to_template_map_.find(keyword); | 1690 keyword_to_template_map_.find(keyword); |
| 1615 if (i == keyword_to_template_map_.end()) { | 1691 if (i == keyword_to_template_map_.end()) { |
| 1616 keyword_to_template_map_[keyword] = existing_turl; | 1692 keyword_to_template_map_[keyword] = |
| 1693 std::make_pair(existing_turl, | |
| 1694 GetEffectiveKeywordLength(keyword, existing_turl)); | |
| 1695 AddToDomainMap(existing_turl); | |
| 1617 } else { | 1696 } else { |
| 1618 // We can theoretically reach here in two cases: | 1697 // We can theoretically reach here in two cases: |
| 1619 // * There is an existing extension keyword and sync brings in a rename of | 1698 // * There is an existing extension keyword and sync brings in a rename of |
| 1620 // a non-extension keyword to match. In this case we just need to pick | 1699 // a non-extension keyword to match. In this case we just need to pick |
| 1621 // which keyword has priority to update the keyword map. | 1700 // which keyword has priority to update the keyword map. |
| 1622 // * Autogeneration of the keyword for a Google default search provider | 1701 // * Autogeneration of the keyword for a Google default search provider |
| 1623 // at load time causes it to conflict with an existing keyword. In this | 1702 // at load time causes it to conflict with an existing keyword. In this |
| 1624 // case we delete the existing keyword if it's replaceable, or else undo | 1703 // case we delete the existing keyword if it's replaceable, or else undo |
| 1625 // the change in keyword for |existing_turl|. | 1704 // the change in keyword for |existing_turl|. |
| 1626 TemplateURL* existing_keyword_turl = i->second; | 1705 TemplateURL* existing_keyword_turl = i->second.first; |
| 1627 if (existing_keyword_turl->GetType() != TemplateURL::NORMAL) { | 1706 if (existing_keyword_turl->GetType() != TemplateURL::NORMAL) { |
| 1628 if (!CanReplace(existing_turl)) | 1707 if (!CanReplace(existing_turl)) { |
| 1629 keyword_to_template_map_[keyword] = existing_turl; | 1708 keyword_to_template_map_[keyword] = |
| 1709 std::make_pair(existing_turl, | |
| 1710 GetEffectiveKeywordLength(keyword, existing_turl)); | |
| 1711 AddToDomainMap(existing_turl); | |
| 1712 } | |
| 1630 } else { | 1713 } else { |
| 1631 if (CanReplace(existing_keyword_turl)) { | 1714 if (CanReplace(existing_keyword_turl)) { |
| 1632 RemoveNoNotify(existing_keyword_turl); | 1715 RemoveNoNotify(existing_keyword_turl); |
| 1633 } else { | 1716 } else { |
| 1634 existing_turl->data_.SetKeyword(old_keyword); | 1717 existing_turl->data_.SetKeyword(old_keyword); |
| 1635 keyword_to_template_map_[old_keyword] = existing_turl; | 1718 keyword_to_template_map_[old_keyword] = |
| 1719 std::make_pair(existing_turl, | |
| 1720 GetEffectiveKeywordLength(old_keyword, | |
| 1721 existing_turl)); | |
| 1722 AddToDomainMap(existing_turl); | |
| 1636 } | 1723 } |
| 1637 } | 1724 } |
| 1638 } | 1725 } |
| 1639 if (!existing_turl->sync_guid().empty()) | 1726 if (!existing_turl->sync_guid().empty()) |
| 1640 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; | 1727 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; |
| 1641 | 1728 |
| 1642 if (web_data_service_.get()) | 1729 if (web_data_service_.get()) |
| 1643 web_data_service_->UpdateKeyword(existing_turl->data()); | 1730 web_data_service_->UpdateKeyword(existing_turl->data()); |
| 1644 | 1731 |
| 1645 // Inform sync of the update. | 1732 // Inform sync of the update. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1750 bool something_changed = false; | 1837 bool something_changed = false; |
| 1751 for (TemplateURLVector::iterator i(template_urls_.begin()); | 1838 for (TemplateURLVector::iterator i(template_urls_.begin()); |
| 1752 i != template_urls_.end(); ++i) { | 1839 i != template_urls_.end(); ++i) { |
| 1753 TemplateURL* t_url = *i; | 1840 TemplateURL* t_url = *i; |
| 1754 if (t_url->HasGoogleBaseURLs(search_terms_data())) { | 1841 if (t_url->HasGoogleBaseURLs(search_terms_data())) { |
| 1755 TemplateURL updated_turl(t_url->data()); | 1842 TemplateURL updated_turl(t_url->data()); |
| 1756 updated_turl.ResetKeywordIfNecessary(search_terms_data(), false); | 1843 updated_turl.ResetKeywordIfNecessary(search_terms_data(), false); |
| 1757 KeywordToTemplateMap::const_iterator existing_entry = | 1844 KeywordToTemplateMap::const_iterator existing_entry = |
| 1758 keyword_to_template_map_.find(updated_turl.keyword()); | 1845 keyword_to_template_map_.find(updated_turl.keyword()); |
| 1759 if ((existing_entry != keyword_to_template_map_.end()) && | 1846 if ((existing_entry != keyword_to_template_map_.end()) && |
| 1760 (existing_entry->second != t_url)) { | 1847 (existing_entry->second.first != t_url)) { |
| 1761 // The new autogenerated keyword conflicts with another TemplateURL. | 1848 // The new autogenerated keyword conflicts with another TemplateURL. |
| 1762 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its | 1849 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its |
| 1763 // current keyword. (This will not prevent |t_url| from auto-updating | 1850 // current keyword. (This will not prevent |t_url| from auto-updating |
| 1764 // the keyword in the future if the conflicting TemplateURL disappears.) | 1851 // the keyword in the future if the conflicting TemplateURL disappears.) |
| 1765 // Note that we must still update |t_url| in this case, or the | 1852 // Note that we must still update |t_url| in this case, or the |
| 1766 // |provider_map_| will not be updated correctly. | 1853 // |provider_map_| will not be updated correctly. |
| 1767 if (CanReplace(existing_entry->second)) | 1854 if (CanReplace(existing_entry->second.first)) |
| 1768 RemoveNoNotify(existing_entry->second); | 1855 RemoveNoNotify(existing_entry->second.first); |
| 1769 else | 1856 else |
| 1770 updated_turl.data_.SetKeyword(t_url->keyword()); | 1857 updated_turl.data_.SetKeyword(t_url->keyword()); |
| 1771 } | 1858 } |
| 1772 something_changed = true; | 1859 something_changed = true; |
| 1773 // This will send the keyword change to sync. Note that other clients | 1860 // This will send the keyword change to sync. Note that other clients |
| 1774 // need to reset the keyword to an appropriate local value when this | 1861 // need to reset the keyword to an appropriate local value when this |
| 1775 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData(). | 1862 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData(). |
| 1776 UpdateNoNotify(t_url, updated_turl); | 1863 UpdateNoNotify(t_url, updated_turl); |
| 1777 } | 1864 } |
| 1778 } | 1865 } |
| (...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2273 if (new_guid.empty()) { | 2360 if (new_guid.empty()) { |
| 2274 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); | 2361 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); |
| 2275 return; | 2362 return; |
| 2276 } | 2363 } |
| 2277 | 2364 |
| 2278 TemplateURL* turl = GetTemplateURLForGUID(new_guid); | 2365 TemplateURL* turl = GetTemplateURLForGUID(new_guid); |
| 2279 if (turl) | 2366 if (turl) |
| 2280 default_search_manager_.SetUserSelectedDefaultSearchEngine(turl->data()); | 2367 default_search_manager_.SetUserSelectedDefaultSearchEngine(turl->data()); |
| 2281 } | 2368 } |
| 2282 | 2369 |
| 2370 template <typename Container> | |
| 2371 void TemplateURLService::FindMatchingKeywordsHelper( | |
| 2372 const Container& keyword_to_template_map, | |
| 2373 const base::string16& prefix, | |
| 2374 bool support_replacement_only, | |
| 2375 TemplateURLMatchesVector* matches) { | |
| 2376 // Sanity check args. | |
| 2377 if (prefix.empty()) | |
| 2378 return; | |
| 2379 DCHECK(matches != NULL); | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: DCHECK(matches); would be shorter
Mark P
2015/11/11 07:41:00
Done.
| |
| 2380 | |
| 2381 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair | |
| 2382 TemplateURL* const kNullTemplateURL = NULL; | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: See earlier comment
Mark P
2015/11/11 07:41:00
Done.
| |
| 2383 | |
| 2384 // Find matching keyword range. Searches the element map for keywords | |
| 2385 // beginning with |prefix| and stores the endpoints of the resulting set in | |
| 2386 // |match_range|. | |
| 2387 const std::pair<typename Container::const_iterator, | |
| 2388 typename Container::const_iterator> match_range( | |
| 2389 std::equal_range( | |
| 2390 keyword_to_template_map.begin(), keyword_to_template_map.end(), | |
| 2391 typename Container::value_type(prefix, | |
| 2392 std::make_pair(kNullTemplateURL, 0)), | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: See earlier comment
Mark P
2015/11/11 07:41:00
See earlier answer. :-)
Peter Kasting
2015/11/12 20:36:10
It may not be shorter to write, but I think just w
Peter Kasting
2015/11/12 22:32:11
You didn't reply to this.
Mark P
2015/11/12 23:18:41
Okay, it's plausible. Done here and everywhere th
| |
| 2393 LessWithPrefix())); | |
| 2394 | |
| 2395 // Return vector of matching keywords. | |
| 2396 for (typename Container::const_iterator i(match_range.first); | |
| 2397 i != match_range.second; ++i) { | |
| 2398 if (!support_replacement_only || | |
| 2399 i->second.first->url_ref().SupportsReplacement(search_terms_data())) | |
| 2400 matches->push_back(i->second); | |
| 2401 } | |
|
Peter Kasting
2015/11/09 23:11:15
Nit: Prefer std::copy_if() with a lambda
Mark P
2015/11/11 07:41:00
Not done. I couldn't figure out the magical incan
Peter Kasting
2015/11/12 20:36:10
You're right. I overlooked the difference in cont
Mark P
2015/11/12 23:18:41
Acknowledged.
| |
| 2402 } | |
| 2403 | |
| 2283 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL( | 2404 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL( |
| 2284 int prepopulated_id) { | 2405 int prepopulated_id) { |
| 2285 for (TemplateURLVector::const_iterator i = template_urls_.begin(); | 2406 for (TemplateURLVector::const_iterator i = template_urls_.begin(); |
| 2286 i != template_urls_.end(); ++i) { | 2407 i != template_urls_.end(); ++i) { |
| 2287 if ((*i)->prepopulate_id() == prepopulated_id) | 2408 if ((*i)->prepopulate_id() == prepopulated_id) |
| 2288 return *i; | 2409 return *i; |
| 2289 } | 2410 } |
| 2290 return NULL; | 2411 return NULL; |
| 2291 } | 2412 } |
| 2292 | 2413 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2331 | 2452 |
| 2332 if (most_recently_intalled_default) { | 2453 if (most_recently_intalled_default) { |
| 2333 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2454 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 2334 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | 2455 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); |
| 2335 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | 2456 default_search_manager_.SetExtensionControlledDefaultSearchEngine( |
| 2336 most_recently_intalled_default->data()); | 2457 most_recently_intalled_default->data()); |
| 2337 } else { | 2458 } else { |
| 2338 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | 2459 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); |
| 2339 } | 2460 } |
| 2340 } | 2461 } |
| OLD | NEW |