WebKit/chromium/src/WebHTTPBody.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 "WebHTTPBody.h"
       
    33 
       
    34 #include "FormData.h"
       
    35 #include "WebFileInfo.h"
       
    36 
       
    37 using namespace WebCore;
       
    38 
       
    39 namespace WebKit {
       
    40 
       
    41 class WebHTTPBodyPrivate : public FormData {
       
    42 };
       
    43 
       
    44 void WebHTTPBody::initialize()
       
    45 {
       
    46     assign(static_cast<WebHTTPBodyPrivate*>(FormData::create().releaseRef()));
       
    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     switch (element.m_type) {
       
    78     case FormDataElement::data:
       
    79         result.type = Element::TypeData;
       
    80         result.data.assign(element.m_data.data(), element.m_data.size());
       
    81         result.filePath.reset();
       
    82 #if ENABLE(BLOB_SLICE)
       
    83         result.fileStart = 0;
       
    84         result.fileLength = 0;
       
    85         result.fileInfo.modificationTime = 0.0;
       
    86 #endif
       
    87         break;
       
    88     case FormDataElement::encodedFile:
       
    89         result.type = Element::TypeFile;
       
    90         result.data.reset();
       
    91         result.filePath = element.m_filename;
       
    92 #if ENABLE(BLOB_SLICE)
       
    93         result.fileStart = element.m_fileStart;
       
    94         result.fileLength = element.m_fileLength;
       
    95         result.fileInfo.modificationTime = element.m_expectedFileModificationTime;
       
    96 #else
       
    97         result.fileStart = 0;
       
    98         result.fileLength = -1;
       
    99         result.fileInfo.modificationTime = 0.0;
       
   100 #endif
       
   101         break;
       
   102     default:
       
   103         ASSERT_NOT_REACHED();
       
   104         return false;
       
   105     }
       
   106 
       
   107     return true;
       
   108 }
       
   109 
       
   110 void WebHTTPBody::appendData(const WebData& data)
       
   111 {
       
   112     ensureMutable();
       
   113     // FIXME: FormDataElement::m_data should be a SharedBuffer<char>.  Then we
       
   114     // could avoid this buffer copy.
       
   115     m_private->appendData(data.data(), data.size());
       
   116 }
       
   117 
       
   118 void WebHTTPBody::appendFile(const WebString& filePath)
       
   119 {
       
   120     ensureMutable();
       
   121     m_private->appendFile(filePath);
       
   122 }
       
   123 
       
   124 void WebHTTPBody::appendFileRange(const WebString& filePath, long long fileStart, long long fileLength, const WebFileInfo& fileInfo)
       
   125 {
       
   126 #if ENABLE(BLOB_SLICE)
       
   127     ensureMutable();
       
   128     m_private->appendFileRange(filePath, fileStart, fileLength, fileInfo.modificationTime);
       
   129 #endif
       
   130 }
       
   131 
       
   132 long long WebHTTPBody::identifier() const
       
   133 {
       
   134     ASSERT(!isNull());
       
   135     return m_private->identifier();
       
   136 }
       
   137 
       
   138 void WebHTTPBody::setIdentifier(long long identifier)
       
   139 {
       
   140     ensureMutable();
       
   141     return m_private->setIdentifier(identifier);
       
   142 }
       
   143 
       
   144 WebHTTPBody::WebHTTPBody(const PassRefPtr<FormData>& data)
       
   145     : m_private(static_cast<WebHTTPBodyPrivate*>(data.releaseRef()))
       
   146 {
       
   147 }
       
   148 
       
   149 WebHTTPBody& WebHTTPBody::operator=(const PassRefPtr<FormData>& data)
       
   150 {
       
   151     assign(static_cast<WebHTTPBodyPrivate*>(data.releaseRef()));
       
   152     return *this;
       
   153 }
       
   154 
       
   155 WebHTTPBody::operator PassRefPtr<FormData>() const
       
   156 {
       
   157     return m_private;
       
   158 }
       
   159 
       
   160 void WebHTTPBody::assign(WebHTTPBodyPrivate* p)
       
   161 {
       
   162     // p is already ref'd for us by the caller
       
   163     if (m_private)
       
   164         m_private->deref();
       
   165     m_private = p;
       
   166 }
       
   167 
       
   168 void WebHTTPBody::ensureMutable()
       
   169 {
       
   170     ASSERT(!isNull());
       
   171     if (!m_private->hasOneRef())
       
   172         assign(static_cast<WebHTTPBodyPrivate*>(m_private->copy().releaseRef()));
       
   173 }
       
   174 
       
   175 } // namespace WebKit