Get chromium's major version directly instead of parsing strings

Getting Chromium's version is not trivial in code and some code
got the version string dynamically during runtime and parsed it.
Since several files defined the same helper function
GetCurrentMajorVersion, those clashed in some extreme
jumbo builds. This drops this function and instead gets the
version directly from the version system via a C/C++ define.

Since the version number now will be compiled into autofill,
it will have to recompile if the major version number changes.

Bug: 907570
Change-Id: I5823d977f3ff58c4b19f173c01031d4a9aa5c56c
Reviewed-on: https://chromium-review.googlesource.com/c/1409548
Reviewed-by: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Commit-Queue: Daniel Bratell <bratell@opera.com>
Cr-Commit-Position: refs/heads/master@{#622490}
diff --git a/components/autofill/core/browser/BUILD.gn b/components/autofill/core/browser/BUILD.gn
index 64cc0ec2..9520b25 100644
--- a/components/autofill/core/browser/BUILD.gn
+++ b/components/autofill/core/browser/BUILD.gn
@@ -4,6 +4,7 @@
 
 import("//build/config/chrome_build.gni")
 import("//build/config/jumbo.gni")
+import("//build/util/version.gni")
 import("//testing/libfuzzer/fuzzer_test.gni")
 
 jumbo_static_library("browser") {
@@ -269,6 +270,8 @@
     ]
   }
 
+  defines = [ "CHROME_VERSION_MAJOR=" + chrome_version_major ]
+
   configs += [ "//build/config:precompiled_headers" ]
 
   public_deps = [
@@ -553,6 +556,8 @@
     ]
   }
 
+  defines = [ "CHROME_VERSION_MAJOR=" + chrome_version_major ]
+
   deps = [
     ":browser",
     ":password_generator",
diff --git a/components/autofill/core/browser/autocomplete_history_manager.cc b/components/autofill/core/browser/autocomplete_history_manager.cc
index c11f9c35..c2005d2 100644
--- a/components/autofill/core/browser/autocomplete_history_manager.cc
+++ b/components/autofill/core/browser/autocomplete_history_manager.cc
@@ -40,10 +40,6 @@
       field.form_control_type == "email";
 }
 
-int GetCurrentMajorVersion() {
-  return atoi(version_info::GetVersionNumber().c_str());
-}
-
 }  // namespace
 
 void AutocompleteHistoryManager::UMARecorder::OnGetAutocompleteSuggestions(
@@ -127,13 +123,11 @@
   if (!is_off_the_record_ &&
       base::FeatureList::IsEnabled(
           autofill::features::kAutocompleteRetentionPolicyEnabled)) {
-    int current_major_version = GetCurrentMajorVersion();
-
     // Upon successful cleanup, the last cleaned-up major version is being
     // stored in this pref.
     int last_cleaned_version = pref_service_->GetInteger(
         prefs::kAutocompleteLastVersionRetentionPolicy);
-    if (current_major_version > last_cleaned_version) {
+    if (CHROME_VERSION_MAJOR > last_cleaned_version) {
       // Trigger the cleanup.
       profile_database_->RemoveExpiredAutocompleteEntries(this);
     }
@@ -341,7 +335,7 @@
 
   // Cleanup was successful, update the latest run milestone.
   pref_service_->SetInteger(prefs::kAutocompleteLastVersionRetentionPolicy,
-                            GetCurrentMajorVersion());
+                            CHROME_VERSION_MAJOR);
 }
 
 void AutocompleteHistoryManager::CancelAllPendingQueries() {
diff --git a/components/autofill/core/browser/autocomplete_history_manager_unittest.cc b/components/autofill/core/browser/autocomplete_history_manager_unittest.cc
index fea1cdc..1c84bfa5 100644
--- a/components/autofill/core/browser/autocomplete_history_manager_unittest.cc
+++ b/components/autofill/core/browser/autocomplete_history_manager_unittest.cc
@@ -95,11 +95,6 @@
 
   DISALLOW_COPY_AND_ASSIGN(MockSuggestionsHandler);
 };
-
-int GetCurrentMajorVersion() {
-  return atoi(version_info::GetVersionNumber().c_str());
-}
-
 }  // namespace
 
 class AutocompleteHistoryManagerTest : public testing::Test {
@@ -111,7 +106,7 @@
 
     // Mock such that we don't trigger the cleanup.
     prefs_->SetInteger(prefs::kAutocompleteLastVersionRetentionPolicy,
-                       GetCurrentMajorVersion());
+                       CHROME_VERSION_MAJOR);
 
     // Set time to some arbitrary date.
     test_clock.SetNow(base::Time::FromDoubleT(1546889367));
@@ -363,7 +358,7 @@
   scoped_features.InitAndEnableFeature(
       features::kAutocompleteRetentionPolicyEnabled);
   prefs_->SetInteger(prefs::kAutocompleteLastVersionRetentionPolicy,
-                     GetCurrentMajorVersion() - 1);
+                     CHROME_VERSION_MAJOR - 1);
 
   EXPECT_CALL(*web_data_service_,
               RemoveExpiredAutocompleteEntries(autocomplete_manager_.get()))
@@ -379,7 +374,7 @@
   scoped_features.InitAndEnableFeature(
       features::kAutocompleteRetentionPolicyEnabled);
   prefs_->SetInteger(prefs::kAutocompleteLastVersionRetentionPolicy,
-                     GetCurrentMajorVersion() - 1);
+                     CHROME_VERSION_MAJOR - 1);
 
   EXPECT_CALL(*web_data_service_,
               RemoveExpiredAutocompleteEntries(autocomplete_manager_.get()))
@@ -396,7 +391,7 @@
   scoped_features.InitAndDisableFeature(
       features::kAutocompleteRetentionPolicyEnabled);
   prefs_->SetInteger(prefs::kAutocompleteLastVersionRetentionPolicy,
-                     GetCurrentMajorVersion() - 1);
+                     CHROME_VERSION_MAJOR - 1);
 
   EXPECT_CALL(*web_data_service_,
               RemoveExpiredAutocompleteEntries(autocomplete_manager_.get()))
@@ -413,7 +408,7 @@
   scoped_features.InitAndEnableFeature(
       features::kAutocompleteRetentionPolicyEnabled);
   prefs_->SetInteger(prefs::kAutocompleteLastVersionRetentionPolicy,
-                     GetCurrentMajorVersion());
+                     CHROME_VERSION_MAJOR);
 
   EXPECT_CALL(*web_data_service_,
               RemoveExpiredAutocompleteEntries(autocomplete_manager_.get()))
@@ -1024,7 +1019,7 @@
       1, std::make_unique<WDResult<size_t>>(AUTOFILL_CLEANUP_RESULT,
                                             cleanup_result));
 
-  EXPECT_EQ(GetCurrentMajorVersion(),
+  EXPECT_EQ(CHROME_VERSION_MAJOR,
             prefs_->GetInteger(prefs::kAutocompleteLastVersionRetentionPolicy));
   histogram_tester.ExpectBucketCount("Autocomplete.Cleanup", cleanup_result, 1);
 }
diff --git a/components/autofill/core/browser/personal_data_manager.cc b/components/autofill/core/browser/personal_data_manager.cc
index f27da65..cae9c2d 100644
--- a/components/autofill/core/browser/personal_data_manager.cc
+++ b/components/autofill/core/browser/personal_data_manager.cc
@@ -141,11 +141,6 @@
                          const std::pair<std::string, int>& b) {
   return a.second < b.second;
 }
-
-int GetCurrentMajorVersion() {
-  return atoi(version_info::GetVersionNumber().c_str());
-}
-
 }  // namespace
 
 // Helper class to abstract the switching between account and profile storage
@@ -321,7 +316,7 @@
   // Check if profile cleanup has already been performed this major version.
   is_autofill_profile_cleanup_pending_ =
       pref_service_->GetInteger(prefs::kAutofillLastVersionDeduped) >=
-      GetCurrentMajorVersion();
+      CHROME_VERSION_MAJOR;
   DVLOG(1) << "Autofill profile cleanup "
            << (is_autofill_profile_cleanup_pending_ ? "needs to be"
                                                     : "has already been")
@@ -1001,10 +996,9 @@
 
   // The profiles' validity states need to be updated for each major version, to
   // keep up with the validation logic.
-  int current_major_version = GetCurrentMajorVersion();
   bool update_validation =
       pref_service_->GetInteger(prefs::kAutofillLastVersionValidated) <
-      current_major_version;
+      CHROME_VERSION_MAJOR;
   for (const auto* profile : profiles) {
     if (!profile->is_client_validity_states_updated() || update_validation) {
       client_profile_validator_->StartProfileValidation(
@@ -1015,7 +1009,7 @@
   // Set the pref to the current major version if already not set.
   if (update_validation)
     pref_service_->SetInteger(prefs::kAutofillLastVersionValidated,
-                              current_major_version);
+                              CHROME_VERSION_MAJOR);
 }
 
 std::vector<AutofillProfile*> PersonalDataManager::GetServerProfiles() const {
@@ -2114,9 +2108,8 @@
   }
 
   // Check if de-duplication has already been performed this major version.
-  int current_major_version = GetCurrentMajorVersion();
   if (pref_service_->GetInteger(prefs::kAutofillLastVersionDeduped) >=
-      current_major_version) {
+      CHROME_VERSION_MAJOR) {
     DVLOG(1)
         << "Autofill profile de-duplication already performed for this version";
     return false;
@@ -2147,7 +2140,7 @@
 
   // Set the pref to the current major version.
   pref_service_->SetInteger(prefs::kAutofillLastVersionDeduped,
-                            current_major_version);
+                            CHROME_VERSION_MAJOR);
   return true;
 }
 
diff --git a/components/autofill/core/browser/personal_data_manager_unittest.cc b/components/autofill/core/browser/personal_data_manager_unittest.cc
index b6a4276..30c166d 100644
--- a/components/autofill/core/browser/personal_data_manager_unittest.cc
+++ b/components/autofill/core/browser/personal_data_manager_unittest.cc
@@ -180,8 +180,7 @@
     personal_data_->pref_service_->SetInteger(
         prefs::kAutofillLastVersionDeduped, 0);
     personal_data_->pref_service_->SetInteger(
-        prefs::kAutofillLastVersionValidated,
-        atoi(version_info::GetVersionNumber().c_str()));
+        prefs::kAutofillLastVersionValidated, CHROME_VERSION_MAJOR);
   }
 
   void TearDownTest() {
@@ -7063,8 +7062,7 @@
                                           AutofillProfile::CLIENT));
 
   // Verify that the version of the last update is set to this version.
-  EXPECT_EQ(atoi(version_info::GetVersionNumber().c_str()),
-            GetLastVersionValidatedUpdate());
+  EXPECT_EQ(CHROME_VERSION_MAJOR, GetLastVersionValidatedUpdate());
 
   // Update should not update any validity state, because both the validity
   // state flag and the version are up to date.