OLD | NEW |
(Empty) | |
| 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 "TextEncoding.h" |
| 36 #include <wtf/OwnPtr.h> |
| 37 #include <wtf/PassRefPtr.h> |
| 38 #include <wtf/RefCounted.h> |
| 39 #include <wtf/Vector.h> |
| 40 #include <wtf/text/CString.h> |
| 41 |
| 42 namespace WebCore { |
| 43 |
| 44 // String ending types. |
| 45 enum LineEnding { |
| 46 EndingTransparent = 0, |
| 47 EndingLF, |
| 48 EndingCR, |
| 49 EndingCRLF, |
| 50 }; |
| 51 |
| 52 class ByteArrayBlobItem; |
| 53 class DataBlobItem; |
| 54 class DataRangeBlobItem; |
| 55 class FileBlobItem; |
| 56 class FileRangeBlobItem; |
| 57 class StringBlobItem; |
| 58 |
| 59 // A base interface for all arbitrary-data-object component items. |
| 60 // The BlobItem and its subclasses are structured like the following: |
| 61 // BlobItem |
| 62 // | |
| 63 // +-------> DataBlobItem |
| 64 // | | |
| 65 // | +---------> StringBlobItem |
| 66 // | +---------> ByteArrayBlobItem |
| 67 // | +---------> DataRangeBlobItem |
| 68 // | |
| 69 // +-------> FileBlobItem |
| 70 // | |
| 71 // +---------> FileRangeBlobItem |
| 72 // |
| 73 class BlobItem : public RefCounted<BlobItem> { |
| 74 public: |
| 75 virtual ~BlobItem() { } |
| 76 virtual unsigned long long size() const = 0; |
| 77 |
| 78 virtual const DataBlobItem* toDataBlobItem() const { return 0; } |
| 79 virtual const StringBlobItem* toStringBlobItem() const { return 0; } |
| 80 virtual const ByteArrayBlobItem* toByteArrayBlobItem() const { return 0; } |
| 81 virtual const FileBlobItem* toFileBlobItem() const { return 0; } |
| 82 #if ENABLE(BLOB_SLICE) |
| 83 virtual const DataRangeBlobItem* toDataRangeBlobItem() const { return 0; } |
| 84 virtual const FileRangeBlobItem* toFileRangeBlobItem() const { return 0; } |
| 85 |
| 86 // Note: no external methods except for Blob::slice should call this. |
| 87 virtual PassRefPtr<BlobItem> slice(long long start, long long length) = 0; |
| 88 #endif // ENABLE(BLOB_SLICE) |
| 89 |
| 90 protected: |
| 91 BlobItem() { } |
| 92 }; |
| 93 |
| 94 typedef Vector<RefPtr<BlobItem> > BlobItemList; |
| 95 |
| 96 class DataBlobItem : public BlobItem { |
| 97 public: |
| 98 virtual const char* data() const = 0; |
| 99 |
| 100 // BlobItem methods. |
| 101 virtual const DataBlobItem* toDataBlobItem() const { return this; } |
| 102 #if ENABLE(BLOB_SLICE) |
| 103 virtual PassRefPtr<BlobItem> slice(long long start, long long length); |
| 104 #endif // ENABLE(BLOB_SLICE) |
| 105 }; |
| 106 |
| 107 class FileBlobItem : public BlobItem { |
| 108 public: |
| 109 static PassRefPtr<BlobItem> create(const String& path); |
| 110 virtual const String& name() const { return m_fileName; } |
| 111 virtual const String& path() const { return m_path; } |
| 112 |
| 113 // BlobItem methods. |
| 114 virtual unsigned long long size() const; |
| 115 virtual const FileBlobItem* toFileBlobItem() const { return this; } |
| 116 #if ENABLE(BLOB_SLICE) |
| 117 virtual PassRefPtr<BlobItem> slice(long long start, long long length); |
| 118 #endif // ENABLE(BLOB_SLICE) |
| 119 |
| 120 protected: |
| 121 FileBlobItem(const String& path); |
| 122 String m_path; |
| 123 String m_fileName; |
| 124 }; |
| 125 |
| 126 class StringBlobItem : public DataBlobItem { |
| 127 public: |
| 128 static PassRefPtr<BlobItem> create(const String&, LineEnding, TextEncoding); |
| 129 static PassRefPtr<BlobItem> create(const CString&); |
| 130 const CString& cstr() const { return m_data; } |
| 131 |
| 132 // BlobItem methods. |
| 133 virtual unsigned long long size() const { return m_data.length(); } |
| 134 virtual const StringBlobItem* toStringBlobItem() const { return this; } |
| 135 |
| 136 // DataBlobItem methods. |
| 137 virtual const char* data() const { return m_data.data(); } |
| 138 |
| 139 private: |
| 140 StringBlobItem(const String&, LineEnding, TextEncoding); |
| 141 StringBlobItem(const CString&); |
| 142 static CString convertToCString(const String&, LineEnding, TextEncoding); |
| 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 lengt
h); |
| 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, dou
ble 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 |
OLD | NEW |