diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index 1ffb3db9510e..4c57171bfea2 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -7,10 +7,9 @@ #include -#include "brave/browser/new_tab/new_tab_shows_options.h" - #include "brave/browser/brave_shields/brave_shields_web_contents_observer.h" #include "brave/browser/ethereum_remote_client/buildflags/buildflags.h" +#include "brave/browser/new_tab/new_tab_shows_options.h" #include "brave/browser/search/ntp_utils.h" #include "brave/browser/themes/brave_dark_mode_utils.h" #include "brave/browser/translate/brave_translate_prefs_migration.h" @@ -23,6 +22,8 @@ #include "brave/components/brave_news/browser/brave_news_controller.h" #include "brave/components/brave_news/browser/brave_news_p3a.h" #include "brave/components/brave_news/browser/brave_news_pref_manager.h" +#include "brave/components/brave_news/common/p3a_pref_names.h" +#include "brave/components/brave_news/common/pref_names.h" #include "brave/components/brave_perf_predictor/browser/p3a_bandwidth_savings_tracker.h" #include "brave/components/brave_perf_predictor/browser/perf_predictor_tab_helper.h" #include "brave/components/brave_rewards/common/pref_names.h" @@ -188,7 +189,7 @@ void RegisterProfilePrefsForMigration( brave_rewards::RegisterProfilePrefsForMigration(registry); - brave_news::p3a::NewsMetrics::RegisterProfilePrefsForMigration(registry); + brave_news::p3a::prefs::RegisterProfileNewsMetricsPrefsForMigration(registry); // Added May 2023 #if defined(TOOLKIT_VIEWS) @@ -234,7 +235,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { brave_shields::RegisterShieldsP3AProfilePrefs(registry); - brave_news::BraveNewsPrefManager::RegisterProfilePrefs(registry); + brave_news::prefs::RegisterProfilePrefs(registry); // TODO(shong): Migrate this to local state also and guard in ENABLE_WIDEVINE. // We don't need to display "don't ask widevine prompt option" in settings diff --git a/chromium_src/chrome/browser/prefs/browser_prefs.cc b/chromium_src/chrome/browser/prefs/browser_prefs.cc index 9fcdd8ce7843..ae4a164dfbd8 100644 --- a/chromium_src/chrome/browser/prefs/browser_prefs.cc +++ b/chromium_src/chrome/browser/prefs/browser_prefs.cc @@ -14,6 +14,8 @@ #include "brave/components/ai_chat/core/common/buildflags/buildflags.h" #include "brave/components/brave_ads/core/public/prefs/obsolete_pref_util.h" #include "brave/components/brave_news/browser/brave_news_p3a.h" +#include "brave/components/brave_news/common/p3a_pref_names.h" +#include "brave/components/brave_news/common/pref_names.h" #include "brave/components/brave_search_conversion/p3a.h" #include "brave/components/brave_shields/content/browser/brave_shields_p3a.h" #include "brave/components/brave_sync/brave_sync_prefs.h" @@ -163,7 +165,7 @@ void MigrateObsoleteProfilePrefs(PrefService* profile_prefs, profile_prefs->ClearPref(sidebar::kSidebarAlignmentChangedTemporarily); #endif - brave_news::p3a::NewsMetrics::MigrateObsoleteProfilePrefs(profile_prefs); + brave_news::p3a::prefs::MigrateObsoleteProfileNewsMetricsPrefs(profile_prefs); // Added 2023-09 ntp_background_images::ViewCounterService::MigrateObsoleteProfilePrefs( diff --git a/chromium_src/ios/chrome/browser/shared/model/prefs/BUILD.gn b/chromium_src/ios/chrome/browser/shared/model/prefs/BUILD.gn index 49e76904d037..a356e8d8cb4d 100644 --- a/chromium_src/ios/chrome/browser/shared/model/prefs/BUILD.gn +++ b/chromium_src/ios/chrome/browser/shared/model/prefs/BUILD.gn @@ -8,6 +8,7 @@ import("//brave/components/ipfs/buildflags/buildflags.gni") group("prefs") { deps = [ "//brave/components/brave_ads/core", + "//brave/components/brave_news/common", "//brave/components/brave_rewards/common", "//brave/components/brave_shields/core/common", "//brave/components/brave_sync:prefs", diff --git a/chromium_src/ios/chrome/browser/shared/model/prefs/DEPS b/chromium_src/ios/chrome/browser/shared/model/prefs/DEPS index 5fc56216bcd6..37a2534bd9d0 100644 --- a/chromium_src/ios/chrome/browser/shared/model/prefs/DEPS +++ b/chromium_src/ios/chrome/browser/shared/model/prefs/DEPS @@ -1,5 +1,6 @@ include_rules = [ "+brave/components/brave_ads/core/public", + "+brave/components/brave_news/common", "+brave/components/brave_rewards/common", "+brave/components/brave_sync", "+brave/components/brave_wallet/browser", diff --git a/chromium_src/ios/chrome/browser/shared/model/prefs/browser_prefs.mm b/chromium_src/ios/chrome/browser/shared/model/prefs/browser_prefs.mm index bee757066264..63ec56cfd234 100644 --- a/chromium_src/ios/chrome/browser/shared/model/prefs/browser_prefs.mm +++ b/chromium_src/ios/chrome/browser/shared/model/prefs/browser_prefs.mm @@ -6,6 +6,7 @@ #include "brave/components/ai_chat/core/browser/ai_chat_metrics.h" #include "brave/components/ai_chat/core/common/pref_names.h" #include "brave/components/brave_ads/core/public/prefs/pref_registry.h" +#include "brave/components/brave_news/common/pref_names.h" #include "brave/components/brave_rewards/common/pref_registry.h" #include "brave/components/brave_shields/core/common/pref_names.h" #include "brave/components/brave_sync/brave_sync_prefs.h" @@ -42,6 +43,7 @@ void BraveRegisterBrowserStatePrefs( ipfs::IpfsService::RegisterProfilePrefs(registry); #endif ai_chat::prefs::RegisterProfilePrefs(registry); + brave_news::prefs::RegisterProfilePrefs(registry); } void BraveRegisterLocalStatePrefs(PrefRegistrySimple* registry) { diff --git a/components/brave_news/browser/BUILD.gn b/components/brave_news/browser/BUILD.gn index 23fc955ac3b4..220eeb80fb71 100644 --- a/components/brave_news/browser/BUILD.gn +++ b/components/brave_news/browser/BUILD.gn @@ -40,8 +40,6 @@ static_library("browser") { "html_parsing.h", "initialization_promise.cc", "initialization_promise.h", - "locales_helper.cc", - "locales_helper.h", "network.cc", "network.h", "publishers_controller.cc", diff --git a/components/brave_news/browser/brave_news_controller.cc b/components/brave_news/browser/brave_news_controller.cc index 56c53b04a144..cbececfcc20e 100644 --- a/components/brave_news/browser/brave_news_controller.cc +++ b/components/brave_news/browser/brave_news_controller.cc @@ -33,7 +33,6 @@ #include "brave/components/brave_news/browser/channels_controller.h" #include "brave/components/brave_news/browser/direct_feed_controller.h" #include "brave/components/brave_news/browser/feed_v2_builder.h" -#include "brave/components/brave_news/browser/locales_helper.h" #include "brave/components/brave_news/browser/network.h" #include "brave/components/brave_news/browser/publishers_controller.h" #include "brave/components/brave_news/browser/publishers_parsing.h" @@ -41,6 +40,7 @@ #include "brave/components/brave_news/browser/topics_fetcher.h" #include "brave/components/brave_news/common/brave_news.mojom.h" #include "brave/components/brave_news/common/features.h" +#include "brave/components/brave_news/common/locales_helper.h" #include "brave/components/brave_private_cdn/private_cdn_helper.h" #include "brave/components/brave_private_cdn/private_cdn_request_helper.h" #include "components/favicon/core/favicon_service.h" diff --git a/components/brave_news/browser/brave_news_p3a.cc b/components/brave_news/browser/brave_news_p3a.cc index b023af28b698..2c56e8fee53c 100644 --- a/components/brave_news/browser/brave_news_p3a.cc +++ b/components/brave_news/browser/brave_news_p3a.cc @@ -12,12 +12,11 @@ #include "base/logging.h" #include "base/metrics/histogram_macros.h" #include "brave/components/brave_news/browser/brave_news_pref_manager.h" -#include "brave/components/brave_news/common/pref_names.h" +#include "brave/components/brave_news/common/p3a_pref_names.h" #include "brave/components/brave_rewards/common/pref_names.h" #include "brave/components/p3a_utils/bucket.h" #include "brave/components/p3a_utils/feature_usage.h" #include "brave/components/time_period_storage/weekly_storage.h" -#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_service.h" namespace brave_news::p3a { @@ -308,30 +307,6 @@ void NewsMetrics::RecordWeeklySessionCount(bool is_add) { total_session_count); } -void NewsMetrics::RegisterProfilePrefs(PrefRegistrySimple* registry) { - registry->RegisterListPref(prefs::kBraveNewsWeeklySessionCount); - registry->RegisterListPref(prefs::kBraveNewsWeeklyDisplayAdViewedCount); - registry->RegisterListPref(prefs::kBraveNewsWeeklyAddedDirectFeedsCount); - registry->RegisterListPref(prefs::kBraveNewsTotalCardViews); - registry->RegisterListPref(prefs::kBraveNewsTotalCardVisits); - registry->RegisterListPref(prefs::kBraveNewsVisitDepthSum); - registry->RegisterListPref(prefs::kBraveNewsTotalSidebarFilterUsages); - p3a_utils::RegisterFeatureUsagePrefs( - registry, prefs::kBraveNewsFirstSessionTime, - prefs::kBraveNewsLastSessionTime, prefs::kBraveNewsUsedSecondDay, nullptr, - nullptr); - registry->RegisterBooleanPref(prefs::kBraveNewsWasEverEnabled, false); -} - -void NewsMetrics::RegisterProfilePrefsForMigration( - PrefRegistrySimple* registry) { - // Reserved for future deprecated P3A-related prefs -} - -void NewsMetrics::MigrateObsoleteProfilePrefs(PrefService* prefs) { - // Reserved for future deprecated P3A-related prefs -} - void NewsMetrics::OnConfigChanged() { DVLOG(1) << __FUNCTION__; if (was_enabled_ == pref_manager_->IsEnabled()) { diff --git a/components/brave_news/browser/brave_news_p3a.h b/components/brave_news/browser/brave_news_p3a.h index 367f388760d1..9430f98c620f 100644 --- a/components/brave_news/browser/brave_news_p3a.h +++ b/components/brave_news/browser/brave_news_p3a.h @@ -12,7 +12,6 @@ #include "base/timer/wall_clock_timer.h" #include "brave/components/brave_news/browser/brave_news_pref_manager.h" -class PrefRegistrySimple; class PrefService; namespace brave_news::p3a { @@ -59,10 +58,6 @@ class NewsMetrics : public BraveNewsPrefManager::PrefObserver { NewsMetrics(const NewsMetrics&) = delete; NewsMetrics& operator=(const NewsMetrics&) = delete; - static void RegisterProfilePrefs(PrefRegistrySimple* registry); - static void RegisterProfilePrefsForMigration(PrefRegistrySimple* registry); - static void MigrateObsoleteProfilePrefs(PrefService* prefs); - void RecordAtInit(); void RecordAtSessionStart(); diff --git a/components/brave_news/browser/brave_news_p3a_unittest.cc b/components/brave_news/browser/brave_news_p3a_unittest.cc index 1a694ebbe469..2835cea448a8 100644 --- a/components/brave_news/browser/brave_news_p3a_unittest.cc +++ b/components/brave_news/browser/brave_news_p3a_unittest.cc @@ -10,9 +10,11 @@ #include "base/test/metrics/histogram_tester.h" #include "brave/components/brave_news/browser/brave_news_controller.h" #include "brave/components/brave_news/browser/brave_news_pref_manager.h" +#include "brave/components/brave_news/common/p3a_pref_names.h" #include "brave/components/brave_news/common/pref_names.h" #include "brave/components/brave_rewards/common/pref_names.h" #include "brave/components/time_period_storage/weekly_storage.h" +#include "components/prefs/pref_registry_simple.h" #include "components/prefs/scoped_user_pref_update.h" #include "components/prefs/testing_pref_service.h" #include "content/public/test/browser_task_environment.h" @@ -29,7 +31,7 @@ class BraveNewsP3ATest : public testing::Test { void SetUp() override { task_environment_.AdvanceClock(base::Days(2)); PrefRegistrySimple* registry = pref_service_.registry(); - BraveNewsPrefManager::RegisterProfilePrefs(registry); + ::brave_news::prefs::RegisterProfilePrefs(registry); registry->RegisterBooleanPref(brave_rewards::prefs::kEnabled, false); pref_manager_ = std::make_unique(pref_service_); @@ -343,14 +345,14 @@ TEST_F(BraveNewsP3ATest, TestIsEnabled) { PrefService* prefs = GetPrefs(); // should not record "disabled" if never opted in - prefs->SetBoolean(prefs::kNewTabPageShowToday, false); + prefs->SetBoolean(::brave_news::prefs::kNewTabPageShowToday, false); histogram_tester_.ExpectTotalCount(kIsEnabledHistogramName, 0); - prefs->SetBoolean(prefs::kBraveNewsOptedIn, true); - prefs->SetBoolean(prefs::kNewTabPageShowToday, true); + prefs->SetBoolean(::brave_news::prefs::kBraveNewsOptedIn, true); + prefs->SetBoolean(::brave_news::prefs::kNewTabPageShowToday, true); histogram_tester_.ExpectUniqueSample(kIsEnabledHistogramName, 1, 1); - prefs->SetBoolean(prefs::kNewTabPageShowToday, false); + prefs->SetBoolean(::brave_news::prefs::kNewTabPageShowToday, false); histogram_tester_.ExpectBucketCount(kIsEnabledHistogramName, 0, 1); } diff --git a/components/brave_news/browser/brave_news_pref_manager.cc b/components/brave_news/browser/brave_news_pref_manager.cc index ac9a630203ff..7435aaed22a3 100644 --- a/components/brave_news/browser/brave_news_pref_manager.cc +++ b/components/brave_news/browser/brave_news_pref_manager.cc @@ -19,12 +19,10 @@ #include "base/uuid.h" #include "brave/components/brave_news/browser/brave_news_p3a.h" #include "brave/components/brave_news/browser/channel_migrator.h" -#include "brave/components/brave_news/browser/locales_helper.h" #include "brave/components/brave_news/common/brave_news.mojom.h" #include "brave/components/brave_news/common/pref_names.h" #include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_observer.h" -#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_service.h" #include "components/prefs/scoped_user_pref_update.h" @@ -154,20 +152,6 @@ SubscriptionsDiff BraveNewsSubscriptions::DiffChannels( return result; } -// static -void BraveNewsPrefManager::RegisterProfilePrefs(PrefRegistrySimple* registry) { - registry->RegisterBooleanPref(prefs::kShouldShowToolbarButton, true); - registry->RegisterBooleanPref(prefs::kNewTabPageShowToday, - IsUserInDefaultEnabledLocale()); - registry->RegisterBooleanPref(prefs::kBraveNewsOptedIn, false); - registry->RegisterDictionaryPref(prefs::kBraveNewsSources); - registry->RegisterDictionaryPref(prefs::kBraveNewsChannels); - registry->RegisterDictionaryPref(prefs::kBraveNewsDirectFeeds); - registry->RegisterBooleanPref(prefs::kBraveNewsOpenArticlesInNewTab, true); - - p3a::NewsMetrics::RegisterProfilePrefs(registry); -} - BraveNewsPrefManager::BraveNewsPrefManager(PrefService& prefs) : prefs_(prefs) { pref_change_registrar_.Init(&*prefs_); pref_change_registrar_.Add( diff --git a/components/brave_news/browser/brave_news_pref_manager.h b/components/brave_news/browser/brave_news_pref_manager.h index 3bdd14fcd2b0..289927a7140f 100644 --- a/components/brave_news/browser/brave_news_pref_manager.h +++ b/components/brave_news/browser/brave_news_pref_manager.h @@ -16,7 +16,6 @@ #include "brave/components/brave_news/common/brave_news.mojom-forward.h" #include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_observer.h" -#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_service.h" #include "url/gurl.h" @@ -91,8 +90,6 @@ class BraveNewsPrefManager { virtual void OnChannelsChanged() {} }; - static void RegisterProfilePrefs(PrefRegistrySimple* registry); - explicit BraveNewsPrefManager(PrefService& prefs); ~BraveNewsPrefManager(); diff --git a/components/brave_news/browser/feed_fetcher.cc b/components/brave_news/browser/feed_fetcher.cc index c87c157b7cd1..ca383ded5969 100644 --- a/components/brave_news/browser/feed_fetcher.cc +++ b/components/brave_news/browser/feed_fetcher.cc @@ -26,12 +26,12 @@ #include "brave/components/brave_news/browser/combined_feed_parsing.h" #include "brave/components/brave_news/browser/direct_feed_fetcher.h" #include "brave/components/brave_news/browser/feed_controller.h" -#include "brave/components/brave_news/browser/locales_helper.h" #include "brave/components/brave_news/browser/network.h" #include "brave/components/brave_news/browser/publishers_controller.h" #include "brave/components/brave_news/browser/urls.h" #include "brave/components/brave_news/common/brave_news.mojom-forward.h" #include "brave/components/brave_news/common/brave_news.mojom-shared.h" +#include "brave/components/brave_news/common/locales_helper.h" #include "brave/components/brave_private_cdn/headers.h" #include "services/network/public/cpp/shared_url_loader_factory.h" diff --git a/components/brave_news/browser/publishers_controller.cc b/components/brave_news/browser/publishers_controller.cc index 6ab682e83c17..be5925adcf91 100644 --- a/components/brave_news/browser/publishers_controller.cc +++ b/components/brave_news/browser/publishers_controller.cc @@ -21,7 +21,6 @@ #include "base/strings/strcat.h" #include "brave/components/api_request_helper/api_request_helper.h" #include "brave/components/brave_news/browser/brave_news_pref_manager.h" -#include "brave/components/brave_news/browser/locales_helper.h" #include "brave/components/brave_news/browser/network.h" #include "brave/components/brave_news/browser/publishers_parsing.h" #include "brave/components/brave_news/browser/urls.h" diff --git a/components/brave_news/browser/publishers_controller.h b/components/brave_news/browser/publishers_controller.h index 455cbc06696f..ebdfbea0c996 100644 --- a/components/brave_news/browser/publishers_controller.h +++ b/components/brave_news/browser/publishers_controller.h @@ -14,11 +14,11 @@ #include "brave/components/api_request_helper/api_request_helper.h" #include "brave/components/brave_news/browser/brave_news_pref_manager.h" #include "brave/components/brave_news/common/brave_news.mojom.h" +#include "brave/components/brave_news/common/locales_helper.h" namespace brave_news { using GetPublishersCallback = mojom::BraveNewsController::GetPublishersCallback; -using Publishers = base::flat_map; class PublishersController { public: diff --git a/components/brave_news/browser/publishers_parsing.h b/components/brave_news/browser/publishers_parsing.h index 142242b93c5f..683c485ff833 100644 --- a/components/brave_news/browser/publishers_parsing.h +++ b/components/brave_news/browser/publishers_parsing.h @@ -11,8 +11,8 @@ #include "base/values.h" #include "brave/components/brave_news/browser/brave_news_pref_manager.h" -#include "brave/components/brave_news/browser/publishers_controller.h" #include "brave/components/brave_news/common/brave_news.mojom-forward.h" +#include "brave/components/brave_news/common/locales_helper.h" namespace brave_news { diff --git a/components/brave_news/browser/publishers_parsing_unittest.cc b/components/brave_news/browser/publishers_parsing_unittest.cc index bacf0dbd83bc..efb4fae55276 100644 --- a/components/brave_news/browser/publishers_parsing_unittest.cc +++ b/components/brave_news/browser/publishers_parsing_unittest.cc @@ -5,14 +5,12 @@ #include "brave/components/brave_news/browser/publishers_parsing.h" -#include #include -#include -#include +#include #include "base/containers/flat_map.h" #include "base/test/values_test_util.h" -#include "brave/components/brave_news/common/brave_news.mojom-forward.h" +#include "brave/components/brave_news/common/brave_news.mojom.h" #include "testing/gtest/include/gtest/gtest.h" namespace brave_news { diff --git a/components/brave_news/browser/test/BUILD.gn b/components/brave_news/browser/test/BUILD.gn index a9d5148ae4fa..e717468dc919 100644 --- a/components/brave_news/browser/test/BUILD.gn +++ b/components/brave_news/browser/test/BUILD.gn @@ -20,7 +20,6 @@ source_set("brave_news_unit_tests") { "//brave/components/brave_news/browser/feed_sampling_unittest.cc", "//brave/components/brave_news/browser/html_parsing_unittest.cc", "//brave/components/brave_news/browser/initialization_promise_unittest.cc", - "//brave/components/brave_news/browser/locales_helper_unittest.cc", "//brave/components/brave_news/browser/publishers_controller_unittest.cc", "//brave/components/brave_news/browser/publishers_parsing_unittest.cc", "//brave/components/brave_news/browser/suggestions_controller_unittest.cc", diff --git a/components/brave_news/common/BUILD.gn b/components/brave_news/common/BUILD.gn index fd449aeceec3..023cebabde24 100644 --- a/components/brave_news/common/BUILD.gn +++ b/components/brave_news/common/BUILD.gn @@ -6,15 +6,39 @@ import("//brave/build/config.gni") import("//mojo/public/tools/bindings/mojom.gni") +source_set("unit_tests") { + testonly = true + + sources = [ "locales_helper_unittest.cc" ] + + deps = [ + ":common", + "//base", + "//testing/gtest", + ] +} + static_library("common") { sources = [ "features.cc", "features.h", + "locales_helper.cc", + "locales_helper.h", + "p3a_pref_names.cc", + "p3a_pref_names.h", + "pref_names.cc", "pref_names.h", "switches.h", ] - deps = [ "//base" ] + public_deps = [ ":mojom" ] + + deps = [ + "//base", + "//brave/components/l10n/common", + "//brave/components/p3a_utils", + "//components/prefs", + ] } mojom("mojom") { diff --git a/components/brave_news/browser/locales_helper.cc b/components/brave_news/common/locales_helper.cc similarity index 92% rename from components/brave_news/browser/locales_helper.cc rename to components/brave_news/common/locales_helper.cc index d95a7d438c94..ef363c335f0e 100644 --- a/components/brave_news/browser/locales_helper.cc +++ b/components/brave_news/common/locales_helper.cc @@ -3,7 +3,7 @@ // 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/brave_news/browser/locales_helper.h" +#include "brave/components/brave_news/common/locales_helper.h" #include #include @@ -14,16 +14,10 @@ #include "base/containers/fixed_flat_set.h" #include "base/containers/flat_map.h" #include "base/containers/flat_set.h" -#include "base/feature_list.h" #include "base/ranges/algorithm.h" #include "base/strings/strcat.h" #include "base/strings/string_util.h" -#include "brave/components/brave_news/browser/publishers_controller.h" -#include "brave/components/brave_news/browser/urls.h" -#include "brave/components/brave_news/common/brave_news.mojom-forward.h" -#include "brave/components/brave_news/common/brave_news.mojom-shared.h" #include "brave/components/brave_news/common/brave_news.mojom.h" -#include "brave/components/brave_news/common/features.h" #include "brave/components/l10n/common/locale_util.h" namespace brave_news { diff --git a/components/brave_news/browser/locales_helper.h b/components/brave_news/common/locales_helper.h similarity index 77% rename from components/brave_news/browser/locales_helper.h rename to components/brave_news/common/locales_helper.h index 80eb87206449..dee4e668dcc7 100644 --- a/components/brave_news/browser/locales_helper.h +++ b/components/brave_news/common/locales_helper.h @@ -3,17 +3,19 @@ // 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_BRAVE_NEWS_BROWSER_LOCALES_HELPER_H_ -#define BRAVE_COMPONENTS_BRAVE_NEWS_BROWSER_LOCALES_HELPER_H_ +#ifndef BRAVE_COMPONENTS_BRAVE_NEWS_COMMON_LOCALES_HELPER_H_ +#define BRAVE_COMPONENTS_BRAVE_NEWS_COMMON_LOCALES_HELPER_H_ #include -#include +#include "base/containers/flat_map.h" #include "base/containers/flat_set.h" -#include "brave/components/brave_news/browser/publishers_controller.h" +#include "brave/components/brave_news/common/brave_news.mojom-forward.h" namespace brave_news { +using Publishers = base::flat_map; + // Gets a list of all available locales from the publishers. base::flat_set GetPublisherLocales(const Publishers& publishers); @@ -34,4 +36,4 @@ base::flat_set GetMinimalLocalesSet( bool IsUserInDefaultEnabledLocale(); } // namespace brave_news -#endif // BRAVE_COMPONENTS_BRAVE_NEWS_BROWSER_LOCALES_HELPER_H_ +#endif // BRAVE_COMPONENTS_BRAVE_NEWS_COMMON_LOCALES_HELPER_H_ diff --git a/components/brave_news/browser/locales_helper_unittest.cc b/components/brave_news/common/locales_helper_unittest.cc similarity index 96% rename from components/brave_news/browser/locales_helper_unittest.cc rename to components/brave_news/common/locales_helper_unittest.cc index edb3a19437af..f435750ff951 100644 --- a/components/brave_news/browser/locales_helper_unittest.cc +++ b/components/brave_news/common/locales_helper_unittest.cc @@ -3,7 +3,7 @@ // 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/brave_news/browser/locales_helper.h" +#include "brave/components/brave_news/common/locales_helper.h" #include #include @@ -12,10 +12,6 @@ #include "base/containers/contains.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" -#include "base/test/scoped_feature_list.h" -#include "brave/components/brave_news/browser/publishers_controller.h" -#include "brave/components/brave_news/browser/publishers_parsing.h" -#include "brave/components/brave_news/browser/urls.h" #include "brave/components/brave_news/common/brave_news.mojom.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/components/brave_news/common/p3a_pref_names.cc b/components/brave_news/common/p3a_pref_names.cc new file mode 100644 index 000000000000..2f33c46c4fd2 --- /dev/null +++ b/components/brave_news/common/p3a_pref_names.cc @@ -0,0 +1,37 @@ +// Copyright (c) 2024 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/brave_news/common/p3a_pref_names.h" + +#include "brave/components/p3a_utils/feature_usage.h" +#include "components/prefs/pref_registry_simple.h" +#include "components/prefs/pref_service.h" + +namespace brave_news::p3a::prefs { + +void RegisterProfileNewsMetricsPrefs(PrefRegistrySimple* registry) { + registry->RegisterListPref(kBraveNewsWeeklySessionCount); + registry->RegisterListPref(kBraveNewsWeeklyDisplayAdViewedCount); + registry->RegisterListPref(kBraveNewsWeeklyAddedDirectFeedsCount); + registry->RegisterListPref(kBraveNewsTotalCardViews); + registry->RegisterListPref(kBraveNewsTotalCardVisits); + registry->RegisterListPref(kBraveNewsVisitDepthSum); + registry->RegisterListPref(kBraveNewsTotalSidebarFilterUsages); + p3a_utils::RegisterFeatureUsagePrefs( + registry, prefs::kBraveNewsFirstSessionTime, + prefs::kBraveNewsLastSessionTime, prefs::kBraveNewsUsedSecondDay, nullptr, + nullptr); + registry->RegisterBooleanPref(kBraveNewsWasEverEnabled, false); +} + +void RegisterProfileNewsMetricsPrefsForMigration(PrefRegistrySimple* registry) { + // Reserved for future deprecated P3A-related prefs +} + +void MigrateObsoleteProfileNewsMetricsPrefs(PrefService* prefs) { + // Reserved for future deprecated P3A-related prefs +} + +} // namespace brave_news::p3a::prefs diff --git a/components/brave_news/common/p3a_pref_names.h b/components/brave_news/common/p3a_pref_names.h new file mode 100644 index 000000000000..ca04277abe2b --- /dev/null +++ b/components/brave_news/common/p3a_pref_names.h @@ -0,0 +1,43 @@ +// Copyright (c) 2024 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_BRAVE_NEWS_COMMON_P3A_PREF_NAMES_H_ +#define BRAVE_COMPONENTS_BRAVE_NEWS_COMMON_P3A_PREF_NAMES_H_ + +class PrefService; +class PrefRegistrySimple; + +namespace brave_news::p3a::prefs { + +inline constexpr char kBraveNewsWeeklySessionCount[] = + "brave.today.p3a_weekly_session_count"; +inline constexpr char kBraveNewsWeeklyDisplayAdViewedCount[] = + "brave.today.p3a_weekly_display_ad_viewed_count"; +inline constexpr char kBraveNewsWeeklyAddedDirectFeedsCount[] = + "brave.today.p3a_weekly_added_direct_feeds_count"; +inline constexpr char kBraveNewsTotalCardViews[] = + "brave.today.p3a_total_card_views"; +inline constexpr char kBraveNewsTotalCardVisits[] = + "brave.today.p3a_total_card_visits"; +inline constexpr char kBraveNewsVisitDepthSum[] = + "brave.today.p3a_card_visit_depth_sum"; +inline constexpr char kBraveNewsTotalSidebarFilterUsages[] = + "brave.today.p3a_total_sidebar_filter_usages"; +inline constexpr char kBraveNewsFirstSessionTime[] = + "brave.today.p3a_first_session_time"; +inline constexpr char kBraveNewsUsedSecondDay[] = + "brave.today.p3a_used_second_day"; +inline constexpr char kBraveNewsLastSessionTime[] = + "brave.today.p3a_last_session_time"; +inline constexpr char kBraveNewsWasEverEnabled[] = + "brave.today.p3a_was_ever_enabled"; + +void RegisterProfileNewsMetricsPrefs(PrefRegistrySimple* registry); +void RegisterProfileNewsMetricsPrefsForMigration(PrefRegistrySimple* registry); +void MigrateObsoleteProfileNewsMetricsPrefs(PrefService* prefs); + +} // namespace brave_news::p3a::prefs + +#endif // BRAVE_COMPONENTS_BRAVE_NEWS_COMMON_P3A_PREF_NAMES_H_ diff --git a/components/brave_news/common/pref_names.cc b/components/brave_news/common/pref_names.cc new file mode 100644 index 000000000000..e0ced69b0308 --- /dev/null +++ b/components/brave_news/common/pref_names.cc @@ -0,0 +1,37 @@ +// Copyright (c) 2024 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/brave_news/common/pref_names.h" + +#include "brave/components/brave_news/common/locales_helper.h" +#include "brave/components/brave_news/common/p3a_pref_names.h" +#include "brave/components/p3a_utils/feature_usage.h" +#include "components/prefs/pref_registry_simple.h" +#include "components/prefs/pref_service.h" + +namespace brave_news { +namespace prefs { + +void RegisterProfilePrefs(PrefRegistrySimple* registry) { + registry->RegisterBooleanPref(kShouldShowToolbarButton, true); + registry->RegisterBooleanPref(kNewTabPageShowToday, + IsUserInDefaultEnabledLocale()); + registry->RegisterBooleanPref(kBraveNewsOptedIn, false); + registry->RegisterDictionaryPref(kBraveNewsSources); + registry->RegisterDictionaryPref(kBraveNewsChannels); + registry->RegisterDictionaryPref(kBraveNewsDirectFeeds); + registry->RegisterBooleanPref(kBraveNewsOpenArticlesInNewTab, true); + + brave_news::p3a::prefs::RegisterProfileNewsMetricsPrefs(registry); +} + +} // namespace prefs + +bool IsEnabled(PrefService* prefs) { + return prefs->GetBoolean(prefs::kNewTabPageShowToday) && + prefs->GetBoolean(prefs::kBraveNewsOptedIn); +} + +} // namespace brave_news diff --git a/components/brave_news/common/pref_names.h b/components/brave_news/common/pref_names.h index 5dd9d2179f09..a84af3eef261 100644 --- a/components/brave_news/common/pref_names.h +++ b/components/brave_news/common/pref_names.h @@ -6,6 +6,8 @@ #ifndef BRAVE_COMPONENTS_BRAVE_NEWS_COMMON_PREF_NAMES_H_ #define BRAVE_COMPONENTS_BRAVE_NEWS_COMMON_PREF_NAMES_H_ +class PrefRegistrySimple; + namespace brave_news { namespace prefs { @@ -19,28 +21,6 @@ inline constexpr char kBraveNewsIntroDismissed[] = inline constexpr char kBraveNewsOptedIn[] = "brave.today.opted_in"; inline constexpr char kShouldShowToolbarButton[] = "brave.today.should_show_toolbar_button"; -inline constexpr char kBraveNewsWeeklySessionCount[] = - "brave.today.p3a_weekly_session_count"; -inline constexpr char kBraveNewsWeeklyDisplayAdViewedCount[] = - "brave.today.p3a_weekly_display_ad_viewed_count"; -inline constexpr char kBraveNewsWeeklyAddedDirectFeedsCount[] = - "brave.today.p3a_weekly_added_direct_feeds_count"; -inline constexpr char kBraveNewsTotalCardViews[] = - "brave.today.p3a_total_card_views"; -inline constexpr char kBraveNewsTotalCardVisits[] = - "brave.today.p3a_total_card_visits"; -inline constexpr char kBraveNewsVisitDepthSum[] = - "brave.today.p3a_card_visit_depth_sum"; -inline constexpr char kBraveNewsTotalSidebarFilterUsages[] = - "brave.today.p3a_total_sidebar_filter_usages"; -inline constexpr char kBraveNewsFirstSessionTime[] = - "brave.today.p3a_first_session_time"; -inline constexpr char kBraveNewsUsedSecondDay[] = - "brave.today.p3a_used_second_day"; -inline constexpr char kBraveNewsLastSessionTime[] = - "brave.today.p3a_last_session_time"; -inline constexpr char kBraveNewsWasEverEnabled[] = - "brave.today.p3a_was_ever_enabled"; inline constexpr char kBraveNewsOpenArticlesInNewTab[] = "brave.news.open-articles-in-new-tab"; @@ -48,6 +28,8 @@ inline constexpr char kBraveNewsOpenArticlesInNewTab[] = inline constexpr char kBraveNewsDirectFeedsKeyTitle[] = "title"; inline constexpr char kBraveNewsDirectFeedsKeySource[] = "source"; +void RegisterProfilePrefs(PrefRegistrySimple* registry); + } // namespace prefs } // namespace brave_news diff --git a/ios/brave-ios/Sources/Brave/Frontend/Rewards/BraveRewards.swift b/ios/brave-ios/Sources/Brave/Frontend/Rewards/BraveRewards.swift index 4b9530905d5c..402221231884 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Rewards/BraveRewards.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Rewards/BraveRewards.swift @@ -11,7 +11,7 @@ import Growth import Preferences import Shared -public class BraveRewards: NSObject { +public class BraveRewards: PreferencesObserver { /// Whether or not Brave Rewards is available/can be enabled public static var isAvailable: Bool { @@ -33,8 +33,6 @@ public class BraveRewards: NSObject { ads = BraveAds(stateStoragePath: configuration.storageURL.appendingPathComponent("ads").path) - super.init() - braveNewsObservation = Preferences.BraveNews.isEnabled.$value .receive(on: DispatchQueue.main) .sink { [weak self] value in @@ -46,6 +44,13 @@ public class BraveRewards: NSObject { if Preferences.Rewards.adsEnabledTimestamp.value == nil, ads.isEnabled { Preferences.Rewards.adsEnabledTimestamp.value = Date() } + + ads.notifyBraveNewsIsEnabledPreferenceDidChange(Preferences.BraveNews.isEnabled.value) + Preferences.BraveNews.isEnabled.observe(from: self) + } + + public func preferencesDidChange(for key: String) { + ads.notifyBraveNewsIsEnabledPreferenceDidChange(Preferences.BraveNews.isEnabled.value) } func startRewardsService(_ completion: (() -> Void)?) { diff --git a/ios/browser/api/ads/brave_ads.h b/ios/browser/api/ads/brave_ads.h index 635c09dafc9e..6af5725cbb1d 100644 --- a/ios/browser/api/ads/brave_ads.h +++ b/ios/browser/api/ads/brave_ads.h @@ -66,6 +66,10 @@ OBJC_EXPORT /// Returns `true` if the user opted-in to search result ads. - (BOOL)isOptedInToSearchResultAds; +/// Used to notify the ads service that the user has opted-in/opted-out to +/// Brave News. +- (void)notifyBraveNewsIsEnabledPreferenceDidChange:(BOOL)isEnabled; + /// Whether or not Brave Ads is enabled and the user should receive /// notification-style ads and be rewarded for it @property(nonatomic, assign, getter=isEnabled) diff --git a/ios/browser/api/ads/brave_ads.mm b/ios/browser/api/ads/brave_ads.mm index 2bcdd90cd397..843d94831cdc 100644 --- a/ios/browser/api/ads/brave_ads.mm +++ b/ios/browser/api/ads/brave_ads.mm @@ -217,6 +217,13 @@ - (BOOL)isOptedInToSearchResultAds { brave_ads::prefs::kOptedInToSearchResultAds); } +- (void)notifyBraveNewsIsEnabledPreferenceDidChange:(BOOL)isEnabled { + [self setProfilePref:brave_news::prefs::kBraveNewsOptedIn + value:base::Value(isEnabled)]; + [self setProfilePref:brave_news::prefs::kNewTabPageShowToday + value:base::Value(isEnabled)]; +} + - (BOOL)isEnabled { return self.profilePrefService->GetBoolean(brave_rewards::prefs::kEnabled); } @@ -1535,9 +1542,7 @@ - (void)recordP2AEvents:(const std::vector&)events { } - (std::optional)getProfilePref:(const std::string&)path { - if (path == brave_news::prefs::kBraveNewsOptedIn || - path == brave_news::prefs::kNewTabPageShowToday || - path == ntp_background_images::prefs::kNewTabPageShowBackgroundImage || + if (path == ntp_background_images::prefs::kNewTabPageShowBackgroundImage || path == ntp_background_images::prefs:: kNewTabPageShowSponsoredImagesBackgroundImage) { // TODO(https://github.com/brave/brave-browser/issues/33745): Decouple Brave