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

Side by Side Diff: chrome/browser/search_engines/template_url_service.cc

Issue 10694096: Made the deletion of the default search provider defensive in TemplateURLService. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: typo fix Created 8 years, 5 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
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 "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/environment.h" 9 #include "base/environment.h"
10 #include "base/guid.h" 10 #include "base/guid.h"
(...skipping 914 matching lines...) Expand 10 before | Expand all | Expand 10 after
925 FindNonExtensionTemplateURLForKeyword(turl->keyword()); 925 FindNonExtensionTemplateURLForKeyword(turl->keyword());
926 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) { 926 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
927 if (!existing_turl) { 927 if (!existing_turl) {
928 NOTREACHED() << "Unexpected sync change state."; 928 NOTREACHED() << "Unexpected sync change state.";
929 error = sync_error_factory_->CreateAndUploadError( 929 error = sync_error_factory_->CreateAndUploadError(
930 FROM_HERE, 930 FROM_HERE,
931 "ProcessSyncChanges failed on ChangeType ACTION_DELETE"); 931 "ProcessSyncChanges failed on ChangeType ACTION_DELETE");
932 LOG(ERROR) << "Trying to delete a non-existent TemplateURL."; 932 LOG(ERROR) << "Trying to delete a non-existent TemplateURL.";
933 continue; 933 continue;
934 } 934 }
935 bool delete_default = (existing_turl == GetDefaultSearchProvider()); 935 if (existing_turl == GetDefaultSearchProvider()) {
936 // The only way Sync can attempt to delete the default search provider
937 // is if we had changed the kSyncedDefaultSearchProviderGUID
938 // preference, but perhaps it has not yet been received. To avoid
939 // situations where this has come in erroneously, we will un-delete
940 // the current default search from the Sync data. If the pref really
941 // does arrive later, then default search will change to the correct
942 // entry, but we'll have this extra entry sitting around. The result is
943 // not ideal, but it prevents a far more severe bug where the default is
944 // unexpectedly swapped to something else. The user can safely delete
945 // the extra entry again later, if they choose. Most users who do not
946 // look at the search engines UI will not notice this.
947 // Note that we append a special character to the end of the keyword in
948 // an attempt to avoid a ping-poinging situation where receiving clients
949 // may try to continually delete the resurrected entry.
950 string16 updated_keyword = UniquifyKeyword(*existing_turl, true);
951 TemplateURLData data(existing_turl->data());
952 data.SetKeyword(updated_keyword);
953 TemplateURL new_turl(existing_turl->profile(), data);
954 UIThreadSearchTermsData search_terms_data(existing_turl->profile());
955 if (UpdateNoNotify(existing_turl, new_turl, search_terms_data))
956 NotifyObservers();
936 957
937 if (delete_default && is_default_search_managed_) { 958 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(new_turl);
938 NOTREACHED() << "Tried to delete managed default search provider"; 959 new_changes.push_back(
939 } else { 960 syncer::SyncChange(syncer::SyncChange::ACTION_ADD, sync_data));
940 if (delete_default) 961 // Ignore the delete attempt. This means we never end up reseting the
941 default_search_provider_ = NULL; 962 // default search provider due to an ACTION_DELETE from sync.
963 continue;
964 }
942 965
943 Remove(existing_turl); 966 Remove(existing_turl);
944
945 if (delete_default) {
946 AutoReset<DefaultSearchChangeOrigin> change_origin(
947 &dsp_change_origin_, DSP_CHANGE_SYNC_DELETE);
948 SetDefaultSearchProvider(FindNewDefaultSearchProvider());
949 }
950 }
951 } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { 967 } else if (iter->change_type() == syncer::SyncChange::ACTION_ADD) {
952 if (existing_turl) { 968 if (existing_turl) {
953 NOTREACHED() << "Unexpected sync change state."; 969 NOTREACHED() << "Unexpected sync change state.";
954 error = sync_error_factory_->CreateAndUploadError( 970 error = sync_error_factory_->CreateAndUploadError(
955 FROM_HERE, 971 FROM_HERE,
956 "ProcessSyncChanges failed on ChangeType ACTION_ADD"); 972 "ProcessSyncChanges failed on ChangeType ACTION_ADD");
957 LOG(ERROR) << "Trying to add an existing TemplateURL."; 973 LOG(ERROR) << "Trying to add an existing TemplateURL.";
958 continue; 974 continue;
959 } 975 }
960 std::string guid = turl->sync_guid(); 976 std::string guid = turl->sync_guid();
(...skipping 1120 matching lines...) Expand 10 before | Expand all | Expand 10 after
2081 TemplateURL* existing_keyword_turl = 2097 TemplateURL* existing_keyword_turl =
2082 FindNonExtensionTemplateURLForKeyword(template_url->keyword()); 2098 FindNonExtensionTemplateURLForKeyword(template_url->keyword());
2083 if (existing_keyword_turl != NULL) { 2099 if (existing_keyword_turl != NULL) {
2084 DCHECK_NE(existing_keyword_turl, template_url); 2100 DCHECK_NE(existing_keyword_turl, template_url);
2085 if (CanReplace(existing_keyword_turl)) { 2101 if (CanReplace(existing_keyword_turl)) {
2086 RemoveNoNotify(existing_keyword_turl); 2102 RemoveNoNotify(existing_keyword_turl);
2087 } else if (CanReplace(template_url)) { 2103 } else if (CanReplace(template_url)) {
2088 delete template_url; 2104 delete template_url;
2089 return false; 2105 return false;
2090 } else { 2106 } else {
2091 string16 new_keyword = UniquifyKeyword(*existing_keyword_turl); 2107 string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false);
2092 ResetTemplateURL(existing_keyword_turl, 2108 ResetTemplateURL(existing_keyword_turl,
2093 existing_keyword_turl->short_name(), new_keyword, 2109 existing_keyword_turl->short_name(), new_keyword,
2094 existing_keyword_turl->url()); 2110 existing_keyword_turl->url());
2095 } 2111 }
2096 } 2112 }
2097 } 2113 }
2098 template_urls_.push_back(template_url); 2114 template_urls_.push_back(template_url);
2099 AddToMaps(template_url); 2115 AddToMaps(template_url);
2100 2116
2101 if (newly_adding) { 2117 if (newly_adding) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2216 DCHECK(loaded_); 2232 DCHECK(loaded_);
2217 DCHECK(!guid.empty()); 2233 DCHECK(!guid.empty());
2218 2234
2219 TemplateURLData data(url->data()); 2235 TemplateURLData data(url->data());
2220 data.sync_guid = guid; 2236 data.sync_guid = guid;
2221 TemplateURL new_url(url->profile(), data); 2237 TemplateURL new_url(url->profile(), data);
2222 UIThreadSearchTermsData search_terms_data(url->profile()); 2238 UIThreadSearchTermsData search_terms_data(url->profile());
2223 UpdateNoNotify(url, new_url, search_terms_data); 2239 UpdateNoNotify(url, new_url, search_terms_data);
2224 } 2240 }
2225 2241
2226 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) { 2242 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl,
2227 // Already unique. 2243 bool force) {
2228 if (!GetTemplateURLForKeyword(turl.keyword())) 2244 if (!force) {
2229 return turl.keyword(); 2245 // Already unique.
2246 if (!GetTemplateURLForKeyword(turl.keyword()))
2247 return turl.keyword();
2230 2248
2231 // First, try to return the generated keyword for the TemplateURL. 2249 // First, try to return the generated keyword for the TemplateURL.
2232 GURL gurl(turl.url()); 2250 GURL gurl(turl.url());
2233 if (gurl.is_valid()) { 2251 if (gurl.is_valid()) {
2234 string16 keyword_candidate = GenerateKeyword(gurl); 2252 string16 keyword_candidate = GenerateKeyword(gurl);
2235 if (!GetTemplateURLForKeyword(keyword_candidate)) 2253 if (!GetTemplateURLForKeyword(keyword_candidate))
2236 return keyword_candidate; 2254 return keyword_candidate;
2255 }
2237 } 2256 }
2238 2257
2239 // We try to uniquify the keyword by appending a special character to the end. 2258 // We try to uniquify the keyword by appending a special character to the end.
2240 // This is a best-effort approach where we try to preserve the original 2259 // This is a best-effort approach where we try to preserve the original
2241 // keyword and let the user do what they will after our attempt. 2260 // keyword and let the user do what they will after our attempt.
2242 string16 keyword_candidate(turl.keyword()); 2261 string16 keyword_candidate(turl.keyword());
2243 do { 2262 do {
2244 keyword_candidate.append(ASCIIToUTF16("_")); 2263 keyword_candidate.append(ASCIIToUTF16("_"));
2245 } while (GetTemplateURLForKeyword(keyword_candidate)); 2264 } while (GetTemplateURLForKeyword(keyword_candidate));
2246 2265
(...skipping 28 matching lines...) Expand all
2275 // with us. Note that if we're being called from 2294 // with us. Note that if we're being called from
2276 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather 2295 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather
2277 // than having just been brought down, then this is wrong, because the 2296 // than having just been brought down, then this is wrong, because the
2278 // server doesn't yet know about this entity; but in this case, 2297 // server doesn't yet know about this entity; but in this case,
2279 // PruneSyncChanges() will prune out the ACTION_DELETE we create here. 2298 // PruneSyncChanges() will prune out the ACTION_DELETE we create here.
2280 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); 2299 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl);
2281 change_list->push_back( 2300 change_list->push_back(
2282 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data)); 2301 syncer::SyncChange(syncer::SyncChange::ACTION_DELETE, sync_data));
2283 Remove(local_turl); 2302 Remove(local_turl);
2284 } else if (local_is_better) { 2303 } else if (local_is_better) {
2285 string16 new_keyword = UniquifyKeyword(*sync_turl); 2304 string16 new_keyword = UniquifyKeyword(*sync_turl, false);
2286 DCHECK(!GetTemplateURLForKeyword(new_keyword)); 2305 DCHECK(!GetTemplateURLForKeyword(new_keyword));
2287 sync_turl->data_.SetKeyword(new_keyword); 2306 sync_turl->data_.SetKeyword(new_keyword);
2288 // If we update the cloud TURL, we need to push an update back to sync 2307 // If we update the cloud TURL, we need to push an update back to sync
2289 // informing it that something has changed. 2308 // informing it that something has changed.
2290 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); 2309 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl);
2291 change_list->push_back( 2310 change_list->push_back(
2292 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data)); 2311 syncer::SyncChange(syncer::SyncChange::ACTION_UPDATE, sync_data));
2293 } else { 2312 } else {
2294 string16 new_keyword = UniquifyKeyword(*local_turl); 2313 string16 new_keyword = UniquifyKeyword(*local_turl, false);
2295 TemplateURLData data(local_turl->data()); 2314 TemplateURLData data(local_turl->data());
2296 data.SetKeyword(new_keyword); 2315 data.SetKeyword(new_keyword);
2297 TemplateURL new_turl(local_turl->profile(), data); 2316 TemplateURL new_turl(local_turl->profile(), data);
2298 UIThreadSearchTermsData search_terms_data(local_turl->profile()); 2317 UIThreadSearchTermsData search_terms_data(local_turl->profile());
2299 if (UpdateNoNotify(local_turl, new_turl, search_terms_data)) 2318 if (UpdateNoNotify(local_turl, new_turl, search_terms_data))
2300 NotifyObservers(); 2319 NotifyObservers();
2301 // Since we're processing sync changes, the UpdateNoNotify() above didn't 2320 // Since we're processing sync changes, the UpdateNoNotify() above didn't
2302 // generate an ACTION_UPDATE. We need to do it manually to keep the server 2321 // generate an ACTION_UPDATE. We need to do it manually to keep the server
2303 // in sync with us. Note that if we're being called from 2322 // in sync with us. Note that if we're being called from
2304 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather 2323 // MergeDataAndStartSyncing(), and this TemplateURL was pre-existing rather
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
2405 // TODO(mpcomplete): If we allow editing extension keywords, then those 2424 // TODO(mpcomplete): If we allow editing extension keywords, then those
2406 // should be persisted to disk and synced. 2425 // should be persisted to disk and synced.
2407 if (template_url->sync_guid().empty() && 2426 if (template_url->sync_guid().empty() &&
2408 !template_url->IsExtensionKeyword()) { 2427 !template_url->IsExtensionKeyword()) {
2409 template_url->data_.sync_guid = base::GenerateGUID(); 2428 template_url->data_.sync_guid = base::GenerateGUID();
2410 if (service_.get()) 2429 if (service_.get())
2411 service_->UpdateKeyword(template_url->data()); 2430 service_->UpdateKeyword(template_url->data());
2412 } 2431 }
2413 } 2432 }
2414 } 2433 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698