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

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: Test updates 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 TemplateURL* default_search_provider = NULL; 696 TemplateURL* default_search_provider = NULL;
701 // Force GetSearchProvidersUsingLoadedEngines() to include the prepopulated 697 // Force GetSearchProvidersUsingLoadedEngines() to include the prepopulated
702 // engines in the list by claiming we are currently on version 0, ensuring 698 // engines in the list by claiming we are currently on version 0, ensuring
703 // that the prepopulate data version will be newer. 699 // that the prepopulate data version will be newer.
704 int new_resource_keyword_version = 0; 700 int new_resource_keyword_version = 0;
705 GetSearchProvidersUsingLoadedEngines(service_.get(), profile_, 701 GetSearchProvidersUsingLoadedEngines(service_.get(), profile_,
706 &entries_to_process, 702 &entries_to_process,
707 &default_search_provider, 703 &default_search_provider,
708 &new_resource_keyword_version, 704 &new_resource_keyword_version,
709 &pre_sync_deletes_); 705 &pre_sync_deletes_);
706 for (TemplateURLVector::iterator i = entries_to_process.begin();
707 i != entries_to_process.end();) {
708 if ((*i)->IsExtensionKeyword())
709 i = entries_to_process.erase(i);
Dan Beam 2013/06/05 03:55:53 you may need to also explicitly delete the entries
710 else
711 ++i;
712 }
710 // Setup search engines and a default one. 713 // Setup search engines and a default one.
711 AddTemplateURLsAndSetupDefaultEngine(&entries_to_process, 714 AddTemplateURLsAndSetupDefaultEngine(&entries_to_process,
712 default_search_provider); 715 default_search_provider);
713 716
714 if (new_resource_keyword_version) 717 if (new_resource_keyword_version)
715 service_->SetBuiltinKeywordVersion(new_resource_keyword_version); 718 service_->SetBuiltinKeywordVersion(new_resource_keyword_version);
716 719
717 EnsureDefaultSearchProviderExists(); 720 EnsureDefaultSearchProviderExists();
718 NotifyObservers(); 721 NotifyObservers();
719 } 722 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 UpdateDefaultSearch(); 863 UpdateDefaultSearch();
861 } 864 }
862 865
863 syncer::SyncDataList TemplateURLService::GetAllSyncData( 866 syncer::SyncDataList TemplateURLService::GetAllSyncData(
864 syncer::ModelType type) const { 867 syncer::ModelType type) const {
865 DCHECK_EQ(syncer::SEARCH_ENGINES, type); 868 DCHECK_EQ(syncer::SEARCH_ENGINES, type);
866 869
867 syncer::SyncDataList current_data; 870 syncer::SyncDataList current_data;
868 for (TemplateURLVector::const_iterator iter = template_urls_.begin(); 871 for (TemplateURLVector::const_iterator iter = template_urls_.begin();
869 iter != template_urls_.end(); ++iter) { 872 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. 873 // We don't sync keywords managed by policy.
876 if ((*iter)->created_by_policy()) 874 if ((*iter)->created_by_policy())
877 continue; 875 continue;
878 current_data.push_back(CreateSyncDataFromTemplateURL(**iter)); 876 current_data.push_back(CreateSyncDataFromTemplateURL(**iter));
879 } 877 }
880 878
881 return current_data; 879 return current_data;
882 } 880 }
883 881
884 syncer::SyncError TemplateURLService::ProcessSyncChanges( 882 syncer::SyncError TemplateURLService::ProcessSyncChanges(
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
1175 syncer::SyncChange::SyncChangeType type) { 1173 syncer::SyncChange::SyncChangeType type) {
1176 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID); 1174 DCHECK_NE(type, syncer::SyncChange::ACTION_INVALID);
1177 DCHECK(turl); 1175 DCHECK(turl);
1178 1176
1179 if (!models_associated_) 1177 if (!models_associated_)
1180 return; // Not syncing. 1178 return; // Not syncing.
1181 1179
1182 if (processing_syncer_changes_) 1180 if (processing_syncer_changes_)
1183 return; // These are changes originating from us. Ignore. 1181 return; // These are changes originating from us. Ignore.
1184 1182
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. 1183 // Avoid syncing keywords managed by policy.
1192 if (turl->created_by_policy()) 1184 if (turl->created_by_policy())
1193 return; 1185 return;
1194 1186
1195 syncer::SyncChangeList changes; 1187 syncer::SyncChangeList changes;
1196 1188
1197 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1189 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1198 changes.push_back(syncer::SyncChange(from_here, 1190 changes.push_back(syncer::SyncChange(from_here,
1199 type, 1191 type,
1200 sync_data)); 1192 sync_data));
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1289 data.alternate_urls.clear(); 1281 data.alternate_urls.clear();
1290 for (int i = 0; i < specifics.alternate_urls_size(); ++i) 1282 for (int i = 0; i < specifics.alternate_urls_size(); ++i)
1291 data.alternate_urls.push_back(specifics.alternate_urls(i)); 1283 data.alternate_urls.push_back(specifics.alternate_urls(i));
1292 data.search_terms_replacement_key = specifics.search_terms_replacement_key(); 1284 data.search_terms_replacement_key = specifics.search_terms_replacement_key();
1293 1285
1294 TemplateURL* turl = new TemplateURL(profile, data); 1286 TemplateURL* turl = new TemplateURL(profile, data);
1295 // If this TemplateURL matches a built-in prepopulated template URL, it's 1287 // 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. 1288 // possible that sync is trying to modify fields that should not be touched.
1297 // Revert these fields to the built-in values. 1289 // Revert these fields to the built-in values.
1298 UpdateTemplateURLIfPrepopulated(turl, profile); 1290 UpdateTemplateURLIfPrepopulated(turl, profile);
1299 DCHECK(!turl->IsExtensionKeyword());
1300 if (reset_keyword || deduped) { 1291 if (reset_keyword || deduped) {
1301 if (reset_keyword) 1292 if (reset_keyword)
1302 turl->ResetKeywordIfNecessary(true); 1293 turl->ResetKeywordIfNecessary(true);
1303 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1294 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1304 change_list->push_back(syncer::SyncChange(FROM_HERE, 1295 change_list->push_back(syncer::SyncChange(FROM_HERE,
1305 syncer::SyncChange::ACTION_UPDATE, 1296 syncer::SyncChange::ACTION_UPDATE,
1306 sync_data)); 1297 sync_data));
1307 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) { 1298 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword()) {
1308 if (!existing_turl) { 1299 if (!existing_turl) {
1309 // We're adding a new TemplateURL that uses the Google base URL, so set 1300 // 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() || 1422 (!best_fallback || !best_fallback->IsExtensionKeyword() ||
1432 (turl->IsExtensionKeyword() && (turl->id() > best_fallback->id())))) 1423 (turl->IsExtensionKeyword() && (turl->id() > best_fallback->id()))))
1433 best_fallback = turl; 1424 best_fallback = turl;
1434 } 1425 }
1435 if (best_fallback) 1426 if (best_fallback)
1436 keyword_to_template_map_[keyword] = best_fallback; 1427 keyword_to_template_map_[keyword] = best_fallback;
1437 else 1428 else
1438 keyword_to_template_map_.erase(keyword); 1429 keyword_to_template_map_.erase(keyword);
1439 } 1430 }
1440 1431
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()) 1432 if (!template_url->sync_guid().empty())
1449 guid_to_template_map_.erase(template_url->sync_guid()); 1433 guid_to_template_map_.erase(template_url->sync_guid());
1450 if (loaded_) { 1434 if (loaded_) {
1451 UIThreadSearchTermsData search_terms_data(template_url->profile()); 1435 UIThreadSearchTermsData search_terms_data(template_url->profile());
1452 provider_map_->Remove(template_url, search_terms_data); 1436 provider_map_->Remove(template_url, search_terms_data);
1453 } 1437 }
1454 } 1438 }
1455 1439
1456 void TemplateURLService::RemoveFromKeywordMapByPointer( 1440 void TemplateURLService::RemoveFromKeywordMapByPointer(
1457 TemplateURL* template_url) { 1441 TemplateURL* template_url) {
(...skipping 22 matching lines...) Expand all
1480 // an extension. In that case, the ranking order is: 1464 // an extension. In that case, the ranking order is:
1481 // Manually-modified keywords > extension keywords > replaceable keywords 1465 // Manually-modified keywords > extension keywords > replaceable keywords
1482 // When there are multiple extensions, the last-added wins. 1466 // When there are multiple extensions, the last-added wins.
1483 bool existing_extension = existing_url->IsExtensionKeyword(); 1467 bool existing_extension = existing_url->IsExtensionKeyword();
1484 DCHECK(existing_extension || template_extension); 1468 DCHECK(existing_extension || template_extension);
1485 if (existing_extension ? 1469 if (existing_extension ?
1486 !CanReplace(template_url) : CanReplace(existing_url)) 1470 !CanReplace(template_url) : CanReplace(existing_url))
1487 keyword_to_template_map_[keyword] = template_url; 1471 keyword_to_template_map_[keyword] = template_url;
1488 } 1472 }
1489 1473
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()) 1474 if (!template_url->sync_guid().empty())
1499 guid_to_template_map_[template_url->sync_guid()] = template_url; 1475 guid_to_template_map_[template_url->sync_guid()] = template_url;
1500 if (loaded_) { 1476 if (loaded_) {
1501 UIThreadSearchTermsData search_terms_data(profile_); 1477 UIThreadSearchTermsData search_terms_data(profile_);
1502 provider_map_->Add(template_url, search_terms_data); 1478 provider_map_->Add(template_url, search_terms_data);
1503 } 1479 }
1504 } 1480 }
1505 1481
1506 void TemplateURLService::SetTemplateURLs(const TemplateURLVector& urls) { 1482 void TemplateURLService::SetTemplateURLs(const TemplateURLVector& urls) {
1507 // Add mappings for the new items. 1483 // Add mappings for the new items.
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
1724 bool TemplateURLService::UpdateNoNotify( 1700 bool TemplateURLService::UpdateNoNotify(
1725 TemplateURL* existing_turl, 1701 TemplateURL* existing_turl,
1726 const TemplateURL& new_values, 1702 const TemplateURL& new_values,
1727 const SearchTermsData& old_search_terms_data) { 1703 const SearchTermsData& old_search_terms_data) {
1728 DCHECK(loaded_); 1704 DCHECK(loaded_);
1729 DCHECK(existing_turl); 1705 DCHECK(existing_turl);
1730 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == 1706 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) ==
1731 template_urls_.end()) 1707 template_urls_.end())
1732 return false; 1708 return false;
1733 1709
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()); 1710 string16 old_keyword(existing_turl->keyword());
1740 keyword_to_template_map_.erase(old_keyword); 1711 keyword_to_template_map_.erase(old_keyword);
1741 if (!existing_turl->sync_guid().empty()) 1712 if (!existing_turl->sync_guid().empty())
1742 guid_to_template_map_.erase(existing_turl->sync_guid()); 1713 guid_to_template_map_.erase(existing_turl->sync_guid());
1743 1714
1744 provider_map_->Remove(existing_turl, old_search_terms_data); 1715 provider_map_->Remove(existing_turl, old_search_terms_data);
1745 TemplateURLID previous_id = existing_turl->id(); 1716 TemplateURLID previous_id = existing_turl->id();
1746 existing_turl->CopyFrom(new_values); 1717 existing_turl->CopyFrom(new_values);
1747 existing_turl->data_.id = previous_id; 1718 existing_turl->data_.id = previous_id;
1748 UIThreadSearchTermsData new_search_terms_data(profile_); 1719 UIThreadSearchTermsData new_search_terms_data(profile_);
1749 provider_map_->Add(existing_turl, new_search_terms_data); 1720 provider_map_->Add(existing_turl, new_search_terms_data);
1750 1721
1751 const string16& keyword = existing_turl->keyword(); 1722 const string16& keyword = existing_turl->keyword();
1752 KeywordToTemplateMap::const_iterator i = 1723 KeywordToTemplateMap::const_iterator i =
1753 keyword_to_template_map_.find(keyword); 1724 keyword_to_template_map_.find(keyword);
1754 if (i == keyword_to_template_map_.end()) { 1725 if (i == keyword_to_template_map_.end()) {
1755 keyword_to_template_map_[keyword] = existing_turl; 1726 keyword_to_template_map_[keyword] = existing_turl;
1756 } else { 1727 } else {
1757 // We can theoretically reach here in two cases: 1728 // We can theoretically reach here in two cases:
1758 // * There is an existing extension keyword and sync brings in a rename of 1729 // * 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 1730 // a non-extension keyword to match. In this case we just need to pick
1760 // which keyword has priority to update the keyword map. 1731 // which keyword has priority to update the keyword map.
1761 // * Autogeneration of the keyword for a Google default search provider 1732 // * Autogeneration of the keyword for a Google default search provider
1762 // at load time causes it to conflict with an existing keyword. In this 1733 // 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 1734 // case we delete the existing keyword if it's replaceable, or else undo
1764 // the change in keyword for |existing_turl|. 1735 // the change in keyword for |existing_turl|.
1765 DCHECK(!existing_turl->IsExtensionKeyword());
1766 TemplateURL* existing_keyword_turl = i->second; 1736 TemplateURL* existing_keyword_turl = i->second;
1767 if (existing_keyword_turl->IsExtensionKeyword()) { 1737 if (existing_keyword_turl->IsExtensionKeyword()) {
1768 if (!CanReplace(existing_turl)) 1738 if (!CanReplace(existing_turl))
1769 keyword_to_template_map_[keyword] = existing_turl; 1739 keyword_to_template_map_[keyword] = existing_turl;
1770 } else { 1740 } else {
1771 if (CanReplace(existing_keyword_turl)) { 1741 if (CanReplace(existing_keyword_turl)) {
1772 RemoveNoNotify(existing_keyword_turl); 1742 RemoveNoNotify(existing_keyword_turl);
1773 } else { 1743 } else {
1774 existing_turl->data_.SetKeyword(old_keyword); 1744 existing_turl->data_.SetKeyword(old_keyword);
1775 keyword_to_template_map_[old_keyword] = existing_turl; 1745 keyword_to_template_map_[old_keyword] = existing_turl;
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
2091 DCHECK(template_url); 2061 DCHECK(template_url);
2092 2062
2093 if (newly_adding) { 2063 if (newly_adding) {
2094 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); 2064 DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
2095 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), 2065 DCHECK(std::find(template_urls_.begin(), template_urls_.end(),
2096 template_url) == template_urls_.end()); 2066 template_url) == template_urls_.end());
2097 template_url->data_.id = ++next_id_; 2067 template_url->data_.id = ++next_id_;
2098 } 2068 }
2099 2069
2100 template_url->ResetKeywordIfNecessary(false); 2070 template_url->ResetKeywordIfNecessary(false);
2101 if (!template_url->IsExtensionKeyword()) { 2071 // 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 2072 // model.
2103 // model. 2073 TemplateURL* existing_keyword_turl =
2104 TemplateURL* existing_keyword_turl = 2074 GetTemplateURLForKeyword(template_url->keyword());
2105 FindNonExtensionTemplateURLForKeyword(template_url->keyword()); 2075 if (existing_keyword_turl != NULL) {
2106 if (existing_keyword_turl != NULL) { 2076 DCHECK_NE(existing_keyword_turl, template_url);
2107 DCHECK_NE(existing_keyword_turl, template_url); 2077 // Only replace one of the TemplateURLs if they are either both extensions,
2108 if (CanReplace(existing_keyword_turl)) { 2078 // or both not extensions.
2109 RemoveNoNotify(existing_keyword_turl); 2079 bool are_same_type = existing_keyword_turl->IsExtensionKeyword() ==
2110 } else if (CanReplace(template_url)) { 2080 template_url->IsExtensionKeyword();
2111 delete template_url; 2081 if (CanReplace(existing_keyword_turl) && are_same_type) {
2112 return false; 2082 RemoveNoNotify(existing_keyword_turl);
2113 } else { 2083 } else if (CanReplace(template_url) && are_same_type) {
2114 string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false); 2084 delete template_url;
2115 ResetTemplateURL(existing_keyword_turl, 2085 return false;
2116 existing_keyword_turl->short_name(), new_keyword, 2086 } else {
2117 existing_keyword_turl->url()); 2087 string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false);
2118 } 2088 ResetTemplateURL(existing_keyword_turl,
2089 existing_keyword_turl->short_name(), new_keyword,
2090 existing_keyword_turl->url());
2119 } 2091 }
2120 } 2092 }
2121 template_urls_.push_back(template_url); 2093 template_urls_.push_back(template_url);
2122 AddToMaps(template_url); 2094 AddToMaps(template_url);
2123 2095
2124 if (newly_adding) { 2096 if (newly_adding) {
2125 // Don't persist extension keywords to disk. They'll get re-added on each 2097 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()); 2098 service_->AddKeyword(template_url->data());
2131 2099
2132 // Inform sync of the addition. Note that this will assign a GUID to 2100 // 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_. 2101 // template_url and add it to the guid_to_template_map_.
2134 ProcessTemplateURLChange(FROM_HERE, 2102 ProcessTemplateURLChange(FROM_HERE,
2135 template_url, 2103 template_url,
2136 syncer::SyncChange::ACTION_ADD); 2104 syncer::SyncChange::ACTION_ADD);
2137 } 2105 }
2138 2106
2139 return true; 2107 return true;
2140 } 2108 }
2141 2109
2142 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { 2110 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) {
2143 TemplateURLVector::iterator i = 2111 TemplateURLVector::iterator i =
2144 std::find(template_urls_.begin(), template_urls_.end(), template_url); 2112 std::find(template_urls_.begin(), template_urls_.end(), template_url);
2145 if (i == template_urls_.end()) 2113 if (i == template_urls_.end())
2146 return; 2114 return;
2147 2115
2148 if (template_url == default_search_provider_) { 2116 if (template_url == default_search_provider_) {
2149 // Should never delete the default search provider. 2117 // Should never delete the default search provider.
2150 NOTREACHED(); 2118 NOTREACHED();
2151 return; 2119 return;
2152 } 2120 }
2153 2121
2154 RemoveFromMaps(template_url); 2122 RemoveFromMaps(template_url);
2155 2123
2156 // Remove it from the vector containing all TemplateURLs. 2124 // Remove it from the vector containing all TemplateURLs.
2157 template_urls_.erase(i); 2125 template_urls_.erase(i);
2158 2126
2159 // Extension keywords are not persisted. 2127 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()); 2128 service_->RemoveKeyword(template_url->id());
2164 2129
2165 // Inform sync of the deletion. 2130 // Inform sync of the deletion.
2166 ProcessTemplateURLChange(FROM_HERE, 2131 ProcessTemplateURLChange(FROM_HERE,
2167 template_url, 2132 template_url,
2168 syncer::SyncChange::ACTION_DELETE); 2133 syncer::SyncChange::ACTION_DELETE);
2169 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, 2134 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
2170 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); 2135 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX);
2171 2136
2172 if (profile_) { 2137 if (profile_) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
2221 } 2186 }
2222 2187
2223 // The database loaded a managed |default_search_provider|, but it has 2188 // 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 2189 // been updated in the prefs. Remove it from the database, and update the
2225 // |default_search_provider| pointer here. 2190 // |default_search_provider| pointer here.
2226 if (*default_search_provider && 2191 if (*default_search_provider &&
2227 (*default_search_provider)->id() == template_url->id()) 2192 (*default_search_provider)->id() == template_url->id())
2228 *default_search_provider = NULL; 2193 *default_search_provider = NULL;
2229 2194
2230 i = template_urls->erase(i); 2195 i = template_urls->erase(i);
2231 // Extension keywords are not persisted. 2196 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()); 2197 service_->RemoveKeyword(template_url->id());
2236 delete template_url; 2198 delete template_url;
2237 } else { 2199 } else {
2238 ++i; 2200 ++i;
2239 } 2201 }
2240 } 2202 }
2241 } 2203 }
2242 2204
2243 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, 2205 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url,
2244 const std::string& guid) { 2206 const std::string& guid) {
2245 DCHECK(loaded_); 2207 DCHECK(loaded_);
2246 DCHECK(!guid.empty()); 2208 DCHECK(!guid.empty());
2247 2209
2248 TemplateURLData data(url->data()); 2210 TemplateURLData data(url->data());
2249 data.sync_guid = guid; 2211 data.sync_guid = guid;
2250 TemplateURL new_url(url->profile(), data); 2212 TemplateURL new_url(url->profile(), data);
2251 UIThreadSearchTermsData search_terms_data(url->profile()); 2213 UIThreadSearchTermsData search_terms_data(url->profile());
2252 UpdateNoNotify(url, new_url, search_terms_data); 2214 UpdateNoNotify(url, new_url, search_terms_data);
2253 } 2215 }
2254 2216
2255 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl, 2217 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl,
2256 bool force) { 2218 bool force) {
2257 if (!force) { 2219 if (!force) {
2258 // Already unique. 2220 // Already unique.
2259 if (!GetTemplateURLForKeyword(turl.keyword())) 2221 if (!GetTemplateURLForKeyword(turl.keyword()))
2260 return turl.keyword(); 2222 return turl.keyword();
2261 2223
2262 // First, try to return the generated keyword for the TemplateURL. 2224 // First, try to return the generated keyword for the TemplateURL (except
2225 // for extensions, as their keywords are not associated with their URLs).
2263 GURL gurl(turl.url()); 2226 GURL gurl(turl.url());
2264 if (gurl.is_valid()) { 2227 if (gurl.is_valid() && !turl.IsExtensionKeyword()) {
2265 string16 keyword_candidate = GenerateKeyword(gurl); 2228 string16 keyword_candidate = GenerateKeyword(gurl);
2266 if (!GetTemplateURLForKeyword(keyword_candidate)) 2229 if (!GetTemplateURLForKeyword(keyword_candidate))
2267 return keyword_candidate; 2230 return keyword_candidate;
2268 } 2231 }
2269 } 2232 }
2270 2233
2271 // We try to uniquify the keyword by appending a special character to the end. 2234 // 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 2235 // 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. 2236 // keyword and let the user do what they will after our attempt.
2274 string16 keyword_candidate(turl.keyword()); 2237 string16 keyword_candidate(turl.keyword());
(...skipping 15 matching lines...) Expand all
2290 2253
2291 void TemplateURLService::ResolveSyncKeywordConflict( 2254 void TemplateURLService::ResolveSyncKeywordConflict(
2292 TemplateURL* unapplied_sync_turl, 2255 TemplateURL* unapplied_sync_turl,
2293 TemplateURL* applied_sync_turl, 2256 TemplateURL* applied_sync_turl,
2294 syncer::SyncChangeList* change_list) { 2257 syncer::SyncChangeList* change_list) {
2295 DCHECK(loaded_); 2258 DCHECK(loaded_);
2296 DCHECK(unapplied_sync_turl); 2259 DCHECK(unapplied_sync_turl);
2297 DCHECK(applied_sync_turl); 2260 DCHECK(applied_sync_turl);
2298 DCHECK(change_list); 2261 DCHECK(change_list);
2299 DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword()); 2262 DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword());
2300 DCHECK(!applied_sync_turl->IsExtensionKeyword());
2301 2263
2302 // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so 2264 // 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 2265 // 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 2266 // uniquify the other one, sending an update to the server for the updated
2305 // entry. 2267 // entry.
2306 const bool applied_turl_is_better = 2268 const bool applied_turl_is_better =
2307 IsLocalTemplateURLBetter(applied_sync_turl, unapplied_sync_turl); 2269 IsLocalTemplateURLBetter(applied_sync_turl, unapplied_sync_turl);
2308 TemplateURL* loser = applied_turl_is_better ? 2270 TemplateURL* loser = applied_turl_is_better ?
2309 unapplied_sync_turl : applied_sync_turl; 2271 unapplied_sync_turl : applied_sync_turl;
2310 string16 new_keyword = UniquifyKeyword(*loser, false); 2272 string16 new_keyword = UniquifyKeyword(*loser, false);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
2442 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID)); 2404 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
2443 } 2405 }
2444 2406
2445 void TemplateURLService::PatchMissingSyncGUIDs( 2407 void TemplateURLService::PatchMissingSyncGUIDs(
2446 TemplateURLVector* template_urls) { 2408 TemplateURLVector* template_urls) {
2447 DCHECK(template_urls); 2409 DCHECK(template_urls);
2448 for (TemplateURLVector::iterator i = template_urls->begin(); 2410 for (TemplateURLVector::iterator i = template_urls->begin();
2449 i != template_urls->end(); ++i) { 2411 i != template_urls->end(); ++i) {
2450 TemplateURL* template_url = *i; 2412 TemplateURL* template_url = *i;
2451 DCHECK(template_url); 2413 DCHECK(template_url);
2452 // Extension keywords are never synced. 2414 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(); 2415 template_url->data_.sync_guid = base::GenerateGUID();
2458 if (service_.get()) 2416 if (service_.get())
2459 service_->UpdateKeyword(template_url->data()); 2417 service_->UpdateKeyword(template_url->data());
2460 } 2418 }
2461 } 2419 }
2462 } 2420 }
2463 2421
2464 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine( 2422 void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine(
2465 TemplateURLVector* template_urls, 2423 TemplateURLVector* template_urls,
2466 TemplateURL* default_search_provider) { 2424 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 2517 // 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. 2518 // logged value of 0 indicates a custom default search provider.
2561 if (default_search_provider_) { 2519 if (default_search_provider_) {
2562 UMA_HISTOGRAM_ENUMERATION( 2520 UMA_HISTOGRAM_ENUMERATION(
2563 kDSPHistogramName, 2521 kDSPHistogramName,
2564 default_search_provider_->prepopulate_id(), 2522 default_search_provider_->prepopulate_id(),
2565 TemplateURLPrepopulateData::kMaxPrepopulatedEngineID); 2523 TemplateURLPrepopulateData::kMaxPrepopulatedEngineID);
2566 } 2524 }
2567 } 2525 }
2568 } 2526 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698