Home | History | Annotate | Download | only in common
      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_field_data.h"
      6 
      7 #include "base/pickle.h"
      8 #include "base/strings/string_util.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 
     11 namespace {
     12 
     13 const int kPickleVersion = 1;
     14 
     15 void AddVectorToPickle(std::vector<base::string16> strings,
     16                        Pickle* pickle) {
     17   pickle->WriteInt(static_cast<int>(strings.size()));
     18   for (size_t i = 0; i < strings.size(); ++i) {
     19     pickle->WriteString16(strings[i]);
     20   }
     21 }
     22 
     23 bool ReadStringVector(PickleIterator* iter,
     24                       std::vector<base::string16>* strings) {
     25   int size;
     26   if (!iter->ReadInt(&size))
     27     return false;
     28 
     29   base::string16 pickle_data;
     30   for (int i = 0; i < size; i++) {
     31     if (!iter->ReadString16(&pickle_data))
     32       return false;
     33 
     34     strings->push_back(pickle_data);
     35   }
     36   return true;
     37 }
     38 
     39 bool ReadTextDirection(PickleIterator* iter,
     40                        base::i18n::TextDirection* direction) {
     41   int pickle_data;
     42   if (!iter->ReadInt(&pickle_data))
     43     return false;
     44 
     45   *direction = static_cast<base::i18n::TextDirection>(pickle_data);
     46   return true;
     47 }
     48 
     49 bool ReadSize(PickleIterator* iter, size_t* size) {
     50   uint64 pickle_data;
     51   if (!iter->ReadUInt64(&pickle_data))
     52     return false;
     53 
     54   *size = static_cast<size_t>(pickle_data);
     55   return true;
     56 }
     57 
     58 }  // namespace
     59 
     60 namespace autofill {
     61 
     62 FormFieldData::FormFieldData()
     63     : max_length(0),
     64       is_autofilled(false),
     65       is_checked(false),
     66       is_checkable(false),
     67       is_focusable(false),
     68       should_autocomplete(true),
     69       text_direction(base::i18n::UNKNOWN_DIRECTION) {
     70 }
     71 
     72 FormFieldData::~FormFieldData() {
     73 }
     74 
     75 bool FormFieldData::operator==(const FormFieldData& field) const {
     76   // A FormFieldData stores a value, but the value is not part of the identity
     77   // of the field, so we don't want to compare the values.
     78   return (label == field.label &&
     79           name == field.name &&
     80           form_control_type == field.form_control_type &&
     81           autocomplete_attribute == field.autocomplete_attribute &&
     82           max_length == field.max_length);
     83 }
     84 
     85 bool FormFieldData::operator!=(const FormFieldData& field) const {
     86   return !operator==(field);
     87 }
     88 
     89 bool FormFieldData::operator<(const FormFieldData& field) const {
     90   if (label == field.label)
     91     return name < field.name;
     92 
     93   return label < field.label;
     94 }
     95 
     96 void SerializeFormFieldData(const FormFieldData& field_data,
     97                             Pickle* pickle) {
     98   pickle->WriteInt(kPickleVersion);
     99   pickle->WriteString16(field_data.label);
    100   pickle->WriteString16(field_data.name);
    101   pickle->WriteString16(field_data.value);
    102   pickle->WriteString(field_data.form_control_type);
    103   pickle->WriteString(field_data.autocomplete_attribute);
    104   pickle->WriteUInt64(static_cast<uint64>(field_data.max_length));
    105   pickle->WriteBool(field_data.is_autofilled);
    106   pickle->WriteBool(field_data.is_checked);
    107   pickle->WriteBool(field_data.is_checkable);
    108   pickle->WriteBool(field_data.is_focusable);
    109   pickle->WriteBool(field_data.should_autocomplete);
    110   pickle->WriteInt(field_data.text_direction);
    111   AddVectorToPickle(field_data.option_values, pickle);
    112   AddVectorToPickle(field_data.option_contents, pickle);
    113 }
    114 
    115 bool DeserializeFormFieldData(PickleIterator* iter,
    116                               FormFieldData* field_data) {
    117   int version;
    118   if (!iter->ReadInt(&version)) {
    119     LOG(ERROR) << "Bad pickle of FormFieldData, no version present";
    120     return false;
    121   }
    122 
    123   switch (version) {
    124     case 1: {
    125       if (!iter->ReadString16(&field_data->label) ||
    126           !iter->ReadString16(&field_data->name) ||
    127           !iter->ReadString16(&field_data->value) ||
    128           !iter->ReadString(&field_data->form_control_type) ||
    129           !iter->ReadString(&field_data->autocomplete_attribute) ||
    130           !ReadSize(iter, &field_data->max_length) ||
    131           !iter->ReadBool(&field_data->is_autofilled) ||
    132           !iter->ReadBool(&field_data->is_checked) ||
    133           !iter->ReadBool(&field_data->is_checkable) ||
    134           !iter->ReadBool(&field_data->is_focusable) ||
    135           !iter->ReadBool(&field_data->should_autocomplete) ||
    136           !ReadTextDirection(iter, &field_data->text_direction) ||
    137           !ReadStringVector(iter, &field_data->option_values) ||
    138           !ReadStringVector(iter, &field_data->option_contents)) {
    139         LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
    140         return false;
    141       }
    142       break;
    143     }
    144     default: {
    145       LOG(ERROR) << "Unknown FormFieldData pickle version " << version;
    146       return false;
    147     }
    148   }
    149   return true;
    150 }
    151 
    152 std::ostream& operator<<(std::ostream& os, const FormFieldData& field) {
    153   return os
    154       << base::UTF16ToUTF8(field.label)
    155       << " "
    156       << base::UTF16ToUTF8(field.name)
    157       << " "
    158       << base::UTF16ToUTF8(field.value)
    159       << " "
    160       << field.form_control_type
    161       << " "
    162       << field.autocomplete_attribute
    163       << " "
    164       << field.max_length
    165       << " "
    166       << (field.is_autofilled ? "true" : "false")
    167       << " "
    168       << (field.is_checked ? "true" : "false")
    169       << " "
    170       << (field.is_checkable ? "true" : "false")
    171       << " "
    172       << (field.is_focusable ? "true" : "false")
    173       << " "
    174       << (field.should_autocomplete ? "true" : "false")
    175       << " "
    176       << field.text_direction;
    177 }
    178 
    179 }  // namespace autofill
    180