OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "core/html/DOMFormData.h" | |
33 | |
34 #include "core/fileapi/Blob.h" | |
35 #include "core/fileapi/File.h" | |
36 #include "core/frame/UseCounter.h" | |
37 #include "core/html/HTMLFormElement.h" | |
38 #include "wtf/text/TextEncoding.h" | |
39 #include "wtf/text/WTFString.h" | |
40 | |
41 namespace blink { | |
42 | |
43 namespace { | |
44 | |
45 class DOMFormDataIterationSource final : public PairIterable<String, FormDataEnt
ryValue>::IterationSource { | |
46 public: | |
47 DOMFormDataIterationSource(DOMFormData* formData) : m_formData(formData), m_
current(0) { } | |
48 | |
49 bool next(ScriptState* scriptState, String& key, FormDataEntryValue& value,
ExceptionState& exceptionState) override | |
50 { | |
51 if (m_current >= m_formData->size()) | |
52 return false; | |
53 | |
54 const FormDataList::Item& entry = m_formData->items()[m_current++]; | |
55 key = m_formData->decode(entry.key()); | |
56 if (entry.isString()) { | |
57 value.setUSVString(m_formData->decode(entry.data())); | |
58 } else { | |
59 ASSERT(entry.isFile()); | |
60 value.setFile(entry.file()); | |
61 } | |
62 return true; | |
63 } | |
64 | |
65 DEFINE_INLINE_VIRTUAL_TRACE() | |
66 { | |
67 visitor->trace(m_formData); | |
68 PairIterable<String, FormDataEntryValue>::IterationSource::trace(visitor
); | |
69 } | |
70 | |
71 private: | |
72 const Member<DOMFormData> m_formData; | |
73 size_t m_current; | |
74 }; | |
75 | |
76 } // namespace | |
77 | |
78 DOMFormData::DOMFormData(const WTF::TextEncoding& encoding) | |
79 : FormDataList(encoding) | |
80 , m_opaque(false) | |
81 { | |
82 } | |
83 | |
84 DOMFormData::DOMFormData(HTMLFormElement* form) | |
85 : FormDataList(UTF8Encoding()) | |
86 , m_opaque(false) | |
87 { | |
88 if (!form) | |
89 return; | |
90 | |
91 for (unsigned i = 0; i < form->associatedElements().size(); ++i) { | |
92 FormAssociatedElement* element = form->associatedElements()[i]; | |
93 if (!toHTMLElement(element)->isDisabledFormControl()) | |
94 element->appendFormData(*this, true); | |
95 } | |
96 } | |
97 | |
98 void DOMFormData::append(const String& name, const String& value) | |
99 { | |
100 appendData(name, value); | |
101 } | |
102 | |
103 void DOMFormData::append(ExecutionContext* context, const String& name, Blob* bl
ob, const String& filename) | |
104 { | |
105 if (blob) { | |
106 if (blob->isFile()) { | |
107 if (filename.isNull()) | |
108 UseCounter::count(context, UseCounter::FormDataAppendFile); | |
109 else | |
110 UseCounter::count(context, UseCounter::FormDataAppendFileWithFil
ename); | |
111 } else { | |
112 if (filename.isNull()) | |
113 UseCounter::count(context, UseCounter::FormDataAppendBlob); | |
114 else | |
115 UseCounter::count(context, UseCounter::FormDataAppendBlobWithFil
ename); | |
116 } | |
117 } else { | |
118 UseCounter::count(context, UseCounter::FormDataAppendNull); | |
119 } | |
120 appendBlob(name, blob, filename); | |
121 } | |
122 | |
123 void DOMFormData::deleteEntry(const String& name) | |
124 { | |
125 const CString keyData = encodeAndNormalize(name); | |
126 size_t i = 0; | |
127 while (i < m_items.size()) { | |
128 if (m_items[i].key() == keyData) { | |
129 m_items.remove(i); | |
130 } else { | |
131 ++i; | |
132 } | |
133 } | |
134 } | |
135 | |
136 void DOMFormData::get(const String& name, FormDataEntryValue& result) | |
137 { | |
138 if (m_opaque) | |
139 return; | |
140 const CString encodedName = encodeAndNormalize(name); | |
141 for (const Item& entry : items()) { | |
142 if (entry.key() == encodedName) { | |
143 if (entry.isString()) { | |
144 result.setUSVString(decode(entry.data())); | |
145 } else { | |
146 ASSERT(entry.isFile()); | |
147 result.setFile(entry.file()); | |
148 } | |
149 return; | |
150 } | |
151 } | |
152 } | |
153 | |
154 HeapVector<FormDataEntryValue> DOMFormData::getAll(const String& name) | |
155 { | |
156 HeapVector<FormDataEntryValue> results; | |
157 | |
158 if (m_opaque) | |
159 return results; | |
160 | |
161 const CString encodedName = encodeAndNormalize(name); | |
162 for (const Item& entry : items()) { | |
163 if (entry.key() != encodedName) | |
164 continue; | |
165 FormDataEntryValue value; | |
166 if (entry.isString()) { | |
167 value.setUSVString(decode(entry.data())); | |
168 } else { | |
169 ASSERT(entry.isFile()); | |
170 value.setFile(entry.file()); | |
171 } | |
172 results.append(value); | |
173 } | |
174 return results; | |
175 } | |
176 | |
177 bool DOMFormData::has(const String& name) | |
178 { | |
179 if (m_opaque) | |
180 return false; | |
181 const CString keyData = encodeAndNormalize(name); | |
182 for (const Item& item : items()) { | |
183 if (item.key() == keyData) | |
184 return true; | |
185 } | |
186 return false; | |
187 } | |
188 | |
189 void DOMFormData::set(const String& name, const String& value) | |
190 { | |
191 setEntry(Item(encodeAndNormalize(name), encodeAndNormalize(value))); | |
192 } | |
193 | |
194 void DOMFormData::set(const String& name, Blob* blob, const String& filename) | |
195 { | |
196 setEntry(Item(encodeAndNormalize(name), blob, filename)); | |
197 } | |
198 | |
199 void DOMFormData::setEntry(const Item& item) | |
200 { | |
201 const CString keyData = item.key(); | |
202 bool found = false; | |
203 size_t i = 0; | |
204 while (i < m_items.size()) { | |
205 if (m_items[i].key() != keyData) { | |
206 ++i; | |
207 } else if (found) { | |
208 m_items.remove(i); | |
209 } else { | |
210 found = true; | |
211 m_items[i] = item; | |
212 ++i; | |
213 } | |
214 } | |
215 if (!found) | |
216 m_items.append(item); | |
217 return; | |
218 } | |
219 | |
220 String DOMFormData::decode(const CString& data) const | |
221 { | |
222 return encoding().decode(data.data(), data.length()); | |
223 } | |
224 | |
225 PairIterable<String, FormDataEntryValue>::IterationSource* DOMFormData::startIte
ration(ScriptState*, ExceptionState&) | |
226 { | |
227 if (m_opaque) | |
228 return new DOMFormDataIterationSource(new DOMFormData(nullptr)); | |
229 | |
230 return new DOMFormDataIterationSource(this); | |
231 } | |
232 | |
233 } // namespace blink | |
OLD | NEW |