1/*
2 * Copyright (C) 2005, 2006, 2008 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
21#ifndef FormDataList_h
22#define FormDataList_h
23
24#include "core/fileapi/Blob.h"
25#include "platform/heap/Handle.h"
26#include "platform/network/FormData.h"
27#include "wtf/Forward.h"
28#include "wtf/text/CString.h"
29#include "wtf/text/TextEncoding.h"
30
31namespace blink {
32
33class FormDataList : public RefCountedWillBeGarbageCollected<FormDataList> {
34    DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(FormDataList);
35public:
36    class Item {
37        ALLOW_ONLY_INLINE_ALLOCATION();
38    public:
39        Item() { }
40        Item(const WTF::CString& data) : m_data(data) { }
41        Item(PassRefPtrWillBeRawPtr<Blob> blob, const String& filename) : m_blob(blob), m_filename(filename) { }
42
43        const WTF::CString& data() const { return m_data; }
44        Blob* blob() const { return m_blob.get(); }
45        const String& filename() const { return m_filename; }
46
47        void trace(Visitor*);
48
49    private:
50        WTF::CString m_data;
51        RefPtrWillBeMember<Blob> m_blob;
52        String m_filename;
53    };
54
55    static PassRefPtrWillBeRawPtr<FormDataList> create(const WTF::TextEncoding& encoding)
56    {
57        return adoptRefWillBeNoop(new FormDataList(encoding));
58    }
59
60    void appendData(const String& key, const String& value)
61    {
62        appendString(key);
63        appendString(value);
64    }
65    void appendData(const String& key, const CString& value)
66    {
67        appendString(key);
68        appendString(value);
69    }
70    void appendData(const String& key, int value)
71    {
72        appendString(key);
73        appendString(String::number(value));
74    }
75    void appendBlob(const String& key, PassRefPtrWillBeRawPtr<Blob> blob, const String& filename = String())
76    {
77        appendString(key);
78        appendBlob(blob, filename);
79    }
80
81    const WillBeHeapVector<Item>& items() const { return m_items; }
82    const WTF::TextEncoding& encoding() const { return m_encoding; }
83
84    PassRefPtr<FormData> createFormData(FormData::EncodingType = FormData::FormURLEncoded);
85    PassRefPtr<FormData> createMultiPartFormData();
86
87    virtual void trace(Visitor*);
88
89protected:
90    explicit FormDataList(const WTF::TextEncoding&);
91
92private:
93    void appendKeyValuePairItemsTo(FormData*, const WTF::TextEncoding&, bool isMultiPartForm, FormData::EncodingType = FormData::FormURLEncoded);
94
95    void appendString(const CString&);
96    void appendString(const String&);
97    void appendBlob(PassRefPtrWillBeRawPtr<Blob>, const String& filename);
98
99    WTF::TextEncoding m_encoding;
100    WillBeHeapVector<Item> m_items;
101};
102
103} // namespace blink
104
105WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(blink::FormDataList::Item);
106
107#endif // FormDataList_h
108