WebCore/platform/BlobItem.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 BlobItem_h
       
    32 #define BlobItem_h
       
    33 
       
    34 #include "PlatformString.h"
       
    35 #include <wtf/OwnPtr.h>
       
    36 #include <wtf/PassRefPtr.h>
       
    37 #include <wtf/RefCounted.h>
       
    38 #include <wtf/Vector.h>
       
    39 #include <wtf/text/CString.h>
       
    40 
       
    41 namespace WebCore {
       
    42 
       
    43 class ByteArrayBlobItem;
       
    44 class DataBlobItem;
       
    45 class DataRangeBlobItem;
       
    46 class FileBlobItem;
       
    47 class FileRangeBlobItem;
       
    48 class StringBlobItem;
       
    49 
       
    50 // A base interface for all arbitrary-data-object component items.
       
    51 // The BlobItem and its subclasses are structured like the following:
       
    52 //    BlobItem
       
    53 //       |
       
    54 //       +-------> DataBlobItem
       
    55 //       |              |
       
    56 //       |              +---------> StringBlobItem
       
    57 //       |              +---------> ByteArrayBlobItem
       
    58 //       |              +---------> DataRangeBlobItem
       
    59 //       |
       
    60 //       +-------> FileBlobItem
       
    61 //                      |
       
    62 //                      +---------> FileRangeBlobItem
       
    63 //
       
    64 class BlobItem : public RefCounted<BlobItem> {
       
    65 public:
       
    66     virtual ~BlobItem() { }
       
    67     virtual unsigned long long size() const = 0;
       
    68 
       
    69     virtual const DataBlobItem* toDataBlobItem() const { return 0; }
       
    70     virtual const StringBlobItem* toStringBlobItem() const { return 0; }
       
    71     virtual const ByteArrayBlobItem* toByteArrayBlobItem() const { return 0; }
       
    72     virtual const FileBlobItem* toFileBlobItem() const { return 0; }
       
    73 #if ENABLE(BLOB_SLICE)
       
    74     virtual const DataRangeBlobItem* toDataRangeBlobItem() const { return 0; }
       
    75     virtual const FileRangeBlobItem* toFileRangeBlobItem() const { return 0; }
       
    76 
       
    77     // Note: no external methods except for Blob::slice should call this.
       
    78     virtual PassRefPtr<BlobItem> slice(long long start, long long length) = 0;
       
    79 #endif // ENABLE(BLOB_SLICE)
       
    80 
       
    81 protected:
       
    82     BlobItem() { }
       
    83 };
       
    84 
       
    85 typedef Vector<RefPtr<BlobItem> > BlobItemList;
       
    86 
       
    87 class DataBlobItem : public BlobItem {
       
    88 public:
       
    89     virtual const char* data() const = 0;
       
    90 
       
    91     // BlobItem methods.
       
    92     virtual const DataBlobItem* toDataBlobItem() const { return this; }
       
    93 #if ENABLE(BLOB_SLICE)
       
    94     virtual PassRefPtr<BlobItem> slice(long long start, long long length);
       
    95 #endif // ENABLE(BLOB_SLICE)
       
    96 };
       
    97 
       
    98 class FileBlobItem : public BlobItem {
       
    99 public:
       
   100     static PassRefPtr<BlobItem> create(const String& path);
       
   101 #if ENABLE(DIRECTORY_UPLOAD)
       
   102     static PassRefPtr<BlobItem> create(const String& path, const String& relativePath);
       
   103 #endif
       
   104     virtual const String& name() const { return m_fileName; }
       
   105     virtual const String& path() const { return m_path; }
       
   106 #if ENABLE(DIRECTORY_UPLOAD)
       
   107     const String& relativePath() const { return m_relativePath; }
       
   108 #endif
       
   109 
       
   110     // BlobItem methods.
       
   111     virtual unsigned long long size() const;
       
   112     virtual const FileBlobItem* toFileBlobItem() const { return this; }
       
   113 #if ENABLE(BLOB_SLICE)
       
   114     virtual PassRefPtr<BlobItem> slice(long long start, long long length);
       
   115 #endif // ENABLE(BLOB_SLICE)
       
   116 
       
   117 protected:
       
   118     FileBlobItem(const String& path);
       
   119 #if ENABLE(DIRECTORY_UPLOAD)
       
   120     FileBlobItem(const String& path, const String& relativePath);
       
   121 #endif
       
   122     String m_path;
       
   123     String m_fileName;
       
   124 #if ENABLE(DIRECTORY_UPLOAD)
       
   125     String m_relativePath;
       
   126 #endif
       
   127 };
       
   128 
       
   129 class StringBlobItem : public DataBlobItem {
       
   130 public:
       
   131     static PassRefPtr<BlobItem> create(const CString&);
       
   132     const CString& cstr() const { return m_data; }
       
   133 
       
   134     // BlobItem methods.
       
   135     virtual unsigned long long size() const { return m_data.length(); }
       
   136     virtual const StringBlobItem* toStringBlobItem() const { return this; }
       
   137 
       
   138     // DataBlobItem methods.
       
   139     virtual const char* data() const { return m_data.data(); }
       
   140 
       
   141 private:
       
   142     StringBlobItem(const CString&);
       
   143     CString m_data;
       
   144 };
       
   145 
       
   146 class ByteArrayBlobItem : public DataBlobItem {
       
   147 public:
       
   148     static PassRefPtr<BlobItem> create(const char* data, size_t size);
       
   149 
       
   150     // BlobItem methods.
       
   151     virtual unsigned long long size() const { return m_bytesArray.size(); }
       
   152     virtual const ByteArrayBlobItem* toByteArrayBlobItem() const { return this; }
       
   153 
       
   154     // DataBlobItem methods.
       
   155     virtual const char* data() const { return m_bytesArray.data(); }
       
   156 
       
   157 private:
       
   158     ByteArrayBlobItem(const char* data, size_t size);
       
   159     Vector<char> m_bytesArray;
       
   160 };
       
   161 
       
   162 #if ENABLE(BLOB_SLICE)
       
   163 
       
   164 // BlobItem class for sliced data (string or bytes-array).
       
   165 class DataRangeBlobItem : public DataBlobItem {
       
   166 public:
       
   167     static PassRefPtr<BlobItem> create(PassRefPtr<DataBlobItem>, long long start, long long length);
       
   168 
       
   169     // BlobItem methods.
       
   170     virtual unsigned long long size() const { return m_length; }
       
   171     virtual const DataRangeBlobItem* toDataRangeBlobItem() const { return this; }
       
   172 
       
   173     // DataBlobItem methods.
       
   174     virtual const char* data() const;
       
   175 
       
   176 private:
       
   177     DataRangeBlobItem(PassRefPtr<DataBlobItem>, long long start, long long length);
       
   178     RefPtr<DataBlobItem> m_item;
       
   179     long long m_start;
       
   180     long long m_length;
       
   181 };
       
   182 
       
   183 class FileRangeBlobItem : public FileBlobItem {
       
   184 public:
       
   185     static PassRefPtr<BlobItem> create(const String& path, long long start, long long length, double snapshotModificationTime);
       
   186     long long start() const { return m_start; }
       
   187     double snapshotModificationTime() const { return m_snapshotModificationTime; }
       
   188 
       
   189     // BlobItem methods.
       
   190     virtual unsigned long long size() const { return m_length; }
       
   191     virtual const FileRangeBlobItem* toFileRangeBlobItem() const { return this; }
       
   192 
       
   193     // FileBlobItem methods.
       
   194     virtual const String& name() const { return m_uniqueName; }
       
   195 
       
   196 private:
       
   197     FileRangeBlobItem(const String& path, long long start, long long length, double snapshotModificationTime);
       
   198     long long m_start;
       
   199     long long m_length;
       
   200     String m_uniqueName;
       
   201 
       
   202     double m_snapshotModificationTime;
       
   203 };
       
   204 
       
   205 #endif // ENABLE(BLOB_SLICE)
       
   206 
       
   207 } // namespace WebCore
       
   208 
       
   209 #endif // BlobItem_h