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     const 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 BlobDataItemList& items() const { return m_items; }
    173     void swapItems(BlobDataItemList&);
    174 
    175     void appendData(PassRefPtr<RawData>, long long offset, long long length);
    176     void appendFile(const String& path);
    177     void appendFile(const String& path, long long offset, long long length, double expectedModificationTime);
    178     void appendBlob(PassRefPtr<BlobDataHandle>, long long offset, long long length);
    179     void appendFileSystemURL(const KURL&, long long offset, long long length, double expectedModificationTime);
    180     void appendText(const String&, bool normalizeLineEndingsToNative);
    181     void appendArrayBuffer(const ArrayBuffer*);
    182     void appendArrayBufferView(const ArrayBufferView*);
    183 
    184     // The value of the size property for a Blob who has this data.
    185     // BlobDataItem::toEndOfFile if the Blob has a file whose size was not yet determined.
    186     long long length() const;
    187 
    188 private:
    189     friend class BlobRegistryImpl;
    190     friend class BlobStorageData;
    191 
    192     // Used by appendArrayBuffer and appendArrayBufferView.
    193     void appendBytes(const void*, long long length);
    194 
    195     BlobData() { }
    196 
    197     String m_contentType;
    198     BlobDataItemList m_items;
    199 };
    200 
    201 
    202 class PLATFORM_EXPORT BlobDataHandle : public ThreadSafeRefCounted<BlobDataHandle> {
    203 public:
    204     // For empty blob construction.
    205     static PassRefPtr<BlobDataHandle> create()
    206     {
    207         return adoptRef(new BlobDataHandle());
    208     }
    209 
    210     // For initial creation.
    211     static PassRefPtr<BlobDataHandle> create(PassOwnPtr<BlobData> data, long long size)
    212     {
    213         return adoptRef(new BlobDataHandle(data, size));
    214     }
    215 
    216     // For deserialization of script values and ipc messages.
    217     static PassRefPtr<BlobDataHandle> create(const String& uuid, const String& type, long long size)
    218     {
    219         return adoptRef(new BlobDataHandle(uuid, type, size));
    220     }
    221 
    222     String uuid() const { return m_uuid.isolatedCopy(); }
    223     String type() const { return m_type.isolatedCopy(); }
    224     unsigned long long size() { return m_size; }
    225 
    226     ~BlobDataHandle();
    227 
    228 private:
    229     BlobDataHandle();
    230     BlobDataHandle(PassOwnPtr<BlobData>, long long size);
    231     BlobDataHandle(const String& uuid, const String& type, long long size);
    232 
    233     const String m_uuid;
    234     const String m_type;
    235     const long long m_size;
    236 };
    237 
    238 } // namespace WebCore
    239 
    240 #endif // BlobData_h
    241