Some refactorings to facilitate a larger change to TemplateURLService.

BUG=365762
R=pkasting@chromium.org

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=269310

Review URL: https://codereview.chromium.org/270533007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269723 0039d316-1c4b-4281-b951-d872f2087c98
parent 36edc406
...@@ -99,7 +99,7 @@ void DefaultSearchManager::RegisterProfilePrefs( ...@@ -99,7 +99,7 @@ void DefaultSearchManager::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) { user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref( registry->RegisterDictionaryPref(
kDefaultSearchProviderDataPrefName, kDefaultSearchProviderDataPrefName,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
} }
// static // static
......
...@@ -6,68 +6,57 @@ ...@@ -6,68 +6,57 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "chrome/browser/search_engines/default_search_manager.h" #include "chrome/browser/search_engines/default_search_manager.h"
#include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
#include "chrome/browser/search_engines/template_url_service.h" #include "chrome/browser/search_engines/template_url_service.h"
namespace { namespace {
void MigrateDefaultSearchPref(PrefService* pref_service) { // Loads the user-selected DSE (if there is one, and it's not masked by policy
DefaultSearchManager default_search_manager( // or an extension) from legacy preferences.
pref_service, DefaultSearchManager::ObserverCallback()); scoped_ptr<TemplateURLData> LoadDefaultSearchProviderFromPrefs(
PrefService* pref_service) {
if (default_search_manager.GetDefaultSearchEngineSource() ==
DefaultSearchManager::FROM_USER) {
return;
}
scoped_ptr<TemplateURLData> legacy_dse_from_prefs; scoped_ptr<TemplateURLData> legacy_dse_from_prefs;
bool legacy_is_managed = false; bool legacy_is_managed = false;
bool has_legacy_dse_from_prefs =
TemplateURLService::LoadDefaultSearchProviderFromPrefs( TemplateURLService::LoadDefaultSearchProviderFromPrefs(
pref_service, &legacy_dse_from_prefs, &legacy_is_managed); pref_service, &legacy_dse_from_prefs, &legacy_is_managed);
return legacy_is_managed ?
scoped_ptr<TemplateURLData>() : legacy_dse_from_prefs.Pass();
}
if (!has_legacy_dse_from_prefs) { void MigrateDefaultSearchPref(PrefService* pref_service) {
// The DSE is undefined. Nothing to migrate. DCHECK(pref_service);
return;
}
if (!legacy_dse_from_prefs) {
// The DSE is defined as NULL. This can only really be done via policy.
// Policy-defined values will be automatically projected into the new
// format. Even if the user did somehow set this manually we do not have a
// way to migrate it.
return;
}
if (legacy_is_managed) {
// The DSE is policy-managed, not user-selected. It will automatically be
// projected into the new location.
return;
}
// If the pre-populated DSE matches the DSE from prefs we assume it is not a scoped_ptr<TemplateURLData> legacy_dse_from_prefs =
// user-selected value. LoadDefaultSearchProviderFromPrefs(pref_service);
scoped_ptr<TemplateURLData> prepopulated_dse( if (!legacy_dse_from_prefs)
TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(pref_service));
if (prepopulated_dse &&
legacy_dse_from_prefs->prepopulate_id ==
prepopulated_dse->prepopulate_id) {
return; return;
}
DefaultSearchManager default_search_manager(
pref_service, DefaultSearchManager::ObserverCallback());
DefaultSearchManager::Source modern_source;
TemplateURLData* modern_value =
default_search_manager.GetDefaultSearchEngine(&modern_source);
if (modern_source == DefaultSearchManager::FROM_FALLBACK) {
// |modern_value| is the prepopulated default. If it matches the legacy DSE
// we assume it is not a user-selected value.
if (!modern_value ||
legacy_dse_from_prefs->prepopulate_id != modern_value->prepopulate_id) {
// This looks like a user-selected value, so let's migrate it.
// TODO(erikwright): Remove this migration logic when this stat approaches
// zero.
UMA_HISTOGRAM_BOOLEAN("Search.MigratedPrefToDictionaryValue", true); UMA_HISTOGRAM_BOOLEAN("Search.MigratedPrefToDictionaryValue", true);
// This looks like a user-selected value, so let's migrate it. Subsequent
// changes to this value will be automatically stored in the correct location.
default_search_manager.SetUserSelectedDefaultSearchEngine( default_search_manager.SetUserSelectedDefaultSearchEngine(
*legacy_dse_from_prefs); *legacy_dse_from_prefs);
}
}
// TODO(erikwright): Clear the legacy value when the modern value is the // TODO(erikwright): Clear the legacy value when the modern value is the
// authority. Don't forget to do this even if we don't migrate (because we // authority.
// migrated prior to implementing the clear.
} }
void OnPrefsInitialized(PrefService* pref_service, void OnPrefsInitialized(PrefService* pref_service,
......
...@@ -51,6 +51,7 @@ ...@@ -51,6 +51,7 @@
#include "sync/protocol/search_engine_specifics.pb.h" #include "sync/protocol/search_engine_specifics.pb.h"
#include "sync/protocol/sync.pb.h" #include "sync/protocol/sync.pb.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;
typedef TemplateURLService::SyncDataMap SyncDataMap; typedef TemplateURLService::SyncDataMap SyncDataMap;
...@@ -265,8 +266,7 @@ TemplateURLService::TemplateURLService(Profile* profile) ...@@ -265,8 +266,7 @@ TemplateURLService::TemplateURLService(Profile* profile)
pending_synced_default_search_(false), pending_synced_default_search_(false),
dsp_change_origin_(DSP_CHANGE_OTHER), dsp_change_origin_(DSP_CHANGE_OTHER),
default_search_manager_( default_search_manager_(
new DefaultSearchManager(GetPrefs(), GetPrefs(), DefaultSearchManager::ObserverCallback()) {
DefaultSearchManager::ObserverCallback())) {
DCHECK(profile_); DCHECK(profile_);
Init(NULL, 0); Init(NULL, 0);
} }
...@@ -286,7 +286,9 @@ TemplateURLService::TemplateURLService(const Initializer* initializers, ...@@ -286,7 +286,9 @@ TemplateURLService::TemplateURLService(const Initializer* initializers,
models_associated_(false), models_associated_(false),
processing_syncer_changes_(false), processing_syncer_changes_(false),
pending_synced_default_search_(false), pending_synced_default_search_(false),
dsp_change_origin_(DSP_CHANGE_OTHER) { dsp_change_origin_(DSP_CHANGE_OTHER),
default_search_manager_(
GetPrefs(), DefaultSearchManager::ObserverCallback()) {
Init(initializers, count); Init(initializers, count);
} }
...@@ -572,7 +574,7 @@ bool TemplateURLService::CanReplaceKeyword( ...@@ -572,7 +574,7 @@ bool TemplateURLService::CanReplaceKeyword(
void TemplateURLService::FindMatchingKeywords( void TemplateURLService::FindMatchingKeywords(
const base::string16& prefix, const base::string16& prefix,
bool support_replacement_only, bool support_replacement_only,
TemplateURLVector* matches) const { TemplateURLVector* matches) {
// Sanity check args. // Sanity check args.
if (prefix.empty()) if (prefix.empty())
return; return;
...@@ -809,12 +811,10 @@ bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { ...@@ -809,12 +811,10 @@ bool TemplateURLService::CanMakeDefault(const TemplateURL* url) {
void TemplateURLService::SetUserSelectedDefaultSearchProvider( void TemplateURLService::SetUserSelectedDefaultSearchProvider(
TemplateURL* url) { TemplateURL* url) {
SetDefaultSearchProvider(url); SetDefaultSearchProvider(url);
if (default_search_manager_) {
if (url) if (url)
default_search_manager_->SetUserSelectedDefaultSearchEngine(url->data()); default_search_manager_.SetUserSelectedDefaultSearchEngine(url->data());
else else
default_search_manager_->ClearUserSelectedDefaultSearchEngine(); default_search_manager_.ClearUserSelectedDefaultSearchEngine();
}
} }
TemplateURL* TemplateURLService::GetDefaultSearchProvider() { TemplateURL* TemplateURLService::GetDefaultSearchProvider() {
...@@ -1167,7 +1167,7 @@ syncer::SyncError TemplateURLService::ProcessSyncChanges( ...@@ -1167,7 +1167,7 @@ syncer::SyncError TemplateURLService::ProcessSyncChanges(
new_changes.push_back(syncer::SyncChange(FROM_HERE, new_changes.push_back(syncer::SyncChange(FROM_HERE,
syncer::SyncChange::ACTION_ADD, syncer::SyncChange::ACTION_ADD,
sync_data)); sync_data));
// Ignore the delete attempt. This means we never end up reseting the // Ignore the delete attempt. This means we never end up resetting the
// default search provider due to an ACTION_DELETE from sync. // default search provider due to an ACTION_DELETE from sync.
continue; continue;
} }
...@@ -2686,7 +2686,7 @@ void TemplateURLService::EnsureDefaultSearchProviderExists() { ...@@ -2686,7 +2686,7 @@ void TemplateURLService::EnsureDefaultSearchProviderExists() {
} }
TemplateURL* TemplateURLService::CreateTemplateURLForExtension( TemplateURL* TemplateURLService::CreateTemplateURLForExtension(
const ExtensionKeyword& extension_keyword) const { const ExtensionKeyword& extension_keyword) {
TemplateURLData data; TemplateURLData data;
data.short_name = base::UTF8ToUTF16(extension_keyword.extension_name); data.short_name = base::UTF8ToUTF16(extension_keyword.extension_name);
data.SetKeyword(base::UTF8ToUTF16(extension_keyword.extension_keyword)); data.SetKeyword(base::UTF8ToUTF16(extension_keyword.extension_keyword));
...@@ -2699,7 +2699,7 @@ TemplateURL* TemplateURLService::CreateTemplateURLForExtension( ...@@ -2699,7 +2699,7 @@ TemplateURL* TemplateURLService::CreateTemplateURLForExtension(
TemplateURL* TemplateURLService::FindTemplateURLForExtension( TemplateURL* TemplateURLService::FindTemplateURLForExtension(
const std::string& extension_id, const std::string& extension_id,
TemplateURL::Type type) const { TemplateURL::Type type) {
DCHECK_NE(TemplateURL::NORMAL, type); DCHECK_NE(TemplateURL::NORMAL, type);
for (TemplateURLVector::const_iterator i = template_urls_.begin(); for (TemplateURLVector::const_iterator i = template_urls_.begin();
i != template_urls_.end(); ++i) { i != template_urls_.end(); ++i) {
......
...@@ -156,7 +156,7 @@ class TemplateURLService : public WebDataServiceConsumer, ...@@ -156,7 +156,7 @@ class TemplateURLService : public WebDataServiceConsumer,
// TemplateURLs that support replacement are returned. // TemplateURLs that support replacement are returned.
void FindMatchingKeywords(const base::string16& prefix, void FindMatchingKeywords(const base::string16& prefix,
bool support_replacement_only, bool support_replacement_only,
TemplateURLVector* matches) const; TemplateURLVector* matches);
// Looks up |keyword| and returns the element it maps to. Returns NULL if // Looks up |keyword| and returns the element it maps to. Returns NULL if
// the keyword was not found. // the keyword was not found.
...@@ -276,7 +276,7 @@ class TemplateURLService : public WebDataServiceConsumer, ...@@ -276,7 +276,7 @@ class TemplateURLService : public WebDataServiceConsumer,
// revved: all existing prepopulated entries are checked against the current // revved: all existing prepopulated entries are checked against the current
// prepopulate data, any now-extraneous safe_for_autoreplace() entries are // prepopulate data, any now-extraneous safe_for_autoreplace() entries are
// removed, any existing engines are reset to the provided data (except for // removed, any existing engines are reset to the provided data (except for
// user-edited names or keywords), and any new prepopulated anegines are // user-edited names or keywords), and any new prepopulated engines are
// added. // added.
// //
// After this, the default search engine is reset to the default entry in the // After this, the default search engine is reset to the default entry in the
...@@ -673,11 +673,11 @@ class TemplateURLService : public WebDataServiceConsumer, ...@@ -673,11 +673,11 @@ class TemplateURLService : public WebDataServiceConsumer,
// Returns a new TemplateURL for the given extension. // Returns a new TemplateURL for the given extension.
TemplateURL* CreateTemplateURLForExtension( TemplateURL* CreateTemplateURLForExtension(
const ExtensionKeyword& extension_keyword) const; const ExtensionKeyword& extension_keyword);
// Returns the TemplateURL associated with |extension_id|, if any. // Returns the TemplateURL associated with |extension_id|, if any.
TemplateURL* FindTemplateURLForExtension(const std::string& extension_id, TemplateURL* FindTemplateURLForExtension(const std::string& extension_id,
TemplateURL::Type type) const; TemplateURL::Type type);
// Finds the most recently-installed NORMAL_CONTROLLED_BY_EXTENSION engine // Finds the most recently-installed NORMAL_CONTROLLED_BY_EXTENSION engine
// that supports replacement and wants to be default, if any. // that supports replacement and wants to be default, if any.
...@@ -732,7 +732,7 @@ class TemplateURLService : public WebDataServiceConsumer, ...@@ -732,7 +732,7 @@ class TemplateURLService : public WebDataServiceConsumer,
std::vector<history::URLVisitedDetails> visits_to_add_; std::vector<history::URLVisitedDetails> visits_to_add_;
// Once loaded, the default search provider. This is a pointer to a // Once loaded, the default search provider. This is a pointer to a
// TemplateURL owned by template_urls_. // TemplateURL owned by |template_urls_|.
TemplateURL* default_search_provider_; TemplateURL* default_search_provider_;
// The initial search provider extracted from preferences. This is only valid // The initial search provider extracted from preferences. This is only valid
...@@ -786,9 +786,8 @@ class TemplateURLService : public WebDataServiceConsumer, ...@@ -786,9 +786,8 @@ class TemplateURLService : public WebDataServiceConsumer,
// Stores a list of callbacks to be run after TemplateURLService has loaded. // Stores a list of callbacks to be run after TemplateURLService has loaded.
base::CallbackList<void(void)> on_loaded_callbacks_; base::CallbackList<void(void)> on_loaded_callbacks_;
// Helper class to manage the default search engine. This will be NULL when // Helper class to manage the default search engine.
// using the testing-specific constructor. DefaultSearchManager default_search_manager_;
scoped_ptr<DefaultSearchManager> default_search_manager_;
DISALLOW_COPY_AND_ASSIGN(TemplateURLService); DISALLOW_COPY_AND_ASSIGN(TemplateURLService);
}; };
......
...@@ -1881,9 +1881,9 @@ TEST_F(TemplateURLServiceSyncTest, PreSyncUpdates) { ...@@ -1881,9 +1881,9 @@ TEST_F(TemplateURLServiceSyncTest, PreSyncUpdates) {
// updated time. // updated time.
TemplateURL* added_turl = model()->GetTemplateURLForKeyword( TemplateURL* added_turl = model()->GetTemplateURLForKeyword(
ASCIIToUTF16(kNewKeyword)); ASCIIToUTF16(kNewKeyword));
ASSERT_TRUE(added_turl);
base::Time new_timestamp = added_turl->last_modified(); base::Time new_timestamp = added_turl->last_modified();
EXPECT_GE(new_timestamp, pre_merge_time); EXPECT_GE(new_timestamp, pre_merge_time);
ASSERT_TRUE(added_turl);
std::string sync_guid = added_turl->sync_guid(); std::string sync_guid = added_turl->sync_guid();
// Bring down a copy of the prepopulate engine from Sync with the old values, // Bring down a copy of the prepopulate engine from Sync with the old values,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment