[AndroidWebViewShell] Replace rebaseline script with a new version using test_runner.py
[chromium-blink-merge.git] / components / search_engines / default_search_pref_migration.cc
blobc9f1b61883c72c4897bf5f19f65fbee816830505
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 base::SplitString(prefs->GetString(prefs::kDefaultSearchProviderEncodings),
71 ';', &default_provider_data->input_encodings);
73 default_provider_data->alternate_urls.clear();
74 const base::ListValue* alternate_urls =
75 prefs->GetList(prefs::kDefaultSearchProviderAlternateURLs);
76 for (size_t i = 0; i < alternate_urls->GetSize(); ++i) {
77 std::string alternate_url;
78 if (alternate_urls->GetString(i, &alternate_url))
79 default_provider_data->alternate_urls.push_back(alternate_url);
82 std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID);
83 if (!id_string.empty()) {
84 int64 value;
85 base::StringToInt64(id_string, &value);
86 default_provider_data->id = value;
89 std::string prepopulate_id =
90 prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID);
91 if (!prepopulate_id.empty()) {
92 int value;
93 base::StringToInt(prepopulate_id, &value);
94 default_provider_data->prepopulate_id = value;
97 return default_provider_data.Pass();
100 void ClearDefaultSearchProviderFromLegacyPrefs(PrefService* prefs) {
101 prefs->ClearPref(prefs::kDefaultSearchProviderName);
102 prefs->ClearPref(prefs::kDefaultSearchProviderKeyword);
103 prefs->ClearPref(prefs::kDefaultSearchProviderSearchURL);
104 prefs->ClearPref(prefs::kDefaultSearchProviderSuggestURL);
105 prefs->ClearPref(prefs::kDefaultSearchProviderInstantURL);
106 prefs->ClearPref(prefs::kDefaultSearchProviderImageURL);
107 prefs->ClearPref(prefs::kDefaultSearchProviderNewTabURL);
108 prefs->ClearPref(prefs::kDefaultSearchProviderSearchURLPostParams);
109 prefs->ClearPref(prefs::kDefaultSearchProviderSuggestURLPostParams);
110 prefs->ClearPref(prefs::kDefaultSearchProviderInstantURLPostParams);
111 prefs->ClearPref(prefs::kDefaultSearchProviderImageURLPostParams);
112 prefs->ClearPref(prefs::kDefaultSearchProviderIconURL);
113 prefs->ClearPref(prefs::kDefaultSearchProviderEncodings);
114 prefs->ClearPref(prefs::kDefaultSearchProviderPrepopulateID);
115 prefs->ClearPref(prefs::kDefaultSearchProviderAlternateURLs);
116 prefs->ClearPref(prefs::kDefaultSearchProviderSearchTermsReplacementKey);
119 void MigrateDefaultSearchPref(PrefService* pref_service) {
120 DCHECK(pref_service);
122 scoped_ptr<TemplateURLData> legacy_dse_from_prefs =
123 LoadDefaultSearchProviderFromLegacyPrefs(pref_service);
124 if (!legacy_dse_from_prefs)
125 return;
127 DefaultSearchManager default_search_manager(
128 pref_service, DefaultSearchManager::ObserverCallback());
129 DefaultSearchManager::Source modern_source;
130 TemplateURLData* modern_value =
131 default_search_manager.GetDefaultSearchEngine(&modern_source);
132 if (modern_source == DefaultSearchManager::FROM_FALLBACK) {
133 // |modern_value| is the prepopulated default. If it matches the legacy DSE
134 // we assume it is not a user-selected value.
135 if (!modern_value ||
136 legacy_dse_from_prefs->prepopulate_id != modern_value->prepopulate_id) {
137 // This looks like a user-selected value, so let's migrate it.
138 // TODO(erikwright): Remove this migration logic when this stat approaches
139 // zero.
140 UMA_HISTOGRAM_BOOLEAN("Search.MigratedPrefToDictionaryValue", true);
141 default_search_manager.SetUserSelectedDefaultSearchEngine(
142 *legacy_dse_from_prefs);
146 ClearDefaultSearchProviderFromLegacyPrefs(pref_service);
149 void OnPrefsInitialized(PrefService* pref_service,
150 bool pref_service_initialization_success) {
151 MigrateDefaultSearchPref(pref_service);
154 } // namespace
156 void ConfigureDefaultSearchPrefMigrationToDictionaryValue(
157 PrefService* pref_service) {
158 if (pref_service->GetInitializationStatus() ==
159 PrefService::INITIALIZATION_STATUS_WAITING) {
160 pref_service->AddPrefInitObserver(
161 base::Bind(&OnPrefsInitialized, base::Unretained(pref_service)));
162 } else {
163 MigrateDefaultSearchPref(pref_service);