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

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

Issue 15572002: Implemented 'Reset Search engines' feature. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Merge with trunk + Peter's comments Created 7 years, 6 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
« no previous file with comments | « chrome/browser/search_engines/util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/util.h" 5 #include "chrome/browser/search_engines/util.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 prepopulated_url->safe_for_autoreplace = false; 142 prepopulated_url->safe_for_autoreplace = false;
143 prepopulated_url->SetKeyword(original_turl->keyword()); 143 prepopulated_url->SetKeyword(original_turl->keyword());
144 prepopulated_url->short_name = original_turl->short_name(); 144 prepopulated_url->short_name = original_turl->short_name();
145 } 145 }
146 prepopulated_url->id = original_turl->id(); 146 prepopulated_url->id = original_turl->id();
147 prepopulated_url->sync_guid = original_turl->sync_guid(); 147 prepopulated_url->sync_guid = original_turl->sync_guid();
148 prepopulated_url->date_created = original_turl->date_created(); 148 prepopulated_url->date_created = original_turl->date_created();
149 prepopulated_url->last_modified = original_turl->last_modified(); 149 prepopulated_url->last_modified = original_turl->last_modified();
150 } 150 }
151 151
152 // Loads engines from prepopulate data and merges them in with the existing 152 // Merges the provided prepopulated engines with the provided existing engines.
153 // engines. This is invoked when the version of the prepopulate data changes. 153 // This is invoked when the version of the prepopulate data changes.
154 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed 154 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed
155 // from the DB will be added to it. Note that this function will take 155 // from the DB will be added to it. Note that this function will take
156 // ownership of |prepopulated_urls| and will clear the vector. 156 // ownership of |prepopulated_urls| and will clear the vector.
157 void MergeEnginesFromPrepopulateData( 157 void MergeEnginesFromPrepopulateData(
158 Profile* profile, 158 Profile* profile,
159 WebDataService* service, 159 WebDataService* service,
160 ScopedVector<TemplateURL>* prepopulated_urls, 160 ScopedVector<TemplateURL>* prepopulated_urls,
161 size_t default_search_index, 161 size_t default_search_index,
162 TemplateURLService::TemplateURLVector* template_urls, 162 TemplateURLService::TemplateURLVector* template_urls,
163 TemplateURL** default_search_provider, 163 TemplateURL** default_search_provider,
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 int* new_resource_keyword_version, 255 int* new_resource_keyword_version,
256 std::set<std::string>* removed_keyword_guids) { 256 std::set<std::string>* removed_keyword_guids) {
257 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 257 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI));
258 DCHECK(template_urls); 258 DCHECK(template_urls);
259 DCHECK(template_urls->empty()); 259 DCHECK(template_urls->empty());
260 DCHECK(default_search_provider); 260 DCHECK(default_search_provider);
261 DCHECK(*default_search_provider == NULL); 261 DCHECK(*default_search_provider == NULL);
262 DCHECK_EQ(KEYWORDS_RESULT, result.GetType()); 262 DCHECK_EQ(KEYWORDS_RESULT, result.GetType());
263 DCHECK(new_resource_keyword_version); 263 DCHECK(new_resource_keyword_version);
264 264
265 *new_resource_keyword_version = 0;
266 WDKeywordsResult keyword_result = reinterpret_cast< 265 WDKeywordsResult keyword_result = reinterpret_cast<
267 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); 266 const WDResult<WDKeywordsResult>*>(&result)->GetValue();
268 267
269 for (KeywordTable::Keywords::iterator i(keyword_result.keywords.begin()); 268 for (KeywordTable::Keywords::iterator i(keyword_result.keywords.begin());
270 i != keyword_result.keywords.end(); ++i) { 269 i != keyword_result.keywords.end(); ++i) {
271 // Fix any duplicate encodings in the local database. Note that we don't 270 // Fix any duplicate encodings in the local database. Note that we don't
272 // adjust the last_modified time of this keyword; this way, we won't later 271 // adjust the last_modified time of this keyword; this way, we won't later
273 // overwrite any changes on the sync server that happened to this keyword 272 // overwrite any changes on the sync server that happened to this keyword
274 // since the last time we synced. Instead, we also run a de-duping pass on 273 // since the last time we synced. Instead, we also run a de-duping pass on
275 // the server-provided data in 274 // the server-provided data in
276 // TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData() and 275 // TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData() and
277 // update the server with the merged, de-duped results at that time. We 276 // update the server with the merged, de-duped results at that time. We
278 // still fix here, though, to correct problems in clients that have disabled 277 // still fix here, though, to correct problems in clients that have disabled
279 // search engine sync, since in that case that code will never be reached. 278 // search engine sync, since in that case that code will never be reached.
280 if (DeDupeEncodings(&i->input_encodings) && service) 279 if (DeDupeEncodings(&i->input_encodings) && service)
281 service->UpdateKeyword(*i); 280 service->UpdateKeyword(*i);
282 template_urls->push_back(new TemplateURL(profile, *i)); 281 template_urls->push_back(new TemplateURL(profile, *i));
283 } 282 }
284 283
285 int64 default_search_provider_id = keyword_result.default_search_provider_id; 284 int64 default_search_provider_id = keyword_result.default_search_provider_id;
286 if (default_search_provider_id) { 285 if (default_search_provider_id) {
287 *default_search_provider = 286 *default_search_provider =
288 GetTemplateURLByID(*template_urls, default_search_provider_id); 287 GetTemplateURLByID(*template_urls, default_search_provider_id);
289 } 288 }
290 289
290 *new_resource_keyword_version = keyword_result.builtin_keyword_version;
291 GetSearchProvidersUsingLoadedEngines(service, profile, template_urls,
292 default_search_provider,
293 new_resource_keyword_version,
294 removed_keyword_guids);
295 }
296
297 void GetSearchProvidersUsingLoadedEngines(
298 WebDataService* service,
299 Profile* profile,
300 TemplateURLService::TemplateURLVector* template_urls,
301 TemplateURL** default_search_provider,
302 int* resource_keyword_version,
303 std::set<std::string>* removed_keyword_guids) {
304 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI));
305 DCHECK(template_urls);
306 DCHECK(default_search_provider);
307 DCHECK(resource_keyword_version);
308
291 ScopedVector<TemplateURL> prepopulated_urls; 309 ScopedVector<TemplateURL> prepopulated_urls;
292 size_t default_search_index; 310 size_t default_search_index;
293 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile, 311 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile,
294 &prepopulated_urls.get(), &default_search_index); 312 &prepopulated_urls.get(), &default_search_index);
295 RemoveDuplicatePrepopulateIDs(service, prepopulated_urls, 313 RemoveDuplicatePrepopulateIDs(service, prepopulated_urls,
296 *default_search_provider, template_urls, 314 *default_search_provider, template_urls,
297 removed_keyword_guids); 315 removed_keyword_guids);
298 316
299 const int resource_keyword_version = 317 const int prepopulate_resource_keyword_version =
300 TemplateURLPrepopulateData::GetDataVersion( 318 TemplateURLPrepopulateData::GetDataVersion(
301 profile ? profile->GetPrefs() : NULL); 319 profile ? profile->GetPrefs() : NULL);
302 if (keyword_result.builtin_keyword_version != resource_keyword_version) { 320 if (*resource_keyword_version < prepopulate_resource_keyword_version) {
303 MergeEnginesFromPrepopulateData(profile, service, &prepopulated_urls, 321 MergeEnginesFromPrepopulateData(profile, service, &prepopulated_urls,
304 default_search_index, template_urls, default_search_provider, 322 default_search_index, template_urls, default_search_provider,
305 removed_keyword_guids); 323 removed_keyword_guids);
306 *new_resource_keyword_version = resource_keyword_version; 324 *resource_keyword_version = prepopulate_resource_keyword_version;
325 } else {
326 *resource_keyword_version = 0;
307 } 327 }
308 } 328 }
309 329
310 bool DeDupeEncodings(std::vector<std::string>* encodings) { 330 bool DeDupeEncodings(std::vector<std::string>* encodings) {
311 std::vector<std::string> deduped_encodings; 331 std::vector<std::string> deduped_encodings;
312 std::set<std::string> encoding_set; 332 std::set<std::string> encoding_set;
313 for (std::vector<std::string>::const_iterator i(encodings->begin()); 333 for (std::vector<std::string>::const_iterator i(encodings->begin());
314 i != encodings->end(); ++i) { 334 i != encodings->end(); ++i) {
315 if (encoding_set.insert(*i).second) 335 if (encoding_set.insert(*i).second)
316 deduped_encodings.push_back(*i); 336 deduped_encodings.push_back(*i);
317 } 337 }
318 encodings->swap(deduped_encodings); 338 encodings->swap(deduped_encodings);
319 return encodings->size() != deduped_encodings.size(); 339 return encodings->size() != deduped_encodings.size();
320 } 340 }
OLDNEW
« no previous file with comments | « chrome/browser/search_engines/util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698