[email protected] | fb47414 | 2012-04-05 07:15:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e52bc1e0 | 2009-12-17 22:45:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | ece282a | 2013-04-11 04:39:57 | [diff] [blame] | 5 | #include "components/autofill/browser/webdata/autofill_entry.h" |
[email protected] | fb47414 | 2012-04-05 07:15:18 | [diff] [blame] | 6 | |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 8d21ff3b | 2010-02-25 00:31:47 | [diff] [blame] | 8 | #include <set> |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 9 | |
| 10 | #include "base/logging.h" |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 11 | #include "base/utf_string_conversions.h" |
| 12 | |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 13 | namespace autofill { |
[email protected] | fb47414 | 2012-04-05 07:15:18 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // The period after which Autofill entries should expire in days. |
| 17 | const int64 kExpirationPeriodInDays = 60; |
| 18 | |
| 19 | } // namespace |
| 20 | |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 21 | AutofillKey::AutofillKey() {} |
| 22 | |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 23 | AutofillKey::AutofillKey(const base::string16& name, |
| 24 | const base::string16& value) |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 25 | : name_(name), |
| 26 | value_(value) { |
| 27 | } |
| 28 | |
| 29 | AutofillKey::AutofillKey(const char* name, const char* value) |
| 30 | : name_(UTF8ToUTF16(name)), |
| 31 | value_(UTF8ToUTF16(value)) { |
| 32 | } |
| 33 | |
| 34 | AutofillKey::AutofillKey(const AutofillKey& key) |
| 35 | : name_(key.name()), |
| 36 | value_(key.value()) { |
| 37 | } |
| 38 | |
| 39 | AutofillKey::~AutofillKey() {} |
[email protected] | e52bc1e0 | 2009-12-17 22:45:27 | [diff] [blame] | 40 | |
| 41 | bool AutofillKey::operator==(const AutofillKey& key) const { |
| 42 | return name_ == key.name() && value_ == key.value(); |
| 43 | } |
[email protected] | f2fe1ca87 | 2010-02-10 23:29:36 | [diff] [blame] | 44 | |
[email protected] | 8b3b23a | 2010-03-01 22:06:21 | [diff] [blame] | 45 | bool 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] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 56 | AutofillEntry::AutofillEntry(const AutofillKey& key, |
| 57 | const std::vector<base::Time>& timestamps) |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 58 | : key_(key) { |
| 59 | timestamps_culled_ = CullTimeStamps(timestamps, ×tamps_); |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | AutofillEntry::~AutofillEntry() {} |
| 63 | |
[email protected] | f2fe1ca87 | 2010-02-10 23:29:36 | [diff] [blame] | 64 | bool AutofillEntry::operator==(const AutofillEntry& entry) const { |
[email protected] | 8d21ff3b | 2010-02-25 00:31:47 | [diff] [blame] | 65 | if (!(key_ == entry.key())) |
| 66 | return false; |
[email protected] | f2fe1ca87 | 2010-02-10 23:29:36 | [diff] [blame] | 67 | |
[email protected] | 8d21ff3b | 2010-02-25 00:31:47 | [diff] [blame] | 68 | 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] | 8b3b23a | 2010-03-01 22:06:21 | [diff] [blame] | 80 | |
| 81 | bool AutofillEntry::operator<(const AutofillEntry& entry) const { |
| 82 | return key_ < entry.key(); |
| 83 | } |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 84 | |
[email protected] | fb47414 | 2012-04-05 07:15:18 | [diff] [blame] | 85 | bool 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 |
| 93 | base::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] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 98 | // 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] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 108 | bool AutofillEntry::CullTimeStamps(const std::vector<base::Time>& source, |
[email protected] | fb47414 | 2012-04-05 07:15:18 | [diff] [blame] | 109 | std::vector<base::Time>* result) { |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 110 | DCHECK(result); |
| 111 | DCHECK(&source != result); |
| 112 | |
| 113 | // First copy the source to result. |
| 114 | result->clear(); |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 115 | |
[email protected] | fb47414 | 2012-04-05 07:15:18 | [diff] [blame] | 116 | if (source.size() <= 2) { |
| 117 | result->insert(result->begin(), source.begin(), source.end()); |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 118 | return false; |
[email protected] | fb47414 | 2012-04-05 07:15:18 | [diff] [blame] | 119 | } |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 120 | |
[email protected] | fb47414 | 2012-04-05 07:15:18 | [diff] [blame] | 121 | result->push_back(source.front()); |
| 122 | result->push_back(source.back()); |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 123 | |
[email protected] | fb47414 | 2012-04-05 07:15:18 | [diff] [blame] | 124 | DVLOG(1) << "Culling timestamps. Current count is : " << source.size(); |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame] | 125 | |
| 126 | return true; |
| 127 | } |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 128 | |
| 129 | } // namespace autofill |