OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search_engines/template_url_service.h" | 5 #include "chrome/browser/search_engines/template_url_service.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/environment.h" | 10 #include "base/environment.h" |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
362 DCHECK(t_url); | 362 DCHECK(t_url); |
363 UIThreadSearchTermsData search_terms_data(t_url->profile()); | 363 UIThreadSearchTermsData search_terms_data(t_url->profile()); |
364 return GenerateSearchURLUsingTermsData(t_url, search_terms_data); | 364 return GenerateSearchURLUsingTermsData(t_url, search_terms_data); |
365 } | 365 } |
366 | 366 |
367 // static | 367 // static |
368 GURL TemplateURLService::GenerateSearchURLUsingTermsData( | 368 GURL TemplateURLService::GenerateSearchURLUsingTermsData( |
369 const TemplateURL* t_url, | 369 const TemplateURL* t_url, |
370 const SearchTermsData& search_terms_data) { | 370 const SearchTermsData& search_terms_data) { |
371 DCHECK(t_url); | 371 DCHECK(t_url); |
372 DCHECK(!t_url->IsExtensionKeyword()); | |
373 | 372 |
374 const TemplateURLRef& search_ref = t_url->url_ref(); | 373 const TemplateURLRef& search_ref = t_url->url_ref(); |
375 if (!search_ref.IsValidUsingTermsData(search_terms_data)) | 374 if (!search_ref.IsValidUsingTermsData(search_terms_data)) |
376 return GURL(); | 375 return GURL(); |
377 | 376 |
378 if (!search_ref.SupportsReplacementUsingTermsData(search_terms_data)) | 377 if (!search_ref.SupportsReplacementUsingTermsData(search_terms_data)) |
379 return GURL(t_url->url()); | 378 return GURL(t_url->url()); |
380 | 379 |
381 return GURL(search_ref.ReplaceSearchTermsUsingTermsData( | 380 return GURL(search_ref.ReplaceSearchTermsUsingTermsData( |
382 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16(kReplacementTerm)), | 381 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16(kReplacementTerm)), |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() { | 576 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() { |
578 return template_urls_; | 577 return template_urls_; |
579 } | 578 } |
580 | 579 |
581 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { | 580 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { |
582 DCHECK(url); | 581 DCHECK(url); |
583 if (std::find(template_urls_.begin(), template_urls_.end(), url) == | 582 if (std::find(template_urls_.begin(), template_urls_.end(), url) == |
584 template_urls_.end()) | 583 template_urls_.end()) |
585 return; | 584 return; |
586 ++url->data_.usage_count; | 585 ++url->data_.usage_count; |
587 // Extension keywords are not persisted. | 586 if (service_.get()) |
588 // TODO(mpcomplete): If we allow editing extension keywords, then those should | |
589 // be persisted to disk and synced. | |
590 if (service_.get() && !url->IsExtensionKeyword()) | |
591 service_.get()->UpdateKeyword(url->data()); | 587 service_.get()->UpdateKeyword(url->data()); |
592 } | 588 } |
593 | 589 |
594 void TemplateURLService::ResetTemplateURL(TemplateURL* url, | 590 void TemplateURLService::ResetTemplateURL(TemplateURL* url, |
595 const string16& title, | 591 const string16& title, |
596 const string16& keyword, | 592 const string16& keyword, |
597 const std::string& search_url) { | 593 const std::string& search_url) { |
598 if (!loaded_) | 594 if (!loaded_) |
599 return; | 595 return; |
600 DCHECK(!keyword.empty()); | 596 DCHECK(!keyword.empty()); |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
907 UpdateDefaultSearch(); | 903 UpdateDefaultSearch(); |
908 } | 904 } |
909 | 905 |
910 syncer::SyncDataList TemplateURLService::GetAllSyncData( | 906 syncer::SyncDataList TemplateURLService::GetAllSyncData( |
911 syncer::ModelType type) const { | 907 syncer::ModelType type) const { |
912 DCHECK_EQ(syncer::SEARCH_ENGINES, type); | 908 DCHECK_EQ(syncer::SEARCH_ENGINES, type); |
913 | 909 |
914 syncer::SyncDataList current_data; | 910 syncer::SyncDataList current_data; |
915 for (TemplateURLVector::const_iterator iter = template_urls_.begin(); | 911 for (TemplateURLVector::const_iterator iter = template_urls_.begin(); |
916 iter != template_urls_.end(); ++iter) { | 912 iter != template_urls_.end(); ++iter) { |
917 // We don't sync extension keywords. | |
918 // TODO(mpcomplete): If we allow editing extension keywords, then those | |
919 // should be persisted to disk and synced. | |
920 if ((*iter)->IsExtensionKeyword()) | |
921 continue; | |
922 // We don't sync keywords managed by policy. | 913 // We don't sync keywords managed by policy. |
923 if ((*iter)->created_by_policy()) | 914 if ((*iter)->created_by_policy()) |
924 continue; | 915 continue; |
925 current_data.push_back(CreateSyncDataFromTemplateURL(**iter)); | 916 current_data.push_back(CreateSyncDataFromTemplateURL(**iter)); |
926 } | 917 } |
927 | 918 |
928 return current_data; | 919 return current_data; |
929 } | 920 } |
930 | 921 |
931 syncer::SyncError TemplateURLService::ProcessSyncChanges( | 922 syncer::SyncError TemplateURLService::ProcessSyncChanges( |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1222 syncer::SyncChange::SyncChangeType type) { | 1213 syncer::SyncChange::SyncChangeType type) { |
1223 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID); | 1214 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID); |
1224 DCHECK(turl); | 1215 DCHECK(turl); |
1225 | 1216 |
1226 if (!models_associated_) | 1217 if (!models_associated_) |
1227 return; // Not syncing. | 1218 return; // Not syncing. |
1228 | 1219 |
1229 if (processing_syncer_changes_) | 1220 if (processing_syncer_changes_) |
1230 return; // These are changes originating from us. Ignore. | 1221 return; // These are changes originating from us. Ignore. |
1231 | 1222 |
1232 // Avoid syncing Extension keywords. | |
1233 // TODO(mpcomplete): If we allow editing extension keywords, then those should | |
1234 // be persisted to disk and synced. | |
1235 if (turl->IsExtensionKeyword()) | |
1236 return; | |
1237 | |
1238 // Avoid syncing keywords managed by policy. | 1223 // Avoid syncing keywords managed by policy. |
1239 if (turl->created_by_policy()) | 1224 if (turl->created_by_policy()) |
1240 return; | 1225 return; |
1241 | 1226 |
1242 syncer::SyncChangeList changes; | 1227 syncer::SyncChangeList changes; |
1243 | 1228 |
1244 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); | 1229 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); |
1245 changes.push_back(syncer::SyncChange(from_here, | 1230 changes.push_back(syncer::SyncChange(from_here, |
1246 type, | 1231 type, |
1247 sync_data)); | 1232 sync_data)); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1336 data.alternate_urls.clear(); | 1321 data.alternate_urls.clear(); |
1337 for (int i = 0; i < specifics.alternate_urls_size(); ++i) | 1322 for (int i = 0; i < specifics.alternate_urls_size(); ++i) |
1338 data.alternate_urls.push_back(specifics.alternate_urls(i)); | 1323 data.alternate_urls.push_back(specifics.alternate_urls(i)); |
1339 data.search_terms_replacement_key = specifics.search_terms_replacement_key(); | 1324 data.search_terms_replacement_key = specifics.search_terms_replacement_key(); |
1340 | 1325 |
1341 TemplateURL* turl = new TemplateURL(profile, data); | 1326 TemplateURL* turl = new TemplateURL(profile, data); |
1342 // If this TemplateURL matches a built-in prepopulated template URL, it's | 1327 // If this TemplateURL matches a built-in prepopulated template URL, it's |
1343 // possible that sync is trying to modify fields that should not be touched. | 1328 // possible that sync is trying to modify fields that should not be touched. |
1344 // Revert these fields to the built-in values. | 1329 // Revert these fields to the built-in values. |
1345 UpdateTemplateURLIfPrepopulated(turl, profile); | 1330 UpdateTemplateURLIfPrepopulated(turl, profile); |
1346 DCHECK(!turl->IsExtensionKeyword()); | |
1347 if (reset_keyword || deduped) { | 1331 if (reset_keyword || deduped) { |
1348 if (reset_keyword) | 1332 if (reset_keyword) |
1349 turl->ResetKeywordIfNecessary(true); | 1333 turl->ResetKeywordIfNecessary(true); |
1350 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); | 1334 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); |
1351 change_list->push_back(syncer::SyncChange(FROM_HERE, | 1335 change_list->push_back(syncer::SyncChange(FROM_HERE, |
1352 syncer::SyncChange::ACTION_UPDATE, | 1336 syncer::SyncChange::ACTION_UPDATE, |
1353 sync_data)); | 1337 sync_data)); |
1354 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { | 1338 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { |
1355 if (!existing_turl) { | 1339 if (!existing_turl) { |
1356 // We're adding a new TemplateURL that uses the Google base URL, so set | 1340 // We're adding a new TemplateURL that uses the Google base URL, so set |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1478 (!best_fallback || !best_fallback->IsExtensionKeyword() || | 1462 (!best_fallback || !best_fallback->IsExtensionKeyword() || |
1479 (turl->IsExtensionKeyword() && (turl->id() > best_fallback->id())))) | 1463 (turl->IsExtensionKeyword() && (turl->id() > best_fallback->id())))) |
1480 best_fallback = turl; | 1464 best_fallback = turl; |
1481 } | 1465 } |
1482 if (best_fallback) | 1466 if (best_fallback) |
1483 keyword_to_template_map_[keyword] = best_fallback; | 1467 keyword_to_template_map_[keyword] = best_fallback; |
1484 else | 1468 else |
1485 keyword_to_template_map_.erase(keyword); | 1469 keyword_to_template_map_.erase(keyword); |
1486 } | 1470 } |
1487 | 1471 |
1488 // If the keyword we're removing is from an extension, we're now done, since | |
1489 // it won't be synced or stored in the provider map. | |
1490 // TODO(mpcomplete): If we allow editing extension keywords, then those should | |
1491 // be synced. | |
1492 if (template_url->IsExtensionKeyword()) | |
1493 return; | |
1494 | |
1495 if (!template_url->sync_guid().empty()) | 1472 if (!template_url->sync_guid().empty()) |
1496 guid_to_template_map_.erase(template_url->sync_guid()); | 1473 guid_to_template_map_.erase(template_url->sync_guid()); |
1497 if (loaded_) { | 1474 if (loaded_) { |
1498 UIThreadSearchTermsData search_terms_data(template_url->profile()); | 1475 UIThreadSearchTermsData search_terms_data(template_url->profile()); |
1499 provider_map_->Remove(template_url, search_terms_data); | 1476 provider_map_->Remove(template_url, search_terms_data); |
1500 } | 1477 } |
1501 } | 1478 } |
1502 | 1479 |
1503 void TemplateURLService::RemoveFromKeywordMapByPointer( | 1480 void TemplateURLService::RemoveFromKeywordMapByPointer( |
1504 TemplateURL* template_url) { | 1481 TemplateURL* template_url) { |
(...skipping 22 matching lines...) Expand all Loading... | |
1527 // an extension. In that case, the ranking order is: | 1504 // an extension. In that case, the ranking order is: |
1528 // Manually-modified keywords > extension keywords > replaceable keywords | 1505 // Manually-modified keywords > extension keywords > replaceable keywords |
1529 // When there are multiple extensions, the last-added wins. | 1506 // When there are multiple extensions, the last-added wins. |
1530 bool existing_extension = existing_url->IsExtensionKeyword(); | 1507 bool existing_extension = existing_url->IsExtensionKeyword(); |
1531 DCHECK(existing_extension || template_extension); | 1508 DCHECK(existing_extension || template_extension); |
1532 if (existing_extension ? | 1509 if (existing_extension ? |
1533 !CanReplace(template_url) : CanReplace(existing_url)) | 1510 !CanReplace(template_url) : CanReplace(existing_url)) |
1534 keyword_to_template_map_[keyword] = template_url; | 1511 keyword_to_template_map_[keyword] = template_url; |
1535 } | 1512 } |
1536 | 1513 |
1537 // Extension keywords are not synced, so they don't go into the GUID map, | |
1538 // and do not use host-based search URLs, so they don't go into the provider | |
1539 // map, so at this point we're done. | |
1540 // TODO(mpcomplete): If we allow editing extension keywords, then those should | |
1541 // be persisted to disk and synced. | |
1542 if (template_extension) | |
1543 return; | |
1544 | |
1545 if (!template_url->sync_guid().empty()) | 1514 if (!template_url->sync_guid().empty()) |
1546 guid_to_template_map_[template_url->sync_guid()] = template_url; | 1515 guid_to_template_map_[template_url->sync_guid()] = template_url; |
1547 if (loaded_) { | 1516 if (loaded_) { |
1548 UIThreadSearchTermsData search_terms_data(profile_); | 1517 UIThreadSearchTermsData search_terms_data(profile_); |
1549 provider_map_->Add(template_url, search_terms_data); | 1518 provider_map_->Add(template_url, search_terms_data); |
1550 } | 1519 } |
1551 } | 1520 } |
1552 | 1521 |
1553 void TemplateURLService::SetTemplateURLs(const TemplateURLVector& urls) { | 1522 void TemplateURLService::SetTemplateURLs(const TemplateURLVector& urls) { |
1554 // Add mappings for the new items. | 1523 // Add mappings for the new items. |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1761 bool TemplateURLService::UpdateNoNotify( | 1730 bool TemplateURLService::UpdateNoNotify( |
1762 TemplateURL* existing_turl, | 1731 TemplateURL* existing_turl, |
1763 const TemplateURL& new_values, | 1732 const TemplateURL& new_values, |
1764 const SearchTermsData& old_search_terms_data) { | 1733 const SearchTermsData& old_search_terms_data) { |
1765 DCHECK(loaded_); | 1734 DCHECK(loaded_); |
1766 DCHECK(existing_turl); | 1735 DCHECK(existing_turl); |
1767 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == | 1736 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == |
1768 template_urls_.end()) | 1737 template_urls_.end()) |
1769 return false; | 1738 return false; |
1770 | 1739 |
1771 // TODO(mpcomplete): If we allow editing extension keywords, then those should | |
1772 // be persisted to disk and synced. In this case this DCHECK should be | |
1773 // removed. | |
1774 DCHECK(!existing_turl->IsExtensionKeyword()); | |
1775 | |
1776 string16 old_keyword(existing_turl->keyword()); | 1740 string16 old_keyword(existing_turl->keyword()); |
1777 keyword_to_template_map_.erase(old_keyword); | 1741 keyword_to_template_map_.erase(old_keyword); |
1778 if (!existing_turl->sync_guid().empty()) | 1742 if (!existing_turl->sync_guid().empty()) |
1779 guid_to_template_map_.erase(existing_turl->sync_guid()); | 1743 guid_to_template_map_.erase(existing_turl->sync_guid()); |
1780 | 1744 |
1781 provider_map_->Remove(existing_turl, old_search_terms_data); | 1745 provider_map_->Remove(existing_turl, old_search_terms_data); |
1782 TemplateURLID previous_id = existing_turl->id(); | 1746 TemplateURLID previous_id = existing_turl->id(); |
1783 existing_turl->CopyFrom(new_values); | 1747 existing_turl->CopyFrom(new_values); |
1784 existing_turl->data_.id = previous_id; | 1748 existing_turl->data_.id = previous_id; |
1785 UIThreadSearchTermsData new_search_terms_data(profile_); | 1749 UIThreadSearchTermsData new_search_terms_data(profile_); |
1786 provider_map_->Add(existing_turl, new_search_terms_data); | 1750 provider_map_->Add(existing_turl, new_search_terms_data); |
1787 | 1751 |
1788 const string16& keyword = existing_turl->keyword(); | 1752 const string16& keyword = existing_turl->keyword(); |
1789 KeywordToTemplateMap::const_iterator i = | 1753 KeywordToTemplateMap::const_iterator i = |
1790 keyword_to_template_map_.find(keyword); | 1754 keyword_to_template_map_.find(keyword); |
1791 if (i == keyword_to_template_map_.end()) { | 1755 if (i == keyword_to_template_map_.end()) { |
1792 keyword_to_template_map_[keyword] = existing_turl; | 1756 keyword_to_template_map_[keyword] = existing_turl; |
1793 } else { | 1757 } else { |
1794 // We can theoretically reach here in two cases: | 1758 // We can theoretically reach here in two cases: |
1795 // * There is an existing extension keyword and sync brings in a rename of | 1759 // * There is an existing extension keyword and sync brings in a rename of |
1796 // a non-extension keyword to match. In this case we just need to pick | 1760 // a non-extension keyword to match. In this case we just need to pick |
1797 // which keyword has priority to update the keyword map. | 1761 // which keyword has priority to update the keyword map. |
1798 // * Autogeneration of the keyword for a Google default search provider | 1762 // * Autogeneration of the keyword for a Google default search provider |
1799 // at load time causes it to conflict with an existing keyword. In this | 1763 // at load time causes it to conflict with an existing keyword. In this |
1800 // case we delete the existing keyword if it's replaceable, or else undo | 1764 // case we delete the existing keyword if it's replaceable, or else undo |
1801 // the change in keyword for |existing_turl|. | 1765 // the change in keyword for |existing_turl|. |
1802 DCHECK(!existing_turl->IsExtensionKeyword()); | |
1803 TemplateURL* existing_keyword_turl = i->second; | 1766 TemplateURL* existing_keyword_turl = i->second; |
1804 if (existing_keyword_turl->IsExtensionKeyword()) { | 1767 if (existing_keyword_turl->IsExtensionKeyword()) { |
1805 if (!CanReplace(existing_turl)) | 1768 if (!CanReplace(existing_turl)) |
1806 keyword_to_template_map_[keyword] = existing_turl; | 1769 keyword_to_template_map_[keyword] = existing_turl; |
1807 } else { | 1770 } else { |
1808 if (CanReplace(existing_keyword_turl)) { | 1771 if (CanReplace(existing_keyword_turl)) { |
1809 RemoveNoNotify(existing_keyword_turl); | 1772 RemoveNoNotify(existing_keyword_turl); |
1810 } else { | 1773 } else { |
1811 existing_turl->data_.SetKeyword(old_keyword); | 1774 existing_turl->data_.SetKeyword(old_keyword); |
1812 keyword_to_template_map_[old_keyword] = existing_turl; | 1775 keyword_to_template_map_[old_keyword] = existing_turl; |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2128 DCHECK(template_url); | 2091 DCHECK(template_url); |
2129 | 2092 |
2130 if (newly_adding) { | 2093 if (newly_adding) { |
2131 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 2094 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
2132 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), | 2095 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), |
2133 template_url) == template_urls_.end()); | 2096 template_url) == template_urls_.end()); |
2134 template_url->data_.id = ++next_id_; | 2097 template_url->data_.id = ++next_id_; |
2135 } | 2098 } |
2136 | 2099 |
2137 template_url->ResetKeywordIfNecessary(false); | 2100 template_url->ResetKeywordIfNecessary(false); |
2138 if (!template_url->IsExtensionKeyword()) { | 2101 // Check whether |template_url|'s keyword conflicts with any already in the |
2139 // Check whether |template_url|'s keyword conflicts with any already in the | 2102 // model. |
2140 // model. | 2103 TemplateURL* existing_keyword_turl = |
2141 TemplateURL* existing_keyword_turl = | 2104 GetTemplateURLForKeyword(template_url->keyword()); |
2142 FindNonExtensionTemplateURLForKeyword(template_url->keyword()); | 2105 if (existing_keyword_turl != NULL) { |
2143 if (existing_keyword_turl != NULL) { | 2106 DCHECK_NE(existing_keyword_turl, template_url); |
2144 DCHECK_NE(existing_keyword_turl, template_url); | 2107 if (CanReplace(existing_keyword_turl)) { |
2145 if (CanReplace(existing_keyword_turl)) { | 2108 RemoveNoNotify(existing_keyword_turl); |
2146 RemoveNoNotify(existing_keyword_turl); | 2109 } else if (CanReplace(template_url)) { |
2147 } else if (CanReplace(template_url)) { | 2110 delete template_url; |
2148 delete template_url; | 2111 return false; |
2149 return false; | 2112 } else { |
2113 string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false); | |
2114 // If the existing TemplateURL is coming from an extension, give the | |
2115 // the new TemplateURL the newly generated keyword. | |
2116 if (existing_keyword_turl->IsExtensionKeyword()) { | |
2117 TemplateURLData data(template_url->data()); | |
2118 data.short_name = template_url->short_name(); | |
2119 data.SetKeyword(new_keyword); | |
2120 data.safe_for_autoreplace = false; | |
2121 data.last_modified = time_provider_(); | |
2122 TemplateURL new_url(template_url->profile(), data); | |
2123 template_url->CopyFrom(new_url); | |
2150 } else { | 2124 } else { |
2151 string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false); | |
2152 ResetTemplateURL(existing_keyword_turl, | 2125 ResetTemplateURL(existing_keyword_turl, |
2153 existing_keyword_turl->short_name(), new_keyword, | 2126 existing_keyword_turl->short_name(), new_keyword, |
Devlin
2013/05/24 02:42:54
indentation
Aaron Jacobs
2013/05/24 03:06:14
Done.
| |
2154 existing_keyword_turl->url()); | 2127 existing_keyword_turl->url()); |
2155 } | 2128 } |
2156 } | 2129 } |
2157 } | 2130 } |
2158 template_urls_.push_back(template_url); | 2131 template_urls_.push_back(template_url); |
2159 AddToMaps(template_url); | 2132 AddToMaps(template_url); |
2160 | 2133 |
2161 if (newly_adding) { | 2134 if (newly_adding) { |
2162 // Don't persist extension keywords to disk. They'll get re-added on each | 2135 if (service_.get()) |
2163 // launch as the extensions are loaded. | |
2164 // TODO(mpcomplete): If we allow editing extension keywords, then those | |
2165 // should be persisted to disk and synced. | |
2166 if (service_.get() && !template_url->IsExtensionKeyword()) | |
2167 service_->AddKeyword(template_url->data()); | 2136 service_->AddKeyword(template_url->data()); |
2168 | 2137 |
2169 // Inform sync of the addition. Note that this will assign a GUID to | 2138 // Inform sync of the addition. Note that this will assign a GUID to |
2170 // template_url and add it to the guid_to_template_map_. | 2139 // template_url and add it to the guid_to_template_map_. |
2171 ProcessTemplateURLChange(FROM_HERE, | 2140 ProcessTemplateURLChange(FROM_HERE, |
2172 template_url, | 2141 template_url, |
2173 syncer::SyncChange::ACTION_ADD); | 2142 syncer::SyncChange::ACTION_ADD); |
2174 } | 2143 } |
2175 | 2144 |
2176 return true; | 2145 return true; |
2177 } | 2146 } |
2178 | 2147 |
2179 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { | 2148 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { |
2180 TemplateURLVector::iterator i = | 2149 TemplateURLVector::iterator i = |
2181 std::find(template_urls_.begin(), template_urls_.end(), template_url); | 2150 std::find(template_urls_.begin(), template_urls_.end(), template_url); |
2182 if (i == template_urls_.end()) | 2151 if (i == template_urls_.end()) |
2183 return; | 2152 return; |
2184 | 2153 |
2185 if (template_url == default_search_provider_) { | 2154 if (template_url == default_search_provider_) { |
2186 // Should never delete the default search provider. | 2155 // Should never delete the default search provider. |
2187 NOTREACHED(); | 2156 NOTREACHED(); |
2188 return; | 2157 return; |
2189 } | 2158 } |
2190 | 2159 |
2191 RemoveFromMaps(template_url); | 2160 RemoveFromMaps(template_url); |
2192 | 2161 |
2193 // Remove it from the vector containing all TemplateURLs. | 2162 // Remove it from the vector containing all TemplateURLs. |
2194 template_urls_.erase(i); | 2163 template_urls_.erase(i); |
2195 | 2164 |
2196 // Extension keywords are not persisted. | 2165 if (service_.get()) |
2197 // TODO(mpcomplete): If we allow editing extension keywords, then those should | |
2198 // be persisted to disk and synced. | |
2199 if (service_.get() && !template_url->IsExtensionKeyword()) | |
2200 service_->RemoveKeyword(template_url->id()); | 2166 service_->RemoveKeyword(template_url->id()); |
2201 | 2167 |
2202 // Inform sync of the deletion. | 2168 // Inform sync of the deletion. |
2203 ProcessTemplateURLChange(FROM_HERE, | 2169 ProcessTemplateURLChange(FROM_HERE, |
2204 template_url, | 2170 template_url, |
2205 syncer::SyncChange::ACTION_DELETE); | 2171 syncer::SyncChange::ACTION_DELETE); |
2206 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, | 2172 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, |
2207 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); | 2173 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); |
2208 | 2174 |
2209 if (profile_) { | 2175 if (profile_) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2258 } | 2224 } |
2259 | 2225 |
2260 // The database loaded a managed |default_search_provider|, but it has | 2226 // The database loaded a managed |default_search_provider|, but it has |
2261 // been updated in the prefs. Remove it from the database, and update the | 2227 // been updated in the prefs. Remove it from the database, and update the |
2262 // |default_search_provider| pointer here. | 2228 // |default_search_provider| pointer here. |
2263 if (*default_search_provider && | 2229 if (*default_search_provider && |
2264 (*default_search_provider)->id() == template_url->id()) | 2230 (*default_search_provider)->id() == template_url->id()) |
2265 *default_search_provider = NULL; | 2231 *default_search_provider = NULL; |
2266 | 2232 |
2267 i = template_urls->erase(i); | 2233 i = template_urls->erase(i); |
2268 // Extension keywords are not persisted. | 2234 if (service_.get()) |
2269 // TODO(mpcomplete): If we allow editing extension keywords, then those | |
2270 // should be persisted to disk and synced. | |
2271 if (service_.get() && !template_url->IsExtensionKeyword()) | |
2272 service_->RemoveKeyword(template_url->id()); | 2235 service_->RemoveKeyword(template_url->id()); |
2273 delete template_url; | 2236 delete template_url; |
2274 } else { | 2237 } else { |
2275 ++i; | 2238 ++i; |
2276 } | 2239 } |
2277 } | 2240 } |
2278 } | 2241 } |
2279 | 2242 |
2280 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, | 2243 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, |
2281 const std::string& guid) { | 2244 const std::string& guid) { |
2282 DCHECK(loaded_); | 2245 DCHECK(loaded_); |
2283 DCHECK(!guid.empty()); | 2246 DCHECK(!guid.empty()); |
2284 | 2247 |
2285 TemplateURLData data(url->data()); | 2248 TemplateURLData data(url->data()); |
2286 data.sync_guid = guid; | 2249 data.sync_guid = guid; |
2287 TemplateURL new_url(url->profile(), data); | 2250 TemplateURL new_url(url->profile(), data); |
2288 UIThreadSearchTermsData search_terms_data(url->profile()); | 2251 UIThreadSearchTermsData search_terms_data(url->profile()); |
2289 UpdateNoNotify(url, new_url, search_terms_data); | 2252 UpdateNoNotify(url, new_url, search_terms_data); |
2290 } | 2253 } |
2291 | 2254 |
2292 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl, | 2255 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl, |
2293 bool force) { | 2256 bool force) { |
2294 if (!force) { | 2257 if (!force) { |
2295 // Already unique. | 2258 // Already unique. |
2296 if (!GetTemplateURLForKeyword(turl.keyword())) | 2259 if (!GetTemplateURLForKeyword(turl.keyword())) |
2297 return turl.keyword(); | 2260 return turl.keyword(); |
2298 | 2261 |
2299 // First, try to return the generated keyword for the TemplateURL. | 2262 // First, try to return the generated keyword for the TemplateURL (except |
2263 // for extensions, as their keywords are not assocaited with their URLs). | |
Devlin
2013/05/24 02:42:54
*associated
Aaron Jacobs
2013/05/24 03:06:14
Done.
| |
2300 GURL gurl(turl.url()); | 2264 GURL gurl(turl.url()); |
2301 if (gurl.is_valid()) { | 2265 if (gurl.is_valid() && !turl.IsExtensionKeyword()) { |
2302 string16 keyword_candidate = GenerateKeyword(gurl); | 2266 string16 keyword_candidate = GenerateKeyword(gurl); |
2303 if (!GetTemplateURLForKeyword(keyword_candidate)) | 2267 if (!GetTemplateURLForKeyword(keyword_candidate)) |
2304 return keyword_candidate; | 2268 return keyword_candidate; |
2305 } | 2269 } |
2306 } | 2270 } |
2307 | 2271 |
2308 // We try to uniquify the keyword by appending a special character to the end. | 2272 // We try to uniquify the keyword by appending a special character to the end. |
2309 // This is a best-effort approach where we try to preserve the original | 2273 // This is a best-effort approach where we try to preserve the original |
2310 // keyword and let the user do what they will after our attempt. | 2274 // keyword and let the user do what they will after our attempt. |
2311 string16 keyword_candidate(turl.keyword()); | 2275 string16 keyword_candidate(turl.keyword()); |
(...skipping 15 matching lines...) Expand all Loading... | |
2327 | 2291 |
2328 void TemplateURLService::ResolveSyncKeywordConflict( | 2292 void TemplateURLService::ResolveSyncKeywordConflict( |
2329 TemplateURL* unapplied_sync_turl, | 2293 TemplateURL* unapplied_sync_turl, |
2330 TemplateURL* applied_sync_turl, | 2294 TemplateURL* applied_sync_turl, |
2331 syncer::SyncChangeList* change_list) { | 2295 syncer::SyncChangeList* change_list) { |
2332 DCHECK(loaded_); | 2296 DCHECK(loaded_); |
2333 DCHECK(unapplied_sync_turl); | 2297 DCHECK(unapplied_sync_turl); |
2334 DCHECK(applied_sync_turl); | 2298 DCHECK(applied_sync_turl); |
2335 DCHECK(change_list); | 2299 DCHECK(change_list); |
2336 DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword()); | 2300 DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword()); |
2337 DCHECK(!applied_sync_turl->IsExtensionKeyword()); | |
2338 | 2301 |
2339 // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so | 2302 // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so |
2340 // don't delete either of them. Instead, determine which is "better" and | 2303 // don't delete either of them. Instead, determine which is "better" and |
2341 // uniquify the other one, sending an update to the server for the updated | 2304 // uniquify the other one, sending an update to the server for the updated |
2342 // entry. | 2305 // entry. |
2343 const bool applied_turl_is_better = | 2306 const bool applied_turl_is_better = |
2344 IsLocalTemplateURLBetter(applied_sync_turl, unapplied_sync_turl); | 2307 IsLocalTemplateURLBetter(applied_sync_turl, unapplied_sync_turl); |
2345 TemplateURL* loser = applied_turl_is_better ? | 2308 TemplateURL* loser = applied_turl_is_better ? |
2346 unapplied_sync_turl : applied_sync_turl; | 2309 unapplied_sync_turl : applied_sync_turl; |
2347 string16 new_keyword = UniquifyKeyword(*loser, false); | 2310 string16 new_keyword = UniquifyKeyword(*loser, false); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2479 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID)); | 2442 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID)); |
2480 } | 2443 } |
2481 | 2444 |
2482 void TemplateURLService::PatchMissingSyncGUIDs( | 2445 void TemplateURLService::PatchMissingSyncGUIDs( |
2483 TemplateURLVector* template_urls) { | 2446 TemplateURLVector* template_urls) { |
2484 DCHECK(template_urls); | 2447 DCHECK(template_urls); |
2485 for (TemplateURLVector::iterator i = template_urls->begin(); | 2448 for (TemplateURLVector::iterator i = template_urls->begin(); |
2486 i != template_urls->end(); ++i) { | 2449 i != template_urls->end(); ++i) { |
2487 TemplateURL* template_url = *i; | 2450 TemplateURL* template_url = *i; |
2488 DCHECK(template_url); | 2451 DCHECK(template_url); |
2489 // Extension keywords are never synced. | 2452 if (template_url->sync_guid().empty()) { |
2490 // TODO(mpcomplete): If we allow editing extension keywords, then those | |
2491 // should be persisted to disk and synced. | |
2492 if (template_url->sync_guid().empty() && | |
2493 !template_url->IsExtensionKeyword()) { | |
2494 template_url->data_.sync_guid = base::GenerateGUID(); | 2453 template_url->data_.sync_guid = base::GenerateGUID(); |
2495 if (service_.get()) | 2454 if (service_.get()) |
2496 service_->UpdateKeyword(template_url->data()); | 2455 service_->UpdateKeyword(template_url->data()); |
2497 } | 2456 } |
2498 } | 2457 } |
2499 } | 2458 } |
OLD | NEW |