Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / network / FormData.h
1 /*
2  * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef FormData_h
21 #define FormData_h
22
23 #include "platform/blob/BlobData.h"
24 #include "platform/weborigin/KURL.h"
25 #include "wtf/Forward.h"
26 #include "wtf/RefCounted.h"
27 #include "wtf/Vector.h"
28 #include "wtf/text/WTFString.h"
29
30 namespace blink {
31
32 class BlobDataHandle;
33
34 class PLATFORM_EXPORT FormDataElement {
35 public:
36     FormDataElement() : m_type(data) { }
37     explicit FormDataElement(const Vector<char>& array) : m_type(data), m_data(array) { }
38
39     FormDataElement(const String& filename, long long fileStart, long long fileLength, double expectedFileModificationTime) : m_type(encodedFile), m_filename(filename), m_fileStart(fileStart), m_fileLength(fileLength), m_expectedFileModificationTime(expectedFileModificationTime) { }
40     explicit FormDataElement(const String& blobUUID, PassRefPtr<BlobDataHandle> optionalHandle) : m_type(encodedBlob), m_blobUUID(blobUUID), m_optionalBlobDataHandle(optionalHandle) { }
41     FormDataElement(const KURL& fileSystemURL, long long start, long long length, double expectedFileModificationTime) : m_type(encodedFileSystemURL), m_fileSystemURL(fileSystemURL), m_fileStart(start), m_fileLength(length), m_expectedFileModificationTime(expectedFileModificationTime) { }
42
43     enum Type {
44         data,
45         encodedFile,
46         encodedBlob,
47         encodedFileSystemURL
48     } m_type;
49     Vector<char> m_data;
50     String m_filename;
51     String m_blobUUID;
52     RefPtr<BlobDataHandle> m_optionalBlobDataHandle;
53     KURL m_fileSystemURL;
54     long long m_fileStart;
55     long long m_fileLength;
56     double m_expectedFileModificationTime;
57 };
58
59 inline bool operator==(const FormDataElement& a, const FormDataElement& b)
60 {
61     if (&a == &b)
62         return true;
63
64     if (a.m_type != b.m_type)
65         return false;
66     if (a.m_type == FormDataElement::data)
67         return a.m_data == b.m_data;
68     if (a.m_type == FormDataElement::encodedFile)
69         return a.m_filename == b.m_filename && a.m_fileStart == b.m_fileStart && a.m_fileLength == b.m_fileLength && a.m_expectedFileModificationTime == b.m_expectedFileModificationTime;
70     if (a.m_type == FormDataElement::encodedBlob)
71         return a.m_blobUUID == b.m_blobUUID;
72     if (a.m_type == FormDataElement::encodedFileSystemURL)
73         return a.m_fileSystemURL == b.m_fileSystemURL;
74
75     return true;
76 }
77
78 inline bool operator!=(const FormDataElement& a, const FormDataElement& b)
79 {
80     return !(a == b);
81 }
82
83 class PLATFORM_EXPORT FormData : public RefCounted<FormData> {
84 public:
85     enum EncodingType {
86         FormURLEncoded, // for application/x-www-form-urlencoded
87         TextPlain, // for text/plain
88         MultipartFormData // for multipart/form-data
89     };
90
91     static PassRefPtr<FormData> create();
92     static PassRefPtr<FormData> create(const void*, size_t);
93     static PassRefPtr<FormData> create(const CString&);
94     static PassRefPtr<FormData> create(const Vector<char>&);
95     PassRefPtr<FormData> copy() const;
96     PassRefPtr<FormData> deepCopy() const;
97     ~FormData();
98
99     void appendData(const void* data, size_t);
100     void appendFile(const String& filePath);
101     void appendFileRange(const String& filename, long long start, long long length, double expectedModificationTime);
102     void appendBlob(const String& blobUUID, PassRefPtr<BlobDataHandle> optionalHandle);
103     void appendFileSystemURL(const KURL&);
104     void appendFileSystemURLRange(const KURL&, long long start, long long length, double expectedModificationTime);
105
106     void flatten(Vector<char>&) const; // omits files
107     String flattenToString() const; // omits files
108
109     bool isEmpty() const { return m_elements.isEmpty(); }
110     const Vector<FormDataElement>& elements() const { return m_elements; }
111
112     const Vector<char>& boundary() const { return m_boundary; }
113     void setBoundary(Vector<char> boundary) { m_boundary = boundary; }
114
115     bool alwaysStream() const { return m_alwaysStream; }
116     void setAlwaysStream(bool alwaysStream) { m_alwaysStream = alwaysStream; }
117
118     // Identifies a particular form submission instance.  A value of 0 is used
119     // to indicate an unspecified identifier.
120     void setIdentifier(int64_t identifier) { m_identifier = identifier; }
121     int64_t identifier() const { return m_identifier; }
122
123     bool containsPasswordData() const { return m_containsPasswordData; }
124     void setContainsPasswordData(bool containsPasswordData) { m_containsPasswordData = containsPasswordData; }
125
126     static EncodingType parseEncodingType(const String& type)
127     {
128         if (equalIgnoringCase(type, "text/plain"))
129             return TextPlain;
130         if (equalIgnoringCase(type, "multipart/form-data"))
131             return MultipartFormData;
132         return FormURLEncoded;
133     }
134
135     // Size of the elements making up the FormData.
136     unsigned long long sizeInBytes() const;
137
138 private:
139     FormData();
140     FormData(const FormData&);
141
142     Vector<FormDataElement> m_elements;
143
144     int64_t m_identifier;
145     bool m_alwaysStream;
146     Vector<char> m_boundary;
147     bool m_containsPasswordData;
148 };
149
150 inline bool operator==(const FormData& a, const FormData& b)
151 {
152     return a.elements() == b.elements();
153 }
154
155 inline bool operator!=(const FormData& a, const FormData& b)
156 {
157     return !(a == b);
158 }
159
160 } // namespace blink
161
162 #endif