| 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_service.h" | 5 #include "chrome/browser/search_engines/template_url_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 } | 370 } |
| 371 if (!prepopulate_id.empty() && !*is_managed) { | 371 if (!prepopulate_id.empty() && !*is_managed) { |
| 372 int value; | 372 int value; |
| 373 base::StringToInt(prepopulate_id, &value); | 373 base::StringToInt(prepopulate_id, &value); |
| 374 (*default_provider_data)->prepopulate_id = value; | 374 (*default_provider_data)->prepopulate_id = value; |
| 375 } | 375 } |
| 376 return true; | 376 return true; |
| 377 } | 377 } |
| 378 | 378 |
| 379 // static | 379 // static |
| 380 base::string16 TemplateURLService::GenerateKeyword(const GURL& url) { | |
| 381 DCHECK(url.is_valid()); | |
| 382 // Strip "www." off the front of the keyword; otherwise the keyword won't work | |
| 383 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 . | |
| 384 // Special case: if the host was exactly "www." (not sure this can happen but | |
| 385 // perhaps with some weird intranet and custom DNS server?), ensure we at | |
| 386 // least don't return the empty string. | |
| 387 base::string16 keyword(net::StripWWWFromHost(url)); | |
| 388 return keyword.empty() ? base::ASCIIToUTF16("www") : keyword; | |
| 389 } | |
| 390 | |
| 391 // static | |
| 392 base::string16 TemplateURLService::CleanUserInputKeyword( | 380 base::string16 TemplateURLService::CleanUserInputKeyword( |
| 393 const base::string16& keyword) { | 381 const base::string16& keyword) { |
| 394 // Remove the scheme. | 382 // Remove the scheme. |
| 395 base::string16 result(base::i18n::ToLower(keyword)); | 383 base::string16 result(base::i18n::ToLower(keyword)); |
| 396 base::TrimWhitespace(result, base::TRIM_ALL, &result); | 384 base::TrimWhitespace(result, base::TRIM_ALL, &result); |
| 397 url::Component scheme_component; | 385 url::Component scheme_component; |
| 398 if (url::ExtractScheme(base::UTF16ToUTF8(keyword).c_str(), | 386 if (url::ExtractScheme(base::UTF16ToUTF8(keyword).c_str(), |
| 399 static_cast<int>(keyword.length()), | 387 static_cast<int>(keyword.length()), |
| 400 &scheme_component)) { | 388 &scheme_component)) { |
| 401 // If the scheme isn't "http" or "https", bail. The user isn't trying to | 389 // If the scheme isn't "http" or "https", bail. The user isn't trying to |
| (...skipping 15 matching lines...) Expand all Loading... |
| 417 | 405 |
| 418 // Remove leading "www.". | 406 // Remove leading "www.". |
| 419 result = net::StripWWW(result); | 407 result = net::StripWWW(result); |
| 420 | 408 |
| 421 // Remove trailing "/". | 409 // Remove trailing "/". |
| 422 return (result.length() > 0 && result[result.length() - 1] == '/') ? | 410 return (result.length() > 0 && result[result.length() - 1] == '/') ? |
| 423 result.substr(0, result.length() - 1) : result; | 411 result.substr(0, result.length() - 1) : result; |
| 424 } | 412 } |
| 425 | 413 |
| 426 // static | 414 // static |
| 427 GURL TemplateURLService::GenerateSearchURL( | |
| 428 const TemplateURL* t_url, | |
| 429 const SearchTermsData& search_terms_data) { | |
| 430 DCHECK(t_url); | |
| 431 | |
| 432 const TemplateURLRef& search_ref = t_url->url_ref(); | |
| 433 if (!search_ref.IsValid(search_terms_data)) | |
| 434 return GURL(); | |
| 435 | |
| 436 if (!search_ref.SupportsReplacement(search_terms_data)) | |
| 437 return GURL(t_url->url()); | |
| 438 | |
| 439 // Use something obscure for the search terms argument so that in the rare | |
| 440 // case the term replaces the URL it's unlikely another keyword would have the | |
| 441 // same url. | |
| 442 // TODO(jnd): Add additional parameters to get post data when the search URL | |
| 443 // has post parameters. | |
| 444 return GURL(search_ref.ReplaceSearchTerms( | |
| 445 TemplateURLRef::SearchTermsArgs( | |
| 446 base::ASCIIToUTF16("blah.blah.blah.blah.blah")), | |
| 447 search_terms_data, NULL)); | |
| 448 } | |
| 449 | |
| 450 // static | |
| 451 void TemplateURLService::SaveDefaultSearchProviderToPrefs( | 415 void TemplateURLService::SaveDefaultSearchProviderToPrefs( |
| 452 const TemplateURL* t_url, | 416 const TemplateURL* t_url, |
| 453 PrefService* prefs) { | 417 PrefService* prefs) { |
| 454 if (!prefs) | 418 if (!prefs) |
| 455 return; | 419 return; |
| 456 | 420 |
| 457 bool enabled = false; | 421 bool enabled = false; |
| 458 std::string search_url; | 422 std::string search_url; |
| 459 std::string suggest_url; | 423 std::string suggest_url; |
| 460 std::string instant_url; | 424 std::string instant_url; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 initial_default_search_provider_.get() : NULL; | 565 initial_default_search_provider_.get() : NULL; |
| 602 } | 566 } |
| 603 | 567 |
| 604 TemplateURL* TemplateURLService::GetTemplateURLForHost( | 568 TemplateURL* TemplateURLService::GetTemplateURLForHost( |
| 605 const std::string& host) { | 569 const std::string& host) { |
| 606 if (loaded_) | 570 if (loaded_) |
| 607 return provider_map_->GetTemplateURLForHost(host); | 571 return provider_map_->GetTemplateURLForHost(host); |
| 608 TemplateURL* initial_dsp = initial_default_search_provider_.get(); | 572 TemplateURL* initial_dsp = initial_default_search_provider_.get(); |
| 609 if (!initial_dsp) | 573 if (!initial_dsp) |
| 610 return NULL; | 574 return NULL; |
| 611 return (GenerateSearchURL(initial_dsp, search_terms_data()).host() == host) ? | 575 return (initial_dsp->GenerateSearchURL(search_terms_data()).host() == host) ? |
| 612 initial_dsp : NULL; | 576 initial_dsp : NULL; |
| 613 } | 577 } |
| 614 | 578 |
| 615 bool TemplateURLService::Add(TemplateURL* template_url) { | 579 bool TemplateURLService::Add(TemplateURL* template_url) { |
| 616 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); | 580 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); |
| 617 if (!AddNoNotify(template_url, true)) | 581 if (!AddNoNotify(template_url, true)) |
| 618 return false; | 582 return false; |
| 619 NotifyObservers(); | 583 NotifyObservers(); |
| 620 return true; | 584 return true; |
| 621 } | 585 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 base::Time created_before) { | 655 base::Time created_before) { |
| 692 GURL o(origin.GetOrigin()); | 656 GURL o(origin.GetOrigin()); |
| 693 bool should_notify = false; | 657 bool should_notify = false; |
| 694 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); | 658 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); |
| 695 for (size_t i = 0; i < template_urls_.size();) { | 659 for (size_t i = 0; i < template_urls_.size();) { |
| 696 if (template_urls_[i]->date_created() >= created_after && | 660 if (template_urls_[i]->date_created() >= created_after && |
| 697 (created_before.is_null() || | 661 (created_before.is_null() || |
| 698 template_urls_[i]->date_created() < created_before) && | 662 template_urls_[i]->date_created() < created_before) && |
| 699 CanReplace(template_urls_[i]) && | 663 CanReplace(template_urls_[i]) && |
| 700 (o.is_empty() || | 664 (o.is_empty() || |
| 701 GenerateSearchURL(template_urls_[i], | 665 template_urls_[i]->GenerateSearchURL( |
| 702 search_terms_data()).GetOrigin() == o)) { | 666 search_terms_data()).GetOrigin() == o)) { |
| 703 RemoveNoNotify(template_urls_[i]); | 667 RemoveNoNotify(template_urls_[i]); |
| 704 should_notify = true; | 668 should_notify = true; |
| 705 } else { | 669 } else { |
| 706 ++i; | 670 ++i; |
| 707 } | 671 } |
| 708 } | 672 } |
| 709 if (should_notify) | 673 if (should_notify) |
| 710 NotifyObservers(); | 674 NotifyObservers(); |
| 711 } | 675 } |
| 712 | 676 |
| (...skipping 1554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2267 if (!force) { | 2231 if (!force) { |
| 2268 // Already unique. | 2232 // Already unique. |
| 2269 if (!GetTemplateURLForKeyword(turl.keyword())) | 2233 if (!GetTemplateURLForKeyword(turl.keyword())) |
| 2270 return turl.keyword(); | 2234 return turl.keyword(); |
| 2271 | 2235 |
| 2272 // First, try to return the generated keyword for the TemplateURL (except | 2236 // First, try to return the generated keyword for the TemplateURL (except |
| 2273 // for extensions, as their keywords are not associated with their URLs). | 2237 // for extensions, as their keywords are not associated with their URLs). |
| 2274 GURL gurl(turl.url()); | 2238 GURL gurl(turl.url()); |
| 2275 if (gurl.is_valid() && | 2239 if (gurl.is_valid() && |
| 2276 (turl.GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { | 2240 (turl.GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { |
| 2277 base::string16 keyword_candidate = GenerateKeyword(gurl); | 2241 base::string16 keyword_candidate = TemplateURL::GenerateKeyword(gurl); |
| 2278 if (!GetTemplateURLForKeyword(keyword_candidate)) | 2242 if (!GetTemplateURLForKeyword(keyword_candidate)) |
| 2279 return keyword_candidate; | 2243 return keyword_candidate; |
| 2280 } | 2244 } |
| 2281 } | 2245 } |
| 2282 | 2246 |
| 2283 // We try to uniquify the keyword by appending a special character to the end. | 2247 // We try to uniquify the keyword by appending a special character to the end. |
| 2284 // This is a best-effort approach where we try to preserve the original | 2248 // This is a best-effort approach where we try to preserve the original |
| 2285 // keyword and let the user do what they will after our attempt. | 2249 // keyword and let the user do what they will after our attempt. |
| 2286 base::string16 keyword_candidate(turl.keyword()); | 2250 base::string16 keyword_candidate(turl.keyword()); |
| 2287 do { | 2251 do { |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2510 | 2474 |
| 2511 if (most_recently_intalled_default) { | 2475 if (most_recently_intalled_default) { |
| 2512 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2476 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 2513 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | 2477 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); |
| 2514 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | 2478 default_search_manager_.SetExtensionControlledDefaultSearchEngine( |
| 2515 most_recently_intalled_default->data()); | 2479 most_recently_intalled_default->data()); |
| 2516 } else { | 2480 } else { |
| 2517 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | 2481 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); |
| 2518 } | 2482 } |
| 2519 } | 2483 } |
| OLD | NEW |