| 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 "components/autofill/core/common/autofill_clock.h" |
| 10 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 11 | 12 |
| 12 namespace autofill { | 13 namespace autofill { |
| 13 | 14 |
| 14 AutofillDataModel::AutofillDataModel(const std::string& guid, | 15 AutofillDataModel::AutofillDataModel(const std::string& guid, |
| 15 const std::string& origin) | 16 const std::string& origin) |
| 16 : guid_(guid), | 17 : guid_(guid), |
| 17 origin_(origin), | 18 origin_(origin), |
| 18 use_count_(1), | 19 use_count_(1), |
| 19 use_date_(base::Time::Now()), | 20 use_date_(AutofillClock::Now()), |
| 20 modification_date_(base::Time::Now()) {} | 21 modification_date_(AutofillClock::Now()) {} |
| 21 AutofillDataModel::~AutofillDataModel() {} | 22 AutofillDataModel::~AutofillDataModel() {} |
| 22 | 23 |
| 23 bool AutofillDataModel::IsVerified() const { | 24 bool AutofillDataModel::IsVerified() const { |
| 24 return !origin_.empty() && !GURL(origin_).is_valid(); | 25 return !origin_.empty() && !GURL(origin_).is_valid(); |
| 25 } | 26 } |
| 26 | 27 |
| 27 // TODO(crbug.com/629507): Add support for injected mock clock for testing. | 28 // TODO(crbug.com/629507): Add support for injected mock clock for testing. |
| 28 void AutofillDataModel::RecordUse() { | 29 void AutofillDataModel::RecordUse() { |
| 29 ++use_count_; | 30 ++use_count_; |
| 30 use_date_ = base::Time::Now(); | 31 use_date_ = AutofillClock::Now(); |
| 31 } | 32 } |
| 32 | 33 |
| 33 bool AutofillDataModel::CompareFrecency(const AutofillDataModel* other, | 34 bool AutofillDataModel::CompareFrecency(const AutofillDataModel* other, |
| 34 base::Time comparison_time) const { | 35 base::Time comparison_time) const { |
| 35 double score = GetFrecencyScore(comparison_time); | 36 double score = GetFrecencyScore(comparison_time); |
| 36 double other_score = other->GetFrecencyScore(comparison_time); | 37 double other_score = other->GetFrecencyScore(comparison_time); |
| 37 | 38 |
| 38 // Ties are broken by MRU, then by GUID comparison. | 39 // Ties are broken by MRU, then by GUID comparison. |
| 39 if (score != other_score) | 40 if (score != other_score) |
| 40 return score > other_score; | 41 return score > other_score; |
| 41 | 42 |
| 42 if (use_date_ != other->use_date_) | 43 if (use_date_ != other->use_date_) |
| 43 return use_date_ > other->use_date_; | 44 return use_date_ > other->use_date_; |
| 44 | 45 |
| 45 return guid_ > other->guid_; | 46 return guid_ > other->guid_; |
| 46 } | 47 } |
| 47 | 48 |
| 48 double AutofillDataModel::GetFrecencyScore(base::Time time) const { | 49 double AutofillDataModel::GetFrecencyScore(base::Time time) const { |
| 49 // The formula calculates a score based on both the frequency and the recency | 50 // The formula calculates a score based on both the frequency and the recency |
| 50 // of the profile and leveraging the properties of the logarithmic function. | 51 // of the profile and leveraging the properties of the logarithmic function. |
| 51 // DaysSinceLastUse() and |use_count_| are offset because their minimum values | 52 // DaysSinceLastUse() and |use_count_| are offset because their minimum values |
| 52 // are respectively 0 and 1 but the formula requires at least a value of 2. | 53 // are respectively 0 and 1 but the formula requires at least a value of 2. |
| 53 // Please update getFrecencyScore in PaymentRequestImpl.java as well if below | 54 // Please update getFrecencyScore in PaymentRequestImpl.java as well if below |
| 54 // formula needs update. | 55 // formula needs update. |
| 55 return -log((time - use_date_).InDays() + 2) / log(use_count_ + 1); | 56 return -log((time - use_date_).InDays() + 2) / log(use_count_ + 1); |
| 56 } | 57 } |
| 57 | 58 |
| 58 } // namespace autofill | 59 } // namespace autofill |
| OLD | NEW |