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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/search_engines/default_search_manager.h" | 15 #include "chrome/browser/search_engines/default_search_manager.h" |
13 #include "chrome/browser/search_engines/template_url.h" | 16 #include "chrome/browser/search_engines/template_url.h" |
14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 17 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
15 #include "chrome/browser/search_engines/template_url_service.h" | 18 #include "chrome/browser/search_engines/template_url_service.h" |
| 19 #include "chrome/common/pref_names.h" |
| 20 #include "url/gurl.h" |
16 | 21 |
17 namespace { | 22 namespace { |
18 | 23 |
| 24 scoped_ptr<TemplateURLData> LoadDefaultSearchProviderFromLegacyPrefs( |
| 25 PrefService* prefs) { |
| 26 if (!prefs || !prefs->HasPrefPath(prefs::kDefaultSearchProviderSearchURL) || |
| 27 !prefs->HasPrefPath(prefs::kDefaultSearchProviderKeyword)) |
| 28 return scoped_ptr<TemplateURLData>(); |
| 29 |
| 30 const PrefService::Preference* pref = |
| 31 prefs->FindPreference(prefs::kDefaultSearchProviderSearchURL); |
| 32 DCHECK(pref); |
| 33 if (pref->IsManaged()) |
| 34 return scoped_ptr<TemplateURLData>(); |
| 35 |
| 36 base::string16 name = |
| 37 base::UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderName)); |
| 38 base::string16 keyword = |
| 39 base::UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderKeyword)); |
| 40 if (keyword.empty()) |
| 41 return scoped_ptr<TemplateURLData>(); |
| 42 std::string search_url = |
| 43 prefs->GetString(prefs::kDefaultSearchProviderSearchURL); |
| 44 if (search_url.empty()) |
| 45 return scoped_ptr<TemplateURLData>(); |
| 46 std::string suggest_url = |
| 47 prefs->GetString(prefs::kDefaultSearchProviderSuggestURL); |
| 48 std::string instant_url = |
| 49 prefs->GetString(prefs::kDefaultSearchProviderInstantURL); |
| 50 std::string image_url = |
| 51 prefs->GetString(prefs::kDefaultSearchProviderImageURL); |
| 52 std::string new_tab_url = |
| 53 prefs->GetString(prefs::kDefaultSearchProviderNewTabURL); |
| 54 std::string search_url_post_params = |
| 55 prefs->GetString(prefs::kDefaultSearchProviderSearchURLPostParams); |
| 56 std::string suggest_url_post_params = |
| 57 prefs->GetString(prefs::kDefaultSearchProviderSuggestURLPostParams); |
| 58 std::string instant_url_post_params = |
| 59 prefs->GetString(prefs::kDefaultSearchProviderInstantURLPostParams); |
| 60 std::string image_url_post_params = |
| 61 prefs->GetString(prefs::kDefaultSearchProviderImageURLPostParams); |
| 62 std::string icon_url = |
| 63 prefs->GetString(prefs::kDefaultSearchProviderIconURL); |
| 64 std::string encodings = |
| 65 prefs->GetString(prefs::kDefaultSearchProviderEncodings); |
| 66 std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID); |
| 67 std::string prepopulate_id = |
| 68 prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID); |
| 69 const base::ListValue* alternate_urls = |
| 70 prefs->GetList(prefs::kDefaultSearchProviderAlternateURLs); |
| 71 std::string search_terms_replacement_key = prefs->GetString( |
| 72 prefs::kDefaultSearchProviderSearchTermsReplacementKey); |
| 73 |
| 74 scoped_ptr<TemplateURLData> default_provider_data(new TemplateURLData); |
| 75 default_provider_data->short_name = name; |
| 76 default_provider_data->SetKeyword(keyword); |
| 77 default_provider_data->SetURL(search_url); |
| 78 default_provider_data->suggestions_url = suggest_url; |
| 79 default_provider_data->instant_url = instant_url; |
| 80 default_provider_data->image_url = image_url; |
| 81 default_provider_data->new_tab_url = new_tab_url; |
| 82 default_provider_data->search_url_post_params = search_url_post_params; |
| 83 default_provider_data->suggestions_url_post_params = |
| 84 suggest_url_post_params; |
| 85 default_provider_data->instant_url_post_params = instant_url_post_params; |
| 86 default_provider_data->image_url_post_params = image_url_post_params; |
| 87 default_provider_data->favicon_url = GURL(icon_url); |
| 88 default_provider_data->show_in_default_list = true; |
| 89 default_provider_data->alternate_urls.clear(); |
| 90 for (size_t i = 0; i < alternate_urls->GetSize(); ++i) { |
| 91 std::string alternate_url; |
| 92 if (alternate_urls->GetString(i, &alternate_url)) |
| 93 default_provider_data->alternate_urls.push_back(alternate_url); |
| 94 } |
| 95 default_provider_data->search_terms_replacement_key = |
| 96 search_terms_replacement_key; |
| 97 base::SplitString(encodings, ';', &default_provider_data->input_encodings); |
| 98 if (!id_string.empty()) { |
| 99 int64 value; |
| 100 base::StringToInt64(id_string, &value); |
| 101 default_provider_data->id = value; |
| 102 } |
| 103 if (!prepopulate_id.empty()) { |
| 104 int value; |
| 105 base::StringToInt(prepopulate_id, &value); |
| 106 default_provider_data->prepopulate_id = value; |
| 107 } |
| 108 return default_provider_data.Pass(); |
| 109 } |
| 110 |
| 111 |
19 void MigrateDefaultSearchPref(PrefService* pref_service) { | 112 void MigrateDefaultSearchPref(PrefService* pref_service) { |
20 DefaultSearchManager default_search_manager(pref_service); | 113 DefaultSearchManager default_search_manager( |
| 114 pref_service, DefaultSearchManager::ObserverCallback()); |
21 | 115 |
22 TemplateURLData modern_user_dse; | 116 if (default_search_manager.GetDefaultSearchEngineSource() != |
23 if (default_search_manager.GetDefaultSearchEngine(&modern_user_dse)) | 117 DefaultSearchManager::FROM_FALLBACK) |
24 return; | 118 return; |
25 | 119 |
26 scoped_ptr<TemplateURLData> legacy_dse_from_prefs; | 120 scoped_ptr<TemplateURLData> legacy_dse_from_prefs( |
27 bool legacy_is_managed = false; | 121 LoadDefaultSearchProviderFromLegacyPrefs(pref_service)); |
28 bool has_legacy_dse_from_prefs = | |
29 TemplateURLService::LoadDefaultSearchProviderFromPrefs( | |
30 pref_service, &legacy_dse_from_prefs, &legacy_is_managed); | |
31 | 122 |
32 if (!has_legacy_dse_from_prefs) { | |
33 // The DSE is undefined. Nothing to migrate. | |
34 return; | |
35 } | |
36 if (!legacy_dse_from_prefs) { | 123 if (!legacy_dse_from_prefs) { |
37 // The DSE is defined as NULL. This can only really be done via policy. | 124 // The DSE is undefined. It might have been policy-defined. Nothing to |
38 // Policy-defined values will be automatically projected into the new | 125 // migrate. |
39 // format. Even if the user did somehow set this manually we do not have a | |
40 // way to migrate it. | |
41 return; | |
42 } | |
43 if (legacy_is_managed) { | |
44 // The DSE is policy-managed, not user-selected. It will automatically be | |
45 // projected into the new location. | |
46 return; | 126 return; |
47 } | 127 } |
48 | 128 |
49 // If the pre-populated DSE matches the DSE from prefs we assume it is not a | 129 // If the pre-populated DSE matches the DSE from prefs we assume it is not a |
50 // user-selected value. | 130 // user-selected value. |
51 scoped_ptr<TemplateURLData> prepopulated_dse( | 131 scoped_ptr<TemplateURLData> prepopulated_dse( |
52 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(pref_service)); | 132 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(pref_service)); |
53 if (prepopulated_dse && | 133 if (prepopulated_dse && |
54 legacy_dse_from_prefs->prepopulate_id == | 134 legacy_dse_from_prefs->prepopulate_id == |
55 prepopulated_dse->prepopulate_id) { | 135 prepopulated_dse->prepopulate_id) { |
(...skipping 22 matching lines...) Expand all Loading... |
78 void ConfigureDefaultSearchPrefMigrationToDictionaryValue( | 158 void ConfigureDefaultSearchPrefMigrationToDictionaryValue( |
79 PrefService* pref_service) { | 159 PrefService* pref_service) { |
80 if (pref_service->GetInitializationStatus() == | 160 if (pref_service->GetInitializationStatus() == |
81 PrefService::INITIALIZATION_STATUS_WAITING) { | 161 PrefService::INITIALIZATION_STATUS_WAITING) { |
82 pref_service->AddPrefInitObserver( | 162 pref_service->AddPrefInitObserver( |
83 base::Bind(&OnPrefsInitialized, base::Unretained(pref_service))); | 163 base::Bind(&OnPrefsInitialized, base::Unretained(pref_service))); |
84 } else { | 164 } else { |
85 MigrateDefaultSearchPref(pref_service); | 165 MigrateDefaultSearchPref(pref_service); |
86 } | 166 } |
87 } | 167 } |
OLD | NEW |