| 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 "chrome/browser/search_engines/default_search_pref_migration.h" | 5 #include "chrome/browser/search_engines/default_search_pref_migration.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 12 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 13 #include "chrome/browser/search_engines/default_search_manager.h" | 12 #include "chrome/browser/search_engines/default_search_manager.h" |
| 14 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 15 #include "chrome/browser/search_engines/template_url_service.h" | 15 #include "chrome/browser/search_engines/template_url_service.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Loads the user-selected DSE (if there is one, and it's not masked by policy | 19 void MigrateDefaultSearchPref(PrefService* pref_service) { |
| 20 // or an extension) from legacy preferences. | 20 DefaultSearchManager default_search_manager( |
| 21 scoped_ptr<TemplateURLData> LoadDefaultSearchProviderFromPrefs( | 21 pref_service, DefaultSearchManager::ObserverCallback()); |
| 22 PrefService* pref_service) { | 22 |
| 23 if (default_search_manager.GetDefaultSearchEngineSource() == |
| 24 DefaultSearchManager::FROM_USER) { |
| 25 return; |
| 26 } |
| 27 |
| 23 scoped_ptr<TemplateURLData> legacy_dse_from_prefs; | 28 scoped_ptr<TemplateURLData> legacy_dse_from_prefs; |
| 24 bool legacy_is_managed = false; | 29 bool legacy_is_managed = false; |
| 25 TemplateURLService::LoadDefaultSearchProviderFromPrefs( | 30 bool has_legacy_dse_from_prefs = |
| 26 pref_service, &legacy_dse_from_prefs, &legacy_is_managed); | 31 TemplateURLService::LoadDefaultSearchProviderFromPrefs( |
| 27 return legacy_is_managed ? | 32 pref_service, &legacy_dse_from_prefs, &legacy_is_managed); |
| 28 scoped_ptr<TemplateURLData>() : legacy_dse_from_prefs.Pass(); | |
| 29 } | |
| 30 | 33 |
| 31 void MigrateDefaultSearchPref(PrefService* pref_service) { | 34 if (!has_legacy_dse_from_prefs) { |
| 32 DCHECK(pref_service); | 35 // The DSE is undefined. Nothing to migrate. |
| 33 | |
| 34 scoped_ptr<TemplateURLData> legacy_dse_from_prefs = | |
| 35 LoadDefaultSearchProviderFromPrefs(pref_service); | |
| 36 if (!legacy_dse_from_prefs) | |
| 37 return; | 36 return; |
| 38 | 37 } |
| 39 DefaultSearchManager default_search_manager( | 38 if (!legacy_dse_from_prefs) { |
| 40 pref_service, DefaultSearchManager::ObserverCallback()); | 39 // The DSE is defined as NULL. This can only really be done via policy. |
| 41 DefaultSearchManager::Source modern_source; | 40 // Policy-defined values will be automatically projected into the new |
| 42 TemplateURLData* modern_value = | 41 // format. Even if the user did somehow set this manually we do not have a |
| 43 default_search_manager.GetDefaultSearchEngine(&modern_source); | 42 // way to migrate it. |
| 44 if (modern_source == DefaultSearchManager::FROM_FALLBACK) { | 43 return; |
| 45 // |modern_value| is the prepopulated default. If it matches the legacy DSE | 44 } |
| 46 // we assume it is not a user-selected value. | 45 if (legacy_is_managed) { |
| 47 if (!modern_value || | 46 // The DSE is policy-managed, not user-selected. It will automatically be |
| 48 legacy_dse_from_prefs->prepopulate_id != modern_value->prepopulate_id) { | 47 // projected into the new location. |
| 49 // This looks like a user-selected value, so let's migrate it. | 48 return; |
| 50 // TODO(erikwright): Remove this migration logic when this stat approaches | |
| 51 // zero. | |
| 52 UMA_HISTOGRAM_BOOLEAN("Search.MigratedPrefToDictionaryValue", true); | |
| 53 default_search_manager.SetUserSelectedDefaultSearchEngine( | |
| 54 *legacy_dse_from_prefs); | |
| 55 } | |
| 56 } | 49 } |
| 57 | 50 |
| 51 // If the pre-populated DSE matches the DSE from prefs we assume it is not a |
| 52 // user-selected value. |
| 53 scoped_ptr<TemplateURLData> prepopulated_dse( |
| 54 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(pref_service)); |
| 55 if (prepopulated_dse && |
| 56 legacy_dse_from_prefs->prepopulate_id == |
| 57 prepopulated_dse->prepopulate_id) { |
| 58 return; |
| 59 } |
| 60 |
| 61 UMA_HISTOGRAM_BOOLEAN("Search.MigratedPrefToDictionaryValue", true); |
| 62 |
| 63 // This looks like a user-selected value, so let's migrate it. Subsequent |
| 64 // changes to this value will be automatically stored in the correct location. |
| 65 default_search_manager.SetUserSelectedDefaultSearchEngine( |
| 66 *legacy_dse_from_prefs); |
| 67 |
| 58 // TODO(erikwright): Clear the legacy value when the modern value is the | 68 // TODO(erikwright): Clear the legacy value when the modern value is the |
| 59 // authority. | 69 // authority. Don't forget to do this even if we don't migrate (because we |
| 70 // migrated prior to implementing the clear. |
| 60 } | 71 } |
| 61 | 72 |
| 62 void OnPrefsInitialized(PrefService* pref_service, | 73 void OnPrefsInitialized(PrefService* pref_service, |
| 63 bool pref_service_initialization_success) { | 74 bool pref_service_initialization_success) { |
| 64 MigrateDefaultSearchPref(pref_service); | 75 MigrateDefaultSearchPref(pref_service); |
| 65 } | 76 } |
| 66 | 77 |
| 67 } // namespace | 78 } // namespace |
| 68 | 79 |
| 69 void ConfigureDefaultSearchPrefMigrationToDictionaryValue( | 80 void ConfigureDefaultSearchPrefMigrationToDictionaryValue( |
| 70 PrefService* pref_service) { | 81 PrefService* pref_service) { |
| 71 if (pref_service->GetInitializationStatus() == | 82 if (pref_service->GetInitializationStatus() == |
| 72 PrefService::INITIALIZATION_STATUS_WAITING) { | 83 PrefService::INITIALIZATION_STATUS_WAITING) { |
| 73 pref_service->AddPrefInitObserver( | 84 pref_service->AddPrefInitObserver( |
| 74 base::Bind(&OnPrefsInitialized, base::Unretained(pref_service))); | 85 base::Bind(&OnPrefsInitialized, base::Unretained(pref_service))); |
| 75 } else { | 86 } else { |
| 76 MigrateDefaultSearchPref(pref_service); | 87 MigrateDefaultSearchPref(pref_service); |
| 77 } | 88 } |
| 78 } | 89 } |
| OLD | NEW |