Home | History | Annotate | Download | only in src
      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 "WebBlobData.h"
     33 
     34 #include "BlobData.h"
     35 #include <wtf/PassOwnPtr.h>
     36 
     37 using namespace WebCore;
     38 
     39 namespace WebKit {
     40 
     41 class WebBlobDataPrivate : public BlobData {
     42 };
     43 
     44 void WebBlobData::initialize()
     45 {
     46     assign(BlobData::create());
     47 }
     48 
     49 void WebBlobData::reset()
     50 {
     51     assign(0);
     52 }
     53 
     54 size_t WebBlobData::itemCount() const
     55 {
     56     ASSERT(!isNull());
     57     return m_private->items().size();
     58 }
     59 
     60 bool WebBlobData::itemAt(size_t index, Item& result) const
     61 {
     62     ASSERT(!isNull());
     63 
     64     if (index >= m_private->items().size())
     65         return false;
     66 
     67     const BlobDataItem& item = m_private->items()[index];
     68     result.data.reset();
     69     result.filePath.reset();
     70     result.blobURL = KURL();
     71     result.offset = item.offset;
     72     result.length = item.length;
     73     result.expectedModificationTime = item.expectedModificationTime;
     74 
     75     switch (item.type) {
     76     case BlobDataItem::Data:
     77         result.type = Item::TypeData;
     78         result.data = item.data;
     79         return true;
     80     case BlobDataItem::File:
     81         result.type = Item::TypeFile;
     82         result.filePath = item.path;
     83         return true;
     84     case BlobDataItem::Blob:
     85         result.type = Item::TypeBlob;
     86         result.blobURL = item.url;
     87         return true;
     88     }
     89     ASSERT_NOT_REACHED();
     90     return false;
     91 }
     92 
     93 WebString WebBlobData::contentType() const
     94 {
     95     ASSERT(!isNull());
     96     return m_private->contentType();
     97 }
     98 
     99 WebString WebBlobData::contentDisposition() const
    100 {
    101     ASSERT(!isNull());
    102     return m_private->contentDisposition();
    103 }
    104 
    105 WebBlobData::WebBlobData(const PassOwnPtr<BlobData>& data)
    106     : m_private(0)
    107 {
    108     assign(data);
    109 }
    110 
    111 WebBlobData& WebBlobData::operator=(const PassOwnPtr<BlobData>& data)
    112 {
    113     assign(data);
    114     return *this;
    115 }
    116 
    117 WebBlobData::operator PassOwnPtr<BlobData>()
    118 {
    119     WebBlobDataPrivate* temp = m_private;
    120     m_private = 0;
    121     return adoptPtr(temp);
    122 }
    123 
    124 void WebBlobData::assign(const PassOwnPtr<BlobData>& data)
    125 {
    126     if (m_private)
    127         delete m_private;
    128     m_private = static_cast<WebBlobDataPrivate*>(data.leakPtr());
    129 }
    130 
    131 } // namespace WebKit
    132