blob: 6d7d538ae8c0a4488d7fb1ceebb7825ec3b88c59 [file] [log] [blame]
[email protected]fb474142012-04-05 07:15:181// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e52bc1e02009-12-17 22:45:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ece282a2013-04-11 04:39:575#include "components/autofill/browser/webdata/autofill_entry.h"
[email protected]fb474142012-04-05 07:15:186
[email protected]1df844692011-05-19 23:23:037#include <algorithm>
[email protected]8d21ff3b2010-02-25 00:31:478#include <set>
[email protected]1df844692011-05-19 23:23:039
10#include "base/logging.h"
[email protected]38e08982010-10-22 17:28:4311#include "base/utf_string_conversions.h"
12
[email protected]e217c5632013-04-12 19:11:4813namespace autofill {
[email protected]fb474142012-04-05 07:15:1814namespace {
15
16// The period after which Autofill entries should expire in days.
17const int64 kExpirationPeriodInDays = 60;
18
19} // namespace
20
[email protected]38e08982010-10-22 17:28:4321AutofillKey::AutofillKey() {}
22
[email protected]d5ca8fb2013-04-11 17:54:3123AutofillKey::AutofillKey(const base::string16& name,
24 const base::string16& value)
[email protected]38e08982010-10-22 17:28:4325 : name_(name),
26 value_(value) {
27}
28
29AutofillKey::AutofillKey(const char* name, const char* value)
30 : name_(UTF8ToUTF16(name)),
31 value_(UTF8ToUTF16(value)) {
32}
33
34AutofillKey::AutofillKey(const AutofillKey& key)
35 : name_(key.name()),
36 value_(key.value()) {
37}
38
39AutofillKey::~AutofillKey() {}
[email protected]e52bc1e02009-12-17 22:45:2740
41bool AutofillKey::operator==(const AutofillKey& key) const {
42 return name_ == key.name() && value_ == key.value();
43}
[email protected]f2fe1ca872010-02-10 23:29:3644
[email protected]8b3b23a2010-03-01 22:06:2145bool AutofillKey::operator<(const AutofillKey& key) const {
46 int diff = name_.compare(key.name());
47 if (diff < 0) {
48 return true;
49 } else if (diff == 0) {
50 return value_.compare(key.value()) < 0;
51 } else {
52 return false;
53 }
54}
55
[email protected]38e08982010-10-22 17:28:4356AutofillEntry::AutofillEntry(const AutofillKey& key,
57 const std::vector<base::Time>& timestamps)
[email protected]1df844692011-05-19 23:23:0358 : key_(key) {
59 timestamps_culled_ = CullTimeStamps(timestamps, &timestamps_);
[email protected]38e08982010-10-22 17:28:4360}
61
62AutofillEntry::~AutofillEntry() {}
63
[email protected]f2fe1ca872010-02-10 23:29:3664bool AutofillEntry::operator==(const AutofillEntry& entry) const {
[email protected]8d21ff3b2010-02-25 00:31:4765 if (!(key_ == entry.key()))
66 return false;
[email protected]f2fe1ca872010-02-10 23:29:3667
[email protected]8d21ff3b2010-02-25 00:31:4768 if (timestamps_.size() != entry.timestamps().size())
69 return false;
70
71 std::set<base::Time> other_timestamps(entry.timestamps().begin(),
72 entry.timestamps().end());
73 for (size_t i = 0; i < timestamps_.size(); i++) {
74 if (other_timestamps.count(timestamps_[i]) == 0)
75 return false;
76 }
77
78 return true;
79}
[email protected]8b3b23a2010-03-01 22:06:2180
81bool AutofillEntry::operator<(const AutofillEntry& entry) const {
82 return key_ < entry.key();
83}
[email protected]1df844692011-05-19 23:23:0384
[email protected]fb474142012-04-05 07:15:1885bool AutofillEntry::IsExpired() const {
86 base::Time time = ExpirationTime();
87 // TODO(georgey): add DCHECK(!timestamps_.empty()) after conversion of the db
88 // is complete.
89 return (timestamps_.empty() || timestamps_.back() < time);
90}
91
92// static
93base::Time AutofillEntry::ExpirationTime() {
94 return base::Time::Now() - base::TimeDelta::FromDays(kExpirationPeriodInDays);
95}
96
97// Culls the list of timestamps to the first and last used.
[email protected]1df844692011-05-19 23:23:0398// If sync is enabled, at every browser restart, sync will do a match up of all
99// autofill items on the server with all items on the web db. When webdb loads
100// all the items in memory(for sync to process. The method is
101// |GetAllAutofillEntries|) they will pass through this method for culling. If
102// sync finds any of these items were culled it will updates the server AND the
103// web db with these new timestamps. However after restart if an autofill item
104// exceeds the |kMaxAutofillTimeStamps| it will NOT be written to web db until
105// restart. But sync when uploading to the server will only upload this culled
106// list. Meaning until restart there will be mis-match in timestamps but
107// it should correct itself at startup.
[email protected]1df844692011-05-19 23:23:03108bool AutofillEntry::CullTimeStamps(const std::vector<base::Time>& source,
[email protected]fb474142012-04-05 07:15:18109 std::vector<base::Time>* result) {
[email protected]1df844692011-05-19 23:23:03110 DCHECK(result);
111 DCHECK(&source != result);
112
113 // First copy the source to result.
114 result->clear();
[email protected]1df844692011-05-19 23:23:03115
[email protected]fb474142012-04-05 07:15:18116 if (source.size() <= 2) {
117 result->insert(result->begin(), source.begin(), source.end());
[email protected]1df844692011-05-19 23:23:03118 return false;
[email protected]fb474142012-04-05 07:15:18119 }
[email protected]1df844692011-05-19 23:23:03120
[email protected]fb474142012-04-05 07:15:18121 result->push_back(source.front());
122 result->push_back(source.back());
[email protected]1df844692011-05-19 23:23:03123
[email protected]fb474142012-04-05 07:15:18124 DVLOG(1) << "Culling timestamps. Current count is : " << source.size();
[email protected]1df844692011-05-19 23:23:03125
126 return true;
127}
[email protected]e217c5632013-04-12 19:11:48128
129} // namespace autofill