| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/search_engines/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 Loading... |
| 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 Loading... |
| 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 TemplateURL* 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) { |
| 479 UpdateExtensionDefaultSearchEngine(); | 485 UpdateExtensionDefaultSearchEngine(); |
| 486 } |
| 480 NotifyObservers(); | 487 NotifyObservers(); |
| 481 } | 488 } |
| 489 |
| 490 return template_url_ptr; |
| 482 } | 491 } |
| 483 | 492 |
| 484 void TemplateURLService::Remove(TemplateURL* template_url) { | 493 void TemplateURLService::Remove(TemplateURL* template_url) { |
| 485 RemoveNoNotify(template_url); | 494 RemoveNoNotify(template_url); |
| 486 NotifyObservers(); | 495 NotifyObservers(); |
| 487 } | 496 } |
| 488 | 497 |
| 489 void TemplateURLService::RemoveExtensionControlledTURL( | 498 void TemplateURLService::RemoveExtensionControlledTURL( |
| 490 const std::string& extension_id, | 499 const std::string& extension_id, |
| 491 TemplateURL::Type type) { | 500 TemplateURL::Type type) { |
| 492 DCHECK(loaded_); | 501 DCHECK(loaded_); |
| 493 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); | 502 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); |
| 494 if (!url) | 503 if (!url) |
| 495 return; | 504 return; |
| 496 // NULL this out so that we can call RemoveNoNotify. | 505 // NULL this out so that we can call RemoveNoNotify. |
| 497 // UpdateExtensionDefaultSearchEngine will cause it to be reset. | 506 // UpdateExtensionDefaultSearchEngine will cause it to be reset. |
| 498 if (default_search_provider_ == url) | 507 if (default_search_provider_ == url) |
| 499 default_search_provider_ = NULL; | 508 default_search_provider_ = nullptr; |
| 500 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 509 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 501 RemoveNoNotify(url); | 510 RemoveNoNotify(url); |
| 502 UpdateExtensionDefaultSearchEngine(); | 511 UpdateExtensionDefaultSearchEngine(); |
| 503 NotifyObservers(); | 512 NotifyObservers(); |
| 504 } | 513 } |
| 505 | 514 |
| 506 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { | 515 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { |
| 507 RemoveAutoGeneratedBetween(created_after, base::Time()); | 516 RemoveAutoGeneratedBetween(created_after, base::Time()); |
| 508 } | 517 } |
| 509 | 518 |
| 510 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, | 519 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, |
| 511 base::Time created_before) { | 520 base::Time created_before) { |
| 512 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), | 521 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), |
| 513 created_after, created_before); | 522 created_after, created_before); |
| 514 } | 523 } |
| 515 | 524 |
| 516 void TemplateURLService::RemoveAutoGeneratedForUrlsBetween( | 525 void TemplateURLService::RemoveAutoGeneratedForUrlsBetween( |
| 517 const base::Callback<bool(const GURL&)>& url_filter, | 526 const base::Callback<bool(const GURL&)>& url_filter, |
| 518 base::Time created_after, | 527 base::Time created_after, |
| 519 base::Time created_before) { | 528 base::Time created_before) { |
| 520 bool should_notify = false; | 529 bool should_notify = false; |
| 521 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 530 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 522 for (size_t i = 0; i < template_urls_.size();) { | 531 for (size_t i = 0; i < template_urls_.size();) { |
| 523 if (template_urls_[i]->date_created() >= created_after && | 532 if (template_urls_[i]->date_created() >= created_after && |
| 524 (created_before.is_null() || | 533 (created_before.is_null() || |
| 525 template_urls_[i]->date_created() < created_before) && | 534 template_urls_[i]->date_created() < created_before) && |
| 526 CanReplace(template_urls_[i]) && | 535 CanReplace(template_urls_[i].get()) && |
| 527 (url_filter.is_null() || | 536 (url_filter.is_null() || |
| 528 url_filter.Run( | 537 url_filter.Run( |
| 529 template_urls_[i]->GenerateSearchURL(search_terms_data())))) { | 538 template_urls_[i]->GenerateSearchURL(search_terms_data())))) { |
| 530 RemoveNoNotify(template_urls_[i]); | 539 RemoveNoNotify(template_urls_[i].get()); |
| 531 should_notify = true; | 540 should_notify = true; |
| 532 } else { | 541 } else { |
| 533 ++i; | 542 ++i; |
| 534 } | 543 } |
| 535 } | 544 } |
| 536 if (should_notify) | 545 if (should_notify) |
| 537 NotifyObservers(); | 546 NotifyObservers(); |
| 538 } | 547 } |
| 539 | 548 |
| 540 void TemplateURLService::RegisterOmniboxKeyword( | 549 void TemplateURLService::RegisterOmniboxKeyword( |
| 541 const std::string& extension_id, | 550 const std::string& extension_id, |
| 542 const std::string& extension_name, | 551 const std::string& extension_name, |
| 543 const std::string& keyword, | 552 const std::string& keyword, |
| 544 const std::string& template_url_string) { | 553 const std::string& template_url_string) { |
| 545 DCHECK(loaded_); | 554 DCHECK(loaded_); |
| 546 | 555 |
| 547 if (FindTemplateURLForExtension(extension_id, | 556 if (FindTemplateURLForExtension(extension_id, |
| 548 TemplateURL::OMNIBOX_API_EXTENSION)) | 557 TemplateURL::OMNIBOX_API_EXTENSION)) |
| 549 return; | 558 return; |
| 550 | 559 |
| 551 TemplateURLData data; | 560 TemplateURLData data; |
| 552 data.SetShortName(base::UTF8ToUTF16(extension_name)); | 561 data.SetShortName(base::UTF8ToUTF16(extension_name)); |
| 553 data.SetKeyword(base::UTF8ToUTF16(keyword)); | 562 data.SetKeyword(base::UTF8ToUTF16(keyword)); |
| 554 data.SetURL(template_url_string); | 563 data.SetURL(template_url_string); |
| 555 TemplateURL* url = new TemplateURL(data); | |
| 556 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info( | 564 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info( |
| 557 new TemplateURL::AssociatedExtensionInfo( | 565 new TemplateURL::AssociatedExtensionInfo( |
| 558 TemplateURL::OMNIBOX_API_EXTENSION, extension_id)); | 566 TemplateURL::OMNIBOX_API_EXTENSION, extension_id)); |
| 559 AddExtensionControlledTURL(url, std::move(info)); | 567 AddExtensionControlledTURL(base::MakeUnique<TemplateURL>(data), |
| 568 std::move(info)); |
| 560 } | 569 } |
| 561 | 570 |
| 562 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() { | 571 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() { |
| 563 return template_urls_; | 572 TemplateURLVector result; |
| 573 for (const auto& turl : template_urls_) |
| 574 result.push_back(turl.get()); |
| 575 return result; |
| 564 } | 576 } |
| 565 | 577 |
| 566 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { | 578 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { |
| 567 DCHECK(url); | 579 DCHECK(url); |
| 568 // Extension-controlled search engines are not persisted. | 580 // Extension-controlled search engines are not persisted. |
| 569 if (url->GetType() != TemplateURL::NORMAL) | 581 if (url->GetType() != TemplateURL::NORMAL) |
| 570 return; | 582 return; |
| 571 if (std::find(template_urls_.begin(), template_urls_.end(), url) == | 583 if (!Contains(&template_urls_, url)) |
| 572 template_urls_.end()) | |
| 573 return; | 584 return; |
| 574 ++url->data_.usage_count; | 585 ++url->data_.usage_count; |
| 575 | 586 |
| 576 if (web_data_service_.get()) | 587 if (web_data_service_.get()) |
| 577 web_data_service_->UpdateKeyword(url->data()); | 588 web_data_service_->UpdateKeyword(url->data()); |
| 578 } | 589 } |
| 579 | 590 |
| 580 void TemplateURLService::ResetTemplateURL(TemplateURL* url, | 591 void TemplateURLService::ResetTemplateURL(TemplateURL* url, |
| 581 const base::string16& title, | 592 const base::string16& title, |
| 582 const base::string16& keyword, | 593 const base::string16& keyword, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 599 TemplateURL* url) { | 610 TemplateURL* url) { |
| 600 // Omnibox keywords cannot be made default. Extension-controlled search | 611 // Omnibox keywords cannot be made default. Extension-controlled search |
| 601 // engines can be made default only by the extension itself because they | 612 // engines can be made default only by the extension itself because they |
| 602 // aren't persisted. | 613 // aren't persisted. |
| 603 DCHECK(!url || (url->GetType() == TemplateURL::NORMAL)); | 614 DCHECK(!url || (url->GetType() == TemplateURL::NORMAL)); |
| 604 if (load_failed_) { | 615 if (load_failed_) { |
| 605 // Skip the DefaultSearchManager, which will persist to user preferences. | 616 // Skip the DefaultSearchManager, which will persist to user preferences. |
| 606 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || | 617 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || |
| 607 (default_search_provider_source_ == | 618 (default_search_provider_source_ == |
| 608 DefaultSearchManager::FROM_FALLBACK)) { | 619 DefaultSearchManager::FROM_FALLBACK)) { |
| 609 ApplyDefaultSearchChange(url ? &url->data() : NULL, | 620 ApplyDefaultSearchChange(url ? &url->data() : nullptr, |
| 610 DefaultSearchManager::FROM_USER); | 621 DefaultSearchManager::FROM_USER); |
| 611 } | 622 } |
| 612 } else { | 623 } else { |
| 613 // We rely on the DefaultSearchManager to call OnDefaultSearchChange if, in | 624 // We rely on the DefaultSearchManager to call OnDefaultSearchChange if, in |
| 614 // fact, the effective DSE changes. | 625 // fact, the effective DSE changes. |
| 615 if (url) | 626 if (url) |
| 616 default_search_manager_.SetUserSelectedDefaultSearchEngine(url->data()); | 627 default_search_manager_.SetUserSelectedDefaultSearchEngine(url->data()); |
| 617 else | 628 else |
| 618 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); | 629 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); |
| 619 } | 630 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 643 | 654 |
| 644 void TemplateURLService::RepairPrepopulatedSearchEngines() { | 655 void TemplateURLService::RepairPrepopulatedSearchEngines() { |
| 645 // Can't clean DB if it hasn't been loaded. | 656 // Can't clean DB if it hasn't been loaded. |
| 646 DCHECK(loaded()); | 657 DCHECK(loaded()); |
| 647 | 658 |
| 648 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || | 659 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || |
| 649 (default_search_provider_source_ == | 660 (default_search_provider_source_ == |
| 650 DefaultSearchManager::FROM_FALLBACK)) { | 661 DefaultSearchManager::FROM_FALLBACK)) { |
| 651 // Clear |default_search_provider_| in case we want to remove the engine it | 662 // 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. | 663 // points to. This will get reset at the end of the function anyway. |
| 653 default_search_provider_ = NULL; | 664 default_search_provider_ = nullptr; |
| 654 } | 665 } |
| 655 | 666 |
| 656 size_t default_search_provider_index = 0; | 667 size_t default_search_provider_index = 0; |
| 657 ScopedVector<TemplateURLData> prepopulated_urls = | 668 ScopedVector<TemplateURLData> prepopulated_urls = |
| 658 TemplateURLPrepopulateData::GetPrepopulatedEngines( | 669 TemplateURLPrepopulateData::GetPrepopulatedEngines( |
| 659 prefs_, &default_search_provider_index); | 670 prefs_, &default_search_provider_index); |
| 660 DCHECK(!prepopulated_urls.empty()); | 671 DCHECK(!prepopulated_urls.empty()); |
| 661 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData( | 672 ActionsFromPrepopulateData actions(CreateActionsFromCurrentPrepopulateData( |
| 662 &prepopulated_urls, template_urls_, default_search_provider_)); | 673 &prepopulated_urls, template_urls_, default_search_provider_)); |
| 663 | 674 |
| 664 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 675 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 665 | 676 |
| 666 // Remove items. | 677 // Remove items. |
| 667 for (std::vector<TemplateURL*>::iterator i = actions.removed_engines.begin(); | 678 for (std::vector<TemplateURL*>::iterator i = actions.removed_engines.begin(); |
| 668 i < actions.removed_engines.end(); ++i) | 679 i < actions.removed_engines.end(); ++i) |
| 669 RemoveNoNotify(*i); | 680 RemoveNoNotify(*i); |
| 670 | 681 |
| 671 // Edit items. | 682 // Edit items. |
| 672 for (EditedEngines::iterator i(actions.edited_engines.begin()); | 683 for (EditedEngines::iterator i(actions.edited_engines.begin()); |
| 673 i < actions.edited_engines.end(); ++i) { | 684 i < actions.edited_engines.end(); ++i) { |
| 674 TemplateURL new_values(i->second); | 685 TemplateURL new_values(i->second); |
| 675 UpdateNoNotify(i->first, new_values); | 686 UpdateNoNotify(i->first, new_values); |
| 676 } | 687 } |
| 677 | 688 |
| 678 // Add items. | 689 // Add items. |
| 679 for (std::vector<TemplateURLData>::const_iterator i = | 690 for (std::vector<TemplateURLData>::const_iterator i = |
| 680 actions.added_engines.begin(); | 691 actions.added_engines.begin(); |
| 681 i < actions.added_engines.end(); | 692 i < actions.added_engines.end(); |
| 682 ++i) { | 693 ++i) { |
| 683 AddNoNotify(new TemplateURL(*i), true); | 694 AddNoNotify(base::MakeUnique<TemplateURL>(*i), true); |
| 684 } | 695 } |
| 685 | 696 |
| 686 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 697 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 687 &dsp_change_origin_, DSP_CHANGE_PROFILE_RESET); | 698 &dsp_change_origin_, DSP_CHANGE_PROFILE_RESET); |
| 688 | 699 |
| 689 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); | 700 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); |
| 690 | 701 |
| 691 if (!default_search_provider_) { | 702 if (!default_search_provider_) { |
| 692 // If the default search provider came from a user pref we would have been | 703 // If the default search provider came from a user pref we would have been |
| 693 // notified of the new (fallback-provided) value in | 704 // notified of the new (fallback-provided) value in |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 744 if (!result) { | 755 if (!result) { |
| 745 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 | 756 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 |
| 746 // is fixed. | 757 // is fixed. |
| 747 tracked_objects::ScopedTracker tracking_profile1( | 758 tracked_objects::ScopedTracker tracking_profile1( |
| 748 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 759 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 749 "422460 TemplateURLService::OnWebDataServiceRequestDone 1")); | 760 "422460 TemplateURLService::OnWebDataServiceRequestDone 1")); |
| 750 | 761 |
| 751 // Results are null if the database went away or (most likely) wasn't | 762 // Results are null if the database went away or (most likely) wasn't |
| 752 // loaded. | 763 // loaded. |
| 753 load_failed_ = true; | 764 load_failed_ = true; |
| 754 web_data_service_ = NULL; | 765 web_data_service_ = nullptr; |
| 755 ChangeToLoadedState(); | 766 ChangeToLoadedState(); |
| 756 return; | 767 return; |
| 757 } | 768 } |
| 758 | 769 |
| 759 TemplateURLVector template_urls; | 770 std::unique_ptr<OwnedTemplateURLVector> template_urls = |
| 771 base::MakeUnique<OwnedTemplateURLVector>(); |
| 760 int new_resource_keyword_version = 0; | 772 int new_resource_keyword_version = 0; |
| 761 { | 773 { |
| 762 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 | 774 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 |
| 763 // is fixed. | 775 // is fixed. |
| 764 tracked_objects::ScopedTracker tracking_profile2( | 776 tracked_objects::ScopedTracker tracking_profile2( |
| 765 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 777 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 766 "422460 TemplateURLService::OnWebDataServiceRequestDone 2")); | 778 "422460 TemplateURLService::OnWebDataServiceRequestDone 2")); |
| 767 | 779 |
| 768 GetSearchProvidersUsingKeywordResult( | 780 GetSearchProvidersUsingKeywordResult( |
| 769 *result, web_data_service_.get(), prefs_, &template_urls, | 781 *result, web_data_service_.get(), prefs_, template_urls.get(), |
| 770 (default_search_provider_source_ == DefaultSearchManager::FROM_USER) | 782 (default_search_provider_source_ == DefaultSearchManager::FROM_USER) |
| 771 ? initial_default_search_provider_.get() | 783 ? initial_default_search_provider_.get() |
| 772 : NULL, | 784 : nullptr, |
| 773 search_terms_data(), &new_resource_keyword_version, &pre_sync_deletes_); | 785 search_terms_data(), &new_resource_keyword_version, &pre_sync_deletes_); |
| 774 } | 786 } |
| 775 | 787 |
| 776 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 788 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 777 | 789 |
| 778 { | 790 { |
| 779 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 | 791 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 |
| 780 // is fixed. | 792 // is fixed. |
| 781 tracked_objects::ScopedTracker tracking_profile4( | 793 tracked_objects::ScopedTracker tracking_profile4( |
| 782 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 794 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 783 "422460 TemplateURLService::OnWebDataServiceRequestDone 4")); | 795 "422460 TemplateURLService::OnWebDataServiceRequestDone 4")); |
| 784 | 796 |
| 785 PatchMissingSyncGUIDs(&template_urls); | 797 PatchMissingSyncGUIDs(template_urls.get()); |
| 786 | 798 |
| 787 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 | 799 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 |
| 788 // is fixed. | 800 // is fixed. |
| 789 tracked_objects::ScopedTracker tracking_profile41( | 801 tracked_objects::ScopedTracker tracking_profile41( |
| 790 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 802 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 791 "422460 TemplateURLService::OnWebDataServiceRequestDone 41")); | 803 "422460 TemplateURLService::OnWebDataServiceRequestDone 41")); |
| 792 | 804 |
| 793 SetTemplateURLs(&template_urls); | 805 SetTemplateURLs(std::move(template_urls)); |
| 794 | 806 |
| 795 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 | 807 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 |
| 796 // is fixed. | 808 // is fixed. |
| 797 tracked_objects::ScopedTracker tracking_profile42( | 809 tracked_objects::ScopedTracker tracking_profile42( |
| 798 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 810 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 799 "422460 TemplateURLService::OnWebDataServiceRequestDone 42")); | 811 "422460 TemplateURLService::OnWebDataServiceRequestDone 42")); |
| 800 | 812 |
| 801 // This initializes provider_map_ which should be done before | 813 // This initializes provider_map_ which should be done before |
| 802 // calling UpdateKeywordSearchTermsForURL. | 814 // calling UpdateKeywordSearchTermsForURL. |
| 803 // This also calls NotifyObservers. | 815 // This also calls NotifyObservers. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 873 void TemplateURLService::Shutdown() { | 885 void TemplateURLService::Shutdown() { |
| 874 if (client_) | 886 if (client_) |
| 875 client_->Shutdown(); | 887 client_->Shutdown(); |
| 876 // This check has to be done at Shutdown() instead of in the dtor to ensure | 888 // 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 | 889 // that no clients of KeywordWebDataService are holding ptrs to it after the |
| 878 // first phase of the KeyedService Shutdown() process. | 890 // first phase of the KeyedService Shutdown() process. |
| 879 if (load_handle_) { | 891 if (load_handle_) { |
| 880 DCHECK(web_data_service_.get()); | 892 DCHECK(web_data_service_.get()); |
| 881 web_data_service_->CancelRequest(load_handle_); | 893 web_data_service_->CancelRequest(load_handle_); |
| 882 } | 894 } |
| 883 web_data_service_ = NULL; | 895 web_data_service_ = nullptr; |
| 884 } | 896 } |
| 885 | 897 |
| 886 syncer::SyncDataList TemplateURLService::GetAllSyncData( | 898 syncer::SyncDataList TemplateURLService::GetAllSyncData( |
| 887 syncer::ModelType type) const { | 899 syncer::ModelType type) const { |
| 888 DCHECK_EQ(syncer::SEARCH_ENGINES, type); | 900 DCHECK_EQ(syncer::SEARCH_ENGINES, type); |
| 889 | 901 |
| 890 syncer::SyncDataList current_data; | 902 syncer::SyncDataList current_data; |
| 891 for (TemplateURLVector::const_iterator iter = template_urls_.begin(); | 903 for (const auto& turl : template_urls_) { |
| 892 iter != template_urls_.end(); ++iter) { | |
| 893 // We don't sync keywords managed by policy. | 904 // We don't sync keywords managed by policy. |
| 894 if ((*iter)->created_by_policy()) | 905 if (turl->created_by_policy()) |
| 895 continue; | 906 continue; |
| 896 // We don't sync extension-controlled search engines. | 907 // We don't sync extension-controlled search engines. |
| 897 if ((*iter)->GetType() != TemplateURL::NORMAL) | 908 if (turl->GetType() != TemplateURL::NORMAL) |
| 898 continue; | 909 continue; |
| 899 current_data.push_back(CreateSyncDataFromTemplateURL(**iter)); | 910 current_data.push_back(CreateSyncDataFromTemplateURL(*turl)); |
| 900 } | 911 } |
| 901 | 912 |
| 902 return current_data; | 913 return current_data; |
| 903 } | 914 } |
| 904 | 915 |
| 905 syncer::SyncError TemplateURLService::ProcessSyncChanges( | 916 syncer::SyncError TemplateURLService::ProcessSyncChanges( |
| 906 const tracked_objects::Location& from_here, | 917 const tracked_objects::Location& from_here, |
| 907 const syncer::SyncChangeList& change_list) { | 918 const syncer::SyncChangeList& change_list) { |
| 908 if (!models_associated_) { | 919 if (!models_associated_) { |
| 909 syncer::SyncError error(FROM_HERE, | 920 syncer::SyncError error(FROM_HERE, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 if (existing_keyword_turl) { | 1008 if (existing_keyword_turl) { |
| 998 // Resolve any conflicts so we can safely add the new entry. | 1009 // Resolve any conflicts so we can safely add the new entry. |
| 999 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl, | 1010 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl, |
| 1000 &new_changes); | 1011 &new_changes); |
| 1001 } | 1012 } |
| 1002 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 1013 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 1003 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD); | 1014 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD); |
| 1004 // Force the local ID to kInvalidTemplateURLID so we can add it. | 1015 // Force the local ID to kInvalidTemplateURLID so we can add it. |
| 1005 TemplateURLData data(turl->data()); | 1016 TemplateURLData data(turl->data()); |
| 1006 data.id = kInvalidTemplateURLID; | 1017 data.id = kInvalidTemplateURLID; |
| 1007 TemplateURL* added = new TemplateURL(data); | 1018 std::unique_ptr<TemplateURL> added_ptr = |
| 1008 if (Add(added)) | 1019 base::MakeUnique<TemplateURL>(data); |
| 1020 TemplateURL* added = added_ptr.get(); |
| 1021 if (Add(std::move(added_ptr))) |
| 1009 MaybeUpdateDSEAfterSync(added); | 1022 MaybeUpdateDSEAfterSync(added); |
| 1010 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE) { | 1023 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE) { |
| 1011 if (!existing_turl) { | 1024 if (!existing_turl) { |
| 1012 error = sync_error_factory_->CreateAndUploadError( | 1025 error = sync_error_factory_->CreateAndUploadError( |
| 1013 FROM_HERE, | 1026 FROM_HERE, |
| 1014 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE"); | 1027 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE"); |
| 1015 continue; | 1028 continue; |
| 1016 } | 1029 } |
| 1017 if (existing_keyword_turl && (existing_keyword_turl != existing_turl)) { | 1030 if (existing_keyword_turl && (existing_keyword_turl != existing_turl)) { |
| 1018 // Resolve any conflicts with other entries so we can safely update the | 1031 // Resolve any conflicts with other entries so we can safely update the |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1271 | 1284 |
| 1272 // Past bugs might have caused either of these fields to be empty. Just | 1285 // Past bugs might have caused either of these fields to be empty. Just |
| 1273 // delete this data off the server. | 1286 // delete this data off the server. |
| 1274 if (specifics.url().empty() || specifics.sync_guid().empty()) { | 1287 if (specifics.url().empty() || specifics.sync_guid().empty()) { |
| 1275 change_list->push_back( | 1288 change_list->push_back( |
| 1276 syncer::SyncChange(FROM_HERE, | 1289 syncer::SyncChange(FROM_HERE, |
| 1277 syncer::SyncChange::ACTION_DELETE, | 1290 syncer::SyncChange::ACTION_DELETE, |
| 1278 sync_data)); | 1291 sync_data)); |
| 1279 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, | 1292 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, |
| 1280 DELETE_ENGINE_EMPTY_FIELD, DELETE_ENGINE_MAX); | 1293 DELETE_ENGINE_EMPTY_FIELD, DELETE_ENGINE_MAX); |
| 1281 return NULL; | 1294 return nullptr; |
| 1282 } | 1295 } |
| 1283 | 1296 |
| 1284 TemplateURLData data(existing_turl ? | 1297 TemplateURLData data(existing_turl ? |
| 1285 existing_turl->data() : TemplateURLData()); | 1298 existing_turl->data() : TemplateURLData()); |
| 1286 data.SetShortName(base::UTF8ToUTF16(specifics.short_name())); | 1299 data.SetShortName(base::UTF8ToUTF16(specifics.short_name())); |
| 1287 data.originating_url = GURL(specifics.originating_url()); | 1300 data.originating_url = GURL(specifics.originating_url()); |
| 1288 base::string16 keyword(base::UTF8ToUTF16(specifics.keyword())); | 1301 base::string16 keyword(base::UTF8ToUTF16(specifics.keyword())); |
| 1289 // NOTE: Once this code has shipped in a couple of stable releases, we can | 1302 // NOTE: Once this code has shipped in a couple of stable releases, we can |
| 1290 // probably remove the migration portion, comment out the | 1303 // probably remove the migration portion, comment out the |
| 1291 // "autogenerate_keyword" field entirely in the .proto file, and fold the | 1304 // "autogenerate_keyword" field entirely in the .proto file, and fold the |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1333 | 1346 |
| 1334 // We used to sync keywords associated with omnibox extensions, but no longer | 1347 // 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 | 1348 // 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 | 1349 // synced old versions of Chrome which were relying on them. Instead, for now |
| 1337 // we simply ignore these. | 1350 // we simply ignore these. |
| 1338 // TODO(vasilii): After a few Chrome versions, change this to go ahead and | 1351 // TODO(vasilii): After a few Chrome versions, change this to go ahead and |
| 1339 // delete these from sync. | 1352 // delete these from sync. |
| 1340 DCHECK(client); | 1353 DCHECK(client); |
| 1341 client->RestoreExtensionInfoIfNecessary(turl.get()); | 1354 client->RestoreExtensionInfoIfNecessary(turl.get()); |
| 1342 if (turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) | 1355 if (turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) |
| 1343 return NULL; | 1356 return nullptr; |
| 1344 | 1357 |
| 1345 DCHECK_EQ(TemplateURL::NORMAL, turl->GetType()); | 1358 DCHECK_EQ(TemplateURL::NORMAL, turl->GetType()); |
| 1346 if (reset_keyword || deduped) { | 1359 if (reset_keyword || deduped) { |
| 1347 if (reset_keyword) | 1360 if (reset_keyword) |
| 1348 turl->ResetKeywordIfNecessary(search_terms_data, true); | 1361 turl->ResetKeywordIfNecessary(search_terms_data, true); |
| 1349 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); | 1362 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); |
| 1350 change_list->push_back(syncer::SyncChange(FROM_HERE, | 1363 change_list->push_back(syncer::SyncChange(FROM_HERE, |
| 1351 syncer::SyncChange::ACTION_UPDATE, | 1364 syncer::SyncChange::ACTION_UPDATE, |
| 1352 sync_data)); | 1365 sync_data)); |
| 1353 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword(search_terms_data)) { | 1366 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword(search_terms_data)) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1417 DCHECK(initializers[i].keyword); | 1430 DCHECK(initializers[i].keyword); |
| 1418 DCHECK(initializers[i].url); | 1431 DCHECK(initializers[i].url); |
| 1419 DCHECK(initializers[i].content); | 1432 DCHECK(initializers[i].content); |
| 1420 | 1433 |
| 1421 // TemplateURLService ends up owning the TemplateURL, don't try and free | 1434 // TemplateURLService ends up owning the TemplateURL, don't try and free |
| 1422 // it. | 1435 // it. |
| 1423 TemplateURLData data; | 1436 TemplateURLData data; |
| 1424 data.SetShortName(base::UTF8ToUTF16(initializers[i].content)); | 1437 data.SetShortName(base::UTF8ToUTF16(initializers[i].content)); |
| 1425 data.SetKeyword(base::UTF8ToUTF16(initializers[i].keyword)); | 1438 data.SetKeyword(base::UTF8ToUTF16(initializers[i].keyword)); |
| 1426 data.SetURL(initializers[i].url); | 1439 data.SetURL(initializers[i].url); |
| 1427 TemplateURL* template_url = new TemplateURL(data); | 1440 AddNoNotify(base::MakeUnique<TemplateURL>(data), true); |
| 1428 AddNoNotify(template_url, true); | |
| 1429 | 1441 |
| 1430 // Set the first provided identifier to be the default. | 1442 // Set the first provided identifier to be the default. |
| 1431 if (i == 0) | 1443 if (i == 0) |
| 1432 default_search_manager_.SetUserSelectedDefaultSearchEngine(data); | 1444 default_search_manager_.SetUserSelectedDefaultSearchEngine(data); |
| 1433 } | 1445 } |
| 1434 } | 1446 } |
| 1435 | 1447 |
| 1436 // Request a server check for the correct Google URL if Google is the | 1448 // Request a server check for the correct Google URL if Google is the |
| 1437 // default search engine. | 1449 // default search engine. |
| 1438 RequestGoogleURLTrackerServerCheckIfNecessary(); | 1450 RequestGoogleURLTrackerServerCheckIfNecessary(); |
| 1439 } | 1451 } |
| 1440 | 1452 |
| 1441 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { | 1453 void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { |
| 1442 const base::string16& keyword = template_url->keyword(); | 1454 const base::string16& keyword = template_url->keyword(); |
| 1443 DCHECK_NE(0U, keyword_to_turl_and_length_.count(keyword)); | 1455 DCHECK_NE(0U, keyword_to_turl_and_length_.count(keyword)); |
| 1444 if (keyword_to_turl_and_length_[keyword].first == template_url) { | 1456 if (keyword_to_turl_and_length_[keyword].first == template_url) { |
| 1445 // We need to check whether the keyword can now be provided by another | 1457 // We need to check whether the keyword can now be provided by another |
| 1446 // TemplateURL. See the comments in AddToMaps() for more information on | 1458 // TemplateURL. See the comments in AddToMaps() for more information on |
| 1447 // extension keywords and how they can coexist with non-extension keywords. | 1459 // 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 | 1460 // 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 | 1461 // installed (which will be the most recently added, which will have the |
| 1450 // highest ID). | 1462 // highest ID). |
| 1451 TemplateURL* best_fallback = NULL; | 1463 TemplateURL* best_fallback = nullptr; |
| 1452 for (TemplateURLVector::const_iterator i(template_urls_.begin()); | 1464 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 | 1465 // This next statement relies on the fact that there can only be one |
| 1456 // non-Omnibox API TemplateURL with a given keyword. | 1466 // non-Omnibox API TemplateURL with a given keyword. |
| 1457 if ((turl != template_url) && (turl->keyword() == keyword) && | 1467 if ((turl.get() != template_url) && (turl->keyword() == keyword) && |
| 1458 (!best_fallback || | 1468 (!best_fallback || |
| 1459 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) || | 1469 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) || |
| 1460 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && | 1470 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && |
| 1461 (turl->id() > best_fallback->id())))) | 1471 (turl->id() > best_fallback->id())))) |
| 1462 best_fallback = turl; | 1472 best_fallback = turl.get(); |
| 1463 } | 1473 } |
| 1464 RemoveFromDomainMap(template_url); | 1474 RemoveFromDomainMap(template_url); |
| 1465 if (best_fallback) { | 1475 if (best_fallback) { |
| 1466 AddToMap(best_fallback); | 1476 AddToMap(best_fallback); |
| 1467 AddToDomainMap(best_fallback); | 1477 AddToDomainMap(best_fallback); |
| 1468 } else { | 1478 } else { |
| 1469 keyword_to_turl_and_length_.erase(keyword); | 1479 keyword_to_turl_and_length_.erase(keyword); |
| 1470 } | 1480 } |
| 1471 } | 1481 } |
| 1472 | 1482 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1544 } | 1554 } |
| 1545 } | 1555 } |
| 1546 | 1556 |
| 1547 void TemplateURLService::AddToMap(TemplateURL* template_url) { | 1557 void TemplateURLService::AddToMap(TemplateURL* template_url) { |
| 1548 const base::string16& keyword = template_url->keyword(); | 1558 const base::string16& keyword = template_url->keyword(); |
| 1549 keyword_to_turl_and_length_[keyword] = | 1559 keyword_to_turl_and_length_[keyword] = |
| 1550 TURLAndMeaningfulLength( | 1560 TURLAndMeaningfulLength( |
| 1551 template_url, GetMeaningfulKeywordLength(keyword, template_url)); | 1561 template_url, GetMeaningfulKeywordLength(keyword, template_url)); |
| 1552 } | 1562 } |
| 1553 | 1563 |
| 1554 // Helper for partition() call in next function. | 1564 void TemplateURLService::SetTemplateURLs( |
| 1555 bool HasValidID(TemplateURL* t_url) { | 1565 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 | 1566 // 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 | 1567 // 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 | 1568 // 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 | 1569 // 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 | 1570 // adding the second. If this happens, the second loop must not attempt to |
| 1565 // access the deleted entry. Partitioning ensures this constraint. | 1571 // access the deleted entry. Partitioning ensures this constraint. |
| 1566 TemplateURLVector::iterator first_invalid( | 1572 auto first_invalid = std::partition( |
| 1567 std::partition(urls->begin(), urls->end(), HasValidID)); | 1573 urls->begin(), urls->end(), [](const std::unique_ptr<TemplateURL>& turl) { |
| 1574 return turl->id() != kInvalidTemplateURLID; |
| 1575 }); |
| 1568 | 1576 |
| 1569 // 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 |
| 1570 // properly set. | 1578 // properly set. |
| 1571 for (TemplateURLVector::const_iterator i = urls->begin(); i != first_invalid; | 1579 for (auto i = urls->begin(); i != first_invalid; ++i) { |
| 1572 ++i) { | |
| 1573 next_id_ = std::max(next_id_, (*i)->id()); | 1580 next_id_ = std::max(next_id_, (*i)->id()); |
| 1574 AddNoNotify(*i, false); | 1581 AddNoNotify(std::move(*i), false); |
| 1575 } | 1582 } |
| 1576 | 1583 |
| 1577 // Next add the new items that don't have id's. | 1584 // Next add the new items that don't have id's. |
| 1578 for (TemplateURLVector::const_iterator i = first_invalid; i != urls->end(); | 1585 for (auto i = first_invalid; i != urls->end(); ++i) |
| 1579 ++i) | 1586 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 } | 1587 } |
| 1586 | 1588 |
| 1587 void TemplateURLService::ChangeToLoadedState() { | 1589 void TemplateURLService::ChangeToLoadedState() { |
| 1588 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is | 1590 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is |
| 1589 // fixed. | 1591 // fixed. |
| 1590 tracked_objects::ScopedTracker tracking_profile1( | 1592 tracked_objects::ScopedTracker tracking_profile1( |
| 1591 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1593 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1592 "422460 TemplateURLService::ChangeToLoadedState 1")); | 1594 "422460 TemplateURLService::ChangeToLoadedState 1")); |
| 1593 | 1595 |
| 1594 DCHECK(!loaded_); | 1596 DCHECK(!loaded_); |
| 1595 | 1597 |
| 1596 provider_map_->Init(template_urls_, search_terms_data()); | 1598 provider_map_->Init(template_urls_, search_terms_data()); |
| 1597 loaded_ = true; | 1599 loaded_ = true; |
| 1598 | 1600 |
| 1599 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is | 1601 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is |
| 1600 // fixed. | 1602 // fixed. |
| 1601 tracked_objects::ScopedTracker tracking_profile2( | 1603 tracked_objects::ScopedTracker tracking_profile2( |
| 1602 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1604 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1603 "422460 TemplateURLService::ChangeToLoadedState 2")); | 1605 "422460 TemplateURLService::ChangeToLoadedState 2")); |
| 1604 | 1606 |
| 1605 // This will cause a call to NotifyObservers(). | 1607 // This will cause a call to NotifyObservers(). |
| 1606 ApplyDefaultSearchChangeNoMetrics( | 1608 ApplyDefaultSearchChangeNoMetrics( |
| 1607 initial_default_search_provider_ ? | 1609 initial_default_search_provider_ |
| 1608 &initial_default_search_provider_->data() : NULL, | 1610 ? &initial_default_search_provider_->data() |
| 1611 : nullptr, |
| 1609 default_search_provider_source_); | 1612 default_search_provider_source_); |
| 1610 initial_default_search_provider_.reset(); | 1613 initial_default_search_provider_.reset(); |
| 1611 | 1614 |
| 1612 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is | 1615 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is |
| 1613 // fixed. | 1616 // fixed. |
| 1614 tracked_objects::ScopedTracker tracking_profile3( | 1617 tracked_objects::ScopedTracker tracking_profile3( |
| 1615 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1618 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1616 "422460 TemplateURLService::ChangeToLoadedState 3")); | 1619 "422460 TemplateURLService::ChangeToLoadedState 3")); |
| 1617 | 1620 |
| 1618 on_loaded_callbacks_.Notify(); | 1621 on_loaded_callbacks_.Notify(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1635 t_url->safe_for_autoreplace()); | 1638 t_url->safe_for_autoreplace()); |
| 1636 } | 1639 } |
| 1637 | 1640 |
| 1638 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword( | 1641 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword( |
| 1639 const base::string16& keyword) { | 1642 const base::string16& keyword) { |
| 1640 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword); | 1643 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword); |
| 1641 if (!keyword_turl || (keyword_turl->GetType() == TemplateURL::NORMAL)) | 1644 if (!keyword_turl || (keyword_turl->GetType() == TemplateURL::NORMAL)) |
| 1642 return keyword_turl; | 1645 return keyword_turl; |
| 1643 // The extension keyword in the model may be hiding a replaceable | 1646 // The extension keyword in the model may be hiding a replaceable |
| 1644 // non-extension keyword. Look for it. | 1647 // non-extension keyword. Look for it. |
| 1645 for (TemplateURLVector::const_iterator i(template_urls_.begin()); | 1648 for (const auto& turl : template_urls_) { |
| 1646 i != template_urls_.end(); ++i) { | 1649 if ((turl->GetType() == TemplateURL::NORMAL) && |
| 1647 if (((*i)->GetType() == TemplateURL::NORMAL) && | 1650 (turl->keyword() == keyword)) |
| 1648 ((*i)->keyword() == keyword)) | 1651 return turl.get(); |
| 1649 return *i; | |
| 1650 } | 1652 } |
| 1651 return NULL; | 1653 return nullptr; |
| 1652 } | 1654 } |
| 1653 | 1655 |
| 1654 bool TemplateURLService::UpdateNoNotify(TemplateURL* existing_turl, | 1656 bool TemplateURLService::UpdateNoNotify(TemplateURL* existing_turl, |
| 1655 const TemplateURL& new_values) { | 1657 const TemplateURL& new_values) { |
| 1656 DCHECK(existing_turl); | 1658 DCHECK(existing_turl); |
| 1657 if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == | 1659 if (!Contains(&template_urls_, existing_turl)) |
| 1658 template_urls_.end()) | |
| 1659 return false; | 1660 return false; |
| 1660 | 1661 |
| 1661 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType()); | 1662 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType()); |
| 1662 | 1663 |
| 1663 base::string16 old_keyword(existing_turl->keyword()); | 1664 base::string16 old_keyword(existing_turl->keyword()); |
| 1664 keyword_to_turl_and_length_.erase(old_keyword); | 1665 keyword_to_turl_and_length_.erase(old_keyword); |
| 1665 RemoveFromDomainMap(existing_turl); | 1666 RemoveFromDomainMap(existing_turl); |
| 1666 if (!existing_turl->sync_guid().empty()) | 1667 if (!existing_turl->sync_guid().empty()) |
| 1667 guid_to_turl_.erase(existing_turl->sync_guid()); | 1668 guid_to_turl_.erase(existing_turl->sync_guid()); |
| 1668 | 1669 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1821 initial_default_search_provider_->HasGoogleBaseURLs( | 1822 initial_default_search_provider_->HasGoogleBaseURLs( |
| 1822 search_terms_data())) | 1823 search_terms_data())) |
| 1823 initial_default_search_provider_->InvalidateCachedValues(); | 1824 initial_default_search_provider_->InvalidateCachedValues(); |
| 1824 initial_default_search_provider_->ResetKeywordIfNecessary( | 1825 initial_default_search_provider_->ResetKeywordIfNecessary( |
| 1825 search_terms_data(), false); | 1826 search_terms_data(), false); |
| 1826 return; | 1827 return; |
| 1827 } | 1828 } |
| 1828 | 1829 |
| 1829 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 1830 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 1830 bool something_changed = false; | 1831 bool something_changed = false; |
| 1831 for (TemplateURLVector::iterator i(template_urls_.begin()); | 1832 for (const auto& turl : template_urls_) { |
| 1832 i != template_urls_.end(); ++i) { | 1833 if (turl->HasGoogleBaseURLs(search_terms_data())) { |
| 1833 TemplateURL* t_url = *i; | 1834 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); | 1835 updated_turl.ResetKeywordIfNecessary(search_terms_data(), false); |
| 1837 KeywordToTURLAndMeaningfulLength::const_iterator existing_entry = | 1836 KeywordToTURLAndMeaningfulLength::const_iterator existing_entry = |
| 1838 keyword_to_turl_and_length_.find(updated_turl.keyword()); | 1837 keyword_to_turl_and_length_.find(updated_turl.keyword()); |
| 1839 if ((existing_entry != keyword_to_turl_and_length_.end()) && | 1838 if ((existing_entry != keyword_to_turl_and_length_.end()) && |
| 1840 (existing_entry->second.first != t_url)) { | 1839 (existing_entry->second.first != turl.get())) { |
| 1841 // The new autogenerated keyword conflicts with another TemplateURL. | 1840 // The new autogenerated keyword conflicts with another TemplateURL. |
| 1842 // Overwrite it if it's replaceable; otherwise, leave |t_url| using its | 1841 // Overwrite it if it's replaceable; otherwise, leave |turl| using its |
| 1843 // current keyword. (This will not prevent |t_url| from auto-updating | 1842 // current keyword. (This will not prevent |turl| from auto-updating |
| 1844 // the keyword in the future if the conflicting TemplateURL disappears.) | 1843 // the keyword in the future if the conflicting TemplateURL disappears.) |
| 1845 // 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 |
| 1846 // |provider_map_| will not be updated correctly. | 1845 // |provider_map_| will not be updated correctly. |
| 1847 if (CanReplace(existing_entry->second.first)) | 1846 if (CanReplace(existing_entry->second.first)) |
| 1848 RemoveNoNotify(existing_entry->second.first); | 1847 RemoveNoNotify(existing_entry->second.first); |
| 1849 else | 1848 else |
| 1850 updated_turl.data_.SetKeyword(t_url->keyword()); | 1849 updated_turl.data_.SetKeyword(turl->keyword()); |
| 1851 } | 1850 } |
| 1852 something_changed = true; | 1851 something_changed = true; |
| 1853 // 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 |
| 1854 // 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 |
| 1855 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData(). | 1854 // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData(). |
| 1856 UpdateNoNotify(t_url, updated_turl); | 1855 UpdateNoNotify(turl.get(), updated_turl); |
| 1857 } | 1856 } |
| 1858 } | 1857 } |
| 1859 if (something_changed) | 1858 if (something_changed) |
| 1860 NotifyObservers(); | 1859 NotifyObservers(); |
| 1861 } | 1860 } |
| 1862 | 1861 |
| 1863 void TemplateURLService::OnDefaultSearchChange( | 1862 void TemplateURLService::OnDefaultSearchChange( |
| 1864 const TemplateURLData* data, | 1863 const TemplateURLData* data, |
| 1865 DefaultSearchManager::Source source) { | 1864 DefaultSearchManager::Source source) { |
| 1866 if (prefs_ && (source == DefaultSearchManager::FROM_USER) && | 1865 if (prefs_ && (source == DefaultSearchManager::FROM_USER) && |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1889 bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( | 1888 bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( |
| 1890 const TemplateURLData* data, | 1889 const TemplateURLData* data, |
| 1891 DefaultSearchManager::Source source) { | 1890 DefaultSearchManager::Source source) { |
| 1892 if (!loaded_) { | 1891 if (!loaded_) { |
| 1893 // Set |initial_default_search_provider_| from the preferences. This is | 1892 // 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 | 1893 // 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 | 1894 // of keywords from Web Data is the owner of everything including the |
| 1896 // default. | 1895 // default. |
| 1897 bool changed = TemplateURL::MatchesData( | 1896 bool changed = TemplateURL::MatchesData( |
| 1898 initial_default_search_provider_.get(), data, search_terms_data()); | 1897 initial_default_search_provider_.get(), data, search_terms_data()); |
| 1899 initial_default_search_provider_.reset( | 1898 initial_default_search_provider_ = |
| 1900 data ? new TemplateURL(*data) : NULL); | 1899 data ? base::MakeUnique<TemplateURL>(*data) : nullptr; |
| 1901 default_search_provider_source_ = source; | 1900 default_search_provider_source_ = source; |
| 1902 return changed; | 1901 return changed; |
| 1903 } | 1902 } |
| 1904 | 1903 |
| 1905 // 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_. |
| 1906 // 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 |
| 1907 // false positive for recursion when the initial_default_search_provider_ is | 1906 // 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. | 1907 // NULL due to policy. We'll never actually get recursion with data == NULL. |
| 1909 if (source == default_search_provider_source_ && data != NULL && | 1908 if (source == default_search_provider_source_ && data != nullptr && |
| 1910 TemplateURL::MatchesData(default_search_provider_, data, | 1909 TemplateURL::MatchesData(default_search_provider_, data, |
| 1911 search_terms_data())) | 1910 search_terms_data())) |
| 1912 return false; | 1911 return false; |
| 1913 | 1912 |
| 1914 // 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 |
| 1915 // a change. | 1914 // a change. |
| 1916 TemplateURL* previous_default_search_engine = default_search_provider_; | 1915 TemplateURL* previous_default_search_engine = default_search_provider_; |
| 1917 | 1916 |
| 1918 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 1917 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 1919 if (default_search_provider_source_ == DefaultSearchManager::FROM_POLICY || | 1918 if (default_search_provider_source_ == DefaultSearchManager::FROM_POLICY || |
| 1920 source == DefaultSearchManager::FROM_POLICY) { | 1919 source == DefaultSearchManager::FROM_POLICY) { |
| 1921 // 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 |
| 1922 // well as to add the new one, if appropriate. | 1921 // well as to add the new one, if appropriate. |
| 1923 UpdateProvidersCreatedByPolicy( | 1922 UpdateProvidersCreatedByPolicy( |
| 1924 &template_urls_, | 1923 &template_urls_, |
| 1925 source == DefaultSearchManager::FROM_POLICY ? data : NULL); | 1924 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); |
| 1926 } | 1925 } |
| 1927 | 1926 |
| 1928 if (!data) { | 1927 if (!data) { |
| 1929 default_search_provider_ = NULL; | 1928 default_search_provider_ = nullptr; |
| 1930 } else if (source == DefaultSearchManager::FROM_EXTENSION) { | 1929 } else if (source == DefaultSearchManager::FROM_EXTENSION) { |
| 1931 default_search_provider_ = FindMatchingExtensionTemplateURL( | 1930 default_search_provider_ = FindMatchingExtensionTemplateURL( |
| 1932 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | 1931 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1933 } else if (source == DefaultSearchManager::FROM_FALLBACK) { | 1932 } else if (source == DefaultSearchManager::FROM_FALLBACK) { |
| 1934 default_search_provider_ = | 1933 default_search_provider_ = |
| 1935 FindPrepopulatedTemplateURL(data->prepopulate_id); | 1934 FindPrepopulatedTemplateURL(data->prepopulate_id); |
| 1936 if (default_search_provider_) { | 1935 if (default_search_provider_) { |
| 1937 TemplateURLData update_data(*data); | 1936 TemplateURLData update_data(*data); |
| 1938 update_data.sync_guid = default_search_provider_->sync_guid(); | 1937 update_data.sync_guid = default_search_provider_->sync_guid(); |
| 1939 if (!default_search_provider_->safe_for_autoreplace()) { | 1938 if (!default_search_provider_->safe_for_autoreplace()) { |
| 1940 update_data.safe_for_autoreplace = false; | 1939 update_data.safe_for_autoreplace = false; |
| 1941 update_data.SetKeyword(default_search_provider_->keyword()); | 1940 update_data.SetKeyword(default_search_provider_->keyword()); |
| 1942 update_data.SetShortName(default_search_provider_->short_name()); | 1941 update_data.SetShortName(default_search_provider_->short_name()); |
| 1943 } | 1942 } |
| 1944 UpdateNoNotify(default_search_provider_, TemplateURL(update_data)); | 1943 UpdateNoNotify(default_search_provider_, TemplateURL(update_data)); |
| 1945 } else { | 1944 } else { |
| 1946 // Normally the prepopulated fallback should be present in | 1945 // Normally the prepopulated fallback should be present in |
| 1947 // |template_urls_|, but in a few cases it might not be: | 1946 // |template_urls_|, but in a few cases it might not be: |
| 1948 // (1) Tests that initialize the TemplateURLService in peculiar ways. | 1947 // (1) Tests that initialize the TemplateURLService in peculiar ways. |
| 1949 // (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 |
| 1950 // lost their user-selected value. | 1949 // lost their user-selected value. |
| 1951 TemplateURL* new_dse = new TemplateURL(*data); | 1950 std::unique_ptr<TemplateURL> new_dse_ptr = |
| 1952 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)) |
| 1953 default_search_provider_ = new_dse; | 1954 default_search_provider_ = new_dse; |
| 1954 } | 1955 } |
| 1955 } else if (source == DefaultSearchManager::FROM_USER) { | 1956 } else if (source == DefaultSearchManager::FROM_USER) { |
| 1956 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid); | 1957 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid); |
| 1957 if (!default_search_provider_ && data->prepopulate_id) { | 1958 if (!default_search_provider_ && data->prepopulate_id) { |
| 1958 default_search_provider_ = | 1959 default_search_provider_ = |
| 1959 FindPrepopulatedTemplateURL(data->prepopulate_id); | 1960 FindPrepopulatedTemplateURL(data->prepopulate_id); |
| 1960 } | 1961 } |
| 1961 TemplateURLData new_data(*data); | 1962 TemplateURLData new_data(*data); |
| 1962 new_data.show_in_default_list = true; | 1963 new_data.show_in_default_list = true; |
| 1963 if (default_search_provider_) { | 1964 if (default_search_provider_) { |
| 1964 UpdateNoNotify(default_search_provider_, TemplateURL(new_data)); | 1965 UpdateNoNotify(default_search_provider_, TemplateURL(new_data)); |
| 1965 } else { | 1966 } else { |
| 1966 new_data.id = kInvalidTemplateURLID; | 1967 new_data.id = kInvalidTemplateURLID; |
| 1967 TemplateURL* new_dse = new TemplateURL(new_data); | 1968 std::unique_ptr<TemplateURL> new_dse_ptr = |
| 1968 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)) |
| 1969 default_search_provider_ = new_dse; | 1972 default_search_provider_ = new_dse; |
| 1970 } | 1973 } |
| 1971 if (default_search_provider_ && prefs_) { | 1974 if (default_search_provider_ && prefs_) { |
| 1972 prefs_->SetString(prefs::kSyncedDefaultSearchProviderGUID, | 1975 prefs_->SetString(prefs::kSyncedDefaultSearchProviderGUID, |
| 1973 default_search_provider_->sync_guid()); | 1976 default_search_provider_->sync_guid()); |
| 1974 } | 1977 } |
| 1975 | 1978 |
| 1976 } | 1979 } |
| 1977 | 1980 |
| 1978 default_search_provider_source_ = source; | 1981 default_search_provider_source_ = source; |
| 1979 | 1982 |
| 1980 bool changed = default_search_provider_ != previous_default_search_engine; | 1983 bool changed = default_search_provider_ != previous_default_search_engine; |
| 1981 if (changed) | 1984 if (changed) |
| 1982 RequestGoogleURLTrackerServerCheckIfNecessary(); | 1985 RequestGoogleURLTrackerServerCheckIfNecessary(); |
| 1983 | 1986 |
| 1984 NotifyObservers(); | 1987 NotifyObservers(); |
| 1985 | 1988 |
| 1986 return changed; | 1989 return changed; |
| 1987 } | 1990 } |
| 1988 | 1991 |
| 1989 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, | 1992 TemplateURL* TemplateURLService::AddNoNotify( |
| 1990 bool newly_adding) { | 1993 std::unique_ptr<TemplateURL> template_url, |
| 1994 bool newly_adding) { |
| 1991 DCHECK(template_url); | 1995 DCHECK(template_url); |
| 1992 | 1996 |
| 1993 if (newly_adding) { | 1997 if (newly_adding) { |
| 1994 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 1998 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
| 1995 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), | 1999 DCHECK(!Contains(&template_urls_, template_url.get())); |
| 1996 template_url) == template_urls_.end()); | |
| 1997 template_url->data_.id = ++next_id_; | 2000 template_url->data_.id = ++next_id_; |
| 1998 } | 2001 } |
| 1999 | 2002 |
| 2000 template_url->ResetKeywordIfNecessary(search_terms_data(), false); | 2003 template_url->ResetKeywordIfNecessary(search_terms_data(), false); |
| 2001 // Check whether |template_url|'s keyword conflicts with any already in the | 2004 // Check whether |template_url|'s keyword conflicts with any already in the |
| 2002 // model. | 2005 // model. |
| 2003 TemplateURL* existing_keyword_turl = | 2006 TemplateURL* existing_keyword_turl = |
| 2004 GetTemplateURLForKeyword(template_url->keyword()); | 2007 GetTemplateURLForKeyword(template_url->keyword()); |
| 2005 | 2008 |
| 2006 // Check whether |template_url|'s keyword conflicts with any already in the | 2009 // 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 | 2010 // 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, | 2011 // processing the template URLs from the web data service. In this case, |
| 2009 // GetTemplateURLForKeyword() will look not only at what's already in the | 2012 // GetTemplateURLForKeyword() will look not only at what's already in the |
| 2010 // model, but at the |initial_default_search_provider_|. Since this engine | 2013 // 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 | 2014 // 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 | 2015 // that any "pre-existing" entries we find are actually coming from |
| 2013 // |template_urls_|, lest we detect a "conflict" between the | 2016 // |template_urls_|, lest we detect a "conflict" between the |
| 2014 // |initial_default_search_provider_| and the web data version of itself. | 2017 // |initial_default_search_provider_| and the web data version of itself. |
| 2015 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && | 2018 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && |
| 2016 existing_keyword_turl && | 2019 existing_keyword_turl && |
| 2017 existing_keyword_turl->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && | 2020 existing_keyword_turl->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && |
| 2018 (std::find(template_urls_.begin(), template_urls_.end(), | 2021 Contains(&template_urls_, existing_keyword_turl)) { |
| 2019 existing_keyword_turl) != template_urls_.end())) { | 2022 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, | 2023 // Only replace one of the TemplateURLs if they are either both extensions, |
| 2022 // or both not extensions. | 2024 // or both not extensions. |
| 2023 bool are_same_type = existing_keyword_turl->GetType() == | 2025 bool are_same_type = existing_keyword_turl->GetType() == |
| 2024 template_url->GetType(); | 2026 template_url->GetType(); |
| 2025 if (CanReplace(existing_keyword_turl) && are_same_type) { | 2027 if (CanReplace(existing_keyword_turl) && are_same_type) { |
| 2026 RemoveNoNotify(existing_keyword_turl); | 2028 RemoveNoNotify(existing_keyword_turl); |
| 2027 } else if (CanReplace(template_url) && are_same_type) { | 2029 } else if (CanReplace(template_url.get()) && are_same_type) { |
| 2028 delete template_url; | 2030 return nullptr; |
| 2029 return false; | |
| 2030 } else { | 2031 } else { |
| 2031 base::string16 new_keyword = | 2032 base::string16 new_keyword = |
| 2032 UniquifyKeyword(*existing_keyword_turl, false); | 2033 UniquifyKeyword(*existing_keyword_turl, false); |
| 2033 ResetTemplateURLNoNotify(existing_keyword_turl, | 2034 ResetTemplateURLNoNotify(existing_keyword_turl, |
| 2034 existing_keyword_turl->short_name(), new_keyword, | 2035 existing_keyword_turl->short_name(), new_keyword, |
| 2035 existing_keyword_turl->url()); | 2036 existing_keyword_turl->url()); |
| 2036 } | 2037 } |
| 2037 } | 2038 } |
| 2038 template_urls_.push_back(template_url); | 2039 TemplateURL* template_url_ptr = template_url.get(); |
| 2039 AddToMaps(template_url); | 2040 template_urls_.push_back(std::move(template_url)); |
| 2041 AddToMaps(template_url_ptr); |
| 2040 | 2042 |
| 2041 if (newly_adding && | 2043 if (newly_adding && (template_url_ptr->GetType() == TemplateURL::NORMAL)) { |
| 2042 (template_url->GetType() == TemplateURL::NORMAL)) { | |
| 2043 if (web_data_service_.get()) | 2044 if (web_data_service_.get()) |
| 2044 web_data_service_->AddKeyword(template_url->data()); | 2045 web_data_service_->AddKeyword(template_url_ptr->data()); |
| 2045 | 2046 |
| 2046 // Inform sync of the addition. Note that this will assign a GUID to | 2047 // Inform sync of the addition. Note that this will assign a GUID to |
| 2047 // template_url and add it to the guid_to_turl_. | 2048 // template_url and add it to the guid_to_turl_. |
| 2048 ProcessTemplateURLChange(FROM_HERE, | 2049 ProcessTemplateURLChange(FROM_HERE, template_url_ptr, |
| 2049 template_url, | |
| 2050 syncer::SyncChange::ACTION_ADD); | 2050 syncer::SyncChange::ACTION_ADD); |
| 2051 } | 2051 } |
| 2052 | 2052 |
| 2053 return true; | 2053 return template_url_ptr; |
| 2054 } | 2054 } |
| 2055 | 2055 |
| 2056 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { | 2056 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { |
| 2057 DCHECK(template_url != default_search_provider_); | 2057 DCHECK(template_url != default_search_provider_); |
| 2058 | 2058 |
| 2059 TemplateURLVector::iterator i = | 2059 auto i = FindTemplateURL(&template_urls_, template_url); |
| 2060 std::find(template_urls_.begin(), template_urls_.end(), template_url); | |
| 2061 if (i == template_urls_.end()) | 2060 if (i == template_urls_.end()) |
| 2062 return; | 2061 return; |
| 2063 | 2062 |
| 2064 RemoveFromMaps(template_url); | 2063 RemoveFromMaps(template_url); |
| 2065 | 2064 |
| 2066 // Remove it from the vector containing all TemplateURLs. | 2065 // Remove it from the vector containing all TemplateURLs. |
| 2066 std::unique_ptr<TemplateURL> scoped_turl = std::move(*i); |
| 2067 template_urls_.erase(i); | 2067 template_urls_.erase(i); |
| 2068 | 2068 |
| 2069 if (template_url->GetType() == TemplateURL::NORMAL) { | 2069 if (template_url->GetType() == TemplateURL::NORMAL) { |
| 2070 if (web_data_service_.get()) | 2070 if (web_data_service_.get()) |
| 2071 web_data_service_->RemoveKeyword(template_url->id()); | 2071 web_data_service_->RemoveKeyword(template_url->id()); |
| 2072 | 2072 |
| 2073 // Inform sync of the deletion. | 2073 // Inform sync of the deletion. |
| 2074 ProcessTemplateURLChange(FROM_HERE, | 2074 ProcessTemplateURLChange(FROM_HERE, |
| 2075 template_url, | 2075 template_url, |
| 2076 syncer::SyncChange::ACTION_DELETE); | 2076 syncer::SyncChange::ACTION_DELETE); |
| 2077 | 2077 |
| 2078 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, | 2078 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, |
| 2079 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); | 2079 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); |
| 2080 } | 2080 } |
| 2081 | 2081 |
| 2082 if (loaded_ && client_) | 2082 if (loaded_ && client_) |
| 2083 client_->DeleteAllSearchTermsForKeyword(template_url->id()); | 2083 client_->DeleteAllSearchTermsForKeyword(template_url->id()); |
| 2084 | |
| 2085 // We own the TemplateURL and need to delete it. | |
| 2086 delete template_url; | |
| 2087 } | 2084 } |
| 2088 | 2085 |
| 2089 bool TemplateURLService::ResetTemplateURLNoNotify( | 2086 bool TemplateURLService::ResetTemplateURLNoNotify( |
| 2090 TemplateURL* url, | 2087 TemplateURL* url, |
| 2091 const base::string16& title, | 2088 const base::string16& title, |
| 2092 const base::string16& keyword, | 2089 const base::string16& keyword, |
| 2093 const std::string& search_url) { | 2090 const std::string& search_url) { |
| 2094 DCHECK(!keyword.empty()); | 2091 DCHECK(!keyword.empty()); |
| 2095 DCHECK(!search_url.empty()); | 2092 DCHECK(!search_url.empty()); |
| 2096 TemplateURLData data(url->data()); | 2093 TemplateURLData data(url->data()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 2115 } | 2112 } |
| 2116 | 2113 |
| 2117 // |template_urls| are the TemplateURLs loaded from the database. | 2114 // |template_urls| are the TemplateURLs loaded from the database. |
| 2118 // |default_from_prefs| is the default search provider from the preferences, or | 2115 // |default_from_prefs| is the default search provider from the preferences, or |
| 2119 // NULL if the DSE is not policy-defined. | 2116 // NULL if the DSE is not policy-defined. |
| 2120 // | 2117 // |
| 2121 // This function removes from the vector and the database all the TemplateURLs | 2118 // 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 | 2119 // 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. | 2120 // which case it is updated with the data from prefs. |
| 2124 void TemplateURLService::UpdateProvidersCreatedByPolicy( | 2121 void TemplateURLService::UpdateProvidersCreatedByPolicy( |
| 2125 TemplateURLVector* template_urls, | 2122 OwnedTemplateURLVector* template_urls, |
| 2126 const TemplateURLData* default_from_prefs) { | 2123 const TemplateURLData* default_from_prefs) { |
| 2127 DCHECK(template_urls); | 2124 DCHECK(template_urls); |
| 2128 | 2125 |
| 2129 for (TemplateURLVector::iterator i = template_urls->begin(); | 2126 for (auto i = template_urls->begin(); i != template_urls->end();) { |
| 2130 i != template_urls->end(); ) { | 2127 TemplateURL* template_url = i->get(); |
| 2131 TemplateURL* template_url = *i; | |
| 2132 if (template_url->created_by_policy()) { | 2128 if (template_url->created_by_policy()) { |
| 2133 if (default_from_prefs && | 2129 if (default_from_prefs && |
| 2134 TemplateURL::MatchesData(template_url, default_from_prefs, | 2130 TemplateURL::MatchesData(template_url, default_from_prefs, |
| 2135 search_terms_data())) { | 2131 search_terms_data())) { |
| 2136 // If the database specified a default search provider that was set | 2132 // If the database specified a default search provider that was set |
| 2137 // by policy, and the default search provider from the preferences | 2133 // 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 | 2134 // is also set by policy and they are the same, keep the entry in the |
| 2139 // database and the |default_search_provider|. | 2135 // database and the |default_search_provider|. |
| 2140 default_search_provider_ = template_url; | 2136 default_search_provider_ = template_url; |
| 2141 // Prevent us from saving any other entries, or creating a new one. | 2137 // Prevent us from saving any other entries, or creating a new one. |
| 2142 default_from_prefs = NULL; | 2138 default_from_prefs = nullptr; |
| 2143 ++i; | 2139 ++i; |
| 2144 continue; | 2140 continue; |
| 2145 } | 2141 } |
| 2146 | 2142 |
| 2143 TemplateURLID id = template_url->id(); |
| 2147 RemoveFromMaps(template_url); | 2144 RemoveFromMaps(template_url); |
| 2148 i = template_urls->erase(i); | 2145 i = template_urls->erase(i); |
| 2149 if (web_data_service_.get()) | 2146 if (web_data_service_.get()) |
| 2150 web_data_service_->RemoveKeyword(template_url->id()); | 2147 web_data_service_->RemoveKeyword(id); |
| 2151 delete template_url; | |
| 2152 } else { | 2148 } else { |
| 2153 ++i; | 2149 ++i; |
| 2154 } | 2150 } |
| 2155 } | 2151 } |
| 2156 | 2152 |
| 2157 if (default_from_prefs) { | 2153 if (default_from_prefs) { |
| 2158 default_search_provider_ = NULL; | 2154 default_search_provider_ = nullptr; |
| 2159 default_search_provider_source_ = DefaultSearchManager::FROM_POLICY; | 2155 default_search_provider_source_ = DefaultSearchManager::FROM_POLICY; |
| 2160 TemplateURLData new_data(*default_from_prefs); | 2156 TemplateURLData new_data(*default_from_prefs); |
| 2161 if (new_data.sync_guid.empty()) | 2157 if (new_data.sync_guid.empty()) |
| 2162 new_data.sync_guid = base::GenerateGUID(); | 2158 new_data.sync_guid = base::GenerateGUID(); |
| 2163 new_data.created_by_policy = true; | 2159 new_data.created_by_policy = true; |
| 2164 TemplateURL* new_dse = new TemplateURL(new_data); | 2160 std::unique_ptr<TemplateURL> new_dse_ptr = |
| 2165 if (AddNoNotify(new_dse, true)) | 2161 base::MakeUnique<TemplateURL>(new_data); |
| 2162 TemplateURL* new_dse = new_dse_ptr.get(); |
| 2163 if (AddNoNotify(std::move(new_dse_ptr), true)) |
| 2166 default_search_provider_ = new_dse; | 2164 default_search_provider_ = new_dse; |
| 2167 } | 2165 } |
| 2168 } | 2166 } |
| 2169 | 2167 |
| 2170 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, | 2168 void TemplateURLService::ResetTemplateURLGUID(TemplateURL* url, |
| 2171 const std::string& guid) { | 2169 const std::string& guid) { |
| 2172 DCHECK(loaded_); | 2170 DCHECK(loaded_); |
| 2173 DCHECK(!guid.empty()); | 2171 DCHECK(!guid.empty()); |
| 2174 | 2172 |
| 2175 TemplateURLData data(url->data()); | 2173 TemplateURLData data(url->data()); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2345 } | 2343 } |
| 2346 // Remove the local data so it isn't written to sync. | 2344 // Remove the local data so it isn't written to sync. |
| 2347 local_data->erase(guid); | 2345 local_data->erase(guid); |
| 2348 } | 2346 } |
| 2349 } | 2347 } |
| 2350 | 2348 |
| 2351 if (should_add_sync_turl) { | 2349 if (should_add_sync_turl) { |
| 2352 // Force the local ID to kInvalidTemplateURLID so we can add it. | 2350 // Force the local ID to kInvalidTemplateURLID so we can add it. |
| 2353 TemplateURLData data(sync_turl->data()); | 2351 TemplateURLData data(sync_turl->data()); |
| 2354 data.id = kInvalidTemplateURLID; | 2352 data.id = kInvalidTemplateURLID; |
| 2355 TemplateURL* added = new TemplateURL(data); | 2353 std::unique_ptr<TemplateURL> added_ptr = |
| 2354 base::MakeUnique<TemplateURL>(data); |
| 2355 TemplateURL* added = added_ptr.get(); |
| 2356 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2356 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 2357 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD); | 2357 &dsp_change_origin_, DSP_CHANGE_SYNC_ADD); |
| 2358 if (Add(added)) | 2358 if (Add(std::move(added_ptr))) |
| 2359 MaybeUpdateDSEAfterSync(added); | 2359 MaybeUpdateDSEAfterSync(added); |
| 2360 merge_result->set_num_items_added( | 2360 merge_result->set_num_items_added(merge_result->num_items_added() + 1); |
| 2361 merge_result->num_items_added() + 1); | |
| 2362 } | 2361 } |
| 2363 } | 2362 } |
| 2364 | 2363 |
| 2365 void TemplateURLService::PatchMissingSyncGUIDs( | 2364 void TemplateURLService::PatchMissingSyncGUIDs( |
| 2366 TemplateURLVector* template_urls) { | 2365 OwnedTemplateURLVector* template_urls) { |
| 2367 DCHECK(template_urls); | 2366 DCHECK(template_urls); |
| 2368 for (TemplateURLVector::iterator i = template_urls->begin(); | 2367 for (auto& template_url : *template_urls) { |
| 2369 i != template_urls->end(); ++i) { | |
| 2370 TemplateURL* template_url = *i; | |
| 2371 DCHECK(template_url); | 2368 DCHECK(template_url); |
| 2372 if (template_url->sync_guid().empty() && | 2369 if (template_url->sync_guid().empty() && |
| 2373 (template_url->GetType() == TemplateURL::NORMAL)) { | 2370 (template_url->GetType() == TemplateURL::NORMAL)) { |
| 2374 template_url->data_.sync_guid = base::GenerateGUID(); | 2371 template_url->data_.sync_guid = base::GenerateGUID(); |
| 2375 if (web_data_service_.get()) | 2372 if (web_data_service_.get()) |
| 2376 web_data_service_->UpdateKeyword(template_url->data()); | 2373 web_data_service_->UpdateKeyword(template_url->data()); |
| 2377 } | 2374 } |
| 2378 } | 2375 } |
| 2379 } | 2376 } |
| 2380 | 2377 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2418 for (typename Container::const_iterator i(match_range.first); | 2415 for (typename Container::const_iterator i(match_range.first); |
| 2419 i != match_range.second; ++i) { | 2416 i != match_range.second; ++i) { |
| 2420 if (!supports_replacement_only || | 2417 if (!supports_replacement_only || |
| 2421 i->second.first->url_ref().SupportsReplacement(search_terms_data())) | 2418 i->second.first->url_ref().SupportsReplacement(search_terms_data())) |
| 2422 matches->push_back(i->second); | 2419 matches->push_back(i->second); |
| 2423 } | 2420 } |
| 2424 } | 2421 } |
| 2425 | 2422 |
| 2426 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL( | 2423 TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL( |
| 2427 int prepopulated_id) { | 2424 int prepopulated_id) { |
| 2428 for (TemplateURLVector::const_iterator i = template_urls_.begin(); | 2425 for (const auto& turl : template_urls_) { |
| 2429 i != template_urls_.end(); ++i) { | 2426 if (turl->prepopulate_id() == prepopulated_id) |
| 2430 if ((*i)->prepopulate_id() == prepopulated_id) | 2427 return turl.get(); |
| 2431 return *i; | |
| 2432 } | 2428 } |
| 2433 return NULL; | 2429 return nullptr; |
| 2434 } | 2430 } |
| 2435 | 2431 |
| 2436 TemplateURL* TemplateURLService::FindTemplateURLForExtension( | 2432 TemplateURL* TemplateURLService::FindTemplateURLForExtension( |
| 2437 const std::string& extension_id, | 2433 const std::string& extension_id, |
| 2438 TemplateURL::Type type) { | 2434 TemplateURL::Type type) { |
| 2439 DCHECK_NE(TemplateURL::NORMAL, type); | 2435 DCHECK_NE(TemplateURL::NORMAL, type); |
| 2440 for (TemplateURLVector::const_iterator i = template_urls_.begin(); | 2436 for (const auto& turl : template_urls_) { |
| 2441 i != template_urls_.end(); ++i) { | 2437 if (turl->GetType() == type && turl->GetExtensionId() == extension_id) |
| 2442 if ((*i)->GetType() == type && | 2438 return turl.get(); |
| 2443 (*i)->GetExtensionId() == extension_id) | |
| 2444 return *i; | |
| 2445 } | 2439 } |
| 2446 return NULL; | 2440 return nullptr; |
| 2447 } | 2441 } |
| 2448 | 2442 |
| 2449 TemplateURL* TemplateURLService::FindMatchingExtensionTemplateURL( | 2443 TemplateURL* TemplateURLService::FindMatchingExtensionTemplateURL( |
| 2450 const TemplateURLData& data, | 2444 const TemplateURLData& data, |
| 2451 TemplateURL::Type type) { | 2445 TemplateURL::Type type) { |
| 2452 DCHECK_NE(TemplateURL::NORMAL, type); | 2446 DCHECK_NE(TemplateURL::NORMAL, type); |
| 2453 for (TemplateURLVector::const_iterator i = template_urls_.begin(); | 2447 for (const auto& turl : template_urls_) { |
| 2454 i != template_urls_.end(); ++i) { | 2448 if (turl->GetType() == type && |
| 2455 if ((*i)->GetType() == type && | 2449 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) |
| 2456 TemplateURL::MatchesData(*i, &data, search_terms_data())) | 2450 return turl.get(); |
| 2457 return *i; | |
| 2458 } | 2451 } |
| 2459 return NULL; | 2452 return nullptr; |
| 2460 } | 2453 } |
| 2461 | 2454 |
| 2462 void TemplateURLService::UpdateExtensionDefaultSearchEngine() { | 2455 void TemplateURLService::UpdateExtensionDefaultSearchEngine() { |
| 2463 TemplateURL* most_recently_intalled_default = NULL; | 2456 TemplateURL* most_recently_intalled_default = nullptr; |
| 2464 for (TemplateURLVector::const_iterator i = template_urls_.begin(); | 2457 for (const auto& turl : template_urls_) { |
| 2465 i != template_urls_.end(); ++i) { | 2458 if ((turl->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && |
| 2466 if (((*i)->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && | 2459 turl->extension_info_->wants_to_be_default_engine && |
| 2467 (*i)->extension_info_->wants_to_be_default_engine && | 2460 turl->SupportsReplacement(search_terms_data()) && |
| 2468 (*i)->SupportsReplacement(search_terms_data()) && | |
| 2469 (!most_recently_intalled_default || | 2461 (!most_recently_intalled_default || |
| 2470 (most_recently_intalled_default->extension_info_->install_time < | 2462 (most_recently_intalled_default->extension_info_->install_time < |
| 2471 (*i)->extension_info_->install_time))) | 2463 turl->extension_info_->install_time))) |
| 2472 most_recently_intalled_default = *i; | 2464 most_recently_intalled_default = turl.get(); |
| 2473 } | 2465 } |
| 2474 | 2466 |
| 2475 if (most_recently_intalled_default) { | 2467 if (most_recently_intalled_default) { |
| 2476 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2468 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 2477 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | 2469 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); |
| 2478 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | 2470 default_search_manager_.SetExtensionControlledDefaultSearchEngine( |
| 2479 most_recently_intalled_default->data()); | 2471 most_recently_intalled_default->data()); |
| 2480 } else { | 2472 } else { |
| 2481 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | 2473 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); |
| 2482 } | 2474 } |
| 2483 } | 2475 } |
| OLD | NEW |