1 // Copyright 2013 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 "components/autofill/core/browser/webdata/autofill_entry.h" 6 7 #include <algorithm> 8 #include <set> 9 10 #include "base/logging.h" 11 #include "base/strings/utf_string_conversions.h" 12 13 namespace autofill { 14 namespace { 15 16 // The period after which Autofill entries should expire in days. 17 const int64 kExpirationPeriodInDays = 60; 18 19 } // namespace 20 21 AutofillKey::AutofillKey() {} 22 23 AutofillKey::AutofillKey(const base::string16& name, 24 const base::string16& value) 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() {} 40 41 bool AutofillKey::operator==(const AutofillKey& key) const { 42 return name_ == key.name() && value_ == key.value(); 43 } 44 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 56 AutofillEntry::AutofillEntry(const AutofillKey& key, 57 const std::vector<base::Time>& timestamps) 58 : key_(key) { 59 timestamps_culled_ = CullTimeStamps(timestamps, ×tamps_); 60 } 61 62 AutofillEntry::~AutofillEntry() {} 63 64 bool AutofillEntry::operator==(const AutofillEntry& entry) const { 65 if (!(key_ == entry.key())) 66 return false; 67 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 } 80 81 bool AutofillEntry::operator<(const AutofillEntry& entry) const { 82 return key_ < entry.key(); 83 } 84 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. 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. 108 bool AutofillEntry::CullTimeStamps(const std::vector<base::Time>& source, 109 std::vector<base::Time>* result) { 110 DCHECK(result); 111 DCHECK(&source != result); 112 113 // First copy the source to result. 114 result->clear(); 115 116 if (source.size() <= 2) { 117 result->insert(result->begin(), source.begin(), source.end()); 118 return false; 119 } 120 121 result->push_back(source.front()); 122 result->push_back(source.back()); 123 124 DVLOG(1) << "Culling timestamps. Current count is : " << source.size(); 125 126 return true; 127 } 128 129 } // namespace autofill 130