17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/form_data.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/pickle.h" 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h" 9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/strings/utf_string_conversions.h" 10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "components/autofill/core/common/form_field_data.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace autofill { 13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace { 15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 1603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)const int kPickleVersion = 2; 17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)bool ReadGURL(PickleIterator* iter, GURL* url) { 19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) std::string spec; 20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (!iter->ReadString(&spec)) 21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return false; 22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) *url = GURL(spec); 24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return true; 25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SerializeFormFieldDataVector(const std::vector<FormFieldData>& fields, 28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Pickle* pickle) { 29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) pickle->WriteInt(static_cast<int>(fields.size())); 30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) for (size_t i = 0; i < fields.size(); ++i) { 31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) SerializeFormFieldData(fields[i], pickle); 32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)bool DeserializeFormFieldDataVector(PickleIterator* iter, 36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) std::vector<FormFieldData>* fields) { 37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int size; 38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (!iter->ReadInt(&size)) 39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return false; 40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) FormFieldData temp; 42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) for (int i = 0; i < size; ++i) { 43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (!DeserializeFormFieldData(iter, &temp)) 44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return false; 45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) fields->push_back(temp); 47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return true; 49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 5103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void LogDeserializationError(int version) { 5203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) DVLOG(1) << "Could not deserialize version " << version 5303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) << " FormData from pickle."; 5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} 5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} // namespace 57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)FormData::FormData() 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : user_submitted(false) { 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)FormData::FormData(const FormData& data) 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : name(data.name), 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) origin(data.origin), 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) action(data.action), 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) user_submitted(data.user_submitted), 677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch fields(data.fields) { 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)FormData::~FormData() { 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool FormData::operator==(const FormData& form) const { 747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch return name == form.name && 757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch origin == form.origin && 767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch action == form.action && 777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch user_submitted == form.user_submitted && 787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch fields == form.fields; 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool FormData::operator!=(const FormData& form) const { 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return !operator==(form); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool FormData::operator<(const FormData& form) const { 86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (name != form.name) 87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return name < form.name; 88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (origin != form.origin) 89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return origin < form.origin; 90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (action != form.action) 91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return action < form.action; 92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (user_submitted != form.user_submitted) 93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return user_submitted < form.user_submitted; 94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return fields < form.fields; 95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 97d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)std::ostream& operator<<(std::ostream& os, const FormData& form) { 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) os << base::UTF16ToUTF8(form.name) << " " 99d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) << form.origin << " " 100d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) << form.action << " " 101d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) << form.user_submitted << " " 102d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) << "Fields:"; 103d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) for (size_t i = 0; i < form.fields.size(); ++i) { 104d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) os << form.fields[i] << ","; 105d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return os; 107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 108d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void SerializeFormData(const FormData& form_data, Pickle* pickle) { 110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) pickle->WriteInt(kPickleVersion); 111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) pickle->WriteString16(form_data.name); 112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) pickle->WriteString(form_data.origin.spec()); 113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) pickle->WriteString(form_data.action.spec()); 114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) pickle->WriteBool(form_data.user_submitted); 115424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) SerializeFormFieldDataVector(form_data.fields, pickle); 116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 117424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 118424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)bool DeserializeFormData(PickleIterator* iter, FormData* form_data) { 119424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int version; 120424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (!iter->ReadInt(&version)) { 12103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) DVLOG(1) << "Bad pickle of FormData, no version present"; 122424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return false; 123424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 124424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 125424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) switch (version) { 126424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) case 1: { 12703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) base::string16 method; 128424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (!iter->ReadString16(&form_data->name) || 12903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) !iter->ReadString16(&method) || 130424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) !ReadGURL(iter, &form_data->origin) || 131424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) !ReadGURL(iter, &form_data->action) || 132424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) !iter->ReadBool(&form_data->user_submitted) || 133424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) !DeserializeFormFieldDataVector(iter, &form_data->fields)) { 13403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) LogDeserializationError(version); 135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return false; 136424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) break; 138424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 13903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) case 2: 14003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) if (!iter->ReadString16(&form_data->name) || 14103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) !ReadGURL(iter, &form_data->origin) || 14203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) !ReadGURL(iter, &form_data->action) || 14303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) !iter->ReadBool(&form_data->user_submitted) || 14403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) !DeserializeFormFieldDataVector(iter, &form_data->fields)) { 14503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) LogDeserializationError(version); 14603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return false; 14703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 14803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) break; 149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) default: { 15003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) DVLOG(1) << "Unknown FormData pickle version " << version; 151424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return false; 152424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 153424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 154424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return true; 155424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 156424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} // namespace autofill 158