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

Side by Side Diff: chrome/browser/search_engines/template_url_service.cc

Issue 15805002: Modify extension omnibox keywords to be user-configurable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Peter's and Dan's requests Created 7 years, 6 months 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 (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
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
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 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 UpdateDefaultSearch(); 856 UpdateDefaultSearch();
861 } 857 }
862 858
863 syncer::SyncDataList TemplateURLService::GetAllSyncData( 859 syncer::SyncDataList TemplateURLService::GetAllSyncData(
864 syncer::ModelType type) const { 860 syncer::ModelType type) const {
865 DCHECK_EQ(syncer::SEARCH_ENGINES, type); 861 DCHECK_EQ(syncer::SEARCH_ENGINES, type);
866 862
867 syncer::SyncDataList current_data; 863 syncer::SyncDataList current_data;
868 for (TemplateURLVector::const_iterator iter = template_urls_.begin(); 864 for (TemplateURLVector::const_iterator iter = template_urls_.begin();
869 iter != template_urls_.end(); ++iter) { 865 iter != template_urls_.end(); ++iter) {
870 // We don't sync extension keywords.
871 // TODO(mpcomplete): If we allow editing extension keywords, then those
872 // should be persisted to disk and synced.
873 if ((*iter)->IsExtensionKeyword())
874 continue;
875 // We don't sync keywords managed by policy. 866 // We don't sync keywords managed by policy.
876 if ((*iter)->created_by_policy()) 867 if ((*iter)->created_by_policy())
877 continue; 868 continue;
878 current_data.push_back(CreateSyncDataFromTemplateURL(**iter)); 869 current_data.push_back(CreateSyncDataFromTemplateURL(**iter));
879 } 870 }
880 871
881 return current_data; 872 return current_data;
882 } 873 }
883 874
884 syncer::SyncError TemplateURLService::ProcessSyncChanges( 875 syncer::SyncError TemplateURLService::ProcessSyncChanges(
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
1175 syncer::SyncChange::SyncChangeType type) { 1166 syncer::SyncChange::SyncChangeType type) {
1176 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID); 1167 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID);
1177 DCHECK(turl); 1168 DCHECK(turl);
1178 1169
1179 if (!models_associated_) 1170 if (!models_associated_)
1180 return; // Not syncing. 1171 return; // Not syncing.
1181 1172
1182 if (processing_syncer_changes_) 1173 if (processing_syncer_changes_)
1183 return; // These are changes originating from us. Ignore. 1174 return; // These are changes originating from us. Ignore.
1184 1175
1185 // Avoid syncing Extension keywords.
1186 // TODO(mpcomplete): If we allow editing extension keywords, then those should
1187 // be persisted to disk and synced.
1188 if (turl->IsExtensionKeyword())
1189 return;
1190
1191 // Avoid syncing keywords managed by policy. 1176 // Avoid syncing keywords managed by policy.
1192 if (turl->created_by_policy()) 1177 if (turl->created_by_policy())
1193 return; 1178 return;
1194 1179
1195 syncer::SyncChangeList changes; 1180 syncer::SyncChangeList changes;
1196 1181
1197 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1182 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1198 changes.push_back(syncer::SyncChange(from_here, 1183 changes.push_back(syncer::SyncChange(from_here,
1199 type, 1184 type,
1200 sync_data)); 1185 sync_data));
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1289 data.alternate_urls.clear(); 1274 data.alternate_urls.clear();
1290 for (int i = 0; i < specifics.alternate_urls_size(); ++i) 1275 for (int i = 0; i < specifics.alternate_urls_size(); ++i)
1291 data.alternate_urls.push_back(specifics.alternate_urls(i)); 1276 data.alternate_urls.push_back(specifics.alternate_urls(i));
1292 data.search_terms_replacement_key = specifics.search_terms_replacement_key(); 1277 data.search_terms_replacement_key = specifics.search_terms_replacement_key();
1293 1278
1294 TemplateURL* turl = new TemplateURL(profile, data); 1279 TemplateURL* turl = new TemplateURL(profile, data);
1295 // If this TemplateURL matches a built-in prepopulated template URL, it's 1280 // If this TemplateURL matches a built-in prepopulated template URL, it's
1296 // possible that sync is trying to modify fields that should not be touched. 1281 // possible that sync is trying to modify fields that should not be touched.
1297 // Revert these fields to the built-in values. 1282 // Revert these fields to the built-in values.
1298 UpdateTemplateURLIfPrepopulated(turl, profile); 1283 UpdateTemplateURLIfPrepopulated(turl, profile);
1299 DCHECK(!turl->IsExtensionKeyword());
1300 if (reset_keyword || deduped) { 1284 if (reset_keyword || deduped) {
1301 if (reset_keyword) 1285 if (reset_keyword)
1302 turl->ResetKeywordIfNecessary(true); 1286 turl->ResetKeywordIfNecessary(true);
1303 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1287 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1304 change_list->push_back(syncer::SyncChange(FROM_HERE, 1288 change_list->push_back(syncer::SyncChange(FROM_HERE,
1305 syncer::SyncChange::ACTION_UPDATE, 1289 syncer::SyncChange::ACTION_UPDATE,
1306 sync_data)); 1290 sync_data));
1307 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { 1291 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) {
1308 if (!existing_turl) { 1292 if (!existing_turl) {
1309 // We're adding a new TemplateURL that uses the Google base URL, so set 1293 // 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
1431 (!best_fallback || !best_fallback->IsExtensionKeyword() || 1415 (!best_fallback || !best_fallback->IsExtensionKeyword() ||
1432 (turl->IsExtensionKeyword() && (turl->id() > best_fallback->id())))) 1416 (turl->IsExtensionKeyword() && (turl->id() > best_fallback->id()))))
1433 best_fallback = turl; 1417 best_fallback = turl;
1434 } 1418 }
1435 if (best_fallback) 1419 if (best_fallback)
1436 keyword_to_template_map_[keyword] = best_fallback; 1420 keyword_to_template_map_[keyword] = best_fallback;
1437 else 1421 else
1438 keyword_to_template_map_.erase(keyword); 1422 keyword_to_template_map_.erase(keyword);
1439 } 1423 }
1440 1424
1441 // If the keyword we're removing is from an extension, we're now done, since
1442 // it won't be synced or stored in the provider map.
1443 // TODO(mpcomplete): If we allow editing extension keywords, then those should
1444 // be synced.
1445 if (template_url->IsExtensionKeyword())
1446 return;
1447
1448 if (!template_url->sync_guid().empty()) 1425 if (!template_url->sync_guid().empty())
1449 guid_to_template_map_.erase(template_url->sync_guid()); 1426 guid_to_template_map_.erase(template_url->sync_guid());
1450 if (loaded_) { 1427 if (loaded_) {
1451 UIThreadSearchTermsData search_terms_data(template_url->profile()); 1428 UIThreadSearchTermsData search_terms_data(template_url->profile());
1452 provider_map_->Remove(template_url, search_terms_data); 1429 provider_map_->Remove(template_url, search_terms_data);
1453 } 1430 }
1454 } 1431 }
1455 1432
1456 void TemplateURLService::RemoveFromKeywordMapByPointer( 1433 void TemplateURLService::RemoveFromKeywordMapByPointer(
1457 TemplateURL* template_url) { 1434 TemplateURL* template_url) {
(...skipping 22 matching lines...) Expand all
1480 // an extension. In that case, the ranking order is: 1457 // an extension. In that case, the ranking order is:
1481 // Manually-modified keywords > extension keywords > replaceable keywords 1458 // Manually-modified keywords > extension keywords > replaceable keywords
1482 // When there are multiple extensions, the last-added wins. 1459 // When there are multiple extensions, the last-added wins.
1483 bool existing_extension = existing_url->IsExtensionKeyword(); 1460 bool existing_extension = existing_url->IsExtensionKeyword();
1484 DCHECK(existing_extension || template_extension); 1461 DCHECK(existing_extension || template_extension);
1485 if (existing_extension ? 1462 if (existing_extension ?
1486 !CanReplace(template_url) : CanReplace(existing_url)) 1463 !CanReplace(template_url) : CanReplace(existing_url))
1487 keyword_to_template_map_[keyword] = template_url; 1464 keyword_to_template_map_[keyword] = template_url;
1488 } 1465 }
1489 1466
1490 // Extension keywords are not synced, so they don't go into the GUID map,
1491 // and do not use host-based search URLs, so they don't go into the provider
1492 // map, so at this point we're done.
1493 // TODO(mpcomplete): If we allow editing extension keywords, then those should
1494 // be persisted to disk and synced.
1495 if (template_extension)
1496 return;
1497
1498 if (!template_url->sync_guid().empty()) 1467 if (!template_url->sync_guid().empty())
1499 guid_to_template_map_[template_url->sync_guid()] = template_url; 1468 guid_to_template_map_[template_url->sync_guid()] = template_url;
1500 if (loaded_) { 1469 if (loaded_) {
1501 UIThreadSearchTermsData search_terms_data(profile_); 1470 UIThreadSearchTermsData search_terms_data(profile_);
1502 provider_map_->Add(template_url, search_terms_data); 1471 provider_map_->Add(template_url, search_terms_data);
1503 } 1472 }
1504 } 1473 }
1505 1474
1506 void TemplateURLService::SetTemplateURLs(const TemplateURLVector& urls) { 1475 void TemplateURLService::SetTemplateURLs(const TemplateURLVector& urls) {
1507 // Add mappings for the new items. 1476 // Add mappings for the new items.
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
1724 bool TemplateURLService::UpdateNoNotify( 1693 bool TemplateURLService::UpdateNoNotify(
1725 TemplateURL* existing_turl, 1694 TemplateURL* existing_turl,
1726 const TemplateURL& new_values, 1695 const TemplateURL& new_values,
1727 const SearchTermsData& old_search_terms_data) { 1696 const SearchTermsData& old_search_terms_data) {
1728 DCHECK(loaded_); 1697 DCHECK(loaded_);
1729 DCHECK(existing_turl); 1698 DCHECK(existing_turl);
1730 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == 1699 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) ==
1731 template_urls_.end()) 1700 template_urls_.end())
1732 return false; 1701 return false;
1733 1702
1734 // TODO(mpcomplete): If we allow editing extension keywords, then those should
1735 // be persisted to disk and synced. In this case this DCHECK should be
1736 // removed.
1737 DCHECK(!existing_turl->IsExtensionKeyword());
1738
1739 string16 old_keyword(existing_turl->keyword()); 1703 string16 old_keyword(existing_turl->keyword());
1740 keyword_to_template_map_.erase(old_keyword); 1704 keyword_to_template_map_.erase(old_keyword);
1741 if (!existing_turl->sync_guid().empty()) 1705 if (!existing_turl->sync_guid().empty())
1742 guid_to_template_map_.erase(existing_turl->sync_guid()); 1706 guid_to_template_map_.erase(existing_turl->sync_guid());
1743 1707
1744 provider_map_->Remove(existing_turl, old_search_terms_data); 1708 provider_map_->Remove(existing_turl, old_search_terms_data);
1745 TemplateURLID previous_id = existing_turl->id(); 1709 TemplateURLID previous_id = existing_turl->id();
1746 existing_turl->CopyFrom(new_values); 1710 existing_turl->CopyFrom(new_values);
1747 existing_turl->data_.id = previous_id; 1711 existing_turl->data_.id = previous_id;
1748 UIThreadSearchTermsData new_search_terms_data(profile_); 1712 UIThreadSearchTermsData new_search_terms_data(profile_);
1749 provider_map_->Add(existing_turl, new_search_terms_data); 1713 provider_map_->Add(existing_turl, new_search_terms_data);
1750 1714
1751 const string16& keyword = existing_turl->keyword(); 1715 const string16& keyword = existing_turl->keyword();
1752 KeywordToTemplateMap::const_iterator i = 1716 KeywordToTemplateMap::const_iterator i =
1753 keyword_to_template_map_.find(keyword); 1717 keyword_to_template_map_.find(keyword);
1754 if (i == keyword_to_template_map_.end()) { 1718 if (i == keyword_to_template_map_.end()) {
1755 keyword_to_template_map_[keyword] = existing_turl; 1719 keyword_to_template_map_[keyword] = existing_turl;
1756 } else { 1720 } else {
1757 // We can theoretically reach here in two cases: 1721 // We can theoretically reach here in two cases:
1758 // * There is an existing extension keyword and sync brings in a rename of 1722 // * There is an existing extension keyword and sync brings in a rename of
1759 // a non-extension keyword to match. In this case we just need to pick 1723 // a non-extension keyword to match. In this case we just need to pick
1760 // which keyword has priority to update the keyword map. 1724 // which keyword has priority to update the keyword map.
1761 // * Autogeneration of the keyword for a Google default search provider 1725 // * Autogeneration of the keyword for a Google default search provider
1762 // at load time causes it to conflict with an existing keyword. In this 1726 // at load time causes it to conflict with an existing keyword. In this
1763 // case we delete the existing keyword if it's replaceable, or else undo 1727 // case we delete the existing keyword if it's replaceable, or else undo
1764 // the change in keyword for |existing_turl|. 1728 // the change in keyword for |existing_turl|.
1765 DCHECK(!existing_turl->IsExtensionKeyword());
1766 TemplateURL* existing_keyword_turl = i->second; 1729 TemplateURL* existing_keyword_turl = i->second;
1767 if (existing_keyword_turl->IsExtensionKeyword()) { 1730 if (existing_keyword_turl->IsExtensionKeyword()) {
1768 if (!CanReplace(existing_turl)) 1731 if (!CanReplace(existing_turl))
1769 keyword_to_template_map_[keyword] = existing_turl; 1732 keyword_to_template_map_[keyword] = existing_turl;
1770 } else { 1733 } else {
1771 if (CanReplace(existing_keyword_turl)) { 1734 if (CanReplace(existing_keyword_turl)) {
1772 RemoveNoNotify(existing_keyword_turl); 1735 RemoveNoNotify(existing_keyword_turl);
1773 } else { 1736 } else {
1774 existing_turl->data_.SetKeyword(old_keyword); 1737 existing_turl->data_.SetKeyword(old_keyword);
1775 keyword_to_template_map_[old_keyword] = existing_turl; 1738 keyword_to_template_map_[old_keyword] = existing_turl;
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
2091 DCHECK(template_url); 2054 DCHECK(template_url);
2092 2055
2093 if (newly_adding) { 2056 if (newly_adding) {
2094 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); 2057 DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
2095 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), 2058 DCHECK(std::find(template_urls_.begin(), template_urls_.end(),
2096 template_url) == template_urls_.end()); 2059 template_url) == template_urls_.end());
2097 template_url->data_.id = ++next_id_; 2060 template_url->data_.id = ++next_id_;
2098 } 2061 }
2099 2062
2100 template_url->ResetKeywordIfNecessary(false); 2063 template_url->ResetKeywordIfNecessary(false);
2101 if (!template_url->IsExtensionKeyword()) { 2064 // Check whether |template_url|'s keyword conflicts with any already in the
2102 // Check whether |template_url|'s keyword conflicts with any already in the 2065 // model.
2103 // model. 2066 TemplateURL* existing_keyword_turl =
2104 TemplateURL* existing_keyword_turl = 2067 GetTemplateURLForKeyword(template_url->keyword());
2105 FindNonExtensionTemplateURLForKeyword(template_url->keyword()); 2068 if (existing_keyword_turl != NULL) {
2106 if (existing_keyword_turl != NULL) { 2069 DCHECK_NE(existing_keyword_turl, template_url);
2107 DCHECK_NE(existing_keyword_turl, template_url); 2070 if (CanReplace(existing_keyword_turl)) {
2108 if (CanReplace(existing_keyword_turl)) { 2071 RemoveNoNotify(existing_keyword_turl);
2109 RemoveNoNotify(existing_keyword_turl); 2072 } else if (CanReplace(template_url)) {
2110 } else if (CanReplace(template_url)) { 2073 delete template_url;
2111 delete template_url; 2074 return false;
2112 return false; 2075 }
2113 } else { 2076
2114 string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false); 2077 string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false);
2115 ResetTemplateURL(existing_keyword_turl, 2078 // If the existing TemplateURL is coming from an extension, give the
2116 existing_keyword_turl->short_name(), new_keyword, 2079 // the new TemplateURL the newly generated keyword.
2117 existing_keyword_turl->url()); 2080 if (existing_keyword_turl->IsExtensionKeyword()) {
2118 } 2081 TemplateURLData data(template_url->data());
2082 data.short_name = template_url->short_name();
2083 data.SetKeyword(new_keyword);
2084 data.safe_for_autoreplace = false;
2085 data.last_modified = time_provider_();
2086 TemplateURL new_url(template_url->profile(), data);
2087 template_url->CopyFrom(new_url);
2088 } else {
2089 ResetTemplateURL(existing_keyword_turl,
2090 existing_keyword_turl->short_name(), new_keyword,
2091 existing_keyword_turl->url());
2119 } 2092 }
2120 } 2093 }
2121 template_urls_.push_back(template_url); 2094 template_urls_.push_back(template_url);
2122 AddToMaps(template_url); 2095 AddToMaps(template_url);
2123 2096
2124 if (newly_adding) { 2097 if (newly_adding) {
2125 // Don't persist extension keywords to disk. They'll get re-added on each 2098 if (service_.get())
2126 // launch as the extensions are loaded.
2127 // TODO(mpcomplete): If we allow editing extension keywords, then those
2128 // should be persisted to disk and synced.
2129 if (service_.get() && !template_url->IsExtensionKeyword())
2130 service_->AddKeyword(template_url->data()); 2099 service_->AddKeyword(template_url->data());
2131 2100
2132 // Inform sync of the addition. Note that this will assign a GUID to 2101 // Inform sync of the addition. Note that this will assign a GUID to
2133 // template_url and add it to the guid_to_template_map_. 2102 // template_url and add it to the guid_to_template_map_.
2134 ProcessTemplateURLChange(FROM_HERE, 2103 ProcessTemplateURLChange(FROM_HERE,
2135 template_url, 2104 template_url,
2136 syncer::SyncChange::ACTION_ADD); 2105 syncer::SyncChange::ACTION_ADD);
2137 } 2106 }
2138 2107
2139 return true; 2108 return true;
2140 } 2109 }
2141 2110
2142 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { 2111 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) {
2143 TemplateURLVector::iterator i = 2112 TemplateURLVector::iterator i =
2144 std::find(template_urls_.begin(), template_urls_.end(), template_url); 2113 std::find(template_urls_.begin(), template_urls_.end(), template_url);
2145 if (i == template_urls_.end()) 2114 if (i == template_urls_.end())
2146 return; 2115 return;
2147 2116
2148 if (template_url == default_search_provider_) { 2117 if (template_url == default_search_provider_) {
2149 // Should never delete the default search provider. 2118 // Should never delete the default search provider.
2150 NOTREACHED(); 2119 NOTREACHED();
2151 return; 2120 return;
2152 } 2121 }
2153 2122
2154 RemoveFromMaps(template_url); 2123 RemoveFromMaps(template_url);
2155 2124
2156 // Remove it from the vector containing all TemplateURLs. 2125 // Remove it from the vector containing all TemplateURLs.
2157 template_urls_.erase(i); 2126 template_urls_.erase(i);
2158 2127
2159 // Extension keywords are not persisted. 2128 if (service_.get())
2160 // TODO(mpcomplete): If we allow editing extension keywords, then those should
2161 // be persisted to disk and synced.
2162 if (service_.get() && !template_url->IsExtensionKeyword())
2163 service_->RemoveKeyword(template_url->id()); 2129 service_->RemoveKeyword(template_url->id());
2164 2130
2165 // Inform sync of the deletion. 2131 // Inform sync of the deletion.
2166 ProcessTemplateURLChange(FROM_HERE, 2132 ProcessTemplateURLChange(FROM_HERE,
2167 template_url, 2133 template_url,
2168 syncer::SyncChange::ACTION_DELETE); 2134 syncer::SyncChange::ACTION_DELETE);
2169 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, 2135 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
2170 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); 2136 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX);
2171 2137
2172 if (profile_) { 2138 if (profile_) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
2221 } 2187 }
2222 2188
2223 // The database loaded a managed |default_search_provider|, but it has 2189 // The database loaded a managed |default_search_provider|, but it has
2224 // been updated in the prefs. Remove it from the database, and update the 2190 // been updated in the prefs. Remove it from the database, and update the
2225 // |default_search_provider| pointer here. 2191 // |default_search_provider| pointer here.
2226 if (*default_search_provider && 2192 if (*default_search_provider &&
2227 (*default_search_provider)->id() == template_url->id()) 2193 (*default_search_provider)->id() == template_url->id())
2228 *default_search_provider = NULL; 2194 *default_search_provider = NULL;
2229 2195
2230 i = template_urls->erase(i); 2196 i = template_urls->erase(i);
2231 // Extension keywords are not persisted. 2197 if (service_.get())
2232 // TODO(mpcomplete): If we allow editing extension keywords, then those
2233 // should be persisted to disk and synced.
2234 if (service_.get() && !template_url->IsExtensionKeyword())
2235 service_->RemoveKeyword(template_url->id()); 2198 service_->RemoveKeyword(template_url->id());
2236 delete template_url; 2199 delete template_url;
2237 } else { 2200 } else {
2238 ++i; 2201 ++i;
2239 } 2202 }
2240 } 2203 }
2241 } 2204 }
2242 2205
2243 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, 2206 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url,
2244 const std::string& guid) { 2207 const std::string& guid) {
2245 DCHECK(loaded_); 2208 DCHECK(loaded_);
2246 DCHECK(!guid.empty()); 2209 DCHECK(!guid.empty());
2247 2210
2248 TemplateURLData data(url->data()); 2211 TemplateURLData data(url->data());
2249 data.sync_guid = guid; 2212 data.sync_guid = guid;
2250 TemplateURL new_url(url->profile(), data); 2213 TemplateURL new_url(url->profile(), data);
2251 UIThreadSearchTermsData search_terms_data(url->profile()); 2214 UIThreadSearchTermsData search_terms_data(url->profile());
2252 UpdateNoNotify(url, new_url, search_terms_data); 2215 UpdateNoNotify(url, new_url, search_terms_data);
2253 } 2216 }
2254 2217
2255 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl, 2218 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl,
2256 bool force) { 2219 bool force) {
2257 if (!force) { 2220 if (!force) {
2258 // Already unique. 2221 // Already unique.
2259 if (!GetTemplateURLForKeyword(turl.keyword())) 2222 if (!GetTemplateURLForKeyword(turl.keyword()))
2260 return turl.keyword(); 2223 return turl.keyword();
2261 2224
2262 // First, try to return the generated keyword for the TemplateURL. 2225 // First, try to return the generated keyword for the TemplateURL (except
2226 // for extensions, as their keywords are not associated with their URLs).
2263 GURL gurl(turl.url()); 2227 GURL gurl(turl.url());
2264 if (gurl.is_valid()) { 2228 if (gurl.is_valid() && !turl.IsExtensionKeyword()) {
2265 string16 keyword_candidate = GenerateKeyword(gurl); 2229 string16 keyword_candidate = GenerateKeyword(gurl);
2266 if (!GetTemplateURLForKeyword(keyword_candidate)) 2230 if (!GetTemplateURLForKeyword(keyword_candidate))
2267 return keyword_candidate; 2231 return keyword_candidate;
2268 } 2232 }
2269 } 2233 }
2270 2234
2271 // We try to uniquify the keyword by appending a special character to the end. 2235 // We try to uniquify the keyword by appending a special character to the end.
2272 // This is a best-effort approach where we try to preserve the original 2236 // This is a best-effort approach where we try to preserve the original
2273 // keyword and let the user do what they will after our attempt. 2237 // keyword and let the user do what they will after our attempt.
2274 string16 keyword_candidate(turl.keyword()); 2238 string16 keyword_candidate(turl.keyword());
(...skipping 15 matching lines...) Expand all
2290 2254
2291 void TemplateURLService::ResolveSyncKeywordConflict( 2255 void TemplateURLService::ResolveSyncKeywordConflict(
2292 TemplateURL* unapplied_sync_turl, 2256 TemplateURL* unapplied_sync_turl,
2293 TemplateURL* applied_sync_turl, 2257 TemplateURL* applied_sync_turl,
2294 syncer::SyncChangeList* change_list) { 2258 syncer::SyncChangeList* change_list) {
2295 DCHECK(loaded_); 2259 DCHECK(loaded_);
2296 DCHECK(unapplied_sync_turl); 2260 DCHECK(unapplied_sync_turl);
2297 DCHECK(applied_sync_turl); 2261 DCHECK(applied_sync_turl);
2298 DCHECK(change_list); 2262 DCHECK(change_list);
2299 DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword()); 2263 DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword());
2300 DCHECK(!applied_sync_turl->IsExtensionKeyword());
2301 2264
2302 // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so 2265 // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so
2303 // don't delete either of them. Instead, determine which is "better" and 2266 // don't delete either of them. Instead, determine which is "better" and
2304 // uniquify the other one, sending an update to the server for the updated 2267 // uniquify the other one, sending an update to the server for the updated
2305 // entry. 2268 // entry.
2306 const bool applied_turl_is_better = 2269 const bool applied_turl_is_better =
2307 IsLocalTemplateURLBetter(applied_sync_turl, unapplied_sync_turl); 2270 IsLocalTemplateURLBetter(applied_sync_turl, unapplied_sync_turl);
2308 TemplateURL* loser = applied_turl_is_better ? 2271 TemplateURL* loser = applied_turl_is_better ?
2309 unapplied_sync_turl : applied_sync_turl; 2272 unapplied_sync_turl : applied_sync_turl;
2310 string16 new_keyword = UniquifyKeyword(*loser, false); 2273 string16 new_keyword = UniquifyKeyword(*loser, false);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
2442 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID)); 2405 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
2443 } 2406 }
2444 2407
2445 void TemplateURLService::PatchMissingSyncGUIDs( 2408 void TemplateURLService::PatchMissingSyncGUIDs(
2446 TemplateURLVector* template_urls) { 2409 TemplateURLVector* template_urls) {
2447 DCHECK(template_urls); 2410 DCHECK(template_urls);
2448 for (TemplateURLVector::iterator i = template_urls->begin(); 2411 for (TemplateURLVector::iterator i = template_urls->begin();
2449 i != template_urls->end(); ++i) { 2412 i != template_urls->end(); ++i) {
2450 TemplateURL* template_url = *i; 2413 TemplateURL* template_url = *i;
2451 DCHECK(template_url); 2414 DCHECK(template_url);
2452 // Extension keywords are never synced. 2415 if (template_url->sync_guid().empty()) {
2453 // TODO(mpcomplete): If we allow editing extension keywords, then those
2454 // should be persisted to disk and synced.
2455 if (template_url->sync_guid().empty() &&
2456 !template_url->IsExtensionKeyword()) {
2457 template_url->data_.sync_guid = base::GenerateGUID(); 2416 template_url->data_.sync_guid = base::GenerateGUID();
2458 if (service_.get()) 2417 if (service_.get())
2459 service_->UpdateKeyword(template_url->data()); 2418 service_->UpdateKeyword(template_url->data());
2460 } 2419 }
2461 } 2420 }
2462 } 2421 }
2463 2422
2464 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine( 2423 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine(
2465 TemplateURLVector* template_urls, 2424 TemplateURLVector* template_urls,
2466 TemplateURL* default_search_provider) { 2425 TemplateURL* default_search_provider) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
2559 // Don't log anything if the user has a NULL default search provider. A 2518 // Don't log anything if the user has a NULL default search provider. A
2560 // logged value of 0 indicates a custom default search provider. 2519 // logged value of 0 indicates a custom default search provider.
2561 if (default_search_provider_) { 2520 if (default_search_provider_) {
2562 UMA_HISTOGRAM_ENUMERATION( 2521 UMA_HISTOGRAM_ENUMERATION(
2563 kDSPHistogramName, 2522 kDSPHistogramName,
2564 default_search_provider_->prepopulate_id(), 2523 default_search_provider_->prepopulate_id(),
2565 TemplateURLPrepopulateData::kMaxPrepopulatedEngineID); 2524 TemplateURLPrepopulateData::kMaxPrepopulatedEngineID);
2566 } 2525 }
2567 } 2526 }
2568 } 2527 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698