Home | History | Annotate | Download | only in blob
      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 "platform/FileMetadata.h"
     35 #include "platform/weborigin/KURL.h"
     36 #include "wtf/Forward.h"
     37 #include "wtf/PassOwnPtr.h"
     38 #include "wtf/ThreadSafeRefCounted.h"
     39 #include "wtf/text/WTFString.h"
     40 
     41 namespace WebCore {
     42 
     43 class BlobDataHandle;
     44 
     45 class PLATFORM_EXPORT RawData : public ThreadSafeRefCounted<RawData> {
     46 public:
     47     static PassRefPtr<RawData> create()
     48     {
     49         return adoptRef(new RawData());
     50     }
     51 
     52     void detachFromCurrentThread();
     53 
     54     const char* data() const { return m_data.data(); }
     55     size_t length() const { return m_data.size(); }
     56     Vector<char>* mutableData() { return &m_data; }
     57 
     58 private:
     59     RawData();
     60 
     61     Vector<char> m_data;
     62 };
     63 
     64 struct PLATFORM_EXPORT BlobDataItem {
     65     static const long long toEndOfFile;
     66 
     67     // Default constructor.
     68     BlobDataItem()
     69         : type(Data)
     70         , offset(0)
     71         , length(toEndOfFile)
     72         , expectedModificationTime(invalidFileTime())
     73     {
     74     }
     75 
     76     // Constructor for String type (complete string).
     77     explicit BlobDataItem(PassRefPtr<RawData> data)
     78         : type(Data)
     79         , data(data)
     80         , offset(0)
     81         , length(toEndOfFile)
     82         , expectedModificationTime(invalidFileTime())
     83     {
     84     }
     85 
     86     // Constructor for File type (complete file).
     87     explicit BlobDataItem(const String& path)
     88         : type(File)
     89         , path(path)
     90         , offset(0)
     91         , length(toEndOfFile)
     92         , expectedModificationTime(invalidFileTime())
     93     {
     94     }
     95 
     96     // Constructor for File type (partial file).
     97     BlobDataItem(const String& path, long long offset, long long length, double expectedModificationTime)
     98         : type(File)
     99         , path(path)
    100         , offset(offset)
    101         , length(length)
    102         , expectedModificationTime(expectedModificationTime)
    103     {
    104     }
    105 
    106     // Constructor for Blob type.
    107     BlobDataItem(PassRefPtr<BlobDataHandle> blobDataHandle, long long offset, long long length)
    108         : type(Blob)
    109         , blobDataHandle(blobDataHandle)
    110         , offset(offset)
    111         , length(length)
    112         , expectedModificationTime(invalidFileTime())
    113     {
    114     }
    115 
    116     // Constructor for FileSystem file type.
    117     BlobDataItem(const KURL& fileSystemURL, long long offset, long long length, double expectedModificationTime)
    118         : type(FileSystemURL)
    119         , fileSystemURL(fileSystemURL)
    120         , offset(offset)
    121         , length(length)
    122         , expectedModificationTime(expectedModificationTime)
    123     {
    124     }
    125 
    126     // Detaches from current thread so that it can be passed to another thread.
    127     void detachFromCurrentThread();
    128 
    129     enum {
    130         Data,
    131         File,
    132         Blob,
    133         FileSystemURL
    134     } type;
    135 
    136     RefPtr<RawData> data; // For Data type.
    137     String path; // For File type.
    138     KURL fileSystemURL; // For FileSystemURL type.
    139     RefPtr<BlobDataHandle> blobDataHandle; // For Blob type.
    140 
    141     long long offset;
    142     long long length;
    143     double expectedModificationTime;
    144 
    145 private:
    146     friend class BlobData;
    147 
    148     // Constructor for String type (partial string).
    149     BlobDataItem(PassRefPtr<RawData> data, long long offset, long long length)
    150         : type(Data)
    151         , data(data)
    152         , offset(offset)
    153         , length(length)
    154         , expectedModificationTime(invalidFileTime())
    155     {
    156     }
    157 };
    158 
    159 typedef Vector<BlobDataItem> BlobDataItemList;
    160 
    161 class PLATFORM_EXPORT BlobData {
    162     WTF_MAKE_FAST_ALLOCATED;
    163 public:
    164     static PassOwnPtr<BlobData> create();
    165 
    166     // Detaches from current thread so that it can be passed to another thread.
    167     void detachFromCurrentThread();
    168 
    169     const String& contentType() const { return m_contentType; }
    170     void setContentType(const String& contentType) { m_contentType = contentType; }
    171 
    172     const String& contentDisposition() const { return m_contentDisposition; }
    173     void setContentDisposition(const String& contentDisposition) { m_contentDisposition = contentDisposition; }
    174 
    175     const BlobDataItemList& items() const { return m_items; }
    176     void swapItems(BlobDataItemList&);
    177 
    178     void appendData(PassRefPtr<RawData>, long long offset, long long length);
    179     void appendFile(const String& path);
    180     void appendFile(const String& path, long long offset, long long length, double expectedModificationTime);
    181     void appendBlob(PassRefPtr<BlobDataHandle>, long long offset, long long length);
    182     void appendFileSystemURL(const KURL&, long long offset, long long length, double expectedModificationTime);
    183 
    184 private:
    185     friend class BlobRegistryImpl;
    186     friend class BlobStorageData;
    187 
    188     BlobData() { }
    189 
    190     // This is only exposed to BlobStorageData.
    191     void appendData(const RawData&, long long offset, long long length);
    192 
    193     String m_contentType;
    194     String m_contentDisposition;
    195     BlobDataItemList m_items;
    196 };
    197 
    198 
    199 class PLATFORM_EXPORT BlobDataHandle : public ThreadSafeRefCounted<BlobDataHandle> {
    200 public:
    201     // For empty blob construction.
    202     static PassRefPtr<BlobDataHandle> create()
    203     {
    204         return adoptRef(new BlobDataHandle());
    205     }
    206 
    207     // For initial creation.
    208     static PassRefPtr<BlobDataHandle> create(PassOwnPtr<BlobData> data, long long size)
    209     {
    210         return adoptRef(new BlobDataHandle(data, size));
    211     }
    212 
    213     // For deserialization of script values and ipc messages.
    214     static PassRefPtr<BlobDataHandle> create(const String& uuid, const String& type, long long size)
    215     {
    216         return adoptRef(new BlobDataHandle(uuid, type, size));
    217     }
    218 
    219     String uuid() const { return m_uuid.isolatedCopy(); }
    220     String type() const { return m_type.isolatedCopy(); }
    221     unsigned long long size() { return m_size; }
    222 
    223     ~BlobDataHandle();
    224 
    225 private:
    226     BlobDataHandle();
    227     BlobDataHandle(PassOwnPtr<BlobData>, long long size);
    228     BlobDataHandle(const String& uuid, const String& type, long long size);
    229 
    230     const String m_uuid;
    231     const String m_type;
    232     const long long m_size;
    233 };
    234 
    235 } // namespace WebCore
    236 
    237 #endif // BlobData_h
    238