2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
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"
45 class PLATFORM_EXPORT RawData : public ThreadSafeRefCounted<RawData> {
47 static PassRefPtr<RawData> create()
49 return adoptRef(new RawData());
52 void detachFromCurrentThread();
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; }
64 struct PLATFORM_EXPORT BlobDataItem {
65 static const long long toEndOfFile;
67 // Default constructor.
72 , expectedModificationTime(invalidFileTime())
76 // Constructor for String type (complete string).
77 explicit BlobDataItem(PassRefPtr<RawData> data)
82 , expectedModificationTime(invalidFileTime())
86 // Constructor for File type (complete file).
87 explicit BlobDataItem(const String& path)
92 , expectedModificationTime(invalidFileTime())
96 // Constructor for File type (partial file).
97 BlobDataItem(const String& path, long long offset, long long length, double expectedModificationTime)
102 , expectedModificationTime(expectedModificationTime)
106 // Constructor for Blob type.
107 BlobDataItem(PassRefPtr<BlobDataHandle> blobDataHandle, long long offset, long long length)
109 , blobDataHandle(blobDataHandle)
112 , expectedModificationTime(invalidFileTime())
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)
122 , expectedModificationTime(expectedModificationTime)
126 // Detaches from current thread so that it can be passed to another thread.
127 void detachFromCurrentThread();
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.
143 double expectedModificationTime;
146 friend class BlobData;
148 // Constructor for String type (partial string).
149 BlobDataItem(PassRefPtr<RawData> data, long long offset, long long length)
154 , expectedModificationTime(invalidFileTime())
159 typedef Vector<BlobDataItem> BlobDataItemList;
161 class PLATFORM_EXPORT BlobData {
162 WTF_MAKE_FAST_ALLOCATED;
164 static PassOwnPtr<BlobData> create();
166 // Detaches from current thread so that it can be passed to another thread.
167 void detachFromCurrentThread();
169 const String& contentType() const { return m_contentType; }
170 void setContentType(const String& contentType) { m_contentType = contentType; }
172 const BlobDataItemList& items() const { return m_items; }
173 void swapItems(BlobDataItemList&);
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*);
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;
189 friend class BlobRegistryImpl;
190 friend class BlobStorageData;
192 // Used by appendArrayBuffer and appendArrayBufferView.
193 void appendBytes(const void*, long long length);
197 String m_contentType;
198 BlobDataItemList m_items;
202 class PLATFORM_EXPORT BlobDataHandle : public ThreadSafeRefCounted<BlobDataHandle> {
204 // For empty blob construction.
205 static PassRefPtr<BlobDataHandle> create()
207 return adoptRef(new BlobDataHandle());
210 // For initial creation.
211 static PassRefPtr<BlobDataHandle> create(PassOwnPtr<BlobData> data, long long size)
213 return adoptRef(new BlobDataHandle(data, size));
216 // For deserialization of script values and ipc messages.
217 static PassRefPtr<BlobDataHandle> create(const String& uuid, const String& type, long long size)
219 return adoptRef(new BlobDataHandle(uuid, type, size));
222 String uuid() const { return m_uuid.isolatedCopy(); }
223 String type() const { return m_type.isolatedCopy(); }
224 unsigned long long size() { return m_size; }
230 BlobDataHandle(PassOwnPtr<BlobData>, long long size);
231 BlobDataHandle(const String& uuid, const String& type, long long size);
235 const long long m_size;
238 } // namespace WebCore