| 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/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" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 static const char kGoogleAcceptedSuggestionParameter[] = | 45 static const char kGoogleAcceptedSuggestionParameter[] = |
| 46 "google:acceptedSuggestion"; | 46 "google:acceptedSuggestion"; |
| 47 // Host/Domain Google searches are relative to. | 47 // Host/Domain Google searches are relative to. |
| 48 static const char kGoogleBaseURLParameter[] = "google:baseURL"; | 48 static const char kGoogleBaseURLParameter[] = "google:baseURL"; |
| 49 static const char kGoogleBaseURLParameterFull[] = "{google:baseURL}"; | 49 static const char kGoogleBaseURLParameterFull[] = "{google:baseURL}"; |
| 50 // Like google:baseURL, but for the Search Suggest capability. | 50 // Like google:baseURL, but for the Search Suggest capability. |
| 51 static const char kGoogleBaseSuggestURLParameter[] = | 51 static const char kGoogleBaseSuggestURLParameter[] = |
| 52 "google:baseSuggestURL"; | 52 "google:baseSuggestURL"; |
| 53 static const char kGoogleBaseSuggestURLParameterFull[] = | 53 static const char kGoogleBaseSuggestURLParameterFull[] = |
| 54 "{google:baseSuggestURL}"; | 54 "{google:baseSuggestURL}"; |
| 55 static const char kGoogleInstantFieldTrialGroupParameter[] = |
| 56 "google:instantFieldTrialGroupParameter"; |
| 55 static const char kGoogleOriginalQueryForSuggestionParameter[] = | 57 static const char kGoogleOriginalQueryForSuggestionParameter[] = |
| 56 "google:originalQueryForSuggestion"; | 58 "google:originalQueryForSuggestion"; |
| 57 static const char kGoogleRLZParameter[] = "google:RLZ"; | 59 static const char kGoogleRLZParameter[] = "google:RLZ"; |
| 58 // Same as kSearchTermsParameter, with no escaping. | 60 // Same as kSearchTermsParameter, with no escaping. |
| 59 static const char kGoogleSearchFieldtrialParameter[] = | 61 static const char kGoogleSearchFieldtrialParameter[] = |
| 60 "google:searchFieldtrialParameter"; | 62 "google:searchFieldtrialParameter"; |
| 61 static const char kGoogleUnescapedSearchTermsParameter[] = | 63 static const char kGoogleUnescapedSearchTermsParameter[] = |
| 62 "google:unescapedSearchTerms"; | 64 "google:unescapedSearchTerms"; |
| 63 static const char kGoogleUnescapedSearchTermsParameterFull[] = | 65 static const char kGoogleUnescapedSearchTermsParameterFull[] = |
| 64 "{google:unescapedSearchTerms}"; | 66 "{google:unescapedSearchTerms}"; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 replacements->push_back(Replacement(ENCODING, start)); | 141 replacements->push_back(Replacement(ENCODING, start)); |
| 140 } else if (parameter == kOutputEncodingParameter) { | 142 } else if (parameter == kOutputEncodingParameter) { |
| 141 if (!optional) | 143 if (!optional) |
| 142 url->insert(start, kOutputEncodingType); | 144 url->insert(start, kOutputEncodingType); |
| 143 } else if (parameter == kGoogleAcceptedSuggestionParameter) { | 145 } else if (parameter == kGoogleAcceptedSuggestionParameter) { |
| 144 replacements->push_back(Replacement(GOOGLE_ACCEPTED_SUGGESTION, start)); | 146 replacements->push_back(Replacement(GOOGLE_ACCEPTED_SUGGESTION, start)); |
| 145 } else if (parameter == kGoogleBaseURLParameter) { | 147 } else if (parameter == kGoogleBaseURLParameter) { |
| 146 replacements->push_back(Replacement(GOOGLE_BASE_URL, start)); | 148 replacements->push_back(Replacement(GOOGLE_BASE_URL, start)); |
| 147 } else if (parameter == kGoogleBaseSuggestURLParameter) { | 149 } else if (parameter == kGoogleBaseSuggestURLParameter) { |
| 148 replacements->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL, start)); | 150 replacements->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL, start)); |
| 151 } else if (parameter == kGoogleInstantFieldTrialGroupParameter) { |
| 152 replacements->push_back(Replacement(GOOGLE_INSTANT_FIELD_TRIAL_GROUP, |
| 153 start)); |
| 149 } else if (parameter == kGoogleOriginalQueryForSuggestionParameter) { | 154 } else if (parameter == kGoogleOriginalQueryForSuggestionParameter) { |
| 150 replacements->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION, | 155 replacements->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION, |
| 151 start)); | 156 start)); |
| 152 } else if (parameter == kGoogleRLZParameter) { | 157 } else if (parameter == kGoogleRLZParameter) { |
| 153 replacements->push_back(Replacement(GOOGLE_RLZ, start)); | 158 replacements->push_back(Replacement(GOOGLE_RLZ, start)); |
| 154 } else if (parameter == kGoogleSearchFieldtrialParameter) { | 159 } else if (parameter == kGoogleSearchFieldtrialParameter) { |
| 155 replacements->push_back(Replacement(GOOGLE_SEARCH_FIELDTRIAL_GROUP, start)); | 160 replacements->push_back(Replacement(GOOGLE_SEARCH_FIELDTRIAL_GROUP, start)); |
| 156 } else if (parameter == kGoogleUnescapedSearchTermsParameter) { | 161 } else if (parameter == kGoogleUnescapedSearchTermsParameter) { |
| 157 replacements->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS, start)); | 162 replacements->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS, start)); |
| 158 } else { | 163 } else { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 // Open brace without a closing brace, return. | 199 // Open brace without a closing brace, return. |
| 195 return std::string(); | 200 return std::string(); |
| 196 } | 201 } |
| 197 } | 202 } |
| 198 } | 203 } |
| 199 *valid = true; | 204 *valid = true; |
| 200 return parsed_url; | 205 return parsed_url; |
| 201 } | 206 } |
| 202 | 207 |
| 203 void TemplateURLRef::ParseIfNecessary() const { | 208 void TemplateURLRef::ParseIfNecessary() const { |
| 204 UIThreadSearchTermsData search_terms_data; | 209 UIThreadSearchTermsData search_terms_data(NULL); |
| 205 ParseIfNecessaryUsingTermsData(search_terms_data); | 210 ParseIfNecessaryUsingTermsData(search_terms_data); |
| 206 } | 211 } |
| 207 | 212 |
| 208 void TemplateURLRef::ParseIfNecessaryUsingTermsData( | 213 void TemplateURLRef::ParseIfNecessaryUsingTermsData( |
| 209 const SearchTermsData& search_terms_data) const { | 214 const SearchTermsData& search_terms_data) const { |
| 210 if (!parsed_) { | 215 if (!parsed_) { |
| 211 parsed_ = true; | 216 parsed_ = true; |
| 212 parsed_url_ = ParseURL(url_, &replacements_, &valid_); | 217 parsed_url_ = ParseURL(url_, &replacements_, &valid_); |
| 213 supports_replacements_ = false; | 218 supports_replacements_ = false; |
| 214 if (valid_) { | 219 if (valid_) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 274 } |
| 270 } | 275 } |
| 271 } | 276 } |
| 272 | 277 |
| 273 // static | 278 // static |
| 274 void TemplateURLRef::SetGoogleBaseURL(std::string* google_base_url) { | 279 void TemplateURLRef::SetGoogleBaseURL(std::string* google_base_url) { |
| 275 UIThreadSearchTermsData::SetGoogleBaseURL(google_base_url); | 280 UIThreadSearchTermsData::SetGoogleBaseURL(google_base_url); |
| 276 } | 281 } |
| 277 | 282 |
| 278 std::string TemplateURLRef::ReplaceSearchTerms( | 283 std::string TemplateURLRef::ReplaceSearchTerms( |
| 284 Profile* profile, |
| 279 const TemplateURL& host, | 285 const TemplateURL& host, |
| 280 const string16& terms, | 286 const string16& terms, |
| 281 int accepted_suggestion, | 287 int accepted_suggestion, |
| 282 const string16& original_query_for_suggestion) const { | 288 const string16& original_query_for_suggestion) const { |
| 283 UIThreadSearchTermsData search_terms_data; | 289 UIThreadSearchTermsData search_terms_data(profile); |
| 284 return ReplaceSearchTermsUsingTermsData(host, | 290 return ReplaceSearchTermsUsingTermsData(host, |
| 285 terms, | 291 terms, |
| 286 accepted_suggestion, | 292 accepted_suggestion, |
| 287 original_query_for_suggestion, | 293 original_query_for_suggestion, |
| 288 search_terms_data); | 294 search_terms_data); |
| 289 } | 295 } |
| 290 | 296 |
| 291 std::string TemplateURLRef::ReplaceSearchTermsUsingTermsData( | 297 std::string TemplateURLRef::ReplaceSearchTermsUsingTermsData( |
| 292 const TemplateURL& host, | 298 const TemplateURL& host, |
| 293 const string16& terms, | 299 const string16& terms, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 break; | 374 break; |
| 369 | 375 |
| 370 case GOOGLE_BASE_URL: | 376 case GOOGLE_BASE_URL: |
| 371 url.insert(i->index, search_terms_data.GoogleBaseURLValue()); | 377 url.insert(i->index, search_terms_data.GoogleBaseURLValue()); |
| 372 break; | 378 break; |
| 373 | 379 |
| 374 case GOOGLE_BASE_SUGGEST_URL: | 380 case GOOGLE_BASE_SUGGEST_URL: |
| 375 url.insert(i->index, search_terms_data.GoogleBaseSuggestURLValue()); | 381 url.insert(i->index, search_terms_data.GoogleBaseSuggestURLValue()); |
| 376 break; | 382 break; |
| 377 | 383 |
| 384 case GOOGLE_INSTANT_FIELD_TRIAL_GROUP: |
| 385 url.insert(i->index, search_terms_data.InstantFieldTrialUrlParam()); |
| 386 break; |
| 387 |
| 378 case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION: | 388 case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION: |
| 379 if (accepted_suggestion >= 0) | 389 if (accepted_suggestion >= 0) |
| 380 url.insert(i->index, "oq=" + UTF16ToUTF8(encoded_original_query) + | 390 url.insert(i->index, "oq=" + UTF16ToUTF8(encoded_original_query) + |
| 381 "&"); | 391 "&"); |
| 382 break; | 392 break; |
| 383 | 393 |
| 384 case GOOGLE_RLZ: { | 394 case GOOGLE_RLZ: { |
| 385 // On platforms that don't have RLZ, we still want this branch | 395 // On platforms that don't have RLZ, we still want this branch |
| 386 // to happen so that we replace the RLZ template with the | 396 // to happen so that we replace the RLZ template with the |
| 387 // empty string. (If we don't handle this case, we hit a | 397 // empty string. (If we don't handle this case, we hit a |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 default: | 438 default: |
| 429 NOTREACHED(); | 439 NOTREACHED(); |
| 430 break; | 440 break; |
| 431 } | 441 } |
| 432 } | 442 } |
| 433 | 443 |
| 434 return url; | 444 return url; |
| 435 } | 445 } |
| 436 | 446 |
| 437 bool TemplateURLRef::SupportsReplacement() const { | 447 bool TemplateURLRef::SupportsReplacement() const { |
| 438 UIThreadSearchTermsData search_terms_data; | 448 UIThreadSearchTermsData search_terms_data(NULL); |
| 439 return SupportsReplacementUsingTermsData(search_terms_data); | 449 return SupportsReplacementUsingTermsData(search_terms_data); |
| 440 } | 450 } |
| 441 | 451 |
| 442 bool TemplateURLRef::SupportsReplacementUsingTermsData( | 452 bool TemplateURLRef::SupportsReplacementUsingTermsData( |
| 443 const SearchTermsData& search_terms_data) const { | 453 const SearchTermsData& search_terms_data) const { |
| 444 ParseIfNecessaryUsingTermsData(search_terms_data); | 454 ParseIfNecessaryUsingTermsData(search_terms_data); |
| 445 return valid_ && supports_replacements_; | 455 return valid_ && supports_replacements_; |
| 446 } | 456 } |
| 447 | 457 |
| 448 bool TemplateURLRef::IsValid() const { | 458 bool TemplateURLRef::IsValid() const { |
| 449 UIThreadSearchTermsData search_terms_data; | 459 UIThreadSearchTermsData search_terms_data(NULL); |
| 450 return IsValidUsingTermsData(search_terms_data); | 460 return IsValidUsingTermsData(search_terms_data); |
| 451 } | 461 } |
| 452 | 462 |
| 453 bool TemplateURLRef::IsValidUsingTermsData( | 463 bool TemplateURLRef::IsValidUsingTermsData( |
| 454 const SearchTermsData& search_terms_data) const { | 464 const SearchTermsData& search_terms_data) const { |
| 455 ParseIfNecessaryUsingTermsData(search_terms_data); | 465 ParseIfNecessaryUsingTermsData(search_terms_data); |
| 456 return valid_; | 466 return valid_; |
| 457 } | 467 } |
| 458 | 468 |
| 459 string16 TemplateURLRef::DisplayURL() const { | 469 string16 TemplateURLRef::DisplayURL() const { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 rep.SetPath(favicon_path, url_parse::Component(0, favicon_path_len)); | 598 rep.SetPath(favicon_path, url_parse::Component(0, favicon_path_len)); |
| 589 rep.ClearUsername(); | 599 rep.ClearUsername(); |
| 590 rep.ClearPassword(); | 600 rep.ClearPassword(); |
| 591 rep.ClearQuery(); | 601 rep.ClearQuery(); |
| 592 rep.ClearRef(); | 602 rep.ClearRef(); |
| 593 return url.ReplaceComponents(rep); | 603 return url.ReplaceComponents(rep); |
| 594 } | 604 } |
| 595 | 605 |
| 596 // static | 606 // static |
| 597 bool TemplateURL::SupportsReplacement(const TemplateURL* turl) { | 607 bool TemplateURL::SupportsReplacement(const TemplateURL* turl) { |
| 598 UIThreadSearchTermsData search_terms_data; | 608 UIThreadSearchTermsData search_terms_data(NULL); |
| 599 return SupportsReplacementUsingTermsData(turl, search_terms_data); | 609 return SupportsReplacementUsingTermsData(turl, search_terms_data); |
| 600 } | 610 } |
| 601 | 611 |
| 602 // static | 612 // static |
| 603 bool TemplateURL::SupportsReplacementUsingTermsData( | 613 bool TemplateURL::SupportsReplacementUsingTermsData( |
| 604 const TemplateURL* turl, | 614 const TemplateURL* turl, |
| 605 const SearchTermsData& search_terms_data) { | 615 const SearchTermsData& search_terms_data) { |
| 606 return turl && turl->url() && | 616 return turl && turl->url() && |
| 607 turl->url()->SupportsReplacementUsingTermsData(search_terms_data); | 617 turl->url()->SupportsReplacementUsingTermsData(search_terms_data); |
| 608 } | 618 } |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 } | 739 } |
| 730 | 740 |
| 731 std::string TemplateURL::GetExtensionId() const { | 741 std::string TemplateURL::GetExtensionId() const { |
| 732 DCHECK(IsExtensionKeyword()); | 742 DCHECK(IsExtensionKeyword()); |
| 733 return GURL(url_.url()).host(); | 743 return GURL(url_.url()).host(); |
| 734 } | 744 } |
| 735 | 745 |
| 736 bool TemplateURL::IsExtensionKeyword() const { | 746 bool TemplateURL::IsExtensionKeyword() const { |
| 737 return GURL(url_.url()).SchemeIs(chrome::kExtensionScheme); | 747 return GURL(url_.url()).SchemeIs(chrome::kExtensionScheme); |
| 738 } | 748 } |
| OLD | NEW |