Index: components/autofill/core/common/form_data.cc |
diff --git a/components/autofill/core/common/form_data.cc b/components/autofill/core/common/form_data.cc |
index e0c3c1c4636b04865eac11c23c0a124ca4533d5d..078a58d146aa4e541d4003ca87929748ff8308b4 100644 |
--- a/components/autofill/core/common/form_data.cc |
+++ b/components/autofill/core/common/form_data.cc |
@@ -4,10 +4,51 @@ |
#include "components/autofill/core/common/form_data.h" |
+#include "base/pickle.h" |
#include "base/strings/string_util.h" |
+#include "components/autofill/core/common/form_field_data.h" |
namespace autofill { |
+namespace { |
+ |
+const int kPickleVersion = 1; |
+ |
+bool ReadGURL(PickleIterator* iter, GURL* url) { |
+ std::string spec; |
+ if (!iter->ReadString(&spec)) |
+ return false; |
+ |
+ *url = GURL(spec); |
+ return true; |
+} |
+ |
+void SerializeFormFieldDataVector(const std::vector<FormFieldData> fields, |
+ Pickle* pickle) { |
+ pickle->WriteInt(static_cast<int>(fields.size())); |
+ for (size_t i = 0; i < fields.size(); ++i) { |
+ SerializeFormFieldData(fields[i], pickle); |
+ } |
+} |
+ |
+bool DeserializeFormFieldDataVector(PickleIterator* iter, |
+ std::vector<FormFieldData>* fields) { |
+ int size; |
+ if (!iter->ReadInt(&size)) |
+ return false; |
+ |
+ FormFieldData temp; |
+ for (int i = 0; i < size; ++i) { |
+ if (!DeserializeFormFieldData(iter, &temp)) |
+ return false; |
+ |
+ fields->push_back(temp); |
+ } |
+ return true; |
+} |
+ |
+} // namespace |
+ |
FormData::FormData() |
: user_submitted(false) { |
} |
@@ -37,4 +78,42 @@ bool FormData::operator!=(const FormData& form) const { |
return !operator==(form); |
} |
+void SerializeFormData(const FormData& form_data, Pickle* pickle) { |
+ pickle->WriteInt(kPickleVersion); |
+ pickle->WriteString16(form_data.name); |
+ pickle->WriteString16(form_data.method); |
+ pickle->WriteString(form_data.origin.spec()); |
+ pickle->WriteString(form_data.action.spec()); |
+ pickle->WriteBool(form_data.user_submitted); |
+ SerializeFormFieldDataVector(form_data.fields, pickle); |
+} |
+ |
+bool DeserializeFormData(PickleIterator* iter, FormData* form_data) { |
+ int version; |
+ if (!iter->ReadInt(&version)) { |
+ LOG(ERROR) << "Bad pickle of FormData, no version present"; |
+ return false; |
+ } |
+ |
+ switch (version) { |
+ case 1: { |
+ if (!iter->ReadString16(&form_data->name) || |
+ !iter->ReadString16(&form_data->method) || |
+ !ReadGURL(iter, &form_data->origin) || |
+ !ReadGURL(iter, &form_data->action) || |
+ !iter->ReadBool(&form_data->user_submitted) || |
+ !DeserializeFormFieldDataVector(iter, &form_data->fields)) { |
+ LOG(ERROR) << "Could not deserialize FormData from pickle"; |
+ return false; |
+ } |
+ break; |
+ } |
+ default: { |
+ LOG(ERROR) << "Unknown FormData pickle version " << version; |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
} // namespace autofill |