OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved. | |
3 * Copyright (C) 2009 Google Inc. All rights reserved. | |
4 * Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies) | |
5 * | |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Library General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2 of the License, or (at your option) any later version. | |
10 * | |
11 * This library is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Library General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Library General Public License | |
17 * along with this library; see the file COPYING.LIB. If not, write to | |
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
19 * Boston, MA 02110-1301, USA. | |
20 */ | |
21 | |
22 #include "config.h" | |
23 #include "platform/network/FormData.h" | |
24 | |
25 #include "platform/FileMetadata.h" | |
26 #include "platform/network/FormDataBuilder.h" | |
27 #include "wtf/text/CString.h" | |
28 #include "wtf/text/TextEncoding.h" | |
29 | |
30 namespace blink { | |
31 | |
32 bool FormDataElement::isSafeToSendToAnotherThread() const | |
33 { | |
34 return m_filename.isSafeToSendToAnotherThread() | |
35 && m_blobUUID.isSafeToSendToAnotherThread() | |
36 && m_fileSystemURL.isSafeToSendToAnotherThread(); | |
37 } | |
38 | |
39 inline FormData::FormData() | |
40 : m_identifier(0) | |
41 , m_containsPasswordData(false) | |
42 { | |
43 } | |
44 | |
45 inline FormData::FormData(const FormData& data) | |
46 : RefCounted<FormData>() | |
47 , m_elements(data.m_elements) | |
48 , m_identifier(data.m_identifier) | |
49 , m_containsPasswordData(data.m_containsPasswordData) | |
50 { | |
51 } | |
52 | |
53 FormData::~FormData() | |
54 { | |
55 } | |
56 | |
57 PassRefPtr<FormData> FormData::create() | |
58 { | |
59 return adoptRef(new FormData); | |
60 } | |
61 | |
62 PassRefPtr<FormData> FormData::create(const void* data, size_t size) | |
63 { | |
64 RefPtr<FormData> result = create(); | |
65 result->appendData(data, size); | |
66 return result.release(); | |
67 } | |
68 | |
69 PassRefPtr<FormData> FormData::create(const CString& string) | |
70 { | |
71 RefPtr<FormData> result = create(); | |
72 result->appendData(string.data(), string.length()); | |
73 return result.release(); | |
74 } | |
75 | |
76 PassRefPtr<FormData> FormData::create(const Vector<char>& vector) | |
77 { | |
78 RefPtr<FormData> result = create(); | |
79 result->appendData(vector.data(), vector.size()); | |
80 return result.release(); | |
81 } | |
82 | |
83 PassRefPtr<FormData> FormData::copy() const | |
84 { | |
85 return adoptRef(new FormData(*this)); | |
86 } | |
87 | |
88 PassRefPtr<FormData> FormData::deepCopy() const | |
89 { | |
90 RefPtr<FormData> formData(create()); | |
91 | |
92 formData->m_identifier = m_identifier; | |
93 formData->m_boundary = m_boundary; | |
94 formData->m_containsPasswordData = m_containsPasswordData; | |
95 | |
96 size_t n = m_elements.size(); | |
97 formData->m_elements.reserveInitialCapacity(n); | |
98 for (size_t i = 0; i < n; ++i) { | |
99 const FormDataElement& e = m_elements[i]; | |
100 switch (e.m_type) { | |
101 case FormDataElement::data: | |
102 formData->m_elements.uncheckedAppend(FormDataElement(e.m_data)); | |
103 break; | |
104 case FormDataElement::encodedFile: | |
105 formData->m_elements.uncheckedAppend(FormDataElement(e.m_filename.is
olatedCopy(), e.m_fileStart, e.m_fileLength, e.m_expectedFileModificationTime)); | |
106 break; | |
107 case FormDataElement::encodedBlob: | |
108 formData->m_elements.uncheckedAppend(FormDataElement(e.m_blobUUID.is
olatedCopy(), e.m_optionalBlobDataHandle)); | |
109 break; | |
110 case FormDataElement::encodedFileSystemURL: | |
111 formData->m_elements.uncheckedAppend(FormDataElement(e.m_fileSystemU
RL.copy(), e.m_fileStart, e.m_fileLength, e.m_expectedFileModificationTime)); | |
112 break; | |
113 } | |
114 } | |
115 return formData.release(); | |
116 } | |
117 | |
118 void FormData::appendData(const void* data, size_t size) | |
119 { | |
120 if (m_elements.isEmpty() || m_elements.last().m_type != FormDataElement::dat
a) | |
121 m_elements.append(FormDataElement()); | |
122 FormDataElement& e = m_elements.last(); | |
123 size_t oldSize = e.m_data.size(); | |
124 e.m_data.grow(oldSize + size); | |
125 memcpy(e.m_data.data() + oldSize, data, size); | |
126 } | |
127 | |
128 void FormData::appendFile(const String& filename) | |
129 { | |
130 m_elements.append(FormDataElement(filename, 0, BlobDataItem::toEndOfFile, in
validFileTime())); | |
131 } | |
132 | |
133 void FormData::appendFileRange(const String& filename, long long start, long lon
g length, double expectedModificationTime) | |
134 { | |
135 m_elements.append(FormDataElement(filename, start, length, expectedModificat
ionTime)); | |
136 } | |
137 | |
138 void FormData::appendBlob(const String& uuid, PassRefPtr<BlobDataHandle> optiona
lHandle) | |
139 { | |
140 m_elements.append(FormDataElement(uuid, optionalHandle)); | |
141 } | |
142 | |
143 void FormData::appendFileSystemURL(const KURL& url) | |
144 { | |
145 m_elements.append(FormDataElement(url, 0, BlobDataItem::toEndOfFile, invalid
FileTime())); | |
146 } | |
147 | |
148 void FormData::appendFileSystemURLRange(const KURL& url, long long start, long l
ong length, double expectedModificationTime) | |
149 { | |
150 m_elements.append(FormDataElement(url, start, length, expectedModificationTi
me)); | |
151 } | |
152 | |
153 void FormData::flatten(Vector<char>& data) const | |
154 { | |
155 // Concatenate all the byte arrays, but omit any files. | |
156 data.clear(); | |
157 size_t n = m_elements.size(); | |
158 for (size_t i = 0; i < n; ++i) { | |
159 const FormDataElement& e = m_elements[i]; | |
160 if (e.m_type == FormDataElement::data) | |
161 data.append(e.m_data.data(), static_cast<size_t>(e.m_data.size())); | |
162 } | |
163 } | |
164 | |
165 String FormData::flattenToString() const | |
166 { | |
167 Vector<char> bytes; | |
168 flatten(bytes); | |
169 return Latin1Encoding().decode(reinterpret_cast<const char*>(bytes.data()),
bytes.size()); | |
170 } | |
171 | |
172 unsigned long long FormData::sizeInBytes() const | |
173 { | |
174 unsigned size = 0; | |
175 size_t n = m_elements.size(); | |
176 for (size_t i = 0; i < n; ++i) { | |
177 const FormDataElement& e = m_elements[i]; | |
178 switch (e.m_type) { | |
179 case FormDataElement::data: | |
180 size += e.m_data.size(); | |
181 break; | |
182 case FormDataElement::encodedFile: | |
183 size += e.m_fileLength - e.m_fileStart; | |
184 break; | |
185 case FormDataElement::encodedBlob: | |
186 if (e.m_optionalBlobDataHandle) | |
187 size += e.m_optionalBlobDataHandle->size(); | |
188 break; | |
189 case FormDataElement::encodedFileSystemURL: | |
190 size += e.m_fileLength - e.m_fileStart; | |
191 break; | |
192 } | |
193 } | |
194 return size; | |
195 } | |
196 | |
197 bool FormData::isSafeToSendToAnotherThread() const | |
198 { | |
199 if (!hasOneRef()) | |
200 return false; | |
201 for (auto& element : m_elements) { | |
202 if (!element.isSafeToSendToAnotherThread()) | |
203 return false; | |
204 } | |
205 return true; | |
206 } | |
207 | |
208 } // namespace blink | |
OLD | NEW |