Complete SyncMessageFilter initialization after SyncChannel initialization
[chromium-blink-merge.git] / components / search_engines / default_search_pref_migration.cc
blob0abcd4373b873d5cd2907a096c1d124864763c73
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
3 // found in the LICENSE file.
5 #include "components/search_engines/default_search_pref_migration.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/histogram.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "components/search_engines/default_search_manager.h"
17 #include "components/search_engines/search_engines_pref_names.h"
18 #include "components/search_engines/template_url_data.h"
19 #include "components/search_engines/template_url_service.h"
20 #include "url/gurl.h"
22 namespace {
24 // Loads the user-selected DSE (if there is one) from legacy preferences.
25 scoped_ptr<TemplateURLData> LoadDefaultSearchProviderFromLegacyPrefs(
26 PrefService* prefs) {
27 if (!prefs->HasPrefPath(prefs::kDefaultSearchProviderSearchURL) ||
28 !prefs->HasPrefPath(prefs::kDefaultSearchProviderKeyword))
29 return scoped_ptr<TemplateURLData>();
31 const PrefService::Preference* pref =
32 prefs->FindPreference(prefs::kDefaultSearchProviderSearchURL);
33 DCHECK(pref);
34 if (pref->IsManaged())
35 return scoped_ptr<TemplateURLData>();
37 base::string16 keyword =
38 base::UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderKeyword));
39 std::string search_url =
40 prefs->GetString(prefs::kDefaultSearchProviderSearchURL);
41 if (keyword.empty() || search_url.empty())
42 return scoped_ptr<TemplateURLData>();
44 scoped_ptr<TemplateURLData> default_provider_data(new TemplateURLData);
45 default_provider_data->SetShortName(
46 base::UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderName)));
47 default_provider_data->SetKeyword(keyword);
48 default_provider_data->SetURL(search_url);
49 default_provider_data->suggestions_url =
50 prefs->GetString(prefs::kDefaultSearchProviderSuggestURL);
51 default_provider_data->instant_url =
52 prefs->GetString(prefs::kDefaultSearchProviderInstantURL);
53 default_provider_data->image_url =
54 prefs->GetString(prefs::kDefaultSearchProviderImageURL);
55 default_provider_data->new_tab_url =
56 prefs->GetString(prefs::kDefaultSearchProviderNewTabURL);
57 default_provider_data->search_url_post_params =
58 prefs->GetString(prefs::kDefaultSearchProviderSearchURLPostParams);
59 default_provider_data->suggestions_url_post_params =
60 prefs->GetString(prefs::kDefaultSearchProviderSuggestURLPostParams);
61 default_provider_data->instant_url_post_params =
62 prefs->GetString(prefs::kDefaultSearchProviderInstantURLPostParams);
63 default_provider_data->image_url_post_params =
64 prefs->GetString(prefs::kDefaultSearchProviderImageURLPostParams);
65 default_provider_data->favicon_url =
66 GURL(prefs->GetString(prefs::kDefaultSearchProviderIconURL));
67 default_provider_data->show_in_default_list = true;
68 default_provider_data->search_terms_replacement_key =
69 prefs->GetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey);
70 default_provider_data->input_encodings = base::SplitString(
71 prefs->GetString(prefs::kDefaultSearchProviderEncodings),
72 ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
74 default_provider_data->alternate_urls.clear();
75 const base::ListValue* alternate_urls =
76 prefs->GetList(prefs::kDefaultSearchProviderAlternateURLs);
77 for (size_t i = 0; i < alternate_urls->GetSize(); ++i) {
78 std::string alternate_url;
79 if (alternate_urls->GetString(i, &alternate_url))
80 default_provider_data->alternate_urls.push_back(alternate_url);
83 std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID);
84 if (!id_string.empty()) {
85 int64 value;
86 base::StringToInt64(id_string, &value);
87 default_provider_data->id = value;
90 std::string prepopulate_id =
91 prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID);
92 if (!prepopulate_id.empty()) {
93 int value;
94 base::StringToInt(prepopulate_id, &value);
95 default_provider_data->prepopulate_id = value;
98 return default_provider_data.Pass();
101 void ClearDefaultSearchProviderFromLegacyPrefs(PrefService* prefs) {
102 prefs->ClearPref(prefs::kDefaultSearchProviderName);
103 prefs->ClearPref(prefs::kDefaultSearchProviderKeyword);
104 prefs->ClearPref(prefs::kDefaultSearchProviderSearchURL);
105 prefs->ClearPref(prefs::kDefaultSearchProviderSuggestURL);
106 prefs->ClearPref(prefs::kDefaultSearchProviderInstantURL);
107 prefs->ClearPref(prefs::kDefaultSearchProviderImageURL);
108 prefs->ClearPref(prefs::kDefaultSearchProviderNewTabURL);
109 prefs->ClearPref(prefs::kDefaultSearchProviderSearchURLPostParams);
110 prefs->ClearPref(prefs::kDefaultSearchProviderSuggestURLPostParams);
111 prefs->ClearPref(prefs::kDefaultSearchProviderInstantURLPostParams);
112 prefs->ClearPref(prefs::kDefaultSearchProviderImageURLPostParams);
113 prefs->ClearPref(prefs::kDefaultSearchProviderIconURL);
114 prefs->ClearPref(prefs::kDefaultSearchProviderEncodings);
115 prefs->ClearPref(prefs::kDefaultSearchProviderPrepopulateID);
116 prefs->ClearPref(prefs::kDefaultSearchProviderAlternateURLs);
117 prefs->ClearPref(prefs::kDefaultSearchProviderSearchTermsReplacementKey);
120 void MigrateDefaultSearchPref(PrefService* pref_service) {
121 DCHECK(pref_service);
123 scoped_ptr<TemplateURLData> legacy_dse_from_prefs =
124 LoadDefaultSearchProviderFromLegacyPrefs(pref_service);
125 if (!legacy_dse_from_prefs)
126 return;
128 DefaultSearchManager default_search_manager(
129 pref_service, DefaultSearchManager::ObserverCallback());
130 DefaultSearchManager::Source modern_source;
131 TemplateURLData* modern_value =
132 default_search_manager.GetDefaultSearchEngine(&modern_source);
133 if (modern_source == DefaultSearchManager::FROM_FALLBACK) {
134 // |modern_value| is the prepopulated default. If it matches the legacy DSE
135 // we assume it is not a user-selected value.
136 if (!modern_value ||
137 legacy_dse_from_prefs->prepopulate_id != modern_value->prepopulate_id) {
138 // This looks like a user-selected value, so let's migrate it.
139 // TODO(erikwright): Remove this migration logic when this stat approaches
140 // zero.
141 UMA_HISTOGRAM_BOOLEAN("Search.MigratedPrefToDictionaryValue", true);
142 default_search_manager.SetUserSelectedDefaultSearchEngine(
143 *legacy_dse_from_prefs);
147 ClearDefaultSearchProviderFromLegacyPrefs(pref_service);
150 void OnPrefsInitialized(PrefService* pref_service,
151 bool pref_service_initialization_success) {
152 MigrateDefaultSearchPref(pref_service);
155 } // namespace
157 void ConfigureDefaultSearchPrefMigrationToDictionaryValue(
158 PrefService* pref_service) {
159 if (pref_service->GetInitializationStatus() ==
160 PrefService::INITIALIZATION_STATUS_WAITING) {
161 pref_service->AddPrefInitObserver(
162 base::Bind(&OnPrefsInitialized, base::Unretained(pref_service)));
163 } else {
164 MigrateDefaultSearchPref(pref_service);