1/*
2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2010, 2011, 2012 Google Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef FormController_h
23#define FormController_h
24
25#include "CheckedRadioButtons.h"
26#include <wtf/Deque.h>
27#include <wtf/Forward.h>
28#include <wtf/ListHashSet.h>
29#include <wtf/Vector.h>
30#include <wtf/text/WTFString.h>
31
32namespace WebCore {
33
34class FormAssociatedElement;
35class FormKeyGenerator;
36class HTMLFormControlElementWithState;
37class HTMLFormElement;
38class SavedFormState;
39
40class FormControlState {
41public:
42    FormControlState() : m_type(TypeSkip) { }
43    explicit FormControlState(const String& value) : m_type(TypeRestore) { m_values.append(value); }
44    static FormControlState deserialize(const Vector<String>& stateVector, size_t& index);
45    FormControlState(const FormControlState& another) : m_type(another.m_type), m_values(another.m_values) { }
46    FormControlState& operator=(const FormControlState&);
47
48    bool isFailure() const { return m_type == TypeFailure; }
49    size_t valueSize() const { return m_values.size(); }
50    const String& operator[](size_t i) const { return m_values[i]; }
51    void append(const String&);
52    void serializeTo(Vector<String>& stateVector) const;
53
54private:
55    enum Type { TypeSkip, TypeRestore, TypeFailure };
56    explicit FormControlState(Type type) : m_type(type) { }
57
58    Type m_type;
59    Vector<String> m_values;
60};
61
62inline FormControlState& FormControlState::operator=(const FormControlState& another)
63{
64    m_type = another.m_type;
65    m_values = another.m_values;
66    return *this;
67}
68
69inline void FormControlState::append(const String& value)
70{
71    m_type = TypeRestore;
72    m_values.append(value);
73}
74
75class FormController {
76    WTF_MAKE_FAST_ALLOCATED;
77public:
78    static PassOwnPtr<FormController> create()
79    {
80        return adoptPtr(new FormController);
81    }
82    ~FormController();
83
84    CheckedRadioButtons& checkedRadioButtons() { return m_checkedRadioButtons; }
85
86    void registerFormElementWithState(HTMLFormControlElementWithState*);
87    void unregisterFormElementWithState(HTMLFormControlElementWithState*);
88    // This should be callled only by Document::formElementsState().
89    Vector<String> formElementsState() const;
90    // This should be callled only by Document::setStateForNewFormElements().
91    void setStateForNewFormElements(const Vector<String>&);
92    void willDeleteForm(HTMLFormElement*);
93    void restoreControlStateFor(HTMLFormControlElementWithState&);
94    void restoreControlStateIn(HTMLFormElement&);
95
96    static Vector<String> getReferencedFilePaths(const Vector<String>& stateVector);
97
98private:
99    typedef ListHashSet<RefPtr<HTMLFormControlElementWithState>, 64> FormElementListHashSet;
100    typedef HashMap<RefPtr<AtomicStringImpl>, OwnPtr<SavedFormState> > SavedFormStateMap;
101
102    FormController();
103    static PassOwnPtr<SavedFormStateMap> createSavedFormStateMap(const FormElementListHashSet&);
104    FormControlState takeStateForFormElement(const HTMLFormControlElementWithState&);
105    static void formStatesFromStateVector(const Vector<String>&, SavedFormStateMap&);
106
107    CheckedRadioButtons m_checkedRadioButtons;
108    FormElementListHashSet m_formElementsWithState;
109    SavedFormStateMap m_savedFormStateMap;
110    OwnPtr<FormKeyGenerator> m_formKeyGenerator;
111};
112
113} // namespace WebCore
114#endif
115