Add configurable history retention setting (#34737)
Adds a privacy setting for controlling how long browsing history is kept, including options to disable history saving or keep history indefinitely. Updates the history expiration backend when the setting changes and shows a notice on the history page when saving is disabled. Co-authored-by: InjectorExe <68108452+InjectorExe@users.noreply.github.com>
This commit is contained in:
@@ -776,6 +776,37 @@
|
||||
<message name="IDS_SETTINGS_PUSH_MESSAGING" desc="Select value">
|
||||
Use Google services for push messaging
|
||||
</message>
|
||||
<!-- History retention -->
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_LABEL" desc="Label for the setting that controls how long browsing history is retained">
|
||||
Local history retention
|
||||
</message>
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_SUB_LABEL" desc="Description for the setting that controls how long browsing history is retained">
|
||||
How long Brave stores your browsing history locally
|
||||
</message>
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_ONE_DAY" desc="Dropdown option that keeps browsing history for one day">
|
||||
1 day
|
||||
</message>
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_ONE_WEEK" desc="Dropdown option that keeps browsing history for one week">
|
||||
1 week
|
||||
</message>
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_ONE_MONTH" desc="Dropdown option that keeps browsing history for one month">
|
||||
1 month
|
||||
</message>
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_THREE_MONTHS" desc="Dropdown option that keeps browsing history for three months">
|
||||
3 months
|
||||
</message>
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_SIX_MONTHS" desc="Dropdown option that keeps browsing history for six months">
|
||||
6 months
|
||||
</message>
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_ONE_YEAR" desc="Dropdown option that keeps browsing history for one year">
|
||||
1 year
|
||||
</message>
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_FIVE_YEARS" desc="Dropdown option that keeps browsing history for five years">
|
||||
5 years
|
||||
</message>
|
||||
<message name="IDS_SETTINGS_HISTORY_RETENTION_FOREVER" desc="Dropdown option that keeps browsing history until the user deletes it">
|
||||
Forever
|
||||
</message>
|
||||
<!-- Diagnostic reports -->
|
||||
<message name="IDS_BRAVE_DIAGNOSTIC_REPORTS_PREF" desc="The label of the checkbox to enable/disable sending diagnostic reports (crashes, hangs).">
|
||||
Automatically send diagnostic reports
|
||||
|
||||
@@ -65,6 +65,7 @@
|
||||
#include "components/content_settings/core/common/pref_names.h"
|
||||
#include "components/embedder_support/pref_names.h"
|
||||
#include "components/gcm_driver/gcm_buildflags.h"
|
||||
#include "components/history/core/common/pref_names.h"
|
||||
#include "components/ntp_tiles/tile_type.h"
|
||||
#include "components/omnibox/browser/omnibox_prefs.h"
|
||||
#include "components/password_manager/core/common/password_manager_pref_names.h"
|
||||
@@ -438,6 +439,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
|
||||
|
||||
registry->RegisterBooleanPref(kShieldsStatsBadgeVisible, true);
|
||||
registry->RegisterBooleanPref(kGoogleLoginControlType, true);
|
||||
registry->RegisterIntegerPref(prefs::kBraveHistoryRetentionDays, 90);
|
||||
registry->RegisterBooleanPref(
|
||||
query_filter::kTrackingQueryParametersFilteringEnabled, true);
|
||||
registry->RegisterBooleanPref(
|
||||
|
||||
@@ -130,6 +130,7 @@ source_set("extensions") {
|
||||
"//chrome/common",
|
||||
"//components/browsing_data/core",
|
||||
"//components/gcm_driver:gcm_buildflags",
|
||||
"//components/history/core/common",
|
||||
"//components/prefs",
|
||||
"//components/update_client:network_impl",
|
||||
"//components/user_prefs",
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "components/bookmarks/common/bookmark_pref_names.h"
|
||||
#include "components/browsing_data/core/pref_names.h"
|
||||
#include "components/gcm_driver/gcm_buildflags.h"
|
||||
#include "components/history/core/common/pref_names.h"
|
||||
#include "components/omnibox/browser/omnibox_prefs.h"
|
||||
#include "extensions/buildflags/buildflags.h"
|
||||
|
||||
@@ -317,6 +318,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetAllowlistedKeys() {
|
||||
#endif
|
||||
(*s_brave_allowlist)[prefs::kWebRTCIPHandlingPolicy] =
|
||||
settings_api::PrefType::kString;
|
||||
(*s_brave_allowlist)[prefs::kBraveHistoryRetentionDays] =
|
||||
settings_api::PrefType::kNumber;
|
||||
// Request OTR feature
|
||||
(*s_brave_allowlist)[request_otr::kRequestOTRActionOption] =
|
||||
settings_api::PrefType::kNumber;
|
||||
|
||||
@@ -28,6 +28,18 @@
|
||||
</cr-button>
|
||||
</template>
|
||||
</settings-toggle-button>
|
||||
<div class="settings-box">
|
||||
<div class="start" id="labelWrapper">
|
||||
<div class="label">$i18n{historyRetentionLabel}</div>
|
||||
<div class="secondary label" id="sub-label">
|
||||
$i18n{historyRetentionSubLabel}
|
||||
</div>
|
||||
</div>
|
||||
<settings-dropdown-menu id="historyRetention"
|
||||
pref="{{prefs.brave.history.retention_days}}"
|
||||
menu-options="[[historyRetentionOptions_]]">
|
||||
</settings-dropdown-menu>
|
||||
</div>
|
||||
<settings-toggle-button
|
||||
class="cr-row"
|
||||
pref="{{prefs.brave.de_amp.enabled}}"
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
import { assert } from 'chrome://resources/js/assert.js'
|
||||
import { SettingsToggleButtonElement } from '../controls/settings_toggle_button.js'
|
||||
|
||||
import '../controls/settings_dropdown_menu.js'
|
||||
import '../privacy_page/do_not_track_toggle.js'
|
||||
|
||||
const SettingsBravePersonalizationOptionsBase = WebUiListenerMixin(
|
||||
@@ -61,6 +62,43 @@ export class SettingsBravePersonalizationOptions extends SettingsBravePersonaliz
|
||||
},
|
||||
},
|
||||
webRTCPolicy_: String,
|
||||
historyRetentionOptions_: {
|
||||
readOnly: true,
|
||||
type: Array,
|
||||
value: function () {
|
||||
return [
|
||||
{ value: 1, name: loadTimeData.getString('historyRetentionOneDay') },
|
||||
{
|
||||
value: 7,
|
||||
name: loadTimeData.getString('historyRetentionOneWeek'),
|
||||
},
|
||||
{
|
||||
value: 30,
|
||||
name: loadTimeData.getString('historyRetentionOneMonth'),
|
||||
},
|
||||
{
|
||||
value: 90,
|
||||
name: loadTimeData.getString('historyRetentionThreeMonths'),
|
||||
},
|
||||
{
|
||||
value: 180,
|
||||
name: loadTimeData.getString('historyRetentionSixMonths'),
|
||||
},
|
||||
{
|
||||
value: 365,
|
||||
name: loadTimeData.getString('historyRetentionOneYear'),
|
||||
},
|
||||
{
|
||||
value: 365 * 5,
|
||||
name: loadTimeData.getString('historyRetentionFiveYears'),
|
||||
},
|
||||
{
|
||||
value: -1,
|
||||
name: loadTimeData.getString('historyRetentionForever'),
|
||||
},
|
||||
]
|
||||
},
|
||||
},
|
||||
isDebounceFeatureEnabled_: {
|
||||
readOnly: true,
|
||||
type: Boolean,
|
||||
@@ -105,6 +143,7 @@ export class SettingsBravePersonalizationOptions extends SettingsBravePersonaliz
|
||||
|
||||
declare private webRTCPolicies_: Object[]
|
||||
declare private webRTCPolicy_: String
|
||||
declare private historyRetentionOptions_: Object[]
|
||||
declare private isDebounceFeatureEnabled_: boolean
|
||||
declare private isRequestOTRFeatureEnabled_: boolean
|
||||
declare private requestOTRActions_: Object[]
|
||||
|
||||
@@ -466,6 +466,17 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
|
||||
{"webRTCPolicySubLabel", IDS_SETTINGS_WEBRTC_POLICY_SUB_LABEL},
|
||||
{"webRTCDefault", IDS_SETTINGS_WEBRTC_POLICY_DEFAULT},
|
||||
{"pushMessagingLabel", IDS_SETTINGS_PUSH_MESSAGING},
|
||||
{"historyRetentionLabel", IDS_SETTINGS_HISTORY_RETENTION_LABEL},
|
||||
{"historyRetentionSubLabel", IDS_SETTINGS_HISTORY_RETENTION_SUB_LABEL},
|
||||
{"historyRetentionOneDay", IDS_SETTINGS_HISTORY_RETENTION_ONE_DAY},
|
||||
{"historyRetentionOneWeek", IDS_SETTINGS_HISTORY_RETENTION_ONE_WEEK},
|
||||
{"historyRetentionOneMonth", IDS_SETTINGS_HISTORY_RETENTION_ONE_MONTH},
|
||||
{"historyRetentionThreeMonths",
|
||||
IDS_SETTINGS_HISTORY_RETENTION_THREE_MONTHS},
|
||||
{"historyRetentionSixMonths", IDS_SETTINGS_HISTORY_RETENTION_SIX_MONTHS},
|
||||
{"historyRetentionOneYear", IDS_SETTINGS_HISTORY_RETENTION_ONE_YEAR},
|
||||
{"historyRetentionFiveYears", IDS_SETTINGS_HISTORY_RETENTION_FIVE_YEARS},
|
||||
{"historyRetentionForever", IDS_SETTINGS_HISTORY_RETENTION_FOREVER},
|
||||
{"defaultPublicAndPrivateInterfaces",
|
||||
IDS_SETTINGS_WEBRTC_POLICY_DEFAULT_PUBLIC_AND_PRIVATE_INTERFACES},
|
||||
{"defaultPublicInterfaceOnly",
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/* Copyright (c) 2026 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "chrome/browser/profiles/profile.h"
|
||||
|
||||
#define BRAVE_BUILD_HISTORY_SERVICE \
|
||||
history_service->InitHistoryRetentionPref( \
|
||||
Profile::FromBrowserContext(context)->GetPrefs());
|
||||
|
||||
#include <chrome/browser/history/history_service_factory.cc>
|
||||
|
||||
#undef BRAVE_BUILD_HISTORY_SERVICE
|
||||
@@ -0,0 +1,42 @@
|
||||
/* Copyright (c) 2026 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#define BRAVE_EXPIRE_HISTORY_BACKEND_START_EXPIRING_OLD_STUFF \
|
||||
work_queue_ = {}; \
|
||||
weak_factory_.InvalidateWeakPtrs(); \
|
||||
if (expiration_threshold_.is_max()) { \
|
||||
return; \
|
||||
}
|
||||
|
||||
#include <components/history/core/browser/expire_history_backend.cc>
|
||||
|
||||
namespace history {
|
||||
|
||||
void ExpireHistoryBackend::UpdateExpirationThreshold(
|
||||
base::TimeDelta threshold) {
|
||||
// StartExpiringOldStuff() schedules a self-perpetuating cleanup loop, so pref
|
||||
// changes must update the running loop instead of starting another one. If
|
||||
// the new value is "forever", cancel pending expiration work. If the previous
|
||||
// value was "forever", the loop is no longer running and must be started
|
||||
// once. Otherwise the existing loop will pick up the new threshold on its
|
||||
// next pass.
|
||||
if (threshold.is_max()) {
|
||||
expiration_threshold_ = threshold;
|
||||
work_queue_ = {};
|
||||
weak_factory_.InvalidateWeakPtrs();
|
||||
return;
|
||||
}
|
||||
|
||||
if (expiration_threshold_.is_max()) {
|
||||
StartExpiringOldStuff(threshold);
|
||||
return;
|
||||
}
|
||||
|
||||
expiration_threshold_ = threshold;
|
||||
}
|
||||
|
||||
} // namespace history
|
||||
|
||||
#undef BRAVE_EXPIRE_HISTORY_BACKEND_START_EXPIRING_OLD_STUFF
|
||||
@@ -0,0 +1,17 @@
|
||||
/* Copyright (c) 2026 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_EXPIRE_HISTORY_BACKEND_H_
|
||||
#define BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_EXPIRE_HISTORY_BACKEND_H_
|
||||
|
||||
#define ClearOldOnDemandFaviconsIfPossible(...) \
|
||||
ClearOldOnDemandFaviconsIfPossible(__VA_ARGS__); \
|
||||
void UpdateExpirationThreshold(base::TimeDelta threshold)
|
||||
|
||||
#include <components/history/core/browser/expire_history_backend.h> // IWYU pragma: export
|
||||
|
||||
#undef ClearOldOnDemandFaviconsIfPossible
|
||||
|
||||
#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_EXPIRE_HISTORY_BACKEND_H_
|
||||
@@ -12,4 +12,8 @@ HistoryCountResult HistoryBackend::GetKnownToSyncCount() {
|
||||
return {db_ && db_->GetKnownToSyncCount(&count), count};
|
||||
}
|
||||
|
||||
void HistoryBackend::UpdateExpirationThreshold(base::TimeDelta threshold) {
|
||||
expirer_.UpdateExpirationThreshold(threshold);
|
||||
}
|
||||
|
||||
} // namespace history
|
||||
|
||||
@@ -6,8 +6,11 @@
|
||||
#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_BACKEND_H_
|
||||
#define BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_BACKEND_H_
|
||||
|
||||
#define GetHistoryCount \
|
||||
GetKnownToSyncCount(); \
|
||||
#include "base/time/time.h"
|
||||
|
||||
#define GetHistoryCount \
|
||||
GetKnownToSyncCount(); \
|
||||
void UpdateExpirationThreshold(base::TimeDelta threshold); \
|
||||
HistoryCountResult GetHistoryCount
|
||||
|
||||
#include <components/history/core/browser/history_backend.h> // IWYU pragma: export
|
||||
|
||||
@@ -3,10 +3,29 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "components/history/core/browser/history_service.h"
|
||||
|
||||
#include "components/history/core/common/pref_names.h"
|
||||
#include "components/prefs/pref_service.h"
|
||||
|
||||
#include <components/history/core/browser/history_service.cc>
|
||||
|
||||
namespace history {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int kDefaultHistoryRetentionDays = 90;
|
||||
constexpr int kKeepHistoryForever = -1;
|
||||
|
||||
base::TimeDelta HistoryRetentionDaysToTimeDelta(int days) {
|
||||
if (days == kKeepHistoryForever) {
|
||||
return base::TimeDelta::Max();
|
||||
}
|
||||
return base::Days(days > 0 ? days : kDefaultHistoryRetentionDays);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void HistoryService::GetKnownToSyncCount(
|
||||
base::OnceCallback<void(HistoryCountResult)> callback) {
|
||||
backend_task_runner_->PostTaskAndReplyWithResult(
|
||||
@@ -15,4 +34,22 @@ void HistoryService::GetKnownToSyncCount(
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void HistoryService::InitHistoryRetentionPref(PrefService* prefs) {
|
||||
DCHECK(prefs);
|
||||
|
||||
history_retention_days_.Init(
|
||||
prefs::kBraveHistoryRetentionDays, prefs,
|
||||
base::BindRepeating(&HistoryService::OnHistoryRetentionDaysChanged,
|
||||
base::Unretained(this)));
|
||||
OnHistoryRetentionDaysChanged();
|
||||
}
|
||||
|
||||
void HistoryService::OnHistoryRetentionDaysChanged() {
|
||||
const base::TimeDelta threshold =
|
||||
HistoryRetentionDaysToTimeDelta(history_retention_days_.GetValue());
|
||||
backend_task_runner_->PostTask(
|
||||
FROM_HERE, base::BindOnce(&HistoryBackend::UpdateExpirationThreshold,
|
||||
history_backend_, threshold));
|
||||
}
|
||||
|
||||
} // namespace history
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_SERVICE_H_
|
||||
#define BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_SERVICE_H_
|
||||
|
||||
#include "components/prefs/pref_member.h"
|
||||
|
||||
class PrefService;
|
||||
|
||||
class BraveHistoryURLProviderTest;
|
||||
class BraveHistoryQuickProviderTest;
|
||||
|
||||
@@ -18,6 +22,13 @@ class BraveHistoryQuickProviderTest;
|
||||
#define AddRelatedSearchesForVisit \
|
||||
GetKnownToSyncCount( \
|
||||
base::OnceCallback<void(history::HistoryCountResult)> callback); \
|
||||
void InitHistoryRetentionPref(PrefService* prefs); \
|
||||
void OnHistoryRetentionDaysChanged(); \
|
||||
\
|
||||
private: \
|
||||
IntegerPrefMember history_retention_days_; \
|
||||
\
|
||||
public: \
|
||||
void AddRelatedSearchesForVisit
|
||||
|
||||
#include <components/history/core/browser/history_service.h> // IWYU pragma: export
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/* Copyright (c) 2026 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_COMMON_PREF_NAMES_H_
|
||||
#define BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_COMMON_PREF_NAMES_H_
|
||||
|
||||
#include <components/history/core/common/pref_names.h> // IWYU pragma: export
|
||||
|
||||
namespace prefs {
|
||||
|
||||
// Number of days to retain browsing history. -1 keeps history forever.
|
||||
inline constexpr char kBraveHistoryRetentionDays[] =
|
||||
"brave.history.retention_days";
|
||||
|
||||
} // namespace prefs
|
||||
|
||||
#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_COMMON_PREF_NAMES_H_
|
||||
@@ -6,12 +6,19 @@
|
||||
source_set("unit_tests") {
|
||||
testonly = true
|
||||
sources = [
|
||||
"//brave/components/history/core/browser/brave_expire_history_backend_unittest.cc",
|
||||
"//brave/components/history/core/browser/brave_visit_database_unittest.cc",
|
||||
]
|
||||
deps = [
|
||||
"//base",
|
||||
"//base/test:test_support",
|
||||
"//components/history/core/browser",
|
||||
"//components/history/core/common",
|
||||
"//components/history/core/test",
|
||||
"//components/prefs:test_support",
|
||||
"//sql:test_support",
|
||||
"//testing/gtest",
|
||||
"//ui/base",
|
||||
"//url",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
include_rules = [
|
||||
"+components/history",
|
||||
"+components/prefs",
|
||||
"+components/sync",
|
||||
"+sql/test",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
/* Copyright (c) 2026 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/files/scoped_temp_dir.h"
|
||||
#include "base/run_loop.h"
|
||||
#include "base/task/cancelable_task_tracker.h"
|
||||
#include "base/test/task_environment.h"
|
||||
#include "base/test/test_future.h"
|
||||
#include "base/time/time.h"
|
||||
#include "components/history/core/browser/expire_history_backend.h"
|
||||
#include "components/history/core/browser/history_backend_notifier.h"
|
||||
#include "components/history/core/browser/history_constants.h"
|
||||
#include "components/history/core/browser/history_database.h"
|
||||
#include "components/history/core/browser/history_service.h"
|
||||
#include "components/history/core/browser/history_types.h"
|
||||
#include "components/history/core/common/pref_names.h"
|
||||
#include "components/history/core/test/history_service_test_util.h"
|
||||
#include "components/history/core/test/test_history_database.h"
|
||||
#include "components/prefs/pref_registry_simple.h"
|
||||
#include "components/prefs/testing_pref_service.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "ui/base/page_transition_types.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
namespace history {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr char kExpiredUrl[] = "https://expired.example/";
|
||||
constexpr char kCurrentUrl[] = "https://current.example/";
|
||||
constexpr char kForeverUrl[] = "https://forever.example/";
|
||||
|
||||
bool QueryURL(HistoryService* history_service,
|
||||
const GURL& url,
|
||||
base::CancelableTaskTracker* tracker) {
|
||||
base::test::TestFuture<QueryURLResult> future;
|
||||
history_service->QueryURL(url, future.GetCallback(), tracker);
|
||||
return future.Get().success;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
class BraveExpireHistoryBackendTest : public testing::Test,
|
||||
public HistoryBackendNotifier {
|
||||
public:
|
||||
BraveExpireHistoryBackendTest()
|
||||
: expirer_(this,
|
||||
/*backend_client=*/nullptr,
|
||||
task_environment_.GetMainThreadTaskRunner()) {}
|
||||
|
||||
protected:
|
||||
base::test::TaskEnvironment task_environment_{
|
||||
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
|
||||
ExpireHistoryBackend expirer_;
|
||||
|
||||
void SetUp() override {
|
||||
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
||||
|
||||
main_db_ = std::make_unique<TestHistoryDatabase>();
|
||||
ASSERT_EQ(sql::INIT_OK,
|
||||
main_db_->Init(temp_dir_.GetPath().Append(kHistoryFilename)));
|
||||
expirer_.SetDatabases(main_db_.get(), /*favicon_db=*/nullptr);
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
expirer_.SetDatabases(nullptr, nullptr);
|
||||
main_db_.reset();
|
||||
}
|
||||
|
||||
URLID AddPageWithVisit(const GURL& url, base::Time visit_time) {
|
||||
URLRow url_row(url);
|
||||
url_row.set_last_visit(visit_time);
|
||||
url_row.set_visit_count(1);
|
||||
const URLID url_id = main_db_->AddURL(url_row);
|
||||
|
||||
VisitRow visit_row;
|
||||
visit_row.url_id = url_id;
|
||||
visit_row.visit_time = visit_time;
|
||||
visit_row.transition = ui::PAGE_TRANSITION_TYPED;
|
||||
visit_row.source = SOURCE_BROWSED;
|
||||
EXPECT_TRUE(main_db_->AddVisit(&visit_row));
|
||||
return url_id;
|
||||
}
|
||||
|
||||
bool HasURL(URLID url_id) {
|
||||
URLRow url_row;
|
||||
return main_db_->GetURLRow(url_id, &url_row);
|
||||
}
|
||||
|
||||
void RunExpirationTimer() {
|
||||
task_environment_.FastForwardBy(base::Seconds(31));
|
||||
}
|
||||
|
||||
protected:
|
||||
// HistoryBackendNotifier:
|
||||
void NotifyFaviconsChanged(const std::set<GURL>& page_urls,
|
||||
const GURL& icon_url) override {}
|
||||
void NotifyURLVisited(VisitedURLInfo visited_url_info) override {}
|
||||
void NotifyURLsModified(const URLRows& changed_urls,
|
||||
bool is_from_expiration) override {}
|
||||
void NotifyDeletions(DeletionInfo deletion_info) override {}
|
||||
void NotifyVisitUpdated(const VisitRow& visit,
|
||||
VisitUpdateReason reason) override {}
|
||||
void NotifyVisitsDeleted(const std::vector<DeletedVisit>& visits) override {}
|
||||
|
||||
private:
|
||||
base::ScopedTempDir temp_dir_;
|
||||
std::unique_ptr<HistoryDatabase> main_db_;
|
||||
};
|
||||
|
||||
TEST_F(BraveExpireHistoryBackendTest, ExpiresOldVisitsForPositiveThreshold) {
|
||||
const URLID expired_url_id =
|
||||
AddPageWithVisit(GURL(kExpiredUrl), base::Time::Now() - base::Days(2));
|
||||
const URLID current_url_id =
|
||||
AddPageWithVisit(GURL(kCurrentUrl), base::Time::Now());
|
||||
|
||||
expirer_.StartExpiringOldStuff(base::Days(1));
|
||||
RunExpirationTimer();
|
||||
|
||||
EXPECT_FALSE(HasURL(expired_url_id));
|
||||
EXPECT_TRUE(HasURL(current_url_id));
|
||||
}
|
||||
|
||||
TEST_F(BraveExpireHistoryBackendTest, ForeverCancelsPendingExpiration) {
|
||||
const URLID expired_url_id =
|
||||
AddPageWithVisit(GURL(kExpiredUrl), base::Time::Now() - base::Days(2));
|
||||
|
||||
expirer_.StartExpiringOldStuff(base::Days(1));
|
||||
expirer_.StartExpiringOldStuff(base::TimeDelta::Max());
|
||||
RunExpirationTimer();
|
||||
|
||||
EXPECT_TRUE(HasURL(expired_url_id));
|
||||
}
|
||||
|
||||
class BraveHistoryRetentionPrefTest : public testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
||||
prefs_.registry()->RegisterIntegerPref(prefs::kBraveHistoryRetentionDays,
|
||||
90);
|
||||
history_service_ = CreateHistoryService(temp_dir_.GetPath(),
|
||||
/*create_db=*/true);
|
||||
ASSERT_TRUE(history_service_);
|
||||
history_service_->InitHistoryRetentionPref(&prefs_);
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
if (history_service_) {
|
||||
base::RunLoop run_loop;
|
||||
history_service_->SetOnBackendDestroyTask(run_loop.QuitClosure());
|
||||
history_service_.reset();
|
||||
run_loop.Run();
|
||||
}
|
||||
}
|
||||
|
||||
void AddPageWithVisit(const GURL& url, base::Time visit_time) {
|
||||
history_service_->AddPageWithDetails(url, std::u16string(),
|
||||
/*visit_count=*/1,
|
||||
/*typed_count=*/0, visit_time,
|
||||
/*hidden=*/false, SOURCE_BROWSED);
|
||||
BlockUntilHistoryProcessesPendingRequests(history_service_.get());
|
||||
}
|
||||
|
||||
void SetRetentionDays(int days) {
|
||||
prefs_.SetInteger(prefs::kBraveHistoryRetentionDays, days);
|
||||
BlockUntilHistoryProcessesPendingRequests(history_service_.get());
|
||||
}
|
||||
|
||||
void RunExpirationTimer() {
|
||||
task_environment_.FastForwardBy(base::Seconds(31));
|
||||
BlockUntilHistoryProcessesPendingRequests(history_service_.get());
|
||||
}
|
||||
|
||||
bool HasURL(const GURL& url) {
|
||||
return QueryURL(history_service_.get(), url, &tracker_);
|
||||
}
|
||||
|
||||
private:
|
||||
base::test::TaskEnvironment task_environment_{
|
||||
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
|
||||
base::ScopedTempDir temp_dir_;
|
||||
TestingPrefServiceSimple prefs_;
|
||||
std::unique_ptr<HistoryService> history_service_;
|
||||
base::CancelableTaskTracker tracker_;
|
||||
};
|
||||
|
||||
TEST_F(BraveHistoryRetentionPrefTest, PrefChangeExpiresOldVisits) {
|
||||
const GURL expired_url(kExpiredUrl);
|
||||
const GURL current_url(kCurrentUrl);
|
||||
AddPageWithVisit(expired_url, base::Time::Now() - base::Days(2));
|
||||
AddPageWithVisit(current_url, base::Time::Now());
|
||||
|
||||
SetRetentionDays(1);
|
||||
RunExpirationTimer();
|
||||
|
||||
EXPECT_FALSE(HasURL(expired_url));
|
||||
EXPECT_TRUE(HasURL(current_url));
|
||||
}
|
||||
|
||||
TEST_F(BraveHistoryRetentionPrefTest, ForeverPrefKeepsOldVisits) {
|
||||
const GURL forever_url(kForeverUrl);
|
||||
AddPageWithVisit(forever_url, base::Time::Now() - base::Days(2));
|
||||
|
||||
SetRetentionDays(-1);
|
||||
RunExpirationTimer();
|
||||
|
||||
EXPECT_TRUE(HasURL(forever_url));
|
||||
}
|
||||
|
||||
} // namespace history
|
||||
@@ -0,0 +1,11 @@
|
||||
diff --git a/chrome/browser/history/history_service_factory.cc b/chrome/browser/history/history_service_factory.cc
|
||||
index f51a9c7b49118cddda8b75d38804c3e9875fef43..b17c0b2f00b86dd07cd46d2a127f75dcf759057f 100644
|
||||
--- a/chrome/browser/history/history_service_factory.cc
|
||||
+++ b/chrome/browser/history/history_service_factory.cc
|
||||
@@ -30,5 +30,6 @@ std::unique_ptr<KeyedService> BuildHistoryService(
|
||||
context->GetPath(), chrome::GetChannel()))) {
|
||||
return nullptr;
|
||||
}
|
||||
+ BRAVE_BUILD_HISTORY_SERVICE
|
||||
return history_service;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
diff --git a/components/history/core/browser/expire_history_backend.cc b/components/history/core/browser/expire_history_backend.cc
|
||||
index 193014683e670facf7192eccf392c800cb2be879..2351d061567bfe09d5a0421fcc6d0d7bc08e4287 100644
|
||||
--- a/components/history/core/browser/expire_history_backend.cc
|
||||
+++ b/components/history/core/browser/expire_history_backend.cc
|
||||
@@ -397,3 +397,4 @@ void ExpireHistoryBackend::StartExpiringOldStuff(
|
||||
// Remove all readers, just in case this was method was called before.
|
||||
readers_.clear();
|
||||
+ BRAVE_EXPIRE_HISTORY_BACKEND_START_EXPIRING_OLD_STUFF
|
||||
// For now, we explicitly add all known readers. If we come up with more
|
||||
Reference in New Issue
Block a user