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

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

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

Powered by Google App Engine
This is Rietveld 408576698