| OLD | NEW |
| 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 <set> | 7 #include <set> |
| 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 14 #include "chrome/browser/search_engines/template_url_service.h" | 15 #include "chrome/browser/search_engines/template_url_service.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 16 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 | 18 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 31 // http://code.google.com/p/chromium/issues/detail?id=2573 | 32 // http://code.google.com/p/chromium/issues/detail?id=2573 |
| 32 return string16(); | 33 return string16(); |
| 33 } | 34 } |
| 34 return default_provider->short_name(); | 35 return default_provider->short_name(); |
| 35 } | 36 } |
| 36 | 37 |
| 37 // Removes (and deletes) TemplateURLs from |urls| that have duplicate | 38 // Removes (and deletes) TemplateURLs from |urls| that have duplicate |
| 38 // prepopulate ids. Duplicate prepopulate ids are not allowed, but due to a | 39 // prepopulate ids. Duplicate prepopulate ids are not allowed, but due to a |
| 39 // bug it was possible get dups. This step is only called when the version | 40 // bug it was possible get dups. This step is only called when the version |
| 40 // number changes. Only pass in a non-NULL value for |service| if the removed | 41 // number changes. Only pass in a non-NULL value for |service| if the removed |
| 41 // items should be removed from the DB. | 42 // items should be removed from the DB. If |removed_keyword_guids| is not NULL, |
| 43 // the Sync GUID of each item removed from the DB will be added to it. |
| 42 static void RemoveDuplicatePrepopulateIDs( | 44 static void RemoveDuplicatePrepopulateIDs( |
| 43 std::vector<TemplateURL*>* template_urls, | 45 std::vector<TemplateURL*>* template_urls, |
| 44 WebDataService* service) { | 46 WebDataService* service, |
| 47 std::set<std::string>* removed_keyword_guids) { |
| 45 DCHECK(template_urls); | 48 DCHECK(template_urls); |
| 46 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); | 49 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 47 | 50 |
| 48 std::set<int> ids; | 51 std::set<int> ids; |
| 49 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); | 52 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); |
| 50 i != template_urls->end(); ) { | 53 i != template_urls->end(); ) { |
| 51 int prepopulate_id = (*i)->prepopulate_id(); | 54 int prepopulate_id = (*i)->prepopulate_id(); |
| 52 if (prepopulate_id) { | 55 if (prepopulate_id) { |
| 53 if (ids.find(prepopulate_id) != ids.end()) { | 56 if (ids.find(prepopulate_id) != ids.end()) { |
| 54 if (service) | 57 if (service) { |
| 55 service->RemoveKeyword((*i)->id()); | 58 service->RemoveKeyword((*i)->id()); |
| 59 if (removed_keyword_guids) |
| 60 removed_keyword_guids->insert((*i)->sync_guid()); |
| 61 } |
| 56 delete *i; | 62 delete *i; |
| 57 i = template_urls->erase(i); | 63 i = template_urls->erase(i); |
| 58 } else { | 64 } else { |
| 59 ids.insert(prepopulate_id); | 65 ids.insert(prepopulate_id); |
| 60 ++i; | 66 ++i; |
| 61 } | 67 } |
| 62 } else { | 68 } else { |
| 63 ++i; | 69 ++i; |
| 64 } | 70 } |
| 65 } | 71 } |
| 66 } | 72 } |
| 67 | 73 |
| 68 // Returns the TemplateURL with id specified from the list of TemplateURLs. | 74 // Returns the TemplateURL with id specified from the list of TemplateURLs. |
| 69 // If not found, returns NULL. | 75 // If not found, returns NULL. |
| 70 TemplateURL* GetTemplateURLByID( | 76 TemplateURL* GetTemplateURLByID( |
| 71 const std::vector<TemplateURL*>& template_urls, | 77 const std::vector<TemplateURL*>& template_urls, |
| 72 int64 id) { | 78 int64 id) { |
| 73 for (std::vector<TemplateURL*>::const_iterator i = template_urls.begin(); | 79 for (std::vector<TemplateURL*>::const_iterator i = template_urls.begin(); |
| 74 i != template_urls.end(); ++i) { | 80 i != template_urls.end(); ++i) { |
| 75 if ((*i)->id() == id) { | 81 if ((*i)->id() == id) { |
| 76 return *i; | 82 return *i; |
| 77 } | 83 } |
| 78 } | 84 } |
| 79 return NULL; | 85 return NULL; |
| 80 } | 86 } |
| 81 | 87 |
| 82 // Loads engines from prepopulate data and merges them in with the existing | 88 // Loads engines from prepopulate data and merges them in with the existing |
| 83 // engines. This is invoked when the version of the prepopulate data changes. | 89 // engines. This is invoked when the version of the prepopulate data changes. |
| 90 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed |
| 91 // from the DB will be added to it. |
| 84 void MergeEnginesFromPrepopulateData( | 92 void MergeEnginesFromPrepopulateData( |
| 85 Profile* profile, | 93 Profile* profile, |
| 86 WebDataService* service, | 94 WebDataService* service, |
| 87 std::vector<TemplateURL*>* template_urls, | 95 std::vector<TemplateURL*>* template_urls, |
| 88 TemplateURL** default_search_provider) { | 96 TemplateURL** default_search_provider, |
| 97 std::set<std::string>* removed_keyword_guids) { |
| 89 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 90 DCHECK(template_urls); | 99 DCHECK(template_urls); |
| 91 DCHECK(default_search_provider); | 100 DCHECK(default_search_provider); |
| 92 | 101 |
| 93 // Create a map to hold all provided |template_urls| that originally came from | 102 // Create a map to hold all provided |template_urls| that originally came from |
| 94 // prepopulate data (i.e. have a non-zero prepopulate_id()). | 103 // prepopulate data (i.e. have a non-zero prepopulate_id()). |
| 95 typedef std::map<int, TemplateURL*> IDMap; | 104 typedef std::map<int, TemplateURL*> IDMap; |
| 96 IDMap id_to_turl; | 105 IDMap id_to_turl; |
| 97 for (std::vector<TemplateURL*>::iterator i(template_urls->begin()); | 106 for (std::vector<TemplateURL*>::iterator i(template_urls->begin()); |
| 98 i != template_urls->end(); ++i) { | 107 i != template_urls->end(); ++i) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 // found in the prepopulate data. Any remaining URLs that haven't been | 165 // found in the prepopulate data. Any remaining URLs that haven't been |
| 157 // user-edited or made default can be removed from the data store. | 166 // user-edited or made default can be removed from the data store. |
| 158 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) { | 167 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) { |
| 159 const TemplateURL* template_url = i->second; | 168 const TemplateURL* template_url = i->second; |
| 160 if ((template_url->safe_for_autoreplace()) && | 169 if ((template_url->safe_for_autoreplace()) && |
| 161 (template_url != *default_search_provider)) { | 170 (template_url != *default_search_provider)) { |
| 162 std::vector<TemplateURL*>::iterator j = | 171 std::vector<TemplateURL*>::iterator j = |
| 163 std::find(template_urls->begin(), template_urls->end(), template_url); | 172 std::find(template_urls->begin(), template_urls->end(), template_url); |
| 164 DCHECK(j != template_urls->end()); | 173 DCHECK(j != template_urls->end()); |
| 165 template_urls->erase(j); | 174 template_urls->erase(j); |
| 166 if (service) | 175 if (service) { |
| 167 service->RemoveKeyword(template_url->id()); | 176 service->RemoveKeyword(template_url->id()); |
| 177 if (removed_keyword_guids) |
| 178 removed_keyword_guids->insert(template_url->sync_guid()); |
| 179 } |
| 168 delete template_url; | 180 delete template_url; |
| 169 } | 181 } |
| 170 } | 182 } |
| 171 } | 183 } |
| 172 | 184 |
| 173 void GetSearchProvidersUsingKeywordResult( | 185 void GetSearchProvidersUsingKeywordResult( |
| 174 const WDTypedResult& result, | 186 const WDTypedResult& result, |
| 175 WebDataService* service, | 187 WebDataService* service, |
| 176 Profile* profile, | 188 Profile* profile, |
| 177 std::vector<TemplateURL*>* template_urls, | 189 std::vector<TemplateURL*>* template_urls, |
| 178 TemplateURL** default_search_provider, | 190 TemplateURL** default_search_provider, |
| 179 int* new_resource_keyword_version) { | 191 int* new_resource_keyword_version, |
| 192 std::set<std::string>* removed_keyword_guids) { |
| 180 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); | 193 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 181 DCHECK(template_urls); | 194 DCHECK(template_urls); |
| 182 DCHECK(template_urls->empty()); | 195 DCHECK(template_urls->empty()); |
| 183 DCHECK(default_search_provider); | 196 DCHECK(default_search_provider); |
| 184 DCHECK(*default_search_provider == NULL); | 197 DCHECK(*default_search_provider == NULL); |
| 185 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); | 198 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); |
| 186 DCHECK(new_resource_keyword_version); | 199 DCHECK(new_resource_keyword_version); |
| 187 | 200 |
| 188 *new_resource_keyword_version = 0; | 201 *new_resource_keyword_version = 0; |
| 189 WDKeywordsResult keyword_result = reinterpret_cast< | 202 WDKeywordsResult keyword_result = reinterpret_cast< |
| 190 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); | 203 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); |
| 191 | 204 |
| 192 for (KeywordTable::Keywords::const_iterator i( | 205 for (KeywordTable::Keywords::const_iterator i( |
| 193 keyword_result.keywords.begin()); i != keyword_result.keywords.end(); | 206 keyword_result.keywords.begin()); i != keyword_result.keywords.end(); |
| 194 ++i) | 207 ++i) |
| 195 template_urls->push_back(new TemplateURL(profile, *i)); | 208 template_urls->push_back(new TemplateURL(profile, *i)); |
| 196 | 209 |
| 197 const int resource_keyword_version = | 210 const int resource_keyword_version = |
| 198 TemplateURLPrepopulateData::GetDataVersion( | 211 TemplateURLPrepopulateData::GetDataVersion( |
| 199 profile ? profile->GetPrefs() : NULL); | 212 profile ? profile->GetPrefs() : NULL); |
| 200 if (keyword_result.builtin_keyword_version != resource_keyword_version) { | 213 if (keyword_result.builtin_keyword_version != resource_keyword_version) { |
| 201 // There should never be duplicate TemplateURLs. We had a bug such that | 214 // There should never be duplicate TemplateURLs. We had a bug such that |
| 202 // duplicate TemplateURLs existed for one locale. As such we invoke | 215 // duplicate TemplateURLs existed for one locale. As such we invoke |
| 203 // RemoveDuplicatePrepopulateIDs to nuke the duplicates. | 216 // RemoveDuplicatePrepopulateIDs to nuke the duplicates. |
| 204 RemoveDuplicatePrepopulateIDs(template_urls, service); | 217 RemoveDuplicatePrepopulateIDs(template_urls, service, |
| 218 removed_keyword_guids); |
| 205 } | 219 } |
| 206 | 220 |
| 207 int64 default_search_provider_id = keyword_result.default_search_provider_id; | 221 int64 default_search_provider_id = keyword_result.default_search_provider_id; |
| 208 if (default_search_provider_id) { | 222 if (default_search_provider_id) { |
| 209 *default_search_provider = | 223 *default_search_provider = |
| 210 GetTemplateURLByID(*template_urls, default_search_provider_id); | 224 GetTemplateURLByID(*template_urls, default_search_provider_id); |
| 211 } | 225 } |
| 212 | 226 |
| 213 if (keyword_result.builtin_keyword_version != resource_keyword_version) { | 227 if (keyword_result.builtin_keyword_version != resource_keyword_version) { |
| 214 MergeEnginesFromPrepopulateData(profile, service, template_urls, | 228 MergeEnginesFromPrepopulateData(profile, service, template_urls, |
| 215 default_search_provider); | 229 default_search_provider, |
| 230 removed_keyword_guids); |
| 216 *new_resource_keyword_version = resource_keyword_version; | 231 *new_resource_keyword_version = resource_keyword_version; |
| 217 } | 232 } |
| 218 } | 233 } |
| 219 | 234 |
| 220 bool DidDefaultSearchProviderChange( | 235 bool DidDefaultSearchProviderChange( |
| 221 const WDTypedResult& result, | 236 const WDTypedResult& result, |
| 222 Profile* profile, | 237 Profile* profile, |
| 223 scoped_ptr<TemplateURL>* backup_default_search_provider) { | 238 scoped_ptr<TemplateURL>* backup_default_search_provider) { |
| 224 DCHECK(backup_default_search_provider); | 239 DCHECK(backup_default_search_provider); |
| 225 DCHECK(!backup_default_search_provider->get()); | 240 DCHECK(!backup_default_search_provider->get()); |
| 226 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); | 241 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); |
| 227 | 242 |
| 228 WDKeywordsResult keyword_result = reinterpret_cast< | 243 WDKeywordsResult keyword_result = reinterpret_cast< |
| 229 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); | 244 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); |
| 230 | 245 |
| 231 if (!keyword_result.did_default_search_provider_change) | 246 if (!keyword_result.did_default_search_provider_change) |
| 232 return false; | 247 return false; |
| 233 | 248 |
| 234 if (keyword_result.backup_valid) { | 249 if (keyword_result.backup_valid) { |
| 235 backup_default_search_provider->reset(new TemplateURL(profile, | 250 backup_default_search_provider->reset(new TemplateURL(profile, |
| 236 keyword_result.default_search_provider_backup)); | 251 keyword_result.default_search_provider_backup)); |
| 237 } | 252 } |
| 238 return true; | 253 return true; |
| 239 } | 254 } |
| 240 | |
| OLD | NEW |