OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/template_url_service.h" | 5 #include "components/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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 dsp_change_origin_(DSP_CHANGE_OTHER), | 229 dsp_change_origin_(DSP_CHANGE_OTHER), |
230 default_search_manager_( | 230 default_search_manager_( |
231 prefs_, | 231 prefs_, |
232 base::Bind(&TemplateURLService::OnDefaultSearchChange, | 232 base::Bind(&TemplateURLService::OnDefaultSearchChange, |
233 base::Unretained(this))) { | 233 base::Unretained(this))) { |
234 Init(initializers, count); | 234 Init(initializers, count); |
235 } | 235 } |
236 | 236 |
237 TemplateURLService::~TemplateURLService() { | 237 TemplateURLService::~TemplateURLService() { |
238 // |web_data_service_| should be deleted during Shutdown(). | 238 // |web_data_service_| should be deleted during Shutdown(). |
239 DCHECK(!web_data_service_); | 239 DCHECK(!web_data_service_.get()); |
240 STLDeleteElements(&template_urls_); | 240 STLDeleteElements(&template_urls_); |
241 } | 241 } |
242 | 242 |
243 // static | 243 // static |
244 bool TemplateURLService::LoadDefaultSearchProviderFromPrefs( | 244 bool TemplateURLService::LoadDefaultSearchProviderFromPrefs( |
245 PrefService* prefs, | 245 PrefService* prefs, |
246 scoped_ptr<TemplateURLData>* default_provider_data, | 246 scoped_ptr<TemplateURLData>* default_provider_data, |
247 bool* is_managed) { | 247 bool* is_managed) { |
248 if (!prefs || !prefs->HasPrefPath(prefs::kDefaultSearchProviderSearchURL) || | 248 if (!prefs || !prefs->HasPrefPath(prefs::kDefaultSearchProviderSearchURL) || |
249 !prefs->HasPrefPath(prefs::kDefaultSearchProviderKeyword)) | 249 !prefs->HasPrefPath(prefs::kDefaultSearchProviderKeyword)) |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { | 663 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { |
664 DCHECK(url); | 664 DCHECK(url); |
665 // Extension-controlled search engines are not persisted. | 665 // Extension-controlled search engines are not persisted. |
666 if (url->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) | 666 if (url->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) |
667 return; | 667 return; |
668 if (std::find(template_urls_.begin(), template_urls_.end(), url) == | 668 if (std::find(template_urls_.begin(), template_urls_.end(), url) == |
669 template_urls_.end()) | 669 template_urls_.end()) |
670 return; | 670 return; |
671 ++url->data_.usage_count; | 671 ++url->data_.usage_count; |
672 | 672 |
673 if (web_data_service_) | 673 if (web_data_service_.get()) |
674 web_data_service_->UpdateKeyword(url->data()); | 674 web_data_service_->UpdateKeyword(url->data()); |
675 } | 675 } |
676 | 676 |
677 void TemplateURLService::ResetTemplateURL(TemplateURL* url, | 677 void TemplateURLService::ResetTemplateURL(TemplateURL* url, |
678 const base::string16& title, | 678 const base::string16& title, |
679 const base::string16& keyword, | 679 const base::string16& keyword, |
680 const std::string& search_url) { | 680 const std::string& search_url) { |
681 if (ResetTemplateURLNoNotify(url, title, keyword, search_url)) | 681 if (ResetTemplateURLNoNotify(url, title, keyword, search_url)) |
682 NotifyObservers(); | 682 NotifyObservers(); |
683 } | 683 } |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 } | 801 } |
802 | 802 |
803 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { | 803 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { |
804 model_observers_.RemoveObserver(observer); | 804 model_observers_.RemoveObserver(observer); |
805 } | 805 } |
806 | 806 |
807 void TemplateURLService::Load() { | 807 void TemplateURLService::Load() { |
808 if (loaded_ || load_handle_) | 808 if (loaded_ || load_handle_) |
809 return; | 809 return; |
810 | 810 |
811 if (web_data_service_) | 811 if (web_data_service_.get()) |
812 load_handle_ = web_data_service_->GetKeywords(this); | 812 load_handle_ = web_data_service_->GetKeywords(this); |
813 else | 813 else |
814 ChangeToLoadedState(); | 814 ChangeToLoadedState(); |
815 } | 815 } |
816 | 816 |
817 scoped_ptr<TemplateURLService::Subscription> | 817 scoped_ptr<TemplateURLService::Subscription> |
818 TemplateURLService::RegisterOnLoadedCallback( | 818 TemplateURLService::RegisterOnLoadedCallback( |
819 const base::Closure& callback) { | 819 const base::Closure& callback) { |
820 return loaded_ ? | 820 return loaded_ ? |
821 scoped_ptr<TemplateURLService::Subscription>() : | 821 scoped_ptr<TemplateURLService::Subscription>() : |
(...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1673 RemoveNoNotify(existing_keyword_turl); | 1673 RemoveNoNotify(existing_keyword_turl); |
1674 } else { | 1674 } else { |
1675 existing_turl->data_.SetKeyword(old_keyword); | 1675 existing_turl->data_.SetKeyword(old_keyword); |
1676 keyword_to_template_map_[old_keyword] = existing_turl; | 1676 keyword_to_template_map_[old_keyword] = existing_turl; |
1677 } | 1677 } |
1678 } | 1678 } |
1679 } | 1679 } |
1680 if (!existing_turl->sync_guid().empty()) | 1680 if (!existing_turl->sync_guid().empty()) |
1681 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; | 1681 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; |
1682 | 1682 |
1683 if (web_data_service_) | 1683 if (web_data_service_.get()) |
1684 web_data_service_->UpdateKeyword(existing_turl->data()); | 1684 web_data_service_->UpdateKeyword(existing_turl->data()); |
1685 | 1685 |
1686 // Inform sync of the update. | 1686 // Inform sync of the update. |
1687 ProcessTemplateURLChange( | 1687 ProcessTemplateURLChange( |
1688 FROM_HERE, existing_turl, syncer::SyncChange::ACTION_UPDATE); | 1688 FROM_HERE, existing_turl, syncer::SyncChange::ACTION_UPDATE); |
1689 | 1689 |
1690 if (default_search_provider_ == existing_turl && | 1690 if (default_search_provider_ == existing_turl && |
1691 default_search_provider_source_ == DefaultSearchManager::FROM_USER) { | 1691 default_search_provider_source_ == DefaultSearchManager::FROM_USER) { |
1692 default_search_manager_.SetUserSelectedDefaultSearchEngine( | 1692 default_search_manager_.SetUserSelectedDefaultSearchEngine( |
1693 default_search_provider_->data()); | 1693 default_search_provider_->data()); |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1990 existing_keyword_turl->short_name(), new_keyword, | 1990 existing_keyword_turl->short_name(), new_keyword, |
1991 existing_keyword_turl->url()); | 1991 existing_keyword_turl->url()); |
1992 } | 1992 } |
1993 } | 1993 } |
1994 template_urls_.push_back(template_url); | 1994 template_urls_.push_back(template_url); |
1995 AddToMaps(template_url); | 1995 AddToMaps(template_url); |
1996 | 1996 |
1997 if (newly_adding && | 1997 if (newly_adding && |
1998 (template_url->GetType() != | 1998 (template_url->GetType() != |
1999 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { | 1999 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { |
2000 if (web_data_service_) | 2000 if (web_data_service_.get()) |
2001 web_data_service_->AddKeyword(template_url->data()); | 2001 web_data_service_->AddKeyword(template_url->data()); |
2002 | 2002 |
2003 // Inform sync of the addition. Note that this will assign a GUID to | 2003 // Inform sync of the addition. Note that this will assign a GUID to |
2004 // template_url and add it to the guid_to_template_map_. | 2004 // template_url and add it to the guid_to_template_map_. |
2005 ProcessTemplateURLChange(FROM_HERE, | 2005 ProcessTemplateURLChange(FROM_HERE, |
2006 template_url, | 2006 template_url, |
2007 syncer::SyncChange::ACTION_ADD); | 2007 syncer::SyncChange::ACTION_ADD); |
2008 } | 2008 } |
2009 | 2009 |
2010 return true; | 2010 return true; |
2011 } | 2011 } |
2012 | 2012 |
2013 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { | 2013 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { |
2014 DCHECK(template_url != default_search_provider_); | 2014 DCHECK(template_url != default_search_provider_); |
2015 | 2015 |
2016 TemplateURLVector::iterator i = | 2016 TemplateURLVector::iterator i = |
2017 std::find(template_urls_.begin(), template_urls_.end(), template_url); | 2017 std::find(template_urls_.begin(), template_urls_.end(), template_url); |
2018 if (i == template_urls_.end()) | 2018 if (i == template_urls_.end()) |
2019 return; | 2019 return; |
2020 | 2020 |
2021 RemoveFromMaps(template_url); | 2021 RemoveFromMaps(template_url); |
2022 | 2022 |
2023 // Remove it from the vector containing all TemplateURLs. | 2023 // Remove it from the vector containing all TemplateURLs. |
2024 template_urls_.erase(i); | 2024 template_urls_.erase(i); |
2025 | 2025 |
2026 if (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) { | 2026 if (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) { |
2027 if (web_data_service_) | 2027 if (web_data_service_.get()) |
2028 web_data_service_->RemoveKeyword(template_url->id()); | 2028 web_data_service_->RemoveKeyword(template_url->id()); |
2029 | 2029 |
2030 // Inform sync of the deletion. | 2030 // Inform sync of the deletion. |
2031 ProcessTemplateURLChange(FROM_HERE, | 2031 ProcessTemplateURLChange(FROM_HERE, |
2032 template_url, | 2032 template_url, |
2033 syncer::SyncChange::ACTION_DELETE); | 2033 syncer::SyncChange::ACTION_DELETE); |
2034 | 2034 |
2035 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, | 2035 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, |
2036 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); | 2036 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); |
2037 } | 2037 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2096 // database and the |default_search_provider|. | 2096 // database and the |default_search_provider|. |
2097 default_search_provider_ = template_url; | 2097 default_search_provider_ = template_url; |
2098 // Prevent us from saving any other entries, or creating a new one. | 2098 // Prevent us from saving any other entries, or creating a new one. |
2099 default_from_prefs = NULL; | 2099 default_from_prefs = NULL; |
2100 ++i; | 2100 ++i; |
2101 continue; | 2101 continue; |
2102 } | 2102 } |
2103 | 2103 |
2104 RemoveFromMaps(template_url); | 2104 RemoveFromMaps(template_url); |
2105 i = template_urls->erase(i); | 2105 i = template_urls->erase(i); |
2106 if (web_data_service_) | 2106 if (web_data_service_.get()) |
2107 web_data_service_->RemoveKeyword(template_url->id()); | 2107 web_data_service_->RemoveKeyword(template_url->id()); |
2108 delete template_url; | 2108 delete template_url; |
2109 } else { | 2109 } else { |
2110 ++i; | 2110 ++i; |
2111 } | 2111 } |
2112 } | 2112 } |
2113 | 2113 |
2114 if (default_from_prefs) { | 2114 if (default_from_prefs) { |
2115 default_search_provider_ = NULL; | 2115 default_search_provider_ = NULL; |
2116 default_search_provider_source_ = DefaultSearchManager::FROM_POLICY; | 2116 default_search_provider_source_ = DefaultSearchManager::FROM_POLICY; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2289 TemplateURLVector* template_urls) { | 2289 TemplateURLVector* template_urls) { |
2290 DCHECK(template_urls); | 2290 DCHECK(template_urls); |
2291 for (TemplateURLVector::iterator i = template_urls->begin(); | 2291 for (TemplateURLVector::iterator i = template_urls->begin(); |
2292 i != template_urls->end(); ++i) { | 2292 i != template_urls->end(); ++i) { |
2293 TemplateURL* template_url = *i; | 2293 TemplateURL* template_url = *i; |
2294 DCHECK(template_url); | 2294 DCHECK(template_url); |
2295 if (template_url->sync_guid().empty() && | 2295 if (template_url->sync_guid().empty() && |
2296 (template_url->GetType() != | 2296 (template_url->GetType() != |
2297 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { | 2297 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { |
2298 template_url->data_.sync_guid = base::GenerateGUID(); | 2298 template_url->data_.sync_guid = base::GenerateGUID(); |
2299 if (web_data_service_) | 2299 if (web_data_service_.get()) |
2300 web_data_service_->UpdateKeyword(template_url->data()); | 2300 web_data_service_->UpdateKeyword(template_url->data()); |
2301 } | 2301 } |
2302 } | 2302 } |
2303 } | 2303 } |
2304 | 2304 |
2305 void TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged() { | 2305 void TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged() { |
2306 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2306 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
2307 &dsp_change_origin_, DSP_CHANGE_SYNC_PREF); | 2307 &dsp_change_origin_, DSP_CHANGE_SYNC_PREF); |
2308 | 2308 |
2309 std::string new_guid = | 2309 std::string new_guid = |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2369 | 2369 |
2370 if (most_recently_intalled_default) { | 2370 if (most_recently_intalled_default) { |
2371 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2371 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
2372 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | 2372 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); |
2373 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | 2373 default_search_manager_.SetExtensionControlledDefaultSearchEngine( |
2374 most_recently_intalled_default->data()); | 2374 most_recently_intalled_default->data()); |
2375 } else { | 2375 } else { |
2376 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | 2376 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); |
2377 } | 2377 } |
2378 } | 2378 } |
OLD | NEW |