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

Unified Diff: Source/core/html/FormDataList.cpp

Issue 564963002: New FormData methods: get, getAll, has, set, delete and iterable (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Implement getAll(), fix get() with no matches 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 side-by-side diff with in-line comments
Download patch
« Source/core/html/FormDataList.h ('K') | « Source/core/html/FormDataList.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/html/FormDataList.cpp
diff --git a/Source/core/html/FormDataList.cpp b/Source/core/html/FormDataList.cpp
index f0d1e1c06229a945f3bcde178935940653043583..2c62b3daf5b239f95f8bf11a2bac8b1480ef8113 100644
--- a/Source/core/html/FormDataList.cpp
+++ b/Source/core/html/FormDataList.cpp
@@ -24,6 +24,7 @@
#include "core/fileapi/File.h"
#include "platform/network/FormDataBuilder.h"
#include "platform/text/LineEnding.h"
+#include "wtf/CurrentTime.h"
namespace blink {
@@ -34,8 +35,7 @@ FormDataList::FormDataList(const WTF::TextEncoding& c)
void FormDataList::appendString(const String& string)
{
- CString encodedString = m_encoding.encode(string, WTF::EntitiesForUnencodables);
- m_items.append(normalizeLineEndingsToCRLF(encodedString));
+ m_items.append(encodeAndNormalize(string));
}
void FormDataList::appendString(const CString& string)
@@ -48,6 +48,124 @@ void FormDataList::appendBlob(PassRefPtrWillBeRawPtr<Blob> blob, const String& f
m_items.append(Item(blob, filename));
}
+void FormDataList::deleteItem(const String& key)
+{
+ const CString keyData = encodeAndNormalize(key);
+ ASSERT(!(m_items.size() % 2));
+ size_t i = 0;
+ while (i < m_items.size()) {
+ if (m_items[i].data() == keyData) {
+ m_items.remove(i, 2);
+ } else {
+ i += 2;
+ }
+ }
+ ASSERT(!(m_items.size() % 2));
+ return;
+}
+
+FormDataList::EntryValue FormDataList::getItem(const String& key) const
+{
+ const CString keyData = encodeAndNormalize(key);
+ const WillBeHeapVector<Item>& items = this->items();
+ size_t formDataListSize = items.size();
+ ASSERT(!(formDataListSize % 2));
+ for (size_t i = 0; i < formDataListSize; i += 2) {
+ const FormDataList::Item& key = items[i];
+ if (key.data() != keyData)
+ continue;
+ const FormDataList::Item& value = items[i + 1];
+ return itemToEntryValue(value);
+ }
+ return EntryValue();
+}
+
+Vector<FormDataList::EntryValue> FormDataList::getAll(const String& key) const
+{
+ Vector<FormDataList::EntryValue> matches;
sof 2014/09/12 15:21:26 Vector => WillBeHeapVector
jsbell 2014/09/12 16:30:09 Done.
+
+ const CString keyData = encodeAndNormalize(key);
+ const WillBeHeapVector<Item>& items = this->items();
+ size_t formDataListSize = items.size();
+ ASSERT(!(formDataListSize % 2));
+ for (size_t i = 0; i < formDataListSize; i += 2) {
+ const FormDataList::Item& key = items[i];
+ if (key.data() != keyData)
+ continue;
+ const FormDataList::Item& value = items[i + 1];
+ matches.append(itemToEntryValue(value));
+ }
+
+ return matches;
+}
+
+FormDataList::EntryValue FormDataList::itemToEntryValue(const FormDataList::Item& value) const
+{
+ if (!value.blob()) {
+ const CString data = value.data();
+ return EntryValue(m_encoding.decode(data.data(), data.length()));
+ }
+
+ if (value.blob()->isFile()) {
+ // FIXME: Overwrite file's name with value.filename().
+ return EntryValue(toFile(value.blob()));
+ }
+
+ String filename = value.filename();
+ if (filename.isNull())
+ filename = "blob";
+ return EntryValue(File::create(filename, currentTimeMS(), value.blob()->blobDataHandle()));
tyoshino (SeeGerritForStatus) 2014/09/12 07:29:32 this looks equivalent to what's specified in the s
jsbell 2014/09/12 16:30:09 Thanks!
+}
+
+bool FormDataList::hasItem(const String& key) const
+{
+ const CString keyData = encodeAndNormalize(key);
+ const WillBeHeapVector<Item>& items = this->items();
+ size_t formDataListSize = items.size();
+ ASSERT(!(formDataListSize % 2));
+ for (size_t i = 0; i < formDataListSize; i += 2) {
+ const FormDataList::Item& key = items[i];
+ if (key.data() == keyData)
+ return true;
+ }
+ return false;
+}
+
+void FormDataList::setBlob(const String& key, PassRefPtrWillBeRawPtr<Blob> blob, const String& filename)
+{
+ setItem(key, Item(blob, filename));
+}
+
+void FormDataList::setData(const String& key, const String& value)
+{
+ setItem(key, encodeAndNormalize(value));
+}
+
+void FormDataList::setItem(const String& key, const Item& item)
+{
+ const CString keyData = encodeAndNormalize(key);
+ ASSERT(!(m_items.size() % 2));
+ bool found = false;
+ size_t i = 0;
+ while (i < m_items.size()) {
+ if (m_items[i].data() != keyData) {
+ i += 2;
+ } else if (found) {
+ m_items.remove(i, 2);
+ } else {
+ found = true;
+ m_items[i + 1] = item;
+ i += 2;
+ }
+ }
+ if (!found) {
+ m_items.append(keyData);
+ m_items.append(item);
+ }
+ ASSERT(!(m_items.size() % 2));
+ return;
+}
+
PassRefPtr<FormData> FormDataList::createFormData(FormData::EncodingType encodingType)
{
RefPtr<FormData> result = FormData::create();
@@ -140,6 +258,12 @@ void FormDataList::appendKeyValuePairItemsTo(FormData* formData, const WTF::Text
formData->appendData(encodedData.data(), encodedData.size());
}
+CString FormDataList::encodeAndNormalize(const String& string) const
+{
+ CString encodedString = m_encoding.encode(string, WTF::EntitiesForUnencodables);
+ return normalizeLineEndingsToCRLF(encodedString);
+}
+
void FormDataList::trace(Visitor* visitor)
{
visitor->trace(m_items);
« Source/core/html/FormDataList.h ('K') | « Source/core/html/FormDataList.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698