[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame^] | 1 | // Copyright (c) 2011 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] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame^] | 5 | #include <algorithm> |
[email protected] | 8d21ff3b | 2010-02-25 00:31:47 | [diff] [blame] | 6 | #include <set> |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame^] | 7 | |
| 8 | #include "base/logging.h" |
[email protected] | e52bc1e0 | 2009-12-17 22:45:27 | [diff] [blame] | 9 | #include "chrome/browser/webdata/autofill_entry.h" |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
| 11 | |
| 12 | AutofillKey::AutofillKey() {} |
| 13 | |
| 14 | AutofillKey::AutofillKey(const string16& name, const string16& value) |
| 15 | : name_(name), |
| 16 | value_(value) { |
| 17 | } |
| 18 | |
| 19 | AutofillKey::AutofillKey(const char* name, const char* value) |
| 20 | : name_(UTF8ToUTF16(name)), |
| 21 | value_(UTF8ToUTF16(value)) { |
| 22 | } |
| 23 | |
| 24 | AutofillKey::AutofillKey(const AutofillKey& key) |
| 25 | : name_(key.name()), |
| 26 | value_(key.value()) { |
| 27 | } |
| 28 | |
| 29 | AutofillKey::~AutofillKey() {} |
[email protected] | e52bc1e0 | 2009-12-17 22:45:27 | [diff] [blame] | 30 | |
| 31 | bool AutofillKey::operator==(const AutofillKey& key) const { |
| 32 | return name_ == key.name() && value_ == key.value(); |
| 33 | } |
[email protected] | f2fe1ca87 | 2010-02-10 23:29:36 | [diff] [blame] | 34 | |
[email protected] | 8b3b23a | 2010-03-01 22:06:21 | [diff] [blame] | 35 | bool 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] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 46 | AutofillEntry::AutofillEntry(const AutofillKey& key, |
| 47 | const std::vector<base::Time>& timestamps) |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame^] | 48 | : key_(key) { |
| 49 | timestamps_culled_ = CullTimeStamps(timestamps, ×tamps_); |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | AutofillEntry::~AutofillEntry() {} |
| 53 | |
[email protected] | f2fe1ca87 | 2010-02-10 23:29:36 | [diff] [blame] | 54 | bool AutofillEntry::operator==(const AutofillEntry& entry) const { |
[email protected] | 8d21ff3b | 2010-02-25 00:31:47 | [diff] [blame] | 55 | if (!(key_ == entry.key())) |
| 56 | return false; |
[email protected] | f2fe1ca87 | 2010-02-10 23:29:36 | [diff] [blame] | 57 | |
[email protected] | 8d21ff3b | 2010-02-25 00:31:47 | [diff] [blame] | 58 | 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] | 8b3b23a | 2010-03-01 22:06:21 | [diff] [blame] | 70 | |
| 71 | bool AutofillEntry::operator<(const AutofillEntry& entry) const { |
| 72 | return key_ < entry.key(); |
| 73 | } |
[email protected] | 1df84469 | 2011-05-19 23:23:03 | [diff] [blame^] | 74 | |
| 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. |
| 87 | bool 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 | } |