Home | History | Annotate | Download | only in network
      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 #ifndef BlobData_h
     32 #define BlobData_h
     33 
     34 #include "KURL.h"
     35 #include "PlatformString.h"
     36 #include <wtf/Forward.h>
     37 #include <wtf/ThreadSafeRefCounted.h>
     38 
     39 namespace WebCore {
     40 
     41 class RawData : public ThreadSafeRefCounted<RawData> {
     42 public:
     43     static PassRefPtr<RawData> create()
     44     {
     45         return adoptRef(new RawData());
     46     }
     47 
     48     void detachFromCurrentThread();
     49 
     50     const char* data() const { return m_data.data(); }
     51     size_t length() const { return m_data.size(); }
     52     Vector<char>* mutableData() { return &m_data; }
     53 
     54 private:
     55     RawData();
     56 
     57     Vector<char> m_data;
     58 };
     59 
     60 struct BlobDataItem {
     61     static const long long toEndOfFile;
     62     static const double doNotCheckFileChange;
     63 
     64     // Default constructor.
     65     BlobDataItem()
     66         : type(Data)
     67         , offset(0)
     68         , length(toEndOfFile)
     69         , expectedModificationTime(doNotCheckFileChange)
     70     {
     71     }
     72 
     73     // Constructor for String type (complete string).
     74     explicit BlobDataItem(PassRefPtr<RawData> data)
     75         : type(Data)
     76         , data(data)
     77         , offset(0)
     78         , length(toEndOfFile)
     79         , expectedModificationTime(doNotCheckFileChange)
     80     {
     81     }
     82 
     83     // Constructor for File type (complete file).
     84     explicit BlobDataItem(const String& path)
     85         : type(File)
     86         , path(path)
     87         , offset(0)
     88         , length(toEndOfFile)
     89         , expectedModificationTime(doNotCheckFileChange)
     90     {
     91     }
     92 
     93     // Constructor for File type (partial file).
     94     BlobDataItem(const String& path, long long offset, long long length, double expectedModificationTime)
     95         : type(File)
     96         , path(path)
     97         , offset(offset)
     98         , length(length)
     99         , expectedModificationTime(expectedModificationTime)
    100     {
    101     }
    102 
    103     // Constructor for Blob type.
    104     BlobDataItem(const KURL& url, long long offset, long long length)
    105         : type(Blob)
    106         , url(url)
    107         , offset(offset)
    108         , length(length)
    109         , expectedModificationTime(doNotCheckFileChange)
    110     {
    111     }
    112 
    113     // Detaches from current thread so that it can be passed to another thread.
    114     void detachFromCurrentThread();
    115 
    116     enum { Data, File, Blob } type;
    117 
    118     // For Data type.
    119     RefPtr<RawData> data;
    120 
    121     // For File type.
    122     String path;
    123 
    124     // For Blob type.
    125     KURL url;
    126 
    127     long long offset;
    128     long long length;
    129     double expectedModificationTime;
    130 
    131 private:
    132     friend class BlobData;
    133 
    134     // Constructor for String type (partial string).
    135     BlobDataItem(PassRefPtr<RawData> data, long long offset, long long length)
    136         : type(Data)
    137         , data(data)
    138         , offset(offset)
    139         , length(length)
    140         , expectedModificationTime(doNotCheckFileChange)
    141     {
    142     }
    143 };
    144 
    145 typedef Vector<BlobDataItem> BlobDataItemList;
    146 
    147 class BlobData {
    148     WTF_MAKE_FAST_ALLOCATED;
    149 public:
    150     static PassOwnPtr<BlobData> create();
    151 
    152     // Detaches from current thread so that it can be passed to another thread.
    153     void detachFromCurrentThread();
    154 
    155     const String& contentType() const { return m_contentType; }
    156     void setContentType(const String& contentType) { m_contentType = contentType; }
    157 
    158     const String& contentDisposition() const { return m_contentDisposition; }
    159     void setContentDisposition(const String& contentDisposition) { m_contentDisposition = contentDisposition; }
    160 
    161     const BlobDataItemList& items() const { return m_items; }
    162     void swapItems(BlobDataItemList&);
    163 
    164     void appendData(PassRefPtr<RawData>, long long offset, long long length);
    165     void appendFile(const String& path);
    166     void appendFile(const String& path, long long offset, long long length, double expectedModificationTime);
    167     void appendBlob(const KURL&, long long offset, long long length);
    168 
    169 private:
    170     friend class BlobRegistryImpl;
    171     friend class BlobStorageData;
    172 
    173     BlobData() { }
    174 
    175     // This is only exposed to BlobStorageData.
    176     void appendData(const RawData&, long long offset, long long length);
    177 
    178     String m_contentType;
    179     String m_contentDisposition;
    180     BlobDataItemList m_items;
    181 };
    182 
    183 } // namespace WebCore
    184 
    185 #endif // BlobData_h
    186