summaryrefslogtreecommitdiffstats
path: root/components/autofill/browser/webdata/autofill_entry.cc
blob: 4d47790263b4cf3c69d4a1ef4d21f0fa5f8cbd58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/autofill/browser/webdata/autofill_entry.h"

#include <algorithm>
#include <set>

#include "base/logging.h"
#include "base/utf_string_conversions.h"

namespace {

// The period after which Autofill entries should expire in days.
const int64 kExpirationPeriodInDays = 60;

}  // namespace

AutofillKey::AutofillKey() {}

AutofillKey::AutofillKey(const base::string16& name,
                         const base::string16& value)
    : name_(name),
      value_(value) {
}

AutofillKey::AutofillKey(const char* name, const char* value)
    : name_(UTF8ToUTF16(name)),
      value_(UTF8ToUTF16(value)) {
}

AutofillKey::AutofillKey(const AutofillKey& key)
    : name_(key.name()),
      value_(key.value()) {
}

AutofillKey::~AutofillKey() {}

bool AutofillKey::operator==(const AutofillKey& key) const {
  return name_ == key.name() && value_ == key.value();
}

bool AutofillKey::operator<(const AutofillKey& key) const {
  int diff = name_.compare(key.name());
  if (diff < 0) {
    return true;
  } else if (diff == 0) {
    return value_.compare(key.value()) < 0;
  } else {
    return false;
  }
}

AutofillEntry::AutofillEntry(const AutofillKey& key,
                             const std::vector<base::Time>& timestamps)
    : key_(key) {
  timestamps_culled_ = CullTimeStamps(timestamps, &timestamps_);
}

AutofillEntry::~AutofillEntry() {}

bool AutofillEntry::operator==(const AutofillEntry& entry) const {
  if (!(key_ == entry.key()))
    return false;

  if (timestamps_.size() != entry.timestamps().size())
    return false;

  std::set<base::Time> other_timestamps(entry.timestamps().begin(),
                                        entry.timestamps().end());
  for (size_t i = 0; i < timestamps_.size(); i++) {
    if (other_timestamps.count(timestamps_[i]) == 0)
      return false;
  }

  return true;
}

bool AutofillEntry::operator<(const AutofillEntry& entry) const {
  return key_ < entry.key();
}

bool AutofillEntry::IsExpired() const {
  base::Time time = ExpirationTime();
  // TODO(georgey): add DCHECK(!timestamps_.empty()) after conversion of the db
  // is complete.
  return (timestamps_.empty() || timestamps_.back() < time);
}

// static
base::Time AutofillEntry::ExpirationTime() {
  return base::Time::Now() - base::TimeDelta::FromDays(kExpirationPeriodInDays);
}

// Culls the list of timestamps to the first and last used.
// If sync is enabled, at every browser restart, sync will do a match up of all
// autofill items on the server with all items on the web db. When webdb loads
// all the items in memory(for sync to process. The method is
// |GetAllAutofillEntries|) they will pass through this method for culling. If
// sync finds any of these items were culled it will updates the server AND the
// web db with these new timestamps. However after restart if an autofill item
// exceeds the |kMaxAutofillTimeStamps| it will NOT be written to web db until
// restart. But sync when uploading to the server will only upload this culled
// list. Meaning until restart there will be mis-match in timestamps but
// it should correct itself at startup.
bool AutofillEntry::CullTimeStamps(const std::vector<base::Time>& source,
                                   std::vector<base::Time>* result) {
  DCHECK(result);
  DCHECK(&source != result);

  // First copy the source to result.
  result->clear();

  if (source.size() <= 2) {
    result->insert(result->begin(), source.begin(), source.end());
    return false;
  }

  result->push_back(source.front());
  result->push_back(source.back());

  DVLOG(1) << "Culling timestamps. Current count is : " << source.size();

  return true;
}