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