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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/search_engines/util.h" 6 7 #include <map> 8 #include <set> 9 #include <string> 10 #include <vector> 11 12 #include "base/logging.h" 13 #include "base/memory/scoped_vector.h" 14 #include "base/prefs/pref_service.h" 15 #include "base/time/time.h" 16 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/search_engines/template_url.h" 18 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" 19 #include "chrome/browser/search_engines/template_url_service.h" 20 #include "chrome/browser/search_engines/template_url_service_factory.h" 21 #include "content/public/browser/browser_thread.h" 22 23 using content::BrowserThread; 24 25 base::string16 GetDefaultSearchEngineName(Profile* profile) { 26 if (!profile) { 27 NOTREACHED(); 28 return base::string16(); 29 } 30 const TemplateURL* const default_provider = 31 TemplateURLServiceFactory::GetForProfile(profile)-> 32 GetDefaultSearchProvider(); 33 if (!default_provider) { 34 // TODO(cpu): bug 1187517. It is possible to have no default provider. 35 // returning an empty string is a stopgap measure for the crash 36 // http://code.google.com/p/chromium/issues/detail?id=2573 37 return base::string16(); 38 } 39 return default_provider->short_name(); 40 } 41 42 GURL GetDefaultSearchURLForSearchTerms(Profile* profile, 43 const base::string16& terms) { 44 DCHECK(profile); 45 const TemplateURL* default_provider = 46 TemplateURLServiceFactory::GetForProfile(profile)-> 47 GetDefaultSearchProvider(); 48 if (!default_provider) 49 return GURL(); 50 const TemplateURLRef& search_url = default_provider->url_ref(); 51 DCHECK(search_url.SupportsReplacement()); 52 TemplateURLRef::SearchTermsArgs search_terms_args(terms); 53 search_terms_args.append_extra_query_params = true; 54 return GURL(search_url.ReplaceSearchTerms(search_terms_args)); 55 } 56 57 void RemoveDuplicatePrepopulateIDs( 58 WebDataService* service, 59 const ScopedVector<TemplateURL>& prepopulated_urls, 60 TemplateURL* default_search_provider, 61 TemplateURLService::TemplateURLVector* template_urls, 62 std::set<std::string>* removed_keyword_guids) { 63 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 64 DCHECK(template_urls); 65 66 // For convenience construct an ID->TemplateURL* map from |prepopulated_urls|. 67 typedef std::map<int, TemplateURL*> PrepopulatedURLMap; 68 PrepopulatedURLMap prepopulated_url_map; 69 for (std::vector<TemplateURL*>::const_iterator i(prepopulated_urls.begin()); 70 i != prepopulated_urls.end(); ++i) 71 prepopulated_url_map[(*i)->prepopulate_id()] = *i; 72 73 // Separate |template_urls| into prepopulated and non-prepopulated groups. 74 typedef std::multimap<int, TemplateURL*> UncheckedURLMap; 75 UncheckedURLMap unchecked_urls; 76 TemplateURLService::TemplateURLVector checked_urls; 77 for (TemplateURLService::TemplateURLVector::iterator i( 78 template_urls->begin()); i != template_urls->end(); ++i) { 79 TemplateURL* turl = *i; 80 int prepopulate_id = turl->prepopulate_id(); 81 if (prepopulate_id) 82 unchecked_urls.insert(std::make_pair(prepopulate_id, turl)); 83 else 84 checked_urls.push_back(turl); 85 } 86 87 // For each group of prepopulated URLs with one ID, find the best URL to use 88 // and add it to the (initially all non-prepopulated) URLs we've already OKed. 89 // Delete the others from the service and from memory. 90 while (!unchecked_urls.empty()) { 91 // Find the best URL. 92 int prepopulate_id = unchecked_urls.begin()->first; 93 PrepopulatedURLMap::const_iterator prepopulated_url = 94 prepopulated_url_map.find(prepopulate_id); 95 UncheckedURLMap::iterator end = unchecked_urls.upper_bound(prepopulate_id); 96 UncheckedURLMap::iterator best = unchecked_urls.begin(); 97 bool matched_keyword = false; 98 for (UncheckedURLMap::iterator i = unchecked_urls.begin(); i != end; ++i) { 99 // A URL is automatically the best if it's the default search engine. 100 if (i->second == default_search_provider) { 101 best = i; 102 break; 103 } 104 105 // Otherwise, a URL is best if it matches the prepopulated data's keyword; 106 // if none match, just fall back to using the one with the lowest ID. 107 if (matched_keyword) 108 continue; 109 if ((prepopulated_url != prepopulated_url_map.end()) && 110 i->second->HasSameKeywordAs(*prepopulated_url->second)) { 111 best = i; 112 matched_keyword = true; 113 } else if (i->second->id() < best->second->id()) { 114 best = i; 115 } 116 } 117 118 // Add the best URL to the checked group and delete the rest. 119 checked_urls.push_back(best->second); 120 for (UncheckedURLMap::iterator i = unchecked_urls.begin(); i != end; ++i) { 121 if (i == best) 122 continue; 123 if (service) { 124 service->RemoveKeyword(i->second->id()); 125 if (removed_keyword_guids) 126 removed_keyword_guids->insert(i->second->sync_guid()); 127 } 128 delete i->second; 129 } 130 131 // Done with this group. 132 unchecked_urls.erase(unchecked_urls.begin(), end); 133 } 134 135 // Return the checked URLs. 136 template_urls->swap(checked_urls); 137 } 138 139 // Returns the TemplateURL with id specified from the list of TemplateURLs. 140 // If not found, returns NULL. 141 TemplateURL* GetTemplateURLByID( 142 const TemplateURLService::TemplateURLVector& template_urls, 143 int64 id) { 144 for (TemplateURLService::TemplateURLVector::const_iterator i( 145 template_urls.begin()); i != template_urls.end(); ++i) { 146 if ((*i)->id() == id) { 147 return *i; 148 } 149 } 150 return NULL; 151 } 152 153 TemplateURL* FindURLByPrepopulateID( 154 const TemplateURLService::TemplateURLVector& template_urls, 155 int prepopulate_id) { 156 for (std::vector<TemplateURL*>::const_iterator i = template_urls.begin(); 157 i < template_urls.end(); ++i) { 158 if ((*i)->prepopulate_id() == prepopulate_id) 159 return *i; 160 } 161 return NULL; 162 } 163 164 void MergeIntoPrepopulatedEngineData(TemplateURLData* prepopulated_url, 165 const TemplateURL* original_turl) { 166 DCHECK_EQ(original_turl->prepopulate_id(), prepopulated_url->prepopulate_id); 167 if (!original_turl->safe_for_autoreplace()) { 168 prepopulated_url->safe_for_autoreplace = false; 169 prepopulated_url->SetKeyword(original_turl->keyword()); 170 prepopulated_url->short_name = original_turl->short_name(); 171 } 172 prepopulated_url->id = original_turl->id(); 173 prepopulated_url->sync_guid = original_turl->sync_guid(); 174 prepopulated_url->date_created = original_turl->date_created(); 175 prepopulated_url->last_modified = original_turl->last_modified(); 176 } 177 178 ActionsFromPrepopulateData::ActionsFromPrepopulateData() {} 179 180 ActionsFromPrepopulateData::~ActionsFromPrepopulateData() {} 181 182 // This is invoked when the version of the prepopulate data changes. 183 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed 184 // from the DB will be added to it. Note that this function will take 185 // ownership of |prepopulated_urls| and will clear the vector. 186 void MergeEnginesFromPrepopulateData( 187 Profile* profile, 188 WebDataService* service, 189 ScopedVector<TemplateURL>* prepopulated_urls, 190 size_t default_search_index, 191 TemplateURLService::TemplateURLVector* template_urls, 192 TemplateURL** default_search_provider, 193 std::set<std::string>* removed_keyword_guids) { 194 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 195 DCHECK(prepopulated_urls); 196 DCHECK(template_urls); 197 DCHECK(default_search_provider); 198 199 int default_prepopulated_id = 200 (*prepopulated_urls)[default_search_index]->prepopulate_id(); 201 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData( 202 prepopulated_urls, *template_urls, *default_search_provider)); 203 204 // Remove items. 205 for (std::vector<TemplateURL*>::iterator i = actions.removed_engines.begin(); 206 i < actions.removed_engines.end(); ++i) { 207 scoped_ptr<TemplateURL> template_url(*i); 208 TemplateURLService::TemplateURLVector::iterator j = 209 std::find(template_urls->begin(), template_urls->end(), template_url); 210 DCHECK(j != template_urls->end()); 211 DCHECK(*j != *default_search_provider); 212 template_urls->erase(j); 213 if (service) { 214 service->RemoveKeyword(template_url->id()); 215 if (removed_keyword_guids) 216 removed_keyword_guids->insert(template_url->sync_guid()); 217 } 218 } 219 220 // Edit items. 221 for (EditedEngines::iterator i(actions.edited_engines.begin()); 222 i < actions.edited_engines.end(); ++i) { 223 TemplateURLData& data = i->second; 224 scoped_ptr<TemplateURL> existing_url(i->first); 225 if (service) 226 service->UpdateKeyword(data); 227 228 // Replace the entry in |template_urls| with the updated one. 229 TemplateURLService::TemplateURLVector::iterator j = std::find( 230 template_urls->begin(), template_urls->end(), existing_url.get()); 231 *j = new TemplateURL(profile, data); 232 if (*default_search_provider == existing_url.get()) 233 *default_search_provider = *j; 234 } 235 236 // Add items. 237 template_urls->insert(template_urls->end(), actions.added_engines.begin(), 238 actions.added_engines.end()); 239 240 if (!*default_search_provider) { 241 // The user had no existing default search provider, so set the 242 // default to the default prepopulated engine. 243 *default_search_provider = FindURLByPrepopulateID(*template_urls, 244 default_prepopulated_id); 245 } 246 } 247 248 ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData( 249 ScopedVector<TemplateURL>* prepopulated_urls, 250 const TemplateURLService::TemplateURLVector& existing_urls, 251 const TemplateURL* default_search_provider) { 252 // Create a map to hold all provided |template_urls| that originally came from 253 // prepopulate data (i.e. have a non-zero prepopulate_id()). 254 typedef std::map<int, TemplateURL*> IDMap; 255 IDMap id_to_turl; 256 for (TemplateURLService::TemplateURLVector::const_iterator i( 257 existing_urls.begin()); i != existing_urls.end(); ++i) { 258 int prepopulate_id = (*i)->prepopulate_id(); 259 if (prepopulate_id > 0) 260 id_to_turl[prepopulate_id] = *i; 261 } 262 263 // For each current prepopulated URL, check whether |template_urls| contained 264 // a matching prepopulated URL. If so, update the passed-in URL to match the 265 // current data. (If the passed-in URL was user-edited, we persist the user's 266 // name and keyword.) If not, add the prepopulated URL. 267 ActionsFromPrepopulateData actions; 268 for (size_t i = 0; i < prepopulated_urls->size(); ++i) { 269 // We take ownership of |prepopulated_urls[i]|. 270 scoped_ptr<TemplateURL> prepopulated_url((*prepopulated_urls)[i]); 271 const int prepopulated_id = prepopulated_url->prepopulate_id(); 272 DCHECK_NE(0, prepopulated_id); 273 274 IDMap::iterator existing_url_iter(id_to_turl.find(prepopulated_id)); 275 if (existing_url_iter != id_to_turl.end()) { 276 // Update the data store with the new prepopulated data. Preserve user 277 // edits to the name and keyword. 278 TemplateURLData data(prepopulated_url->data()); 279 TemplateURL* existing_url(existing_url_iter->second); 280 id_to_turl.erase(existing_url_iter); 281 MergeIntoPrepopulatedEngineData(&data, existing_url); 282 // Update last_modified to ensure that if this entry is later merged with 283 // entries from Sync, the conflict resolution logic knows that this was 284 // updated and propagates the new values to the server. 285 data.last_modified = base::Time::Now(); 286 actions.edited_engines.push_back(std::make_pair(existing_url, data)); 287 } else { 288 actions.added_engines.push_back(prepopulated_url.release()); 289 } 290 } 291 // The above loop takes ownership of all the contents of prepopulated_urls. 292 // Clear the pointers. 293 prepopulated_urls->weak_erase(prepopulated_urls->begin(), 294 prepopulated_urls->end()); 295 296 // The block above removed all the URLs from the |id_to_turl| map that were 297 // found in the prepopulate data. Any remaining URLs that haven't been 298 // user-edited or made default can be removed from the data store. 299 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) { 300 TemplateURL* template_url = i->second; 301 if ((template_url->safe_for_autoreplace()) && 302 (template_url != default_search_provider)) 303 actions.removed_engines.push_back(template_url); 304 } 305 306 return actions; 307 } 308 309 void GetSearchProvidersUsingKeywordResult( 310 const WDTypedResult& result, 311 WebDataService* service, 312 Profile* profile, 313 TemplateURLService::TemplateURLVector* template_urls, 314 TemplateURL** default_search_provider, 315 int* new_resource_keyword_version, 316 std::set<std::string>* removed_keyword_guids) { 317 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 318 DCHECK(template_urls); 319 DCHECK(template_urls->empty()); 320 DCHECK(default_search_provider); 321 DCHECK(*default_search_provider == NULL); 322 DCHECK_EQ(KEYWORDS_RESULT, result.GetType()); 323 DCHECK(new_resource_keyword_version); 324 325 WDKeywordsResult keyword_result = reinterpret_cast< 326 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); 327 328 for (KeywordTable::Keywords::iterator i(keyword_result.keywords.begin()); 329 i != keyword_result.keywords.end(); ++i) { 330 // Fix any duplicate encodings in the local database. Note that we don't 331 // adjust the last_modified time of this keyword; this way, we won't later 332 // overwrite any changes on the sync server that happened to this keyword 333 // since the last time we synced. Instead, we also run a de-duping pass on 334 // the server-provided data in 335 // TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData() and 336 // update the server with the merged, de-duped results at that time. We 337 // still fix here, though, to correct problems in clients that have disabled 338 // search engine sync, since in that case that code will never be reached. 339 if (DeDupeEncodings(&i->input_encodings) && service) 340 service->UpdateKeyword(*i); 341 template_urls->push_back(new TemplateURL(profile, *i)); 342 } 343 344 int64 default_search_provider_id = keyword_result.default_search_provider_id; 345 if (default_search_provider_id) { 346 *default_search_provider = 347 GetTemplateURLByID(*template_urls, default_search_provider_id); 348 } 349 350 *new_resource_keyword_version = keyword_result.builtin_keyword_version; 351 GetSearchProvidersUsingLoadedEngines(service, profile, template_urls, 352 default_search_provider, 353 new_resource_keyword_version, 354 removed_keyword_guids); 355 } 356 357 void GetSearchProvidersUsingLoadedEngines( 358 WebDataService* service, 359 Profile* profile, 360 TemplateURLService::TemplateURLVector* template_urls, 361 TemplateURL** default_search_provider, 362 int* resource_keyword_version, 363 std::set<std::string>* removed_keyword_guids) { 364 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 365 DCHECK(template_urls); 366 DCHECK(default_search_provider); 367 DCHECK(resource_keyword_version); 368 369 size_t default_search_index; 370 ScopedVector<TemplateURL> prepopulated_urls = 371 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile, 372 &default_search_index); 373 RemoveDuplicatePrepopulateIDs(service, prepopulated_urls, 374 *default_search_provider, template_urls, 375 removed_keyword_guids); 376 377 const int prepopulate_resource_keyword_version = 378 TemplateURLPrepopulateData::GetDataVersion( 379 profile ? profile->GetPrefs() : NULL); 380 if (*resource_keyword_version < prepopulate_resource_keyword_version) { 381 MergeEnginesFromPrepopulateData(profile, service, &prepopulated_urls, 382 default_search_index, template_urls, default_search_provider, 383 removed_keyword_guids); 384 *resource_keyword_version = prepopulate_resource_keyword_version; 385 } else { 386 *resource_keyword_version = 0; 387 } 388 } 389 390 bool DeDupeEncodings(std::vector<std::string>* encodings) { 391 std::vector<std::string> deduped_encodings; 392 std::set<std::string> encoding_set; 393 for (std::vector<std::string>::const_iterator i(encodings->begin()); 394 i != encodings->end(); ++i) { 395 if (encoding_set.insert(*i).second) 396 deduped_encodings.push_back(*i); 397 } 398 encodings->swap(deduped_encodings); 399 return encodings->size() != deduped_encodings.size(); 400 } 401