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

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 1574 matching lines...) Expand 10 before | Expand all | Expand 10 after
1585 best_fallback = turl; 1585 best_fallback = turl;
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 // |provider_map_| is only initialized after loading has completed.
1595 if (loaded_) { 1596 if (loaded_) {
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();
(...skipping 10 matching lines...) Expand all
1615 bool existing_url_is_omnibox_api = 1616 bool existing_url_is_omnibox_api =
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;
1626 // |provider_map_| is only initialized after loading has completed.
1625 if (loaded_) { 1627 if (loaded_) {
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 }
(...skipping 76 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 UIThreadSearchTermsData new_search_terms_data(profile_);
1743 provider_map_->Add(existing_turl, new_search_terms_data);
1744 }
1738 1745
1739 const base::string16& keyword = existing_turl->keyword(); 1746 const base::string16& keyword = existing_turl->keyword();
1740 KeywordToTemplateMap::const_iterator i = 1747 KeywordToTemplateMap::const_iterator i =
1741 keyword_to_template_map_.find(keyword); 1748 keyword_to_template_map_.find(keyword);
1742 if (i == keyword_to_template_map_.end()) { 1749 if (i == keyword_to_template_map_.end()) {
1743 keyword_to_template_map_[keyword] = existing_turl; 1750 keyword_to_template_map_[keyword] = existing_turl;
1744 } else { 1751 } else {
1745 // We can theoretically reach here in two cases: 1752 // We can theoretically reach here in two cases:
1746 // * There is an existing extension keyword and sync brings in a rename of 1753 // * 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 1754 // 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(), 2054 DCHECK(std::find(template_urls_.begin(), template_urls_.end(),
2048 template_url) == template_urls_.end()); 2055 template_url) == template_urls_.end());
2049 template_url->data_.id = ++next_id_; 2056 template_url->data_.id = ++next_id_;
2050 } 2057 }
2051 2058
2052 template_url->ResetKeywordIfNecessary(false); 2059 template_url->ResetKeywordIfNecessary(false);
2053 // Check whether |template_url|'s keyword conflicts with any already in the 2060 // Check whether |template_url|'s keyword conflicts with any already in the
2054 // model. 2061 // model.
2055 TemplateURL* existing_keyword_turl = 2062 TemplateURL* existing_keyword_turl =
2056 GetTemplateURLForKeyword(template_url->keyword()); 2063 GetTemplateURLForKeyword(template_url->keyword());
2057 if (existing_keyword_turl != NULL) { 2064
2065 // Check whether |template_url|'s keyword conflicts with any already in the
2066 // model. Note that we can reach here during the loading phase while
2067 // processing the template URLs from the web data service. In this case,
2068 // GetTemplateURLForKeyword() will look not only at what's already in the
2069 // model, but at the |initial_default_search_provider_|. Since this engine
2070 // will presumably also be present in the web data, we need to double-check
2071 // that any "pre-existing" entries we find are actually coming from
2072 // |template_urls_|, lest we detect a "conflict" between the
2073 // |initial_default_search_provider_| and the web data version of itself.
2074 if (existing_keyword_turl &&
2075 (std::find(template_urls_.begin(), template_urls_.end(),
2076 existing_keyword_turl) != template_urls_.end())) {
2058 DCHECK_NE(existing_keyword_turl, template_url); 2077 DCHECK_NE(existing_keyword_turl, template_url);
2059 // Only replace one of the TemplateURLs if they are either both extensions, 2078 // Only replace one of the TemplateURLs if they are either both extensions,
2060 // or both not extensions. 2079 // or both not extensions.
2061 bool are_same_type = existing_keyword_turl->GetType() == 2080 bool are_same_type = existing_keyword_turl->GetType() ==
2062 template_url->GetType(); 2081 template_url->GetType();
2063 if (CanReplace(existing_keyword_turl) && are_same_type) { 2082 if (CanReplace(existing_keyword_turl) && are_same_type) {
2064 RemoveNoNotify(existing_keyword_turl); 2083 RemoveNoNotify(existing_keyword_turl);
2065 } else if (CanReplace(template_url) && are_same_type) { 2084 } else if (CanReplace(template_url) && are_same_type) {
2066 delete template_url; 2085 delete template_url;
2067 return false; 2086 return false;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2111 2130
2112 // Inform sync of the deletion. 2131 // Inform sync of the deletion.
2113 ProcessTemplateURLChange(FROM_HERE, 2132 ProcessTemplateURLChange(FROM_HERE,
2114 template_url, 2133 template_url,
2115 syncer::SyncChange::ACTION_DELETE); 2134 syncer::SyncChange::ACTION_DELETE);
2116 2135
2117 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, 2136 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
2118 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); 2137 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX);
2119 } 2138 }
2120 2139
2121 if (profile_) { 2140 if (loaded_ && profile_) {
2122 content::Source<Profile> source(profile_); 2141 content::Source<Profile> source(profile_);
2123 TemplateURLID id = template_url->id(); 2142 TemplateURLID id = template_url->id();
2124 content::NotificationService::current()->Notify( 2143 content::NotificationService::current()->Notify(
2125 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, 2144 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED,
2126 source, 2145 source,
2127 content::Details<TemplateURLID>(&id)); 2146 content::Details<TemplateURLID>(&id));
2128 } 2147 }
2129 2148
2130 // We own the TemplateURL and need to delete it. 2149 // We own the TemplateURL and need to delete it.
2131 delete template_url; 2150 delete template_url;
2132 } 2151 }
2133 2152
2134 bool TemplateURLService::ResetTemplateURLNoNotify( 2153 bool TemplateURLService::ResetTemplateURLNoNotify(
2135 TemplateURL* url, 2154 TemplateURL* url,
2136 const base::string16& title, 2155 const base::string16& title,
2137 const base::string16& keyword, 2156 const base::string16& keyword,
2138 const std::string& search_url) { 2157 const std::string& search_url) {
2139 if (!loaded_)
2140 return false;
2141 DCHECK(!keyword.empty()); 2158 DCHECK(!keyword.empty());
2142 DCHECK(!search_url.empty()); 2159 DCHECK(!search_url.empty());
2143 TemplateURLData data(url->data()); 2160 TemplateURLData data(url->data());
2144 data.short_name = title; 2161 data.short_name = title;
2145 data.SetKeyword(keyword); 2162 data.SetKeyword(keyword);
2146 if (search_url != data.url()) { 2163 if (search_url != data.url()) {
2147 data.SetURL(search_url); 2164 data.SetURL(search_url);
2148 // The urls have changed, reset the favicon url. 2165 // The urls have changed, reset the favicon url.
2149 data.favicon_url = GURL(); 2166 data.favicon_url = GURL();
2150 } 2167 }
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
2473 2490
2474 if (most_recently_intalled_default) { 2491 if (most_recently_intalled_default) {
2475 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 2492 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
2476 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); 2493 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION);
2477 default_search_manager_.SetExtensionControlledDefaultSearchEngine( 2494 default_search_manager_.SetExtensionControlledDefaultSearchEngine(
2478 most_recently_intalled_default->data()); 2495 most_recently_intalled_default->data());
2479 } else { 2496 } else {
2480 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); 2497 default_search_manager_.ClearExtensionControlledDefaultSearchEngine();
2481 } 2498 }
2482 } 2499 }
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