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 "core/platform/FileSystem.h"
     35 #include "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 RawData : public ThreadSafeRefCounted<RawData> {
     44 public:
     45     static PassRefPtr<RawData> create()
     46     {
     47         return adoptRef(new RawData());
     48     }
     49 
     50     void detachFromCurrentThread();
     51 
     52     const char* data() const { return m_data.data(); }
     53     size_t length() const { return m_data.size(); }
     54     Vector<char>* mutableData() { return &m_data; }
     55 
     56 private:
     57     RawData();
     58 
     59     Vector<char> m_data;
     60 };
     61 
     62 struct BlobDataItem {
     63     static const long long toEndOfFile;
     64 
     65     // Default constructor.
     66     BlobDataItem()
     67         : type(Data)
     68         , offset(0)
     69         , length(toEndOfFile)
     70         , expectedModificationTime(invalidFileTime())
     71     {
     72     }
     73 
     74     // Constructor for String type (complete string).
     75     explicit BlobDataItem(PassRefPtr<RawData> data)
     76         : type(Data)
     77         , data(data)
     78         , offset(0)
     79         , length(toEndOfFile)
     80         , expectedModificationTime(invalidFileTime())
     81     {
     82     }
     83 
     84     // Constructor for File type (complete file).
     85     explicit BlobDataItem(const String& path)
     86         : type(File)
     87         , path(path)
     88         , offset(0)
     89         , length(toEndOfFile)
     90         , expectedModificationTime(invalidFileTime())
     91     {
     92     }
     93 
     94     // Constructor for File type (partial file).
     95     BlobDataItem(const String& path, long long offset, long long length, double expectedModificationTime)
     96         : type(File)
     97         , path(path)
     98         , offset(offset)
     99         , length(length)
    100         , expectedModificationTime(expectedModificationTime)
    101     {
    102     }
    103 
    104     // Constructor for Blob type.
    105     BlobDataItem(const KURL& url, long long offset, long long length)
    106         : type(Blob)
    107         , url(url)
    108         , offset(offset)
    109         , length(length)
    110         , expectedModificationTime(invalidFileTime())
    111     {
    112     }
    113 
    114     // Constructor for URL type (e.g. FileSystem files).
    115     BlobDataItem(const KURL& url, long long offset, long long length, double expectedModificationTime)
    116         : type(URL)
    117         , url(url)
    118         , offset(offset)
    119         , length(length)
    120         , expectedModificationTime(expectedModificationTime)
    121     {
    122     }
    123 
    124     // Detaches from current thread so that it can be passed to another thread.
    125     void detachFromCurrentThread();
    126 
    127     enum {
    128         Data,
    129         File,
    130         Blob,
    131         URL
    132     } type;
    133 
    134     // For Data type.
    135     RefPtr<RawData> data;
    136 
    137     // For File type.
    138     String path;
    139 
    140     // For Blob or URL type.
    141     KURL url;
    142 
    143     long long offset;
    144     long long length;
    145     double expectedModificationTime;
    146 
    147 private:
    148     friend class BlobData;
    149 
    150     // Constructor for String type (partial string).
    151     BlobDataItem(PassRefPtr<RawData> data, long long offset, long long length)
    152         : type(Data)
    153         , data(data)
    154         , offset(offset)
    155         , length(length)
    156         , expectedModificationTime(invalidFileTime())
    157     {
    158     }
    159 };
    160 
    161 typedef Vector<BlobDataItem> BlobDataItemList;
    162 
    163 class BlobData {
    164     WTF_MAKE_FAST_ALLOCATED;
    165 public:
    166     static PassOwnPtr<BlobData> create();
    167 
    168     // Detaches from current thread so that it can be passed to another thread.
    169     void detachFromCurrentThread();
    170 
    171     const String& contentType() const { return m_contentType; }
    172     void setContentType(const String& contentType) { m_contentType = contentType; }
    173 
    174     const String& contentDisposition() const { return m_contentDisposition; }
    175     void setContentDisposition(const String& contentDisposition) { m_contentDisposition = contentDisposition; }
    176 
    177     const BlobDataItemList& items() const { return m_items; }
    178     void swapItems(BlobDataItemList&);
    179 
    180     void appendData(PassRefPtr<RawData>, long long offset, long long length);
    181     void appendFile(const String& path);
    182     void appendFile(const String& path, long long offset, long long length, double expectedModificationTime);
    183     void appendBlob(const KURL&, long long offset, long long length);
    184     void appendURL(const KURL&, long long offset, long long length, double expectedModificationTime);
    185 
    186 private:
    187     friend class BlobRegistryImpl;
    188     friend class BlobStorageData;
    189 
    190     BlobData() { }
    191 
    192     // This is only exposed to BlobStorageData.
    193     void appendData(const RawData&, long long offset, long long length);
    194 
    195     String m_contentType;
    196     String m_contentDisposition;
    197     BlobDataItemList m_items;
    198 };
    199 
    200 // FIXME: This class is mostly place holder until I get farther along with
    201 // https://bugs.webkit.org/show_bug.cgi?id=108733 and more specifically with landing
    202 // https://codereview.chromium.org/11192017/.
    203 class BlobDataHandle : public ThreadSafeRefCounted<BlobDataHandle> {
    204 public:
    205     static PassRefPtr<BlobDataHandle> create(PassOwnPtr<BlobData> data, long long size)
    206     {
    207         return adoptRef(new BlobDataHandle(data, size));
    208     }
    209 
    210     ~BlobDataHandle();
    211 
    212 private:
    213     BlobDataHandle(PassOwnPtr<BlobData>, long long size);
    214     KURL m_internalURL;
    215 };
    216 
    217 } // namespace WebCore
    218 
    219 #endif // BlobData_h
    220