Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: Source/core/html/DOMFormData.cpp

Issue 564963002: New FormData methods: get, getAll, has, set, delete and iterable (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Stuff new methods behind a flag Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "core/html/DOMFormData.h" 32 #include "core/html/DOMFormData.h"
33 33
34 #include "bindings/core/v8/V8Binding.h"
35 #include "bindings/core/v8/V8File.h"
36 #include "core/dom/Iterator.h"
34 #include "core/fileapi/Blob.h" 37 #include "core/fileapi/Blob.h"
38 #include "core/fileapi/File.h"
35 #include "core/html/HTMLFormElement.h" 39 #include "core/html/HTMLFormElement.h"
36 #include "wtf/text/TextEncoding.h" 40 #include "wtf/text/TextEncoding.h"
37 #include "wtf/text/WTFString.h" 41 #include "wtf/text/WTFString.h"
38 42
39 namespace blink { 43 namespace blink {
40 44
45 namespace {
46
47 ScriptValue entryValueToScriptValue(ScriptState* scriptState, const FormDataList ::Entry& entry)
48 {
49 if (entry.isString())
50 return ScriptValue(scriptState, v8String(scriptState->isolate(), entry.s tring()));
51 if (entry.isFile())
52 return ScriptValue(scriptState, toV8(entry.file(), scriptState->context( )->Global(), scriptState->isolate()));
53 ASSERT(entry.isNone());
54 return ScriptValue(scriptState, v8::Null(scriptState->isolate()));
55 }
56
57 class DOMFormDataIterator FINAL : public Iterator {
58 public:
59 enum IterationType { KeyValue, Key, Value };
60
61 DOMFormDataIterator(DOMFormData* formData, IterationType type) : m_formData( formData), m_type(type), m_current(0) { }
62
63 virtual ScriptValue next(ScriptState* scriptState, ExceptionState& exception State) OVERRIDE
64 {
65 // FIXME: This neither snapshots nor handles deletion during iteration
66 // the same way as the ES6 spec (i.e. with 'empty' slots). Removing
67 // entries at/behind the iterator position will result in later
68 // entries being skipped.
arv (Not doing code reviews) 2014/09/17 19:49:38 That is pretty unfortunate. Can we make sure this
jsbell 2014/09/18 16:18:59 Agreed. I think we need to spec to stabilize, to
69
70 v8::Isolate* isolate = scriptState->isolate();
71 if (m_current >= m_formData->size())
72 return ScriptValue(scriptState, v8DoneIteratorResult(isolate));
73
74 const FormDataList::Entry entry = m_formData->getEntry(m_current++);
75 ASSERT(!entry.isNone());
76 ScriptValue name, value;
77 if (m_type != Value)
78 name = ScriptValue(scriptState, v8String(scriptState->isolate(), ent ry.name()));
79 if (m_type != Key)
80 value = entryValueToScriptValue(scriptState, entry);
81
82 switch (m_type) {
83 case KeyValue: {
84 Vector<ScriptValue> pair;
85 pair.append(name);
86 pair.append(value);
87 return ScriptValue(scriptState, v8IteratorResult(scriptState, pair)) ;
88 }
89 case Key:
90 return ScriptValue(scriptState, v8IteratorResult(scriptState, name)) ;
91
92 case Value:
93 return ScriptValue(scriptState, v8IteratorResult(scriptState, value) );
94 }
95 ASSERT_NOT_REACHED();
96 return ScriptValue();
97 }
98
99 virtual ScriptValue next(ScriptState* scriptState, ScriptValue value, Except ionState& exceptionState) OVERRIDE
100 {
101 // FIXME: Implement the "bring your own payload object" variant of next( ).
arv (Not doing code reviews) 2014/09/17 19:49:38 Not sure what this means?
jsbell 2014/09/18 16:18:59 Huh... I thought at one point you could call iter.
arv (Not doing code reviews) 2014/09/18 17:33:45 You should not reject an argument, just ignore it.
jsbell 2014/09/18 23:56:29 Got it. I was looking at the InternalsIterator for
102 exceptionState.throwTypeError("Not implemented");
103 return ScriptValue();
104 }
105
106 virtual void trace(Visitor* visitor)
107 {
108 Iterator::trace(visitor);
109 visitor->trace(m_formData);
110 }
111
112 private:
113 const RefPtrWillBeMember<DOMFormData> m_formData;
114 const IterationType m_type;
115 size_t m_current;
116 };
117
118 } // namespace
119
41 DOMFormData::DOMFormData(const WTF::TextEncoding& encoding) 120 DOMFormData::DOMFormData(const WTF::TextEncoding& encoding)
42 : FormDataList(encoding) 121 : FormDataList(encoding)
43 { 122 {
44 } 123 }
45 124
46 DOMFormData::DOMFormData(HTMLFormElement* form) 125 DOMFormData::DOMFormData(HTMLFormElement* form)
47 : FormDataList(UTF8Encoding()) 126 : FormDataList(UTF8Encoding())
48 { 127 {
49 if (!form) 128 if (!form)
50 return; 129 return;
(...skipping 10 matching lines...) Expand all
61 if (!name.isEmpty()) 140 if (!name.isEmpty())
62 appendData(name, value); 141 appendData(name, value);
63 } 142 }
64 143
65 void DOMFormData::append(const String& name, Blob* blob, const String& filename) 144 void DOMFormData::append(const String& name, Blob* blob, const String& filename)
66 { 145 {
67 if (!name.isEmpty()) 146 if (!name.isEmpty())
68 appendBlob(name, blob, filename); 147 appendBlob(name, blob, filename);
69 } 148 }
70 149
150 void DOMFormData::remove(const String& name)
arv (Not doing code reviews) 2014/09/17 19:49:38 Why this extra indirection? Can you just use imple
jsbell 2014/09/18 16:18:58 Yeah, this doesn't gain anything. I'll flatten thi
151 {
152 return deleteEntry(name);
153 }
154
155 ScriptValue DOMFormData::get(ScriptState* scriptState, const String& name)
156 {
157 return entryValueToScriptValue(scriptState, getEntry(name));
158 }
159
160 Vector<ScriptValue> DOMFormData::getAll(ScriptState* scriptState, const String& name)
161 {
162 Vector<ScriptValue> results;
163 WillBeHeapVector<FormDataList::Entry> entries = FormDataList::getAll(name);
164 for (size_t i = 0; i < entries.size(); ++i) {
165 const FormDataList::Entry& entry = entries[i];
166 ASSERT(!entry.isNone());
167 ASSERT(entry.name() == name);
168 results.append(entryValueToScriptValue(scriptState, entry));
169 }
170 ASSERT(results.size() == entries.size());
171 return results;
172 }
173
174 bool DOMFormData::has(const String& name)
175 {
176 return hasEntry(name);
arv (Not doing code reviews) 2014/09/17 19:49:38 Maybe use implementedAs in the IDL here too?
jsbell 2014/09/18 16:18:58 Ditto.
177 }
178
179 void DOMFormData::set(const String& name, const String& value)
180 {
181 if (!name.isEmpty())
182 setData(name, value);
183 }
184
185 void DOMFormData::set(const String& name, Blob* blob, const String& filename)
186 {
187 if (!name.isEmpty())
188 setBlob(name, blob, filename);
189 }
190
191 Iterator* DOMFormData::entries()
192 {
193 return new DOMFormDataIterator(this, DOMFormDataIterator::KeyValue);
194 }
195
196 Iterator* DOMFormData::keys()
197 {
198 return new DOMFormDataIterator(this, DOMFormDataIterator::Key);
199 }
200
201 Iterator* DOMFormData::values()
202 {
203 return new DOMFormDataIterator(this, DOMFormDataIterator::Value);
204 }
205
71 } // namespace blink 206 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698