Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: components/autofill/core/common/form_data.cc

Issue 23033010: [password autofill] Add serialization for FormData and FormFieldData (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Windows Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/autofill/core/common/form_data.h" 5 #include "components/autofill/core/common/form_data.h"
6 6
7 #include "base/pickle.h"
7 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "components/autofill/core/common/form_field_data.h"
8 10
9 namespace autofill { 11 namespace autofill {
10 12
13 namespace {
14
15 const int kPickleVersion = 1;
16
17 bool ReadGURL(PickleIterator* iter, GURL* url) {
18 std::string spec;
19 if (!iter->ReadString(&spec))
20 return false;
21
22 *url = GURL(spec);
23 return true;
24 }
25
26 void SerializeFormFieldDataVector(const std::vector<FormFieldData> fields,
27 Pickle* pickle) {
28 pickle->WriteInt(static_cast<int>(fields.size()));
29 for (size_t i = 0; i < fields.size(); ++i) {
30 SerializeFormFieldData(fields[i], pickle);
31 }
32 }
33
34 bool DeserializeFormFieldDataVector(PickleIterator* iter,
35 std::vector<FormFieldData>* fields) {
36 int size;
37 if (!iter->ReadInt(&size))
38 return false;
39
40 FormFieldData temp;
41 for (int i = 0; i < size; ++i) {
42 if (!DeserializeFormFieldData(iter, &temp))
43 return false;
44
45 fields->push_back(temp);
46 }
47 return true;
48 }
49
50 } // namespace
51
11 FormData::FormData() 52 FormData::FormData()
12 : user_submitted(false) { 53 : user_submitted(false) {
13 } 54 }
14 55
15 FormData::FormData(const FormData& data) 56 FormData::FormData(const FormData& data)
16 : name(data.name), 57 : name(data.name),
17 method(data.method), 58 method(data.method),
18 origin(data.origin), 59 origin(data.origin),
19 action(data.action), 60 action(data.action),
20 user_submitted(data.user_submitted), 61 user_submitted(data.user_submitted),
21 fields(data.fields) { 62 fields(data.fields) {
22 } 63 }
23 64
24 FormData::~FormData() { 65 FormData::~FormData() {
25 } 66 }
26 67
27 bool FormData::operator==(const FormData& form) const { 68 bool FormData::operator==(const FormData& form) const {
28 return name == form.name && 69 return name == form.name &&
29 StringToLowerASCII(method) == StringToLowerASCII(form.method) && 70 StringToLowerASCII(method) == StringToLowerASCII(form.method) &&
30 origin == form.origin && 71 origin == form.origin &&
31 action == form.action && 72 action == form.action &&
32 user_submitted == form.user_submitted && 73 user_submitted == form.user_submitted &&
33 fields == form.fields; 74 fields == form.fields;
34 } 75 }
35 76
36 bool FormData::operator!=(const FormData& form) const { 77 bool FormData::operator!=(const FormData& form) const {
37 return !operator==(form); 78 return !operator==(form);
38 } 79 }
39 80
81 void SerializeFormData(const FormData& form_data, Pickle* pickle) {
82 pickle->WriteInt(kPickleVersion);
83 pickle->WriteString16(form_data.name);
84 pickle->WriteString16(form_data.method);
85 pickle->WriteString(form_data.origin.spec());
86 pickle->WriteString(form_data.action.spec());
87 pickle->WriteBool(form_data.user_submitted);
88 SerializeFormFieldDataVector(form_data.fields, pickle);
89 }
90
91 bool DeserializeFormData(PickleIterator* iter, FormData* form_data) {
92 int version;
93 if (!iter->ReadInt(&version)) {
94 LOG(ERROR) << "Bad pickle of FormData, no version present";
95 return false;
96 }
97
98 switch (version) {
99 case 1: {
100 if (!iter->ReadString16(&form_data->name) ||
101 !iter->ReadString16(&form_data->method) ||
102 !ReadGURL(iter, &form_data->origin) ||
103 !ReadGURL(iter, &form_data->action) ||
104 !iter->ReadBool(&form_data->user_submitted) ||
105 !DeserializeFormFieldDataVector(iter, &form_data->fields)) {
106 LOG(ERROR) << "Could not deserialize FormData from pickle";
107 return false;
108 }
109 break;
110 }
111 default: {
112 LOG(ERROR) << "Unknown FormData pickle version " << version;
113 return false;
114 }
115 }
116 return true;
117 }
118
40 } // namespace autofill 119 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/common/form_data.h ('k') | components/autofill/core/common/form_data_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698