Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: chrome/browser/search_engines/default_search_pref_migration.cc

Issue 270533007: Some refactorings to facilitate a larger change to TemplateURLService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: IWYU Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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);
Peter Kasting 2014/05/08 17:36:43 Nit: It would be nice to cleanup this function to
erikwright (departed) 2014/05/08 18:24:20 It returns a bool at the moment, which indicates a
27 if (legacy_is_managed)
28 legacy_dse_from_prefs.reset();
29 return legacy_dse_from_prefs.Pass();
Peter Kasting 2014/05/08 17:36:43 Nit: Shorter: return legacy_is_managed ?
erikwright (departed) 2014/05/08 18:24:20 Done.
30 }
31
19 void MigrateDefaultSearchPref(PrefService* pref_service) { 32 void MigrateDefaultSearchPref(PrefService* pref_service) {
33 DCHECK(pref_service);
34
35 scoped_ptr<TemplateURLData> legacy_dse_from_prefs =
36 LoadDefaultSearchProviderFromPrefs(pref_service);
37 if (!legacy_dse_from_prefs)
38 return;
39
20 DefaultSearchManager default_search_manager( 40 DefaultSearchManager default_search_manager(
21 pref_service, DefaultSearchManager::ObserverCallback()); 41 pref_service, DefaultSearchManager::ObserverCallback());
22 42 DefaultSearchManager::Source modern_source;
23 if (default_search_manager.GetDefaultSearchEngineSource() == 43 TemplateURLData* modern_value =
24 DefaultSearchManager::FROM_USER) { 44 default_search_manager.GetDefaultSearchEngine(&modern_source);
25 return; 45 if (modern_source == DefaultSearchManager::FROM_FALLBACK) {
46 // |modern_value| is the prepopulated default. If it matches the legacy DSE
47 // we assume it is not a user-selected value.
48 if (!modern_value ||
49 legacy_dse_from_prefs->prepopulate_id != modern_value->prepopulate_id) {
50 // This looks like a user-selected value, so let's migrate it.
51 // TODO(erikwright): Remove this migration logic when this stat approaches
52 // zero.
53 UMA_HISTOGRAM_BOOLEAN("Search.MigratedPrefToDictionaryValue", true);
54 default_search_manager.SetUserSelectedDefaultSearchEngine(
55 *legacy_dse_from_prefs);
56 }
26 } 57 }
27 58
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 59 // 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 60 // authority.
70 // migrated prior to implementing the clear.
71 } 61 }
72 62
73 void OnPrefsInitialized(PrefService* pref_service, 63 void OnPrefsInitialized(PrefService* pref_service,
74 bool pref_service_initialization_success) { 64 bool pref_service_initialization_success) {
75 MigrateDefaultSearchPref(pref_service); 65 MigrateDefaultSearchPref(pref_service);
76 } 66 }
77 67
78 } // namespace 68 } // namespace
79 69
80 void ConfigureDefaultSearchPrefMigrationToDictionaryValue( 70 void ConfigureDefaultSearchPrefMigrationToDictionaryValue(
81 PrefService* pref_service) { 71 PrefService* pref_service) {
82 if (pref_service->GetInitializationStatus() == 72 if (pref_service->GetInitializationStatus() ==
83 PrefService::INITIALIZATION_STATUS_WAITING) { 73 PrefService::INITIALIZATION_STATUS_WAITING) {
84 pref_service->AddPrefInitObserver( 74 pref_service->AddPrefInitObserver(
85 base::Bind(&OnPrefsInitialized, base::Unretained(pref_service))); 75 base::Bind(&OnPrefsInitialized, base::Unretained(pref_service)));
86 } else { 76 } else {
87 MigrateDefaultSearchPref(pref_service); 77 MigrateDefaultSearchPref(pref_service);
88 } 78 }
89 } 79 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698