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
32 namespace WebCore {
33
34 class FormAssociatedElement;
35 class FormKeyGenerator;
36 class HTMLFormControlElementWithState;
37 class HTMLFormElement;
38 class SavedFormState;
39
40 class FormControlState {
41 public:
FormControlState()42 FormControlState() : m_type(TypeSkip) { }
FormControlState(const String & value)43 explicit FormControlState(const String& value) : m_type(TypeRestore) { m_values.append(value); }
44 static FormControlState deserialize(const Vector<String>& stateVector, size_t& index);
FormControlState(const FormControlState & another)45 FormControlState(const FormControlState& another) : m_type(another.m_type), m_values(another.m_values) { }
46 FormControlState& operator=(const FormControlState&);
47
isFailure()48 bool isFailure() const { return m_type == TypeFailure; }
valueSize()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
54 private:
55 enum Type { TypeSkip, TypeRestore, TypeFailure };
FormControlState(Type type)56 explicit FormControlState(Type type) : m_type(type) { }
57
58 Type m_type;
59 Vector<String> m_values;
60 };
61
62 inline FormControlState& FormControlState::operator=(const FormControlState& another)
63 {
64 m_type = another.m_type;
65 m_values = another.m_values;
66 return *this;
67 }
68
append(const String & value)69 inline void FormControlState::append(const String& value)
70 {
71 m_type = TypeRestore;
72 m_values.append(value);
73 }
74
75 typedef HashMap<AtomicString, OwnPtr<SavedFormState> > SavedFormStateMap;
76
77 class DocumentState FINAL : public RefCountedWillBeGarbageCollected<DocumentState> {
78 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(DocumentState);
79 public:
80 static PassRefPtrWillBeRawPtr<DocumentState> create();
81 void trace(Visitor*);
82
83 void addControl(HTMLFormControlElementWithState*);
84 void removeControl(HTMLFormControlElementWithState*);
85 Vector<String> toStateVector();
86
87 private:
88 typedef WillBeHeapListHashSet<RefPtrWillBeMember<HTMLFormControlElementWithState>, 64> FormElementListHashSet;
89 FormElementListHashSet m_formControls;
90 };
91
92 class FormController FINAL : public NoBaseWillBeGarbageCollectedFinalized<FormController> {
93 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
94 public:
create()95 static PassOwnPtrWillBeRawPtr<FormController> create()
96 {
97 return adoptPtrWillBeNoop(new FormController);
98 }
99 ~FormController();
100 void trace(Visitor*);
101
radioButtonGroupScope()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
116 private:
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 WebCore
128 #endif
129