1// Copyright (c) 2011 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 "chrome/browser/autofill/autofill_field.h"
6
7#include "base/logging.h"
8#include "base/sha1.h"
9#include "base/string_number_conversions.h"
10#include "base/utf_string_conversions.h"
11
12namespace {
13
14static std::string Hash32Bit(const std::string& str) {
15  std::string hash_bin = base::SHA1HashString(str);
16  DCHECK_EQ(20U, hash_bin.length());
17
18  uint32 hash32 = ((hash_bin[0] & 0xFF) << 24) |
19                  ((hash_bin[1] & 0xFF) << 16) |
20                  ((hash_bin[2] & 0xFF) << 8) |
21                   (hash_bin[3] & 0xFF);
22
23  return base::UintToString(hash32);
24}
25
26}  // namespace
27
28AutofillField::AutofillField()
29    : server_type_(NO_SERVER_DATA),
30      heuristic_type_(UNKNOWN_TYPE) {
31}
32
33AutofillField::AutofillField(const webkit_glue::FormField& field,
34                             const string16& unique_name)
35    : webkit_glue::FormField(field),
36      unique_name_(unique_name),
37      server_type_(NO_SERVER_DATA),
38      heuristic_type_(UNKNOWN_TYPE) {
39}
40
41AutofillField::~AutofillField() {}
42
43void AutofillField::set_heuristic_type(AutofillFieldType type) {
44  if (type >= 0 && type < MAX_VALID_FIELD_TYPE) {
45    heuristic_type_ = type;
46  } else {
47    NOTREACHED();
48    // This case should not be reachable; but since this has potential
49    // implications on data uploaded to the server, better safe than sorry.
50    heuristic_type_ = UNKNOWN_TYPE;
51  }
52}
53
54AutofillFieldType AutofillField::type() const {
55  if (server_type_ != NO_SERVER_DATA)
56    return server_type_;
57
58  return heuristic_type_;
59}
60
61bool AutofillField::IsEmpty() const {
62  return value.empty();
63}
64
65std::string AutofillField::FieldSignature() const {
66  std::string field_name = UTF16ToUTF8(name);
67  std::string type = UTF16ToUTF8(form_control_type);
68  std::string field_string = field_name + "&" + type;
69  return Hash32Bit(field_string);
70}
71
72bool AutofillField::IsFieldFillable() const {
73  return type() != UNKNOWN_TYPE;
74}
75