| 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.h" | 5 #include "chrome/browser/search_engines/template_url.h" |
| 6 | 6 |
| 7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
| 8 #include "base/i18n/icu_string_conversions.h" | 8 #include "base/i18n/icu_string_conversions.h" |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/autocomplete/autocomplete_field_trial.h" | 15 #include "chrome/browser/autocomplete/autocomplete_field_trial.h" |
| 16 #include "chrome/browser/google/google_util.h" |
| 16 #include "chrome/browser/search_engines/search_terms_data.h" | 17 #include "chrome/browser/search_engines/search_terms_data.h" |
| 17 #include "chrome/browser/search_engines/template_url_service.h" | 18 #include "chrome/browser/search_engines/template_url_service.h" |
| 18 #include "chrome/common/guid.h" | 19 #include "chrome/common/guid.h" |
| 19 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
| 20 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 22 | 23 |
| 23 // The TemplateURLRef has any number of terms that need to be replaced. Each of | 24 // The TemplateURLRef has any number of terms that need to be replaced. Each of |
| 24 // the terms is enclosed in braces. If the character preceeding the final | 25 // the terms is enclosed in braces. If the character preceeding the final |
| 25 // brace is a ?, it indicates the term is optional and can be replaced with | 26 // brace is a ?, it indicates the term is optional and can be replaced with |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 TemplateURLData::TemplateURLData() | 529 TemplateURLData::TemplateURLData() |
| 529 : show_in_default_list(false), | 530 : show_in_default_list(false), |
| 530 safe_for_autoreplace(false), | 531 safe_for_autoreplace(false), |
| 531 id(0), | 532 id(0), |
| 532 date_created(base::Time::Now()), | 533 date_created(base::Time::Now()), |
| 533 last_modified(base::Time::Now()), | 534 last_modified(base::Time::Now()), |
| 534 created_by_policy(false), | 535 created_by_policy(false), |
| 535 usage_count(0), | 536 usage_count(0), |
| 536 prepopulate_id(0), | 537 prepopulate_id(0), |
| 537 sync_guid(guid::GenerateGUID()), | 538 sync_guid(guid::GenerateGUID()), |
| 538 url_("x"), | 539 keyword_(ASCIIToUTF16("dummy")), |
| 539 autogenerate_keyword_(false), | 540 url_("x") { |
| 540 keyword_generated_(false) { | |
| 541 } | 541 } |
| 542 | 542 |
| 543 TemplateURLData::~TemplateURLData() { | 543 TemplateURLData::~TemplateURLData() { |
| 544 } | 544 } |
| 545 | 545 |
| 546 void TemplateURLData::SetKeyword(const string16& keyword) { | 546 void TemplateURLData::SetKeyword(const string16& keyword) { |
| 547 DCHECK(!keyword.empty()); |
| 548 |
| 547 // Case sensitive keyword matching is confusing. As such, we force all | 549 // Case sensitive keyword matching is confusing. As such, we force all |
| 548 // keywords to be lower case. | 550 // keywords to be lower case. |
| 549 keyword_ = base::i18n::ToLower(keyword); | 551 keyword_ = base::i18n::ToLower(keyword); |
| 550 autogenerate_keyword_ = false; | |
| 551 } | |
| 552 | |
| 553 const string16& TemplateURLData::keyword(TemplateURL* t_url) const { | |
| 554 EnsureKeyword(t_url); | |
| 555 return keyword_; | |
| 556 } | |
| 557 | |
| 558 void TemplateURLData::SetAutogenerateKeyword(bool autogenerate_keyword) { | |
| 559 autogenerate_keyword_ = autogenerate_keyword; | |
| 560 if (autogenerate_keyword_) { | |
| 561 keyword_.clear(); | |
| 562 keyword_generated_ = false; | |
| 563 } | |
| 564 } | |
| 565 | |
| 566 void TemplateURLData::EnsureKeyword(TemplateURL* t_url) const { | |
| 567 if (autogenerate_keyword_ && !keyword_generated_) { | |
| 568 // Generate a keyword and cache it. | |
| 569 keyword_ = base::i18n::ToLower(TemplateURLService::GenerateKeyword( | |
| 570 TemplateURLService::GenerateSearchURL(t_url).GetWithEmptyPath(), true)); | |
| 571 keyword_generated_ = true; | |
| 572 } | |
| 573 } | 552 } |
| 574 | 553 |
| 575 void TemplateURLData::SetURL(const std::string& url) { | 554 void TemplateURLData::SetURL(const std::string& url) { |
| 576 DCHECK(!url.empty()); | 555 DCHECK(!url.empty()); |
| 577 url_ = url; | 556 url_ = url; |
| 578 } | 557 } |
| 579 | 558 |
| 580 | 559 |
| 581 // TemplateURL ---------------------------------------------------------------- | 560 // TemplateURL ---------------------------------------------------------------- |
| 582 | 561 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 bool TemplateURL::SupportsReplacement() const { | 626 bool TemplateURL::SupportsReplacement() const { |
| 648 UIThreadSearchTermsData search_terms_data; | 627 UIThreadSearchTermsData search_terms_data; |
| 649 return SupportsReplacementUsingTermsData(search_terms_data); | 628 return SupportsReplacementUsingTermsData(search_terms_data); |
| 650 } | 629 } |
| 651 | 630 |
| 652 bool TemplateURL::SupportsReplacementUsingTermsData( | 631 bool TemplateURL::SupportsReplacementUsingTermsData( |
| 653 const SearchTermsData& search_terms_data) const { | 632 const SearchTermsData& search_terms_data) const { |
| 654 return url_ref_.SupportsReplacementUsingTermsData(search_terms_data); | 633 return url_ref_.SupportsReplacementUsingTermsData(search_terms_data); |
| 655 } | 634 } |
| 656 | 635 |
| 636 bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword() const { |
| 637 return !IsExtensionKeyword() && url_ref_.HasGoogleBaseURLs() && |
| 638 google_util::IsGoogleHostname(UTF16ToUTF8(data_.keyword()), |
| 639 google_util::DISALLOW_SUBDOMAIN); |
| 640 } |
| 641 |
| 642 bool TemplateURL::HasSameKeywordAs(const TemplateURL& other) const { |
| 643 return (data_.keyword() == other.data_.keyword()) || |
| 644 (IsGoogleSearchURLWithReplaceableKeyword() && |
| 645 other.IsGoogleSearchURLWithReplaceableKeyword()); |
| 646 } |
| 647 |
| 657 std::string TemplateURL::GetExtensionId() const { | 648 std::string TemplateURL::GetExtensionId() const { |
| 658 DCHECK(IsExtensionKeyword()); | 649 DCHECK(IsExtensionKeyword()); |
| 659 return GURL(data_.url()).host(); | 650 return GURL(data_.url()).host(); |
| 660 } | 651 } |
| 661 | 652 |
| 662 bool TemplateURL::IsExtensionKeyword() const { | 653 bool TemplateURL::IsExtensionKeyword() const { |
| 663 return GURL(data_.url()).SchemeIs(chrome::kExtensionScheme); | 654 return GURL(data_.url()).SchemeIs(chrome::kExtensionScheme); |
| 664 } | 655 } |
| 665 | 656 |
| 666 void TemplateURL::SetURL(const std::string& url) { | 657 void TemplateURL::SetURL(const std::string& url) { |
| 667 data_.SetURL(url); | 658 data_.SetURL(url); |
| 668 url_ref_.InvalidateCachedValues(); | 659 url_ref_.InvalidateCachedValues(); |
| 669 } | 660 } |
| 670 | 661 |
| 671 void TemplateURL::SetPrepopulateId(int id) { | 662 void TemplateURL::SetPrepopulateId(int id) { |
| 672 data_.prepopulate_id = id; | 663 data_.prepopulate_id = id; |
| 673 const bool prepopulated = id > 0; | 664 const bool prepopulated = id > 0; |
| 674 url_ref_.prepopulated_ = prepopulated; | 665 url_ref_.prepopulated_ = prepopulated; |
| 675 suggestions_url_ref_.prepopulated_ = prepopulated; | 666 suggestions_url_ref_.prepopulated_ = prepopulated; |
| 676 instant_url_ref_.prepopulated_ = prepopulated; | 667 instant_url_ref_.prepopulated_ = prepopulated; |
| 677 } | 668 } |
| 678 | 669 |
| 670 void TemplateURL::ResetKeywordIfNecessary(bool force) { |
| 671 if (IsGoogleSearchURLWithReplaceableKeyword() || force) { |
| 672 DCHECK(!IsExtensionKeyword()); |
| 673 GURL url(TemplateURLService::GenerateSearchURL(this)); |
| 674 if (url.is_valid()) |
| 675 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); |
| 676 } |
| 677 } |
| 678 |
| 679 void TemplateURL::InvalidateCachedValues() { | 679 void TemplateURL::InvalidateCachedValues() { |
| 680 url_ref_.InvalidateCachedValues(); | 680 url_ref_.InvalidateCachedValues(); |
| 681 suggestions_url_ref_.InvalidateCachedValues(); | 681 suggestions_url_ref_.InvalidateCachedValues(); |
| 682 instant_url_ref_.InvalidateCachedValues(); | 682 instant_url_ref_.InvalidateCachedValues(); |
| 683 data_.SetAutogenerateKeyword(data_.autogenerate_keyword()); | 683 ResetKeywordIfNecessary(false); |
| 684 } | 684 } |
| OLD | NEW |