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

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

Issue 287563004: Fix a bug where two loaded keywords clash after a GoogleBaseURL change. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review comments. Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/auto_reset.h" 10 #include "base/auto_reset.h"
(...skipping 1575 matching lines...) Expand 10 before | Expand all | Expand 10 after
1586 } 1586 }
1587 if (best_fallback) 1587 if (best_fallback)
1588 keyword_to_template_map_[keyword] = best_fallback; 1588 keyword_to_template_map_[keyword] = best_fallback;
1589 else 1589 else
1590 keyword_to_template_map_.erase(keyword); 1590 keyword_to_template_map_.erase(keyword);
1591 } 1591 }
1592 1592
1593 if (!template_url->sync_guid().empty()) 1593 if (!template_url->sync_guid().empty())
1594 guid_to_template_map_.erase(template_url->sync_guid()); 1594 guid_to_template_map_.erase(template_url->sync_guid());
1595 if (loaded_) { 1595 if (loaded_) {
1596 // |provider_map_| is only initialized after loading has completed.
Peter Kasting 2014/05/13 19:04:23 Move this comment above the conditional instead of
1596 UIThreadSearchTermsData search_terms_data(template_url->profile()); 1597 UIThreadSearchTermsData search_terms_data(template_url->profile());
1597 provider_map_->Remove(template_url, search_terms_data); 1598 provider_map_->Remove(template_url, search_terms_data);
1598 } 1599 }
1599 } 1600 }
1600 1601
1601 void TemplateURLService::AddToMaps(TemplateURL* template_url) { 1602 void TemplateURLService::AddToMaps(TemplateURL* template_url) {
1602 bool template_url_is_omnibox_api = 1603 bool template_url_is_omnibox_api =
1603 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; 1604 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION;
1604 const base::string16& keyword = template_url->keyword(); 1605 const base::string16& keyword = template_url->keyword();
1605 KeywordToTemplateMap::const_iterator i = 1606 KeywordToTemplateMap::const_iterator i =
(...skipping 10 matching lines...) Expand all
1616 existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; 1617 existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION;
1617 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api); 1618 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api);
1618 if (existing_url_is_omnibox_api ? 1619 if (existing_url_is_omnibox_api ?
1619 !CanReplace(template_url) : CanReplace(existing_url)) 1620 !CanReplace(template_url) : CanReplace(existing_url))
1620 keyword_to_template_map_[keyword] = template_url; 1621 keyword_to_template_map_[keyword] = template_url;
1621 } 1622 }
1622 1623
1623 if (!template_url->sync_guid().empty()) 1624 if (!template_url->sync_guid().empty())
1624 guid_to_template_map_[template_url->sync_guid()] = template_url; 1625 guid_to_template_map_[template_url->sync_guid()] = template_url;
1625 if (loaded_) { 1626 if (loaded_) {
1627 // |provider_map_| is only initialized after loading has completed.
1626 UIThreadSearchTermsData search_terms_data(profile_); 1628 UIThreadSearchTermsData search_terms_data(profile_);
1627 provider_map_->Add(template_url, search_terms_data); 1629 provider_map_->Add(template_url, search_terms_data);
1628 } 1630 }
1629 } 1631 }
1630 1632
1631 // Helper for partition() call in next function. 1633 // Helper for partition() call in next function.
1632 bool HasValidID(TemplateURL* t_url) { 1634 bool HasValidID(TemplateURL* t_url) {
1633 return t_url->id() != kInvalidTemplateURLID; 1635 return t_url->id() != kInvalidTemplateURLID;
1634 } 1636 }
1635 1637
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1711 ((*i)->keyword() == keyword)) 1713 ((*i)->keyword() == keyword))
1712 return *i; 1714 return *i;
1713 } 1715 }
1714 return NULL; 1716 return NULL;
1715 } 1717 }
1716 1718
1717 bool TemplateURLService::UpdateNoNotify( 1719 bool TemplateURLService::UpdateNoNotify(
1718 TemplateURL* existing_turl, 1720 TemplateURL* existing_turl,
1719 const TemplateURL& new_values, 1721 const TemplateURL& new_values,
1720 const SearchTermsData& old_search_terms_data) { 1722 const SearchTermsData& old_search_terms_data) {
1721 DCHECK(loaded_);
1722 DCHECK(existing_turl); 1723 DCHECK(existing_turl);
1723 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == 1724 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) ==
1724 template_urls_.end()) 1725 template_urls_.end())
1725 return false; 1726 return false;
1726 1727
1727 base::string16 old_keyword(existing_turl->keyword()); 1728 base::string16 old_keyword(existing_turl->keyword());
1728 keyword_to_template_map_.erase(old_keyword); 1729 keyword_to_template_map_.erase(old_keyword);
1729 if (!existing_turl->sync_guid().empty()) 1730 if (!existing_turl->sync_guid().empty())
1730 guid_to_template_map_.erase(existing_turl->sync_guid()); 1731 guid_to_template_map_.erase(existing_turl->sync_guid());
1731 1732
1732 provider_map_->Remove(existing_turl, old_search_terms_data); 1733 // |provider_map_| is only initialized after loading has completed.
1734 if (loaded_)
1735 provider_map_->Remove(existing_turl, old_search_terms_data);
1736
1733 TemplateURLID previous_id = existing_turl->id(); 1737 TemplateURLID previous_id = existing_turl->id();
1734 existing_turl->CopyFrom(new_values); 1738 existing_turl->CopyFrom(new_values);
1735 existing_turl->data_.id = previous_id; 1739 existing_turl->data_.id = previous_id;
1736 UIThreadSearchTermsData new_search_terms_data(profile_); 1740
1737 provider_map_->Add(existing_turl, new_search_terms_data); 1741 if (loaded_) {
1742 // |provider_map_| is only initialized after loading has completed.
Peter Kasting 2014/05/13 19:04:23 Remove this comment, the one above is clear enough
1743 UIThreadSearchTermsData new_search_terms_data(profile_);
1744 provider_map_->Add(existing_turl, new_search_terms_data);
1745 }
1738 1746
1739 const base::string16& keyword = existing_turl->keyword(); 1747 const base::string16& keyword = existing_turl->keyword();
1740 KeywordToTemplateMap::const_iterator i = 1748 KeywordToTemplateMap::const_iterator i =
1741 keyword_to_template_map_.find(keyword); 1749 keyword_to_template_map_.find(keyword);
1742 if (i == keyword_to_template_map_.end()) { 1750 if (i == keyword_to_template_map_.end()) {
1743 keyword_to_template_map_[keyword] = existing_turl; 1751 keyword_to_template_map_[keyword] = existing_turl;
1744 } else { 1752 } else {
1745 // We can theoretically reach here in two cases: 1753 // We can theoretically reach here in two cases:
1746 // * There is an existing extension keyword and sync brings in a rename of 1754 // * There is an existing extension keyword and sync brings in a rename of
1747 // a non-extension keyword to match. In this case we just need to pick 1755 // a non-extension keyword to match. In this case we just need to pick
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
2047 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), 2055 DCHECK(std::find(template_urls_.begin(), template_urls_.end(),
2048 template_url) == template_urls_.end()); 2056 template_url) == template_urls_.end());
2049 template_url->data_.id = ++next_id_; 2057 template_url->data_.id = ++next_id_;
2050 } 2058 }
2051 2059
2052 template_url->ResetKeywordIfNecessary(false); 2060 template_url->ResetKeywordIfNecessary(false);
2053 // Check whether |template_url|'s keyword conflicts with any already in the 2061 // Check whether |template_url|'s keyword conflicts with any already in the
2054 // model. 2062 // model.
2055 TemplateURL* existing_keyword_turl = 2063 TemplateURL* existing_keyword_turl =
2056 GetTemplateURLForKeyword(template_url->keyword()); 2064 GetTemplateURLForKeyword(template_url->keyword());
2057 if (existing_keyword_turl != NULL) { 2065
2066 // We might be processing results from WebData now, in which case we are not
2067 // in the loaded state and existing_keyword_turl might be the (not yet added)
2068 // initial_default_search_provider_.
2069 bool existing_keyword_already_stored =
2070 existing_keyword_turl &&
2071 (std::find(template_urls_.begin(),
2072 template_urls_.end(),
2073 existing_keyword_turl) != template_urls_.end());
2074
2075 // We only do the following if there is a conflict already processed into
2076 // template_urls_.
2077 if (existing_keyword_already_stored) {
Peter Kasting 2014/05/13 19:04:23 Nit: How about this, which seems a bit clearer to
2058 DCHECK_NE(existing_keyword_turl, template_url); 2078 DCHECK_NE(existing_keyword_turl, template_url);
2059 // Only replace one of the TemplateURLs if they are either both extensions, 2079 // Only replace one of the TemplateURLs if they are either both extensions,
2060 // or both not extensions. 2080 // or both not extensions.
2061 bool are_same_type = existing_keyword_turl->GetType() == 2081 bool are_same_type = existing_keyword_turl->GetType() ==
2062 template_url->GetType(); 2082 template_url->GetType();
2063 if (CanReplace(existing_keyword_turl) && are_same_type) { 2083 if (CanReplace(existing_keyword_turl) && are_same_type) {
2064 RemoveNoNotify(existing_keyword_turl); 2084 RemoveNoNotify(existing_keyword_turl);
2065 } else if (CanReplace(template_url) && are_same_type) { 2085 } else if (CanReplace(template_url) && are_same_type) {
2066 delete template_url; 2086 delete template_url;
2067 return false; 2087 return false;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2111 2131
2112 // Inform sync of the deletion. 2132 // Inform sync of the deletion.
2113 ProcessTemplateURLChange(FROM_HERE, 2133 ProcessTemplateURLChange(FROM_HERE,
2114 template_url, 2134 template_url,
2115 syncer::SyncChange::ACTION_DELETE); 2135 syncer::SyncChange::ACTION_DELETE);
2116 2136
2117 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, 2137 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
2118 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); 2138 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX);
2119 } 2139 }
2120 2140
2121 if (profile_) { 2141 if (loaded_ && profile_) {
2122 content::Source<Profile> source(profile_); 2142 content::Source<Profile> source(profile_);
2123 TemplateURLID id = template_url->id(); 2143 TemplateURLID id = template_url->id();
2124 content::NotificationService::current()->Notify( 2144 content::NotificationService::current()->Notify(
2125 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, 2145 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED,
2126 source, 2146 source,
2127 content::Details<TemplateURLID>(&id)); 2147 content::Details<TemplateURLID>(&id));
2128 } 2148 }
2129 2149
2130 // We own the TemplateURL and need to delete it. 2150 // We own the TemplateURL and need to delete it.
2131 delete template_url; 2151 delete template_url;
2132 } 2152 }
2133 2153
2134 bool TemplateURLService::ResetTemplateURLNoNotify( 2154 bool TemplateURLService::ResetTemplateURLNoNotify(
2135 TemplateURL* url, 2155 TemplateURL* url,
2136 const base::string16& title, 2156 const base::string16& title,
2137 const base::string16& keyword, 2157 const base::string16& keyword,
2138 const std::string& search_url) { 2158 const std::string& search_url) {
2139 if (!loaded_)
2140 return false;
2141 DCHECK(!keyword.empty()); 2159 DCHECK(!keyword.empty());
2142 DCHECK(!search_url.empty()); 2160 DCHECK(!search_url.empty());
2143 TemplateURLData data(url->data()); 2161 TemplateURLData data(url->data());
2144 data.short_name = title; 2162 data.short_name = title;
2145 data.SetKeyword(keyword); 2163 data.SetKeyword(keyword);
2146 if (search_url != data.url()) { 2164 if (search_url != data.url()) {
2147 data.SetURL(search_url); 2165 data.SetURL(search_url);
2148 // The urls have changed, reset the favicon url. 2166 // The urls have changed, reset the favicon url.
2149 data.favicon_url = GURL(); 2167 data.favicon_url = GURL();
2150 } 2168 }
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
2473 2491
2474 if (most_recently_intalled_default) { 2492 if (most_recently_intalled_default) {
2475 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 2493 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
2476 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); 2494 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION);
2477 default_search_manager_.SetExtensionControlledDefaultSearchEngine( 2495 default_search_manager_.SetExtensionControlledDefaultSearchEngine(
2478 most_recently_intalled_default->data()); 2496 most_recently_intalled_default->data());
2479 } else { 2497 } else {
2480 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); 2498 default_search_manager_.ClearExtensionControlledDefaultSearchEngine();
2481 } 2499 }
2482 } 2500 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698