-
Notifications
You must be signed in to change notification settings - Fork 920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add metric for NTP default page #27898
Open
DJAndries
wants to merge
1
commit into
master
Choose a base branch
from
ntp-default-p3a
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* 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/browser/misc_metrics/profile_new_tab_metrics.h" | ||
|
||
#include <optional> | ||
|
||
#include "base/containers/contains.h" | ||
#include "base/metrics/histogram_macros.h" | ||
#include "brave/browser/new_tab/new_tab_shows_options.h" | ||
#include "brave/components/constants/pref_names.h" | ||
#include "chrome/common/pref_names.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "url/gurl.h" | ||
|
||
namespace misc_metrics { | ||
|
||
ProfileNewTabMetrics::ProfileNewTabMetrics(PrefService* profile_prefs) | ||
: profile_prefs_(profile_prefs) { | ||
pref_change_registrar_.Init(profile_prefs_); | ||
|
||
auto callback = base::BindRepeating( | ||
&ProfileNewTabMetrics::ReportNewTabPageDefault, base::Unretained(this)); | ||
pref_change_registrar_.Add(kNewTabPageShowsOptions, callback); | ||
pref_change_registrar_.Add(prefs::kHomePage, callback); | ||
pref_change_registrar_.Add(prefs::kHomePageIsNewTabPage, callback); | ||
|
||
// Report the initial state | ||
ReportNewTabPageDefault(); | ||
} | ||
|
||
ProfileNewTabMetrics::~ProfileNewTabMetrics() = default; | ||
|
||
void ProfileNewTabMetrics::ReportNewTabPageDefault() { | ||
std::optional<NewTabPageDefaultType> type; | ||
brave::NewTabPageShowsOptions option = | ||
static_cast<brave::NewTabPageShowsOptions>( | ||
profile_prefs_->GetInteger(kNewTabPageShowsOptions)); | ||
|
||
if (option == brave::NewTabPageShowsOptions::kDashboard) { | ||
type = NewTabPageDefaultType::kDashboard; | ||
} else if (option == brave::NewTabPageShowsOptions::kHomepage) { | ||
if (profile_prefs_->GetBoolean(prefs::kHomePageIsNewTabPage)) { | ||
type = NewTabPageDefaultType::kDashboard; | ||
} else { | ||
GURL homepage_url(profile_prefs_->GetString(prefs::kHomePage)); | ||
|
||
if (homepage_url.host() == "search.brave.com") { | ||
type = NewTabPageDefaultType::kHomepageBraveSearch; | ||
} else if (base::Contains(homepage_url.host(), "google")) { | ||
type = NewTabPageDefaultType::kHomepageGoogle; | ||
} else if (base::Contains(homepage_url.host(), "duckduckgo")) { | ||
type = NewTabPageDefaultType::kHomepageDuckDuckGo; | ||
} else { | ||
type = NewTabPageDefaultType::kHomepageOther; | ||
} | ||
} | ||
} else if (option == brave::NewTabPageShowsOptions::kBlankpage) { | ||
type = NewTabPageDefaultType::kBlank; | ||
} | ||
|
||
if (type) { | ||
UMA_HISTOGRAM_ENUMERATION(kNewTabPageDefaultHistogramName, *type); | ||
} | ||
} | ||
|
||
} // namespace misc_metrics |
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,45 @@ | ||
/* 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_BROWSER_MISC_METRICS_PROFILE_NEW_TAB_METRICS_H_ | ||
#define BRAVE_BROWSER_MISC_METRICS_PROFILE_NEW_TAB_METRICS_H_ | ||
|
||
#include "components/prefs/pref_change_registrar.h" | ||
|
||
class PrefService; | ||
|
||
namespace misc_metrics { | ||
|
||
inline constexpr char kNewTabPageDefaultHistogramName[] = | ||
"Brave.NTP.DefaultPage"; | ||
|
||
enum class NewTabPageDefaultType { | ||
kDashboard = 0, | ||
kBlank = 1, | ||
kHomepageBraveSearch = 2, | ||
kHomepageGoogle = 3, | ||
kHomepageDuckDuckGo = 4, | ||
kHomepageOther = 5, | ||
kMaxValue = kHomepageOther | ||
}; | ||
|
||
class ProfileNewTabMetrics { | ||
public: | ||
explicit ProfileNewTabMetrics(PrefService* profile_prefs); | ||
~ProfileNewTabMetrics(); | ||
|
||
ProfileNewTabMetrics(const ProfileNewTabMetrics&) = delete; | ||
ProfileNewTabMetrics& operator=(const ProfileNewTabMetrics&) = delete; | ||
|
||
private: | ||
void ReportNewTabPageDefault(); | ||
|
||
raw_ptr<PrefService> profile_prefs_; | ||
PrefChangeRegistrar pref_change_registrar_; | ||
}; | ||
|
||
} // namespace misc_metrics | ||
|
||
#endif // BRAVE_BROWSER_MISC_METRICS_PROFILE_NEW_TAB_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,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/browser/misc_metrics/profile_new_tab_metrics.h" | ||
|
||
#include "base/test/metrics/histogram_tester.h" | ||
#include "brave/browser/new_tab/new_tab_shows_options.h" | ||
#include "brave/components/constants/pref_names.h" | ||
#include "chrome/common/pref_names.h" | ||
#include "components/prefs/pref_registry_simple.h" | ||
#include "components/prefs/testing_pref_service.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace misc_metrics { | ||
|
||
class ProfileNewTabMetricsTest : public testing::Test { | ||
public: | ||
ProfileNewTabMetricsTest() = default; | ||
|
||
void SetUp() override { | ||
pref_service_.registry()->RegisterIntegerPref( | ||
kNewTabPageShowsOptions, | ||
static_cast<int>(brave::NewTabPageShowsOptions::kDashboard)); | ||
pref_service_.registry()->RegisterStringPref(prefs::kHomePage, ""); | ||
pref_service_.registry()->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, | ||
false); | ||
} | ||
|
||
protected: | ||
TestingPrefServiceSimple pref_service_; | ||
base::HistogramTester histogram_tester_; | ||
}; | ||
|
||
TEST_F(ProfileNewTabMetricsTest, TestDashboardOption) { | ||
pref_service_.SetInteger( | ||
kNewTabPageShowsOptions, | ||
static_cast<int>(brave::NewTabPageShowsOptions::kDashboard)); | ||
|
||
ProfileNewTabMetrics metrics(&pref_service_); | ||
|
||
histogram_tester_.ExpectUniqueSample(kNewTabPageDefaultHistogramName, 0, 1); | ||
} | ||
|
||
TEST_F(ProfileNewTabMetricsTest, TestBlankOption) { | ||
pref_service_.SetInteger( | ||
kNewTabPageShowsOptions, | ||
static_cast<int>(brave::NewTabPageShowsOptions::kBlankpage)); | ||
|
||
ProfileNewTabMetrics metrics(&pref_service_); | ||
|
||
histogram_tester_.ExpectUniqueSample(kNewTabPageDefaultHistogramName, 1, 1); | ||
} | ||
|
||
TEST_F(ProfileNewTabMetricsTest, TestHomepageOptions) { | ||
pref_service_.SetInteger( | ||
kNewTabPageShowsOptions, | ||
static_cast<int>(brave::NewTabPageShowsOptions::kHomepage)); | ||
|
||
pref_service_.SetBoolean(prefs::kHomePageIsNewTabPage, true); | ||
|
||
ProfileNewTabMetrics metrics(&pref_service_); | ||
|
||
histogram_tester_.ExpectUniqueSample(kNewTabPageDefaultHistogramName, 0, 1); | ||
|
||
histogram_tester_.ExpectTotalCount(kNewTabPageDefaultHistogramName, 1); | ||
|
||
pref_service_.SetBoolean(prefs::kHomePageIsNewTabPage, false); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 5, 1); | ||
|
||
pref_service_.SetString(prefs::kHomePage, "https://search.brave.com"); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 2, 1); | ||
|
||
pref_service_.SetString(prefs::kHomePage, "https://www.google.com"); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 3, 1); | ||
|
||
pref_service_.SetString(prefs::kHomePage, "https://duckduckgo.com"); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 4, 1); | ||
|
||
pref_service_.SetString(prefs::kHomePage, "https://example.com"); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 5, 2); | ||
|
||
histogram_tester_.ExpectTotalCount(kNewTabPageDefaultHistogramName, 6); | ||
} | ||
|
||
} // namespace misc_metrics |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reported by reviewdog 🐶
[semgrep] base::Unretained is most of the time unrequited, and a weak reference is better suited for secure coding.
Consider swapping Unretained for a weak reference.
base::Unretained usage may be acceptable when a callback owner is guaranteed
to be destroyed with the object base::Unretained is pointing to, for example:
- PrefChangeRegistrar
- base::*Timer
- mojo::Receiver
- any other class member destroyed when the class is deallocated
Source: https://github.com/brave/security-action/blob/main/assets/semgrep_rules/client/chromium-uaf.yaml
Cc @thypon @goodov @iefremov