| 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_manager.h" | 5 #include "chrome/browser/search_engines/default_search_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/i18n/case_conversion.h" | 11 #include "base/i18n/case_conversion.h" |
| 12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/prefs/pref_value_map.h" |
| 13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/search_engines/template_url.h" | 21 #include "chrome/browser/search_engines/template_url.h" |
| 21 #include "chrome/browser/search_engines/template_url_service.h" | 22 #include "chrome/browser/search_engines/template_url_service.h" |
| 22 #include "chrome/browser/search_engines/util.h" | 23 #include "chrome/browser/search_engines/util.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 const char DefaultSearchManager::kInputEncodings[] = "input_encodings"; | 61 const char DefaultSearchManager::kInputEncodings[] = "input_encodings"; |
| 61 | 62 |
| 62 const char DefaultSearchManager::kDateCreated[] = "date_created"; | 63 const char DefaultSearchManager::kDateCreated[] = "date_created"; |
| 63 const char DefaultSearchManager::kLastModified[] = "last_modified"; | 64 const char DefaultSearchManager::kLastModified[] = "last_modified"; |
| 64 | 65 |
| 65 const char DefaultSearchManager::kUsageCount[] = "usage_count"; | 66 const char DefaultSearchManager::kUsageCount[] = "usage_count"; |
| 66 const char DefaultSearchManager::kAlternateURLs[] = "alternate_urls"; | 67 const char DefaultSearchManager::kAlternateURLs[] = "alternate_urls"; |
| 67 const char DefaultSearchManager::kSearchTermsReplacementKey[] = | 68 const char DefaultSearchManager::kSearchTermsReplacementKey[] = |
| 68 "search_terms_replacement_key"; | 69 "search_terms_replacement_key"; |
| 69 const char DefaultSearchManager::kCreatedByPolicy[] = "created_by_policy"; | 70 const char DefaultSearchManager::kCreatedByPolicy[] = "created_by_policy"; |
| 71 const char DefaultSearchManager::kDisabledByPolicy[] = "disabled_by_policy"; |
| 70 | 72 |
| 71 DefaultSearchManager::DefaultSearchManager(PrefService* pref_service) | 73 DefaultSearchManager::DefaultSearchManager(PrefService* pref_service) |
| 72 : pref_service_(pref_service) { | 74 : pref_service_(pref_service) { |
| 73 DCHECK(pref_service_); | 75 DCHECK(pref_service_); |
| 74 } | 76 } |
| 75 | 77 |
| 76 DefaultSearchManager::~DefaultSearchManager() { | 78 DefaultSearchManager::~DefaultSearchManager() { |
| 77 } | 79 } |
| 78 | 80 |
| 79 // static | 81 // static |
| 80 void DefaultSearchManager::RegisterProfilePrefs( | 82 void DefaultSearchManager::RegisterProfilePrefs( |
| 81 user_prefs::PrefRegistrySyncable* registry) { | 83 user_prefs::PrefRegistrySyncable* registry) { |
| 82 registry->RegisterDictionaryPref( | 84 registry->RegisterDictionaryPref( |
| 83 kDefaultSearchProviderData, | 85 kDefaultSearchProviderData, |
| 84 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 86 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 85 } | 87 } |
| 86 | 88 |
| 89 // static |
| 90 void DefaultSearchManager::AddPrefValueToMap(PrefValueMap* pref_value_map, |
| 91 base::DictionaryValue* value) { |
| 92 pref_value_map->SetValue(kDefaultSearchProviderData, value); |
| 93 } |
| 94 |
| 87 bool DefaultSearchManager::GetDefaultSearchEngine(TemplateURLData* data) { | 95 bool DefaultSearchManager::GetDefaultSearchEngine(TemplateURLData* data) { |
| 88 const base::DictionaryValue* url_dict = | 96 const base::DictionaryValue* url_dict = |
| 89 pref_service_->GetDictionary(kDefaultSearchProviderData); | 97 pref_service_->GetDictionary(kDefaultSearchProviderData); |
| 90 | 98 |
| 91 if (url_dict->empty()) | 99 if (url_dict->empty()) |
| 92 return false; | 100 return false; |
| 93 | 101 |
| 94 std::string search_url; | 102 std::string search_url; |
| 95 base::string16 keyword; | 103 base::string16 keyword; |
| 96 url_dict->GetString(kURL, &search_url); | 104 url_dict->GetString(kURL, &search_url); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 139 |
| 132 double date_created = 0.0; | 140 double date_created = 0.0; |
| 133 double last_modified = 0.0; | 141 double last_modified = 0.0; |
| 134 url_dict->GetDouble(kDateCreated, &date_created); | 142 url_dict->GetDouble(kDateCreated, &date_created); |
| 135 url_dict->GetDouble(kLastModified, &last_modified); | 143 url_dict->GetDouble(kLastModified, &last_modified); |
| 136 data->date_created = base::Time::FromInternalValue(date_created); | 144 data->date_created = base::Time::FromInternalValue(date_created); |
| 137 data->last_modified = base::Time::FromInternalValue(last_modified); | 145 data->last_modified = base::Time::FromInternalValue(last_modified); |
| 138 | 146 |
| 139 url_dict->GetInteger(kUsageCount, &data->usage_count); | 147 url_dict->GetInteger(kUsageCount, &data->usage_count); |
| 140 | 148 |
| 141 const base::ListValue* alternate_urls = new base::ListValue; | 149 const base::ListValue* alternate_urls; |
| 142 url_dict->GetList(kAlternateURLs, &alternate_urls); | 150 url_dict->GetList(kAlternateURLs, &alternate_urls); |
| 143 data->alternate_urls.clear(); | 151 data->alternate_urls.clear(); |
| 144 for (base::ListValue::const_iterator it = alternate_urls->begin(); | 152 for (base::ListValue::const_iterator it = alternate_urls->begin(); |
| 145 it != alternate_urls->end(); ++it) { | 153 it != alternate_urls->end(); ++it) { |
| 146 std::string alternate_url; | 154 std::string alternate_url; |
| 147 if ((*it)->GetAsString(&alternate_url)) | 155 if ((*it)->GetAsString(&alternate_url)) |
| 148 data->alternate_urls.push_back(alternate_url); | 156 data->alternate_urls.push_back(alternate_url); |
| 149 } | 157 } |
| 150 | 158 |
| 151 const base::ListValue* encodings = new base::ListValue; | 159 const base::ListValue* encodings; |
| 152 url_dict->GetList(kInputEncodings, &encodings); | 160 url_dict->GetList(kInputEncodings, &encodings); |
| 153 data->input_encodings.clear(); | 161 data->input_encodings.clear(); |
| 154 for (base::ListValue::const_iterator it = encodings->begin(); | 162 for (base::ListValue::const_iterator it = encodings->begin(); |
| 155 it != encodings->end(); ++it) { | 163 it != encodings->end(); ++it) { |
| 156 std::string encoding; | 164 std::string encoding; |
| 157 if ((*it)->GetAsString(&encoding)) | 165 if ((*it)->GetAsString(&encoding)) |
| 158 data->input_encodings.push_back(encoding); | 166 data->input_encodings.push_back(encoding); |
| 159 } | 167 } |
| 160 | 168 |
| 161 url_dict->GetString(kSearchTermsReplacementKey, | 169 url_dict->GetString(kSearchTermsReplacementKey, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 180 url_dict.SetString(kURL, data.url()); | 188 url_dict.SetString(kURL, data.url()); |
| 181 url_dict.SetString(kSuggestionsURL, data.suggestions_url); | 189 url_dict.SetString(kSuggestionsURL, data.suggestions_url); |
| 182 url_dict.SetString(kInstantURL, data.instant_url); | 190 url_dict.SetString(kInstantURL, data.instant_url); |
| 183 url_dict.SetString(kImageURL, data.image_url); | 191 url_dict.SetString(kImageURL, data.image_url); |
| 184 url_dict.SetString(kNewTabURL, data.new_tab_url); | 192 url_dict.SetString(kNewTabURL, data.new_tab_url); |
| 185 url_dict.SetString(kFaviconURL, data.favicon_url.spec()); | 193 url_dict.SetString(kFaviconURL, data.favicon_url.spec()); |
| 186 url_dict.SetString(kOriginatingURL, data.originating_url.spec()); | 194 url_dict.SetString(kOriginatingURL, data.originating_url.spec()); |
| 187 | 195 |
| 188 url_dict.SetString(kSearchURLPostParams, data.search_url_post_params); | 196 url_dict.SetString(kSearchURLPostParams, data.search_url_post_params); |
| 189 url_dict.SetString(kSuggestionsURLPostParams, | 197 url_dict.SetString(kSuggestionsURLPostParams, |
| 190 data.suggestions_url_post_params); | 198 data.suggestions_url_post_params); |
| 191 url_dict.SetString(kInstantURLPostParams, data.instant_url_post_params); | 199 url_dict.SetString(kInstantURLPostParams, data.instant_url_post_params); |
| 192 url_dict.SetString(kImageURLPostParams, data.image_url_post_params); | 200 url_dict.SetString(kImageURLPostParams, data.image_url_post_params); |
| 193 | 201 |
| 194 url_dict.SetBoolean(kSafeForAutoReplace, data.safe_for_autoreplace); | 202 url_dict.SetBoolean(kSafeForAutoReplace, data.safe_for_autoreplace); |
| 195 | 203 |
| 196 url_dict.SetDouble(kDateCreated, data.date_created.ToInternalValue()); | 204 url_dict.SetDouble(kDateCreated, data.date_created.ToInternalValue()); |
| 197 url_dict.SetDouble(kLastModified, data.last_modified.ToInternalValue()); | 205 url_dict.SetDouble(kLastModified, data.last_modified.ToInternalValue()); |
| 198 url_dict.SetInteger(kUsageCount, data.usage_count); | 206 url_dict.SetInteger(kUsageCount, data.usage_count); |
| 199 | 207 |
| 200 base::ListValue* alternate_urls = new base::ListValue; | 208 scoped_ptr<base::ListValue> alternate_urls(new base::ListValue); |
| 201 for (std::vector<std::string>::const_iterator it = | 209 for (std::vector<std::string>::const_iterator it = |
| 202 data.alternate_urls.begin(); | 210 data.alternate_urls.begin(); |
| 203 it != data.alternate_urls.end(); ++it) { | 211 it != data.alternate_urls.end(); ++it) { |
| 204 alternate_urls->AppendString(*it); | 212 alternate_urls->AppendString(*it); |
| 205 } | 213 } |
| 206 url_dict.Set(kAlternateURLs, alternate_urls); | 214 url_dict.Set(kAlternateURLs, alternate_urls.release()); |
| 207 | 215 |
| 208 base::ListValue* encodings = new base::ListValue; | 216 scoped_ptr<base::ListValue> encodings(new base::ListValue); |
| 209 for (std::vector<std::string>::const_iterator it = | 217 for (std::vector<std::string>::const_iterator it = |
| 210 data.input_encodings.begin(); | 218 data.input_encodings.begin(); |
| 211 it != data.input_encodings.end(); ++it) { | 219 it != data.input_encodings.end(); ++it) { |
| 212 encodings->AppendString(*it); | 220 encodings->AppendString(*it); |
| 213 } | 221 } |
| 214 url_dict.Set(kInputEncodings, encodings); | 222 url_dict.Set(kInputEncodings, encodings.release()); |
| 215 | 223 |
| 216 url_dict.SetString(kSearchTermsReplacementKey, | 224 url_dict.SetString(kSearchTermsReplacementKey, |
| 217 data.search_terms_replacement_key); | 225 data.search_terms_replacement_key); |
| 218 | 226 |
| 219 url_dict.SetBoolean(kCreatedByPolicy, data.created_by_policy); | 227 url_dict.SetBoolean(kCreatedByPolicy, data.created_by_policy); |
| 220 | 228 |
| 221 pref_service_->Set(kDefaultSearchProviderData, url_dict); | 229 pref_service_->Set(kDefaultSearchProviderData, url_dict); |
| 222 } | 230 } |
| 223 | 231 |
| 224 void DefaultSearchManager::ClearUserSelectedDefaultSearchEngine() { | 232 void DefaultSearchManager::ClearUserSelectedDefaultSearchEngine() { |
| 225 pref_service_->ClearPref(kDefaultSearchProviderData); | 233 pref_service_->ClearPref(kDefaultSearchProviderData); |
| 226 } | 234 } |
| OLD | NEW |