OLD | NEW |
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 657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 } | 668 } |
669 | 669 |
670 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { | 670 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { |
671 return !is_default_search_managed() && | 671 return !is_default_search_managed() && |
672 !IsExtensionControlledDefaultSearch() && | 672 !IsExtensionControlledDefaultSearch() && |
673 (url != GetDefaultSearchProvider()) && | 673 (url != GetDefaultSearchProvider()) && |
674 url->url_ref().SupportsReplacement() && | 674 url->url_ref().SupportsReplacement() && |
675 (url->GetType() == TemplateURL::NORMAL); | 675 (url->GetType() == TemplateURL::NORMAL); |
676 } | 676 } |
677 | 677 |
678 void TemplateURLService::SetDefaultSearchProvider(TemplateURL* url) { | 678 void TemplateURLService::SetUserSelectedDefaultSearchProvider( |
679 DCHECK(!is_default_search_managed_); | 679 TemplateURL* url) { |
680 // Omnibox keywords cannot be made default. Extension-controlled search | 680 SetDefaultSearchProvider(url); |
681 // engines can be made default only by the extension itself because they | |
682 // aren't persisted. | |
683 DCHECK(!url || (url->GetType() == TemplateURL::NORMAL)); | |
684 | |
685 // Always persist the setting in the database, that way if the backup | |
686 // signature has changed out from under us it gets reset correctly. | |
687 if (SetDefaultSearchProviderNoNotify(url)) | |
688 NotifyObservers(); | |
689 } | 681 } |
690 | 682 |
691 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { | 683 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { |
692 if (loaded_ && !load_failed_) | 684 if (loaded_ && !load_failed_) |
693 return default_search_provider_; | 685 return default_search_provider_; |
694 // We're not loaded, rely on the default search provider stored in prefs. | 686 // We're not loaded, rely on the default search provider stored in prefs. |
695 return initial_default_search_provider_.get(); | 687 return initial_default_search_provider_.get(); |
696 } | 688 } |
697 | 689 |
698 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( | 690 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
921 | 913 |
922 void TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged() { | 914 void TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged() { |
923 // Listen for changes to the default search from Sync. | 915 // Listen for changes to the default search from Sync. |
924 PrefService* prefs = GetPrefs(); | 916 PrefService* prefs = GetPrefs(); |
925 TemplateURL* new_default_search = GetTemplateURLForGUID( | 917 TemplateURL* new_default_search = GetTemplateURLForGUID( |
926 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID)); | 918 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID)); |
927 if (new_default_search && !is_default_search_managed_) { | 919 if (new_default_search && !is_default_search_managed_) { |
928 if (new_default_search != GetDefaultSearchProvider()) { | 920 if (new_default_search != GetDefaultSearchProvider()) { |
929 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 921 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
930 &dsp_change_origin_, DSP_CHANGE_SYNC_PREF); | 922 &dsp_change_origin_, DSP_CHANGE_SYNC_PREF); |
931 SetDefaultSearchProvider(new_default_search); | 923 SetUserSelectedDefaultSearchProvider(new_default_search); |
932 pending_synced_default_search_ = false; | 924 pending_synced_default_search_ = false; |
933 } | 925 } |
934 } else { | 926 } else { |
935 // If it's not there, or if default search is currently managed, set a | 927 // If it's not there, or if default search is currently managed, set a |
936 // flag to indicate that we waiting on the search engine entry to come | 928 // flag to indicate that we waiting on the search engine entry to come |
937 // in through Sync. | 929 // in through Sync. |
938 pending_synced_default_search_ = true; | 930 pending_synced_default_search_ = true; |
939 } | 931 } |
940 UpdateDefaultSearch(); | 932 UpdateDefaultSearch(); |
941 } | 933 } |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1199 &local_data_map, &merge_result); | 1191 &local_data_map, &merge_result); |
1200 } | 1192 } |
1201 } | 1193 } |
1202 | 1194 |
1203 // If there is a pending synced default search provider that was processed | 1195 // If there is a pending synced default search provider that was processed |
1204 // above, set it now. | 1196 // above, set it now. |
1205 TemplateURL* pending_default = GetPendingSyncedDefaultSearchProvider(); | 1197 TemplateURL* pending_default = GetPendingSyncedDefaultSearchProvider(); |
1206 if (pending_default) { | 1198 if (pending_default) { |
1207 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 1199 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
1208 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD); | 1200 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD); |
1209 SetDefaultSearchProvider(pending_default); | 1201 SetUserSelectedDefaultSearchProvider(pending_default); |
1210 } | 1202 } |
1211 | 1203 |
1212 // The remaining SyncData in local_data_map should be everything that needs to | 1204 // The remaining SyncData in local_data_map should be everything that needs to |
1213 // be pushed as ADDs to sync. | 1205 // be pushed as ADDs to sync. |
1214 for (SyncDataMap::const_iterator iter = local_data_map.begin(); | 1206 for (SyncDataMap::const_iterator iter = local_data_map.begin(); |
1215 iter != local_data_map.end(); ++iter) { | 1207 iter != local_data_map.end(); ++iter) { |
1216 new_changes.push_back( | 1208 new_changes.push_back( |
1217 syncer::SyncChange(FROM_HERE, | 1209 syncer::SyncChange(FROM_HERE, |
1218 syncer::SyncChange::ACTION_ADD, | 1210 syncer::SyncChange::ACTION_ADD, |
1219 iter->second)); | 1211 iter->second)); |
(...skipping 902 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2122 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2114 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
2123 &dsp_change_origin_, DSP_CHANGE_SYNC_NOT_MANAGED); | 2115 &dsp_change_origin_, DSP_CHANGE_SYNC_NOT_MANAGED); |
2124 SetDefaultSearchProviderNoNotify(synced_default); | 2116 SetDefaultSearchProviderNoNotify(synced_default); |
2125 } else { | 2117 } else { |
2126 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); | 2118 SetDefaultSearchProviderNoNotify(FindNewDefaultSearchProvider()); |
2127 } | 2119 } |
2128 } | 2120 } |
2129 NotifyObservers(); | 2121 NotifyObservers(); |
2130 } | 2122 } |
2131 | 2123 |
| 2124 void TemplateURLService::SetDefaultSearchProvider( |
| 2125 TemplateURL* url) { |
| 2126 DCHECK(!is_default_search_managed_); |
| 2127 // Omnibox keywords cannot be made default. Extension-controlled search |
| 2128 // engines can be made default only by the extension itself because they |
| 2129 // aren't persisted. |
| 2130 DCHECK(!url || (url->GetType() == TemplateURL::NORMAL)); |
| 2131 |
| 2132 // Always persist the setting in the database, that way if the backup |
| 2133 // signature has changed out from under us it gets reset correctly. |
| 2134 if (SetDefaultSearchProviderNoNotify(url)) |
| 2135 NotifyObservers(); |
| 2136 } |
| 2137 |
2132 bool TemplateURLService::SetDefaultSearchProviderNoNotify(TemplateURL* url) { | 2138 bool TemplateURLService::SetDefaultSearchProviderNoNotify(TemplateURL* url) { |
2133 if (url) { | 2139 if (url) { |
2134 if (std::find(template_urls_.begin(), template_urls_.end(), url) == | 2140 if (std::find(template_urls_.begin(), template_urls_.end(), url) == |
2135 template_urls_.end()) | 2141 template_urls_.end()) |
2136 return false; | 2142 return false; |
2137 // Omnibox keywords cannot be made default. | 2143 // Omnibox keywords cannot be made default. |
2138 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, url->GetType()); | 2144 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, url->GetType()); |
2139 } | 2145 } |
2140 | 2146 |
2141 // Only bother reassigning |url| if it has changed. Notice that we don't just | 2147 // Only bother reassigning |url| if it has changed. Notice that we don't just |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2741 new_dse = *i; | 2747 new_dse = *i; |
2742 break; | 2748 break; |
2743 } | 2749 } |
2744 } | 2750 } |
2745 } | 2751 } |
2746 } | 2752 } |
2747 if (!new_dse) | 2753 if (!new_dse) |
2748 new_dse = FindNewDefaultSearchProvider(); | 2754 new_dse = FindNewDefaultSearchProvider(); |
2749 SetDefaultSearchProviderNoNotify(new_dse); | 2755 SetDefaultSearchProviderNoNotify(new_dse); |
2750 } | 2756 } |
OLD | NEW |