| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/autofill/core/browser/autofill_data_model.h" | 5 #include "components/autofill/core/browser/autofill_data_model.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "components/autofill/core/browser/autofill_type.h" | 9 #include "components/autofill/core/browser/autofill_type.h" |
| 10 #include "url/gurl.h" | 10 #include "url/gurl.h" |
| 11 | 11 |
| 12 namespace autofill { | 12 namespace autofill { |
| 13 | 13 |
| 14 AutofillDataModel::AutofillDataModel(const std::string& guid, | 14 AutofillDataModel::AutofillDataModel(const std::string& guid, |
| 15 const std::string& origin) | 15 const std::string& origin) |
| 16 : guid_(guid), | 16 : guid_(guid), |
| 17 origin_(origin), | 17 origin_(origin), |
| 18 use_count_(1), | 18 use_count_(1), |
| 19 use_date_(base::Time::Now()), | 19 use_date_(base::Time::Now()), |
| 20 modification_date_(base::Time::Now()) {} | 20 modification_date_(base::Time::Now()) {} |
| 21 AutofillDataModel::~AutofillDataModel() {} | 21 AutofillDataModel::~AutofillDataModel() {} |
| 22 | 22 |
| 23 bool AutofillDataModel::IsVerified() const { | 23 bool AutofillDataModel::IsVerified() const { |
| 24 return !origin_.empty() && !GURL(origin_).is_valid(); | 24 return !origin_.empty() && !GURL(origin_).is_valid(); |
| 25 } | 25 } |
| 26 | 26 |
| 27 // TODO(crbug.com/629507): Add support for injected mock clock for testing. |
| 27 void AutofillDataModel::RecordUse() { | 28 void AutofillDataModel::RecordUse() { |
| 28 ++use_count_; | 29 ++use_count_; |
| 29 use_date_ = base::Time::Now(); | 30 use_date_ = base::Time::Now(); |
| 30 } | 31 } |
| 31 | 32 |
| 32 bool AutofillDataModel::CompareFrecency(const AutofillDataModel* other, | 33 bool AutofillDataModel::CompareFrecency(const AutofillDataModel* other, |
| 33 base::Time comparison_time) const { | 34 base::Time comparison_time) const { |
| 34 double score = GetFrecencyScore(comparison_time); | 35 double score = GetFrecencyScore(comparison_time); |
| 35 double other_score = other->GetFrecencyScore(comparison_time); | 36 double other_score = other->GetFrecencyScore(comparison_time); |
| 36 | 37 |
| 37 // Ties are broken by MRU, then by GUID comparison. | 38 // Ties are broken by MRU, then by GUID comparison. |
| 38 if (score != other_score) | 39 if (score != other_score) |
| 39 return score > other_score; | 40 return score > other_score; |
| 40 | 41 |
| 41 if (use_date_ != other->use_date_) | 42 if (use_date_ != other->use_date_) |
| 42 return use_date_ > other->use_date_; | 43 return use_date_ > other->use_date_; |
| 43 | 44 |
| 44 return guid_ > other->guid_; | 45 return guid_ > other->guid_; |
| 45 } | 46 } |
| 46 | 47 |
| 47 double AutofillDataModel::GetFrecencyScore(base::Time time) const { | 48 double AutofillDataModel::GetFrecencyScore(base::Time time) const { |
| 48 // The formula calculates a score based on both the frequency and the recency | 49 // The formula calculates a score based on both the frequency and the recency |
| 49 // of the profile and leveraging the properties of the logarithmic function. | 50 // of the profile and leveraging the properties of the logarithmic function. |
| 50 // DaysSinceLastUse() and |use_count_| are offset because their minimum values | 51 // DaysSinceLastUse() and |use_count_| are offset because their minimum values |
| 51 // are respectively 0 and 1 but the formula requires at least a value of 2. | 52 // are respectively 0 and 1 but the formula requires at least a value of 2. |
| 52 return -log((time - use_date_).InDays() + 2) / log(use_count_ + 1); | 53 return -log((time - use_date_).InDays() + 2) / log(use_count_ + 1); |
| 53 } | 54 } |
| 54 | 55 |
| 55 } // namespace autofill | 56 } // namespace autofill |
| OLD | NEW |