OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/template_url.h" | 5 #include "components/search_engines/template_url.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 // boundary delimiter line. Uses random number generator here to create | 316 // boundary delimiter line. Uses random number generator here to create |
317 // a unique boundary delimiter for form data encoding. | 317 // a unique boundary delimiter for form data encoding. |
318 std::string boundary = base::StringPrintf(kMultipartBoundary, | 318 std::string boundary = base::StringPrintf(kMultipartBoundary, |
319 base::RandUint64()); | 319 base::RandUint64()); |
320 // Sets the content MIME type. | 320 // Sets the content MIME type. |
321 post_content->first = kUploadDataMIMEType; | 321 post_content->first = kUploadDataMIMEType; |
322 post_content->first += boundary; | 322 post_content->first += boundary; |
323 // Encodes the post parameters. | 323 // Encodes the post parameters. |
324 std::string* post_data = &post_content->second; | 324 std::string* post_data = &post_content->second; |
325 post_data->clear(); | 325 post_data->clear(); |
326 for (PostParams::const_iterator param = post_params.begin(); | 326 for (const auto& param : post_params) { |
327 param != post_params.end(); ++param) { | 327 DCHECK(!param.name.empty()); |
328 DCHECK(!param->first.empty()); | 328 net::AddMultipartValueForUpload(param.name, param.value, boundary, |
329 net::AddMultipartValueForUpload(param->first, param->second, boundary, | 329 param.content_type, post_data); |
330 std::string(), post_data); | |
331 } | 330 } |
332 net::AddMultipartFinalDelimiterForUpload(boundary, post_data); | 331 net::AddMultipartFinalDelimiterForUpload(boundary, post_data); |
333 return true; | 332 return true; |
334 } | 333 } |
335 | 334 |
336 bool TemplateURLRef::SupportsReplacement( | 335 bool TemplateURLRef::SupportsReplacement( |
337 const SearchTermsData& search_terms_data) const { | 336 const SearchTermsData& search_terms_data) const { |
338 ParseIfNecessary(search_terms_data); | 337 ParseIfNecessary(search_terms_data); |
339 return valid_ && supports_replacements_; | 338 return valid_ && supports_replacements_; |
340 } | 339 } |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 Strings parts; | 706 Strings parts; |
708 // The '=' delimiter is required and the name must be not empty. | 707 // The '=' delimiter is required and the name must be not empty. |
709 base::SplitString(*iterator, '=', &parts); | 708 base::SplitString(*iterator, '=', &parts); |
710 if ((parts.size() != 2U) || parts[0].empty()) | 709 if ((parts.size() != 2U) || parts[0].empty()) |
711 return std::string(); | 710 return std::string(); |
712 | 711 |
713 std::string& value = parts[1]; | 712 std::string& value = parts[1]; |
714 size_t replacements_size = replacements->size(); | 713 size_t replacements_size = replacements->size(); |
715 if (IsTemplateParameterString(value)) | 714 if (IsTemplateParameterString(value)) |
716 ParseParameter(0, value.length() - 1, &value, replacements); | 715 ParseParameter(0, value.length() - 1, &value, replacements); |
717 post_params->push_back(std::make_pair(parts[0], value)); | 716 PostParam param = { parts[0], value }; |
| 717 post_params->push_back(param); |
718 // If there was a replacement added, points its index to last added | 718 // If there was a replacement added, points its index to last added |
719 // PostParam. | 719 // PostParam. |
720 if (replacements->size() > replacements_size) { | 720 if (replacements->size() > replacements_size) { |
721 DCHECK_EQ(replacements_size + 1, replacements->size()); | 721 DCHECK_EQ(replacements_size + 1, replacements->size()); |
722 Replacement* r = &replacements->back(); | 722 Replacement* r = &replacements->back(); |
723 r->is_post_param = true; | 723 r->is_post_param = true; |
724 r->index = post_params->size() - 1; | 724 r->index = post_params->size() - 1; |
725 } | 725 } |
726 } | 726 } |
727 DCHECK(!post_params->empty()); | 727 DCHECK(!post_params->empty()); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
790 path_ = url.path(); | 790 path_ = url.path(); |
791 } | 791 } |
792 | 792 |
793 void TemplateURLRef::HandleReplacement(const std::string& name, | 793 void TemplateURLRef::HandleReplacement(const std::string& name, |
794 const std::string& value, | 794 const std::string& value, |
795 const Replacement& replacement, | 795 const Replacement& replacement, |
796 std::string* url) const { | 796 std::string* url) const { |
797 size_t pos = replacement.index; | 797 size_t pos = replacement.index; |
798 if (replacement.is_post_param) { | 798 if (replacement.is_post_param) { |
799 DCHECK_LT(pos, post_params_.size()); | 799 DCHECK_LT(pos, post_params_.size()); |
800 DCHECK(!post_params_[pos].first.empty()); | 800 DCHECK(!post_params_[pos].name.empty()); |
801 post_params_[pos].second = value; | 801 post_params_[pos].value = value; |
802 } else { | 802 } else { |
803 url->insert(pos, name.empty() ? value : (name + "=" + value + "&")); | 803 url->insert(pos, name.empty() ? value : (name + "=" + value + "&")); |
804 } | 804 } |
805 } | 805 } |
806 | 806 |
807 std::string TemplateURLRef::HandleReplacements( | 807 std::string TemplateURLRef::HandleReplacements( |
808 const SearchTermsArgs& search_terms_args, | 808 const SearchTermsArgs& search_terms_args, |
809 const SearchTermsData& search_terms_data, | 809 const SearchTermsData& search_terms_data, |
810 PostContent* post_content) const { | 810 PostContent* post_content) const { |
811 if (replacements_.empty()) { | 811 if (replacements_.empty()) { |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1090 break; | 1090 break; |
1091 | 1091 |
1092 case SEARCH_TERMS: | 1092 case SEARCH_TERMS: |
1093 HandleReplacement( | 1093 HandleReplacement( |
1094 std::string(), base::UTF16ToUTF8(encoded_terms), *i, &url); | 1094 std::string(), base::UTF16ToUTF8(encoded_terms), *i, &url); |
1095 break; | 1095 break; |
1096 | 1096 |
1097 case GOOGLE_IMAGE_THUMBNAIL: | 1097 case GOOGLE_IMAGE_THUMBNAIL: |
1098 HandleReplacement( | 1098 HandleReplacement( |
1099 std::string(), search_terms_args.image_thumbnail_content, *i, &url); | 1099 std::string(), search_terms_args.image_thumbnail_content, *i, &url); |
| 1100 post_params_[i->index].content_type = "image/jpeg"; |
1100 break; | 1101 break; |
1101 | 1102 |
1102 case GOOGLE_IMAGE_URL: | 1103 case GOOGLE_IMAGE_URL: |
1103 if (search_terms_args.image_url.is_valid()) { | 1104 if (search_terms_args.image_url.is_valid()) { |
1104 HandleReplacement( | 1105 HandleReplacement( |
1105 std::string(), search_terms_args.image_url.spec(), *i, &url); | 1106 std::string(), search_terms_args.image_url.spec(), *i, &url); |
1106 } | 1107 } |
1107 break; | 1108 break; |
1108 | 1109 |
1109 case GOOGLE_IMAGE_ORIGINAL_WIDTH: | 1110 case GOOGLE_IMAGE_ORIGINAL_WIDTH: |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1465 // patterns. This means that given patterns | 1466 // patterns. This means that given patterns |
1466 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 1467 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
1467 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 1468 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
1468 // return false. This is important for at least Google, where such URLs | 1469 // return false. This is important for at least Google, where such URLs |
1469 // are invalid. | 1470 // are invalid. |
1470 return !search_terms->empty(); | 1471 return !search_terms->empty(); |
1471 } | 1472 } |
1472 } | 1473 } |
1473 return false; | 1474 return false; |
1474 } | 1475 } |
OLD | NEW |