blob: fe874a58cd8c4da8be15155a99f5e15ed43ace29 [file] [log] [blame]
[email protected]1df844692011-05-19 23:23:031// Copyright (c) 2011 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]1df844692011-05-19 23:23:035#include <algorithm>
[email protected]8d21ff3b2010-02-25 00:31:476#include <set>
[email protected]1df844692011-05-19 23:23:037
8#include "base/logging.h"
[email protected]e52bc1e02009-12-17 22:45:279#include "chrome/browser/webdata/autofill_entry.h"
[email protected]38e08982010-10-22 17:28:4310#include "base/utf_string_conversions.h"
11
12AutofillKey::AutofillKey() {}
13
14AutofillKey::AutofillKey(const string16& name, const string16& value)
15 : name_(name),
16 value_(value) {
17}
18
19AutofillKey::AutofillKey(const char* name, const char* value)
20 : name_(UTF8ToUTF16(name)),
21 value_(UTF8ToUTF16(value)) {
22}
23
24AutofillKey::AutofillKey(const AutofillKey& key)
25 : name_(key.name()),
26 value_(key.value()) {
27}
28
29AutofillKey::~AutofillKey() {}
[email protected]e52bc1e02009-12-17 22:45:2730
31bool AutofillKey::operator==(const AutofillKey& key) const {
32 return name_ == key.name() && value_ == key.value();
33}
[email protected]f2fe1ca872010-02-10 23:29:3634
[email protected]8b3b23a2010-03-01 22:06:2135bool AutofillKey::operator<(const AutofillKey& key) const {
36 int diff = name_.compare(key.name());
37 if (diff < 0) {
38 return true;
39 } else if (diff == 0) {
40 return value_.compare(key.value()) < 0;
41 } else {
42 return false;
43 }
44}
45
[email protected]38e08982010-10-22 17:28:4346AutofillEntry::AutofillEntry(const AutofillKey& key,
47 const std::vector<base::Time>& timestamps)
[email protected]1df844692011-05-19 23:23:0348 : key_(key) {
49 timestamps_culled_ = CullTimeStamps(timestamps, &timestamps_);
[email protected]38e08982010-10-22 17:28:4350}
51
52AutofillEntry::~AutofillEntry() {}
53
[email protected]f2fe1ca872010-02-10 23:29:3654bool AutofillEntry::operator==(const AutofillEntry& entry) const {
[email protected]8d21ff3b2010-02-25 00:31:4755 if (!(key_ == entry.key()))
56 return false;
[email protected]f2fe1ca872010-02-10 23:29:3657
[email protected]8d21ff3b2010-02-25 00:31:4758 if (timestamps_.size() != entry.timestamps().size())
59 return false;
60
61 std::set<base::Time> other_timestamps(entry.timestamps().begin(),
62 entry.timestamps().end());
63 for (size_t i = 0; i < timestamps_.size(); i++) {
64 if (other_timestamps.count(timestamps_[i]) == 0)
65 return false;
66 }
67
68 return true;
69}
[email protected]8b3b23a2010-03-01 22:06:2170
71bool AutofillEntry::operator<(const AutofillEntry& entry) const {
72 return key_ < entry.key();
73}
[email protected]1df844692011-05-19 23:23:0374
75// Culls the list of timestamps to the most recent |kMaxAutofillTimeStamps|.
76// If sync is enabled, at every browser restart, sync will do a match up of all
77// autofill items on the server with all items on the web db. When webdb loads
78// all the items in memory(for sync to process. The method is
79// |GetAllAutofillEntries|) they will pass through this method for culling. If
80// sync finds any of these items were culled it will updates the server AND the
81// web db with these new timestamps. However after restart if an autofill item
82// exceeds the |kMaxAutofillTimeStamps| it will NOT be written to web db until
83// restart. But sync when uploading to the server will only upload this culled
84// list. Meaning until restart there will be mis-match in timestamps but
85// it should correct itself at startup.
86// Also if sync is not enabled this culling would never take place.
87bool AutofillEntry::CullTimeStamps(const std::vector<base::Time>& source,
88 std::vector<base::Time>* result) {
89 DCHECK(result);
90 DCHECK(&source != result);
91
92 // First copy the source to result.
93 result->clear();
94 result->insert(result->begin(), source.begin(), source.end());
95
96 if (source.size() <= kMaxAutofillTimeStamps)
97 return false;
98
99 VLOG(1) << "Culling timestamps. Current count is : " << source.size();
100
101 // Regular sort does ascending order. So we pass in a comparator function.
102 partial_sort(result->begin(), result->begin() + kMaxAutofillTimeStamps,
103 result->end(), std::greater<base::Time>());
104
105 result->erase(result->begin() + kMaxAutofillTimeStamps, result->end());
106
107 return true;
108}