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

Side by Side Diff: components/search_engines/util.cc

Issue 2290503003: Remove use of stl_util in search_engines. (Closed)
Patch Set: rebase Created 4 years, 3 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
OLDNEW
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 <map> 11 #include <map>
11 #include <set> 12 #include <set>
12 #include <string> 13 #include <string>
13 #include <vector> 14 #include <vector>
14 15
15 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/memory/ptr_util.h"
16 #include "base/memory/scoped_vector.h" 18 #include "base/memory/scoped_vector.h"
17 #include "base/time/time.h" 19 #include "base/time/time.h"
18 #include "components/prefs/pref_service.h" 20 #include "components/prefs/pref_service.h"
19 #include "components/search_engines/template_url.h" 21 #include "components/search_engines/template_url.h"
20 #include "components/search_engines/template_url_prepopulate_data.h" 22 #include "components/search_engines/template_url_prepopulate_data.h"
21 #include "components/search_engines/template_url_service.h" 23 #include "components/search_engines/template_url_service.h"
22 24
23 base::string16 GetDefaultSearchEngineName(TemplateURLService* service) { 25 base::string16 GetDefaultSearchEngineName(TemplateURLService* service) {
24 DCHECK(service); 26 DCHECK(service);
25 const TemplateURL* const default_provider = 27 const TemplateURL* const default_provider =
(...skipping 18 matching lines...) Expand all
44 TemplateURLRef::SearchTermsArgs search_terms_args(terms); 46 TemplateURLRef::SearchTermsArgs search_terms_args(terms);
45 search_terms_args.append_extra_query_params = true; 47 search_terms_args.append_extra_query_params = true;
46 return GURL(search_url.ReplaceSearchTerms(search_terms_args, 48 return GURL(search_url.ReplaceSearchTerms(search_terms_args,
47 service->search_terms_data())); 49 service->search_terms_data()));
48 } 50 }
49 51
50 void RemoveDuplicatePrepopulateIDs( 52 void RemoveDuplicatePrepopulateIDs(
51 KeywordWebDataService* service, 53 KeywordWebDataService* service,
52 const ScopedVector<TemplateURLData>& prepopulated_urls, 54 const ScopedVector<TemplateURLData>& prepopulated_urls,
53 TemplateURL* default_search_provider, 55 TemplateURL* default_search_provider,
54 TemplateURLService::TemplateURLVector* template_urls, 56 TemplateURLService::OwnedTemplateURLVector* template_urls,
55 const SearchTermsData& search_terms_data, 57 const SearchTermsData& search_terms_data,
56 std::set<std::string>* removed_keyword_guids) { 58 std::set<std::string>* removed_keyword_guids) {
57 DCHECK(template_urls); 59 DCHECK(template_urls);
58 60
59 // For convenience construct an ID->TemplateURL* map from |prepopulated_urls|. 61 // For convenience construct an ID->TemplateURL* map from |prepopulated_urls|.
60 typedef std::map<int, TemplateURLData*> PrepopulatedURLMap; 62 std::map<int, TemplateURLData*> prepopulated_url_map;
61 PrepopulatedURLMap prepopulated_url_map;
62 for (std::vector<TemplateURLData*>::const_iterator i( 63 for (std::vector<TemplateURLData*>::const_iterator i(
63 prepopulated_urls.begin()); 64 prepopulated_urls.begin());
64 i != prepopulated_urls.end(); 65 i != prepopulated_urls.end();
65 ++i) 66 ++i)
66 prepopulated_url_map[(*i)->prepopulate_id] = *i; 67 prepopulated_url_map[(*i)->prepopulate_id] = *i;
67 68
68 // Separate |template_urls| into prepopulated and non-prepopulated groups. 69 // Separate |template_urls| into prepopulated and non-prepopulated groups.
69 typedef std::multimap<int, TemplateURL*> UncheckedURLMap; 70 std::multimap<int, std::unique_ptr<TemplateURL>> unchecked_urls;
70 UncheckedURLMap unchecked_urls; 71 TemplateURLService::OwnedTemplateURLVector checked_urls;
71 TemplateURLService::TemplateURLVector checked_urls; 72 for (auto& turl : *template_urls) {
72 for (TemplateURLService::TemplateURLVector::iterator i(
73 template_urls->begin()); i != template_urls->end(); ++i) {
74 TemplateURL* turl = *i;
75 int prepopulate_id = turl->prepopulate_id(); 73 int prepopulate_id = turl->prepopulate_id();
76 if (prepopulate_id) 74 if (prepopulate_id)
77 unchecked_urls.insert(std::make_pair(prepopulate_id, turl)); 75 unchecked_urls.insert(std::make_pair(prepopulate_id, std::move(turl)));
78 else 76 else
79 checked_urls.push_back(turl); 77 checked_urls.push_back(std::move(turl));
80 } 78 }
81 79
82 // For each group of prepopulated URLs with one ID, find the best URL to use 80 // For each group of prepopulated URLs with one ID, find the best URL to use
83 // and add it to the (initially all non-prepopulated) URLs we've already OKed. 81 // and add it to the (initially all non-prepopulated) URLs we've already OKed.
84 // Delete the others from the service and from memory. 82 // Delete the others from the service and from memory.
85 while (!unchecked_urls.empty()) { 83 while (!unchecked_urls.empty()) {
86 // Find the best URL. 84 // Find the best URL.
87 int prepopulate_id = unchecked_urls.begin()->first; 85 int prepopulate_id = unchecked_urls.begin()->first;
88 PrepopulatedURLMap::const_iterator prepopulated_url = 86 auto prepopulated_url = prepopulated_url_map.find(prepopulate_id);
89 prepopulated_url_map.find(prepopulate_id); 87 auto end = unchecked_urls.upper_bound(prepopulate_id);
90 UncheckedURLMap::iterator end = unchecked_urls.upper_bound(prepopulate_id); 88 auto best = unchecked_urls.begin();
91 UncheckedURLMap::iterator best = unchecked_urls.begin();
92 bool matched_keyword = false; 89 bool matched_keyword = false;
93 for (UncheckedURLMap::iterator i = unchecked_urls.begin(); i != end; ++i) { 90 for (auto i = unchecked_urls.begin(); i != end; ++i) {
94 // If the user-selected DSE is a prepopulated engine its properties will 91 // If the user-selected DSE is a prepopulated engine its properties will
95 // either come from the prepopulation origin or from the user preferences 92 // either come from the prepopulation origin or from the user preferences
96 // file (see DefaultSearchManager). Those properties will end up 93 // file (see DefaultSearchManager). Those properties will end up
97 // overwriting whatever we load now anyway. If we are eliminating 94 // overwriting whatever we load now anyway. If we are eliminating
98 // duplicates, then, we err on the side of keeping the thing that looks 95 // duplicates, then, we err on the side of keeping the thing that looks
99 // more like the value we will end up with in the end. 96 // more like the value we will end up with in the end.
100 if (default_search_provider && 97 if (default_search_provider &&
101 (default_search_provider->prepopulate_id() == 98 (default_search_provider->prepopulate_id() ==
102 i->second->prepopulate_id()) && 99 i->second->prepopulate_id()) &&
103 default_search_provider->HasSameKeywordAs(i->second->data(), 100 default_search_provider->HasSameKeywordAs(i->second->data(),
(...skipping 10 matching lines...) Expand all
114 i->second->HasSameKeywordAs(*prepopulated_url->second, 111 i->second->HasSameKeywordAs(*prepopulated_url->second,
115 search_terms_data)) { 112 search_terms_data)) {
116 best = i; 113 best = i;
117 matched_keyword = true; 114 matched_keyword = true;
118 } else if (i->second->id() < best->second->id()) { 115 } else if (i->second->id() < best->second->id()) {
119 best = i; 116 best = i;
120 } 117 }
121 } 118 }
122 119
123 // Add the best URL to the checked group and delete the rest. 120 // Add the best URL to the checked group and delete the rest.
124 checked_urls.push_back(best->second); 121 checked_urls.push_back(std::move(best->second));
125 for (UncheckedURLMap::iterator i = unchecked_urls.begin(); i != end; ++i) { 122 for (auto i = unchecked_urls.begin(); i != end; ++i) {
126 if (i == best) 123 if (i == best)
127 continue; 124 continue;
128 if (service) { 125 if (service) {
129 service->RemoveKeyword(i->second->id()); 126 service->RemoveKeyword(i->second->id());
130 if (removed_keyword_guids) 127 if (removed_keyword_guids)
131 removed_keyword_guids->insert(i->second->sync_guid()); 128 removed_keyword_guids->insert(i->second->sync_guid());
132 } 129 }
133 delete i->second;
134 } 130 }
135 131
136 // Done with this group. 132 // Done with this group.
137 unchecked_urls.erase(unchecked_urls.begin(), end); 133 unchecked_urls.erase(unchecked_urls.begin(), end);
138 } 134 }
139 135
140 // Return the checked URLs. 136 // Return the checked URLs.
141 template_urls->swap(checked_urls); 137 template_urls->swap(checked_urls);
142 } 138 }
143 139
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 ActionsFromPrepopulateData::~ActionsFromPrepopulateData() {} 184 ActionsFromPrepopulateData::~ActionsFromPrepopulateData() {}
189 185
190 // This is invoked when the version of the prepopulate data changes. 186 // This is invoked when the version of the prepopulate data changes.
191 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed 187 // If |removed_keyword_guids| is not NULL, the Sync GUID of each item removed
192 // from the DB will be added to it. Note that this function will take 188 // from the DB will be added to it. Note that this function will take
193 // ownership of |prepopulated_urls| and will clear the vector. 189 // ownership of |prepopulated_urls| and will clear the vector.
194 void MergeEnginesFromPrepopulateData( 190 void MergeEnginesFromPrepopulateData(
195 KeywordWebDataService* service, 191 KeywordWebDataService* service,
196 ScopedVector<TemplateURLData>* prepopulated_urls, 192 ScopedVector<TemplateURLData>* prepopulated_urls,
197 size_t default_search_index, 193 size_t default_search_index,
198 TemplateURLService::TemplateURLVector* template_urls, 194 TemplateURLService::OwnedTemplateURLVector* template_urls,
199 TemplateURL* default_search_provider, 195 TemplateURL* default_search_provider,
200 std::set<std::string>* removed_keyword_guids) { 196 std::set<std::string>* removed_keyword_guids) {
201 DCHECK(prepopulated_urls); 197 DCHECK(prepopulated_urls);
202 DCHECK(template_urls); 198 DCHECK(template_urls);
203 199
204 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData( 200 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData(
205 prepopulated_urls, *template_urls, default_search_provider)); 201 prepopulated_urls, *template_urls, default_search_provider));
206 202
207 // Remove items. 203 // Remove items.
208 for (std::vector<TemplateURL*>::iterator i = actions.removed_engines.begin(); 204 for (const auto& removed_engine : actions.removed_engines) {
Peter Kasting 2016/09/01 08:21:39 Nit: I think this should have been const auto* ins
Avi (use Gerrit) 2016/09/01 15:14:47 Done.
209 i < actions.removed_engines.end(); ++i) { 205 auto j = FindTemplateURL(template_urls, removed_engine);
210 std::unique_ptr<TemplateURL> template_url(*i);
211 TemplateURLService::TemplateURLVector::iterator j = std::find(
212 template_urls->begin(), template_urls->end(), template_url.get());
213 DCHECK(j != template_urls->end()); 206 DCHECK(j != template_urls->end());
207 std::unique_ptr<TemplateURL> template_url = std::move(*j);
214 DCHECK(!default_search_provider || 208 DCHECK(!default_search_provider ||
215 (*j)->prepopulate_id() != default_search_provider->prepopulate_id()); 209 (*j)->prepopulate_id() != default_search_provider->prepopulate_id());
216 template_urls->erase(j); 210 template_urls->erase(j);
217 if (service) { 211 if (service) {
218 service->RemoveKeyword(template_url->id()); 212 service->RemoveKeyword(template_url->id());
219 if (removed_keyword_guids) 213 if (removed_keyword_guids)
220 removed_keyword_guids->insert(template_url->sync_guid()); 214 removed_keyword_guids->insert(template_url->sync_guid());
221 } 215 }
222 } 216 }
223 217
224 // Edit items. 218 // Edit items.
225 for (EditedEngines::iterator i(actions.edited_engines.begin()); 219 for (const auto& edited_engine : actions.edited_engines) {
226 i < actions.edited_engines.end(); ++i) { 220 const TemplateURLData& data = edited_engine.second;
227 TemplateURLData& data = i->second;
228 std::unique_ptr<TemplateURL> existing_url(i->first);
229 if (service) 221 if (service)
230 service->UpdateKeyword(data); 222 service->UpdateKeyword(data);
231 223
232 // Replace the entry in |template_urls| with the updated one. 224 // Replace the entry in |template_urls| with the updated one.
233 TemplateURLService::TemplateURLVector::iterator j = std::find( 225 auto j = FindTemplateURL(template_urls, edited_engine.first);
234 template_urls->begin(), template_urls->end(), existing_url.get()); 226 *j = base::MakeUnique<TemplateURL>(data);
235 *j = new TemplateURL(data);
236 } 227 }
237 228
238 // Add items. 229 // Add items.
239 for (std::vector<TemplateURLData>::const_iterator it = 230 for (const auto& added_engine : actions.added_engines)
240 actions.added_engines.begin(); 231 template_urls->push_back(base::MakeUnique<TemplateURL>(added_engine));
241 it != actions.added_engines.end();
242 ++it) {
243 template_urls->push_back(new TemplateURL(*it));
244 }
245 } 232 }
246 233
247 ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData( 234 ActionsFromPrepopulateData CreateActionsFromCurrentPrepopulateData(
248 ScopedVector<TemplateURLData>* prepopulated_urls, 235 ScopedVector<TemplateURLData>* prepopulated_urls,
249 const TemplateURLService::TemplateURLVector& existing_urls, 236 const TemplateURLService::OwnedTemplateURLVector& existing_urls,
250 const TemplateURL* default_search_provider) { 237 const TemplateURL* default_search_provider) {
251 // Create a map to hold all provided |template_urls| that originally came from 238 // Create a map to hold all provided |template_urls| that originally came from
252 // prepopulate data (i.e. have a non-zero prepopulate_id()). 239 // prepopulate data (i.e. have a non-zero prepopulate_id()).
253 typedef std::map<int, TemplateURL*> IDMap; 240 typedef std::map<int, TemplateURL*> IDMap;
254 IDMap id_to_turl; 241 IDMap id_to_turl;
255 for (TemplateURLService::TemplateURLVector::const_iterator i( 242 for (auto& turl : existing_urls) {
256 existing_urls.begin()); i != existing_urls.end(); ++i) { 243 int prepopulate_id = turl->prepopulate_id();
257 int prepopulate_id = (*i)->prepopulate_id();
258 if (prepopulate_id > 0) 244 if (prepopulate_id > 0)
259 id_to_turl[prepopulate_id] = *i; 245 id_to_turl[prepopulate_id] = turl.get();
260 } 246 }
261 247
262 // For each current prepopulated URL, check whether |template_urls| contained 248 // For each current prepopulated URL, check whether |template_urls| contained
263 // a matching prepopulated URL. If so, update the passed-in URL to match the 249 // a matching prepopulated URL. If so, update the passed-in URL to match the
264 // current data. (If the passed-in URL was user-edited, we persist the user's 250 // current data. (If the passed-in URL was user-edited, we persist the user's
265 // name and keyword.) If not, add the prepopulated URL. 251 // name and keyword.) If not, add the prepopulated URL.
266 ActionsFromPrepopulateData actions; 252 ActionsFromPrepopulateData actions;
267 for (size_t i = 0; i < prepopulated_urls->size(); ++i) { 253 for (size_t i = 0; i < prepopulated_urls->size(); ++i) {
268 // We take ownership of |prepopulated_urls[i]|. 254 // We take ownership of |prepopulated_urls[i]|.
269 std::unique_ptr<TemplateURLData> prepopulated_url((*prepopulated_urls)[i]); 255 std::unique_ptr<TemplateURLData> prepopulated_url((*prepopulated_urls)[i]);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 actions.removed_engines.push_back(template_url); 294 actions.removed_engines.push_back(template_url);
309 } 295 }
310 296
311 return actions; 297 return actions;
312 } 298 }
313 299
314 void GetSearchProvidersUsingKeywordResult( 300 void GetSearchProvidersUsingKeywordResult(
315 const WDTypedResult& result, 301 const WDTypedResult& result,
316 KeywordWebDataService* service, 302 KeywordWebDataService* service,
317 PrefService* prefs, 303 PrefService* prefs,
318 TemplateURLService::TemplateURLVector* template_urls, 304 TemplateURLService::OwnedTemplateURLVector* template_urls,
319 TemplateURL* default_search_provider, 305 TemplateURL* default_search_provider,
320 const SearchTermsData& search_terms_data, 306 const SearchTermsData& search_terms_data,
321 int* new_resource_keyword_version, 307 int* new_resource_keyword_version,
322 std::set<std::string>* removed_keyword_guids) { 308 std::set<std::string>* removed_keyword_guids) {
323 DCHECK(template_urls); 309 DCHECK(template_urls);
324 DCHECK(template_urls->empty()); 310 DCHECK(template_urls->empty());
325 DCHECK_EQ(KEYWORDS_RESULT, result.GetType()); 311 DCHECK_EQ(KEYWORDS_RESULT, result.GetType());
326 DCHECK(new_resource_keyword_version); 312 DCHECK(new_resource_keyword_version);
327 313
328 WDKeywordsResult keyword_result = reinterpret_cast< 314 WDKeywordsResult keyword_result = reinterpret_cast<
329 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); 315 const WDResult<WDKeywordsResult>*>(&result)->GetValue();
330 316
331 for (KeywordTable::Keywords::iterator i(keyword_result.keywords.begin()); 317 for (auto& keyword : keyword_result.keywords) {
332 i != keyword_result.keywords.end(); ++i) {
333 // Fix any duplicate encodings in the local database. Note that we don't 318 // Fix any duplicate encodings in the local database. Note that we don't
334 // adjust the last_modified time of this keyword; this way, we won't later 319 // adjust the last_modified time of this keyword; this way, we won't later
335 // overwrite any changes on the sync server that happened to this keyword 320 // overwrite any changes on the sync server that happened to this keyword
336 // since the last time we synced. Instead, we also run a de-duping pass on 321 // since the last time we synced. Instead, we also run a de-duping pass on
337 // the server-provided data in 322 // the server-provided data in
338 // TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData() and 323 // TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData() and
339 // update the server with the merged, de-duped results at that time. We 324 // update the server with the merged, de-duped results at that time. We
340 // still fix here, though, to correct problems in clients that have disabled 325 // still fix here, though, to correct problems in clients that have disabled
341 // search engine sync, since in that case that code will never be reached. 326 // search engine sync, since in that case that code will never be reached.
342 if (DeDupeEncodings(&i->input_encodings) && service) 327 if (DeDupeEncodings(&keyword.input_encodings) && service)
343 service->UpdateKeyword(*i); 328 service->UpdateKeyword(keyword);
344 template_urls->push_back(new TemplateURL(*i)); 329 template_urls->push_back(base::MakeUnique<TemplateURL>(keyword));
345 } 330 }
346 331
347 *new_resource_keyword_version = keyword_result.builtin_keyword_version; 332 *new_resource_keyword_version = keyword_result.builtin_keyword_version;
348 GetSearchProvidersUsingLoadedEngines(service, prefs, template_urls, 333 GetSearchProvidersUsingLoadedEngines(service, prefs, template_urls,
349 default_search_provider, 334 default_search_provider,
350 search_terms_data, 335 search_terms_data,
351 new_resource_keyword_version, 336 new_resource_keyword_version,
352 removed_keyword_guids); 337 removed_keyword_guids);
353 } 338 }
354 339
355 void GetSearchProvidersUsingLoadedEngines( 340 void GetSearchProvidersUsingLoadedEngines(
356 KeywordWebDataService* service, 341 KeywordWebDataService* service,
357 PrefService* prefs, 342 PrefService* prefs,
358 TemplateURLService::TemplateURLVector* template_urls, 343 TemplateURLService::OwnedTemplateURLVector* template_urls,
359 TemplateURL* default_search_provider, 344 TemplateURL* default_search_provider,
360 const SearchTermsData& search_terms_data, 345 const SearchTermsData& search_terms_data,
361 int* resource_keyword_version, 346 int* resource_keyword_version,
362 std::set<std::string>* removed_keyword_guids) { 347 std::set<std::string>* removed_keyword_guids) {
363 DCHECK(template_urls); 348 DCHECK(template_urls);
364 DCHECK(resource_keyword_version); 349 DCHECK(resource_keyword_version);
365 size_t default_search_index; 350 size_t default_search_index;
366 ScopedVector<TemplateURLData> prepopulated_urls = 351 ScopedVector<TemplateURLData> prepopulated_urls =
367 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, 352 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs,
368 &default_search_index); 353 &default_search_index);
(...skipping 17 matching lines...) Expand all
386 std::vector<std::string> deduped_encodings; 371 std::vector<std::string> deduped_encodings;
387 std::set<std::string> encoding_set; 372 std::set<std::string> encoding_set;
388 for (std::vector<std::string>::const_iterator i(encodings->begin()); 373 for (std::vector<std::string>::const_iterator i(encodings->begin());
389 i != encodings->end(); ++i) { 374 i != encodings->end(); ++i) {
390 if (encoding_set.insert(*i).second) 375 if (encoding_set.insert(*i).second)
391 deduped_encodings.push_back(*i); 376 deduped_encodings.push_back(*i);
392 } 377 }
393 encodings->swap(deduped_encodings); 378 encodings->swap(deduped_encodings);
394 return encodings->size() != deduped_encodings.size(); 379 return encodings->size() != deduped_encodings.size();
395 } 380 }
381
382 TemplateURLService::OwnedTemplateURLVector::iterator FindTemplateURL(
383 TemplateURLService::OwnedTemplateURLVector* urls,
384 const TemplateURL* url) {
385 return std::find_if(urls->begin(), urls->end(),
386 [url](const std::unique_ptr<TemplateURL>& ptr) {
387 return ptr.get() == url;
388 });
389 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698