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

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: almost all of peter's comments 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).
Peter Kasting 2015/11/12 20:36:11 Nit: google -> "google"
Mark P 2015/11/12 21:42:49 Done.
174 size_t GetEffectiveKeywordLength(const base::string16& keyword,
Peter Kasting 2015/11/12 20:36:11 Nit: Effective -> Meaningful
Mark P 2015/11/12 21:42:49 Done. Sorry I missed this during past revisions.
175 const TemplateURL* turl) {
176 if (OmniboxFieldTrial::KeywordRequiresRegistry())
177 return keyword.length();
178 const size_t registry_length = GetRegistryLength(keyword);
179 // If there's a registry, subtract the length of the registry portion of the
180 // keyword (e.g., co.uk) and an addition one for the dot that preceeds the
181 // registry. Don't return something less than 0 regardless.
Peter Kasting 2015/11/12 20:36:11 Nit: The point of the conditional is sort of obvio
Mark P 2015/11/12 21:42:49 Didn't restructure the beginning of this because I
182 if (registry_length >= keyword.length())
183 return 0;
184 return keyword.length() - (registry_length ? (registry_length + 1) : 0);
185 }
186
152 } // namespace 187 } // namespace
153 188
154 189
155 // TemplateURLService::LessWithPrefix ----------------------------------------- 190 // TemplateURLService::LessWithPrefix -----------------------------------------
156 191
157 class TemplateURLService::LessWithPrefix { 192 class TemplateURLService::LessWithPrefix {
158 public: 193 public:
159 // We want to find the set of keywords that begin with a prefix. The STL 194 // 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 195 // 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 196 // 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; 197 // 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 198 // 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" 199 // 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 200 // the prefix. Note: this is still a strict weak ordering, as required by
166 // equal_range() (though I will not prove that here). 201 // equal_range() (though I will not prove that here).
167 // 202 //
168 // Unfortunately the calling convention is not "prefix and element" but 203 // 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 204 // rather "two elements", so we pass the prefix as a fake "element" which has
170 // a NULL KeywordDataElement pointer. 205 // a NULL KeywordDataElement pointer.
171 bool operator()(const KeywordToTemplateMap::value_type& elem1, 206 bool operator()(const KeywordToTemplateMap::value_type& elem1,
172 const KeywordToTemplateMap::value_type& elem2) const { 207 const KeywordToTemplateMap::value_type& elem2) const {
173 return (elem1.second == NULL) ? 208 return (elem1.second.first == NULL) ?
174 (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) : 209 (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) :
175 (elem1.first < elem2.first); 210 (elem1.first < elem2.first);
176 } 211 }
177 }; 212 };
178 213
179 214
180 // TemplateURLService --------------------------------------------------------- 215 // TemplateURLService ---------------------------------------------------------
181 216
182 TemplateURLService::TemplateURLService( 217 TemplateURLService::TemplateURLService(
183 PrefService* prefs, 218 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 372 // 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 373 // 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 374 // 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 375 // 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., 376 // whether the user has a TemplateURL that has been manually modified (e.g.,
342 // renamed) connected to the same host. 377 // renamed) connected to the same host.
343 return !url.is_valid() || url.host().empty() || 378 return !url.is_valid() || url.host().empty() ||
344 CanAddAutogeneratedKeywordForHost(url.host()); 379 CanAddAutogeneratedKeywordForHost(url.host());
345 } 380 }
346 381
347 void TemplateURLService::FindMatchingKeywords( 382 void TemplateURLService::AddMatchingKeywords(
348 const base::string16& prefix, 383 const base::string16& prefix,
349 bool support_replacement_only, 384 bool supports_replacement_only,
350 TemplateURLVector* matches) { 385 TURLsAndMeaningfulLengths* matches) {
351 // Sanity check args. 386 AddMatchingKeywordsHelper(
352 if (prefix.empty()) 387 keyword_to_template_map_, prefix, supports_replacement_only, matches);
353 return; 388 }
354 DCHECK(matches != NULL);
355 DCHECK(matches->empty()); // The code for exact matches assumes this.
356 389
357 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair 390 void TemplateURLService::AddMatchingDomainKeywords(
358 TemplateURL* const kNullTemplateURL = NULL; 391 const base::string16& prefix,
359 392 bool supports_replacement_only,
360 // Find matching keyword range. Searches the element map for keywords 393 TURLsAndMeaningfulLengths* matches) {
361 // beginning with |prefix| and stores the endpoints of the resulting set in 394 AddMatchingKeywordsHelper(
362 // |match_range|. 395 keyword_domain_to_template_map_, prefix, supports_replacement_only,
363 const std::pair<KeywordToTemplateMap::const_iterator, 396 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 } 397 }
378 398
379 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( 399 TemplateURL* TemplateURLService::GetTemplateURLForKeyword(
380 const base::string16& keyword) { 400 const base::string16& keyword) {
381 KeywordToTemplateMap::const_iterator elem( 401 KeywordToTemplateMap::const_iterator elem(
382 keyword_to_template_map_.find(keyword)); 402 keyword_to_template_map_.find(keyword));
383 if (elem != keyword_to_template_map_.end()) 403 if (elem != keyword_to_template_map_.end())
384 return elem->second; 404 return elem->second.first;
385 return (!loaded_ && 405 return (!loaded_ &&
386 initial_default_search_provider_.get() && 406 initial_default_search_provider_.get() &&
387 (initial_default_search_provider_->keyword() == keyword)) ? 407 (initial_default_search_provider_->keyword() == keyword)) ?
388 initial_default_search_provider_.get() : NULL; 408 initial_default_search_provider_.get() : NULL;
389 } 409 }
390 410
391 TemplateURL* TemplateURLService::GetTemplateURLForGUID( 411 TemplateURL* TemplateURLService::GetTemplateURLForGUID(
392 const std::string& sync_guid) { 412 const std::string& sync_guid) {
393 GUIDToTemplateMap::const_iterator elem(guid_to_template_map_.find(sync_guid)); 413 GUIDToTemplateMap::const_iterator elem(guid_to_template_map_.find(sync_guid));
394 if (elem != guid_to_template_map_.end()) 414 if (elem != guid_to_template_map_.end())
(...skipping 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 } 1427 }
1408 1428
1409 // Request a server check for the correct Google URL if Google is the 1429 // Request a server check for the correct Google URL if Google is the
1410 // default search engine. 1430 // default search engine.
1411 RequestGoogleURLTrackerServerCheckIfNecessary(); 1431 RequestGoogleURLTrackerServerCheckIfNecessary();
1412 } 1432 }
1413 1433
1414 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { 1434 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) {
1415 const base::string16& keyword = template_url->keyword(); 1435 const base::string16& keyword = template_url->keyword();
1416 DCHECK_NE(0U, keyword_to_template_map_.count(keyword)); 1436 DCHECK_NE(0U, keyword_to_template_map_.count(keyword));
1417 if (keyword_to_template_map_[keyword] == template_url) { 1437 if (keyword_to_template_map_[keyword].first == template_url) {
1418 // We need to check whether the keyword can now be provided by another 1438 // We need to check whether the keyword can now be provided by another
1419 // TemplateURL. See the comments in AddToMaps() for more information on 1439 // TemplateURL. See the comments in AddToMaps() for more information on
1420 // extension keywords and how they can coexist with non-extension keywords. 1440 // 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 1441 // 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 1442 // installed (which will be the most recently added, which will have the
1423 // highest ID). 1443 // highest ID).
1424 TemplateURL* best_fallback = NULL; 1444 TemplateURL* best_fallback = NULL;
1425 for (TemplateURLVector::const_iterator i(template_urls_.begin()); 1445 for (TemplateURLVector::const_iterator i(template_urls_.begin());
1426 i != template_urls_.end(); ++i) { 1446 i != template_urls_.end(); ++i) {
1427 TemplateURL* turl = *i; 1447 TemplateURL* turl = *i;
1428 // This next statement relies on the fact that there can only be one 1448 // This next statement relies on the fact that there can only be one
1429 // non-Omnibox API TemplateURL with a given keyword. 1449 // non-Omnibox API TemplateURL with a given keyword.
1430 if ((turl != template_url) && (turl->keyword() == keyword) && 1450 if ((turl != template_url) && (turl->keyword() == keyword) &&
1431 (!best_fallback || 1451 (!best_fallback ||
1432 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) || 1452 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) ||
1433 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && 1453 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
1434 (turl->id() > best_fallback->id())))) 1454 (turl->id() > best_fallback->id()))))
1435 best_fallback = turl; 1455 best_fallback = turl;
1436 } 1456 }
1437 if (best_fallback) 1457 RemoveFromDomainMap(template_url);
1438 keyword_to_template_map_[keyword] = best_fallback; 1458 if (best_fallback) {
1439 else 1459 keyword_to_template_map_[keyword] =
1460 std::make_pair(best_fallback,
1461 GetEffectiveKeywordLength(keyword, best_fallback));
1462 AddToDomainMap(best_fallback);
1463 } else {
1440 keyword_to_template_map_.erase(keyword); 1464 keyword_to_template_map_.erase(keyword);
1465 }
1441 } 1466 }
1442 1467
1443 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) 1468 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)
1444 return; 1469 return;
1445 1470
1446 if (!template_url->sync_guid().empty()) 1471 if (!template_url->sync_guid().empty())
1447 guid_to_template_map_.erase(template_url->sync_guid()); 1472 guid_to_template_map_.erase(template_url->sync_guid());
1448 // |provider_map_| is only initialized after loading has completed. 1473 // |provider_map_| is only initialized after loading has completed.
1449 if (loaded_) { 1474 if (loaded_) {
1450 provider_map_->Remove(template_url); 1475 provider_map_->Remove(template_url);
1451 } 1476 }
1452 } 1477 }
1453 1478
1454 void TemplateURLService::AddToMaps(TemplateURL* template_url) { 1479 void TemplateURLService::AddToMaps(TemplateURL* template_url) {
1455 bool template_url_is_omnibox_api = 1480 bool template_url_is_omnibox_api =
1456 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; 1481 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION;
1457 const base::string16& keyword = template_url->keyword(); 1482 const base::string16& keyword = template_url->keyword();
1458 KeywordToTemplateMap::const_iterator i = 1483 KeywordToTemplateMap::const_iterator i =
1459 keyword_to_template_map_.find(keyword); 1484 keyword_to_template_map_.find(keyword);
1460 if (i == keyword_to_template_map_.end()) { 1485 if (i == keyword_to_template_map_.end()) {
1461 keyword_to_template_map_[keyword] = template_url; 1486 keyword_to_template_map_[keyword] =
1487 std::make_pair(template_url,
1488 GetEffectiveKeywordLength(keyword, template_url));
1489 AddToDomainMap(template_url);
1462 } else { 1490 } else {
1463 const TemplateURL* existing_url = i->second; 1491 const TemplateURL* existing_url = i->second.first;
1464 // We should only have overlapping keywords when at least one comes from 1492 // We should only have overlapping keywords when at least one comes from
1465 // an extension. In that case, the ranking order is: 1493 // an extension. In that case, the ranking order is:
1466 // Manually-modified keywords > extension keywords > replaceable keywords 1494 // Manually-modified keywords > extension keywords > replaceable keywords
1467 // When there are multiple extensions, the last-added wins. 1495 // When there are multiple extensions, the last-added wins.
1468 bool existing_url_is_omnibox_api = 1496 bool existing_url_is_omnibox_api =
1469 existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; 1497 existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION;
1470 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api); 1498 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api);
1471 if (existing_url_is_omnibox_api ? 1499 if (existing_url_is_omnibox_api ?
1472 !CanReplace(template_url) : CanReplace(existing_url)) 1500 !CanReplace(template_url) : CanReplace(existing_url)) {
1473 keyword_to_template_map_[keyword] = template_url; 1501 RemoveFromDomainMap(existing_url);
1502 keyword_to_template_map_[keyword] =
1503 std::make_pair(template_url,
1504 GetEffectiveKeywordLength(keyword, template_url));
1505 AddToDomainMap(template_url);
1506 }
1474 } 1507 }
1475 1508
1476 if (template_url_is_omnibox_api) 1509 if (template_url_is_omnibox_api)
1477 return; 1510 return;
1478 1511
1479 if (!template_url->sync_guid().empty()) 1512 if (!template_url->sync_guid().empty())
1480 guid_to_template_map_[template_url->sync_guid()] = template_url; 1513 guid_to_template_map_[template_url->sync_guid()] = template_url;
1481 // |provider_map_| is only initialized after loading has completed. 1514 // |provider_map_| is only initialized after loading has completed.
1482 if (loaded_) 1515 if (loaded_)
1483 provider_map_->Add(template_url, search_terms_data()); 1516 provider_map_->Add(template_url, search_terms_data());
1484 } 1517 }
1485 1518
1519 void TemplateURLService::RemoveFromDomainMap(const TemplateURL* template_url) {
1520 const base::string16 domain = GetDomainAndRegistry(template_url->keyword());
1521 if (domain.empty())
1522 return;
1523
1524 for (KeywordDomainToTemplateMap::const_iterator it(
1525 keyword_domain_to_template_map_.lower_bound(domain));
1526 it != keyword_domain_to_template_map_.upper_bound(domain); ) {
Peter Kasting 2015/11/12 20:36:11 You should pull the upper_bound() out of the loop
Mark P 2015/11/12 21:42:49 Still TODO.
Mark P 2015/11/12 23:18:41 Good point. Done, but then changed (see below)
1527 if (it->second.first == template_url) {
Peter Kasting 2015/11/12 20:36:11 Nit: {} unnecessary
Mark P 2015/11/12 21:42:49 Done.
1528 it = keyword_domain_to_template_map_.erase(it);
1529 } else {
1530 ++it;
1531 }
1532 }
1533 }
1534
1535 void TemplateURLService::AddToDomainMap(TemplateURL* template_url) {
1536 const base::string16 domain = GetDomainAndRegistry(template_url->keyword());
1537 // Only bother adding an entry to the domain map if its key in the domain
1538 // map would be different from the key in the regular map.
1539 if (domain != template_url->keyword()) {
1540 keyword_domain_to_template_map_.insert(std::make_pair(
1541 domain,
1542 std::make_pair(template_url,
1543 GetEffectiveKeywordLength(domain, template_url))));
1544 }
1545 }
1546
1486 // Helper for partition() call in next function. 1547 // Helper for partition() call in next function.
1487 bool HasValidID(TemplateURL* t_url) { 1548 bool HasValidID(TemplateURL* t_url) {
1488 return t_url->id() != kInvalidTemplateURLID; 1549 return t_url->id() != kInvalidTemplateURLID;
1489 } 1550 }
1490 1551
1491 void TemplateURLService::SetTemplateURLs(TemplateURLVector* urls) { 1552 void TemplateURLService::SetTemplateURLs(TemplateURLVector* urls) {
1492 // Partition the URLs first, instead of implementing the loops below by simply 1553 // 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 1554 // 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 1555 // 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 1556 // 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) { 1648 const TemplateURL& new_values) {
1588 DCHECK(existing_turl); 1649 DCHECK(existing_turl);
1589 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == 1650 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) ==
1590 template_urls_.end()) 1651 template_urls_.end())
1591 return false; 1652 return false;
1592 1653
1593 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType()); 1654 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType());
1594 1655
1595 base::string16 old_keyword(existing_turl->keyword()); 1656 base::string16 old_keyword(existing_turl->keyword());
1596 keyword_to_template_map_.erase(old_keyword); 1657 keyword_to_template_map_.erase(old_keyword);
1658 RemoveFromDomainMap(existing_turl);
1597 if (!existing_turl->sync_guid().empty()) 1659 if (!existing_turl->sync_guid().empty())
1598 guid_to_template_map_.erase(existing_turl->sync_guid()); 1660 guid_to_template_map_.erase(existing_turl->sync_guid());
1599 1661
1600 // |provider_map_| is only initialized after loading has completed. 1662 // |provider_map_| is only initialized after loading has completed.
1601 if (loaded_) 1663 if (loaded_)
1602 provider_map_->Remove(existing_turl); 1664 provider_map_->Remove(existing_turl);
1603 1665
1604 TemplateURLID previous_id = existing_turl->id(); 1666 TemplateURLID previous_id = existing_turl->id();
1605 existing_turl->CopyFrom(new_values); 1667 existing_turl->CopyFrom(new_values);
1606 existing_turl->data_.id = previous_id; 1668 existing_turl->data_.id = previous_id;
1607 1669
1608 if (loaded_) { 1670 if (loaded_) {
1609 provider_map_->Add(existing_turl, search_terms_data()); 1671 provider_map_->Add(existing_turl, search_terms_data());
1610 } 1672 }
1611 1673
1612 const base::string16& keyword = existing_turl->keyword(); 1674 const base::string16& keyword = existing_turl->keyword();
1613 KeywordToTemplateMap::const_iterator i = 1675 KeywordToTemplateMap::const_iterator i =
1614 keyword_to_template_map_.find(keyword); 1676 keyword_to_template_map_.find(keyword);
1615 if (i == keyword_to_template_map_.end()) { 1677 if (i == keyword_to_template_map_.end()) {
1616 keyword_to_template_map_[keyword] = existing_turl; 1678 keyword_to_template_map_[keyword] =
1679 std::make_pair(existing_turl,
1680 GetEffectiveKeywordLength(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 keyword_to_template_map_[keyword] =
1695 std::make_pair(existing_turl,
1696 GetEffectiveKeywordLength(keyword, existing_turl));
1697 AddToDomainMap(existing_turl);
1698 }
1630 } else { 1699 } else {
1631 if (CanReplace(existing_keyword_turl)) { 1700 if (CanReplace(existing_keyword_turl)) {
1632 RemoveNoNotify(existing_keyword_turl); 1701 RemoveNoNotify(existing_keyword_turl);
1633 } else { 1702 } else {
1634 existing_turl->data_.SetKeyword(old_keyword); 1703 existing_turl->data_.SetKeyword(old_keyword);
1635 keyword_to_template_map_[old_keyword] = existing_turl; 1704 keyword_to_template_map_[old_keyword] =
1705 std::make_pair(existing_turl,
1706 GetEffectiveKeywordLength(old_keyword,
1707 existing_turl));
1708 AddToDomainMap(existing_turl);
1636 } 1709 }
1637 } 1710 }
1638 } 1711 }
1639 if (!existing_turl->sync_guid().empty()) 1712 if (!existing_turl->sync_guid().empty())
1640 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; 1713 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl;
1641 1714
1642 if (web_data_service_.get()) 1715 if (web_data_service_.get())
1643 web_data_service_->UpdateKeyword(existing_turl->data()); 1716 web_data_service_->UpdateKeyword(existing_turl->data());
1644 1717
1645 // Inform sync of the update. 1718 // Inform sync of the update.
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1750 bool something_changed = false; 1823 bool something_changed = false;
1751 for (TemplateURLVector::iterator i(template_urls_.begin()); 1824 for (TemplateURLVector::iterator i(template_urls_.begin());
1752 i != template_urls_.end(); ++i) { 1825 i != template_urls_.end(); ++i) {
1753 TemplateURL* t_url = *i; 1826 TemplateURL* t_url = *i;
1754 if (t_url->HasGoogleBaseURLs(search_terms_data())) { 1827 if (t_url->HasGoogleBaseURLs(search_terms_data())) {
1755 TemplateURL updated_turl(t_url->data()); 1828 TemplateURL updated_turl(t_url->data());
1756 updated_turl.ResetKeywordIfNecessary(search_terms_data(), false); 1829 updated_turl.ResetKeywordIfNecessary(search_terms_data(), false);
1757 KeywordToTemplateMap::const_iterator existing_entry = 1830 KeywordToTemplateMap::const_iterator existing_entry =
1758 keyword_to_template_map_.find(updated_turl.keyword()); 1831 keyword_to_template_map_.find(updated_turl.keyword());
1759 if ((existing_entry != keyword_to_template_map_.end()) && 1832 if ((existing_entry != keyword_to_template_map_.end()) &&
1760 (existing_entry->second != t_url)) { 1833 (existing_entry->second.first != t_url)) {
1761 // The new autogenerated keyword conflicts with another TemplateURL. 1834 // The new autogenerated keyword conflicts with another TemplateURL.
1762 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its 1835 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its
1763 // current keyword. (This will not prevent |t_url| from auto-updating 1836 // current keyword. (This will not prevent |t_url| from auto-updating
1764 // the keyword in the future if the conflicting TemplateURL disappears.) 1837 // the keyword in the future if the conflicting TemplateURL disappears.)
1765 // Note that we must still update |t_url| in this case, or the 1838 // Note that we must still update |t_url| in this case, or the
1766 // |provider_map_| will not be updated correctly. 1839 // |provider_map_| will not be updated correctly.
1767 if (CanReplace(existing_entry->second)) 1840 if (CanReplace(existing_entry->second.first))
1768 RemoveNoNotify(existing_entry->second); 1841 RemoveNoNotify(existing_entry->second.first);
1769 else 1842 else
1770 updated_turl.data_.SetKeyword(t_url->keyword()); 1843 updated_turl.data_.SetKeyword(t_url->keyword());
1771 } 1844 }
1772 something_changed = true; 1845 something_changed = true;
1773 // This will send the keyword change to sync. Note that other clients 1846 // 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 1847 // need to reset the keyword to an appropriate local value when this
1775 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData(). 1848 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData().
1776 UpdateNoNotify(t_url, updated_turl); 1849 UpdateNoNotify(t_url, updated_turl);
1777 } 1850 }
1778 } 1851 }
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after
2273 if (new_guid.empty()) { 2346 if (new_guid.empty()) {
2274 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); 2347 default_search_manager_.ClearUserSelectedDefaultSearchEngine();
2275 return; 2348 return;
2276 } 2349 }
2277 2350
2278 TemplateURL* turl = GetTemplateURLForGUID(new_guid); 2351 TemplateURL* turl = GetTemplateURLForGUID(new_guid);
2279 if (turl) 2352 if (turl)
2280 default_search_manager_.SetUserSelectedDefaultSearchEngine(turl->data()); 2353 default_search_manager_.SetUserSelectedDefaultSearchEngine(turl->data());
2281 } 2354 }
2282 2355
2356 template <typename Container>
2357 void TemplateURLService::AddMatchingKeywordsHelper(
2358 const Container& keyword_to_template_map,
2359 const base::string16& prefix,
2360 bool supports_replacement_only,
2361 TURLsAndMeaningfulLengths* matches) {
2362 // Sanity check args.
2363 if (prefix.empty())
2364 return;
2365 DCHECK(matches);
2366
2367 // Find matching keyword range. Searches the element map for keywords
2368 // beginning with |prefix| and stores the endpoints of the resulting set in
2369 // |match_range|.
2370 const std::pair<typename Container::const_iterator,
2371 typename Container::const_iterator> match_range(
Peter Kasting 2015/11/12 20:36:11 Nit: I'd probably use auto here
Mark P 2015/11/12 21:42:49 Done.
2372 std::equal_range(
2373 keyword_to_template_map.begin(), keyword_to_template_map.end(),
2374 typename Container::value_type(prefix,
2375 std::make_pair(nullptr, 0)),
2376 LessWithPrefix()));
2377
2378 // Add to vector of matching keywords.
2379 for (typename Container::const_iterator i(match_range.first);
2380 i != match_range.second; ++i) {
2381 if (!supports_replacement_only ||
2382 i->second.first->url_ref().SupportsReplacement(search_terms_data()))
2383 matches->push_back(i->second);
2384 }
2385 }
2386
2283 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL( 2387 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL(
2284 int prepopulated_id) { 2388 int prepopulated_id) {
2285 for (TemplateURLVector::const_iterator i = template_urls_.begin(); 2389 for (TemplateURLVector::const_iterator i = template_urls_.begin();
2286 i != template_urls_.end(); ++i) { 2390 i != template_urls_.end(); ++i) {
2287 if ((*i)->prepopulate_id() == prepopulated_id) 2391 if ((*i)->prepopulate_id() == prepopulated_id)
2288 return *i; 2392 return *i;
2289 } 2393 }
2290 return NULL; 2394 return NULL;
2291 } 2395 }
2292 2396
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2331 2435
2332 if (most_recently_intalled_default) { 2436 if (most_recently_intalled_default) {
2333 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 2437 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
2334 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); 2438 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION);
2335 default_search_manager_.SetExtensionControlledDefaultSearchEngine( 2439 default_search_manager_.SetExtensionControlledDefaultSearchEngine(
2336 most_recently_intalled_default->data()); 2440 most_recently_intalled_default->data());
2337 } else { 2441 } else {
2338 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); 2442 default_search_manager_.ClearExtensionControlledDefaultSearchEngine();
2339 } 2443 }
2340 } 2444 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698