- add sources.
[platform/framework/web/crosswalk.git] / src / components / autofill / core / common / form_data.cc
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
12 namespace autofill {
13
14 namespace {
15
16 const int kPickleVersion = 1;
17
18 bool 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
27 void 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
35 bool 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
51 }  // namespace
52
53 FormData::FormData()
54     : user_submitted(false) {
55 }
56
57 FormData::FormData(const FormData& data)
58     : name(data.name),
59       method(data.method),
60       origin(data.origin),
61       action(data.action),
62       user_submitted(data.user_submitted),
63       fields(data.fields) {
64 }
65
66 FormData::~FormData() {
67 }
68
69 bool FormData::operator==(const FormData& form) const {
70   return name == form.name &&
71          StringToLowerASCII(method) == StringToLowerASCII(form.method) &&
72          origin == form.origin &&
73          action == form.action &&
74          user_submitted == form.user_submitted &&
75          fields == form.fields;
76 }
77
78 bool FormData::operator!=(const FormData& form) const {
79   return !operator==(form);
80 }
81
82 std::ostream& operator<<(std::ostream& os, const FormData& form) {
83   os << UTF16ToUTF8(form.name) << " "
84      << UTF16ToUTF8(form.method) << " "
85      << form.origin << " "
86      << form.action << " "
87      << form.user_submitted << " "
88      << "Fields:";
89   for (size_t i = 0; i < form.fields.size(); ++i) {
90     os << form.fields[i] << ",";
91   }
92   return os;
93 }
94
95 void SerializeFormData(const FormData& form_data, Pickle* pickle) {
96   pickle->WriteInt(kPickleVersion);
97   pickle->WriteString16(form_data.name);
98   pickle->WriteString16(form_data.method);
99   pickle->WriteString(form_data.origin.spec());
100   pickle->WriteString(form_data.action.spec());
101   pickle->WriteBool(form_data.user_submitted);
102   SerializeFormFieldDataVector(form_data.fields, pickle);
103 }
104
105 bool DeserializeFormData(PickleIterator* iter, FormData* form_data) {
106   int version;
107   if (!iter->ReadInt(&version)) {
108     LOG(ERROR) << "Bad pickle of FormData, no version present";
109     return false;
110   }
111
112   switch (version) {
113     case 1: {
114       if (!iter->ReadString16(&form_data->name) ||
115           !iter->ReadString16(&form_data->method) ||
116           !ReadGURL(iter, &form_data->origin) ||
117           !ReadGURL(iter, &form_data->action) ||
118           !iter->ReadBool(&form_data->user_submitted) ||
119           !DeserializeFormFieldDataVector(iter, &form_data->fields)) {
120         LOG(ERROR) << "Could not deserialize FormData from pickle";
121         return false;
122       }
123       break;
124     }
125     default: {
126       LOG(ERROR) << "Unknown FormData pickle version " << version;
127       return false;
128     }
129   }
130   return true;
131 }
132
133 }  // namespace autofill