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 string16 GetDefaultSearchEngineName(Profile* profile) { 26 if (!profile) { 27 NOTREACHED(); 28 return 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 string16(); 38 } 39 return default_provider->short_name(); 40 } 41 42 GURL GetDefaultSearchURLForSearchTerms(Profile* profile, 43 const 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 void MergeIntoPrepopulatedEngineData(TemplateURLData* prepopulated_url, 154 const TemplateURL* original_turl) { 155 DCHECK_EQ(original_turl->prepopulate_id(), prepopulated_url->prepopulate_id); 156 if (!original_turl->safe_for_autoreplace()) { 157 prepopulated_url->safe_for_autoreplace = false; 158 prepopulated_url->SetKeyword(original_turl->keyword()); 159 prepopulated_url->short_name = original_turl->short_name(); 160 } 161 prepopulated_url->id = original_turl->id(); 162 prepopulated_url->sync_guid = original_turl->sync_guid(); 163 prepopulated_url->date_created = original_turl->date_created(); 164 prepopulated_url->last_modified = original_turl->last_modified(); 165 } 166 167 // Merges the provided prepopulated engines with the provided existing engines. 168 // This is invoked when the version of the prepopulate data changes. 169 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed 170 // from the DB will be added to it. Note that this function will take 171 // ownership of |prepopulated_urls| and will clear the vector. 172 void MergeEnginesFromPrepopulateData( 173 Profile* profile, 174 WebDataService* service, 175 ScopedVector<TemplateURL>* prepopulated_urls, 176 size_t default_search_index, 177 TemplateURLService::TemplateURLVector* template_urls, 178 TemplateURL** default_search_provider, 179 std::set<std::string>* removed_keyword_guids) { 180 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 181 DCHECK(template_urls); 182 DCHECK(default_search_provider); 183 184 // Create a map to hold all provided |template_urls| that originally came from 185 // prepopulate data (i.e. have a non-zero prepopulate_id()). 186 typedef std::map<int, TemplateURL*> IDMap; 187 IDMap id_to_turl; 188 for (TemplateURLService::TemplateURLVector::iterator i( 189 template_urls->begin()); i != template_urls->end(); ++i) { 190 int prepopulate_id = (*i)->prepopulate_id(); 191 if (prepopulate_id > 0) 192 id_to_turl[prepopulate_id] = *i; 193 } 194 195 // For each current prepopulated URL, check whether |template_urls| contained 196 // a matching prepopulated URL. If so, update the passed-in URL to match the 197 // current data. (If the passed-in URL was user-edited, we persist the user's 198 // name and keyword.) If not, add the prepopulated URL to |template_urls|. 199 // Along the way, point |default_search_provider| at the default prepopulated 200 // URL, if the user hasn't already set another URL as default. 201 for (size_t i = 0; i < prepopulated_urls->size(); ++i) { 202 // We take ownership of |prepopulated_urls[i]|. 203 scoped_ptr<TemplateURL> prepopulated_url((*prepopulated_urls)[i]); 204 const int prepopulated_id = prepopulated_url->prepopulate_id(); 205 DCHECK_NE(0, prepopulated_id); 206 207 TemplateURL* url_in_vector = NULL; 208 IDMap::iterator existing_url_iter(id_to_turl.find(prepopulated_id)); 209 if (existing_url_iter != id_to_turl.end()) { 210 // Update the data store with the new prepopulated data. Preserve user 211 // edits to the name and keyword. 212 TemplateURLData data(prepopulated_url->data()); 213 scoped_ptr<TemplateURL> existing_url(existing_url_iter->second); 214 id_to_turl.erase(existing_url_iter); 215 MergeIntoPrepopulatedEngineData(&data, existing_url.get()); 216 // Update last_modified to ensure that if this entry is later merged with 217 // entries from Sync, the conflict resolution logic knows that this was 218 // updated and propagates the new values to the server. 219 data.last_modified = base::Time::Now(); 220 if (service) 221 service->UpdateKeyword(data); 222 223 // Replace the entry in |template_urls| with the updated one. 224 TemplateURLService::TemplateURLVector::iterator j = std::find( 225 template_urls->begin(), template_urls->end(), existing_url.get()); 226 *j = new TemplateURL(profile, data); 227 url_in_vector = *j; 228 if (*default_search_provider == existing_url.get()) 229 *default_search_provider = url_in_vector; 230 } else { 231 template_urls->push_back(prepopulated_url.release()); 232 url_in_vector = template_urls->back(); 233 } 234 DCHECK(url_in_vector); 235 if (i == default_search_index && !*default_search_provider) 236 *default_search_provider = url_in_vector; 237 } 238 // The above loop takes ownership of all the contents of prepopulated_urls. 239 // Clear the pointers. 240 prepopulated_urls->weak_erase(prepopulated_urls->begin(), 241 prepopulated_urls->end()); 242 243 // The block above removed all the URLs from the |id_to_turl| map that were 244 // found in the prepopulate data. Any remaining URLs that haven't been 245 // user-edited or made default can be removed from the data store. 246 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) { 247 const TemplateURL* template_url = i->second; 248 if ((template_url->safe_for_autoreplace()) && 249 (template_url != *default_search_provider)) { 250 TemplateURLService::TemplateURLVector::iterator j = 251 std::find(template_urls->begin(), template_urls->end(), template_url); 252 DCHECK(j != template_urls->end()); 253 template_urls->erase(j); 254 if (service) { 255 service->RemoveKeyword(template_url->id()); 256 if (removed_keyword_guids) 257 removed_keyword_guids->insert(template_url->sync_guid()); 258 } 259 delete template_url; 260 } 261 } 262 } 263 264 void GetSearchProvidersUsingKeywordResult( 265 const WDTypedResult& result, 266 WebDataService* service, 267 Profile* profile, 268 TemplateURLService::TemplateURLVector* template_urls, 269 TemplateURL** default_search_provider, 270 int* new_resource_keyword_version, 271 std::set<std::string>* removed_keyword_guids) { 272 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 273 DCHECK(template_urls); 274 DCHECK(template_urls->empty()); 275 DCHECK(default_search_provider); 276 DCHECK(*default_search_provider == NULL); 277 DCHECK_EQ(KEYWORDS_RESULT, result.GetType()); 278 DCHECK(new_resource_keyword_version); 279 280 WDKeywordsResult keyword_result = reinterpret_cast< 281 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); 282 283 for (KeywordTable::Keywords::iterator i(keyword_result.keywords.begin()); 284 i != keyword_result.keywords.end(); ++i) { 285 // Fix any duplicate encodings in the local database. Note that we don't 286 // adjust the last_modified time of this keyword; this way, we won't later 287 // overwrite any changes on the sync server that happened to this keyword 288 // since the last time we synced. Instead, we also run a de-duping pass on 289 // the server-provided data in 290 // TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData() and 291 // update the server with the merged, de-duped results at that time. We 292 // still fix here, though, to correct problems in clients that have disabled 293 // search engine sync, since in that case that code will never be reached. 294 if (DeDupeEncodings(&i->input_encodings) && service) 295 service->UpdateKeyword(*i); 296 template_urls->push_back(new TemplateURL(profile, *i)); 297 } 298 299 int64 default_search_provider_id = keyword_result.default_search_provider_id; 300 if (default_search_provider_id) { 301 *default_search_provider = 302 GetTemplateURLByID(*template_urls, default_search_provider_id); 303 } 304 305 *new_resource_keyword_version = keyword_result.builtin_keyword_version; 306 GetSearchProvidersUsingLoadedEngines(service, profile, template_urls, 307 default_search_provider, 308 new_resource_keyword_version, 309 removed_keyword_guids); 310 } 311 312 void GetSearchProvidersUsingLoadedEngines( 313 WebDataService* service, 314 Profile* profile, 315 TemplateURLService::TemplateURLVector* template_urls, 316 TemplateURL** default_search_provider, 317 int* resource_keyword_version, 318 std::set<std::string>* removed_keyword_guids) { 319 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 320 DCHECK(template_urls); 321 DCHECK(default_search_provider); 322 DCHECK(resource_keyword_version); 323 324 ScopedVector<TemplateURL> prepopulated_urls; 325 size_t default_search_index; 326 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile, 327 &prepopulated_urls.get(), &default_search_index); 328 RemoveDuplicatePrepopulateIDs(service, prepopulated_urls, 329 *default_search_provider, template_urls, 330 removed_keyword_guids); 331 332 const int prepopulate_resource_keyword_version = 333 TemplateURLPrepopulateData::GetDataVersion( 334 profile ? profile->GetPrefs() : NULL); 335 if (*resource_keyword_version < prepopulate_resource_keyword_version) { 336 MergeEnginesFromPrepopulateData(profile, service, &prepopulated_urls, 337 default_search_index, template_urls, default_search_provider, 338 removed_keyword_guids); 339 *resource_keyword_version = prepopulate_resource_keyword_version; 340 } else { 341 *resource_keyword_version = 0; 342 } 343 } 344 345 bool DeDupeEncodings(std::vector<std::string>* encodings) { 346 std::vector<std::string> deduped_encodings; 347 std::set<std::string> encoding_set; 348 for (std::vector<std::string>::const_iterator i(encodings->begin()); 349 i != encodings->end(); ++i) { 350 if (encoding_set.insert(*i).second) 351 deduped_encodings.push_back(*i); 352 } 353 encodings->swap(deduped_encodings); 354 return encodings->size() != deduped_encodings.size(); 355 } 356