OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/importer/profile_writer.h" | 5 #include "chrome/browser/importer/profile_writer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 // HostPathMap. | 234 // HostPathMap. |
235 // | 235 // |
236 // If |try_url_if_invalid| is true, and |t_url| isn't valid, a string is built | 236 // If |try_url_if_invalid| is true, and |t_url| isn't valid, a string is built |
237 // from the raw TemplateURL string. Use a value of true for |try_url_if_invalid| | 237 // from the raw TemplateURL string. Use a value of true for |try_url_if_invalid| |
238 // when checking imported URLs as the imported URL may not be valid yet may | 238 // when checking imported URLs as the imported URL may not be valid yet may |
239 // match the host+path of one of the default URLs. This is used to catch the | 239 // match the host+path of one of the default URLs. This is used to catch the |
240 // case of IE using an invalid OSDD URL for Live Search, yet the host+path | 240 // case of IE using an invalid OSDD URL for Live Search, yet the host+path |
241 // matches our prepopulate data. IE's URL for Live Search is something like | 241 // matches our prepopulate data. IE's URL for Live Search is something like |
242 // 'http://...{Language}...'. As {Language} is not a valid OSDD parameter value | 242 // 'http://...{Language}...'. As {Language} is not a valid OSDD parameter value |
243 // the TemplateURL is invalid. | 243 // the TemplateURL is invalid. |
244 static std::string BuildHostPathKey(const TemplateURL* t_url, | 244 static std::string BuildHostPathKey(Profile* profile, |
245 const TemplateURL* t_url, | |
245 bool try_url_if_invalid) { | 246 bool try_url_if_invalid) { |
246 if (t_url->url()) { | 247 if (t_url->url()) { |
247 if (try_url_if_invalid && !t_url->url()->IsValid()) | 248 if (try_url_if_invalid && !t_url->url()->IsValid()) |
248 return HostPathKeyForURL(GURL(t_url->url()->url())); | 249 return HostPathKeyForURL(GURL(t_url->url()->url())); |
249 | 250 |
250 if (t_url->url()->SupportsReplacement()) { | 251 if (t_url->url()->SupportsReplacement()) { |
251 return HostPathKeyForURL(GURL( | 252 return HostPathKeyForURL(GURL( |
252 t_url->url()->ReplaceSearchTerms( | 253 t_url->url()->ReplaceSearchTerms( |
253 *t_url, ASCIIToUTF16("random string"), | 254 profile, *t_url, ASCIIToUTF16("random string"), |
Peter Kasting
2011/08/11 00:35:17
HostPathKeyForURL() strips the query params so thi
| |
254 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()))); | 255 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16()))); |
255 } | 256 } |
256 } | 257 } |
257 return std::string(); | 258 return std::string(); |
258 } | 259 } |
259 | 260 |
260 // Builds a set that contains an entry of the host+path for each TemplateURL in | 261 // Builds a set that contains an entry of the host+path for each TemplateURL in |
261 // the TemplateURLService that has a valid search url. | 262 // the TemplateURLService that has a valid search url. |
262 static void BuildHostPathMap(const TemplateURLService& model, | 263 static void BuildHostPathMap(Profile* profile, |
264 const TemplateURLService& model, | |
263 HostPathMap* host_path_map) { | 265 HostPathMap* host_path_map) { |
264 std::vector<const TemplateURL*> template_urls = model.GetTemplateURLs(); | 266 std::vector<const TemplateURL*> template_urls = model.GetTemplateURLs(); |
265 for (size_t i = 0; i < template_urls.size(); ++i) { | 267 for (size_t i = 0; i < template_urls.size(); ++i) { |
266 const std::string host_path = BuildHostPathKey(template_urls[i], false); | 268 const std::string host_path = BuildHostPathKey(profile, template_urls[i], |
269 false); | |
267 if (!host_path.empty()) { | 270 if (!host_path.empty()) { |
268 const TemplateURL* existing_turl = (*host_path_map)[host_path]; | 271 const TemplateURL* existing_turl = (*host_path_map)[host_path]; |
269 if (!existing_turl || | 272 if (!existing_turl || |
270 (template_urls[i]->show_in_default_list() && | 273 (template_urls[i]->show_in_default_list() && |
271 !existing_turl->show_in_default_list())) { | 274 !existing_turl->show_in_default_list())) { |
272 // If there are multiple TemplateURLs with the same host+path, favor | 275 // If there are multiple TemplateURLs with the same host+path, favor |
273 // those shown in the default list. If there are multiple potential | 276 // those shown in the default list. If there are multiple potential |
274 // defaults, favor the first one, which should be the more commonly used | 277 // defaults, favor the first one, which should be the more commonly used |
275 // one. | 278 // one. |
276 (*host_path_map)[host_path] = template_urls[i]; | 279 (*host_path_map)[host_path] = template_urls[i]; |
277 } | 280 } |
278 } // else case, TemplateURL doesn't have a search url, doesn't support | 281 } // else case, TemplateURL doesn't have a search url, doesn't support |
279 // replacement, or doesn't have valid GURL. Ignore it. | 282 // replacement, or doesn't have valid GURL. Ignore it. |
280 } | 283 } |
281 } | 284 } |
282 | 285 |
283 void ProfileWriter::AddKeywords(const std::vector<TemplateURL*>& template_urls, | 286 void ProfileWriter::AddKeywords(const std::vector<TemplateURL*>& template_urls, |
284 int default_keyword_index, | 287 int default_keyword_index, |
285 bool unique_on_host_and_path) { | 288 bool unique_on_host_and_path) { |
286 TemplateURLService* model = | 289 TemplateURLService* model = |
287 TemplateURLServiceFactory::GetForProfile(profile_); | 290 TemplateURLServiceFactory::GetForProfile(profile_); |
288 HostPathMap host_path_map; | 291 HostPathMap host_path_map; |
289 if (unique_on_host_and_path) | 292 if (unique_on_host_and_path) |
290 BuildHostPathMap(*model, &host_path_map); | 293 BuildHostPathMap(profile_, *model, &host_path_map); |
291 | 294 |
292 for (std::vector<TemplateURL*>::const_iterator i = template_urls.begin(); | 295 for (std::vector<TemplateURL*>::const_iterator i = template_urls.begin(); |
293 i != template_urls.end(); ++i) { | 296 i != template_urls.end(); ++i) { |
294 TemplateURL* t_url = *i; | 297 TemplateURL* t_url = *i; |
295 bool default_keyword = | 298 bool default_keyword = |
296 default_keyword_index >= 0 && | 299 default_keyword_index >= 0 && |
297 (i - template_urls.begin() == default_keyword_index); | 300 (i - template_urls.begin() == default_keyword_index); |
298 | 301 |
299 // TemplateURLService requires keywords to be unique. If there is already a | 302 // TemplateURLService requires keywords to be unique. If there is already a |
300 // TemplateURL with this keyword, don't import it again. | 303 // TemplateURL with this keyword, don't import it again. |
301 const TemplateURL* turl_with_keyword = | 304 const TemplateURL* turl_with_keyword = |
302 model->GetTemplateURLForKeyword(t_url->keyword()); | 305 model->GetTemplateURLForKeyword(t_url->keyword()); |
303 if (turl_with_keyword != NULL) { | 306 if (turl_with_keyword != NULL) { |
304 if (default_keyword) | 307 if (default_keyword) |
305 model->SetDefaultSearchProvider(turl_with_keyword); | 308 model->SetDefaultSearchProvider(turl_with_keyword); |
306 delete t_url; | 309 delete t_url; |
307 continue; | 310 continue; |
308 } | 311 } |
309 | 312 |
310 // For search engines if there is already a keyword with the same | 313 // For search engines if there is already a keyword with the same |
311 // host+path, we don't import it. This is done to avoid both duplicate | 314 // host+path, we don't import it. This is done to avoid both duplicate |
312 // search providers (such as two Googles, or two Yahoos) as well as making | 315 // search providers (such as two Googles, or two Yahoos) as well as making |
313 // sure the search engines we provide aren't replaced by those from the | 316 // sure the search engines we provide aren't replaced by those from the |
314 // imported browser. | 317 // imported browser. |
315 if (unique_on_host_and_path && | 318 if (unique_on_host_and_path && |
316 host_path_map.find( | 319 host_path_map.find( |
317 BuildHostPathKey(t_url, true)) != host_path_map.end()) { | 320 BuildHostPathKey(profile_, t_url, true)) != host_path_map.end()) { |
318 if (default_keyword) { | 321 if (default_keyword) { |
319 const TemplateURL* turl_with_host_path = | 322 const TemplateURL* turl_with_host_path = |
320 host_path_map[BuildHostPathKey(t_url, true)]; | 323 host_path_map[BuildHostPathKey(profile_, t_url, true)]; |
321 if (turl_with_host_path) | 324 if (turl_with_host_path) |
322 model->SetDefaultSearchProvider(turl_with_host_path); | 325 model->SetDefaultSearchProvider(turl_with_host_path); |
323 else | 326 else |
324 NOTREACHED(); // BuildHostPathMap should only insert non-null values. | 327 NOTREACHED(); // BuildHostPathMap should only insert non-null values. |
325 } | 328 } |
326 delete t_url; | 329 delete t_url; |
327 continue; | 330 continue; |
328 } | 331 } |
329 if (t_url->url() && t_url->url()->IsValid()) { | 332 if (t_url->url() && t_url->url()->IsValid()) { |
330 model->Add(t_url); | 333 model->Add(t_url); |
331 if (default_keyword && TemplateURL::SupportsReplacement(t_url)) | 334 if (default_keyword && TemplateURL::SupportsReplacement(t_url)) |
332 model->SetDefaultSearchProvider(t_url); | 335 model->SetDefaultSearchProvider(t_url); |
333 } else { | 336 } else { |
334 // Don't add invalid TemplateURLs to the model. | 337 // Don't add invalid TemplateURLs to the model. |
335 delete t_url; | 338 delete t_url; |
336 } | 339 } |
337 } | 340 } |
338 } | 341 } |
339 | 342 |
340 ProfileWriter::~ProfileWriter() {} | 343 ProfileWriter::~ProfileWriter() {} |
OLD | NEW |