-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
306 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* Copyright (c) 2025 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 "brave/components/speedreader/speedreader_metrics.h" | ||
|
||
#include "base/metrics/histogram_macros.h" | ||
#include "brave/components/p3a_utils/bucket.h" | ||
#include "brave/components/speedreader/speedreader_pref_names.h" | ||
#include "components/content_settings/core/browser/host_content_settings_map.h" | ||
#include "components/content_settings/core/common/content_settings_pattern.h" | ||
#include "components/content_settings/core/common/content_settings_types.h" | ||
#include "components/prefs/pref_registry_simple.h" | ||
#include "components/prefs/pref_service.h" | ||
|
||
namespace speedreader { | ||
|
||
namespace { | ||
|
||
constexpr int kPageViewsBuckets[] = {5, 10, 20, 30}; | ||
|
||
constexpr base::TimeDelta kUpdateInterval = base::Days(1); | ||
|
||
enum class EnabledSitesMetricValue { | ||
kNone = 0, // No sites enabled | ||
kOne = 1, // 1 site enabled | ||
kMultiple = 2, // 2+ sites enabled | ||
kAll = 3, // All sites enabled | ||
kMaxValue = kAll | ||
}; | ||
|
||
} // namespace | ||
|
||
SpeedreaderMetrics::SpeedreaderMetrics( | ||
PrefService* local_state, | ||
HostContentSettingsMap* host_content_settings_map, | ||
bool is_enabled_for_all_sites) | ||
: page_views_storage_(local_state, kSpeedreaderPageViewsStoragePref), | ||
host_content_settings_map_(host_content_settings_map), | ||
local_state_(local_state) { | ||
ReportPageViews(); | ||
UpdateEnabledSitesMetric(is_enabled_for_all_sites); | ||
} | ||
|
||
SpeedreaderMetrics::~SpeedreaderMetrics() = default; | ||
|
||
// static | ||
void SpeedreaderMetrics::RegisterPrefs(PrefRegistrySimple* registry) { | ||
registry->RegisterListPref(kSpeedreaderPageViewsStoragePref); | ||
} | ||
|
||
void SpeedreaderMetrics::RecordPageView() { | ||
page_views_storage_.AddDelta(1); | ||
ReportPageViews(); | ||
} | ||
|
||
void SpeedreaderMetrics::UpdateEnabledSitesMetric( | ||
bool is_enabled_for_all_sites) { | ||
EnabledSitesMetricValue value = EnabledSitesMetricValue::kNone; | ||
|
||
if (is_enabled_for_all_sites) { | ||
value = EnabledSitesMetricValue::kAll; | ||
} else if (host_content_settings_map_) { | ||
auto settings = host_content_settings_map_->GetSettingsForOneType( | ||
ContentSettingsType::BRAVE_SPEEDREADER); | ||
size_t enabled_sites_count = | ||
std::ranges::count_if(settings, [](const auto& setting) { | ||
return setting.GetContentSetting() == CONTENT_SETTING_ALLOW; | ||
}); | ||
if (enabled_sites_count == 1) { | ||
value = EnabledSitesMetricValue::kOne; | ||
} else if (enabled_sites_count > 1) { | ||
value = EnabledSitesMetricValue::kMultiple; | ||
} | ||
} | ||
|
||
UMA_HISTOGRAM_ENUMERATION(kSpeedreaderEnabledSitesHistogramName, value); | ||
} | ||
|
||
void SpeedreaderMetrics::ReportPageViews() { | ||
int page_views = page_views_storage_.GetMonthlySum(); | ||
if (page_views > 0) { | ||
p3a_utils::RecordToHistogramBucket(kSpeedreaderPageViewsHistogramName, | ||
kPageViewsBuckets, page_views); | ||
} | ||
|
||
update_timer_.Start(FROM_HERE, base::Time::Now() + kUpdateInterval, this, | ||
&SpeedreaderMetrics::ReportPageViews); | ||
} | ||
|
||
} // namespace speedreader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* Copyright (c) 2025 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_COMPONENTS_SPEEDREADER_SPEEDREADER_METRICS_H_ | ||
#define BRAVE_COMPONENTS_SPEEDREADER_SPEEDREADER_METRICS_H_ | ||
|
||
#include "base/timer/wall_clock_timer.h" | ||
#include "brave/components/time_period_storage/monthly_storage.h" | ||
|
||
class PrefRegistrySimple; | ||
class PrefService; | ||
class HostContentSettingsMap; | ||
|
||
namespace speedreader { | ||
|
||
inline constexpr char kSpeedreaderPageViewsHistogramName[] = | ||
"Brave.Speedreader.PageViews"; | ||
inline constexpr char kSpeedreaderEnabledSitesHistogramName[] = | ||
"Brave.Speedreader.EnabledSites"; | ||
|
||
class SpeedreaderMetrics { | ||
public: | ||
SpeedreaderMetrics(PrefService* local_state, | ||
HostContentSettingsMap* host_content_settings_map, | ||
bool is_enabled_for_all_sites); | ||
~SpeedreaderMetrics(); | ||
|
||
SpeedreaderMetrics(const SpeedreaderMetrics&) = delete; | ||
SpeedreaderMetrics& operator=(const SpeedreaderMetrics&) = delete; | ||
|
||
static void RegisterPrefs(PrefRegistrySimple* registry); | ||
|
||
void RecordPageView(); | ||
|
||
void UpdateEnabledSitesMetric(bool is_enabled_for_all_sites); | ||
|
||
private: | ||
void ReportPageViews(); | ||
|
||
MonthlyStorage page_views_storage_; | ||
raw_ptr<HostContentSettingsMap> host_content_settings_map_; | ||
raw_ptr<PrefService> local_state_; | ||
|
||
base::WallClockTimer update_timer_; | ||
}; | ||
|
||
} // namespace speedreader | ||
|
||
#endif // BRAVE_COMPONENTS_SPEEDREADER_SPEEDREADER_METRICS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* Copyright (c) 2025 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 "brave/components/speedreader/speedreader_metrics.h" | ||
|
||
#include <memory> | ||
|
||
#include "base/test/metrics/histogram_tester.h" | ||
#include "base/test/task_environment.h" | ||
#include "components/content_settings/core/browser/host_content_settings_map.h" | ||
#include "components/content_settings/core/common/content_settings_pattern.h" | ||
#include "components/prefs/testing_pref_service.h" | ||
#include "components/sync_preferences/testing_pref_service_syncable.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace speedreader { | ||
|
||
class SpeedreaderMetricsTest : public testing::Test { | ||
public: | ||
SpeedreaderMetricsTest() | ||
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {} | ||
|
||
void SetUp() override { | ||
SpeedreaderMetrics::RegisterPrefs(local_state_.registry()); | ||
HostContentSettingsMap::RegisterProfilePrefs(profile_prefs_.registry()); | ||
|
||
host_content_settings_map_ = | ||
new HostContentSettingsMap(&profile_prefs_, /*is_off_the_record*/ false, | ||
/*store_last_modified*/ false, | ||
/*restore_session*/ false, | ||
/*should_record_metrics*/ false); | ||
|
||
metrics_ = std::make_unique<SpeedreaderMetrics>( | ||
&local_state_, host_content_settings_map_.get(), false); | ||
} | ||
|
||
void TearDown() override { host_content_settings_map_->ShutdownOnUIThread(); } | ||
|
||
protected: | ||
base::test::TaskEnvironment task_environment_; | ||
TestingPrefServiceSimple local_state_; | ||
sync_preferences::TestingPrefServiceSyncable profile_prefs_; | ||
scoped_refptr<HostContentSettingsMap> host_content_settings_map_; | ||
std::unique_ptr<SpeedreaderMetrics> metrics_; | ||
base::HistogramTester histogram_tester_; | ||
}; | ||
|
||
TEST_F(SpeedreaderMetricsTest, RecordPageViews) { | ||
for (int i = 0; i < 5; i++) { | ||
metrics_->RecordPageView(); | ||
} | ||
|
||
histogram_tester_.ExpectUniqueSample(kSpeedreaderPageViewsHistogramName, 0, | ||
5); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
metrics_->RecordPageView(); | ||
} | ||
|
||
histogram_tester_.ExpectBucketCount(kSpeedreaderPageViewsHistogramName, 1, 5); | ||
histogram_tester_.ExpectTotalCount(kSpeedreaderPageViewsHistogramName, 10); | ||
|
||
task_environment_.FastForwardBy(base::Days(45)); | ||
|
||
histogram_tester_.ExpectBucketCount(kSpeedreaderPageViewsHistogramName, 1, | ||
34); | ||
histogram_tester_.ExpectTotalCount(kSpeedreaderPageViewsHistogramName, 39); | ||
} | ||
|
||
TEST_F(SpeedreaderMetricsTest, EnabledSitesMetric) { | ||
histogram_tester_.ExpectUniqueSample(kSpeedreaderEnabledSitesHistogramName, 0, | ||
1); | ||
|
||
ContentSettingsPattern pattern = | ||
ContentSettingsPattern::FromString("*://example.com/*"); | ||
host_content_settings_map_->SetContentSettingCustomScope( | ||
pattern, ContentSettingsPattern::Wildcard(), | ||
ContentSettingsType::BRAVE_SPEEDREADER, CONTENT_SETTING_ALLOW); | ||
|
||
metrics_->UpdateEnabledSitesMetric(false); | ||
histogram_tester_.ExpectBucketCount(kSpeedreaderEnabledSitesHistogramName, 1, | ||
1); | ||
|
||
pattern = ContentSettingsPattern::FromString("*://brave.com/*"); | ||
host_content_settings_map_->SetContentSettingCustomScope( | ||
pattern, ContentSettingsPattern::Wildcard(), | ||
ContentSettingsType::BRAVE_SPEEDREADER, CONTENT_SETTING_ALLOW); | ||
|
||
metrics_->UpdateEnabledSitesMetric(false); | ||
histogram_tester_.ExpectBucketCount(kSpeedreaderEnabledSitesHistogramName, 2, | ||
1); | ||
|
||
metrics_->UpdateEnabledSitesMetric(true); | ||
histogram_tester_.ExpectBucketCount(kSpeedreaderEnabledSitesHistogramName, 3, | ||
1); | ||
} | ||
|
||
} // namespace speedreader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.