[AF] Profile time precision consistency

Interacting with database/sync converts dates between time_t/Time.
That causes some inconsistencies.

Solution: when comparing two profiles' use_dates discard the
microsecnods.

Bug: 923965
Change-Id: I5491e43bd068917524e3773431523b86522b1de2
Reviewed-on: https://chromium-review.googlesource.com/c/1407779
Reviewed-by: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Commit-Queue: Parastoo Geranmayeh <parastoog@google.com>
Cr-Commit-Position: refs/heads/master@{#624779}
diff --git a/components/autofill/core/browser/autofill_data_model.cc b/components/autofill/core/browser/autofill_data_model.cc
index ba64963..913bfe2 100644
--- a/components/autofill/core/browser/autofill_data_model.cc
+++ b/components/autofill/core/browser/autofill_data_model.cc
@@ -16,11 +16,10 @@
 
 AutofillDataModel::AutofillDataModel(const std::string& guid,
                                      const std::string& origin)
-    : guid_(guid),
-      origin_(origin),
-      use_count_(1),
-      use_date_(AutofillClock::Now()),
-      modification_date_(AutofillClock::Now()) {}
+    : guid_(guid), origin_(origin), use_count_(1) {
+  set_use_date(AutofillClock::Now());
+  set_modification_date(AutofillClock::Now());
+}
 AutofillDataModel::~AutofillDataModel() {}
 
 bool AutofillDataModel::IsVerified() const {
@@ -30,7 +29,12 @@
 // TODO(crbug.com/629507): Add support for injected mock clock for testing.
 void AutofillDataModel::RecordUse() {
   ++use_count_;
-  use_date_ = AutofillClock::Now();
+  set_use_date(AutofillClock::Now());
+}
+
+bool AutofillDataModel::UseDateEqualsInSeconds(
+    const AutofillDataModel* other) const {
+  return !((other->use_date() - use_date()).InSeconds());
 }
 
 bool AutofillDataModel::CompareFrecency(const AutofillDataModel* other,
diff --git a/components/autofill/core/browser/autofill_data_model.h b/components/autofill/core/browser/autofill_data_model.h
index 538ef54..719df50 100644
--- a/components/autofill/core/browser/autofill_data_model.h
+++ b/components/autofill/core/browser/autofill_data_model.h
@@ -38,9 +38,14 @@
   size_t use_count() const { return use_count_; }
   void set_use_count(size_t count) { use_count_ = count; }
 
+  // Writing in and reading from database converts dates between time_t and
+  // Time, therefore the microseconds get lost. Therefore, we need to round the
+  // dates to seconds for both |use_date_| and |modification_date_|.
   const base::Time& use_date() const { return use_date_; }
   void set_use_date(const base::Time& time) { use_date_ = time; }
 
+  bool UseDateEqualsInSeconds(const AutofillDataModel* other) const;
+
   const base::Time& modification_date() const { return modification_date_; }
   // This should only be called from database code.
   void set_modification_date(const base::Time& time) {
@@ -85,10 +90,12 @@
   // The number of times this model has been used.
   size_t use_count_;
 
-  // The last time the model was used.
+  // The last time the model was used, rounded in seconds. Any change should
+  // use set_previous_use_date()
   base::Time use_date_;
 
-  // The last time data in the model was modified.
+  // The last time data in the model was modified, rounded in seconds. Any
+  // change should use set_previous_modification_date()
   base::Time modification_date_;
 
   // Returns a score based on both the recency (relative to |time|) and
diff --git a/components/autofill/core/browser/autofill_profile.cc b/components/autofill/core/browser/autofill_profile.cc
index 9bd7f27..4d1e041 100644
--- a/components/autofill/core/browser/autofill_profile.cc
+++ b/components/autofill/core/browser/autofill_profile.cc
@@ -454,8 +454,7 @@
 bool AutofillProfile::EqualsForSyncPurposes(const AutofillProfile& profile)
     const {
   return use_count() == profile.use_count() &&
-         use_date() == profile.use_date() &&
-         EqualsSansGuid(profile);
+         UseDateEqualsInSeconds(&profile) && EqualsSansGuid(profile);
 }
 
 bool AutofillProfile::EqualsForClientValidationPurpose(
@@ -471,7 +470,7 @@
 bool AutofillProfile::EqualsIncludingUsageStatsForTesting(
     const AutofillProfile& profile) const {
   return use_count() == profile.use_count() &&
-         use_date() == profile.use_date() && *this == profile;
+         UseDateEqualsInSeconds(&profile) && *this == profile;
 }
 
 bool AutofillProfile::operator==(const AutofillProfile& profile) const {
@@ -816,7 +815,7 @@
 }
 
 void AutofillProfile::RecordAndLogUse() {
-  previous_use_date_ = use_date();
+  set_previous_use_date(use_date());
   UMA_HISTOGRAM_COUNTS_1000("Autofill.DaysSinceLastUse.Profile",
                             (AutofillClock::Now() - use_date()).InDays());
   RecordUse();