Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 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/browser/autofill_field.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/sha1.h" |
| 9 | #include "base/strings/string_number_conversions.h" |
| 10 | #include "base/strings/utf_string_conversions.h" |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 11 | #include "components/autofill/core/browser/autofill_type.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 12 | |
| 13 | namespace { |
| 14 | |
| 15 | static std::string Hash32Bit(const std::string& str) { |
| 16 | std::string hash_bin = base::SHA1HashString(str); |
| 17 | DCHECK_EQ(20U, hash_bin.length()); |
| 18 | |
| 19 | uint32 hash32 = ((hash_bin[0] & 0xFF) << 24) | |
| 20 | ((hash_bin[1] & 0xFF) << 16) | |
| 21 | ((hash_bin[2] & 0xFF) << 8) | |
| 22 | (hash_bin[3] & 0xFF); |
| 23 | |
| 24 | return base::UintToString(hash32); |
| 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | namespace autofill { |
| 30 | |
| 31 | AutofillField::AutofillField() |
| 32 | : server_type_(NO_SERVER_DATA), |
| 33 | heuristic_type_(UNKNOWN_TYPE), |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 34 | html_type_(HTML_TYPE_UNKNOWN), |
| 35 | html_mode_(HTML_MODE_NONE), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 36 | phone_part_(IGNORED) { |
| 37 | } |
| 38 | |
| 39 | AutofillField::AutofillField(const FormFieldData& field, |
| 40 | const base::string16& unique_name) |
| 41 | : FormFieldData(field), |
| 42 | unique_name_(unique_name), |
| 43 | server_type_(NO_SERVER_DATA), |
| 44 | heuristic_type_(UNKNOWN_TYPE), |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 45 | html_type_(HTML_TYPE_UNKNOWN), |
| 46 | html_mode_(HTML_MODE_NONE), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 47 | phone_part_(IGNORED) { |
| 48 | } |
| 49 | |
| 50 | AutofillField::~AutofillField() {} |
| 51 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 52 | void AutofillField::set_heuristic_type(ServerFieldType type) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 53 | if (type >= 0 && type < MAX_VALID_FIELD_TYPE && |
| 54 | type != FIELD_WITH_DEFAULT_VALUE) { |
| 55 | heuristic_type_ = type; |
| 56 | } else { |
| 57 | NOTREACHED(); |
| 58 | // This case should not be reachable; but since this has potential |
| 59 | // implications on data uploaded to the server, better safe than sorry. |
| 60 | heuristic_type_ = UNKNOWN_TYPE; |
| 61 | } |
| 62 | } |
| 63 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 64 | void AutofillField::set_server_type(ServerFieldType type) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 65 | // Chrome no longer supports fax numbers, but the server still does. |
| 66 | if (type >= PHONE_FAX_NUMBER && type <= PHONE_FAX_WHOLE_NUMBER) |
| 67 | return; |
| 68 | |
| 69 | server_type_ = type; |
| 70 | } |
| 71 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 72 | void AutofillField::SetHtmlType(HtmlFieldType type, HtmlFieldMode mode) { |
| 73 | html_type_ = type; |
| 74 | html_mode_ = mode; |
| 75 | |
| 76 | if (type == HTML_TYPE_TEL_LOCAL_PREFIX) |
| 77 | phone_part_ = AutofillField::PHONE_PREFIX; |
| 78 | else if (type == HTML_TYPE_TEL_LOCAL_SUFFIX) |
| 79 | phone_part_ = AutofillField::PHONE_SUFFIX; |
| 80 | } |
| 81 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 82 | AutofillType AutofillField::Type() const { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 83 | if (html_type_ != HTML_TYPE_UNKNOWN) |
| 84 | return AutofillType(html_type_, html_mode_); |
| 85 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 86 | if (server_type_ != NO_SERVER_DATA) |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 87 | return AutofillType(server_type_); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 88 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 89 | return AutofillType(heuristic_type_); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | bool AutofillField::IsEmpty() const { |
| 93 | return value.empty(); |
| 94 | } |
| 95 | |
| 96 | std::string AutofillField::FieldSignature() const { |
| 97 | std::string field_name = UTF16ToUTF8(name); |
| 98 | std::string field_string = field_name + "&" + form_control_type; |
| 99 | return Hash32Bit(field_string); |
| 100 | } |
| 101 | |
| 102 | bool AutofillField::IsFieldFillable() const { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 103 | return !Type().IsUnknown(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace autofill |