Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: components/search_engines/template_url_service.cc

Issue 1411543011: Omnibox: Make Keyword Provide More Generous with Matching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: all peter's comments (except one), including previously-promised refactoring, and additional unit t… Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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. For example,
154 // www.google.co.uk will return 5 (the length of co.uk).
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. For example,
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 instance, for the keyword
173 // google.co.uk, this can return 6 (the length of "google").
174 size_t GetMeaningfulKeywordLength(const base::string16& keyword,
175 const TemplateURL* turl) {
176 if (OmniboxFieldTrial::KeywordRequiresRegistry())
177 return keyword.length();
178 const size_t registry_length = GetRegistryLength(keyword);
179 // The meaningful keyword length is the length of any portion before the
180 // registry ("co.uk") and its preceding dot.
181 return std::max<int>(
182 keyword.length() - (registry_length ? (registry_length + 1) : 0), 0);
Peter Kasting 2015/11/12 22:32:11 Actually, I'm not convinced this will do the right
Mark P 2015/11/12 23:18:41 I don't think so. Suppose the user has a keyword
Peter Kasting 2015/11/12 23:37:51 No, it won't; the RCDS functions are all guarantee
Mark P 2015/11/12 23:49:34 Okay, revised and added a DCHECK to confirm your a
183 }
184
152 } // namespace 185 } // namespace
153 186
154 187
155 // TemplateURLService::LessWithPrefix ----------------------------------------- 188 // TemplateURLService::LessWithPrefix -----------------------------------------
156 189
157 class TemplateURLService::LessWithPrefix { 190 class TemplateURLService::LessWithPrefix {
158 public: 191 public:
159 // We want to find the set of keywords that begin with a prefix. The STL 192 // 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 193 // 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 194 // 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; 195 // 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 196 // 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" 197 // 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 198 // the prefix. Note: this is still a strict weak ordering, as required by
166 // equal_range() (though I will not prove that here). 199 // equal_range() (though I will not prove that here).
167 // 200 //
168 // Unfortunately the calling convention is not "prefix and element" but 201 // 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 202 // rather "two elements", so we pass the prefix as a fake "element" which has
170 // a NULL KeywordDataElement pointer. 203 // a NULL KeywordDataElement pointer.
171 bool operator()(const KeywordToTemplateMap::value_type& elem1, 204 bool operator()(const KeywordToTemplateMap::value_type& elem1,
172 const KeywordToTemplateMap::value_type& elem2) const { 205 const KeywordToTemplateMap::value_type& elem2) const {
173 return (elem1.second == NULL) ? 206 return (elem1.second.first == NULL) ?
174 (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) : 207 (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) :
175 (elem1.first < elem2.first); 208 (elem1.first < elem2.first);
176 } 209 }
177 }; 210 };
178 211
179 212
180 // TemplateURLService --------------------------------------------------------- 213 // TemplateURLService ---------------------------------------------------------
181 214
182 TemplateURLService::TemplateURLService( 215 TemplateURLService::TemplateURLService(
183 PrefService* prefs, 216 PrefService* prefs,
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 // We don't have a TemplateURL with keyword. We still may not allow this 370 // We don't have a TemplateURL with keyword. We still may not allow this
338 // keyword if there's evidence we may have created this keyword before and 371 // keyword if there's evidence we may have created this keyword before and
339 // the user renamed it (because, for instance, the keyword is a common word 372 // the user renamed it (because, for instance, the keyword is a common word
340 // that may interfere with search queries). An easy heuristic for this is 373 // 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., 374 // whether the user has a TemplateURL that has been manually modified (e.g.,
342 // renamed) connected to the same host. 375 // renamed) connected to the same host.
343 return !url.is_valid() || url.host().empty() || 376 return !url.is_valid() || url.host().empty() ||
344 CanAddAutogeneratedKeywordForHost(url.host()); 377 CanAddAutogeneratedKeywordForHost(url.host());
345 } 378 }
346 379
347 void TemplateURLService::FindMatchingKeywords( 380 void TemplateURLService::AddMatchingKeywords(
348 const base::string16& prefix, 381 const base::string16& prefix,
349 bool support_replacement_only, 382 bool supports_replacement_only,
350 TemplateURLVector* matches) { 383 TURLsAndMeaningfulLengths* matches) {
351 // Sanity check args. 384 AddMatchingKeywordsHelper(
352 if (prefix.empty()) 385 keyword_to_template_map_, prefix, supports_replacement_only, matches);
353 return; 386 }
354 DCHECK(matches != NULL);
355 DCHECK(matches->empty()); // The code for exact matches assumes this.
356 387
357 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair 388 void TemplateURLService::AddMatchingDomainKeywords(
358 TemplateURL* const kNullTemplateURL = NULL; 389 const base::string16& prefix,
359 390 bool supports_replacement_only,
360 // Find matching keyword range. Searches the element map for keywords 391 TURLsAndMeaningfulLengths* matches) {
361 // beginning with |prefix| and stores the endpoints of the resulting set in 392 AddMatchingKeywordsHelper(
362 // |match_range|. 393 keyword_domain_to_template_map_, prefix, supports_replacement_only,
363 const std::pair<KeywordToTemplateMap::const_iterator, 394 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 } 395 }
378 396
379 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( 397 TemplateURL* TemplateURLService::GetTemplateURLForKeyword(
380 const base::string16& keyword) { 398 const base::string16& keyword) {
381 KeywordToTemplateMap::const_iterator elem( 399 KeywordToTemplateMap::const_iterator elem(
382 keyword_to_template_map_.find(keyword)); 400 keyword_to_template_map_.find(keyword));
383 if (elem != keyword_to_template_map_.end()) 401 if (elem != keyword_to_template_map_.end())
384 return elem->second; 402 return elem->second.first;
385 return (!loaded_ && 403 return (!loaded_ &&
386 initial_default_search_provider_.get() && 404 initial_default_search_provider_.get() &&
387 (initial_default_search_provider_->keyword() == keyword)) ? 405 (initial_default_search_provider_->keyword() == keyword)) ?
388 initial_default_search_provider_.get() : NULL; 406 initial_default_search_provider_.get() : NULL;
389 } 407 }
390 408
391 TemplateURL* TemplateURLService::GetTemplateURLForGUID( 409 TemplateURL* TemplateURLService::GetTemplateURLForGUID(
392 const std::string& sync_guid) { 410 const std::string& sync_guid) {
393 GUIDToTemplateMap::const_iterator elem(guid_to_template_map_.find(sync_guid)); 411 GUIDToTemplateMap::const_iterator elem(guid_to_template_map_.find(sync_guid));
394 if (elem != guid_to_template_map_.end()) 412 if (elem != guid_to_template_map_.end())
(...skipping 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 } 1425 }
1408 1426
1409 // Request a server check for the correct Google URL if Google is the 1427 // Request a server check for the correct Google URL if Google is the
1410 // default search engine. 1428 // default search engine.
1411 RequestGoogleURLTrackerServerCheckIfNecessary(); 1429 RequestGoogleURLTrackerServerCheckIfNecessary();
1412 } 1430 }
1413 1431
1414 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { 1432 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) {
1415 const base::string16& keyword = template_url->keyword(); 1433 const base::string16& keyword = template_url->keyword();
1416 DCHECK_NE(0U, keyword_to_template_map_.count(keyword)); 1434 DCHECK_NE(0U, keyword_to_template_map_.count(keyword));
1417 if (keyword_to_template_map_[keyword] == template_url) { 1435 if (keyword_to_template_map_[keyword].first == template_url) {
1418 // We need to check whether the keyword can now be provided by another 1436 // We need to check whether the keyword can now be provided by another
1419 // TemplateURL. See the comments in AddToMaps() for more information on 1437 // TemplateURL. See the comments in AddToMaps() for more information on
1420 // extension keywords and how they can coexist with non-extension keywords. 1438 // 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 1439 // 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 1440 // installed (which will be the most recently added, which will have the
1423 // highest ID). 1441 // highest ID).
1424 TemplateURL* best_fallback = NULL; 1442 TemplateURL* best_fallback = NULL;
1425 for (TemplateURLVector::const_iterator i(template_urls_.begin()); 1443 for (TemplateURLVector::const_iterator i(template_urls_.begin());
1426 i != template_urls_.end(); ++i) { 1444 i != template_urls_.end(); ++i) {
1427 TemplateURL* turl = *i; 1445 TemplateURL* turl = *i;
1428 // This next statement relies on the fact that there can only be one 1446 // This next statement relies on the fact that there can only be one
1429 // non-Omnibox API TemplateURL with a given keyword. 1447 // non-Omnibox API TemplateURL with a given keyword.
1430 if ((turl != template_url) && (turl->keyword() == keyword) && 1448 if ((turl != template_url) && (turl->keyword() == keyword) &&
1431 (!best_fallback || 1449 (!best_fallback ||
1432 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) || 1450 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) ||
1433 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && 1451 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
1434 (turl->id() > best_fallback->id())))) 1452 (turl->id() > best_fallback->id()))))
1435 best_fallback = turl; 1453 best_fallback = turl;
1436 } 1454 }
1437 if (best_fallback) 1455 RemoveFromDomainMap(template_url);
1438 keyword_to_template_map_[keyword] = best_fallback; 1456 if (best_fallback) {
1439 else 1457 AddToMap(&keyword_to_template_map_, keyword, best_fallback);
1458 AddToDomainMap(best_fallback);
1459 } else {
1440 keyword_to_template_map_.erase(keyword); 1460 keyword_to_template_map_.erase(keyword);
1461 }
1441 } 1462 }
1442 1463
1443 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) 1464 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)
1444 return; 1465 return;
1445 1466
1446 if (!template_url->sync_guid().empty()) 1467 if (!template_url->sync_guid().empty())
1447 guid_to_template_map_.erase(template_url->sync_guid()); 1468 guid_to_template_map_.erase(template_url->sync_guid());
1448 // |provider_map_| is only initialized after loading has completed. 1469 // |provider_map_| is only initialized after loading has completed.
1449 if (loaded_) { 1470 if (loaded_) {
1450 provider_map_->Remove(template_url); 1471 provider_map_->Remove(template_url);
1451 } 1472 }
1452 } 1473 }
1453 1474
1454 void TemplateURLService::AddToMaps(TemplateURL* template_url) { 1475 void TemplateURLService::AddToMaps(TemplateURL* template_url) {
1455 bool template_url_is_omnibox_api = 1476 bool template_url_is_omnibox_api =
1456 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; 1477 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION;
1457 const base::string16& keyword = template_url->keyword(); 1478 const base::string16& keyword = template_url->keyword();
1458 KeywordToTemplateMap::const_iterator i = 1479 KeywordToTemplateMap::const_iterator i =
1459 keyword_to_template_map_.find(keyword); 1480 keyword_to_template_map_.find(keyword);
1460 if (i == keyword_to_template_map_.end()) { 1481 if (i == keyword_to_template_map_.end()) {
1461 keyword_to_template_map_[keyword] = template_url; 1482 AddToMap(&keyword_to_template_map_, keyword, template_url);
1483 AddToDomainMap(template_url);
1462 } else { 1484 } else {
1463 const TemplateURL* existing_url = i->second; 1485 const TemplateURL* existing_url = i->second.first;
1464 // We should only have overlapping keywords when at least one comes from 1486 // We should only have overlapping keywords when at least one comes from
1465 // an extension. In that case, the ranking order is: 1487 // an extension. In that case, the ranking order is:
1466 // Manually-modified keywords > extension keywords > replaceable keywords 1488 // Manually-modified keywords > extension keywords > replaceable keywords
1467 // When there are multiple extensions, the last-added wins. 1489 // When there are multiple extensions, the last-added wins.
1468 bool existing_url_is_omnibox_api = 1490 bool existing_url_is_omnibox_api =
1469 existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; 1491 existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION;
1470 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api); 1492 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api);
1471 if (existing_url_is_omnibox_api ? 1493 if (existing_url_is_omnibox_api ?
1472 !CanReplace(template_url) : CanReplace(existing_url)) 1494 !CanReplace(template_url) : CanReplace(existing_url)) {
1473 keyword_to_template_map_[keyword] = template_url; 1495 RemoveFromDomainMap(existing_url);
1496 AddToMap(&keyword_to_template_map_, keyword, template_url);
1497 AddToDomainMap(template_url);
1498 }
1474 } 1499 }
1475 1500
1476 if (template_url_is_omnibox_api) 1501 if (template_url_is_omnibox_api)
1477 return; 1502 return;
1478 1503
1479 if (!template_url->sync_guid().empty()) 1504 if (!template_url->sync_guid().empty())
1480 guid_to_template_map_[template_url->sync_guid()] = template_url; 1505 guid_to_template_map_[template_url->sync_guid()] = template_url;
1481 // |provider_map_| is only initialized after loading has completed. 1506 // |provider_map_| is only initialized after loading has completed.
1482 if (loaded_) 1507 if (loaded_)
1483 provider_map_->Add(template_url, search_terms_data()); 1508 provider_map_->Add(template_url, search_terms_data());
1484 } 1509 }
1485 1510
1511 void TemplateURLService::RemoveFromDomainMap(const TemplateURL* template_url) {
1512 const base::string16 domain = GetDomainAndRegistry(template_url->keyword());
1513 if (domain.empty())
1514 return;
1515
1516 // Cached for efficiency.
1517 const KeywordDomainToTemplateMap::const_iterator upper_bound =
1518 keyword_domain_to_template_map_.upper_bound(domain);
1519 for (KeywordDomainToTemplateMap::const_iterator it(
1520 keyword_domain_to_template_map_.lower_bound(domain));
1521 it != upper_bound; ) {
1522 if (it->second.first == template_url)
1523 it = keyword_domain_to_template_map_.erase(it);
1524 else
1525 ++it;
1526 }
1527 }
1528
1529 void TemplateURLService::AddToDomainMap(TemplateURL* template_url) {
1530 const base::string16 domain = GetDomainAndRegistry(template_url->keyword());
1531 // Only bother adding an entry to the domain map if its key in the domain
1532 // map would be different from the key in the regular map.
1533 if (domain != template_url->keyword())
1534 AddToMap(&keyword_domain_to_template_map_, domain, template_url);
1535 }
1536
1537 // static
1538 template <typename Container>
1539 void TemplateURLService::AddToMap(
1540 Container* keyword_to_template_map,
1541 const base::string16& keyword,
1542 TemplateURL* template_url) {
1543 keyword_to_template_map->insert(std::make_pair(
1544 keyword,
1545 std::make_pair(template_url,
1546 GetMeaningfulKeywordLength(keyword, template_url))));
1547 }
1548
1486 // Helper for partition() call in next function. 1549 // Helper for partition() call in next function.
1487 bool HasValidID(TemplateURL* t_url) { 1550 bool HasValidID(TemplateURL* t_url) {
1488 return t_url->id() != kInvalidTemplateURLID; 1551 return t_url->id() != kInvalidTemplateURLID;
1489 } 1552 }
1490 1553
1491 void TemplateURLService::SetTemplateURLs(TemplateURLVector* urls) { 1554 void TemplateURLService::SetTemplateURLs(TemplateURLVector* urls) {
1492 // Partition the URLs first, instead of implementing the loops below by simply 1555 // 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 1556 // 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 1557 // 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 1558 // 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
1587 const TemplateURL& new_values) { 1650 const TemplateURL& new_values) {
1588 DCHECK(existing_turl); 1651 DCHECK(existing_turl);
1589 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == 1652 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) ==
1590 template_urls_.end()) 1653 template_urls_.end())
1591 return false; 1654 return false;
1592 1655
1593 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType()); 1656 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType());
1594 1657
1595 base::string16 old_keyword(existing_turl->keyword()); 1658 base::string16 old_keyword(existing_turl->keyword());
1596 keyword_to_template_map_.erase(old_keyword); 1659 keyword_to_template_map_.erase(old_keyword);
1660 RemoveFromDomainMap(existing_turl);
1597 if (!existing_turl->sync_guid().empty()) 1661 if (!existing_turl->sync_guid().empty())
1598 guid_to_template_map_.erase(existing_turl->sync_guid()); 1662 guid_to_template_map_.erase(existing_turl->sync_guid());
1599 1663
1600 // |provider_map_| is only initialized after loading has completed. 1664 // |provider_map_| is only initialized after loading has completed.
1601 if (loaded_) 1665 if (loaded_)
1602 provider_map_->Remove(existing_turl); 1666 provider_map_->Remove(existing_turl);
1603 1667
1604 TemplateURLID previous_id = existing_turl->id(); 1668 TemplateURLID previous_id = existing_turl->id();
1605 existing_turl->CopyFrom(new_values); 1669 existing_turl->CopyFrom(new_values);
1606 existing_turl->data_.id = previous_id; 1670 existing_turl->data_.id = previous_id;
1607 1671
1608 if (loaded_) { 1672 if (loaded_) {
1609 provider_map_->Add(existing_turl, search_terms_data()); 1673 provider_map_->Add(existing_turl, search_terms_data());
1610 } 1674 }
1611 1675
1612 const base::string16& keyword = existing_turl->keyword(); 1676 const base::string16& keyword = existing_turl->keyword();
1613 KeywordToTemplateMap::const_iterator i = 1677 KeywordToTemplateMap::const_iterator i =
1614 keyword_to_template_map_.find(keyword); 1678 keyword_to_template_map_.find(keyword);
1615 if (i == keyword_to_template_map_.end()) { 1679 if (i == keyword_to_template_map_.end()) {
1616 keyword_to_template_map_[keyword] = existing_turl; 1680 AddToMap(&keyword_to_template_map_, keyword, existing_turl);
1681 AddToDomainMap(existing_turl);
1617 } else { 1682 } else {
1618 // We can theoretically reach here in two cases: 1683 // We can theoretically reach here in two cases:
1619 // * There is an existing extension keyword and sync brings in a rename of 1684 // * 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 1685 // a non-extension keyword to match. In this case we just need to pick
1621 // which keyword has priority to update the keyword map. 1686 // which keyword has priority to update the keyword map.
1622 // * Autogeneration of the keyword for a Google default search provider 1687 // * Autogeneration of the keyword for a Google default search provider
1623 // at load time causes it to conflict with an existing keyword. In this 1688 // 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 1689 // case we delete the existing keyword if it's replaceable, or else undo
1625 // the change in keyword for |existing_turl|. 1690 // the change in keyword for |existing_turl|.
1626 TemplateURL* existing_keyword_turl = i->second; 1691 TemplateURL* existing_keyword_turl = i->second.first;
1627 if (existing_keyword_turl->GetType() != TemplateURL::NORMAL) { 1692 if (existing_keyword_turl->GetType() != TemplateURL::NORMAL) {
1628 if (!CanReplace(existing_turl)) 1693 if (!CanReplace(existing_turl)) {
1629 keyword_to_template_map_[keyword] = existing_turl; 1694 AddToMap(&keyword_to_template_map_, keyword, existing_turl);
1695 AddToDomainMap(existing_turl);
1696 }
1630 } else { 1697 } else {
1631 if (CanReplace(existing_keyword_turl)) { 1698 if (CanReplace(existing_keyword_turl)) {
1632 RemoveNoNotify(existing_keyword_turl); 1699 RemoveNoNotify(existing_keyword_turl);
1633 } else { 1700 } else {
1634 existing_turl->data_.SetKeyword(old_keyword); 1701 existing_turl->data_.SetKeyword(old_keyword);
1635 keyword_to_template_map_[old_keyword] = existing_turl; 1702 AddToMap(&keyword_to_template_map_, old_keyword, existing_turl);
1703 AddToDomainMap(existing_turl);
1636 } 1704 }
1637 } 1705 }
1638 } 1706 }
1639 if (!existing_turl->sync_guid().empty()) 1707 if (!existing_turl->sync_guid().empty())
1640 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; 1708 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl;
1641 1709
1642 if (web_data_service_.get()) 1710 if (web_data_service_.get())
1643 web_data_service_->UpdateKeyword(existing_turl->data()); 1711 web_data_service_->UpdateKeyword(existing_turl->data());
1644 1712
1645 // Inform sync of the update. 1713 // Inform sync of the update.
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1750 bool something_changed = false; 1818 bool something_changed = false;
1751 for (TemplateURLVector::iterator i(template_urls_.begin()); 1819 for (TemplateURLVector::iterator i(template_urls_.begin());
1752 i != template_urls_.end(); ++i) { 1820 i != template_urls_.end(); ++i) {
1753 TemplateURL* t_url = *i; 1821 TemplateURL* t_url = *i;
1754 if (t_url->HasGoogleBaseURLs(search_terms_data())) { 1822 if (t_url->HasGoogleBaseURLs(search_terms_data())) {
1755 TemplateURL updated_turl(t_url->data()); 1823 TemplateURL updated_turl(t_url->data());
1756 updated_turl.ResetKeywordIfNecessary(search_terms_data(), false); 1824 updated_turl.ResetKeywordIfNecessary(search_terms_data(), false);
1757 KeywordToTemplateMap::const_iterator existing_entry = 1825 KeywordToTemplateMap::const_iterator existing_entry =
1758 keyword_to_template_map_.find(updated_turl.keyword()); 1826 keyword_to_template_map_.find(updated_turl.keyword());
1759 if ((existing_entry != keyword_to_template_map_.end()) && 1827 if ((existing_entry != keyword_to_template_map_.end()) &&
1760 (existing_entry->second != t_url)) { 1828 (existing_entry->second.first != t_url)) {
1761 // The new autogenerated keyword conflicts with another TemplateURL. 1829 // The new autogenerated keyword conflicts with another TemplateURL.
1762 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its 1830 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its
1763 // current keyword. (This will not prevent |t_url| from auto-updating 1831 // current keyword. (This will not prevent |t_url| from auto-updating
1764 // the keyword in the future if the conflicting TemplateURL disappears.) 1832 // the keyword in the future if the conflicting TemplateURL disappears.)
1765 // Note that we must still update |t_url| in this case, or the 1833 // Note that we must still update |t_url| in this case, or the
1766 // |provider_map_| will not be updated correctly. 1834 // |provider_map_| will not be updated correctly.
1767 if (CanReplace(existing_entry->second)) 1835 if (CanReplace(existing_entry->second.first))
1768 RemoveNoNotify(existing_entry->second); 1836 RemoveNoNotify(existing_entry->second.first);
1769 else 1837 else
1770 updated_turl.data_.SetKeyword(t_url->keyword()); 1838 updated_turl.data_.SetKeyword(t_url->keyword());
1771 } 1839 }
1772 something_changed = true; 1840 something_changed = true;
1773 // This will send the keyword change to sync. Note that other clients 1841 // 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 1842 // need to reset the keyword to an appropriate local value when this
1775 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData(). 1843 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData().
1776 UpdateNoNotify(t_url, updated_turl); 1844 UpdateNoNotify(t_url, updated_turl);
1777 } 1845 }
1778 } 1846 }
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after
2273 if (new_guid.empty()) { 2341 if (new_guid.empty()) {
2274 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); 2342 default_search_manager_.ClearUserSelectedDefaultSearchEngine();
2275 return; 2343 return;
2276 } 2344 }
2277 2345
2278 TemplateURL* turl = GetTemplateURLForGUID(new_guid); 2346 TemplateURL* turl = GetTemplateURLForGUID(new_guid);
2279 if (turl) 2347 if (turl)
2280 default_search_manager_.SetUserSelectedDefaultSearchEngine(turl->data()); 2348 default_search_manager_.SetUserSelectedDefaultSearchEngine(turl->data());
2281 } 2349 }
2282 2350
2351 template <typename Container>
2352 void TemplateURLService::AddMatchingKeywordsHelper(
2353 const Container& keyword_to_template_map,
2354 const base::string16& prefix,
2355 bool supports_replacement_only,
2356 TURLsAndMeaningfulLengths* matches) {
2357 // Sanity check args.
2358 if (prefix.empty())
2359 return;
2360 DCHECK(matches);
2361
2362 // Find matching keyword range. Searches the element map for keywords
2363 // beginning with |prefix| and stores the endpoints of the resulting set in
2364 // |match_range|.
2365 const auto match_range(std::equal_range(
2366 keyword_to_template_map.begin(), keyword_to_template_map.end(),
2367 typename Container::value_type(prefix,
2368 std::make_pair(nullptr, 0)),
2369 LessWithPrefix()));
2370
2371 // Add to vector of matching keywords.
2372 for (typename Container::const_iterator i(match_range.first);
2373 i != match_range.second; ++i) {
2374 if (!supports_replacement_only ||
2375 i->second.first->url_ref().SupportsReplacement(search_terms_data()))
2376 matches->push_back(i->second);
2377 }
2378 }
2379
2283 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL( 2380 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL(
2284 int prepopulated_id) { 2381 int prepopulated_id) {
2285 for (TemplateURLVector::const_iterator i = template_urls_.begin(); 2382 for (TemplateURLVector::const_iterator i = template_urls_.begin();
2286 i != template_urls_.end(); ++i) { 2383 i != template_urls_.end(); ++i) {
2287 if ((*i)->prepopulate_id() == prepopulated_id) 2384 if ((*i)->prepopulate_id() == prepopulated_id)
2288 return *i; 2385 return *i;
2289 } 2386 }
2290 return NULL; 2387 return NULL;
2291 } 2388 }
2292 2389
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2331 2428
2332 if (most_recently_intalled_default) { 2429 if (most_recently_intalled_default) {
2333 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 2430 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
2334 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); 2431 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION);
2335 default_search_manager_.SetExtensionControlledDefaultSearchEngine( 2432 default_search_manager_.SetExtensionControlledDefaultSearchEngine(
2336 most_recently_intalled_default->data()); 2433 most_recently_intalled_default->data());
2337 } else { 2434 } else {
2338 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); 2435 default_search_manager_.ClearExtensionControlledDefaultSearchEngine();
2339 } 2436 }
2340 } 2437 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698