diff --git a/build/patches/Allow-change-keep-navigation-history.patch b/build/patches/Allow-change-keep-navigation-history.patch new file mode 100644 index 00000000..1b4d982d --- /dev/null +++ b/build/patches/Allow-change-keep-navigation-history.patch @@ -0,0 +1,469 @@ +From: uazo +Date: Mon, 31 Jan 2022 12:49:39 +0000 +Subject: Add an option that allows changing the number of days to keep + navigation history + +Adds a new option in the privacy settings that allows the selection of +the days to keep in the browsing history. +Selecting the "Never" item activates the kSavingBrowserHistoryDisabled +flag and immediately deletes all the history. +--- + .../preference_spinner_single_line.xml | 26 ++++++-- + .../java/res/xml/privacy_preferences.xml | 6 ++ + .../privacy/settings/PrivacySettings.java | 61 +++++++++++++++++++ + .../history/history_service_factory.cc | 2 + + chrome/browser/preferences/BUILD.gn | 1 + + chrome/browser/profiles/profile_impl.cc | 1 + + .../strings/android_chrome_strings.grd | 9 +++ + .../java/res/layout/preference_spinner.xml | 7 +++ + .../layout/preference_spinner_single_line.xml | 7 +++ + .../settings/SpinnerPreference.java | 7 +++ + .../core/browser/expire_history_backend.cc | 6 ++ + .../core/browser/expire_history_backend.h | 2 + + .../history/core/browser/history_backend.cc | 8 +++ + .../history/core/browser/history_backend.h | 2 + + .../history/core/browser/history_service.cc | 31 ++++++++++ + .../history/core/browser/history_service.h | 8 +++ + components/history/core/common/pref_names.cc | 4 ++ + components/history/core/common/pref_names.h | 1 + + 18 files changed, 183 insertions(+), 6 deletions(-) + +diff --git a/chrome/android/java/res/layout-sw360dp/preference_spinner_single_line.xml b/chrome/android/java/res/layout-sw360dp/preference_spinner_single_line.xml +--- a/chrome/android/java/res/layout-sw360dp/preference_spinner_single_line.xml ++++ b/chrome/android/java/res/layout-sw360dp/preference_spinner_single_line.xml +@@ -11,19 +11,33 @@ + android:layout_height="wrap_content" + android:orientation="horizontal" + android:paddingEnd="@dimen/pref_spinner_padding_end" +- android:paddingTop="12dp" ++ android:paddingTop="0dp" + android:paddingBottom="6dp"> + +- ++ android:paddingBottom="12dp"> ++ ++ ++ ++ ++ ++ + + ++ + options = new ArrayList<>(); ++ options.add(new TimeFrequencySpinnerOption(0, ++ getActivity().getString(R.string.history_expire_days_never_record))); ++ options.add(new TimeFrequencySpinnerOption(1)); ++ options.add(new TimeFrequencySpinnerOption(7)); ++ options.add(new TimeFrequencySpinnerOption(15)); ++ options.add(new TimeFrequencySpinnerOption(30)); ++ options.add(new TimeFrequencySpinnerOption(60)); ++ options.add(new TimeFrequencySpinnerOption(90)); ++ TimeFrequencySpinnerOption[] spinnerOptions = options.toArray(new TimeFrequencySpinnerOption[0]); ++ ++ int selectedTimeFrequency = UserPrefs.get(Profile.getLastUsedRegularProfile()) ++ .getInteger(Pref.EXPIRE_DAYS_THRESHOLD); ++ int spinnerOptionIndex = -1; ++ for (int i = 0; i < spinnerOptions.length; ++i) { ++ if (spinnerOptions[i].getDays() == selectedTimeFrequency) { ++ spinnerOptionIndex = i; ++ break; ++ } ++ } ++ ++ SpinnerPreference spinner = (SpinnerPreference) findPreference(PREF_HISTORY_EXPIRE_DAYS_THRESHOLD); ++ spinner.setOptions(spinnerOptions, spinnerOptionIndex); ++ spinner.setSummary(getResources().getString(R.string.history_expire_days_threshold_summary)); ++ spinner.setOnPreferenceChangeListener((preference, newValue) -> { ++ UserPrefs.get(Profile.getLastUsedRegularProfile()) ++ .setInteger(Pref.EXPIRE_DAYS_THRESHOLD, ++ ((TimeFrequencySpinnerOption) newValue).getDays()); ++ return true; ++ }); ++ + Preference phoneAsASecurityKey = findPreference(PREF_PHONE_AS_A_SECURITY_KEY); + phoneAsASecurityKey.setVisible( + ChromeFeatureList.isEnabled(ChromeFeatureList.WEB_AUTH_PHONE_SUPPORT)); +@@ -320,6 +358,29 @@ public class PrivacySettings + }; + } + ++ class TimeFrequencySpinnerOption { ++ private int mDays; ++ private String mDescription; ++ ++ public TimeFrequencySpinnerOption(int days) { ++ this(days, Integer.toString(days)); ++ } ++ ++ public TimeFrequencySpinnerOption(int days, String description) { ++ mDays = days; ++ mDescription = description; ++ } ++ ++ public int getDays() { ++ return mDays; ++ } ++ ++ @Override ++ public String toString() { ++ return mDescription; ++ } ++ } ++ + /** + * This checks whether Autofill Assistant is enabled and was shown at least once (only then + * will the AA switch be assigned a value). +diff --git a/chrome/browser/history/history_service_factory.cc b/chrome/browser/history/history_service_factory.cc +--- a/chrome/browser/history/history_service_factory.cc ++++ b/chrome/browser/history/history_service_factory.cc +@@ -30,6 +30,8 @@ std::unique_ptr BuildHistoryService( + history::HistoryDatabaseParamsForPath(context->GetPath()))) { + return nullptr; + } ++ Profile* profile = Profile::FromBrowserContext(context); ++ history_service->InitFromPreferences(profile->GetPrefs()); + return history_service; + } + +diff --git a/chrome/browser/preferences/BUILD.gn b/chrome/browser/preferences/BUILD.gn +--- a/chrome/browser/preferences/BUILD.gn ++++ b/chrome/browser/preferences/BUILD.gn +@@ -40,6 +40,7 @@ java_cpp_strings("java_pref_names_srcjar") { + "//components/safe_browsing/core/common/safe_browsing_prefs.cc", + "//components/signin/public/base/signin_pref_names.cc", + "//components/translate/core/browser/translate_pref_names.cc", ++ "//components/history/core/common/pref_names.cc", + ] + + template = "android/java_templates/Pref.java.tmpl" +diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc +--- a/chrome/browser/profiles/profile_impl.cc ++++ b/chrome/browser/profiles/profile_impl.cc +@@ -371,6 +371,7 @@ std::unique_ptr Profile::CreateProfile(const base::FilePath& path, + void ProfileImpl::RegisterProfilePrefs( + user_prefs::PrefRegistrySyncable* registry) { + registry->RegisterBooleanPref(prefs::kSavingBrowserHistoryDisabled, false); ++ registry->RegisterIntegerPref(prefs::kExpireDaysThreshold, 90); + registry->RegisterBooleanPref(prefs::kAllowDeletingBrowserHistory, true); + registry->RegisterBooleanPref(prefs::kForceGoogleSafeSearch, false); + registry->RegisterIntegerPref(prefs::kForceYouTubeRestrict, +diff --git a/chrome/browser/ui/android/strings/android_chrome_strings.grd b/chrome/browser/ui/android/strings/android_chrome_strings.grd +--- a/chrome/browser/ui/android/strings/android_chrome_strings.grd ++++ b/chrome/browser/ui/android/strings/android_chrome_strings.grd +@@ -1014,6 +1014,15 @@ Your Google account may have other forms of browsing history like searches and a + + Browsing history + ++ ++ Expire history days threshold ++ ++ ++ Never ++ ++ ++ Number of days to keep navigation history ++ + + Cookies and site data + +diff --git a/components/browser_ui/settings/android/java/res/layout/preference_spinner.xml b/components/browser_ui/settings/android/java/res/layout/preference_spinner.xml +--- a/components/browser_ui/settings/android/java/res/layout/preference_spinner.xml ++++ b/components/browser_ui/settings/android/java/res/layout/preference_spinner.xml +@@ -18,6 +18,13 @@ + android:layout_width="match_parent" + style="@style/PreferenceTitle" /> + ++ ++ + + ++ ++ + PostTask( ++ FROM_HERE, base::BindOnce(&HistoryBackend::DeleteAllHistory, this)); ++ } ++ expirer_.SetExpireDaysThreshold(base::Days(days)); ++} ++ + void HistoryBackend::OnMemoryPressure( + base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { + // TODO(sebmarchand): Check if MEMORY_PRESSURE_LEVEL_MODERATE should also be +diff --git a/components/history/core/browser/history_backend.h b/components/history/core/browser/history_backend.h +--- a/components/history/core/browser/history_backend.h ++++ b/components/history/core/browser/history_backend.h +@@ -211,6 +211,8 @@ class HistoryBackend : public base::RefCountedThreadSafe, + void Init(bool force_fail, + const HistoryDatabaseParams& history_database_params); + ++ void SetExpireDaysThreshold(int days); ++ + // Notification that the history system is shutting down. This will break + // the refs owned by the delegate and any pending transaction so it will + // actually be deleted. +diff --git a/components/history/core/browser/history_service.cc b/components/history/core/browser/history_service.cc +--- a/components/history/core/browser/history_service.cc ++++ b/components/history/core/browser/history_service.cc +@@ -35,6 +35,9 @@ + #include "base/time/time.h" + #include "base/trace_event/trace_event.h" + #include "build/build_config.h" ++#include "components/prefs/pref_service.h" ++#include "components/prefs/pref_change_registrar.h" ++#include "components/history/core/common/pref_names.h" + #include "components/history/core/browser/download_row.h" + #include "components/history/core/browser/history_backend.h" + #include "components/history/core/browser/history_backend_client.h" +@@ -1036,6 +1039,9 @@ void HistoryService::Cleanup() { + return; + } + ++ active_user_pref_change_registrar_.reset(); ++ active_user_pref_service_ = nullptr; ++ + NotifyHistoryServiceBeingDeleted(); + + weak_ptr_factory_.InvalidateWeakPtrs(); +@@ -1100,6 +1106,31 @@ bool HistoryService::Init( + return true; + } + ++void HistoryService::InitFromPreferences(PrefService* prefs) { ++ DCHECK(prefs); ++ ++ active_user_pref_service_ = prefs; ++ OnUserPrefChanged(); ++ ++ active_user_pref_change_registrar_ = std::make_unique(); ++ active_user_pref_change_registrar_->Init(prefs); ++ active_user_pref_change_registrar_->Add( ++ prefs::kExpireDaysThreshold, ++ base::BindRepeating( ++ &HistoryService::OnUserPrefChanged, ++ base::Unretained(this))); ++} ++ ++void HistoryService::OnUserPrefChanged() { ++ DCHECK(active_user_pref_service_); ++ ++ int kExpireDaysThreshold = ++ active_user_pref_service_->GetInteger(prefs::kExpireDaysThreshold); ++ active_user_pref_service_->SetBoolean(prefs::kSavingBrowserHistoryDisabled, ++ (kExpireDaysThreshold == 0)); ++ history_backend_->SetExpireDaysThreshold(kExpireDaysThreshold); ++} ++ + void HistoryService::ScheduleAutocomplete( + base::OnceCallback callback) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); +diff --git a/components/history/core/browser/history_service.h b/components/history/core/browser/history_service.h +--- a/components/history/core/browser/history_service.h ++++ b/components/history/core/browser/history_service.h +@@ -31,6 +31,8 @@ + #include "build/build_config.h" + #include "components/favicon_base/favicon_callback.h" + #include "components/favicon_base/favicon_usage_data.h" ++#include "components/prefs/pref_service.h" ++#include "components/prefs/pref_change_registrar.h" + #include "components/history/core/browser/history_types.h" + #include "components/history/core/browser/keyword_id.h" + #include "components/keyed_service/core/keyed_service.h" +@@ -105,6 +107,8 @@ class HistoryService : public KeyedService { + return Init(false, history_database_params); + } + ++ void InitFromPreferences(PrefService* prefs); ++ + // Triggers the backend to load if it hasn't already, and then returns whether + // it's finished loading. + // Note: Virtual needed for mocking. +@@ -980,6 +984,10 @@ class HistoryService : public KeyedService { + + base::OnceClosure origin_queried_closure_for_testing_; + ++ void OnUserPrefChanged(); ++ PrefService* active_user_pref_service_ = nullptr; ++ std::unique_ptr active_user_pref_change_registrar_; ++ + // All vended weak pointers are invalidated in Cleanup(). + base::WeakPtrFactory weak_ptr_factory_{this}; + }; +diff --git a/components/history/core/common/pref_names.cc b/components/history/core/common/pref_names.cc +--- a/components/history/core/common/pref_names.cc ++++ b/components/history/core/common/pref_names.cc +@@ -9,4 +9,8 @@ namespace prefs { + // Boolean controlling whether history saving is disabled. + const char kSavingBrowserHistoryDisabled[] = "history.saving_disabled"; + ++// The number of days old a history entry can be before it is considered "old" ++// and is deleted. ++const char kExpireDaysThreshold[] = "history.expire_days_threshold"; ++ + } // namespace prefs +diff --git a/components/history/core/common/pref_names.h b/components/history/core/common/pref_names.h +--- a/components/history/core/common/pref_names.h ++++ b/components/history/core/common/pref_names.h +@@ -12,6 +12,7 @@ + namespace prefs { + + extern const char kSavingBrowserHistoryDisabled[]; ++extern const char kExpireDaysThreshold[]; + + } // namespace prefs + +-- +2.25.1