Home | History | Annotate | Download | only in network
      1 /*
      2  * Copyright (C) 2004, 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 #ifndef FormData_h
     21 #define FormData_h
     22 
     23 #include "PlatformString.h"
     24 #include <wtf/RefCounted.h>
     25 #include <wtf/Vector.h>
     26 
     27 namespace WebCore {
     28 
     29 class ChromeClient;
     30 
     31 class FormDataElement {
     32 public:
     33     FormDataElement() : m_type(data) { }
     34     FormDataElement(const Vector<char>& array) : m_type(data), m_data(array) { }
     35     FormDataElement(const String& filename, bool shouldGenerateFile) : m_type(encodedFile), m_filename(filename), m_shouldGenerateFile(shouldGenerateFile) { }
     36 
     37     enum { data, encodedFile } m_type;
     38     Vector<char> m_data;
     39     String m_filename;
     40     String m_generatedFilename;
     41     bool m_shouldGenerateFile;
     42 };
     43 
     44 inline bool operator==(const FormDataElement& a, const FormDataElement& b)
     45 {
     46     if (&a == &b)
     47         return true;
     48 
     49     if (a.m_type != b.m_type)
     50         return false;
     51     if (a.m_data != b.m_data)
     52         return false;
     53     if (a.m_filename != b.m_filename)
     54         return false;
     55 
     56     return true;
     57 }
     58 
     59 inline bool operator!=(const FormDataElement& a, const FormDataElement& b)
     60 {
     61     return !(a == b);
     62 }
     63 
     64 class FormData : public RefCounted<FormData> {
     65 public:
     66     static PassRefPtr<FormData> create();
     67     static PassRefPtr<FormData> create(const void*, size_t);
     68     static PassRefPtr<FormData> create(const CString&);
     69     static PassRefPtr<FormData> create(const Vector<char>&);
     70     PassRefPtr<FormData> copy() const;
     71     PassRefPtr<FormData> deepCopy() const;
     72     ~FormData();
     73 
     74     void appendData(const void* data, size_t);
     75     void appendFile(const String& filename, bool shouldGenerateFile = false);
     76 
     77     void flatten(Vector<char>&) const; // omits files
     78     String flattenToString() const; // omits files
     79 
     80     bool isEmpty() const { return m_elements.isEmpty(); }
     81     const Vector<FormDataElement>& elements() const { return m_elements; }
     82 
     83     void generateFiles(ChromeClient*);
     84     void removeGeneratedFilesIfNeeded();
     85 
     86     bool alwaysStream() const { return m_alwaysStream; }
     87     void setAlwaysStream(bool alwaysStream) { m_alwaysStream = alwaysStream; }
     88 
     89     // Identifies a particular form submission instance.  A value of 0 is used
     90     // to indicate an unspecified identifier.
     91     void setIdentifier(int64_t identifier) { m_identifier = identifier; }
     92     int64_t identifier() const { return m_identifier; }
     93 
     94 private:
     95     FormData();
     96     FormData(const FormData&);
     97 
     98     Vector<FormDataElement> m_elements;
     99     int64_t m_identifier;
    100     bool m_hasGeneratedFiles;
    101     bool m_alwaysStream;
    102 };
    103 
    104 inline bool operator==(const FormData& a, const FormData& b)
    105 {
    106     return a.elements() == b.elements();
    107 }
    108 
    109 inline bool operator!=(const FormData& a, const FormData& b)
    110 {
    111     return a.elements() != b.elements();
    112 }
    113 
    114 } // namespace WebCore
    115 
    116 #endif
    117