form_data.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "components/autofill/core/common/form_data.h" 6 7#include "base/pickle.h" 8#include "base/strings/string_util.h" 9#include "base/strings/utf_string_conversions.h" 10#include "components/autofill/core/common/form_field_data.h" 11 12namespace autofill { 13 14namespace { 15 16const int kPickleVersion = 2; 17 18bool ReadGURL(PickleIterator* iter, GURL* url) { 19 std::string spec; 20 if (!iter->ReadString(&spec)) 21 return false; 22 23 *url = GURL(spec); 24 return true; 25} 26 27void SerializeFormFieldDataVector(const std::vector<FormFieldData>& fields, 28 Pickle* pickle) { 29 pickle->WriteInt(static_cast<int>(fields.size())); 30 for (size_t i = 0; i < fields.size(); ++i) { 31 SerializeFormFieldData(fields[i], pickle); 32 } 33} 34 35bool DeserializeFormFieldDataVector(PickleIterator* iter, 36 std::vector<FormFieldData>* fields) { 37 int size; 38 if (!iter->ReadInt(&size)) 39 return false; 40 41 FormFieldData temp; 42 for (int i = 0; i < size; ++i) { 43 if (!DeserializeFormFieldData(iter, &temp)) 44 return false; 45 46 fields->push_back(temp); 47 } 48 return true; 49} 50 51void LogDeserializationError(int version) { 52 DVLOG(1) << "Could not deserialize version " << version 53 << " FormData from pickle."; 54} 55 56} // namespace 57 58FormData::FormData() 59 : user_submitted(false) { 60} 61 62FormData::FormData(const FormData& data) 63 : name(data.name), 64 origin(data.origin), 65 action(data.action), 66 user_submitted(data.user_submitted), 67 fields(data.fields) { 68} 69 70FormData::~FormData() { 71} 72 73bool FormData::operator==(const FormData& form) const { 74 return name == form.name && 75 origin == form.origin && 76 action == form.action && 77 user_submitted == form.user_submitted && 78 fields == form.fields; 79} 80 81bool FormData::operator!=(const FormData& form) const { 82 return !operator==(form); 83} 84 85bool FormData::operator<(const FormData& form) const { 86 if (name != form.name) 87 return name < form.name; 88 if (origin != form.origin) 89 return origin < form.origin; 90 if (action != form.action) 91 return action < form.action; 92 if (user_submitted != form.user_submitted) 93 return user_submitted < form.user_submitted; 94 return fields < form.fields; 95} 96 97std::ostream& operator<<(std::ostream& os, const FormData& form) { 98 os << base::UTF16ToUTF8(form.name) << " " 99 << form.origin << " " 100 << form.action << " " 101 << form.user_submitted << " " 102 << "Fields:"; 103 for (size_t i = 0; i < form.fields.size(); ++i) { 104 os << form.fields[i] << ","; 105 } 106 return os; 107} 108 109void SerializeFormData(const FormData& form_data, Pickle* pickle) { 110 pickle->WriteInt(kPickleVersion); 111 pickle->WriteString16(form_data.name); 112 pickle->WriteString(form_data.origin.spec()); 113 pickle->WriteString(form_data.action.spec()); 114 pickle->WriteBool(form_data.user_submitted); 115 SerializeFormFieldDataVector(form_data.fields, pickle); 116} 117 118bool DeserializeFormData(PickleIterator* iter, FormData* form_data) { 119 int version; 120 if (!iter->ReadInt(&version)) { 121 DVLOG(1) << "Bad pickle of FormData, no version present"; 122 return false; 123 } 124 125 switch (version) { 126 case 1: { 127 base::string16 method; 128 if (!iter->ReadString16(&form_data->name) || 129 !iter->ReadString16(&method) || 130 !ReadGURL(iter, &form_data->origin) || 131 !ReadGURL(iter, &form_data->action) || 132 !iter->ReadBool(&form_data->user_submitted) || 133 !DeserializeFormFieldDataVector(iter, &form_data->fields)) { 134 LogDeserializationError(version); 135 return false; 136 } 137 break; 138 } 139 case 2: 140 if (!iter->ReadString16(&form_data->name) || 141 !ReadGURL(iter, &form_data->origin) || 142 !ReadGURL(iter, &form_data->action) || 143 !iter->ReadBool(&form_data->user_submitted) || 144 !DeserializeFormFieldDataVector(iter, &form_data->fields)) { 145 LogDeserializationError(version); 146 return false; 147 } 148 break; 149 default: { 150 DVLOG(1) << "Unknown FormData pickle version " << version; 151 return false; 152 } 153 } 154 return true; 155} 156 157} // namespace autofill 158