| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/webdata/autofill_entry.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // The period after which Autofill entries should expire in days. | |
| 16 const int64 kExpirationPeriodInDays = 60; | |
| 17 | |
| 18 } // namespace | |
| 19 | |
| 20 AutofillKey::AutofillKey() {} | |
| 21 | |
| 22 AutofillKey::AutofillKey(const string16& name, const string16& value) | |
| 23 : name_(name), | |
| 24 value_(value) { | |
| 25 } | |
| 26 | |
| 27 AutofillKey::AutofillKey(const char* name, const char* value) | |
| 28 : name_(UTF8ToUTF16(name)), | |
| 29 value_(UTF8ToUTF16(value)) { | |
| 30 } | |
| 31 | |
| 32 AutofillKey::AutofillKey(const AutofillKey& key) | |
| 33 : name_(key.name()), | |
| 34 value_(key.value()) { | |
| 35 } | |
| 36 | |
| 37 AutofillKey::~AutofillKey() {} | |
| 38 | |
| 39 bool AutofillKey::operator==(const AutofillKey& key) const { | |
| 40 return name_ == key.name() && value_ == key.value(); | |
| 41 } | |
| 42 | |
| 43 bool AutofillKey::operator<(const AutofillKey& key) const { | |
| 44 int diff = name_.compare(key.name()); | |
| 45 if (diff < 0) { | |
| 46 return true; | |
| 47 } else if (diff == 0) { | |
| 48 return value_.compare(key.value()) < 0; | |
| 49 } else { | |
| 50 return false; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 AutofillEntry::AutofillEntry(const AutofillKey& key, | |
| 55 const std::vector<base::Time>& timestamps) | |
| 56 : key_(key) { | |
| 57 timestamps_culled_ = CullTimeStamps(timestamps, ×tamps_); | |
| 58 } | |
| 59 | |
| 60 AutofillEntry::~AutofillEntry() {} | |
| 61 | |
| 62 bool AutofillEntry::operator==(const AutofillEntry& entry) const { | |
| 63 if (!(key_ == entry.key())) | |
| 64 return false; | |
| 65 | |
| 66 if (timestamps_.size() != entry.timestamps().size()) | |
| 67 return false; | |
| 68 | |
| 69 std::set<base::Time> other_timestamps(entry.timestamps().begin(), | |
| 70 entry.timestamps().end()); | |
| 71 for (size_t i = 0; i < timestamps_.size(); i++) { | |
| 72 if (other_timestamps.count(timestamps_[i]) == 0) | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 bool AutofillEntry::operator<(const AutofillEntry& entry) const { | |
| 80 return key_ < entry.key(); | |
| 81 } | |
| 82 | |
| 83 bool AutofillEntry::IsExpired() const { | |
| 84 base::Time time = ExpirationTime(); | |
| 85 // TODO(georgey): add DCHECK(!timestamps_.empty()) after conversion of the db | |
| 86 // is complete. | |
| 87 return (timestamps_.empty() || timestamps_.back() < time); | |
| 88 } | |
| 89 | |
| 90 // static | |
| 91 base::Time AutofillEntry::ExpirationTime() { | |
| 92 return base::Time::Now() - base::TimeDelta::FromDays(kExpirationPeriodInDays); | |
| 93 } | |
| 94 | |
| 95 // Culls the list of timestamps to the first and last used. | |
| 96 // If sync is enabled, at every browser restart, sync will do a match up of all | |
| 97 // autofill items on the server with all items on the web db. When webdb loads | |
| 98 // all the items in memory(for sync to process. The method is | |
| 99 // |GetAllAutofillEntries|) they will pass through this method for culling. If | |
| 100 // sync finds any of these items were culled it will updates the server AND the | |
| 101 // web db with these new timestamps. However after restart if an autofill item | |
| 102 // exceeds the |kMaxAutofillTimeStamps| it will NOT be written to web db until | |
| 103 // restart. But sync when uploading to the server will only upload this culled | |
| 104 // list. Meaning until restart there will be mis-match in timestamps but | |
| 105 // it should correct itself at startup. | |
| 106 bool AutofillEntry::CullTimeStamps(const std::vector<base::Time>& source, | |
| 107 std::vector<base::Time>* result) { | |
| 108 DCHECK(result); | |
| 109 DCHECK(&source != result); | |
| 110 | |
| 111 // First copy the source to result. | |
| 112 result->clear(); | |
| 113 | |
| 114 if (source.size() <= 2) { | |
| 115 result->insert(result->begin(), source.begin(), source.end()); | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 result->push_back(source.front()); | |
| 120 result->push_back(source.back()); | |
| 121 | |
| 122 DVLOG(1) << "Culling timestamps. Current count is : " << source.size(); | |
| 123 | |
| 124 return true; | |
| 125 } | |
| OLD | NEW |