autofill_field.cc revision 3240926e260ce088908e02ac07a6cf7b0c0cbf44
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"
11#include "components/autofill/core/browser/autofill_type.h"
12
13namespace {
14
15static 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
29namespace autofill {
30
31AutofillField::AutofillField()
32    : server_type_(NO_SERVER_DATA),
33      heuristic_type_(UNKNOWN_TYPE),
34      phone_part_(IGNORED) {
35}
36
37AutofillField::AutofillField(const FormFieldData& field,
38                             const base::string16& unique_name)
39    : FormFieldData(field),
40      unique_name_(unique_name),
41      server_type_(NO_SERVER_DATA),
42      heuristic_type_(UNKNOWN_TYPE),
43      phone_part_(IGNORED) {
44}
45
46AutofillField::~AutofillField() {}
47
48void AutofillField::set_heuristic_type(ServerFieldType type) {
49  if (type >= 0 && type < MAX_VALID_FIELD_TYPE &&
50      type != FIELD_WITH_DEFAULT_VALUE) {
51    heuristic_type_ = type;
52  } else {
53    NOTREACHED();
54    // This case should not be reachable; but since this has potential
55    // implications on data uploaded to the server, better safe than sorry.
56    heuristic_type_ = UNKNOWN_TYPE;
57  }
58}
59
60void AutofillField::set_server_type(ServerFieldType type) {
61  // Chrome no longer supports fax numbers, but the server still does.
62  if (type >= PHONE_FAX_NUMBER && type <= PHONE_FAX_WHOLE_NUMBER)
63    return;
64
65  server_type_ = type;
66}
67
68AutofillType AutofillField::Type() const {
69  if (server_type_ != NO_SERVER_DATA)
70    return AutofillType(server_type_);
71
72  return AutofillType(heuristic_type_);
73}
74
75bool AutofillField::IsEmpty() const {
76  return value.empty();
77}
78
79std::string AutofillField::FieldSignature() const {
80  std::string field_name = UTF16ToUTF8(name);
81  std::string field_string = field_name + "&" + form_control_type;
82  return Hash32Bit(field_string);
83}
84
85bool AutofillField::IsFieldFillable() const {
86  return Type().server_type() != UNKNOWN_TYPE;
87}
88
89}  // namespace autofill
90