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 "core/html/forms/RadioButtonGroupScope.h"
26#include "platform/heap/Handle.h"
27#include "wtf/Forward.h"
28#include "wtf/ListHashSet.h"
29#include "wtf/Vector.h"
30#include "wtf/text/AtomicStringHash.h"
31
32namespace blink {
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
75typedef HashMap<AtomicString, OwnPtr<SavedFormState> > SavedFormStateMap;
76
77class DocumentState FINAL : public RefCountedWillBeGarbageCollected<DocumentState> {
78    DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(DocumentState);
79public:
80    static PassRefPtrWillBeRawPtr<DocumentState> create();
81    void trace(Visitor*);
82
83    void addControl(HTMLFormControlElementWithState*);
84    void removeControl(HTMLFormControlElementWithState*);
85    Vector<String> toStateVector();
86
87private:
88    typedef WillBeHeapListHashSet<RefPtrWillBeMember<HTMLFormControlElementWithState>, 64> FormElementListHashSet;
89    FormElementListHashSet m_formControls;
90};
91
92class FormController FINAL : public NoBaseWillBeGarbageCollectedFinalized<FormController> {
93    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
94public:
95    static PassOwnPtrWillBeRawPtr<FormController> create()
96    {
97        return adoptPtrWillBeNoop(new FormController);
98    }
99    ~FormController();
100    void trace(Visitor*);
101
102    RadioButtonGroupScope& radioButtonGroupScope() { return m_radioButtonGroupScope; }
103
104    void registerStatefulFormControl(HTMLFormControlElementWithState&);
105    void unregisterStatefulFormControl(HTMLFormControlElementWithState&);
106    // This should be callled only by Document::formElementsState().
107    DocumentState* formElementsState() const;
108    // This should be callled only by Document::setStateForNewFormElements().
109    void setStateForNewFormElements(const Vector<String>&);
110    void willDeleteForm(HTMLFormElement*);
111    void restoreControlStateFor(HTMLFormControlElementWithState&);
112    void restoreControlStateIn(HTMLFormElement&);
113
114    static Vector<String> getReferencedFilePaths(const Vector<String>& stateVector);
115
116private:
117    FormController();
118    FormControlState takeStateForFormElement(const HTMLFormControlElementWithState&);
119    static void formStatesFromStateVector(const Vector<String>&, SavedFormStateMap&);
120
121    RadioButtonGroupScope m_radioButtonGroupScope;
122    RefPtrWillBeMember<DocumentState> m_documentState;
123    SavedFormStateMap m_savedFormStateMap;
124    OwnPtrWillBeMember<FormKeyGenerator> m_formKeyGenerator;
125};
126
127} // namespace blink
128#endif
129