Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / forms / FormController.h
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 blink {
33
34 class FormKeyGenerator;
35 class HTMLFormControlElementWithState;
36 class HTMLFormElement;
37 class SavedFormState;
38
39 class FormControlState {
40 public:
41     FormControlState() : m_type(TypeSkip) { }
42     explicit FormControlState(const String& value) : m_type(TypeRestore) { m_values.append(value); }
43     static FormControlState deserialize(const Vector<String>& stateVector, size_t& index);
44     FormControlState(const FormControlState& another) : m_type(another.m_type), m_values(another.m_values) { }
45     FormControlState& operator=(const FormControlState&);
46
47     bool isFailure() const { return m_type == TypeFailure; }
48     size_t valueSize() const { return m_values.size(); }
49     const String& operator[](size_t i) const { return m_values[i]; }
50     void append(const String&);
51     void serializeTo(Vector<String>& stateVector) const;
52
53 private:
54     enum Type { TypeSkip, TypeRestore, TypeFailure };
55     explicit FormControlState(Type type) : m_type(type) { }
56
57     Type m_type;
58     Vector<String> m_values;
59 };
60
61 inline FormControlState& FormControlState::operator=(const FormControlState& another)
62 {
63     m_type = another.m_type;
64     m_values = another.m_values;
65     return *this;
66 }
67
68 inline void FormControlState::append(const String& value)
69 {
70     m_type = TypeRestore;
71     m_values.append(value);
72 }
73
74 typedef HashMap<AtomicString, OwnPtr<SavedFormState>> SavedFormStateMap;
75
76 class DocumentState final : public RefCountedWillBeGarbageCollected<DocumentState> {
77     DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(DocumentState);
78 public:
79     static PassRefPtrWillBeRawPtr<DocumentState> create();
80     void trace(Visitor*);
81
82     void addControl(HTMLFormControlElementWithState*);
83     void removeControl(HTMLFormControlElementWithState*);
84     Vector<String> toStateVector();
85
86 private:
87     typedef WillBeHeapListHashSet<RefPtrWillBeMember<HTMLFormControlElementWithState>, 64> FormElementListHashSet;
88     FormElementListHashSet m_formControls;
89 };
90
91 class FormController final : public NoBaseWillBeGarbageCollectedFinalized<FormController> {
92     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
93 public:
94     static PassOwnPtrWillBeRawPtr<FormController> create()
95     {
96         return adoptPtrWillBeNoop(new FormController);
97     }
98     ~FormController();
99     void trace(Visitor*);
100
101     RadioButtonGroupScope& radioButtonGroupScope() { return m_radioButtonGroupScope; }
102
103     void registerStatefulFormControl(HTMLFormControlElementWithState&);
104     void unregisterStatefulFormControl(HTMLFormControlElementWithState&);
105     // This should be callled only by Document::formElementsState().
106     DocumentState* formElementsState() const;
107     // This should be callled only by Document::setStateForNewFormElements().
108     void setStateForNewFormElements(const Vector<String>&);
109     void willDeleteForm(HTMLFormElement*);
110     void restoreControlStateFor(HTMLFormControlElementWithState&);
111     void restoreControlStateIn(HTMLFormElement&);
112
113     static Vector<String> getReferencedFilePaths(const Vector<String>& stateVector);
114
115 private:
116     FormController();
117     FormControlState takeStateForFormElement(const HTMLFormControlElementWithState&);
118     static void formStatesFromStateVector(const Vector<String>&, SavedFormStateMap&);
119
120     RadioButtonGroupScope m_radioButtonGroupScope;
121     RefPtrWillBeMember<DocumentState> m_documentState;
122     SavedFormStateMap m_savedFormStateMap;
123     OwnPtrWillBeMember<FormKeyGenerator> m_formKeyGenerator;
124 };
125
126 } // namespace blink
127 #endif