OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_model.h" | 5 #include "chrome/browser/search_engines/template_url_model.h" |
6 | 6 |
7 | 7 |
8 #include "app/l10n_util.h" | 8 #include "app/l10n_util.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
703 | 703 |
704 void TemplateURLModel::NotifyLoaded() { | 704 void TemplateURLModel::NotifyLoaded() { |
705 NotificationService::current()->Notify( | 705 NotificationService::current()->Notify( |
706 NotificationType::TEMPLATE_URL_MODEL_LOADED, | 706 NotificationType::TEMPLATE_URL_MODEL_LOADED, |
707 Source<TemplateURLModel>(this), | 707 Source<TemplateURLModel>(this), |
708 NotificationService::NoDetails()); | 708 NotificationService::NoDetails()); |
709 } | 709 } |
710 | 710 |
711 void TemplateURLModel::MergeEnginesFromPrepopulateData() { | 711 void TemplateURLModel::MergeEnginesFromPrepopulateData() { |
712 // Build a map from prepopulate id to TemplateURL of existing urls. | 712 // Build a map from prepopulate id to TemplateURL of existing urls. |
713 std::map<int, const TemplateURL*> id_to_turl; | 713 typedef std::map<int, const TemplateURL*> IDMap; |
714 for (size_t i = 0; i < template_urls_.size(); ++i) { | 714 IDMap id_to_turl; |
715 if (template_urls_[i]->prepopulate_id() > 0) | 715 for (TemplateURLVector::const_iterator i(template_urls_.begin()); |
716 id_to_turl[template_urls_[i]->prepopulate_id()] = template_urls_[i]; | 716 i != template_urls_.end(); ++i) { |
| 717 int prepopulate_id = (*i)->prepopulate_id(); |
| 718 if (prepopulate_id > 0) |
| 719 id_to_turl[prepopulate_id] = *i; |
717 } | 720 } |
718 | 721 |
719 std::vector<TemplateURL*> loaded_urls; | 722 std::vector<TemplateURL*> loaded_urls; |
720 size_t default_search_index; | 723 size_t default_search_index; |
721 TemplateURLPrepopulateData::GetPrepopulatedEngines(GetPrefs(), | 724 TemplateURLPrepopulateData::GetPrepopulatedEngines(GetPrefs(), |
722 &loaded_urls, | 725 &loaded_urls, |
723 &default_search_index); | 726 &default_search_index); |
724 | 727 |
| 728 std::set<int> updated_ids; |
725 for (size_t i = 0; i < loaded_urls.size(); ++i) { | 729 for (size_t i = 0; i < loaded_urls.size(); ++i) { |
726 scoped_ptr<TemplateURL> t_url(loaded_urls[i]); | 730 scoped_ptr<TemplateURL> t_url(loaded_urls[i]); |
727 | 731 int t_url_id = t_url->prepopulate_id(); |
728 if (!t_url->prepopulate_id()) { | 732 if (!t_url_id || updated_ids.count(t_url_id)) { |
729 // Prepopulate engines need an id. | 733 // Prepopulate engines need a unique id. |
730 NOTREACHED(); | 734 NOTREACHED(); |
731 continue; | 735 continue; |
732 } | 736 } |
733 | 737 |
734 const TemplateURL* existing_url = id_to_turl[t_url->prepopulate_id()]; | 738 IDMap::iterator existing_url_iter(id_to_turl.find(t_url_id)); |
735 if (existing_url) { | 739 if (existing_url_iter != id_to_turl.end()) { |
| 740 const TemplateURL* existing_url = existing_url_iter->second; |
736 if (!existing_url->safe_for_autoreplace()) { | 741 if (!existing_url->safe_for_autoreplace()) { |
737 // User edited the entry, preserve the keyword and description. | 742 // User edited the entry, preserve the keyword and description. |
738 loaded_urls[i]->set_safe_for_autoreplace(false); | 743 loaded_urls[i]->set_safe_for_autoreplace(false); |
739 loaded_urls[i]->set_keyword(existing_url->keyword()); | 744 loaded_urls[i]->set_keyword(existing_url->keyword()); |
740 loaded_urls[i]->set_autogenerate_keyword( | 745 loaded_urls[i]->set_autogenerate_keyword( |
741 existing_url->autogenerate_keyword()); | 746 existing_url->autogenerate_keyword()); |
742 loaded_urls[i]->set_short_name(existing_url->short_name()); | 747 loaded_urls[i]->set_short_name(existing_url->short_name()); |
743 } | 748 } |
744 Replace(existing_url, loaded_urls[i]); | 749 Replace(existing_url, loaded_urls[i]); |
745 id_to_turl[t_url->prepopulate_id()] = loaded_urls[i]; | 750 id_to_turl.erase(existing_url_iter); |
746 } else { | 751 } else { |
747 Add(loaded_urls[i]); | 752 Add(loaded_urls[i]); |
748 } | 753 } |
749 if (i == default_search_index && !default_search_provider_) | 754 if (i == default_search_index && !default_search_provider_) |
750 SetDefaultSearchProvider(loaded_urls[i]); | 755 SetDefaultSearchProvider(loaded_urls[i]); |
751 | 756 |
| 757 updated_ids.insert(t_url_id); |
752 t_url.release(); | 758 t_url.release(); |
753 } | 759 } |
| 760 |
| 761 // Remove any prepopulated engines which are no longer in the master list, as |
| 762 // long as the user hasn't modified them or made them the default engine. |
| 763 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) { |
| 764 const TemplateURL* template_url = i->second; |
| 765 // We use default_search_provider_ instead of GetDefaultSearchProvider() |
| 766 // because we're running before |loaded_| is set, and calling |
| 767 // GetDefaultSearchProvider() will erroneously try to read the prefs. |
| 768 if ((template_url->safe_for_autoreplace()) && |
| 769 (template_url != default_search_provider_)) |
| 770 Remove(template_url); |
| 771 } |
754 } | 772 } |
755 | 773 |
756 void TemplateURLModel::SaveDefaultSearchProviderToPrefs( | 774 void TemplateURLModel::SaveDefaultSearchProviderToPrefs( |
757 const TemplateURL* t_url) { | 775 const TemplateURL* t_url) { |
758 PrefService* prefs = GetPrefs(); | 776 PrefService* prefs = GetPrefs(); |
759 if (!prefs) | 777 if (!prefs) |
760 return; | 778 return; |
761 | 779 |
762 RegisterPrefs(prefs); | 780 RegisterPrefs(prefs); |
763 | 781 |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
994 AddToMaps(t_url); | 1012 AddToMaps(t_url); |
995 something_changed = true; | 1013 something_changed = true; |
996 } | 1014 } |
997 } | 1015 } |
998 | 1016 |
999 if (something_changed && loaded_) { | 1017 if (something_changed && loaded_) { |
1000 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, | 1018 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, |
1001 OnTemplateURLModelChanged()); | 1019 OnTemplateURLModelChanged()); |
1002 } | 1020 } |
1003 } | 1021 } |
OLD | NEW |