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 "BlobDataFileReference.h"
35#include "ThreadSafeDataBuffer.h"
36#include <wtf/Forward.h>
37#include <wtf/ThreadSafeRefCounted.h>
38#include <wtf/text/WTFString.h>
39
40namespace WebCore {
41
42class BlobDataItem {
43public:
44 WEBCORE_EXPORT static const long long toEndOfFile;
45
46 enum class Type {
47 Data,
48 File
49 };
50
51 Type type() const { return m_type; }
52
53 // For Data type.
54 const ThreadSafeDataBuffer& data() const { return m_data; }
55
56 // For File type.
57 BlobDataFileReference* file() const { return m_file.get(); }
58
59 long long offset() const { return m_offset; }
60 WEBCORE_EXPORT long long length() const; // Computes file length if it's not known yet.
61
62private:
63 friend class BlobData;
64
65 explicit BlobDataItem(Ref<BlobDataFileReference>&& file)
66 : m_type(Type::File)
67 , m_file(WTFMove(file))
68 , m_offset(0)
69 , m_length(toEndOfFile)
70 {
71 }
72
73 BlobDataItem(ThreadSafeDataBuffer data, long long offset, long long length)
74 : m_type(Type::Data)
75 , m_data(data)
76 , m_offset(offset)
77 , m_length(length)
78 {
79 }
80
81 BlobDataItem(BlobDataFileReference* file, long long offset, long long length)
82 : m_type(Type::File)
83 , m_file(file)
84 , m_offset(offset)
85 , m_length(length)
86 {
87 }
88
89 Type m_type;
90 ThreadSafeDataBuffer m_data;
91 RefPtr<BlobDataFileReference> m_file;
92
93 long long m_offset;
94 long long m_length;
95};
96
97typedef Vector<BlobDataItem> BlobDataItemList;
98
99class BlobData : public ThreadSafeRefCounted<BlobData> {
100public:
101 static Ref<BlobData> create(const String& contentType)
102 {
103 return adoptRef(*new BlobData(contentType));
104 }
105
106 const String& contentType() const { return m_contentType; }
107
108 const BlobDataItemList& items() const { return m_items; }
109 void swapItems(BlobDataItemList&);
110
111 void appendData(const ThreadSafeDataBuffer&);
112 void appendFile(Ref<BlobDataFileReference>&&);
113
114private:
115 friend class BlobRegistryImpl;
116 BlobData(const String& contentType);
117
118 void appendData(const ThreadSafeDataBuffer&, long long offset, long long length);
119 void appendFile(BlobDataFileReference*, long long offset, long long length);
120
121 String m_contentType;
122 BlobDataItemList m_items;
123};
124
125} // namespace WebCore
126
127#endif // BlobData_h
128