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