diff options
Diffstat (limited to 'chromium/components/search_engines/template_url_service.cc')
-rw-r--r-- | chromium/components/search_engines/template_url_service.cc | 490 |
1 files changed, 248 insertions, 242 deletions
diff --git a/chromium/components/search_engines/template_url_service.cc b/chromium/components/search_engines/template_url_service.cc index 064a37db4e6..29a06edce37 100644 --- a/chromium/components/search_engines/template_url_service.cc +++ b/chromium/components/search_engines/template_url_service.cc @@ -8,14 +8,14 @@ #include <utility> #include "base/auto_reset.h" +#include "base/callback.h" #include "base/command_line.h" #include "base/compiler_specific.h" #include "base/guid.h" #include "base/i18n/case_conversion.h" -#include "base/memory/scoped_vector.h" +#include "base/memory/ptr_util.h" #include "base/metrics/histogram_macros.h" #include "base/profiler/scoped_tracker.h" -#include "base/stl_util.h" #include "base/strings/string_split.h" #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" @@ -33,13 +33,13 @@ #include "components/search_engines/template_url_service_client.h" #include "components/search_engines/template_url_service_observer.h" #include "components/search_engines/util.h" +#include "components/sync/api/sync_change.h" +#include "components/sync/api/sync_error_factory.h" +#include "components/sync/protocol/search_engine_specifics.pb.h" +#include "components/sync/protocol/sync.pb.h" #include "components/url_formatter/url_fixer.h" #include "components/url_formatter/url_formatter.h" #include "net/base/registry_controlled_domains/registry_controlled_domain.h" -#include "sync/api/sync_change.h" -#include "sync/api/sync_error_factory.h" -#include "sync/protocol/search_engine_specifics.pb.h" -#include "sync/protocol/sync.pb.h" #include "url/gurl.h" typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; @@ -182,7 +182,17 @@ size_t GetMeaningfulKeywordLength(const base::string16& keyword, // The meaningful keyword length is the length of any portion before the // registry ("co.uk") and its preceding dot. return keyword.length() - (registry_length ? (registry_length + 1) : 0); +} + +bool Contains(TemplateURLService::OwnedTemplateURLVector* template_urls, + TemplateURL* turl) { + return FindTemplateURL(template_urls, turl) != template_urls->end(); +} +bool IsCreatedByExtension(TemplateURL* template_url) { + return template_url->type() == + TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION || + template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION; } } // namespace @@ -206,9 +216,9 @@ class TemplateURLService::LessWithPrefix { bool operator()( const KeywordToTURLAndMeaningfulLength::value_type& elem1, const KeywordToTURLAndMeaningfulLength::value_type& elem2) const { - return (elem1.second.first == NULL) ? - (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) : - (elem1.first < elem2.first); + return (elem1.second.first == nullptr) + ? (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) + : (elem1.first < elem2.first); } }; @@ -235,7 +245,7 @@ TemplateURLService::TemplateURLService( load_failed_(false), disable_load_(false), load_handle_(0), - default_search_provider_(NULL), + default_search_provider_(nullptr), next_id_(kInvalidTemplateURLID + 1), clock_(new base::DefaultClock), models_associated_(false), @@ -246,22 +256,22 @@ TemplateURLService::TemplateURLService( base::Bind(&TemplateURLService::OnDefaultSearchChange, base::Unretained(this))) { DCHECK(search_terms_data_); - Init(NULL, 0); + Init(nullptr, 0); } TemplateURLService::TemplateURLService(const Initializer* initializers, const int count) - : prefs_(NULL), + : prefs_(nullptr), search_terms_data_(new SearchTermsData), - web_data_service_(NULL), - google_url_tracker_(NULL), - rappor_service_(NULL), + web_data_service_(nullptr), + google_url_tracker_(nullptr), + rappor_service_(nullptr), provider_map_(new SearchHostToURLsMap), loaded_(false), load_failed_(false), disable_load_(false), load_handle_(0), - default_search_provider_(NULL), + default_search_provider_(nullptr), next_id_(kInvalidTemplateURLID + 1), clock_(new base::DefaultClock), models_associated_(false), @@ -277,15 +287,19 @@ TemplateURLService::TemplateURLService(const Initializer* initializers, TemplateURLService::~TemplateURLService() { // |web_data_service_| should be deleted during Shutdown(). DCHECK(!web_data_service_.get()); - STLDeleteElements(&template_urls_); } // static void TemplateURLService::RegisterProfilePrefs( user_prefs::PrefRegistrySyncable* registry) { +#if defined(OS_IOS) || defined(OS_ANDROID) + uint32_t flags = PrefRegistry::NO_REGISTRATION_FLAGS; +#else + uint32_t flags = user_prefs::PrefRegistrySyncable::SYNCABLE_PREF; +#endif registry->RegisterStringPref(prefs::kSyncedDefaultSearchProviderGUID, std::string(), - user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); + flags); registry->RegisterBooleanPref(prefs::kDefaultSearchProviderEnabled, true); registry->RegisterStringPref(prefs::kDefaultSearchProviderName, std::string()); @@ -405,10 +419,10 @@ TemplateURL* TemplateURLService::GetTemplateURLForKeyword( keyword_to_turl_and_length_.find(keyword)); if (elem != keyword_to_turl_and_length_.end()) return elem->second.first; - return (!loaded_ && - initial_default_search_provider_.get() && - (initial_default_search_provider_->keyword() == keyword)) ? - initial_default_search_provider_.get() : NULL; + return (!loaded_ && initial_default_search_provider_ && + (initial_default_search_provider_->keyword() == keyword)) + ? initial_default_search_provider_.get() + : nullptr; } TemplateURL* TemplateURLService::GetTemplateURLForGUID( @@ -416,10 +430,10 @@ TemplateURL* TemplateURLService::GetTemplateURLForGUID( GUIDToTURL::const_iterator elem(guid_to_turl_.find(sync_guid)); if (elem != guid_to_turl_.end()) return elem->second; - return (!loaded_ && - initial_default_search_provider_.get() && - (initial_default_search_provider_->sync_guid() == sync_guid)) ? - initial_default_search_provider_.get() : NULL; + return (!loaded_ && initial_default_search_provider_ && + (initial_default_search_provider_->sync_guid() == sync_guid)) + ? initial_default_search_provider_.get() + : nullptr; } TemplateURL* TemplateURLService::GetTemplateURLForHost( @@ -427,52 +441,59 @@ TemplateURL* TemplateURLService::GetTemplateURLForHost( if (loaded_) return provider_map_->GetTemplateURLForHost(host); TemplateURL* initial_dsp = initial_default_search_provider_.get(); - if (!initial_dsp) - return NULL; - return (initial_dsp->GenerateSearchURL(search_terms_data()).host() == host) ? - initial_dsp : NULL; + return (initial_dsp && + (initial_dsp->GenerateSearchURL(search_terms_data()).host() == host)) + ? initial_dsp + : nullptr; } -bool TemplateURLService::Add(TemplateURL* template_url) { +TemplateURL* TemplateURLService::Add( + std::unique_ptr<TemplateURL> template_url) { KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); - if (!AddNoNotify(template_url, true)) - return false; - NotifyObservers(); - return true; + TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); + if (template_url_ptr) + NotifyObservers(); + return template_url_ptr; } -void TemplateURLService::AddWithOverrides(TemplateURL* template_url, - const base::string16& short_name, - const base::string16& keyword, - const std::string& url) { +TemplateURL* TemplateURLService::AddWithOverrides( + std::unique_ptr<TemplateURL> template_url, + const base::string16& short_name, + const base::string16& keyword, + const std::string& url) { DCHECK(!short_name.empty()); DCHECK(!keyword.empty()); DCHECK(!url.empty()); template_url->data_.SetShortName(short_name); template_url->data_.SetKeyword(keyword); template_url->SetURL(url); - Add(template_url); + return Add(std::move(template_url)); } -void TemplateURLService::AddExtensionControlledTURL( - TemplateURL* template_url, +TemplateURL* TemplateURLService::AddExtensionControlledTURL( + std::unique_ptr<TemplateURL> template_url, std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) { DCHECK(loaded_); DCHECK(template_url); DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); DCHECK(info); - DCHECK_NE(TemplateURL::NORMAL, info->type); + DCHECK_NE(TemplateURL::NORMAL, template_url->type()); DCHECK_EQ(info->wants_to_be_default_engine, template_url->show_in_default_list()); - DCHECK(!FindTemplateURLForExtension(info->extension_id, info->type)); + DCHECK( + !FindTemplateURLForExtension(info->extension_id, template_url->type())); template_url->extension_info_.swap(info); KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); - if (AddNoNotify(template_url, true)) { - if (template_url->extension_info_->wants_to_be_default_engine) + TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); + if (template_url_ptr) { + if (template_url_ptr->extension_info_->wants_to_be_default_engine) { UpdateExtensionDefaultSearchEngine(); + } NotifyObservers(); } + + return template_url_ptr; } void TemplateURLService::Remove(TemplateURL* template_url) { @@ -490,7 +511,7 @@ void TemplateURLService::RemoveExtensionControlledTURL( // NULL this out so that we can call RemoveNoNotify. // UpdateExtensionDefaultSearchEngine will cause it to be reset. if (default_search_provider_ == url) - default_search_provider_ = NULL; + default_search_provider_ = nullptr; KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); RemoveNoNotify(url); UpdateExtensionDefaultSearchEngine(); @@ -503,25 +524,25 @@ void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, base::Time created_before) { - RemoveAutoGeneratedForOriginBetween(GURL(), created_after, created_before); + RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), + created_after, created_before); } -void TemplateURLService::RemoveAutoGeneratedForOriginBetween( - const GURL& origin, +void TemplateURLService::RemoveAutoGeneratedForUrlsBetween( + const base::Callback<bool(const GURL&)>& url_filter, base::Time created_after, base::Time created_before) { - GURL o(origin.GetOrigin()); bool should_notify = false; KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); for (size_t i = 0; i < template_urls_.size();) { if (template_urls_[i]->date_created() >= created_after && (created_before.is_null() || template_urls_[i]->date_created() < created_before) && - CanReplace(template_urls_[i]) && - (o.is_empty() || - template_urls_[i]->GenerateSearchURL( - search_terms_data()).GetOrigin() == o)) { - RemoveNoNotify(template_urls_[i]); + CanReplace(template_urls_[i].get()) && + (url_filter.is_null() || + url_filter.Run( + template_urls_[i]->GenerateSearchURL(search_terms_data())))) { + RemoveNoNotify(template_urls_[i].get()); should_notify = true; } else { ++i; @@ -546,24 +567,26 @@ void TemplateURLService::RegisterOmniboxKeyword( data.SetShortName(base::UTF8ToUTF16(extension_name)); data.SetKeyword(base::UTF8ToUTF16(keyword)); data.SetURL(template_url_string); - TemplateURL* url = new TemplateURL(data); std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info( - new TemplateURL::AssociatedExtensionInfo( - TemplateURL::OMNIBOX_API_EXTENSION, extension_id)); - AddExtensionControlledTURL(url, std::move(info)); + new TemplateURL::AssociatedExtensionInfo(extension_id)); + AddExtensionControlledTURL( + base::MakeUnique<TemplateURL>(data, TemplateURL::OMNIBOX_API_EXTENSION), + std::move(info)); } TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() { - return template_urls_; + TemplateURLVector result; + for (const auto& turl : template_urls_) + result.push_back(turl.get()); + return result; } void TemplateURLService::IncrementUsageCount(TemplateURL* url) { DCHECK(url); // Extension-controlled search engines are not persisted. - if (url->GetType() != TemplateURL::NORMAL) + if (url->type() != TemplateURL::NORMAL) return; - if (std::find(template_urls_.begin(), template_urls_.end(), url) == - template_urls_.end()) + if (!Contains(&template_urls_, url)) return; ++url->data_.usage_count; @@ -586,7 +609,7 @@ bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { DefaultSearchManager::FROM_FALLBACK)) && (url != GetDefaultSearchProvider()) && url->url_ref().SupportsReplacement(search_terms_data()) && - (url->GetType() == TemplateURL::NORMAL); + (url->type() == TemplateURL::NORMAL); } void TemplateURLService::SetUserSelectedDefaultSearchProvider( @@ -594,13 +617,13 @@ void TemplateURLService::SetUserSelectedDefaultSearchProvider( // Omnibox keywords cannot be made default. Extension-controlled search // engines can be made default only by the extension itself because they // aren't persisted. - DCHECK(!url || (url->GetType() == TemplateURL::NORMAL)); + DCHECK(!url || !IsCreatedByExtension(url)); if (load_failed_) { // Skip the DefaultSearchManager, which will persist to user preferences. if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || (default_search_provider_source_ == DefaultSearchManager::FROM_FALLBACK)) { - ApplyDefaultSearchChange(url ? &url->data() : NULL, + ApplyDefaultSearchChange(url ? &url->data() : nullptr, DefaultSearchManager::FROM_USER); } } else { @@ -644,11 +667,11 @@ void TemplateURLService::RepairPrepopulatedSearchEngines() { DefaultSearchManager::FROM_FALLBACK)) { // Clear |default_search_provider_| in case we want to remove the engine it // points to. This will get reset at the end of the function anyway. - default_search_provider_ = NULL; + default_search_provider_ = nullptr; } size_t default_search_provider_index = 0; - ScopedVector<TemplateURLData> prepopulated_urls = + std::vector<std::unique_ptr<TemplateURLData>> prepopulated_urls = TemplateURLPrepopulateData::GetPrepopulatedEngines( prefs_, &default_search_provider_index); DCHECK(!prepopulated_urls.empty()); @@ -674,7 +697,7 @@ void TemplateURLService::RepairPrepopulatedSearchEngines() { actions.added_engines.begin(); i < actions.added_engines.end(); ++i) { - AddNoNotify(new TemplateURL(*i), true); + AddNoNotify(base::MakeUnique<TemplateURL>(*i), true); } base::AutoReset<DefaultSearchChangeOrigin> change_origin( @@ -745,12 +768,13 @@ void TemplateURLService::OnWebDataServiceRequestDone( // Results are null if the database went away or (most likely) wasn't // loaded. load_failed_ = true; - web_data_service_ = NULL; + web_data_service_ = nullptr; ChangeToLoadedState(); return; } - TemplateURLVector template_urls; + std::unique_ptr<OwnedTemplateURLVector> template_urls = + base::MakeUnique<OwnedTemplateURLVector>(); int new_resource_keyword_version = 0; { // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 @@ -760,10 +784,10 @@ void TemplateURLService::OnWebDataServiceRequestDone( "422460 TemplateURLService::OnWebDataServiceRequestDone 2")); GetSearchProvidersUsingKeywordResult( - *result, web_data_service_.get(), prefs_, &template_urls, + *result, web_data_service_.get(), prefs_, template_urls.get(), (default_search_provider_source_ == DefaultSearchManager::FROM_USER) ? initial_default_search_provider_.get() - : NULL, + : nullptr, search_terms_data(), &new_resource_keyword_version, &pre_sync_deletes_); } @@ -776,7 +800,7 @@ void TemplateURLService::OnWebDataServiceRequestDone( FROM_HERE_WITH_EXPLICIT_FUNCTION( "422460 TemplateURLService::OnWebDataServiceRequestDone 4")); - PatchMissingSyncGUIDs(&template_urls); + PatchMissingSyncGUIDs(template_urls.get()); // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 // is fixed. @@ -784,7 +808,7 @@ void TemplateURLService::OnWebDataServiceRequestDone( FROM_HERE_WITH_EXPLICIT_FUNCTION( "422460 TemplateURLService::OnWebDataServiceRequestDone 41")); - SetTemplateURLs(&template_urls); + SetTemplateURLs(std::move(template_urls)); // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 // is fixed. @@ -850,7 +874,7 @@ base::string16 TemplateURLService::GetKeywordShortName( // to track changes to the model, this should become a DCHECK. if (template_url) { *is_omnibox_api_extension_keyword = - template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; + template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION; return template_url->AdjustedShortNameForLocaleDirection(); } *is_omnibox_api_extension_keyword = false; @@ -874,7 +898,7 @@ void TemplateURLService::Shutdown() { DCHECK(web_data_service_.get()); web_data_service_->CancelRequest(load_handle_); } - web_data_service_ = NULL; + web_data_service_ = nullptr; } syncer::SyncDataList TemplateURLService::GetAllSyncData( @@ -882,15 +906,14 @@ syncer::SyncDataList TemplateURLService::GetAllSyncData( DCHECK_EQ(syncer::SEARCH_ENGINES, type); syncer::SyncDataList current_data; - for (TemplateURLVector::const_iterator iter = template_urls_.begin(); - iter != template_urls_.end(); ++iter) { + for (const auto& turl : template_urls_) { // We don't sync keywords managed by policy. - if ((*iter)->created_by_policy()) + if (turl->created_by_policy()) continue; // We don't sync extension-controlled search engines. - if ((*iter)->GetType() != TemplateURL::NORMAL) + if (turl->type() != TemplateURL::NORMAL) continue; - current_data.push_back(CreateSyncDataFromTemplateURL(**iter)); + current_data.push_back(CreateSyncDataFromTemplateURL(*turl)); } return current_data; @@ -998,8 +1021,10 @@ syncer::SyncError TemplateURLService::ProcessSyncChanges( // Force the local ID to kInvalidTemplateURLID so we can add it. TemplateURLData data(turl->data()); data.id = kInvalidTemplateURLID; - TemplateURL* added = new TemplateURL(data); - if (Add(added)) + std::unique_ptr<TemplateURL> added_ptr = + base::MakeUnique<TemplateURL>(data); + TemplateURL* added = added_ptr.get(); + if (Add(std::move(added_ptr))) MaybeUpdateDSEAfterSync(added); } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE) { if (!existing_turl) { @@ -1192,7 +1217,7 @@ void TemplateURLService::ProcessTemplateURLChange( return; // Avoid syncing extension-controlled search engines. - if (turl->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) + if (turl->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) return; syncer::SyncChangeList changes; @@ -1272,7 +1297,7 @@ TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( sync_data)); UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, DELETE_ENGINE_EMPTY_FIELD, DELETE_ENGINE_MAX); - return NULL; + return nullptr; } TemplateURLData data(existing_turl ? @@ -1326,17 +1351,19 @@ TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( UpdateTemplateURLIfPrepopulated(turl.get(), prefs); // We used to sync keywords associated with omnibox extensions, but no longer - // want to. However, if we delete these keywords from sync, we'll break any - // synced old versions of Chrome which were relying on them. Instead, for now - // we simply ignore these. - // TODO(vasilii): After a few Chrome versions, change this to go ahead and - // delete these from sync. + // want to. Delete them from the server. + // TODO(vasilii): After a few Chrome versions, delete this code together with + // IsOmniboxExtensionURL(). DCHECK(client); - client->RestoreExtensionInfoIfNecessary(turl.get()); - if (turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) - return NULL; + if (client->IsOmniboxExtensionURL(turl->url())) { + change_list->push_back( + syncer::SyncChange(FROM_HERE, + syncer::SyncChange::ACTION_DELETE, + sync_data)); + return nullptr; + } - DCHECK_EQ(TemplateURL::NORMAL, turl->GetType()); + DCHECK_EQ(TemplateURL::NORMAL, turl->type()); if (reset_keyword || deduped) { if (reset_keyword) turl->ResetKeywordIfNecessary(search_terms_data, true); @@ -1418,8 +1445,7 @@ void TemplateURLService::Init(const Initializer* initializers, data.SetShortName(base::UTF8ToUTF16(initializers[i].content)); data.SetKeyword(base::UTF8ToUTF16(initializers[i].keyword)); data.SetURL(initializers[i].url); - TemplateURL* template_url = new TemplateURL(data); - AddNoNotify(template_url, true); + AddNoNotify(base::MakeUnique<TemplateURL>(data), true); // Set the first provided identifier to be the default. if (i == 0) @@ -1442,18 +1468,16 @@ void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { // In the case of more than one extension, we use the most recently // installed (which will be the most recently added, which will have the // highest ID). - TemplateURL* best_fallback = NULL; - for (TemplateURLVector::const_iterator i(template_urls_.begin()); - i != template_urls_.end(); ++i) { - TemplateURL* turl = *i; + TemplateURL* best_fallback = nullptr; + for (const auto& turl : template_urls_) { // This next statement relies on the fact that there can only be one // non-Omnibox API TemplateURL with a given keyword. - if ((turl != template_url) && (turl->keyword() == keyword) && + if ((turl.get() != template_url) && (turl->keyword() == keyword) && (!best_fallback || - (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) || - ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && + (best_fallback->type() != TemplateURL::OMNIBOX_API_EXTENSION) || + ((turl->type() == TemplateURL::OMNIBOX_API_EXTENSION) && (turl->id() > best_fallback->id())))) - best_fallback = turl; + best_fallback = turl.get(); } RemoveFromDomainMap(template_url); if (best_fallback) { @@ -1464,7 +1488,7 @@ void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { } } - if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) + if (template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION) return; if (!template_url->sync_guid().empty()) @@ -1477,7 +1501,7 @@ void TemplateURLService::RemoveFromMaps(TemplateURL* template_url) { void TemplateURLService::AddToMaps(TemplateURL* template_url) { bool template_url_is_omnibox_api = - template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; + template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION; const base::string16& keyword = template_url->keyword(); KeywordToTURLAndMeaningfulLength::const_iterator i = keyword_to_turl_and_length_.find(keyword); @@ -1491,7 +1515,7 @@ void TemplateURLService::AddToMaps(TemplateURL* template_url) { // Manually-modified keywords > extension keywords > replaceable keywords // When there are multiple extensions, the last-added wins. bool existing_url_is_omnibox_api = - existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; + existing_url->type() == TemplateURL::OMNIBOX_API_EXTENSION; DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api); if (existing_url_is_omnibox_api ? !CanReplace(template_url) : CanReplace(existing_url)) { @@ -1545,37 +1569,29 @@ void TemplateURLService::AddToMap(TemplateURL* template_url) { template_url, GetMeaningfulKeywordLength(keyword, template_url)); } -// Helper for partition() call in next function. -bool HasValidID(TemplateURL* t_url) { - return t_url->id() != kInvalidTemplateURLID; -} - -void TemplateURLService::SetTemplateURLs(TemplateURLVector* urls) { +void TemplateURLService::SetTemplateURLs( + std::unique_ptr<OwnedTemplateURLVector> urls) { // Partition the URLs first, instead of implementing the loops below by simply // scanning the input twice. While it's not supposed to happen normally, it's // possible for corrupt databases to return multiple entries with the same // keyword. In this case, the first loop may delete the first entry when // adding the second. If this happens, the second loop must not attempt to // access the deleted entry. Partitioning ensures this constraint. - TemplateURLVector::iterator first_invalid( - std::partition(urls->begin(), urls->end(), HasValidID)); + auto first_invalid = std::partition( + urls->begin(), urls->end(), [](const std::unique_ptr<TemplateURL>& turl) { + return turl->id() != kInvalidTemplateURLID; + }); // First, add the items that already have id's, so that the next_id_ gets // properly set. - for (TemplateURLVector::const_iterator i = urls->begin(); i != first_invalid; - ++i) { + for (auto i = urls->begin(); i != first_invalid; ++i) { next_id_ = std::max(next_id_, (*i)->id()); - AddNoNotify(*i, false); + AddNoNotify(std::move(*i), false); } // Next add the new items that don't have id's. - for (TemplateURLVector::const_iterator i = first_invalid; i != urls->end(); - ++i) - AddNoNotify(*i, true); - - // Clear the input vector to reduce the chance callers will try to use a - // (possibly deleted) entry. - urls->clear(); + for (auto i = first_invalid; i != urls->end(); ++i) + AddNoNotify(std::move(*i), true); } void TemplateURLService::ChangeToLoadedState() { @@ -1598,8 +1614,9 @@ void TemplateURLService::ChangeToLoadedState() { // This will cause a call to NotifyObservers(). ApplyDefaultSearchChangeNoMetrics( - initial_default_search_provider_ ? - &initial_default_search_provider_->data() : NULL, + initial_default_search_provider_ + ? &initial_default_search_provider_->data() + : nullptr, default_search_provider_source_); initial_default_search_provider_.reset(); @@ -1632,27 +1649,25 @@ bool TemplateURLService::CanReplace(const TemplateURL* t_url) { TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword( const base::string16& keyword) { TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword); - if (!keyword_turl || (keyword_turl->GetType() == TemplateURL::NORMAL)) + if (!keyword_turl || (keyword_turl->type() == TemplateURL::NORMAL)) return keyword_turl; // The extension keyword in the model may be hiding a replaceable // non-extension keyword. Look for it. - for (TemplateURLVector::const_iterator i(template_urls_.begin()); - i != template_urls_.end(); ++i) { - if (((*i)->GetType() == TemplateURL::NORMAL) && - ((*i)->keyword() == keyword)) - return *i; + for (const auto& turl : template_urls_) { + if ((turl->type() == TemplateURL::NORMAL) && + (turl->keyword() == keyword)) + return turl.get(); } - return NULL; + return nullptr; } bool TemplateURLService::UpdateNoNotify(TemplateURL* existing_turl, const TemplateURL& new_values) { DCHECK(existing_turl); - if (std::find(template_urls_.begin(), template_urls_.end(), existing_turl) == - template_urls_.end()) + if (!Contains(&template_urls_, existing_turl)) return false; - DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType()); + DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->type()); base::string16 old_keyword(existing_turl->keyword()); keyword_to_turl_and_length_.erase(old_keyword); @@ -1688,7 +1703,7 @@ bool TemplateURLService::UpdateNoNotify(TemplateURL* existing_turl, // case we delete the existing keyword if it's replaceable, or else undo // the change in keyword for |existing_turl|. TemplateURL* existing_keyword_turl = i->second.first; - if (existing_keyword_turl->GetType() != TemplateURL::NORMAL) { + if (existing_keyword_turl->type() != TemplateURL::NORMAL) { if (!CanReplace(existing_turl)) { AddToMap(existing_turl); AddToDomainMap(existing_turl); @@ -1730,14 +1745,14 @@ void TemplateURLService::UpdateTemplateURLIfPrepopulated( return; size_t default_search_index; - ScopedVector<TemplateURLData> prepopulated_urls = - TemplateURLPrepopulateData::GetPrepopulatedEngines( - prefs, &default_search_index); - - for (size_t i = 0; i < prepopulated_urls.size(); ++i) { - if (prepopulated_urls[i]->prepopulate_id == prepopulate_id) { - MergeIntoPrepopulatedEngineData(template_url, prepopulated_urls[i]); - template_url->CopyFrom(TemplateURL(*prepopulated_urls[i])); + std::vector<std::unique_ptr<TemplateURLData>> prepopulated_urls = + TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, + &default_search_index); + + for (const auto& url : prepopulated_urls) { + if (url->prepopulate_id == prepopulate_id) { + MergeIntoPrepopulatedEngineData(template_url, url.get()); + template_url->CopyFrom(TemplateURL(*url)); } } } @@ -1822,32 +1837,30 @@ void TemplateURLService::GoogleBaseURLChanged() { KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); bool something_changed = false; - for (TemplateURLVector::iterator i(template_urls_.begin()); - i != template_urls_.end(); ++i) { - TemplateURL* t_url = *i; - if (t_url->HasGoogleBaseURLs(search_terms_data())) { - TemplateURL updated_turl(t_url->data()); + for (const auto& turl : template_urls_) { + if (turl->HasGoogleBaseURLs(search_terms_data())) { + TemplateURL updated_turl(turl->data()); updated_turl.ResetKeywordIfNecessary(search_terms_data(), false); KeywordToTURLAndMeaningfulLength::const_iterator existing_entry = keyword_to_turl_and_length_.find(updated_turl.keyword()); if ((existing_entry != keyword_to_turl_and_length_.end()) && - (existing_entry->second.first != t_url)) { + (existing_entry->second.first != turl.get())) { // The new autogenerated keyword conflicts with another TemplateURL. - // Overwrite it if it's replaceable; otherwise, leave |t_url| using its - // current keyword. (This will not prevent |t_url| from auto-updating + // Overwrite it if it's replaceable; otherwise, leave |turl| using its + // current keyword. (This will not prevent |turl| from auto-updating // the keyword in the future if the conflicting TemplateURL disappears.) - // Note that we must still update |t_url| in this case, or the + // Note that we must still update |turl| in this case, or the // |provider_map_| will not be updated correctly. if (CanReplace(existing_entry->second.first)) RemoveNoNotify(existing_entry->second.first); else - updated_turl.data_.SetKeyword(t_url->keyword()); + updated_turl.data_.SetKeyword(turl->keyword()); } something_changed = true; // This will send the keyword change to sync. Note that other clients // need to reset the keyword to an appropriate local value when this // change arrives; see CreateTemplateURLFromTemplateURLAndSyncData(). - UpdateNoNotify(t_url, updated_turl); + UpdateNoNotify(turl.get(), updated_turl); } } if (something_changed) @@ -1890,8 +1903,8 @@ bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( // default. bool changed = TemplateURL::MatchesData( initial_default_search_provider_.get(), data, search_terms_data()); - initial_default_search_provider_.reset( - data ? new TemplateURL(*data) : NULL); + initial_default_search_provider_ = + data ? base::MakeUnique<TemplateURL>(*data) : nullptr; default_search_provider_source_ = source; return changed; } @@ -1900,7 +1913,7 @@ bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( // Note that we exclude the case of data == NULL because that could cause a // false positive for recursion when the initial_default_search_provider_ is // NULL due to policy. We'll never actually get recursion with data == NULL. - if (source == default_search_provider_source_ && data != NULL && + if (source == default_search_provider_source_ && data != nullptr && TemplateURL::MatchesData(default_search_provider_, data, search_terms_data())) return false; @@ -1916,11 +1929,11 @@ bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( // well as to add the new one, if appropriate. UpdateProvidersCreatedByPolicy( &template_urls_, - source == DefaultSearchManager::FROM_POLICY ? data : NULL); + source == DefaultSearchManager::FROM_POLICY ? data : nullptr); } if (!data) { - default_search_provider_ = NULL; + default_search_provider_ = nullptr; } else if (source == DefaultSearchManager::FROM_EXTENSION) { default_search_provider_ = FindMatchingExtensionTemplateURL( *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); @@ -1942,8 +1955,10 @@ bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( // (1) Tests that initialize the TemplateURLService in peculiar ways. // (2) If the user deleted the pre-populated default and we subsequently // lost their user-selected value. - TemplateURL* new_dse = new TemplateURL(*data); - if (AddNoNotify(new_dse, true)) + std::unique_ptr<TemplateURL> new_dse_ptr = + base::MakeUnique<TemplateURL>(*data); + TemplateURL* new_dse = new_dse_ptr.get(); + if (AddNoNotify(std::move(new_dse_ptr), true)) default_search_provider_ = new_dse; } } else if (source == DefaultSearchManager::FROM_USER) { @@ -1958,8 +1973,10 @@ bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( UpdateNoNotify(default_search_provider_, TemplateURL(new_data)); } else { new_data.id = kInvalidTemplateURLID; - TemplateURL* new_dse = new TemplateURL(new_data); - if (AddNoNotify(new_dse, true)) + std::unique_ptr<TemplateURL> new_dse_ptr = + base::MakeUnique<TemplateURL>(new_data); + TemplateURL* new_dse = new_dse_ptr.get(); + if (AddNoNotify(std::move(new_dse_ptr), true)) default_search_provider_ = new_dse; } if (default_search_provider_ && prefs_) { @@ -1980,14 +1997,14 @@ bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( return changed; } -bool TemplateURLService::AddNoNotify(TemplateURL* template_url, - bool newly_adding) { +TemplateURL* TemplateURLService::AddNoNotify( + std::unique_ptr<TemplateURL> template_url, + bool newly_adding) { DCHECK(template_url); if (newly_adding) { DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); - DCHECK(std::find(template_urls_.begin(), template_urls_.end(), - template_url) == template_urls_.end()); + DCHECK(!Contains(&template_urls_, template_url.get())); template_url->data_.id = ++next_id_; } @@ -2006,21 +2023,19 @@ bool TemplateURLService::AddNoNotify(TemplateURL* template_url, // that any "pre-existing" entries we find are actually coming from // |template_urls_|, lest we detect a "conflict" between the // |initial_default_search_provider_| and the web data version of itself. - if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && + if (template_url->type() != TemplateURL::OMNIBOX_API_EXTENSION && existing_keyword_turl && - existing_keyword_turl->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && - (std::find(template_urls_.begin(), template_urls_.end(), - existing_keyword_turl) != template_urls_.end())) { - DCHECK_NE(existing_keyword_turl, template_url); + existing_keyword_turl->type() != TemplateURL::OMNIBOX_API_EXTENSION && + Contains(&template_urls_, existing_keyword_turl)) { + DCHECK_NE(existing_keyword_turl, template_url.get()); // Only replace one of the TemplateURLs if they are either both extensions, // or both not extensions. - bool are_same_type = existing_keyword_turl->GetType() == - template_url->GetType(); + bool are_same_type = IsCreatedByExtension(existing_keyword_turl) == + IsCreatedByExtension(template_url.get()); if (CanReplace(existing_keyword_turl) && are_same_type) { RemoveNoNotify(existing_keyword_turl); - } else if (CanReplace(template_url) && are_same_type) { - delete template_url; - return false; + } else if (CanReplace(template_url.get()) && are_same_type) { + return nullptr; } else { base::string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false); @@ -2029,38 +2044,37 @@ bool TemplateURLService::AddNoNotify(TemplateURL* template_url, existing_keyword_turl->url()); } } - template_urls_.push_back(template_url); - AddToMaps(template_url); + TemplateURL* template_url_ptr = template_url.get(); + template_urls_.push_back(std::move(template_url)); + AddToMaps(template_url_ptr); - if (newly_adding && - (template_url->GetType() == TemplateURL::NORMAL)) { + if (newly_adding && (template_url_ptr->type() == TemplateURL::NORMAL)) { if (web_data_service_.get()) - web_data_service_->AddKeyword(template_url->data()); + web_data_service_->AddKeyword(template_url_ptr->data()); // Inform sync of the addition. Note that this will assign a GUID to // template_url and add it to the guid_to_turl_. - ProcessTemplateURLChange(FROM_HERE, - template_url, + ProcessTemplateURLChange(FROM_HERE, template_url_ptr, syncer::SyncChange::ACTION_ADD); } - return true; + return template_url_ptr; } void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { DCHECK(template_url != default_search_provider_); - TemplateURLVector::iterator i = - std::find(template_urls_.begin(), template_urls_.end(), template_url); + auto i = FindTemplateURL(&template_urls_, template_url); if (i == template_urls_.end()) return; RemoveFromMaps(template_url); // Remove it from the vector containing all TemplateURLs. + std::unique_ptr<TemplateURL> scoped_turl = std::move(*i); template_urls_.erase(i); - if (template_url->GetType() == TemplateURL::NORMAL) { + if (template_url->type() == TemplateURL::NORMAL) { if (web_data_service_.get()) web_data_service_->RemoveKeyword(template_url->id()); @@ -2075,9 +2089,6 @@ void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { if (loaded_ && client_) client_->DeleteAllSearchTermsForKeyword(template_url->id()); - - // We own the TemplateURL and need to delete it. - delete template_url; } bool TemplateURLService::ResetTemplateURLNoNotify( @@ -2116,13 +2127,12 @@ void TemplateURLService::NotifyObservers() { // that were set by policy, unless it is the current default search provider, in // which case it is updated with the data from prefs. void TemplateURLService::UpdateProvidersCreatedByPolicy( - TemplateURLVector* template_urls, + OwnedTemplateURLVector* template_urls, const TemplateURLData* default_from_prefs) { DCHECK(template_urls); - for (TemplateURLVector::iterator i = template_urls->begin(); - i != template_urls->end(); ) { - TemplateURL* template_url = *i; + for (auto i = template_urls->begin(); i != template_urls->end();) { + TemplateURL* template_url = i->get(); if (template_url->created_by_policy()) { if (default_from_prefs && TemplateURL::MatchesData(template_url, default_from_prefs, @@ -2133,30 +2143,32 @@ void TemplateURLService::UpdateProvidersCreatedByPolicy( // database and the |default_search_provider|. default_search_provider_ = template_url; // Prevent us from saving any other entries, or creating a new one. - default_from_prefs = NULL; + default_from_prefs = nullptr; ++i; continue; } + TemplateURLID id = template_url->id(); RemoveFromMaps(template_url); i = template_urls->erase(i); if (web_data_service_.get()) - web_data_service_->RemoveKeyword(template_url->id()); - delete template_url; + web_data_service_->RemoveKeyword(id); } else { ++i; } } if (default_from_prefs) { - default_search_provider_ = NULL; + default_search_provider_ = nullptr; default_search_provider_source_ = DefaultSearchManager::FROM_POLICY; TemplateURLData new_data(*default_from_prefs); if (new_data.sync_guid.empty()) new_data.sync_guid = base::GenerateGUID(); new_data.created_by_policy = true; - TemplateURL* new_dse = new TemplateURL(new_data); - if (AddNoNotify(new_dse, true)) + std::unique_ptr<TemplateURL> new_dse_ptr = + base::MakeUnique<TemplateURL>(new_data); + TemplateURL* new_dse = new_dse_ptr.get(); + if (AddNoNotify(std::move(new_dse_ptr), true)) default_search_provider_ = new_dse; } } @@ -2182,7 +2194,7 @@ base::string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl, // for extensions, as their keywords are not associated with their URLs). GURL gurl(turl.url()); if (gurl.is_valid() && - (turl.GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { + (turl.type() != TemplateURL::OMNIBOX_API_EXTENSION)) { base::string16 keyword_candidate = TemplateURL::GenerateKeyword(gurl); if (!GetTemplateURLForKeyword(keyword_candidate)) return keyword_candidate; @@ -2218,7 +2230,7 @@ void TemplateURLService::ResolveSyncKeywordConflict( DCHECK(applied_sync_turl); DCHECK(change_list); DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword()); - DCHECK_EQ(TemplateURL::NORMAL, applied_sync_turl->GetType()); + DCHECK_EQ(TemplateURL::NORMAL, applied_sync_turl->type()); // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so // don't delete either of them. Instead, determine which is "better" and @@ -2346,25 +2358,24 @@ void TemplateURLService::MergeInSyncTemplateURL( // Force the local ID to kInvalidTemplateURLID so we can add it. TemplateURLData data(sync_turl->data()); data.id = kInvalidTemplateURLID; - TemplateURL* added = new TemplateURL(data); + std::unique_ptr<TemplateURL> added_ptr = + base::MakeUnique<TemplateURL>(data); + TemplateURL* added = added_ptr.get(); base::AutoReset<DefaultSearchChangeOrigin> change_origin( &dsp_change_origin_, DSP_CHANGE_SYNC_ADD); - if (Add(added)) + if (Add(std::move(added_ptr))) MaybeUpdateDSEAfterSync(added); - merge_result->set_num_items_added( - merge_result->num_items_added() + 1); + merge_result->set_num_items_added(merge_result->num_items_added() + 1); } } void TemplateURLService::PatchMissingSyncGUIDs( - TemplateURLVector* template_urls) { + OwnedTemplateURLVector* template_urls) { DCHECK(template_urls); - for (TemplateURLVector::iterator i = template_urls->begin(); - i != template_urls->end(); ++i) { - TemplateURL* template_url = *i; + for (auto& template_url : *template_urls) { DCHECK(template_url); if (template_url->sync_guid().empty() && - (template_url->GetType() == TemplateURL::NORMAL)) { + (template_url->type() == TemplateURL::NORMAL)) { template_url->data_.sync_guid = base::GenerateGUID(); if (web_data_service_.get()) web_data_service_->UpdateKeyword(template_url->data()); @@ -2419,51 +2430,46 @@ void TemplateURLService::AddMatchingKeywordsHelper( TemplateURL* TemplateURLService::FindPrepopulatedTemplateURL( int prepopulated_id) { - for (TemplateURLVector::const_iterator i = template_urls_.begin(); - i != template_urls_.end(); ++i) { - if ((*i)->prepopulate_id() == prepopulated_id) - return *i; + for (const auto& turl : template_urls_) { + if (turl->prepopulate_id() == prepopulated_id) + return turl.get(); } - return NULL; + return nullptr; } TemplateURL* TemplateURLService::FindTemplateURLForExtension( const std::string& extension_id, TemplateURL::Type type) { DCHECK_NE(TemplateURL::NORMAL, type); - for (TemplateURLVector::const_iterator i = template_urls_.begin(); - i != template_urls_.end(); ++i) { - if ((*i)->GetType() == type && - (*i)->GetExtensionId() == extension_id) - return *i; + for (const auto& turl : template_urls_) { + if (turl->type() == type && turl->GetExtensionId() == extension_id) + return turl.get(); } - return NULL; + return nullptr; } TemplateURL* TemplateURLService::FindMatchingExtensionTemplateURL( const TemplateURLData& data, TemplateURL::Type type) { DCHECK_NE(TemplateURL::NORMAL, type); - for (TemplateURLVector::const_iterator i = template_urls_.begin(); - i != template_urls_.end(); ++i) { - if ((*i)->GetType() == type && - TemplateURL::MatchesData(*i, &data, search_terms_data())) - return *i; + for (const auto& turl : template_urls_) { + if (turl->type() == type && + TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) + return turl.get(); } - return NULL; + return nullptr; } void TemplateURLService::UpdateExtensionDefaultSearchEngine() { - TemplateURL* most_recently_intalled_default = NULL; - for (TemplateURLVector::const_iterator i = template_urls_.begin(); - i != template_urls_.end(); ++i) { - if (((*i)->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && - (*i)->extension_info_->wants_to_be_default_engine && - (*i)->SupportsReplacement(search_terms_data()) && + TemplateURL* most_recently_intalled_default = nullptr; + for (const auto& turl : template_urls_) { + if ((turl->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && + turl->extension_info_->wants_to_be_default_engine && + turl->SupportsReplacement(search_terms_data()) && (!most_recently_intalled_default || (most_recently_intalled_default->extension_info_->install_time < - (*i)->extension_info_->install_time))) - most_recently_intalled_default = *i; + turl->extension_info_->install_time))) + most_recently_intalled_default = turl.get(); } if (most_recently_intalled_default) { |