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

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

Issue 2290503003: Remove use of stl_util in search_engines. (Closed)
Patch Set: ios for reals 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/template_url_service.h" 5 #include "components/search_engines/template_url_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/auto_reset.h" 10 #include "base/auto_reset.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/guid.h" 13 #include "base/guid.h"
14 #include "base/i18n/case_conversion.h" 14 #include "base/i18n/case_conversion.h"
15 #include "base/memory/ptr_util.h"
15 #include "base/memory/scoped_vector.h" 16 #include "base/memory/scoped_vector.h"
16 #include "base/metrics/histogram_macros.h" 17 #include "base/metrics/histogram_macros.h"
17 #include "base/profiler/scoped_tracker.h" 18 #include "base/profiler/scoped_tracker.h"
18 #include "base/stl_util.h"
19 #include "base/strings/string_split.h" 19 #include "base/strings/string_split.h"
20 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
21 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
22 #include "base/time/default_clock.h" 22 #include "base/time/default_clock.h"
23 #include "base/time/time.h" 23 #include "base/time/time.h"
24 #include "components/omnibox/browser/omnibox_field_trial.h" 24 #include "components/omnibox/browser/omnibox_field_trial.h"
25 #include "components/pref_registry/pref_registry_syncable.h" 25 #include "components/pref_registry/pref_registry_syncable.h"
26 #include "components/prefs/pref_service.h" 26 #include "components/prefs/pref_service.h"
27 #include "components/rappor/rappor_service.h" 27 #include "components/rappor/rappor_service.h"
28 #include "components/search_engines/search_engines_pref_names.h" 28 #include "components/search_engines/search_engines_pref_names.h"
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 const TemplateURL* turl) { 175 const TemplateURL* turl) {
176 if (OmniboxFieldTrial::KeywordRequiresRegistry()) 176 if (OmniboxFieldTrial::KeywordRequiresRegistry())
177 return keyword.length(); 177 return keyword.length();
178 const size_t registry_length = GetRegistryLength(keyword); 178 const size_t registry_length = GetRegistryLength(keyword);
179 if (registry_length == std::string::npos) 179 if (registry_length == std::string::npos)
180 return keyword.length(); 180 return keyword.length();
181 DCHECK_LT(registry_length, keyword.length()); 181 DCHECK_LT(registry_length, keyword.length());
182 // The meaningful keyword length is the length of any portion before the 182 // The meaningful keyword length is the length of any portion before the
183 // registry ("co.uk") and its preceding dot. 183 // registry ("co.uk") and its preceding dot.
184 return keyword.length() - (registry_length ? (registry_length + 1) : 0); 184 return keyword.length() - (registry_length ? (registry_length + 1) : 0);
185 }
185 186
187 bool Contains(TemplateURLService::OwnedTemplateURLVector* template_urls,
188 TemplateURL* turl) {
189 return FindTemplateURL(template_urls, turl) != template_urls->end();
186 } 190 }
187 191
188 } // namespace 192 } // namespace
189 193
190 // TemplateURLService::LessWithPrefix ----------------------------------------- 194 // TemplateURLService::LessWithPrefix -----------------------------------------
191 195
192 class TemplateURLService::LessWithPrefix { 196 class TemplateURLService::LessWithPrefix {
193 public: 197 public:
194 // We want to find the set of keywords that begin with a prefix. The STL 198 // We want to find the set of keywords that begin with a prefix. The STL
195 // algorithms will return the set of elements that are "equal to" the 199 // algorithms will return the set of elements that are "equal to" the
196 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When 200 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When
197 // cmp() is the typical std::less<>, this results in lexicographic equality; 201 // cmp() is the typical std::less<>, this results in lexicographic equality;
198 // we need to extend this to mark a prefix as "not less than" a keyword it 202 // we need to extend this to mark a prefix as "not less than" a keyword it
199 // begins, which will cause the desired elements to be considered "equal to" 203 // begins, which will cause the desired elements to be considered "equal to"
200 // the prefix. Note: this is still a strict weak ordering, as required by 204 // the prefix. Note: this is still a strict weak ordering, as required by
201 // equal_range() (though I will not prove that here). 205 // equal_range() (though I will not prove that here).
202 // 206 //
203 // Unfortunately the calling convention is not "prefix and element" but 207 // Unfortunately the calling convention is not "prefix and element" but
204 // rather "two elements", so we pass the prefix as a fake "element" which has 208 // rather "two elements", so we pass the prefix as a fake "element" which has
205 // a NULL KeywordDataElement pointer. 209 // a NULL KeywordDataElement pointer.
206 bool operator()( 210 bool operator()(
207 const KeywordToTURLAndMeaningfulLength::value_type& elem1, 211 const KeywordToTURLAndMeaningfulLength::value_type& elem1,
208 const KeywordToTURLAndMeaningfulLength::value_type& elem2) const { 212 const KeywordToTURLAndMeaningfulLength::value_type& elem2) const {
209 return (elem1.second.first == NULL) ? 213 return (elem1.second.first == nullptr)
210 (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) : 214 ? (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0)
211 (elem1.first < elem2.first); 215 : (elem1.first < elem2.first);
212 } 216 }
213 }; 217 };
214 218
215 219
216 // TemplateURLService --------------------------------------------------------- 220 // TemplateURLService ---------------------------------------------------------
217 221
218 TemplateURLService::TemplateURLService( 222 TemplateURLService::TemplateURLService(
219 PrefService* prefs, 223 PrefService* prefs,
220 std::unique_ptr<SearchTermsData> search_terms_data, 224 std::unique_ptr<SearchTermsData> search_terms_data,
221 const scoped_refptr<KeywordWebDataService>& web_data_service, 225 const scoped_refptr<KeywordWebDataService>& web_data_service,
222 std::unique_ptr<TemplateURLServiceClient> client, 226 std::unique_ptr<TemplateURLServiceClient> client,
223 GoogleURLTracker* google_url_tracker, 227 GoogleURLTracker* google_url_tracker,
224 rappor::RapporService* rappor_service, 228 rappor::RapporService* rappor_service,
225 const base::Closure& dsp_change_callback) 229 const base::Closure& dsp_change_callback)
226 : prefs_(prefs), 230 : prefs_(prefs),
227 search_terms_data_(std::move(search_terms_data)), 231 search_terms_data_(std::move(search_terms_data)),
228 web_data_service_(web_data_service), 232 web_data_service_(web_data_service),
229 client_(std::move(client)), 233 client_(std::move(client)),
230 google_url_tracker_(google_url_tracker), 234 google_url_tracker_(google_url_tracker),
231 rappor_service_(rappor_service), 235 rappor_service_(rappor_service),
232 dsp_change_callback_(dsp_change_callback), 236 dsp_change_callback_(dsp_change_callback),
233 provider_map_(new SearchHostToURLsMap), 237 provider_map_(new SearchHostToURLsMap),
234 loaded_(false), 238 loaded_(false),
235 load_failed_(false), 239 load_failed_(false),
236 disable_load_(false), 240 disable_load_(false),
237 load_handle_(0), 241 load_handle_(0),
238 default_search_provider_(NULL), 242 default_search_provider_(nullptr),
239 next_id_(kInvalidTemplateURLID + 1), 243 next_id_(kInvalidTemplateURLID + 1),
240 clock_(new base::DefaultClock), 244 clock_(new base::DefaultClock),
241 models_associated_(false), 245 models_associated_(false),
242 processing_syncer_changes_(false), 246 processing_syncer_changes_(false),
243 dsp_change_origin_(DSP_CHANGE_OTHER), 247 dsp_change_origin_(DSP_CHANGE_OTHER),
244 default_search_manager_( 248 default_search_manager_(
245 prefs_, 249 prefs_,
246 base::Bind(&TemplateURLService::OnDefaultSearchChange, 250 base::Bind(&TemplateURLService::OnDefaultSearchChange,
247 base::Unretained(this))) { 251 base::Unretained(this))) {
248 DCHECK(search_terms_data_); 252 DCHECK(search_terms_data_);
249 Init(NULL, 0); 253 Init(nullptr, 0);
250 } 254 }
251 255
252 TemplateURLService::TemplateURLService(const Initializer* initializers, 256 TemplateURLService::TemplateURLService(const Initializer* initializers,
253 const int count) 257 const int count)
254 : prefs_(NULL), 258 : prefs_(nullptr),
255 search_terms_data_(new SearchTermsData), 259 search_terms_data_(new SearchTermsData),
256 web_data_service_(NULL), 260 web_data_service_(nullptr),
257 google_url_tracker_(NULL), 261 google_url_tracker_(nullptr),
258 rappor_service_(NULL), 262 rappor_service_(nullptr),
259 provider_map_(new SearchHostToURLsMap), 263 provider_map_(new SearchHostToURLsMap),
260 loaded_(false), 264 loaded_(false),
261 load_failed_(false), 265 load_failed_(false),
262 disable_load_(false), 266 disable_load_(false),
263 load_handle_(0), 267 load_handle_(0),
264 default_search_provider_(NULL), 268 default_search_provider_(nullptr),
265 next_id_(kInvalidTemplateURLID + 1), 269 next_id_(kInvalidTemplateURLID + 1),
266 clock_(new base::DefaultClock), 270 clock_(new base::DefaultClock),
267 models_associated_(false), 271 models_associated_(false),
268 processing_syncer_changes_(false), 272 processing_syncer_changes_(false),
269 dsp_change_origin_(DSP_CHANGE_OTHER), 273 dsp_change_origin_(DSP_CHANGE_OTHER),
270 default_search_manager_( 274 default_search_manager_(
271 prefs_, 275 prefs_,
272 base::Bind(&TemplateURLService::OnDefaultSearchChange, 276 base::Bind(&TemplateURLService::OnDefaultSearchChange,
273 base::Unretained(this))) { 277 base::Unretained(this))) {
274 Init(initializers, count); 278 Init(initializers, count);
275 } 279 }
276 280
277 TemplateURLService::~TemplateURLService() { 281 TemplateURLService::~TemplateURLService() {
278 // |web_data_service_| should be deleted during Shutdown(). 282 // |web_data_service_| should be deleted during Shutdown().
279 DCHECK(!web_data_service_.get()); 283 DCHECK(!web_data_service_.get());
280 base::STLDeleteElements(&template_urls_);
281 } 284 }
282 285
283 // static 286 // static
284 void TemplateURLService::RegisterProfilePrefs( 287 void TemplateURLService::RegisterProfilePrefs(
285 user_prefs::PrefRegistrySyncable* registry) { 288 user_prefs::PrefRegistrySyncable* registry) {
286 #if defined(OS_IOS) || defined(OS_ANDROID) 289 #if defined(OS_IOS) || defined(OS_ANDROID)
287 uint32_t flags = PrefRegistry::NO_REGISTRATION_FLAGS; 290 uint32_t flags = PrefRegistry::NO_REGISTRATION_FLAGS;
288 #else 291 #else
289 uint32_t flags = user_prefs::PrefRegistrySyncable::SYNCABLE_PREF; 292 uint32_t flags = user_prefs::PrefRegistrySyncable::SYNCABLE_PREF;
290 #endif 293 #endif
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 keyword_domain_to_turl_and_length_, prefix, supports_replacement_only, 406 keyword_domain_to_turl_and_length_, prefix, supports_replacement_only,
404 matches); 407 matches);
405 } 408 }
406 409
407 TemplateURL* TemplateURLService::GetTemplateURLForKeyword( 410 TemplateURL* TemplateURLService::GetTemplateURLForKeyword(
408 const base::string16& keyword) { 411 const base::string16& keyword) {
409 KeywordToTURLAndMeaningfulLength::const_iterator elem( 412 KeywordToTURLAndMeaningfulLength::const_iterator elem(
410 keyword_to_turl_and_length_.find(keyword)); 413 keyword_to_turl_and_length_.find(keyword));
411 if (elem != keyword_to_turl_and_length_.end()) 414 if (elem != keyword_to_turl_and_length_.end())
412 return elem->second.first; 415 return elem->second.first;
413 return (!loaded_ && 416 return (!loaded_ && initial_default_search_provider_.get() &&
Peter Kasting 2016/08/31 04:12:56 Nit: Can this .get() be removed?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
414 initial_default_search_provider_.get() && 417 (initial_default_search_provider_->keyword() == keyword))
415 (initial_default_search_provider_->keyword() == keyword)) ? 418 ? initial_default_search_provider_.get()
416 initial_default_search_provider_.get() : NULL; 419 : nullptr;
417 } 420 }
418 421
419 TemplateURL* TemplateURLService::GetTemplateURLForGUID( 422 TemplateURL* TemplateURLService::GetTemplateURLForGUID(
420 const std::string& sync_guid) { 423 const std::string& sync_guid) {
421 GUIDToTURL::const_iterator elem(guid_to_turl_.find(sync_guid)); 424 GUIDToTURL::const_iterator elem(guid_to_turl_.find(sync_guid));
422 if (elem != guid_to_turl_.end()) 425 if (elem != guid_to_turl_.end())
423 return elem->second; 426 return elem->second;
424 return (!loaded_ && 427 return (!loaded_ && initial_default_search_provider_.get() &&
Peter Kasting 2016/08/31 04:12:57 Nit: Can this .get() be removed?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
425 initial_default_search_provider_.get() && 428 (initial_default_search_provider_->sync_guid() == sync_guid))
426 (initial_default_search_provider_->sync_guid() == sync_guid)) ? 429 ? initial_default_search_provider_.get()
427 initial_default_search_provider_.get() : NULL; 430 : nullptr;
428 } 431 }
429 432
430 TemplateURL* TemplateURLService::GetTemplateURLForHost( 433 TemplateURL* TemplateURLService::GetTemplateURLForHost(
431 const std::string& host) { 434 const std::string& host) {
432 if (loaded_) 435 if (loaded_)
433 return provider_map_->GetTemplateURLForHost(host); 436 return provider_map_->GetTemplateURLForHost(host);
434 TemplateURL* initial_dsp = initial_default_search_provider_.get(); 437 TemplateURL* initial_dsp = initial_default_search_provider_.get();
435 if (!initial_dsp) 438 if (!initial_dsp)
436 return NULL; 439 return nullptr;
437 return (initial_dsp->GenerateSearchURL(search_terms_data()).host() == host) ? 440 return (initial_dsp->GenerateSearchURL(search_terms_data()).host() == host)
438 initial_dsp : NULL; 441 ? initial_dsp
442 : nullptr;
Peter Kasting 2016/08/31 04:12:57 Nit: Simpler: return (initial_dsp &&
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
439 } 443 }
440 444
441 bool TemplateURLService::Add(TemplateURL* template_url) { 445 bool TemplateURLService::Add(std::unique_ptr<TemplateURL> template_url) {
442 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 446 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
443 if (!AddNoNotify(template_url, true)) 447 if (!AddNoNotify(std::move(template_url), true))
444 return false; 448 return false;
445 NotifyObservers(); 449 NotifyObservers();
446 return true; 450 return true;
447 } 451 }
448 452
449 void TemplateURLService::AddWithOverrides(TemplateURL* template_url, 453 void TemplateURLService::AddWithOverrides(
450 const base::string16& short_name, 454 std::unique_ptr<TemplateURL> template_url,
451 const base::string16& keyword, 455 const base::string16& short_name,
452 const std::string& url) { 456 const base::string16& keyword,
457 const std::string& url) {
453 DCHECK(!short_name.empty()); 458 DCHECK(!short_name.empty());
454 DCHECK(!keyword.empty()); 459 DCHECK(!keyword.empty());
455 DCHECK(!url.empty()); 460 DCHECK(!url.empty());
456 template_url->data_.SetShortName(short_name); 461 template_url->data_.SetShortName(short_name);
457 template_url->data_.SetKeyword(keyword); 462 template_url->data_.SetKeyword(keyword);
458 template_url->SetURL(url); 463 template_url->SetURL(url);
459 Add(template_url); 464 Add(std::move(template_url));
460 } 465 }
461 466
462 void TemplateURLService::AddExtensionControlledTURL( 467 void TemplateURLService::AddExtensionControlledTURL(
463 TemplateURL* template_url, 468 std::unique_ptr<TemplateURL> template_url,
464 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) { 469 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) {
465 DCHECK(loaded_); 470 DCHECK(loaded_);
466 DCHECK(template_url); 471 DCHECK(template_url.get());
Peter Kasting 2016/08/31 04:12:57 Nit: I'm surprised this is necessary.
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
467 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); 472 DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
468 DCHECK(info); 473 DCHECK(info);
469 DCHECK_NE(TemplateURL::NORMAL, info->type); 474 DCHECK_NE(TemplateURL::NORMAL, info->type);
470 DCHECK_EQ(info->wants_to_be_default_engine, 475 DCHECK_EQ(info->wants_to_be_default_engine,
471 template_url->show_in_default_list()); 476 template_url->show_in_default_list());
472 DCHECK(!FindTemplateURLForExtension(info->extension_id, info->type)); 477 DCHECK(!FindTemplateURLForExtension(info->extension_id, info->type));
473 template_url->extension_info_.swap(info); 478 template_url->extension_info_.swap(info);
474 479
475 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 480 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
476 if (AddNoNotify(template_url, true)) { 481 bool wants_to_be_default_engine =
477 if (template_url->extension_info_->wants_to_be_default_engine) 482 template_url->extension_info_->wants_to_be_default_engine;
Peter Kasting 2016/08/31 04:12:57 Nit: Might make sense to modify AddNoNotify() to r
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
483 if (AddNoNotify(std::move(template_url), true)) {
484 if (wants_to_be_default_engine)
478 UpdateExtensionDefaultSearchEngine(); 485 UpdateExtensionDefaultSearchEngine();
479 NotifyObservers(); 486 NotifyObservers();
480 } 487 }
481 } 488 }
482 489
483 void TemplateURLService::Remove(TemplateURL* template_url) { 490 void TemplateURLService::Remove(TemplateURL* template_url) {
484 RemoveNoNotify(template_url); 491 RemoveNoNotify(template_url);
485 NotifyObservers(); 492 NotifyObservers();
486 } 493 }
487 494
488 void TemplateURLService::RemoveExtensionControlledTURL( 495 void TemplateURLService::RemoveExtensionControlledTURL(
489 const std::string& extension_id, 496 const std::string& extension_id,
490 TemplateURL::Type type) { 497 TemplateURL::Type type) {
491 DCHECK(loaded_); 498 DCHECK(loaded_);
492 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); 499 TemplateURL* url = FindTemplateURLForExtension(extension_id, type);
493 if (!url) 500 if (!url)
494 return; 501 return;
495 // NULL this out so that we can call RemoveNoNotify. 502 // NULL this out so that we can call RemoveNoNotify.
496 // UpdateExtensionDefaultSearchEngine will cause it to be reset. 503 // UpdateExtensionDefaultSearchEngine will cause it to be reset.
497 if (default_search_provider_ == url) 504 if (default_search_provider_ == url)
498 default_search_provider_ = NULL; 505 default_search_provider_ = nullptr;
499 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 506 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
500 RemoveNoNotify(url); 507 RemoveNoNotify(url);
501 UpdateExtensionDefaultSearchEngine(); 508 UpdateExtensionDefaultSearchEngine();
502 NotifyObservers(); 509 NotifyObservers();
503 } 510 }
504 511
505 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { 512 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) {
506 RemoveAutoGeneratedBetween(created_after, base::Time()); 513 RemoveAutoGeneratedBetween(created_after, base::Time());
507 } 514 }
508 515
509 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, 516 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after,
510 base::Time created_before) { 517 base::Time created_before) {
511 RemoveAutoGeneratedForOriginBetween(GURL(), created_after, created_before); 518 RemoveAutoGeneratedForOriginBetween(GURL(), created_after, created_before);
512 } 519 }
513 520
514 void TemplateURLService::RemoveAutoGeneratedForOriginBetween( 521 void TemplateURLService::RemoveAutoGeneratedForOriginBetween(
515 const GURL& origin, 522 const GURL& origin,
516 base::Time created_after, 523 base::Time created_after,
517 base::Time created_before) { 524 base::Time created_before) {
518 GURL o(origin.GetOrigin()); 525 GURL o(origin.GetOrigin());
519 bool should_notify = false; 526 bool should_notify = false;
520 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 527 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
521 for (size_t i = 0; i < template_urls_.size();) { 528 for (size_t i = 0; i < template_urls_.size();) {
522 if (template_urls_[i]->date_created() >= created_after && 529 if (template_urls_[i]->date_created() >= created_after &&
523 (created_before.is_null() || 530 (created_before.is_null() ||
524 template_urls_[i]->date_created() < created_before) && 531 template_urls_[i]->date_created() < created_before) &&
525 CanReplace(template_urls_[i]) && 532 CanReplace(template_urls_[i].get()) &&
526 (o.is_empty() || 533 (o.is_empty() ||
527 template_urls_[i]->GenerateSearchURL( 534 template_urls_[i]
528 search_terms_data()).GetOrigin() == o)) { 535 ->GenerateSearchURL(search_terms_data())
529 RemoveNoNotify(template_urls_[i]); 536 .GetOrigin() == o)) {
537 RemoveNoNotify(template_urls_[i].get());
530 should_notify = true; 538 should_notify = true;
531 } else { 539 } else {
532 ++i; 540 ++i;
533 } 541 }
534 } 542 }
535 if (should_notify) 543 if (should_notify)
536 NotifyObservers(); 544 NotifyObservers();
537 } 545 }
538 546
539 void TemplateURLService::RegisterOmniboxKeyword( 547 void TemplateURLService::RegisterOmniboxKeyword(
540 const std::string& extension_id, 548 const std::string& extension_id,
541 const std::string& extension_name, 549 const std::string& extension_name,
542 const std::string& keyword, 550 const std::string& keyword,
543 const std::string& template_url_string) { 551 const std::string& template_url_string) {
544 DCHECK(loaded_); 552 DCHECK(loaded_);
545 553
546 if (FindTemplateURLForExtension(extension_id, 554 if (FindTemplateURLForExtension(extension_id,
547 TemplateURL::OMNIBOX_API_EXTENSION)) 555 TemplateURL::OMNIBOX_API_EXTENSION))
548 return; 556 return;
549 557
550 TemplateURLData data; 558 TemplateURLData data;
551 data.SetShortName(base::UTF8ToUTF16(extension_name)); 559 data.SetShortName(base::UTF8ToUTF16(extension_name));
552 data.SetKeyword(base::UTF8ToUTF16(keyword)); 560 data.SetKeyword(base::UTF8ToUTF16(keyword));
553 data.SetURL(template_url_string); 561 data.SetURL(template_url_string);
554 TemplateURL* url = new TemplateURL(data); 562 std::unique_ptr<TemplateURL> turl = base::MakeUnique<TemplateURL>(data);
Peter Kasting 2016/08/31 04:12:57 Nit: Can just inline below.
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
555 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info( 563 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info(
556 new TemplateURL::AssociatedExtensionInfo( 564 new TemplateURL::AssociatedExtensionInfo(
557 TemplateURL::OMNIBOX_API_EXTENSION, extension_id)); 565 TemplateURL::OMNIBOX_API_EXTENSION, extension_id));
558 AddExtensionControlledTURL(url, std::move(info)); 566 AddExtensionControlledTURL(std::move(turl), std::move(info));
559 } 567 }
560 568
561 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() { 569 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() {
562 return template_urls_; 570 TemplateURLVector result;
571 for (const auto& turl : template_urls_)
572 result.push_back(turl.get());
573 return result;
563 } 574 }
564 575
565 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { 576 void TemplateURLService::IncrementUsageCount(TemplateURL* url) {
566 DCHECK(url); 577 DCHECK(url);
567 // Extension-controlled search engines are not persisted. 578 // Extension-controlled search engines are not persisted.
568 if (url->GetType() != TemplateURL::NORMAL) 579 if (url->GetType() != TemplateURL::NORMAL)
569 return; 580 return;
570 if (std::find(template_urls_.begin(), template_urls_.end(), url) == 581 if (!Contains(&template_urls_, url))
571 template_urls_.end())
572 return; 582 return;
573 ++url->data_.usage_count; 583 ++url->data_.usage_count;
574 584
575 if (web_data_service_.get()) 585 if (web_data_service_.get())
576 web_data_service_->UpdateKeyword(url->data()); 586 web_data_service_->UpdateKeyword(url->data());
577 } 587 }
578 588
579 void TemplateURLService::ResetTemplateURL(TemplateURL* url, 589 void TemplateURLService::ResetTemplateURL(TemplateURL* url,
580 const base::string16& title, 590 const base::string16& title,
581 const base::string16& keyword, 591 const base::string16& keyword,
(...skipping 16 matching lines...) Expand all
598 TemplateURL* url) { 608 TemplateURL* url) {
599 // Omnibox keywords cannot be made default. Extension-controlled search 609 // Omnibox keywords cannot be made default. Extension-controlled search
600 // engines can be made default only by the extension itself because they 610 // engines can be made default only by the extension itself because they
601 // aren't persisted. 611 // aren't persisted.
602 DCHECK(!url || (url->GetType() == TemplateURL::NORMAL)); 612 DCHECK(!url || (url->GetType() == TemplateURL::NORMAL));
603 if (load_failed_) { 613 if (load_failed_) {
604 // Skip the DefaultSearchManager, which will persist to user preferences. 614 // Skip the DefaultSearchManager, which will persist to user preferences.
605 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || 615 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) ||
606 (default_search_provider_source_ == 616 (default_search_provider_source_ ==
607 DefaultSearchManager::FROM_FALLBACK)) { 617 DefaultSearchManager::FROM_FALLBACK)) {
608 ApplyDefaultSearchChange(url ? &url->data() : NULL, 618 ApplyDefaultSearchChange(url ? &url->data() : nullptr,
609 DefaultSearchManager::FROM_USER); 619 DefaultSearchManager::FROM_USER);
610 } 620 }
611 } else { 621 } else {
612 // We rely on the DefaultSearchManager to call OnDefaultSearchChange if, in 622 // We rely on the DefaultSearchManager to call OnDefaultSearchChange if, in
613 // fact, the effective DSE changes. 623 // fact, the effective DSE changes.
614 if (url) 624 if (url)
615 default_search_manager_.SetUserSelectedDefaultSearchEngine(url->data()); 625 default_search_manager_.SetUserSelectedDefaultSearchEngine(url->data());
616 else 626 else
617 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); 627 default_search_manager_.ClearUserSelectedDefaultSearchEngine();
618 } 628 }
(...skipping 23 matching lines...) Expand all
642 652
643 void TemplateURLService::RepairPrepopulatedSearchEngines() { 653 void TemplateURLService::RepairPrepopulatedSearchEngines() {
644 // Can't clean DB if it hasn't been loaded. 654 // Can't clean DB if it hasn't been loaded.
645 DCHECK(loaded()); 655 DCHECK(loaded());
646 656
647 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || 657 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) ||
648 (default_search_provider_source_ == 658 (default_search_provider_source_ ==
649 DefaultSearchManager::FROM_FALLBACK)) { 659 DefaultSearchManager::FROM_FALLBACK)) {
650 // Clear |default_search_provider_| in case we want to remove the engine it 660 // Clear |default_search_provider_| in case we want to remove the engine it
651 // points to. This will get reset at the end of the function anyway. 661 // points to. This will get reset at the end of the function anyway.
652 default_search_provider_ = NULL; 662 default_search_provider_ = nullptr;
653 } 663 }
654 664
655 size_t default_search_provider_index = 0; 665 size_t default_search_provider_index = 0;
656 ScopedVector<TemplateURLData> prepopulated_urls = 666 ScopedVector<TemplateURLData> prepopulated_urls =
657 TemplateURLPrepopulateData::GetPrepopulatedEngines( 667 TemplateURLPrepopulateData::GetPrepopulatedEngines(
658 prefs_, &default_search_provider_index); 668 prefs_, &default_search_provider_index);
659 DCHECK(!prepopulated_urls.empty()); 669 DCHECK(!prepopulated_urls.empty());
660 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData( 670 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData(
661 &prepopulated_urls, template_urls_, default_search_provider_)); 671 &prepopulated_urls, template_urls_, default_search_provider_));
662 672
663 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 673 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
664 674
665 // Remove items. 675 // Remove items.
666 for (std::vector<TemplateURL*>::iterator i = actions.removed_engines.begin(); 676 for (std::vector<TemplateURL*>::iterator i = actions.removed_engines.begin();
667 i < actions.removed_engines.end(); ++i) 677 i < actions.removed_engines.end(); ++i)
668 RemoveNoNotify(*i); 678 RemoveNoNotify(*i);
669 679
670 // Edit items. 680 // Edit items.
671 for (EditedEngines::iterator i(actions.edited_engines.begin()); 681 for (EditedEngines::iterator i(actions.edited_engines.begin());
672 i < actions.edited_engines.end(); ++i) { 682 i < actions.edited_engines.end(); ++i) {
673 TemplateURL new_values(i->second); 683 TemplateURL new_values(i->second);
674 UpdateNoNotify(i->first, new_values); 684 UpdateNoNotify(i->first, new_values);
675 } 685 }
676 686
677 // Add items. 687 // Add items.
678 for (std::vector<TemplateURLData>::const_iterator i = 688 for (std::vector<TemplateURLData>::const_iterator i =
679 actions.added_engines.begin(); 689 actions.added_engines.begin();
680 i < actions.added_engines.end(); 690 i < actions.added_engines.end();
681 ++i) { 691 ++i) {
682 AddNoNotify(new TemplateURL(*i), true); 692 AddNoNotify(base::MakeUnique<TemplateURL>(*i), true);
683 } 693 }
684 694
685 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 695 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
686 &dsp_change_origin_, DSP_CHANGE_PROFILE_RESET); 696 &dsp_change_origin_, DSP_CHANGE_PROFILE_RESET);
687 697
688 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); 698 default_search_manager_.ClearUserSelectedDefaultSearchEngine();
689 699
690 if (!default_search_provider_) { 700 if (!default_search_provider_) {
691 // If the default search provider came from a user pref we would have been 701 // If the default search provider came from a user pref we would have been
692 // notified of the new (fallback-provided) value in 702 // notified of the new (fallback-provided) value in
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
743 if (!result) { 753 if (!result) {
744 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 754 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460
745 // is fixed. 755 // is fixed.
746 tracked_objects::ScopedTracker tracking_profile1( 756 tracked_objects::ScopedTracker tracking_profile1(
747 FROM_HERE_WITH_EXPLICIT_FUNCTION( 757 FROM_HERE_WITH_EXPLICIT_FUNCTION(
748 "422460 TemplateURLService::OnWebDataServiceRequestDone 1")); 758 "422460 TemplateURLService::OnWebDataServiceRequestDone 1"));
749 759
750 // Results are null if the database went away or (most likely) wasn't 760 // Results are null if the database went away or (most likely) wasn't
751 // loaded. 761 // loaded.
752 load_failed_ = true; 762 load_failed_ = true;
753 web_data_service_ = NULL; 763 web_data_service_ = nullptr;
754 ChangeToLoadedState(); 764 ChangeToLoadedState();
755 return; 765 return;
756 } 766 }
757 767
758 TemplateURLVector template_urls; 768 std::unique_ptr<OwnedTemplateURLVector> template_urls =
769 base::MakeUnique<OwnedTemplateURLVector>();
759 int new_resource_keyword_version = 0; 770 int new_resource_keyword_version = 0;
760 { 771 {
761 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 772 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460
762 // is fixed. 773 // is fixed.
763 tracked_objects::ScopedTracker tracking_profile2( 774 tracked_objects::ScopedTracker tracking_profile2(
764 FROM_HERE_WITH_EXPLICIT_FUNCTION( 775 FROM_HERE_WITH_EXPLICIT_FUNCTION(
765 "422460 TemplateURLService::OnWebDataServiceRequestDone 2")); 776 "422460 TemplateURLService::OnWebDataServiceRequestDone 2"));
766 777
767 GetSearchProvidersUsingKeywordResult( 778 GetSearchProvidersUsingKeywordResult(
768 *result, web_data_service_.get(), prefs_, &template_urls, 779 *result, web_data_service_.get(), prefs_, template_urls.get(),
769 (default_search_provider_source_ == DefaultSearchManager::FROM_USER) 780 (default_search_provider_source_ == DefaultSearchManager::FROM_USER)
770 ? initial_default_search_provider_.get() 781 ? initial_default_search_provider_.get()
771 : NULL, 782 : nullptr,
772 search_terms_data(), &new_resource_keyword_version, &pre_sync_deletes_); 783 search_terms_data(), &new_resource_keyword_version, &pre_sync_deletes_);
773 } 784 }
774 785
775 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 786 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
776 787
777 { 788 {
778 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 789 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460
779 // is fixed. 790 // is fixed.
780 tracked_objects::ScopedTracker tracking_profile4( 791 tracked_objects::ScopedTracker tracking_profile4(
781 FROM_HERE_WITH_EXPLICIT_FUNCTION( 792 FROM_HERE_WITH_EXPLICIT_FUNCTION(
782 "422460 TemplateURLService::OnWebDataServiceRequestDone 4")); 793 "422460 TemplateURLService::OnWebDataServiceRequestDone 4"));
783 794
784 PatchMissingSyncGUIDs(&template_urls); 795 PatchMissingSyncGUIDs(template_urls.get());
785 796
786 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 797 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460
787 // is fixed. 798 // is fixed.
788 tracked_objects::ScopedTracker tracking_profile41( 799 tracked_objects::ScopedTracker tracking_profile41(
789 FROM_HERE_WITH_EXPLICIT_FUNCTION( 800 FROM_HERE_WITH_EXPLICIT_FUNCTION(
790 "422460 TemplateURLService::OnWebDataServiceRequestDone 41")); 801 "422460 TemplateURLService::OnWebDataServiceRequestDone 41"));
791 802
792 SetTemplateURLs(&template_urls); 803 SetTemplateURLs(std::move(template_urls));
793 804
794 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 805 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460
795 // is fixed. 806 // is fixed.
796 tracked_objects::ScopedTracker tracking_profile42( 807 tracked_objects::ScopedTracker tracking_profile42(
797 FROM_HERE_WITH_EXPLICIT_FUNCTION( 808 FROM_HERE_WITH_EXPLICIT_FUNCTION(
798 "422460 TemplateURLService::OnWebDataServiceRequestDone 42")); 809 "422460 TemplateURLService::OnWebDataServiceRequestDone 42"));
799 810
800 // This initializes provider_map_ which should be done before 811 // This initializes provider_map_ which should be done before
801 // calling UpdateKeywordSearchTermsForURL. 812 // calling UpdateKeywordSearchTermsForURL.
802 // This also calls NotifyObservers. 813 // This also calls NotifyObservers.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 void TemplateURLService::Shutdown() { 883 void TemplateURLService::Shutdown() {
873 if (client_) 884 if (client_)
874 client_->Shutdown(); 885 client_->Shutdown();
875 // This check has to be done at Shutdown() instead of in the dtor to ensure 886 // This check has to be done at Shutdown() instead of in the dtor to ensure
876 // that no clients of KeywordWebDataService are holding ptrs to it after the 887 // that no clients of KeywordWebDataService are holding ptrs to it after the
877 // first phase of the KeyedService Shutdown() process. 888 // first phase of the KeyedService Shutdown() process.
878 if (load_handle_) { 889 if (load_handle_) {
879 DCHECK(web_data_service_.get()); 890 DCHECK(web_data_service_.get());
880 web_data_service_->CancelRequest(load_handle_); 891 web_data_service_->CancelRequest(load_handle_);
881 } 892 }
882 web_data_service_ = NULL; 893 web_data_service_ = nullptr;
883 } 894 }
884 895
885 syncer::SyncDataList TemplateURLService::GetAllSyncData( 896 syncer::SyncDataList TemplateURLService::GetAllSyncData(
886 syncer::ModelType type) const { 897 syncer::ModelType type) const {
887 DCHECK_EQ(syncer::SEARCH_ENGINES, type); 898 DCHECK_EQ(syncer::SEARCH_ENGINES, type);
888 899
889 syncer::SyncDataList current_data; 900 syncer::SyncDataList current_data;
890 for (TemplateURLVector::const_iterator iter = template_urls_.begin(); 901 for (auto& turl : template_urls_) {
Peter Kasting 2016/08/31 04:12:57 Nit: const auto&?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
891 iter != template_urls_.end(); ++iter) {
892 // We don't sync keywords managed by policy. 902 // We don't sync keywords managed by policy.
893 if ((*iter)->created_by_policy()) 903 if (turl->created_by_policy())
894 continue; 904 continue;
895 // We don't sync extension-controlled search engines. 905 // We don't sync extension-controlled search engines.
896 if ((*iter)->GetType() != TemplateURL::NORMAL) 906 if (turl->GetType() != TemplateURL::NORMAL)
897 continue; 907 continue;
898 current_data.push_back(CreateSyncDataFromTemplateURL(**iter)); 908 current_data.push_back(CreateSyncDataFromTemplateURL(*turl));
899 } 909 }
900 910
901 return current_data; 911 return current_data;
902 } 912 }
903 913
904 syncer::SyncError TemplateURLService::ProcessSyncChanges( 914 syncer::SyncError TemplateURLService::ProcessSyncChanges(
905 const tracked_objects::Location& from_here, 915 const tracked_objects::Location& from_here,
906 const syncer::SyncChangeList& change_list) { 916 const syncer::SyncChangeList& change_list) {
907 if (!models_associated_) { 917 if (!models_associated_) {
908 syncer::SyncError error(FROM_HERE, 918 syncer::SyncError error(FROM_HERE,
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
996 if (existing_keyword_turl) { 1006 if (existing_keyword_turl) {
997 // Resolve any conflicts so we can safely add the new entry. 1007 // Resolve any conflicts so we can safely add the new entry.
998 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl, 1008 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl,
999 &new_changes); 1009 &new_changes);
1000 } 1010 }
1001 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 1011 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
1002 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD); 1012 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD);
1003 // Force the local ID to kInvalidTemplateURLID so we can add it. 1013 // Force the local ID to kInvalidTemplateURLID so we can add it.
1004 TemplateURLData data(turl->data()); 1014 TemplateURLData data(turl->data());
1005 data.id = kInvalidTemplateURLID; 1015 data.id = kInvalidTemplateURLID;
1006 TemplateURL* added = new TemplateURL(data); 1016 std::unique_ptr<TemplateURL> added_ptr =
1007 if (Add(added)) 1017 base::MakeUnique<TemplateURL>(data);
1018 TemplateURL* added = added_ptr.get();
1019 if (Add(std::move(added_ptr)))
1008 MaybeUpdateDSEAfterSync(added); 1020 MaybeUpdateDSEAfterSync(added);
1009 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE) { 1021 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE) {
1010 if (!existing_turl) { 1022 if (!existing_turl) {
1011 error = sync_error_factory_->CreateAndUploadError( 1023 error = sync_error_factory_->CreateAndUploadError(
1012 FROM_HERE, 1024 FROM_HERE,
1013 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE"); 1025 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE");
1014 continue; 1026 continue;
1015 } 1027 }
1016 if (existing_keyword_turl && (existing_keyword_turl != existing_turl)) { 1028 if (existing_keyword_turl && (existing_keyword_turl != existing_turl)) {
1017 // Resolve any conflicts with other entries so we can safely update the 1029 // Resolve any conflicts with other entries so we can safely update the
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
1270 1282
1271 // Past bugs might have caused either of these fields to be empty. Just 1283 // Past bugs might have caused either of these fields to be empty. Just
1272 // delete this data off the server. 1284 // delete this data off the server.
1273 if (specifics.url().empty() || specifics.sync_guid().empty()) { 1285 if (specifics.url().empty() || specifics.sync_guid().empty()) {
1274 change_list->push_back( 1286 change_list->push_back(
1275 syncer::SyncChange(FROM_HERE, 1287 syncer::SyncChange(FROM_HERE,
1276 syncer::SyncChange::ACTION_DELETE, 1288 syncer::SyncChange::ACTION_DELETE,
1277 sync_data)); 1289 sync_data));
1278 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, 1290 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
1279 DELETE_ENGINE_EMPTY_FIELD, DELETE_ENGINE_MAX); 1291 DELETE_ENGINE_EMPTY_FIELD, DELETE_ENGINE_MAX);
1280 return NULL; 1292 return nullptr;
1281 } 1293 }
1282 1294
1283 TemplateURLData data(existing_turl ? 1295 TemplateURLData data(existing_turl ?
1284 existing_turl->data() : TemplateURLData()); 1296 existing_turl->data() : TemplateURLData());
1285 data.SetShortName(base::UTF8ToUTF16(specifics.short_name())); 1297 data.SetShortName(base::UTF8ToUTF16(specifics.short_name()));
1286 data.originating_url = GURL(specifics.originating_url()); 1298 data.originating_url = GURL(specifics.originating_url());
1287 base::string16 keyword(base::UTF8ToUTF16(specifics.keyword())); 1299 base::string16 keyword(base::UTF8ToUTF16(specifics.keyword()));
1288 // NOTE: Once this code has shipped in a couple of stable releases, we can 1300 // NOTE: Once this code has shipped in a couple of stable releases, we can
1289 // probably remove the migration portion, comment out the 1301 // probably remove the migration portion, comment out the
1290 // "autogenerate_keyword" field entirely in the .proto file, and fold the 1302 // "autogenerate_keyword" field entirely in the .proto file, and fold the
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1332 1344
1333 // We used to sync keywords associated with omnibox extensions, but no longer 1345 // We used to sync keywords associated with omnibox extensions, but no longer
1334 // want to. However, if we delete these keywords from sync, we'll break any 1346 // want to. However, if we delete these keywords from sync, we'll break any
1335 // synced old versions of Chrome which were relying on them. Instead, for now 1347 // synced old versions of Chrome which were relying on them. Instead, for now
1336 // we simply ignore these. 1348 // we simply ignore these.
1337 // TODO(vasilii): After a few Chrome versions, change this to go ahead and 1349 // TODO(vasilii): After a few Chrome versions, change this to go ahead and
1338 // delete these from sync. 1350 // delete these from sync.
1339 DCHECK(client); 1351 DCHECK(client);
1340 client->RestoreExtensionInfoIfNecessary(turl.get()); 1352 client->RestoreExtensionInfoIfNecessary(turl.get());
1341 if (turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) 1353 if (turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)
1342 return NULL; 1354 return nullptr;
1343 1355
1344 DCHECK_EQ(TemplateURL::NORMAL, turl->GetType()); 1356 DCHECK_EQ(TemplateURL::NORMAL, turl->GetType());
1345 if (reset_keyword || deduped) { 1357 if (reset_keyword || deduped) {
1346 if (reset_keyword) 1358 if (reset_keyword)
1347 turl->ResetKeywordIfNecessary(search_terms_data, true); 1359 turl->ResetKeywordIfNecessary(search_terms_data, true);
1348 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1360 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1349 change_list->push_back(syncer::SyncChange(FROM_HERE, 1361 change_list->push_back(syncer::SyncChange(FROM_HERE,
1350 syncer::SyncChange::ACTION_UPDATE, 1362 syncer::SyncChange::ACTION_UPDATE,
1351 sync_data)); 1363 sync_data));
1352 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword(search_terms_data)) { 1364 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword(search_terms_data)) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1416 DCHECK(initializers[i].keyword); 1428 DCHECK(initializers[i].keyword);
1417 DCHECK(initializers[i].url); 1429 DCHECK(initializers[i].url);
1418 DCHECK(initializers[i].content); 1430 DCHECK(initializers[i].content);
1419 1431
1420 // TemplateURLService ends up owning the TemplateURL, don't try and free 1432 // TemplateURLService ends up owning the TemplateURL, don't try and free
1421 // it. 1433 // it.
1422 TemplateURLData data; 1434 TemplateURLData data;
1423 data.SetShortName(base::UTF8ToUTF16(initializers[i].content)); 1435 data.SetShortName(base::UTF8ToUTF16(initializers[i].content));
1424 data.SetKeyword(base::UTF8ToUTF16(initializers[i].keyword)); 1436 data.SetKeyword(base::UTF8ToUTF16(initializers[i].keyword));
1425 data.SetURL(initializers[i].url); 1437 data.SetURL(initializers[i].url);
1426 TemplateURL* template_url = new TemplateURL(data); 1438 AddNoNotify(base::MakeUnique<TemplateURL>(data), true);
1427 AddNoNotify(template_url, true);
1428 1439
1429 // Set the first provided identifier to be the default. 1440 // Set the first provided identifier to be the default.
1430 if (i == 0) 1441 if (i == 0)
1431 default_search_manager_.SetUserSelectedDefaultSearchEngine(data); 1442 default_search_manager_.SetUserSelectedDefaultSearchEngine(data);
1432 } 1443 }
1433 } 1444 }
1434 1445
1435 // Request a server check for the correct Google URL if Google is the 1446 // Request a server check for the correct Google URL if Google is the
1436 // default search engine. 1447 // default search engine.
1437 RequestGoogleURLTrackerServerCheckIfNecessary(); 1448 RequestGoogleURLTrackerServerCheckIfNecessary();
1438 } 1449 }
1439 1450
1440 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { 1451 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) {
1441 const base::string16& keyword = template_url->keyword(); 1452 const base::string16& keyword = template_url->keyword();
1442 DCHECK_NE(0U, keyword_to_turl_and_length_.count(keyword)); 1453 DCHECK_NE(0U, keyword_to_turl_and_length_.count(keyword));
1443 if (keyword_to_turl_and_length_[keyword].first == template_url) { 1454 if (keyword_to_turl_and_length_[keyword].first == template_url) {
1444 // We need to check whether the keyword can now be provided by another 1455 // We need to check whether the keyword can now be provided by another
1445 // TemplateURL. See the comments in AddToMaps() for more information on 1456 // TemplateURL. See the comments in AddToMaps() for more information on
1446 // extension keywords and how they can coexist with non-extension keywords. 1457 // extension keywords and how they can coexist with non-extension keywords.
1447 // In the case of more than one extension, we use the most recently 1458 // In the case of more than one extension, we use the most recently
1448 // installed (which will be the most recently added, which will have the 1459 // installed (which will be the most recently added, which will have the
1449 // highest ID). 1460 // highest ID).
1450 TemplateURL* best_fallback = NULL; 1461 TemplateURL* best_fallback = nullptr;
1451 for (TemplateURLVector::const_iterator i(template_urls_.begin()); 1462 for (auto& turl : template_urls_) {
Peter Kasting 2016/08/31 04:12:56 Nit: const auto&?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
1452 i != template_urls_.end(); ++i) {
1453 TemplateURL* turl = *i;
1454 // This next statement relies on the fact that there can only be one 1463 // This next statement relies on the fact that there can only be one
1455 // non-Omnibox API TemplateURL with a given keyword. 1464 // non-Omnibox API TemplateURL with a given keyword.
1456 if ((turl != template_url) && (turl->keyword() == keyword) && 1465 if ((turl.get() != template_url) && (turl->keyword() == keyword) &&
1457 (!best_fallback || 1466 (!best_fallback ||
1458 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) || 1467 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) ||
1459 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && 1468 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) &&
1460 (turl->id() > best_fallback->id())))) 1469 (turl->id() > best_fallback->id()))))
1461 best_fallback = turl; 1470 best_fallback = turl.get();
1462 } 1471 }
1463 RemoveFromDomainMap(template_url); 1472 RemoveFromDomainMap(template_url);
1464 if (best_fallback) { 1473 if (best_fallback) {
1465 AddToMap(best_fallback); 1474 AddToMap(best_fallback);
1466 AddToDomainMap(best_fallback); 1475 AddToDomainMap(best_fallback);
1467 } else { 1476 } else {
1468 keyword_to_turl_and_length_.erase(keyword); 1477 keyword_to_turl_and_length_.erase(keyword);
1469 } 1478 }
1470 } 1479 }
1471 1480
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1544 } 1553 }
1545 1554
1546 void TemplateURLService::AddToMap(TemplateURL* template_url) { 1555 void TemplateURLService::AddToMap(TemplateURL* template_url) {
1547 const base::string16& keyword = template_url->keyword(); 1556 const base::string16& keyword = template_url->keyword();
1548 keyword_to_turl_and_length_[keyword] = 1557 keyword_to_turl_and_length_[keyword] =
1549 TURLAndMeaningfulLength( 1558 TURLAndMeaningfulLength(
1550 template_url, GetMeaningfulKeywordLength(keyword, template_url)); 1559 template_url, GetMeaningfulKeywordLength(keyword, template_url));
1551 } 1560 }
1552 1561
1553 // Helper for partition() call in next function. 1562 // Helper for partition() call in next function.
1554 bool HasValidID(TemplateURL* t_url) { 1563 bool HasValidID(const std::unique_ptr<TemplateURL>& t_url) {
Peter Kasting 2016/08/31 04:12:57 Nit: Perhaps this should just be inlined below as
Avi (use Gerrit) 2016/09/01 00:34:27 C++11!
1555 return t_url->id() != kInvalidTemplateURLID; 1564 return t_url->id() != kInvalidTemplateURLID;
1556 } 1565 }
1557 1566
1558 void TemplateURLService::SetTemplateURLs(TemplateURLVector* urls) { 1567 void TemplateURLService::SetTemplateURLs(
1568 std::unique_ptr<OwnedTemplateURLVector> urls) {
1559 // Partition the URLs first, instead of implementing the loops below by simply 1569 // Partition the URLs first, instead of implementing the loops below by simply
1560 // scanning the input twice. While it's not supposed to happen normally, it's 1570 // scanning the input twice. While it's not supposed to happen normally, it's
1561 // possible for corrupt databases to return multiple entries with the same 1571 // possible for corrupt databases to return multiple entries with the same
1562 // keyword. In this case, the first loop may delete the first entry when 1572 // keyword. In this case, the first loop may delete the first entry when
1563 // adding the second. If this happens, the second loop must not attempt to 1573 // adding the second. If this happens, the second loop must not attempt to
1564 // access the deleted entry. Partitioning ensures this constraint. 1574 // access the deleted entry. Partitioning ensures this constraint.
1565 TemplateURLVector::iterator first_invalid( 1575 auto first_invalid(std::partition(urls->begin(), urls->end(), HasValidID));
1566 std::partition(urls->begin(), urls->end(), HasValidID));
1567 1576
1568 // First, add the items that already have id's, so that the next_id_ gets 1577 // First, add the items that already have id's, so that the next_id_ gets
1569 // properly set. 1578 // properly set.
1570 for (TemplateURLVector::const_iterator i = urls->begin(); i != first_invalid; 1579 for (auto i = urls->begin(); i != first_invalid; ++i) {
1571 ++i) {
1572 next_id_ = std::max(next_id_, (*i)->id()); 1580 next_id_ = std::max(next_id_, (*i)->id());
1573 AddNoNotify(*i, false); 1581 AddNoNotify(std::move(*i), false);
1574 } 1582 }
1575 1583
1576 // Next add the new items that don't have id's. 1584 // Next add the new items that don't have id's.
1577 for (TemplateURLVector::const_iterator i = first_invalid; i != urls->end(); 1585 for (auto i = first_invalid; i != urls->end(); ++i)
1578 ++i) 1586 AddNoNotify(std::move(*i), true);
1579 AddNoNotify(*i, true);
1580
1581 // Clear the input vector to reduce the chance callers will try to use a
1582 // (possibly deleted) entry.
1583 urls->clear();
1584 } 1587 }
1585 1588
1586 void TemplateURLService::ChangeToLoadedState() { 1589 void TemplateURLService::ChangeToLoadedState() {
1587 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is 1590 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
1588 // fixed. 1591 // fixed.
1589 tracked_objects::ScopedTracker tracking_profile1( 1592 tracked_objects::ScopedTracker tracking_profile1(
1590 FROM_HERE_WITH_EXPLICIT_FUNCTION( 1593 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1591 "422460 TemplateURLService::ChangeToLoadedState 1")); 1594 "422460 TemplateURLService::ChangeToLoadedState 1"));
1592 1595
1593 DCHECK(!loaded_); 1596 DCHECK(!loaded_);
1594 1597
1595 provider_map_->Init(template_urls_, search_terms_data()); 1598 provider_map_->Init(template_urls_, search_terms_data());
1596 loaded_ = true; 1599 loaded_ = true;
1597 1600
1598 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is 1601 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
1599 // fixed. 1602 // fixed.
1600 tracked_objects::ScopedTracker tracking_profile2( 1603 tracked_objects::ScopedTracker tracking_profile2(
1601 FROM_HERE_WITH_EXPLICIT_FUNCTION( 1604 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1602 "422460 TemplateURLService::ChangeToLoadedState 2")); 1605 "422460 TemplateURLService::ChangeToLoadedState 2"));
1603 1606
1604 // This will cause a call to NotifyObservers(). 1607 // This will cause a call to NotifyObservers().
1605 ApplyDefaultSearchChangeNoMetrics( 1608 ApplyDefaultSearchChangeNoMetrics(
1606 initial_default_search_provider_ ? 1609 initial_default_search_provider_
1607 &initial_default_search_provider_->data() : NULL, 1610 ? &initial_default_search_provider_->data()
1611 : nullptr,
1608 default_search_provider_source_); 1612 default_search_provider_source_);
1609 initial_default_search_provider_.reset(); 1613 initial_default_search_provider_.reset();
1610 1614
1611 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is 1615 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
1612 // fixed. 1616 // fixed.
1613 tracked_objects::ScopedTracker tracking_profile3( 1617 tracked_objects::ScopedTracker tracking_profile3(
1614 FROM_HERE_WITH_EXPLICIT_FUNCTION( 1618 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1615 "422460 TemplateURLService::ChangeToLoadedState 3")); 1619 "422460 TemplateURLService::ChangeToLoadedState 3"));
1616 1620
1617 on_loaded_callbacks_.Notify(); 1621 on_loaded_callbacks_.Notify();
(...skipping 16 matching lines...) Expand all
1634 t_url->safe_for_autoreplace()); 1638 t_url->safe_for_autoreplace());
1635 } 1639 }
1636 1640
1637 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword( 1641 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword(
1638 const base::string16& keyword) { 1642 const base::string16& keyword) {
1639 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword); 1643 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword);
1640 if (!keyword_turl || (keyword_turl->GetType() == TemplateURL::NORMAL)) 1644 if (!keyword_turl || (keyword_turl->GetType() == TemplateURL::NORMAL))
1641 return keyword_turl; 1645 return keyword_turl;
1642 // The extension keyword in the model may be hiding a replaceable 1646 // The extension keyword in the model may be hiding a replaceable
1643 // non-extension keyword. Look for it. 1647 // non-extension keyword. Look for it.
1644 for (TemplateURLVector::const_iterator i(template_urls_.begin()); 1648 for (auto& turl : template_urls_) {
Peter Kasting 2016/08/31 04:12:56 Nit: const auto&?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
1645 i != template_urls_.end(); ++i) { 1649 if ((turl->GetType() == TemplateURL::NORMAL) &&
1646 if (((*i)->GetType() == TemplateURL::NORMAL) && 1650 (turl->keyword() == keyword))
1647 ((*i)->keyword() == keyword)) 1651 return turl.get();
1648 return *i;
1649 } 1652 }
1650 return NULL; 1653 return nullptr;
1651 } 1654 }
1652 1655
1653 bool TemplateURLService::UpdateNoNotify(TemplateURL* existing_turl, 1656 bool TemplateURLService::UpdateNoNotify(TemplateURL* existing_turl,
1654 const TemplateURL& new_values) { 1657 const TemplateURL& new_values) {
1655 DCHECK(existing_turl); 1658 DCHECK(existing_turl);
1656 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == 1659 if (!Contains(&template_urls_, existing_turl))
1657 template_urls_.end())
1658 return false; 1660 return false;
1659 1661
1660 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType()); 1662 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType());
1661 1663
1662 base::string16 old_keyword(existing_turl->keyword()); 1664 base::string16 old_keyword(existing_turl->keyword());
1663 keyword_to_turl_and_length_.erase(old_keyword); 1665 keyword_to_turl_and_length_.erase(old_keyword);
1664 RemoveFromDomainMap(existing_turl); 1666 RemoveFromDomainMap(existing_turl);
1665 if (!existing_turl->sync_guid().empty()) 1667 if (!existing_turl->sync_guid().empty())
1666 guid_to_turl_.erase(existing_turl->sync_guid()); 1668 guid_to_turl_.erase(existing_turl->sync_guid());
1667 1669
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
1820 initial_default_search_provider_->HasGoogleBaseURLs( 1822 initial_default_search_provider_->HasGoogleBaseURLs(
1821 search_terms_data())) 1823 search_terms_data()))
1822 initial_default_search_provider_->InvalidateCachedValues(); 1824 initial_default_search_provider_->InvalidateCachedValues();
1823 initial_default_search_provider_->ResetKeywordIfNecessary( 1825 initial_default_search_provider_->ResetKeywordIfNecessary(
1824 search_terms_data(), false); 1826 search_terms_data(), false);
1825 return; 1827 return;
1826 } 1828 }
1827 1829
1828 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 1830 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
1829 bool something_changed = false; 1831 bool something_changed = false;
1830 for (TemplateURLVector::iterator i(template_urls_.begin()); 1832 for (auto& turl : template_urls_) {
Peter Kasting 2016/08/31 04:12:56 Nit: const auto&?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
1831 i != template_urls_.end(); ++i) { 1833 if (turl->HasGoogleBaseURLs(search_terms_data())) {
1832 TemplateURL* t_url = *i; 1834 TemplateURL updated_turl(turl->data());
1833 if (t_url->HasGoogleBaseURLs(search_terms_data())) {
1834 TemplateURL updated_turl(t_url->data());
1835 updated_turl.ResetKeywordIfNecessary(search_terms_data(), false); 1835 updated_turl.ResetKeywordIfNecessary(search_terms_data(), false);
1836 KeywordToTURLAndMeaningfulLength::const_iterator existing_entry = 1836 KeywordToTURLAndMeaningfulLength::const_iterator existing_entry =
1837 keyword_to_turl_and_length_.find(updated_turl.keyword()); 1837 keyword_to_turl_and_length_.find(updated_turl.keyword());
1838 if ((existing_entry != keyword_to_turl_and_length_.end()) && 1838 if ((existing_entry != keyword_to_turl_and_length_.end()) &&
1839 (existing_entry->second.first != t_url)) { 1839 (existing_entry->second.first != turl.get())) {
1840 // The new autogenerated keyword conflicts with another TemplateURL. 1840 // The new autogenerated keyword conflicts with another TemplateURL.
1841 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its 1841 // Overwrite it if it's replaceable; otherwise, leave |turl| using its
1842 // current keyword. (This will not prevent |t_url| from auto-updating 1842 // current keyword. (This will not prevent |turl| from auto-updating
1843 // the keyword in the future if the conflicting TemplateURL disappears.) 1843 // the keyword in the future if the conflicting TemplateURL disappears.)
1844 // Note that we must still update |t_url| in this case, or the 1844 // Note that we must still update |turl| in this case, or the
1845 // |provider_map_| will not be updated correctly. 1845 // |provider_map_| will not be updated correctly.
1846 if (CanReplace(existing_entry->second.first)) 1846 if (CanReplace(existing_entry->second.first))
1847 RemoveNoNotify(existing_entry->second.first); 1847 RemoveNoNotify(existing_entry->second.first);
1848 else 1848 else
1849 updated_turl.data_.SetKeyword(t_url->keyword()); 1849 updated_turl.data_.SetKeyword(turl->keyword());
1850 } 1850 }
1851 something_changed = true; 1851 something_changed = true;
1852 // This will send the keyword change to sync. Note that other clients 1852 // This will send the keyword change to sync. Note that other clients
1853 // need to reset the keyword to an appropriate local value when this 1853 // need to reset the keyword to an appropriate local value when this
1854 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData(). 1854 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData().
1855 UpdateNoNotify(t_url, updated_turl); 1855 UpdateNoNotify(turl.get(), updated_turl);
1856 } 1856 }
1857 } 1857 }
1858 if (something_changed) 1858 if (something_changed)
1859 NotifyObservers(); 1859 NotifyObservers();
1860 } 1860 }
1861 1861
1862 void TemplateURLService::OnDefaultSearchChange( 1862 void TemplateURLService::OnDefaultSearchChange(
1863 const TemplateURLData* data, 1863 const TemplateURLData* data,
1864 DefaultSearchManager::Source source) { 1864 DefaultSearchManager::Source source) {
1865 if (prefs_ && (source == DefaultSearchManager::FROM_USER) && 1865 if (prefs_ && (source == DefaultSearchManager::FROM_USER) &&
(...skipping 22 matching lines...) Expand all
1888 bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( 1888 bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics(
1889 const TemplateURLData* data, 1889 const TemplateURLData* data,
1890 DefaultSearchManager::Source source) { 1890 DefaultSearchManager::Source source) {
1891 if (!loaded_) { 1891 if (!loaded_) {
1892 // Set |initial_default_search_provider_| from the preferences. This is 1892 // Set |initial_default_search_provider_| from the preferences. This is
1893 // mainly so we can hold ownership until we get to the point where the list 1893 // mainly so we can hold ownership until we get to the point where the list
1894 // of keywords from Web Data is the owner of everything including the 1894 // of keywords from Web Data is the owner of everything including the
1895 // default. 1895 // default.
1896 bool changed = TemplateURL::MatchesData( 1896 bool changed = TemplateURL::MatchesData(
1897 initial_default_search_provider_.get(), data, search_terms_data()); 1897 initial_default_search_provider_.get(), data, search_terms_data());
1898 initial_default_search_provider_.reset( 1898 initial_default_search_provider_.reset(data ? new TemplateURL(*data)
1899 data ? new TemplateURL(*data) : NULL); 1899 : nullptr);
Peter Kasting 2016/08/31 04:12:56 Nit: Maybe initial_default_search_provider_ =
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
1900 default_search_provider_source_ = source; 1900 default_search_provider_source_ = source;
1901 return changed; 1901 return changed;
1902 } 1902 }
1903 1903
1904 // Prevent recursion if we update the value stored in default_search_manager_. 1904 // Prevent recursion if we update the value stored in default_search_manager_.
1905 // Note that we exclude the case of data == NULL because that could cause a 1905 // Note that we exclude the case of data == NULL because that could cause a
1906 // false positive for recursion when the initial_default_search_provider_ is 1906 // false positive for recursion when the initial_default_search_provider_ is
1907 // NULL due to policy. We'll never actually get recursion with data == NULL. 1907 // NULL due to policy. We'll never actually get recursion with data == NULL.
1908 if (source == default_search_provider_source_ && data != NULL && 1908 if (source == default_search_provider_source_ && data != nullptr &&
1909 TemplateURL::MatchesData(default_search_provider_, data, 1909 TemplateURL::MatchesData(default_search_provider_, data,
1910 search_terms_data())) 1910 search_terms_data()))
1911 return false; 1911 return false;
1912 1912
1913 // This may be deleted later. Use exclusively for pointer comparison to detect 1913 // This may be deleted later. Use exclusively for pointer comparison to detect
1914 // a change. 1914 // a change.
1915 TemplateURL* previous_default_search_engine = default_search_provider_; 1915 TemplateURL* previous_default_search_engine = default_search_provider_;
1916 1916
1917 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 1917 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
1918 if (default_search_provider_source_ == DefaultSearchManager::FROM_POLICY || 1918 if (default_search_provider_source_ == DefaultSearchManager::FROM_POLICY ||
1919 source == DefaultSearchManager::FROM_POLICY) { 1919 source == DefaultSearchManager::FROM_POLICY) {
1920 // We do this both to remove any no-longer-applicable policy-defined DSE as 1920 // We do this both to remove any no-longer-applicable policy-defined DSE as
1921 // well as to add the new one, if appropriate. 1921 // well as to add the new one, if appropriate.
1922 UpdateProvidersCreatedByPolicy( 1922 UpdateProvidersCreatedByPolicy(
1923 &template_urls_, 1923 &template_urls_,
1924 source == DefaultSearchManager::FROM_POLICY ? data : NULL); 1924 source == DefaultSearchManager::FROM_POLICY ? data : nullptr);
1925 } 1925 }
1926 1926
1927 if (!data) { 1927 if (!data) {
1928 default_search_provider_ = NULL; 1928 default_search_provider_ = nullptr;
1929 } else if (source == DefaultSearchManager::FROM_EXTENSION) { 1929 } else if (source == DefaultSearchManager::FROM_EXTENSION) {
1930 default_search_provider_ = FindMatchingExtensionTemplateURL( 1930 default_search_provider_ = FindMatchingExtensionTemplateURL(
1931 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); 1931 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION);
1932 } else if (source == DefaultSearchManager::FROM_FALLBACK) { 1932 } else if (source == DefaultSearchManager::FROM_FALLBACK) {
1933 default_search_provider_ = 1933 default_search_provider_ =
1934 FindPrepopulatedTemplateURL(data->prepopulate_id); 1934 FindPrepopulatedTemplateURL(data->prepopulate_id);
1935 if (default_search_provider_) { 1935 if (default_search_provider_) {
1936 TemplateURLData update_data(*data); 1936 TemplateURLData update_data(*data);
1937 update_data.sync_guid = default_search_provider_->sync_guid(); 1937 update_data.sync_guid = default_search_provider_->sync_guid();
1938 if (!default_search_provider_->safe_for_autoreplace()) { 1938 if (!default_search_provider_->safe_for_autoreplace()) {
1939 update_data.safe_for_autoreplace = false; 1939 update_data.safe_for_autoreplace = false;
1940 update_data.SetKeyword(default_search_provider_->keyword()); 1940 update_data.SetKeyword(default_search_provider_->keyword());
1941 update_data.SetShortName(default_search_provider_->short_name()); 1941 update_data.SetShortName(default_search_provider_->short_name());
1942 } 1942 }
1943 UpdateNoNotify(default_search_provider_, TemplateURL(update_data)); 1943 UpdateNoNotify(default_search_provider_, TemplateURL(update_data));
1944 } else { 1944 } else {
1945 // Normally the prepopulated fallback should be present in 1945 // Normally the prepopulated fallback should be present in
1946 // |template_urls_|, but in a few cases it might not be: 1946 // |template_urls_|, but in a few cases it might not be:
1947 // (1) Tests that initialize the TemplateURLService in peculiar ways. 1947 // (1) Tests that initialize the TemplateURLService in peculiar ways.
1948 // (2) If the user deleted the pre-populated default and we subsequently 1948 // (2) If the user deleted the pre-populated default and we subsequently
1949 // lost their user-selected value. 1949 // lost their user-selected value.
1950 TemplateURL* new_dse = new TemplateURL(*data); 1950 std::unique_ptr<TemplateURL> new_dse_ptr =
1951 if (AddNoNotify(new_dse, true)) 1951 base::MakeUnique<TemplateURL>(*data);
1952 TemplateURL* new_dse = new_dse_ptr.get();
1953 if (AddNoNotify(std::move(new_dse_ptr), true))
1952 default_search_provider_ = new_dse; 1954 default_search_provider_ = new_dse;
1953 } 1955 }
1954 } else if (source == DefaultSearchManager::FROM_USER) { 1956 } else if (source == DefaultSearchManager::FROM_USER) {
1955 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid); 1957 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid);
1956 if (!default_search_provider_ && data->prepopulate_id) { 1958 if (!default_search_provider_ && data->prepopulate_id) {
1957 default_search_provider_ = 1959 default_search_provider_ =
1958 FindPrepopulatedTemplateURL(data->prepopulate_id); 1960 FindPrepopulatedTemplateURL(data->prepopulate_id);
1959 } 1961 }
1960 TemplateURLData new_data(*data); 1962 TemplateURLData new_data(*data);
1961 new_data.show_in_default_list = true; 1963 new_data.show_in_default_list = true;
1962 if (default_search_provider_) { 1964 if (default_search_provider_) {
1963 UpdateNoNotify(default_search_provider_, TemplateURL(new_data)); 1965 UpdateNoNotify(default_search_provider_, TemplateURL(new_data));
1964 } else { 1966 } else {
1965 new_data.id = kInvalidTemplateURLID; 1967 new_data.id = kInvalidTemplateURLID;
1966 TemplateURL* new_dse = new TemplateURL(new_data); 1968 std::unique_ptr<TemplateURL> new_dse_ptr =
1967 if (AddNoNotify(new_dse, true)) 1969 base::MakeUnique<TemplateURL>(new_data);
1970 TemplateURL* new_dse = new_dse_ptr.get();
1971 if (AddNoNotify(std::move(new_dse_ptr), true))
1968 default_search_provider_ = new_dse; 1972 default_search_provider_ = new_dse;
1969 } 1973 }
1970 if (default_search_provider_ && prefs_) { 1974 if (default_search_provider_ && prefs_) {
1971 prefs_->SetString(prefs::kSyncedDefaultSearchProviderGUID, 1975 prefs_->SetString(prefs::kSyncedDefaultSearchProviderGUID,
1972 default_search_provider_->sync_guid()); 1976 default_search_provider_->sync_guid());
1973 } 1977 }
1974 1978
1975 } 1979 }
1976 1980
1977 default_search_provider_source_ = source; 1981 default_search_provider_source_ = source;
1978 1982
1979 bool changed = default_search_provider_ != previous_default_search_engine; 1983 bool changed = default_search_provider_ != previous_default_search_engine;
1980 if (changed) 1984 if (changed)
1981 RequestGoogleURLTrackerServerCheckIfNecessary(); 1985 RequestGoogleURLTrackerServerCheckIfNecessary();
1982 1986
1983 NotifyObservers(); 1987 NotifyObservers();
1984 1988
1985 return changed; 1989 return changed;
1986 } 1990 }
1987 1991
1988 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, 1992 bool TemplateURLService::AddNoNotify(std::unique_ptr<TemplateURL> template_url,
1989 bool newly_adding) { 1993 bool newly_adding) {
1990 DCHECK(template_url); 1994 DCHECK(template_url);
1991 1995
1992 if (newly_adding) { 1996 if (newly_adding) {
1993 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); 1997 DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
1994 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), 1998 DCHECK(!Contains(&template_urls_, template_url.get()));
1995 template_url) == template_urls_.end());
1996 template_url->data_.id = ++next_id_; 1999 template_url->data_.id = ++next_id_;
1997 } 2000 }
1998 2001
1999 template_url->ResetKeywordIfNecessary(search_terms_data(), false); 2002 template_url->ResetKeywordIfNecessary(search_terms_data(), false);
2000 // Check whether |template_url|'s keyword conflicts with any already in the 2003 // Check whether |template_url|'s keyword conflicts with any already in the
2001 // model. 2004 // model.
2002 TemplateURL* existing_keyword_turl = 2005 TemplateURL* existing_keyword_turl =
2003 GetTemplateURLForKeyword(template_url->keyword()); 2006 GetTemplateURLForKeyword(template_url->keyword());
2004 2007
2005 // Check whether |template_url|'s keyword conflicts with any already in the 2008 // Check whether |template_url|'s keyword conflicts with any already in the
2006 // model. Note that we can reach here during the loading phase while 2009 // model. Note that we can reach here during the loading phase while
2007 // processing the template URLs from the web data service. In this case, 2010 // processing the template URLs from the web data service. In this case,
2008 // GetTemplateURLForKeyword() will look not only at what's already in the 2011 // GetTemplateURLForKeyword() will look not only at what's already in the
2009 // model, but at the |initial_default_search_provider_|. Since this engine 2012 // model, but at the |initial_default_search_provider_|. Since this engine
2010 // will presumably also be present in the web data, we need to double-check 2013 // will presumably also be present in the web data, we need to double-check
2011 // that any "pre-existing" entries we find are actually coming from 2014 // that any "pre-existing" entries we find are actually coming from
2012 // |template_urls_|, lest we detect a "conflict" between the 2015 // |template_urls_|, lest we detect a "conflict" between the
2013 // |initial_default_search_provider_| and the web data version of itself. 2016 // |initial_default_search_provider_| and the web data version of itself.
2014 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && 2017 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION &&
2015 existing_keyword_turl && 2018 existing_keyword_turl &&
2016 existing_keyword_turl->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && 2019 existing_keyword_turl->GetType() != TemplateURL::OMNIBOX_API_EXTENSION &&
2017 (std::find(template_urls_.begin(), template_urls_.end(), 2020 (Contains(&template_urls_, existing_keyword_turl))) {
Peter Kasting 2016/08/31 04:12:57 Nit: Remove extra parens
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
2018 existing_keyword_turl) != template_urls_.end())) { 2021 DCHECK_NE(existing_keyword_turl, template_url.get());
2019 DCHECK_NE(existing_keyword_turl, template_url);
2020 // Only replace one of the TemplateURLs if they are either both extensions, 2022 // Only replace one of the TemplateURLs if they are either both extensions,
2021 // or both not extensions. 2023 // or both not extensions.
2022 bool are_same_type = existing_keyword_turl->GetType() == 2024 bool are_same_type = existing_keyword_turl->GetType() ==
2023 template_url->GetType(); 2025 template_url->GetType();
2024 if (CanReplace(existing_keyword_turl) && are_same_type) { 2026 if (CanReplace(existing_keyword_turl) && are_same_type) {
2025 RemoveNoNotify(existing_keyword_turl); 2027 RemoveNoNotify(existing_keyword_turl);
2026 } else if (CanReplace(template_url) && are_same_type) { 2028 } else if (CanReplace(template_url.get()) && are_same_type) {
2027 delete template_url;
2028 return false; 2029 return false;
2029 } else { 2030 } else {
2030 base::string16 new_keyword = 2031 base::string16 new_keyword =
2031 UniquifyKeyword(*existing_keyword_turl, false); 2032 UniquifyKeyword(*existing_keyword_turl, false);
2032 ResetTemplateURLNoNotify(existing_keyword_turl, 2033 ResetTemplateURLNoNotify(existing_keyword_turl,
2033 existing_keyword_turl->short_name(), new_keyword, 2034 existing_keyword_turl->short_name(), new_keyword,
2034 existing_keyword_turl->url()); 2035 existing_keyword_turl->url());
2035 } 2036 }
2036 } 2037 }
2037 template_urls_.push_back(template_url); 2038 TemplateURL* template_url_ptr = template_url.get();
2038 AddToMaps(template_url); 2039 template_urls_.push_back(std::move(template_url));
2040 AddToMaps(template_url_ptr);
2039 2041
2040 if (newly_adding && 2042 if (newly_adding && (template_url_ptr->GetType() == TemplateURL::NORMAL)) {
2041 (template_url->GetType() == TemplateURL::NORMAL)) {
2042 if (web_data_service_.get()) 2043 if (web_data_service_.get())
2043 web_data_service_->AddKeyword(template_url->data()); 2044 web_data_service_->AddKeyword(template_url_ptr->data());
2044 2045
2045 // Inform sync of the addition. Note that this will assign a GUID to 2046 // Inform sync of the addition. Note that this will assign a GUID to
2046 // template_url and add it to the guid_to_turl_. 2047 // template_url and add it to the guid_to_turl_.
2047 ProcessTemplateURLChange(FROM_HERE, 2048 ProcessTemplateURLChange(FROM_HERE, template_url_ptr,
2048 template_url,
2049 syncer::SyncChange::ACTION_ADD); 2049 syncer::SyncChange::ACTION_ADD);
2050 } 2050 }
2051 2051
2052 return true; 2052 return true;
2053 } 2053 }
2054 2054
2055 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { 2055 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) {
2056 DCHECK(template_url != default_search_provider_); 2056 DCHECK(template_url != default_search_provider_);
2057 2057
2058 TemplateURLVector::iterator i = 2058 auto i = FindTemplateURL(&template_urls_, template_url);
2059 std::find(template_urls_.begin(), template_urls_.end(), template_url);
2060 if (i == template_urls_.end()) 2059 if (i == template_urls_.end())
2061 return; 2060 return;
2062 2061
2063 RemoveFromMaps(template_url); 2062 RemoveFromMaps(template_url);
2064 2063
2065 // Remove it from the vector containing all TemplateURLs. 2064 // Remove it from the vector containing all TemplateURLs.
2065 std::unique_ptr<TemplateURL> scoped_turl = std::move(*i);
2066 template_urls_.erase(i); 2066 template_urls_.erase(i);
2067 2067
2068 if (template_url->GetType() == TemplateURL::NORMAL) { 2068 if (template_url->GetType() == TemplateURL::NORMAL) {
2069 if (web_data_service_.get()) 2069 if (web_data_service_.get())
2070 web_data_service_->RemoveKeyword(template_url->id()); 2070 web_data_service_->RemoveKeyword(template_url->id());
2071 2071
2072 // Inform sync of the deletion. 2072 // Inform sync of the deletion.
2073 ProcessTemplateURLChange(FROM_HERE, 2073 ProcessTemplateURLChange(FROM_HERE,
2074 template_url, 2074 template_url,
2075 syncer::SyncChange::ACTION_DELETE); 2075 syncer::SyncChange::ACTION_DELETE);
2076 2076
2077 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, 2077 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
2078 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); 2078 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX);
2079 } 2079 }
2080 2080
2081 if (loaded_ && client_) 2081 if (loaded_ && client_)
2082 client_->DeleteAllSearchTermsForKeyword(template_url->id()); 2082 client_->DeleteAllSearchTermsForKeyword(template_url->id());
2083
2084 // We own the TemplateURL and need to delete it.
2085 delete template_url;
2086 } 2083 }
2087 2084
2088 bool TemplateURLService::ResetTemplateURLNoNotify( 2085 bool TemplateURLService::ResetTemplateURLNoNotify(
2089 TemplateURL* url, 2086 TemplateURL* url,
2090 const base::string16& title, 2087 const base::string16& title,
2091 const base::string16& keyword, 2088 const base::string16& keyword,
2092 const std::string& search_url) { 2089 const std::string& search_url) {
2093 DCHECK(!keyword.empty()); 2090 DCHECK(!keyword.empty());
2094 DCHECK(!search_url.empty()); 2091 DCHECK(!search_url.empty());
2095 TemplateURLData data(url->data()); 2092 TemplateURLData data(url->data());
(...skipping 18 matching lines...) Expand all
2114 } 2111 }
2115 2112
2116 // |template_urls| are the TemplateURLs loaded from the database. 2113 // |template_urls| are the TemplateURLs loaded from the database.
2117 // |default_from_prefs| is the default search provider from the preferences, or 2114 // |default_from_prefs| is the default search provider from the preferences, or
2118 // NULL if the DSE is not policy-defined. 2115 // NULL if the DSE is not policy-defined.
2119 // 2116 //
2120 // This function removes from the vector and the database all the TemplateURLs 2117 // This function removes from the vector and the database all the TemplateURLs
2121 // that were set by policy, unless it is the current default search provider, in 2118 // that were set by policy, unless it is the current default search provider, in
2122 // which case it is updated with the data from prefs. 2119 // which case it is updated with the data from prefs.
2123 void TemplateURLService::UpdateProvidersCreatedByPolicy( 2120 void TemplateURLService::UpdateProvidersCreatedByPolicy(
2124 TemplateURLVector* template_urls, 2121 OwnedTemplateURLVector* template_urls,
2125 const TemplateURLData* default_from_prefs) { 2122 const TemplateURLData* default_from_prefs) {
2126 DCHECK(template_urls); 2123 DCHECK(template_urls);
2127 2124
2128 for (TemplateURLVector::iterator i = template_urls->begin(); 2125 for (auto i = template_urls->begin(); i != template_urls->end();) {
2129 i != template_urls->end(); ) { 2126 TemplateURL* template_url = (*i).get();
Peter Kasting 2016/08/31 04:12:57 Nit: i->get()
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
2130 TemplateURL* template_url = *i;
2131 if (template_url->created_by_policy()) { 2127 if (template_url->created_by_policy()) {
2132 if (default_from_prefs && 2128 if (default_from_prefs &&
2133 TemplateURL::MatchesData(template_url, default_from_prefs, 2129 TemplateURL::MatchesData(template_url, default_from_prefs,
2134 search_terms_data())) { 2130 search_terms_data())) {
2135 // If the database specified a default search provider that was set 2131 // If the database specified a default search provider that was set
2136 // by policy, and the default search provider from the preferences 2132 // by policy, and the default search provider from the preferences
2137 // is also set by policy and they are the same, keep the entry in the 2133 // is also set by policy and they are the same, keep the entry in the
2138 // database and the |default_search_provider|. 2134 // database and the |default_search_provider|.
2139 default_search_provider_ = template_url; 2135 default_search_provider_ = template_url;
2140 // Prevent us from saving any other entries, or creating a new one. 2136 // Prevent us from saving any other entries, or creating a new one.
2141 default_from_prefs = NULL; 2137 default_from_prefs = nullptr;
2142 ++i; 2138 ++i;
2143 continue; 2139 continue;
2144 } 2140 }
2145 2141
2142 TemplateURLID id = template_url->id();
2146 RemoveFromMaps(template_url); 2143 RemoveFromMaps(template_url);
2147 i = template_urls->erase(i); 2144 i = template_urls->erase(i);
2148 if (web_data_service_.get()) 2145 if (web_data_service_.get())
2149 web_data_service_->RemoveKeyword(template_url->id()); 2146 web_data_service_->RemoveKeyword(id);
2150 delete template_url;
2151 } else { 2147 } else {
2152 ++i; 2148 ++i;
2153 } 2149 }
2154 } 2150 }
2155 2151
2156 if (default_from_prefs) { 2152 if (default_from_prefs) {
2157 default_search_provider_ = NULL; 2153 default_search_provider_ = nullptr;
2158 default_search_provider_source_ = DefaultSearchManager::FROM_POLICY; 2154 default_search_provider_source_ = DefaultSearchManager::FROM_POLICY;
2159 TemplateURLData new_data(*default_from_prefs); 2155 TemplateURLData new_data(*default_from_prefs);
2160 if (new_data.sync_guid.empty()) 2156 if (new_data.sync_guid.empty())
2161 new_data.sync_guid = base::GenerateGUID(); 2157 new_data.sync_guid = base::GenerateGUID();
2162 new_data.created_by_policy = true; 2158 new_data.created_by_policy = true;
2163 TemplateURL* new_dse = new TemplateURL(new_data); 2159 std::unique_ptr<TemplateURL> new_dse_ptr =
2164 if (AddNoNotify(new_dse, true)) 2160 base::MakeUnique<TemplateURL>(new_data);
2161 TemplateURL* new_dse = new_dse_ptr.get();
2162 if (AddNoNotify(std::move(new_dse_ptr), true))
2165 default_search_provider_ = new_dse; 2163 default_search_provider_ = new_dse;
2166 } 2164 }
2167 } 2165 }
2168 2166
2169 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, 2167 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url,
2170 const std::string& guid) { 2168 const std::string& guid) {
2171 DCHECK(loaded_); 2169 DCHECK(loaded_);
2172 DCHECK(!guid.empty()); 2170 DCHECK(!guid.empty());
2173 2171
2174 TemplateURLData data(url->data()); 2172 TemplateURLData data(url->data());
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
2344 } 2342 }
2345 // Remove the local data so it isn't written to sync. 2343 // Remove the local data so it isn't written to sync.
2346 local_data->erase(guid); 2344 local_data->erase(guid);
2347 } 2345 }
2348 } 2346 }
2349 2347
2350 if (should_add_sync_turl) { 2348 if (should_add_sync_turl) {
2351 // Force the local ID to kInvalidTemplateURLID so we can add it. 2349 // Force the local ID to kInvalidTemplateURLID so we can add it.
2352 TemplateURLData data(sync_turl->data()); 2350 TemplateURLData data(sync_turl->data());
2353 data.id = kInvalidTemplateURLID; 2351 data.id = kInvalidTemplateURLID;
2354 TemplateURL* added = new TemplateURL(data); 2352 std::unique_ptr<TemplateURL> added_ptr =
2353 base::MakeUnique<TemplateURL>(data);
2354 TemplateURL* added = added_ptr.get();
2355 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 2355 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
2356 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD); 2356 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD);
2357 if (Add(added)) 2357 if (Add(std::move(added_ptr)))
2358 MaybeUpdateDSEAfterSync(added); 2358 MaybeUpdateDSEAfterSync(added);
2359 merge_result->set_num_items_added( 2359 merge_result->set_num_items_added(merge_result->num_items_added() + 1);
2360 merge_result->num_items_added() + 1);
2361 } 2360 }
2362 } 2361 }
2363 2362
2364 void TemplateURLService::PatchMissingSyncGUIDs( 2363 void TemplateURLService::PatchMissingSyncGUIDs(
2365 TemplateURLVector* template_urls) { 2364 OwnedTemplateURLVector* template_urls) {
2366 DCHECK(template_urls); 2365 DCHECK(template_urls);
2367 for (TemplateURLVector::iterator i = template_urls->begin(); 2366 for (auto& template_url : *template_urls) {
2368 i != template_urls->end(); ++i) {
2369 TemplateURL* template_url = *i;
2370 DCHECK(template_url); 2367 DCHECK(template_url);
2371 if (template_url->sync_guid().empty() && 2368 if (template_url->sync_guid().empty() &&
2372 (template_url->GetType() == TemplateURL::NORMAL)) { 2369 (template_url->GetType() == TemplateURL::NORMAL)) {
2373 template_url->data_.sync_guid = base::GenerateGUID(); 2370 template_url->data_.sync_guid = base::GenerateGUID();
2374 if (web_data_service_.get()) 2371 if (web_data_service_.get())
2375 web_data_service_->UpdateKeyword(template_url->data()); 2372 web_data_service_->UpdateKeyword(template_url->data());
2376 } 2373 }
2377 } 2374 }
2378 } 2375 }
2379 2376
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
2417 for (typename Container::const_iterator i(match_range.first); 2414 for (typename Container::const_iterator i(match_range.first);
2418 i != match_range.second; ++i) { 2415 i != match_range.second; ++i) {
2419 if (!supports_replacement_only || 2416 if (!supports_replacement_only ||
2420 i->second.first->url_ref().SupportsReplacement(search_terms_data())) 2417 i->second.first->url_ref().SupportsReplacement(search_terms_data()))
2421 matches->push_back(i->second); 2418 matches->push_back(i->second);
2422 } 2419 }
2423 } 2420 }
2424 2421
2425 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL( 2422 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL(
2426 int prepopulated_id) { 2423 int prepopulated_id) {
2427 for (TemplateURLVector::const_iterator i = template_urls_.begin(); 2424 for (auto& turl : template_urls_) {
Peter Kasting 2016/08/31 04:12:57 Nit: const auto&?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
2428 i != template_urls_.end(); ++i) { 2425 if (turl->prepopulate_id() == prepopulated_id)
2429 if ((*i)->prepopulate_id() == prepopulated_id) 2426 return turl.get();
2430 return *i;
2431 } 2427 }
2432 return NULL; 2428 return nullptr;
2433 } 2429 }
2434 2430
2435 TemplateURL* TemplateURLService::FindTemplateURLForExtension( 2431 TemplateURL* TemplateURLService::FindTemplateURLForExtension(
2436 const std::string& extension_id, 2432 const std::string& extension_id,
2437 TemplateURL::Type type) { 2433 TemplateURL::Type type) {
2438 DCHECK_NE(TemplateURL::NORMAL, type); 2434 DCHECK_NE(TemplateURL::NORMAL, type);
2439 for (TemplateURLVector::const_iterator i = template_urls_.begin(); 2435 for (auto& turl : template_urls_) {
Peter Kasting 2016/08/31 04:12:57 Nit: const auto&?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
2440 i != template_urls_.end(); ++i) { 2436 if (turl->GetType() == type && turl->GetExtensionId() == extension_id)
2441 if ((*i)->GetType() == type && 2437 return turl.get();
2442 (*i)->GetExtensionId() == extension_id)
2443 return *i;
2444 } 2438 }
2445 return NULL; 2439 return nullptr;
2446 } 2440 }
2447 2441
2448 TemplateURL* TemplateURLService::FindMatchingExtensionTemplateURL( 2442 TemplateURL* TemplateURLService::FindMatchingExtensionTemplateURL(
2449 const TemplateURLData& data, 2443 const TemplateURLData& data,
2450 TemplateURL::Type type) { 2444 TemplateURL::Type type) {
2451 DCHECK_NE(TemplateURL::NORMAL, type); 2445 DCHECK_NE(TemplateURL::NORMAL, type);
2452 for (TemplateURLVector::const_iterator i = template_urls_.begin(); 2446 for (auto& turl : template_urls_) {
Peter Kasting 2016/08/31 04:12:57 Nit: const auto&?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
2453 i != template_urls_.end(); ++i) { 2447 if (turl->GetType() == type &&
2454 if ((*i)->GetType() == type && 2448 TemplateURL::MatchesData(turl.get(), &data, search_terms_data()))
2455 TemplateURL::MatchesData(*i, &data, search_terms_data())) 2449 return turl.get();
2456 return *i;
2457 } 2450 }
2458 return NULL; 2451 return nullptr;
2459 } 2452 }
2460 2453
2461 void TemplateURLService::UpdateExtensionDefaultSearchEngine() { 2454 void TemplateURLService::UpdateExtensionDefaultSearchEngine() {
2462 TemplateURL* most_recently_intalled_default = NULL; 2455 TemplateURL* most_recently_intalled_default = nullptr;
2463 for (TemplateURLVector::const_iterator i = template_urls_.begin(); 2456 for (auto& turl : template_urls_) {
Peter Kasting 2016/08/31 04:12:57 Nit: const auto&?
Avi (use Gerrit) 2016/09/01 00:34:27 Done.
2464 i != template_urls_.end(); ++i) { 2457 if ((turl->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) &&
2465 if (((*i)->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && 2458 turl->extension_info_->wants_to_be_default_engine &&
2466 (*i)->extension_info_->wants_to_be_default_engine && 2459 turl->SupportsReplacement(search_terms_data()) &&
2467 (*i)->SupportsReplacement(search_terms_data()) &&
2468 (!most_recently_intalled_default || 2460 (!most_recently_intalled_default ||
2469 (most_recently_intalled_default->extension_info_->install_time < 2461 (most_recently_intalled_default->extension_info_->install_time <
2470 (*i)->extension_info_->install_time))) 2462 turl->extension_info_->install_time)))
2471 most_recently_intalled_default = *i; 2463 most_recently_intalled_default = turl.get();
2472 } 2464 }
2473 2465
2474 if (most_recently_intalled_default) { 2466 if (most_recently_intalled_default) {
2475 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 2467 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
2476 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); 2468 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION);
2477 default_search_manager_.SetExtensionControlledDefaultSearchEngine( 2469 default_search_manager_.SetExtensionControlledDefaultSearchEngine(
2478 most_recently_intalled_default->data()); 2470 most_recently_intalled_default->data());
2479 } else { 2471 } else {
2480 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); 2472 default_search_manager_.ClearExtensionControlledDefaultSearchEngine();
2481 } 2473 }
2482 } 2474 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698