Revert of Enabling audio quality test on mac. (patchset #1 id:1 of https://codereview...
[chromium-blink-merge.git] / components / autofill / core / common / autofill_data_validation.cc
blobc8f6584a430db08e33717e4c7234fe35c7d62bdc
1 // Copyright 2014 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.
5 #include "components/autofill/core/common/autofill_data_validation.h"
7 #include "components/autofill/core/common/form_data.h"
8 #include "components/autofill/core/common/form_field_data.h"
9 #include "components/autofill/core/common/password_form_fill_data.h"
10 #include "url/gurl.h"
12 namespace autofill {
14 const size_t kMaxDataLength = 1024;
16 // Allow enough space for all countries (roughly 300 distinct values) and all
17 // timezones (roughly 400 distinct values), plus some extra wiggle room.
18 const size_t kMaxListSize = 512;
20 bool IsValidString(const std::string& str) {
21 return str.size() <= kMaxDataLength;
24 bool IsValidString16(const base::string16& str) {
25 return str.size() <= kMaxDataLength;
28 bool IsValidGURL(const GURL& url) {
29 return url.is_empty() || url.is_valid();
32 bool IsValidFormFieldData(const FormFieldData& field) {
33 return
34 IsValidString16(field.label) &&
35 IsValidString16(field.name) &&
36 IsValidString16(field.value) &&
37 IsValidString(field.form_control_type) &&
38 IsValidString(field.autocomplete_attribute) &&
39 IsValidString16Vector(field.option_values) &&
40 IsValidString16Vector(field.option_contents);
43 bool IsValidFormData(const FormData& form) {
44 if (!IsValidString16(form.name) ||
45 !IsValidGURL(form.origin) ||
46 !IsValidGURL(form.action))
47 return false;
49 if (form.fields.size() > kMaxListSize)
50 return false;
52 for (std::vector<FormFieldData>::const_iterator it = form.fields.begin();
53 it != form.fields.end(); ++it) {
54 if (!IsValidFormFieldData(*it))
55 return false;
58 return true;
61 bool IsValidPasswordFormFillData(const PasswordFormFillData& form) {
62 if (!IsValidFormData(form.basic_data) ||
63 !IsValidString(form.preferred_realm))
64 return false;
66 for (PasswordFormFillData::LoginCollection::const_iterator it =
67 form.additional_logins.begin();
68 it != form.additional_logins.end(); ++it) {
69 if (!IsValidString16(it->first) ||
70 !IsValidString16(it->second.password) ||
71 !IsValidString(it->second.realm))
72 return false;
75 for (PasswordFormFillData::UsernamesCollection::const_iterator it =
76 form.other_possible_usernames.begin();
77 it != form.other_possible_usernames.end(); ++it) {
78 if (!IsValidString16(it->first.username) ||
79 !IsValidString16(it->first.password) ||
80 !IsValidString(it->first.realm) ||
81 !IsValidString16Vector(it->second))
82 return false;
85 return true;
88 bool IsValidString16Vector(const std::vector<base::string16>& v) {
89 if (v.size() > kMaxListSize)
90 return false;
92 for (std::vector<base::string16>::const_iterator it = v.begin();
93 it != v.end(); ++it) {
94 if (!IsValidString16(*it))
95 return false;
98 return true;
101 bool IsValidFormDataVector(const std::vector<FormData>& v) {
102 if (v.size() > kMaxListSize)
103 return false;
105 for (std::vector<FormData>::const_iterator it = v.begin(); it != v.end();
106 ++it) {
107 if (!IsValidFormData(*it))
108 return false;
111 return true;
114 } // namespace autofill