OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/template_url_prepopulate_data.h" | 5 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
6 | 6 |
7 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
8 #include <locale.h> | 8 #include <locale.h> |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 3132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3143 const int kCurrentDataVersion = 38; | 3143 const int kCurrentDataVersion = 38; |
3144 if (!prefs) | 3144 if (!prefs) |
3145 return kCurrentDataVersion; | 3145 return kCurrentDataVersion; |
3146 // If a version number exist in the preferences file, it overrides the | 3146 // If a version number exist in the preferences file, it overrides the |
3147 // version of the built-in data. | 3147 // version of the built-in data. |
3148 int version = | 3148 int version = |
3149 prefs->GetInteger(prefs::kSearchProviderOverridesVersion); | 3149 prefs->GetInteger(prefs::kSearchProviderOverridesVersion); |
3150 return (version >= 0) ? version : kCurrentDataVersion; | 3150 return (version >= 0) ? version : kCurrentDataVersion; |
3151 } | 3151 } |
3152 | 3152 |
3153 TemplateURL* MakePrepopulatedTemplateURL(const string16& name, | 3153 TemplateURL* MakePrepopulatedTemplateURL(Profile* profile, |
| 3154 const string16& name, |
3154 const string16& keyword, | 3155 const string16& keyword, |
3155 const base::StringPiece& search_url, | 3156 const base::StringPiece& search_url, |
3156 const base::StringPiece& suggest_url, | 3157 const base::StringPiece& suggest_url, |
3157 const base::StringPiece& instant_url, | 3158 const base::StringPiece& instant_url, |
3158 const base::StringPiece& favicon_url, | 3159 const base::StringPiece& favicon_url, |
3159 const base::StringPiece& encoding, | 3160 const base::StringPiece& encoding, |
3160 int id) { | 3161 int id) { |
3161 TemplateURLData data; | 3162 TemplateURLData data; |
3162 data.short_name = name; | 3163 data.short_name = name; |
3163 if (keyword.empty()) | 3164 if (keyword.empty()) |
3164 data.SetAutogenerateKeyword(true); | 3165 data.SetAutogenerateKeyword(true); |
3165 else | 3166 else |
3166 data.SetKeyword(keyword); | 3167 data.SetKeyword(keyword); |
3167 data.SetURL(search_url.as_string()); | 3168 data.SetURL(search_url.as_string()); |
3168 data.suggestions_url = suggest_url.as_string(); | 3169 data.suggestions_url = suggest_url.as_string(); |
3169 data.instant_url = instant_url.as_string(); | 3170 data.instant_url = instant_url.as_string(); |
3170 data.favicon_url = GURL(favicon_url.as_string()); | 3171 data.favicon_url = GURL(favicon_url.as_string()); |
3171 data.show_in_default_list = true; | 3172 data.show_in_default_list = true; |
3172 data.safe_for_autoreplace = true; | 3173 data.safe_for_autoreplace = true; |
3173 data.input_encodings.push_back(encoding.as_string()); | 3174 data.input_encodings.push_back(encoding.as_string()); |
3174 data.date_created = base::Time(); | 3175 data.date_created = base::Time(); |
3175 data.last_modified = base::Time(); | 3176 data.last_modified = base::Time(); |
3176 data.prepopulate_id = id; | 3177 data.prepopulate_id = id; |
3177 return new TemplateURL(data); | 3178 return new TemplateURL(profile, data); |
3178 } | 3179 } |
3179 | 3180 |
3180 void GetPrepopulatedTemplateFromPrefs(PrefService* prefs, | 3181 void GetPrepopulatedTemplateFromPrefs(Profile* profile, |
3181 std::vector<TemplateURL*>* t_urls) { | 3182 std::vector<TemplateURL*>* t_urls) { |
3182 if (!prefs) | 3183 if (!profile) |
3183 return; | 3184 return; |
3184 | 3185 |
3185 const ListValue* list = | 3186 const ListValue* list = |
3186 prefs->GetList(prefs::kSearchProviderOverrides); | 3187 profile->GetPrefs()->GetList(prefs::kSearchProviderOverrides); |
3187 if (!list) | 3188 if (!list) |
3188 return; | 3189 return; |
3189 | 3190 |
3190 string16 name; | 3191 string16 name; |
3191 string16 keyword; | 3192 string16 keyword; |
3192 std::string search_url; | 3193 std::string search_url; |
3193 std::string suggest_url; | 3194 std::string suggest_url; |
3194 std::string instant_url; | 3195 std::string instant_url; |
3195 std::string favicon_url; | 3196 std::string favicon_url; |
3196 std::string encoding; | 3197 std::string encoding; |
(...skipping 13 matching lines...) Expand all Loading... |
3210 engine->Get("encoding", &val) && val->GetAsString(&encoding) && | 3211 engine->Get("encoding", &val) && val->GetAsString(&encoding) && |
3211 engine->Get("id", &val) && val->GetAsInteger(&id)) { | 3212 engine->Get("id", &val) && val->GetAsInteger(&id)) { |
3212 // These next fields are not allowed to be empty. | 3213 // These next fields are not allowed to be empty. |
3213 if (name.empty() || search_url.empty() || favicon_url.empty() || | 3214 if (name.empty() || search_url.empty() || favicon_url.empty() || |
3214 encoding.empty()) | 3215 encoding.empty()) |
3215 return; | 3216 return; |
3216 } else { | 3217 } else { |
3217 // Got a parsing error. No big deal. | 3218 // Got a parsing error. No big deal. |
3218 continue; | 3219 continue; |
3219 } | 3220 } |
3220 t_urls->push_back(MakePrepopulatedTemplateURL(name, keyword, search_url, | 3221 t_urls->push_back(MakePrepopulatedTemplateURL(profile, name, keyword, |
3221 suggest_url, instant_url, favicon_url, encoding, id)); | 3222 search_url, suggest_url, instant_url, favicon_url, encoding, id)); |
3222 } | 3223 } |
3223 } | 3224 } |
3224 | 3225 |
3225 // The caller owns the returned TemplateURL. | 3226 // The caller owns the returned TemplateURL. |
3226 TemplateURL* MakePrepopulatedTemplateURLFromPrepopulateEngine( | 3227 TemplateURL* MakePrepopulatedTemplateURLFromPrepopulateEngine( |
| 3228 Profile* profile, |
3227 const PrepopulatedEngine& engine) { | 3229 const PrepopulatedEngine& engine) { |
3228 return MakePrepopulatedTemplateURL(WideToUTF16(engine.name), | 3230 return MakePrepopulatedTemplateURL(profile, WideToUTF16(engine.name), |
3229 WideToUTF16(engine.keyword), engine.search_url, engine.suggest_url, | 3231 WideToUTF16(engine.keyword), engine.search_url, engine.suggest_url, |
3230 engine.instant_url, engine.favicon_url, engine.encoding, engine.id); | 3232 engine.instant_url, engine.favicon_url, engine.encoding, engine.id); |
3231 } | 3233 } |
3232 | 3234 |
3233 void GetPrepopulatedEngines(PrefService* prefs, | 3235 void GetPrepopulatedEngines(Profile* profile, |
3234 std::vector<TemplateURL*>* t_urls, | 3236 std::vector<TemplateURL*>* t_urls, |
3235 size_t* default_search_provider_index) { | 3237 size_t* default_search_provider_index) { |
3236 // If there is a set of search engines in the preferences file, it overrides | 3238 // If there is a set of search engines in the preferences file, it overrides |
3237 // the built-in set. | 3239 // the built-in set. |
3238 *default_search_provider_index = 0; | 3240 *default_search_provider_index = 0; |
3239 GetPrepopulatedTemplateFromPrefs(prefs, t_urls); | 3241 GetPrepopulatedTemplateFromPrefs(profile, t_urls); |
3240 if (!t_urls->empty()) | 3242 if (!t_urls->empty()) |
3241 return; | 3243 return; |
3242 | 3244 |
3243 const PrepopulatedEngine** engines; | 3245 const PrepopulatedEngine** engines; |
3244 size_t num_engines; | 3246 size_t num_engines; |
3245 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); | 3247 GetPrepopulationSetFromCountryID(profile ? profile->GetPrefs() : NULL, |
| 3248 &engines, &num_engines); |
3246 for (size_t i = 0; i != num_engines; ++i) { | 3249 for (size_t i = 0; i != num_engines; ++i) { |
3247 t_urls->push_back( | 3250 t_urls->push_back( |
3248 MakePrepopulatedTemplateURLFromPrepopulateEngine(*engines[i])); | 3251 MakePrepopulatedTemplateURLFromPrepopulateEngine(profile, *engines[i])); |
3249 } | 3252 } |
3250 } | 3253 } |
3251 | 3254 |
3252 TemplateURL* GetPrepopulatedDefaultSearch(PrefService* prefs) { | 3255 TemplateURL* GetPrepopulatedDefaultSearch(Profile* profile) { |
3253 TemplateURL* default_search_provider = NULL; | 3256 TemplateURL* default_search_provider = NULL; |
3254 ScopedVector<TemplateURL> loaded_urls; | 3257 ScopedVector<TemplateURL> loaded_urls; |
3255 size_t default_search_index; | 3258 size_t default_search_index; |
3256 // This could be more efficient. We are loading all the URLs to only keep | 3259 // This could be more efficient. We are loading all the URLs to only keep |
3257 // the first one. | 3260 // the first one. |
3258 GetPrepopulatedEngines(prefs, &loaded_urls.get(), &default_search_index); | 3261 GetPrepopulatedEngines(profile, &loaded_urls.get(), &default_search_index); |
3259 if (default_search_index < loaded_urls.size()) { | 3262 if (default_search_index < loaded_urls.size()) { |
3260 default_search_provider = loaded_urls[default_search_index]; | 3263 default_search_provider = loaded_urls[default_search_index]; |
3261 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); | 3264 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); |
3262 } | 3265 } |
3263 return default_search_provider; | 3266 return default_search_provider; |
3264 } | 3267 } |
3265 | 3268 |
3266 static const PrepopulatedEngine* GetEngineForURL(const std::string& url) { | 3269 static const PrepopulatedEngine* GetEngineForURL(const std::string& url) { |
3267 // We may get a valid URL, or we may get the Google prepopulate URL which | 3270 // We may get a valid URL, or we may get the Google prepopulate URL which |
3268 // can't be converted to a GURL. Instead of forcing callers to substitute to | 3271 // can't be converted to a GURL. Instead of forcing callers to substitute to |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3304 UTF8ToUTF16(as_gurl.host()) : | 3307 UTF8ToUTF16(as_gurl.host()) : |
3305 l10n_util::GetStringUTF16(IDS_UNKNOWN_SEARCH_ENGINE_NAME); | 3308 l10n_util::GetStringUTF16(IDS_UNKNOWN_SEARCH_ENGINE_NAME); |
3306 } | 3309 } |
3307 | 3310 |
3308 SearchEngineType GetEngineType(const std::string& url) { | 3311 SearchEngineType GetEngineType(const std::string& url) { |
3309 const PrepopulatedEngine* engine = GetEngineForURL(url); | 3312 const PrepopulatedEngine* engine = GetEngineForURL(url); |
3310 return engine ? engine->type : SEARCH_ENGINE_OTHER; | 3313 return engine ? engine->type : SEARCH_ENGINE_OTHER; |
3311 } | 3314 } |
3312 | 3315 |
3313 } // namespace TemplateURLPrepopulateData | 3316 } // namespace TemplateURLPrepopulateData |
OLD | NEW |