OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/util.h" | 5 #include "components/search_engines/util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <map> | 11 #include <map> |
12 #include <set> | 12 #include <set> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/memory/scoped_vector.h" | |
19 #include "base/time/time.h" | 18 #include "base/time/time.h" |
20 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
21 #include "components/search_engines/template_url.h" | 20 #include "components/search_engines/template_url.h" |
22 #include "components/search_engines/template_url_prepopulate_data.h" | 21 #include "components/search_engines/template_url_prepopulate_data.h" |
23 #include "components/search_engines/template_url_service.h" | 22 #include "components/search_engines/template_url_service.h" |
24 | 23 |
25 base::string16 GetDefaultSearchEngineName(TemplateURLService* service) { | 24 base::string16 GetDefaultSearchEngineName(TemplateURLService* service) { |
26 DCHECK(service); | 25 DCHECK(service); |
27 const TemplateURL* const default_provider = | 26 const TemplateURL* const default_provider = |
28 service->GetDefaultSearchProvider(); | 27 service->GetDefaultSearchProvider(); |
(...skipping 15 matching lines...) Expand all Loading... |
44 const TemplateURLRef& search_url = default_provider->url_ref(); | 43 const TemplateURLRef& search_url = default_provider->url_ref(); |
45 DCHECK(search_url.SupportsReplacement(service->search_terms_data())); | 44 DCHECK(search_url.SupportsReplacement(service->search_terms_data())); |
46 TemplateURLRef::SearchTermsArgs search_terms_args(terms); | 45 TemplateURLRef::SearchTermsArgs search_terms_args(terms); |
47 search_terms_args.append_extra_query_params = true; | 46 search_terms_args.append_extra_query_params = true; |
48 return GURL(search_url.ReplaceSearchTerms(search_terms_args, | 47 return GURL(search_url.ReplaceSearchTerms(search_terms_args, |
49 service->search_terms_data())); | 48 service->search_terms_data())); |
50 } | 49 } |
51 | 50 |
52 void RemoveDuplicatePrepopulateIDs( | 51 void RemoveDuplicatePrepopulateIDs( |
53 KeywordWebDataService* service, | 52 KeywordWebDataService* service, |
54 const ScopedVector<TemplateURLData>& prepopulated_urls, | 53 const std::vector<std::unique_ptr<TemplateURLData>>& prepopulated_urls, |
55 TemplateURL* default_search_provider, | 54 TemplateURL* default_search_provider, |
56 TemplateURLService::OwnedTemplateURLVector* template_urls, | 55 TemplateURLService::OwnedTemplateURLVector* template_urls, |
57 const SearchTermsData& search_terms_data, | 56 const SearchTermsData& search_terms_data, |
58 std::set<std::string>* removed_keyword_guids) { | 57 std::set<std::string>* removed_keyword_guids) { |
59 DCHECK(template_urls); | 58 DCHECK(template_urls); |
60 | 59 |
61 // For convenience construct an ID->TemplateURL* map from |prepopulated_urls|. | 60 // For convenience construct an ID->TemplateURL* map from |prepopulated_urls|. |
62 std::map<int, TemplateURLData*> prepopulated_url_map; | 61 std::map<int, TemplateURLData*> prepopulated_url_map; |
63 for (std::vector<TemplateURLData*>::const_iterator i( | 62 for (const auto& url : prepopulated_urls) |
64 prepopulated_urls.begin()); | 63 prepopulated_url_map[url->prepopulate_id] = url.get(); |
65 i != prepopulated_urls.end(); | |
66 ++i) | |
67 prepopulated_url_map[(*i)->prepopulate_id] = *i; | |
68 | 64 |
69 // Separate |template_urls| into prepopulated and non-prepopulated groups. | 65 // Separate |template_urls| into prepopulated and non-prepopulated groups. |
70 std::multimap<int, std::unique_ptr<TemplateURL>> unchecked_urls; | 66 std::multimap<int, std::unique_ptr<TemplateURL>> unchecked_urls; |
71 TemplateURLService::OwnedTemplateURLVector checked_urls; | 67 TemplateURLService::OwnedTemplateURLVector checked_urls; |
72 for (auto& turl : *template_urls) { | 68 for (auto& turl : *template_urls) { |
73 int prepopulate_id = turl->prepopulate_id(); | 69 int prepopulate_id = turl->prepopulate_id(); |
74 if (prepopulate_id) | 70 if (prepopulate_id) |
75 unchecked_urls.insert(std::make_pair(prepopulate_id, std::move(turl))); | 71 unchecked_urls.insert(std::make_pair(prepopulate_id, std::move(turl))); |
76 else | 72 else |
77 checked_urls.push_back(std::move(turl)); | 73 checked_urls.push_back(std::move(turl)); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 const ActionsFromPrepopulateData& other) = default; | 178 const ActionsFromPrepopulateData& other) = default; |
183 | 179 |
184 ActionsFromPrepopulateData::~ActionsFromPrepopulateData() {} | 180 ActionsFromPrepopulateData::~ActionsFromPrepopulateData() {} |
185 | 181 |
186 // This is invoked when the version of the prepopulate data changes. | 182 // This is invoked when the version of the prepopulate data changes. |
187 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed | 183 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed |
188 // from the DB will be added to it. Note that this function will take | 184 // from the DB will be added to it. Note that this function will take |
189 // ownership of |prepopulated_urls| and will clear the vector. | 185 // ownership of |prepopulated_urls| and will clear the vector. |
190 void MergeEnginesFromPrepopulateData( | 186 void MergeEnginesFromPrepopulateData( |
191 KeywordWebDataService* service, | 187 KeywordWebDataService* service, |
192 ScopedVector<TemplateURLData>* prepopulated_urls, | 188 std::vector<std::unique_ptr<TemplateURLData>>* prepopulated_urls, |
193 size_t default_search_index, | 189 size_t default_search_index, |
194 TemplateURLService::OwnedTemplateURLVector* template_urls, | 190 TemplateURLService::OwnedTemplateURLVector* template_urls, |
195 TemplateURL* default_search_provider, | 191 TemplateURL* default_search_provider, |
196 std::set<std::string>* removed_keyword_guids) { | 192 std::set<std::string>* removed_keyword_guids) { |
197 DCHECK(prepopulated_urls); | 193 DCHECK(prepopulated_urls); |
198 DCHECK(template_urls); | 194 DCHECK(template_urls); |
199 | 195 |
200 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData( | 196 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData( |
201 prepopulated_urls, *template_urls, default_search_provider)); | 197 prepopulated_urls, *template_urls, default_search_provider)); |
202 | 198 |
(...skipping 22 matching lines...) Expand all Loading... |
225 auto j = FindTemplateURL(template_urls, edited_engine.first); | 221 auto j = FindTemplateURL(template_urls, edited_engine.first); |
226 *j = base::MakeUnique<TemplateURL>(data); | 222 *j = base::MakeUnique<TemplateURL>(data); |
227 } | 223 } |
228 | 224 |
229 // Add items. | 225 // Add items. |
230 for (const auto& added_engine : actions.added_engines) | 226 for (const auto& added_engine : actions.added_engines) |
231 template_urls->push_back(base::MakeUnique<TemplateURL>(added_engine)); | 227 template_urls->push_back(base::MakeUnique<TemplateURL>(added_engine)); |
232 } | 228 } |
233 | 229 |
234 ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData( | 230 ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData( |
235 ScopedVector<TemplateURLData>* prepopulated_urls, | 231 std::vector<std::unique_ptr<TemplateURLData>>* prepopulated_urls, |
236 const TemplateURLService::OwnedTemplateURLVector& existing_urls, | 232 const TemplateURLService::OwnedTemplateURLVector& existing_urls, |
237 const TemplateURL* default_search_provider) { | 233 const TemplateURL* default_search_provider) { |
238 // Create a map to hold all provided |template_urls| that originally came from | 234 // Create a map to hold all provided |template_urls| that originally came from |
239 // prepopulate data (i.e. have a non-zero prepopulate_id()). | 235 // prepopulate data (i.e. have a non-zero prepopulate_id()). |
240 typedef std::map<int, TemplateURL*> IDMap; | 236 std::map<int, TemplateURL*> id_to_turl; |
241 IDMap id_to_turl; | |
242 for (auto& turl : existing_urls) { | 237 for (auto& turl : existing_urls) { |
243 int prepopulate_id = turl->prepopulate_id(); | 238 int prepopulate_id = turl->prepopulate_id(); |
244 if (prepopulate_id > 0) | 239 if (prepopulate_id > 0) |
245 id_to_turl[prepopulate_id] = turl.get(); | 240 id_to_turl[prepopulate_id] = turl.get(); |
246 } | 241 } |
247 | 242 |
248 // For each current prepopulated URL, check whether |template_urls| contained | 243 // For each current prepopulated URL, check whether |template_urls| contained |
249 // a matching prepopulated URL. If so, update the passed-in URL to match the | 244 // a matching prepopulated URL. If so, update the passed-in URL to match the |
250 // current data. (If the passed-in URL was user-edited, we persist the user's | 245 // current data. (If the passed-in URL was user-edited, we persist the user's |
251 // name and keyword.) If not, add the prepopulated URL. | 246 // name and keyword.) If not, add the prepopulated URL. |
252 ActionsFromPrepopulateData actions; | 247 ActionsFromPrepopulateData actions; |
253 for (size_t i = 0; i < prepopulated_urls->size(); ++i) { | 248 for (auto& prepopulated_url : *prepopulated_urls) { |
254 // We take ownership of |prepopulated_urls[i]|. | |
255 std::unique_ptr<TemplateURLData> prepopulated_url((*prepopulated_urls)[i]); | |
256 const int prepopulated_id = prepopulated_url->prepopulate_id; | 249 const int prepopulated_id = prepopulated_url->prepopulate_id; |
257 DCHECK_NE(0, prepopulated_id); | 250 DCHECK_NE(0, prepopulated_id); |
258 | 251 |
259 IDMap::iterator existing_url_iter(id_to_turl.find(prepopulated_id)); | 252 auto existing_url_iter = id_to_turl.find(prepopulated_id); |
260 if (existing_url_iter != id_to_turl.end()) { | 253 if (existing_url_iter != id_to_turl.end()) { |
261 // Update the data store with the new prepopulated data. Preserve user | 254 // Update the data store with the new prepopulated data. Preserve user |
262 // edits to the name and keyword. | 255 // edits to the name and keyword. |
263 TemplateURL* existing_url(existing_url_iter->second); | 256 TemplateURL* existing_url(existing_url_iter->second); |
264 id_to_turl.erase(existing_url_iter); | 257 id_to_turl.erase(existing_url_iter); |
265 MergeIntoPrepopulatedEngineData(existing_url, prepopulated_url.get()); | 258 MergeIntoPrepopulatedEngineData(existing_url, prepopulated_url.get()); |
266 // Update last_modified to ensure that if this entry is later merged with | 259 // Update last_modified to ensure that if this entry is later merged with |
267 // entries from Sync, the conflict resolution logic knows that this was | 260 // entries from Sync, the conflict resolution logic knows that this was |
268 // updated and propagates the new values to the server. | 261 // updated and propagates the new values to the server. |
269 prepopulated_url->last_modified = base::Time::Now(); | 262 prepopulated_url->last_modified = base::Time::Now(); |
270 actions.edited_engines.push_back( | 263 actions.edited_engines.push_back( |
271 std::make_pair(existing_url, *prepopulated_url)); | 264 std::make_pair(existing_url, *prepopulated_url)); |
272 } else { | 265 } else { |
273 actions.added_engines.push_back(*prepopulated_url); | 266 actions.added_engines.push_back(*prepopulated_url); |
274 } | 267 } |
275 } | 268 } |
276 // The above loop takes ownership of all the contents of prepopulated_urls. | |
277 // Clear the pointers. | |
278 prepopulated_urls->weak_erase(prepopulated_urls->begin(), | |
279 prepopulated_urls->end()); | |
280 | 269 |
281 // The block above removed all the URLs from the |id_to_turl| map that were | 270 // The block above removed all the URLs from the |id_to_turl| map that were |
282 // found in the prepopulate data. Any remaining URLs that haven't been | 271 // found in the prepopulate data. Any remaining URLs that haven't been |
283 // user-edited or made default can be removed from the data store. | 272 // user-edited or made default can be removed from the data store. |
284 // We assume that this entry is equivalent to the DSE if its prepopulate ID | 273 // We assume that this entry is equivalent to the DSE if its prepopulate ID |
285 // and keyword both match. If the prepopulate ID _does_ match all properties | 274 // and keyword both match. If the prepopulate ID _does_ match all properties |
286 // will be replaced with those from |default_search_provider| anyway. | 275 // will be replaced with those from |default_search_provider| anyway. |
287 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) { | 276 for (auto i = id_to_turl.begin(); i != id_to_turl.end(); ++i) { |
288 TemplateURL* template_url = i->second; | 277 TemplateURL* template_url = i->second; |
289 if ((template_url->safe_for_autoreplace()) && | 278 if ((template_url->safe_for_autoreplace()) && |
290 (!default_search_provider || | 279 (!default_search_provider || |
291 (template_url->prepopulate_id() != | 280 (template_url->prepopulate_id() != |
292 default_search_provider->prepopulate_id()) || | 281 default_search_provider->prepopulate_id()) || |
293 (template_url->keyword() != default_search_provider->keyword()))) | 282 (template_url->keyword() != default_search_provider->keyword()))) |
294 actions.removed_engines.push_back(template_url); | 283 actions.removed_engines.push_back(template_url); |
295 } | 284 } |
296 | 285 |
297 return actions; | 286 return actions; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 KeywordWebDataService* service, | 330 KeywordWebDataService* service, |
342 PrefService* prefs, | 331 PrefService* prefs, |
343 TemplateURLService::OwnedTemplateURLVector* template_urls, | 332 TemplateURLService::OwnedTemplateURLVector* template_urls, |
344 TemplateURL* default_search_provider, | 333 TemplateURL* default_search_provider, |
345 const SearchTermsData& search_terms_data, | 334 const SearchTermsData& search_terms_data, |
346 int* resource_keyword_version, | 335 int* resource_keyword_version, |
347 std::set<std::string>* removed_keyword_guids) { | 336 std::set<std::string>* removed_keyword_guids) { |
348 DCHECK(template_urls); | 337 DCHECK(template_urls); |
349 DCHECK(resource_keyword_version); | 338 DCHECK(resource_keyword_version); |
350 size_t default_search_index; | 339 size_t default_search_index; |
351 ScopedVector<TemplateURLData> prepopulated_urls = | 340 std::vector<std::unique_ptr<TemplateURLData>> prepopulated_urls = |
352 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, | 341 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, |
353 &default_search_index); | 342 &default_search_index); |
354 RemoveDuplicatePrepopulateIDs(service, prepopulated_urls, | 343 RemoveDuplicatePrepopulateIDs(service, prepopulated_urls, |
355 default_search_provider, template_urls, | 344 default_search_provider, template_urls, |
356 search_terms_data, removed_keyword_guids); | 345 search_terms_data, removed_keyword_guids); |
357 | 346 |
358 const int prepopulate_resource_keyword_version = | 347 const int prepopulate_resource_keyword_version = |
359 TemplateURLPrepopulateData::GetDataVersion(prefs); | 348 TemplateURLPrepopulateData::GetDataVersion(prefs); |
360 if (*resource_keyword_version < prepopulate_resource_keyword_version) { | 349 if (*resource_keyword_version < prepopulate_resource_keyword_version) { |
361 MergeEnginesFromPrepopulateData( | 350 MergeEnginesFromPrepopulateData( |
(...skipping 18 matching lines...) Expand all Loading... |
380 } | 369 } |
381 | 370 |
382 TemplateURLService::OwnedTemplateURLVector::iterator FindTemplateURL( | 371 TemplateURLService::OwnedTemplateURLVector::iterator FindTemplateURL( |
383 TemplateURLService::OwnedTemplateURLVector* urls, | 372 TemplateURLService::OwnedTemplateURLVector* urls, |
384 const TemplateURL* url) { | 373 const TemplateURL* url) { |
385 return std::find_if(urls->begin(), urls->end(), | 374 return std::find_if(urls->begin(), urls->end(), |
386 [url](const std::unique_ptr<TemplateURL>& ptr) { | 375 [url](const std::unique_ptr<TemplateURL>& ptr) { |
387 return ptr.get() == url; | 376 return ptr.get() == url; |
388 }); | 377 }); |
389 } | 378 } |
OLD | NEW |