1 /*
2 * Copyright (C) 2009 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 "public/platform/WebHTTPBody.h"
33
34 #include "platform/FileMetadata.h"
35 #include "platform/network/FormData.h"
36
37 namespace blink {
38
39 class WebHTTPBodyPrivate : public FormData {
40 };
41
initialize()42 void WebHTTPBody::initialize()
43 {
44 assign(static_cast<WebHTTPBodyPrivate*>(FormData::create().leakRef()));
45 }
46
reset()47 void WebHTTPBody::reset()
48 {
49 assign(0);
50 }
51
assign(const WebHTTPBody & other)52 void WebHTTPBody::assign(const WebHTTPBody& other)
53 {
54 WebHTTPBodyPrivate* p = const_cast<WebHTTPBodyPrivate*>(other.m_private);
55 if (p)
56 p->ref();
57 assign(p);
58 }
59
elementCount() const60 size_t WebHTTPBody::elementCount() const
61 {
62 ASSERT(!isNull());
63 return m_private->elements().size();
64 }
65
elementAt(size_t index,Element & result) const66 bool WebHTTPBody::elementAt(size_t index, Element& result) const
67 {
68 ASSERT(!isNull());
69
70 if (index >= m_private->elements().size())
71 return false;
72
73 const FormDataElement& element = m_private->elements()[index];
74
75 result.data.reset();
76 result.filePath.reset();
77 result.fileStart = 0;
78 result.fileLength = 0;
79 result.modificationTime = invalidFileTime();
80 result.blobUUID.reset();
81
82 switch (element.m_type) {
83 case FormDataElement::data:
84 result.type = Element::TypeData;
85 result.data.assign(element.m_data.data(), element.m_data.size());
86 break;
87 case FormDataElement::encodedFile:
88 result.type = Element::TypeFile;
89 result.filePath = element.m_filename;
90 result.fileStart = element.m_fileStart;
91 result.fileLength = element.m_fileLength;
92 result.modificationTime = element.m_expectedFileModificationTime;
93 break;
94 case FormDataElement::encodedBlob:
95 result.type = Element::TypeBlob;
96 result.blobUUID = element.m_blobUUID;
97 break;
98 case FormDataElement::encodedFileSystemURL:
99 result.type = Element::TypeFileSystemURL;
100 result.fileSystemURL = element.m_fileSystemURL;
101 result.fileStart = element.m_fileStart;
102 result.fileLength = element.m_fileLength;
103 result.modificationTime = element.m_expectedFileModificationTime;
104 break;
105 default:
106 ASSERT_NOT_REACHED();
107 return false;
108 }
109
110 return true;
111 }
112
appendData(const WebData & data)113 void WebHTTPBody::appendData(const WebData& data)
114 {
115 ensureMutable();
116 // FIXME: FormDataElement::m_data should be a SharedBuffer<char>. Then we
117 // could avoid this buffer copy.
118 m_private->appendData(data.data(), data.size());
119 }
120
appendFile(const WebString & filePath)121 void WebHTTPBody::appendFile(const WebString& filePath)
122 {
123 ensureMutable();
124 m_private->appendFile(filePath);
125 }
126
appendFileRange(const WebString & filePath,long long fileStart,long long fileLength,double modificationTime)127 void WebHTTPBody::appendFileRange(const WebString& filePath, long long fileStart, long long fileLength, double modificationTime)
128 {
129 ensureMutable();
130 m_private->appendFileRange(filePath, fileStart, fileLength, modificationTime);
131 }
132
appendFileSystemURLRange(const WebURL & url,long long start,long long length,double modificationTime)133 void WebHTTPBody::appendFileSystemURLRange(const WebURL& url, long long start, long long length, double modificationTime)
134 {
135 // Currently we only support filesystem URL.
136 ASSERT(KURL(url).protocolIs("filesystem"));
137 ensureMutable();
138 m_private->appendFileSystemURLRange(url, start, length, modificationTime);
139 }
140
appendBlob(const WebString & uuid)141 void WebHTTPBody::appendBlob(const WebString& uuid)
142 {
143 ensureMutable();
144 m_private->appendBlob(uuid, nullptr);
145 }
146
identifier() const147 long long WebHTTPBody::identifier() const
148 {
149 ASSERT(!isNull());
150 return m_private->identifier();
151 }
152
setIdentifier(long long identifier)153 void WebHTTPBody::setIdentifier(long long identifier)
154 {
155 ensureMutable();
156 return m_private->setIdentifier(identifier);
157 }
158
containsPasswordData() const159 bool WebHTTPBody::containsPasswordData() const
160 {
161 return m_private->containsPasswordData();
162 }
163
setContainsPasswordData(bool containsPasswordData)164 void WebHTTPBody::setContainsPasswordData(bool containsPasswordData)
165 {
166 m_private->setContainsPasswordData(containsPasswordData);
167 }
168
WebHTTPBody(const PassRefPtr<FormData> & data)169 WebHTTPBody::WebHTTPBody(const PassRefPtr<FormData>& data)
170 : m_private(static_cast<WebHTTPBodyPrivate*>(data.leakRef()))
171 {
172 }
173
operator =(const PassRefPtr<FormData> & data)174 WebHTTPBody& WebHTTPBody::operator=(const PassRefPtr<FormData>& data)
175 {
176 assign(static_cast<WebHTTPBodyPrivate*>(data.leakRef()));
177 return *this;
178 }
179
operator PassRefPtr<FormData>() const180 WebHTTPBody::operator PassRefPtr<FormData>() const
181 {
182 return m_private;
183 }
184
assign(WebHTTPBodyPrivate * p)185 void WebHTTPBody::assign(WebHTTPBodyPrivate* p)
186 {
187 // p is already ref'd for us by the caller
188 if (m_private)
189 m_private->deref();
190 m_private = p;
191 }
192
ensureMutable()193 void WebHTTPBody::ensureMutable()
194 {
195 ASSERT(!isNull());
196 if (!m_private->hasOneRef())
197 assign(static_cast<WebHTTPBodyPrivate*>(m_private->copy().leakRef()));
198 }
199
200 } // namespace blink
201