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

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

Issue 268643002: Use the DefaultSearchManager as the exclusive authority on DSE, ignoring Web Data. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix the KeywordEditorControllerTest. Created 6 years, 7 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 | Annotate | Revision Log
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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 while (!unchecked_urls.empty()) { 92 while (!unchecked_urls.empty()) {
93 // Find the best URL. 93 // Find the best URL.
94 int prepopulate_id = unchecked_urls.begin()->first; 94 int prepopulate_id = unchecked_urls.begin()->first;
95 PrepopulatedURLMap::const_iterator prepopulated_url = 95 PrepopulatedURLMap::const_iterator prepopulated_url =
96 prepopulated_url_map.find(prepopulate_id); 96 prepopulated_url_map.find(prepopulate_id);
97 UncheckedURLMap::iterator end = unchecked_urls.upper_bound(prepopulate_id); 97 UncheckedURLMap::iterator end = unchecked_urls.upper_bound(prepopulate_id);
98 UncheckedURLMap::iterator best = unchecked_urls.begin(); 98 UncheckedURLMap::iterator best = unchecked_urls.begin();
99 bool matched_keyword = false; 99 bool matched_keyword = false;
100 for (UncheckedURLMap::iterator i = unchecked_urls.begin(); i != end; ++i) { 100 for (UncheckedURLMap::iterator i = unchecked_urls.begin(); i != end; ++i) {
101 // A URL is automatically the best if it's the default search engine. 101 // A URL is automatically the best if it's the default search engine.
102 if (i->second == default_search_provider) { 102 if (default_search_provider &&
103 default_search_provider->sync_guid() == i->second->sync_guid()) {
103 best = i; 104 best = i;
104 break; 105 break;
105 } 106 }
106 107
107 // Otherwise, a URL is best if it matches the prepopulated data's keyword; 108 // Otherwise, a URL is best if it matches the prepopulated data's keyword;
108 // if none match, just fall back to using the one with the lowest ID. 109 // if none match, just fall back to using the one with the lowest ID.
109 if (matched_keyword) 110 if (matched_keyword)
110 continue; 111 continue;
111 if ((prepopulated_url != prepopulated_url_map.end()) && 112 if ((prepopulated_url != prepopulated_url_map.end()) &&
112 i->second->HasSameKeywordAs(*prepopulated_url->second)) { 113 i->second->HasSameKeywordAs(*prepopulated_url->second)) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 // This is invoked when the version of the prepopulate data changes. 185 // This is invoked when the version of the prepopulate data changes.
185 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed 186 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed
186 // from the DB will be added to it. Note that this function will take 187 // from the DB will be added to it. Note that this function will take
187 // ownership of |prepopulated_urls| and will clear the vector. 188 // ownership of |prepopulated_urls| and will clear the vector.
188 void MergeEnginesFromPrepopulateData( 189 void MergeEnginesFromPrepopulateData(
189 Profile* profile, 190 Profile* profile,
190 WebDataService* service, 191 WebDataService* service,
191 ScopedVector<TemplateURLData>* prepopulated_urls, 192 ScopedVector<TemplateURLData>* prepopulated_urls,
192 size_t default_search_index, 193 size_t default_search_index,
193 TemplateURLService::TemplateURLVector* template_urls, 194 TemplateURLService::TemplateURLVector* template_urls,
194 TemplateURL** default_search_provider, 195 TemplateURL* default_search_provider,
195 std::set<std::string>* removed_keyword_guids) { 196 std::set<std::string>* removed_keyword_guids) {
196 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 197 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI));
197 DCHECK(prepopulated_urls); 198 DCHECK(prepopulated_urls);
198 DCHECK(template_urls); 199 DCHECK(template_urls);
199 DCHECK(default_search_provider);
200 200
201 int default_prepopulated_id =
202 (*prepopulated_urls)[default_search_index]->prepopulate_id;
203 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData( 201 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData(
204 prepopulated_urls, *template_urls, *default_search_provider)); 202 prepopulated_urls, *template_urls, default_search_provider));
205 203
206 // Remove items. 204 // Remove items.
207 for (std::vector<TemplateURL*>::iterator i = actions.removed_engines.begin(); 205 for (std::vector<TemplateURL*>::iterator i = actions.removed_engines.begin();
208 i < actions.removed_engines.end(); ++i) { 206 i < actions.removed_engines.end(); ++i) {
209 scoped_ptr<TemplateURL> template_url(*i); 207 scoped_ptr<TemplateURL> template_url(*i);
210 TemplateURLService::TemplateURLVector::iterator j = 208 TemplateURLService::TemplateURLVector::iterator j =
211 std::find(template_urls->begin(), template_urls->end(), template_url); 209 std::find(template_urls->begin(), template_urls->end(), template_url);
212 DCHECK(j != template_urls->end()); 210 DCHECK(j != template_urls->end());
213 DCHECK(*j != *default_search_provider); 211 DCHECK(!default_search_provider ||
212 (*j)->sync_guid() != default_search_provider->sync_guid());
Peter Kasting 2014/05/03 00:28:18 How does sync have anything to do with this? It s
erikwright (departed) 2014/05/05 01:49:38 I will revisit this part of the implementation now
erikwright (departed) 2014/05/05 06:01:57 In the end, if the prepopulate_id matches the DSE,
214 template_urls->erase(j); 213 template_urls->erase(j);
215 if (service) { 214 if (service) {
216 service->RemoveKeyword(template_url->id()); 215 service->RemoveKeyword(template_url->id());
217 if (removed_keyword_guids) 216 if (removed_keyword_guids)
218 removed_keyword_guids->insert(template_url->sync_guid()); 217 removed_keyword_guids->insert(template_url->sync_guid());
219 } 218 }
220 } 219 }
221 220
222 // Edit items. 221 // Edit items.
223 for (EditedEngines::iterator i(actions.edited_engines.begin()); 222 for (EditedEngines::iterator i(actions.edited_engines.begin());
224 i < actions.edited_engines.end(); ++i) { 223 i < actions.edited_engines.end(); ++i) {
225 TemplateURLData& data = i->second; 224 TemplateURLData& data = i->second;
226 scoped_ptr<TemplateURL> existing_url(i->first); 225 scoped_ptr<TemplateURL> existing_url(i->first);
227 if (service) 226 if (service)
228 service->UpdateKeyword(data); 227 service->UpdateKeyword(data);
229 228
230 // Replace the entry in |template_urls| with the updated one. 229 // Replace the entry in |template_urls| with the updated one.
231 TemplateURLService::TemplateURLVector::iterator j = std::find( 230 TemplateURLService::TemplateURLVector::iterator j = std::find(
232 template_urls->begin(), template_urls->end(), existing_url.get()); 231 template_urls->begin(), template_urls->end(), existing_url.get());
233 *j = new TemplateURL(profile, data); 232 *j = new TemplateURL(profile, data);
234 if (*default_search_provider == existing_url.get())
235 *default_search_provider = *j;
236 } 233 }
237 234
238 // Add items. 235 // Add items.
239 for (std::vector<TemplateURLData>::const_iterator it = 236 for (std::vector<TemplateURLData>::const_iterator it =
240 actions.added_engines.begin(); 237 actions.added_engines.begin();
241 it != actions.added_engines.end(); 238 it != actions.added_engines.end();
242 ++it) { 239 ++it) {
243 template_urls->push_back(new TemplateURL(profile, *it)); 240 template_urls->push_back(new TemplateURL(profile, *it));
244 } 241 }
245
246 if (!*default_search_provider) {
247 // The user had no existing default search provider, so set the
248 // default to the default prepopulated engine.
249 *default_search_provider = FindURLByPrepopulateID(*template_urls,
250 default_prepopulated_id);
251 }
252 } 242 }
253 243
254 ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData( 244 ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData(
255 ScopedVector<TemplateURLData>* prepopulated_urls, 245 ScopedVector<TemplateURLData>* prepopulated_urls,
256 const TemplateURLService::TemplateURLVector& existing_urls, 246 const TemplateURLService::TemplateURLVector& existing_urls,
257 const TemplateURL* default_search_provider) { 247 const TemplateURL* default_search_provider) {
258 // Create a map to hold all provided |template_urls| that originally came from 248 // Create a map to hold all provided |template_urls| that originally came from
259 // prepopulate data (i.e. have a non-zero prepopulate_id()). 249 // prepopulate data (i.e. have a non-zero prepopulate_id()).
260 typedef std::map<int, TemplateURL*> IDMap; 250 typedef std::map<int, TemplateURL*> IDMap;
261 IDMap id_to_turl; 251 IDMap id_to_turl;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 // Clear the pointers. 288 // Clear the pointers.
299 prepopulated_urls->weak_erase(prepopulated_urls->begin(), 289 prepopulated_urls->weak_erase(prepopulated_urls->begin(),
300 prepopulated_urls->end()); 290 prepopulated_urls->end());
301 291
302 // The block above removed all the URLs from the |id_to_turl| map that were 292 // The block above removed all the URLs from the |id_to_turl| map that were
303 // found in the prepopulate data. Any remaining URLs that haven't been 293 // found in the prepopulate data. Any remaining URLs that haven't been
304 // user-edited or made default can be removed from the data store. 294 // user-edited or made default can be removed from the data store.
305 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) { 295 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) {
306 TemplateURL* template_url = i->second; 296 TemplateURL* template_url = i->second;
307 if ((template_url->safe_for_autoreplace()) && 297 if ((template_url->safe_for_autoreplace()) &&
308 (template_url != default_search_provider)) 298 (!default_search_provider ||
299 template_url->sync_guid() != default_search_provider->sync_guid()))
Peter Kasting 2014/05/03 00:28:18 Same comment.
erikwright (departed) 2014/05/05 01:49:38 Ditto.
309 actions.removed_engines.push_back(template_url); 300 actions.removed_engines.push_back(template_url);
310 } 301 }
311 302
312 return actions; 303 return actions;
313 } 304 }
314 305
315 void GetSearchProvidersUsingKeywordResult( 306 void GetSearchProvidersUsingKeywordResult(
316 const WDTypedResult& result, 307 const WDTypedResult& result,
317 WebDataService* service, 308 WebDataService* service,
318 Profile* profile, 309 Profile* profile,
319 TemplateURLService::TemplateURLVector* template_urls, 310 TemplateURLService::TemplateURLVector* template_urls,
320 TemplateURL** default_search_provider, 311 TemplateURL* default_search_provider,
321 int* new_resource_keyword_version, 312 int* new_resource_keyword_version,
322 std::set<std::string>* removed_keyword_guids) { 313 std::set<std::string>* removed_keyword_guids) {
323 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 314 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI));
324 DCHECK(template_urls); 315 DCHECK(template_urls);
325 DCHECK(template_urls->empty()); 316 DCHECK(template_urls->empty());
326 DCHECK(default_search_provider);
327 DCHECK(*default_search_provider == NULL);
328 DCHECK_EQ(KEYWORDS_RESULT, result.GetType()); 317 DCHECK_EQ(KEYWORDS_RESULT, result.GetType());
329 DCHECK(new_resource_keyword_version); 318 DCHECK(new_resource_keyword_version);
330 319
331 WDKeywordsResult keyword_result = reinterpret_cast< 320 WDKeywordsResult keyword_result = reinterpret_cast<
332 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); 321 const WDResult<WDKeywordsResult>*>(&result)->GetValue();
333 322
334 for (KeywordTable::Keywords::iterator i(keyword_result.keywords.begin()); 323 for (KeywordTable::Keywords::iterator i(keyword_result.keywords.begin());
335 i != keyword_result.keywords.end(); ++i) { 324 i != keyword_result.keywords.end(); ++i) {
336 // Fix any duplicate encodings in the local database. Note that we don't 325 // Fix any duplicate encodings in the local database. Note that we don't
337 // adjust the last_modified time of this keyword; this way, we won't later 326 // adjust the last_modified time of this keyword; this way, we won't later
338 // overwrite any changes on the sync server that happened to this keyword 327 // overwrite any changes on the sync server that happened to this keyword
339 // since the last time we synced. Instead, we also run a de-duping pass on 328 // since the last time we synced. Instead, we also run a de-duping pass on
340 // the server-provided data in 329 // the server-provided data in
341 // TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData() and 330 // TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData() and
342 // update the server with the merged, de-duped results at that time. We 331 // update the server with the merged, de-duped results at that time. We
343 // still fix here, though, to correct problems in clients that have disabled 332 // still fix here, though, to correct problems in clients that have disabled
344 // search engine sync, since in that case that code will never be reached. 333 // search engine sync, since in that case that code will never be reached.
345 if (DeDupeEncodings(&i->input_encodings) && service) 334 if (DeDupeEncodings(&i->input_encodings) && service)
346 service->UpdateKeyword(*i); 335 service->UpdateKeyword(*i);
347 template_urls->push_back(new TemplateURL(profile, *i)); 336 template_urls->push_back(new TemplateURL(profile, *i));
348 } 337 }
349 338
350 int64 default_search_provider_id = keyword_result.default_search_provider_id;
351 if (default_search_provider_id) {
352 *default_search_provider =
353 GetTemplateURLByID(*template_urls, default_search_provider_id);
354 }
355
356 *new_resource_keyword_version = keyword_result.builtin_keyword_version; 339 *new_resource_keyword_version = keyword_result.builtin_keyword_version;
357 GetSearchProvidersUsingLoadedEngines(service, profile, template_urls, 340 GetSearchProvidersUsingLoadedEngines(service, profile, template_urls,
358 default_search_provider, 341 default_search_provider,
359 new_resource_keyword_version, 342 new_resource_keyword_version,
360 removed_keyword_guids); 343 removed_keyword_guids);
361 } 344 }
362 345
363 void GetSearchProvidersUsingLoadedEngines( 346 void GetSearchProvidersUsingLoadedEngines(
364 WebDataService* service, 347 WebDataService* service,
365 Profile* profile, 348 Profile* profile,
366 TemplateURLService::TemplateURLVector* template_urls, 349 TemplateURLService::TemplateURLVector* template_urls,
367 TemplateURL** default_search_provider, 350 TemplateURL* default_search_provider,
368 int* resource_keyword_version, 351 int* resource_keyword_version,
369 std::set<std::string>* removed_keyword_guids) { 352 std::set<std::string>* removed_keyword_guids) {
370 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 353 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI));
371 DCHECK(template_urls); 354 DCHECK(template_urls);
372 DCHECK(default_search_provider);
373 DCHECK(resource_keyword_version); 355 DCHECK(resource_keyword_version);
374 PrefService* prefs = profile ? profile->GetPrefs() : NULL; 356 PrefService* prefs = profile ? profile->GetPrefs() : NULL;
375 size_t default_search_index; 357 size_t default_search_index;
376 ScopedVector<TemplateURLData> prepopulated_urls = 358 ScopedVector<TemplateURLData> prepopulated_urls =
377 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, 359 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs,
378 &default_search_index); 360 &default_search_index);
379 RemoveDuplicatePrepopulateIDs(service, prepopulated_urls, 361 RemoveDuplicatePrepopulateIDs(service, prepopulated_urls,
380 *default_search_provider, template_urls, 362 default_search_provider, template_urls,
381 removed_keyword_guids); 363 removed_keyword_guids);
382 364
383 const int prepopulate_resource_keyword_version = 365 const int prepopulate_resource_keyword_version =
384 TemplateURLPrepopulateData::GetDataVersion(prefs); 366 TemplateURLPrepopulateData::GetDataVersion(prefs);
385 if (*resource_keyword_version < prepopulate_resource_keyword_version) { 367 if (*resource_keyword_version < prepopulate_resource_keyword_version) {
386 MergeEnginesFromPrepopulateData(profile, service, &prepopulated_urls, 368 MergeEnginesFromPrepopulateData(profile, service, &prepopulated_urls,
387 default_search_index, template_urls, default_search_provider, 369 default_search_index, template_urls, default_search_provider,
388 removed_keyword_guids); 370 removed_keyword_guids);
389 *resource_keyword_version = prepopulate_resource_keyword_version; 371 *resource_keyword_version = prepopulate_resource_keyword_version;
390 } else { 372 } else {
391 *resource_keyword_version = 0; 373 *resource_keyword_version = 0;
392 } 374 }
393 } 375 }
394 376
395 bool DeDupeEncodings(std::vector<std::string>* encodings) { 377 bool DeDupeEncodings(std::vector<std::string>* encodings) {
396 std::vector<std::string> deduped_encodings; 378 std::vector<std::string> deduped_encodings;
397 std::set<std::string> encoding_set; 379 std::set<std::string> encoding_set;
398 for (std::vector<std::string>::const_iterator i(encodings->begin()); 380 for (std::vector<std::string>::const_iterator i(encodings->begin());
399 i != encodings->end(); ++i) { 381 i != encodings->end(); ++i) {
400 if (encoding_set.insert(*i).second) 382 if (encoding_set.insert(*i).second)
401 deduped_encodings.push_back(*i); 383 deduped_encodings.push_back(*i);
402 } 384 }
403 encodings->swap(deduped_encodings); 385 encodings->swap(deduped_encodings);
404 return encodings->size() != deduped_encodings.size(); 386 return encodings->size() != deduped_encodings.size();
405 } 387 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698