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 870 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 if (!loaded_) | 881 if (!loaded_) |
882 visits_to_add_.push_back(*visit_details.ptr()); | 882 visits_to_add_.push_back(*visit_details.ptr()); |
883 else | 883 else |
884 UpdateKeywordSearchTermsForURL(*visit_details.ptr()); | 884 UpdateKeywordSearchTermsForURL(*visit_details.ptr()); |
885 } else if (type == chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED) { | 885 } else if (type == chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED) { |
886 // Policy has been updated, so the default search prefs may be different. | 886 // Policy has been updated, so the default search prefs may be different. |
887 // Reload the default search provider from them. | 887 // Reload the default search provider from them. |
888 // TODO(pkasting): Rather than communicating via prefs, we should eventually | 888 // TODO(pkasting): Rather than communicating via prefs, we should eventually |
889 // observe policy changes directly. | 889 // observe policy changes directly. |
890 UpdateDefaultSearch(); | 890 UpdateDefaultSearch(); |
891 } else if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) { | 891 } else { |
| 892 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED, type); |
892 if (loaded_) { | 893 if (loaded_) { |
893 GoogleBaseURLChanged( | 894 GoogleBaseURLChanged( |
894 content::Details<GoogleURLTracker::UpdatedDetails>(details)->first); | 895 content::Details<GoogleURLTracker::UpdatedDetails>(details)->first); |
895 } | 896 } |
896 } else { | |
897 NOTREACHED(); | |
898 } | 897 } |
899 } | 898 } |
900 | 899 |
901 void TemplateURLService::Shutdown() { | 900 void TemplateURLService::Shutdown() { |
902 // This check has to be done at Shutdown() instead of in the dtor to ensure | 901 // This check has to be done at Shutdown() instead of in the dtor to ensure |
903 // that no clients of WebDataService are holding ptrs to it after the first | 902 // that no clients of WebDataService are holding ptrs to it after the first |
904 // phase of the KeyedService Shutdown() process. | 903 // phase of the KeyedService Shutdown() process. |
905 if (load_handle_) { | 904 if (load_handle_) { |
906 DCHECK(service_.get()); | 905 DCHECK(service_.get()); |
907 service_->CancelRequest(load_handle_); | 906 service_->CancelRequest(load_handle_); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 | 984 |
986 // Explicitly don't check for conflicts against extension keywords; in this | 985 // Explicitly don't check for conflicts against extension keywords; in this |
987 // case the functions which modify the keyword map know how to handle the | 986 // case the functions which modify the keyword map know how to handle the |
988 // conflicts. | 987 // conflicts. |
989 // TODO(mpcomplete): If we allow editing extension keywords, then those will | 988 // TODO(mpcomplete): If we allow editing extension keywords, then those will |
990 // need to undergo conflict resolution. | 989 // need to undergo conflict resolution. |
991 TemplateURL* existing_keyword_turl = | 990 TemplateURL* existing_keyword_turl = |
992 FindNonExtensionTemplateURLForKeyword(turl->keyword()); | 991 FindNonExtensionTemplateURLForKeyword(turl->keyword()); |
993 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) { | 992 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) { |
994 if (!existing_turl) { | 993 if (!existing_turl) { |
995 NOTREACHED() << "Unexpected sync change state."; | |
996 error = sync_error_factory_->CreateAndUploadError( | 994 error = sync_error_factory_->CreateAndUploadError( |
997 FROM_HERE, | 995 FROM_HERE, |
998 "ProcessSyncChanges failed on ChangeType ACTION_DELETE"); | 996 "ProcessSyncChanges failed on ChangeType ACTION_DELETE"); |
999 LOG(ERROR) << "Trying to delete a non-existent TemplateURL."; | |
1000 continue; | 997 continue; |
1001 } | 998 } |
1002 if (existing_turl == GetDefaultSearchProvider()) { | 999 if (existing_turl == GetDefaultSearchProvider()) { |
1003 // The only way Sync can attempt to delete the default search provider | 1000 // The only way Sync can attempt to delete the default search provider |
1004 // is if we had changed the kSyncedDefaultSearchProviderGUID | 1001 // is if we had changed the kSyncedDefaultSearchProviderGUID |
1005 // preference, but perhaps it has not yet been received. To avoid | 1002 // preference, but perhaps it has not yet been received. To avoid |
1006 // situations where this has come in erroneously, we will un-delete | 1003 // situations where this has come in erroneously, we will un-delete |
1007 // the current default search from the Sync data. If the pref really | 1004 // the current default search from the Sync data. If the pref really |
1008 // does arrive later, then default search will change to the correct | 1005 // does arrive later, then default search will change to the correct |
1009 // entry, but we'll have this extra entry sitting around. The result is | 1006 // entry, but we'll have this extra entry sitting around. The result is |
(...skipping 17 matching lines...) Expand all Loading... |
1027 syncer::SyncChange::ACTION_ADD, | 1024 syncer::SyncChange::ACTION_ADD, |
1028 sync_data)); | 1025 sync_data)); |
1029 // Ignore the delete attempt. This means we never end up reseting the | 1026 // Ignore the delete attempt. This means we never end up reseting the |
1030 // default search provider due to an ACTION_DELETE from sync. | 1027 // default search provider due to an ACTION_DELETE from sync. |
1031 continue; | 1028 continue; |
1032 } | 1029 } |
1033 | 1030 |
1034 Remove(existing_turl); | 1031 Remove(existing_turl); |
1035 } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { | 1032 } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { |
1036 if (existing_turl) { | 1033 if (existing_turl) { |
1037 NOTREACHED() << "Unexpected sync change state."; | |
1038 error = sync_error_factory_->CreateAndUploadError( | 1034 error = sync_error_factory_->CreateAndUploadError( |
1039 FROM_HERE, | 1035 FROM_HERE, |
1040 "ProcessSyncChanges failed on ChangeType ACTION_ADD"); | 1036 "ProcessSyncChanges failed on ChangeType ACTION_ADD"); |
1041 LOG(ERROR) << "Trying to add an existing TemplateURL."; | |
1042 continue; | 1037 continue; |
1043 } | 1038 } |
1044 const std::string guid = turl->sync_guid(); | 1039 const std::string guid = turl->sync_guid(); |
1045 if (existing_keyword_turl) { | 1040 if (existing_keyword_turl) { |
1046 // Resolve any conflicts so we can safely add the new entry. | 1041 // Resolve any conflicts so we can safely add the new entry. |
1047 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl, | 1042 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl, |
1048 &new_changes); | 1043 &new_changes); |
1049 } | 1044 } |
1050 // Force the local ID to kInvalidTemplateURLID so we can add it. | 1045 // Force the local ID to kInvalidTemplateURLID so we can add it. |
1051 TemplateURLData data(turl->data()); | 1046 TemplateURLData data(turl->data()); |
1052 data.id = kInvalidTemplateURLID; | 1047 data.id = kInvalidTemplateURLID; |
1053 Add(new TemplateURL(profile_, data)); | 1048 Add(new TemplateURL(profile_, data)); |
1054 | 1049 |
1055 // Possibly set the newly added |turl| as the default search provider. | 1050 // Possibly set the newly added |turl| as the default search provider. |
1056 SetDefaultSearchProviderIfNewlySynced(guid); | 1051 SetDefaultSearchProviderIfNewlySynced(guid); |
1057 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE) { | 1052 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE) { |
1058 if (!existing_turl) { | 1053 if (!existing_turl) { |
1059 NOTREACHED() << "Unexpected sync change state."; | |
1060 error = sync_error_factory_->CreateAndUploadError( | 1054 error = sync_error_factory_->CreateAndUploadError( |
1061 FROM_HERE, | 1055 FROM_HERE, |
1062 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE"); | 1056 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE"); |
1063 LOG(ERROR) << "Trying to update a non-existent TemplateURL."; | |
1064 continue; | 1057 continue; |
1065 } | 1058 } |
1066 if (existing_keyword_turl && (existing_keyword_turl != existing_turl)) { | 1059 if (existing_keyword_turl && (existing_keyword_turl != existing_turl)) { |
1067 // Resolve any conflicts with other entries so we can safely update the | 1060 // Resolve any conflicts with other entries so we can safely update the |
1068 // keyword. | 1061 // keyword. |
1069 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl, | 1062 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl, |
1070 &new_changes); | 1063 &new_changes); |
1071 } | 1064 } |
1072 UIThreadSearchTermsData search_terms_data(existing_turl->profile()); | 1065 UIThreadSearchTermsData search_terms_data(existing_turl->profile()); |
1073 if (UpdateNoNotify(existing_turl, *turl, search_terms_data)) | 1066 if (UpdateNoNotify(existing_turl, *turl, search_terms_data)) |
1074 NotifyObservers(); | 1067 NotifyObservers(); |
1075 } else { | 1068 } else { |
1076 // We've unexpectedly received an ACTION_INVALID. | 1069 // We've unexpectedly received an ACTION_INVALID. |
1077 NOTREACHED() << "Unexpected sync change state."; | |
1078 error = sync_error_factory_->CreateAndUploadError( | 1070 error = sync_error_factory_->CreateAndUploadError( |
1079 FROM_HERE, | 1071 FROM_HERE, |
1080 "ProcessSyncChanges received an ACTION_INVALID"); | 1072 "ProcessSyncChanges received an ACTION_INVALID"); |
1081 } | 1073 } |
1082 } | 1074 } |
1083 | 1075 |
1084 // If something went wrong, we want to prematurely exit to avoid pushing | 1076 // If something went wrong, we want to prematurely exit to avoid pushing |
1085 // inconsistent data to Sync. We return the last error we received. | 1077 // inconsistent data to Sync. We return the last error we received. |
1086 if (error.IsSet()) | 1078 if (error.IsSet()) |
1087 return error; | 1079 return error; |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1446 profile_source); | 1438 profile_source); |
1447 notification_registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | 1439 notification_registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
1448 profile_source); | 1440 profile_source); |
1449 pref_change_registrar_.Init(GetPrefs()); | 1441 pref_change_registrar_.Init(GetPrefs()); |
1450 pref_change_registrar_.Add( | 1442 pref_change_registrar_.Add( |
1451 prefs::kSyncedDefaultSearchProviderGUID, | 1443 prefs::kSyncedDefaultSearchProviderGUID, |
1452 base::Bind( | 1444 base::Bind( |
1453 &TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged, | 1445 &TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged, |
1454 base::Unretained(this))); | 1446 base::Unretained(this))); |
1455 } | 1447 } |
1456 notification_registrar_.Add(this, | 1448 notification_registrar_.Add( |
1457 chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED, | 1449 this, chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED, |
1458 content::NotificationService::AllSources()); | 1450 content::NotificationService::AllSources()); |
1459 | 1451 |
1460 if (num_initializers > 0) { | 1452 if (num_initializers > 0) { |
1461 // This path is only hit by test code and is used to simulate a loaded | 1453 // This path is only hit by test code and is used to simulate a loaded |
1462 // TemplateURLService. | 1454 // TemplateURLService. |
1463 ChangeToLoadedState(); | 1455 ChangeToLoadedState(); |
1464 | 1456 |
1465 // Add specific initializers, if any. | 1457 // Add specific initializers, if any. |
1466 for (int i(0); i < num_initializers; ++i) { | 1458 for (int i(0); i < num_initializers; ++i) { |
1467 DCHECK(initializers[i].keyword); | 1459 DCHECK(initializers[i].keyword); |
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2180 // likely to race with the update from the other client, resulting in | 2172 // likely to race with the update from the other client, resulting in |
2181 // a possibly random default search provider. | 2173 // a possibly random default search provider. |
2182 if (sync_processor_.get() && url && !url->sync_guid().empty() && | 2174 if (sync_processor_.get() && url && !url->sync_guid().empty() && |
2183 GetPrefs() && !processing_syncer_changes_) { | 2175 GetPrefs() && !processing_syncer_changes_) { |
2184 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, | 2176 GetPrefs()->SetString(prefs::kSyncedDefaultSearchProviderGUID, |
2185 url->sync_guid()); | 2177 url->sync_guid()); |
2186 } | 2178 } |
2187 } | 2179 } |
2188 | 2180 |
2189 if (service_.get()) | 2181 if (service_.get()) |
2190 service_->SetDefaultSearchProvider(url); | 2182 service_->SetDefaultSearchProviderID(url ? url->id() : 0); |
2191 | 2183 |
2192 // Inform sync the change to the show_in_default_list flag. | 2184 // Inform sync the change to the show_in_default_list flag. |
2193 if (url) | 2185 if (url) |
2194 ProcessTemplateURLChange(FROM_HERE, | 2186 ProcessTemplateURLChange(FROM_HERE, |
2195 url, | 2187 url, |
2196 syncer::SyncChange::ACTION_UPDATE); | 2188 syncer::SyncChange::ACTION_UPDATE); |
2197 return true; | 2189 return true; |
2198 } | 2190 } |
2199 | 2191 |
2200 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, | 2192 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2245 // template_url and add it to the guid_to_template_map_. | 2237 // template_url and add it to the guid_to_template_map_. |
2246 ProcessTemplateURLChange(FROM_HERE, | 2238 ProcessTemplateURLChange(FROM_HERE, |
2247 template_url, | 2239 template_url, |
2248 syncer::SyncChange::ACTION_ADD); | 2240 syncer::SyncChange::ACTION_ADD); |
2249 } | 2241 } |
2250 | 2242 |
2251 return true; | 2243 return true; |
2252 } | 2244 } |
2253 | 2245 |
2254 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { | 2246 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { |
| 2247 DCHECK(template_url != default_search_provider_); |
| 2248 |
2255 TemplateURLVector::iterator i = | 2249 TemplateURLVector::iterator i = |
2256 std::find(template_urls_.begin(), template_urls_.end(), template_url); | 2250 std::find(template_urls_.begin(), template_urls_.end(), template_url); |
2257 if (i == template_urls_.end()) | 2251 if (i == template_urls_.end()) |
2258 return; | 2252 return; |
2259 | 2253 |
2260 if (template_url == default_search_provider_) { | |
2261 // Should never delete the default search provider. | |
2262 NOTREACHED(); | |
2263 return; | |
2264 } | |
2265 | |
2266 RemoveFromMaps(template_url); | 2254 RemoveFromMaps(template_url); |
2267 | 2255 |
2268 // Remove it from the vector containing all TemplateURLs. | 2256 // Remove it from the vector containing all TemplateURLs. |
2269 template_urls_.erase(i); | 2257 template_urls_.erase(i); |
2270 | 2258 |
2271 if (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) { | 2259 if (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) { |
2272 if (service_.get()) | 2260 if (service_.get()) |
2273 service_->RemoveKeyword(template_url->id()); | 2261 service_->RemoveKeyword(template_url->id()); |
2274 | 2262 |
2275 // Inform sync of the deletion. | 2263 // Inform sync of the deletion. |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2735 new_dse = *i; | 2723 new_dse = *i; |
2736 break; | 2724 break; |
2737 } | 2725 } |
2738 } | 2726 } |
2739 } | 2727 } |
2740 } | 2728 } |
2741 if (!new_dse) | 2729 if (!new_dse) |
2742 new_dse = FindNewDefaultSearchProvider(); | 2730 new_dse = FindNewDefaultSearchProvider(); |
2743 SetDefaultSearchProviderNoNotify(new_dse); | 2731 SetDefaultSearchProviderNoNotify(new_dse); |
2744 } | 2732 } |
OLD | NEW |