OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/autofill/core/browser/legal_message_line.h" |
| 6 |
| 7 #include "base/i18n/message_formatter.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 |
| 12 namespace autofill { |
| 13 namespace { |
| 14 |
| 15 // Replace "{0}", "{1}", ... in |template_icu| with corresponding strings |
| 16 // from |display_texts|. Sets |out_message| to the resulting string, with |
| 17 // start position of each replacement in |out_offsets|. |
| 18 // Return false on failure. If false is returned then contents of |out_message| |
| 19 // and |out_offsets| are undefined. |
| 20 bool ReplaceTemplatePlaceholders( |
| 21 const base::string16& template_icu, |
| 22 const std::vector<base::string16>& display_texts, |
| 23 base::string16* out_message, |
| 24 std::vector<size_t>* out_offsets) { |
| 25 // Escape "$" -> "$$" for ReplaceStringPlaceholders(). |
| 26 // |
| 27 // Edge cases: |
| 28 // 1. Two or more consecutive $ characters will be incorrectly expanded |
| 29 // ("$$" -> "$$$$", which ReplaceStringPlaceholders() then turns into |
| 30 // "$$$"). |
| 31 // |
| 32 // 2. "${" will cause false to be returned. "${0}" will expand to "$${0}". |
| 33 // FormatWithNumberedArgs() turns it into "$$$1", which |
| 34 // ReplaceStringPlaceholders() then turns into "$$1" without doing the |
| 35 // parameter replacement. This causes false to be returned because each |
| 36 // parameter is not used exactly once. |
| 37 // |
| 38 // Both of these cases are noted in the header file, and are unlikely to |
| 39 // occur in any actual legal message. |
| 40 base::string16 template_icu_escaped; |
| 41 base::ReplaceChars(template_icu, base::ASCIIToUTF16("$"), |
| 42 base::ASCIIToUTF16("$$"), &template_icu_escaped); |
| 43 |
| 44 // Replace "{0}" -> "$1", "{1}" -> "$2", ... to prepare |template_dollars| |
| 45 // for ReplaceStringPlaceholders(). |
| 46 base::string16 template_dollars = |
| 47 base::i18n::MessageFormatter::FormatWithNumberedArgs( |
| 48 template_icu_escaped, "$1", "$2", "$3", "$4", "$5", "$6", "$7"); |
| 49 |
| 50 // FormatWithNumberedArgs() returns an empty string on failure. |
| 51 if (template_dollars.empty() && !template_icu.empty()) |
| 52 return false; |
| 53 |
| 54 // Replace "$1", "$2", ... with the display text of each parameter. |
| 55 *out_message = base::ReplaceStringPlaceholders(template_dollars, |
| 56 display_texts, out_offsets); |
| 57 |
| 58 // Each parameter must be used exactly once. If a parameter is unused or |
| 59 // used more than once then it can't be determined which |offsets| entry |
| 60 // corresponds to which parameter. |
| 61 return out_offsets->size() == display_texts.size(); |
| 62 } |
| 63 |
| 64 } // namespace |
| 65 |
| 66 // static |
| 67 bool LegalMessageLine::Parse(const base::DictionaryValue& legal_message, |
| 68 LegalMessageLines* out) { |
| 69 const base::ListValue* lines = nullptr; |
| 70 if (legal_message.GetList("line", &lines)) { |
| 71 out->resize(lines->GetSize()); |
| 72 for (size_t i = 0; i < lines->GetSize(); ++i) { |
| 73 const base::DictionaryValue* single_line; |
| 74 if (!lines->GetDictionary(i, &single_line) || |
| 75 !(*out)[i].ParseLine(*single_line)) { |
| 76 out->clear(); |
| 77 return false; |
| 78 } |
| 79 } |
| 80 } |
| 81 |
| 82 return true; |
| 83 } |
| 84 |
| 85 LegalMessageLine::LegalMessageLine() {} |
| 86 |
| 87 LegalMessageLine::~LegalMessageLine() {} |
| 88 |
| 89 bool LegalMessageLine::ParseLine(const base::DictionaryValue& line) { |
| 90 // |display_texts| elements are the strings that will be substituted for |
| 91 // "{0}", "{1}", etc. in the template string. |
| 92 std::vector<base::string16> display_texts; |
| 93 |
| 94 // Process all the template parameters. |
| 95 const base::ListValue* template_parameters = nullptr; |
| 96 if (line.GetList("template_parameter", &template_parameters)) { |
| 97 display_texts.resize(template_parameters->GetSize()); |
| 98 links_.resize(template_parameters->GetSize()); |
| 99 |
| 100 for (size_t parameter_index = 0; |
| 101 parameter_index < template_parameters->GetSize(); ++parameter_index) { |
| 102 const base::DictionaryValue* single_parameter; |
| 103 std::string url; |
| 104 if (!template_parameters->GetDictionary(parameter_index, |
| 105 &single_parameter) || |
| 106 !single_parameter->GetString("display_text", |
| 107 &display_texts[parameter_index]) || |
| 108 !single_parameter->GetString("url", &url)) { |
| 109 return false; |
| 110 } |
| 111 links_[parameter_index].url = GURL(url); |
| 112 } |
| 113 } |
| 114 |
| 115 // Read the template string. It's a small subset of the ICU message format |
| 116 // syntax. |
| 117 base::string16 template_icu; |
| 118 if (!line.GetString("template", &template_icu)) |
| 119 return false; |
| 120 |
| 121 // Replace the placeholders in |template_icu| with strings from |
| 122 // |display_texts|, and store the start position of each replacement in |
| 123 // |offsets|. |
| 124 std::vector<size_t> offsets; |
| 125 if (!ReplaceTemplatePlaceholders(template_icu, display_texts, &text_, |
| 126 &offsets)) { |
| 127 return false; |
| 128 } |
| 129 |
| 130 // Fill in range values for all links. |
| 131 for (size_t offset_index = 0; offset_index < offsets.size(); ++offset_index) { |
| 132 size_t range_start = offsets[offset_index]; |
| 133 links_[offset_index].range = gfx::Range( |
| 134 range_start, range_start + display_texts[offset_index].size()); |
| 135 } |
| 136 |
| 137 return true; |
| 138 } |
| 139 |
| 140 } // namespace autofill |
OLD | NEW |