/chrome/browser/feature_guide/notifications/feature_notification_guide_service_factory.cc

https://github.com/chromium/chromium · C++ · 130 lines · 107 code · 17 blank · 6 comment · 2 complexity · 806067a1dd9c1592a50222c37276142b MD5 · raw file

  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chrome/browser/feature_guide/notifications/feature_notification_guide_service_factory.h"
  5. #include "base/feature_list.h"
  6. #include "base/memory/singleton.h"
  7. #include "base/metrics/field_trial_params.h"
  8. #include "base/time/default_clock.h"
  9. #include "build/build_config.h"
  10. #include "chrome/browser/feature_engagement/tracker_factory.h"
  11. #include "chrome/browser/feature_guide/notifications/config.h"
  12. #include "chrome/browser/feature_guide/notifications/feature_notification_guide_service.h"
  13. #include "chrome/browser/feature_guide/notifications/feature_type.h"
  14. #include "chrome/browser/feature_guide/notifications/internal/feature_notification_guide_service_impl.h"
  15. #include "chrome/browser/notifications/scheduler/notification_schedule_service_factory.h"
  16. #include "chrome/browser/profiles/profile.h"
  17. #include "chrome/browser/profiles/profile_key.h"
  18. #include "chrome/browser/segmentation_platform/segmentation_platform_service_factory.h"
  19. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  20. #include "components/segmentation_platform/public/segmentation_platform_service.h"
  21. #include "content/public/browser/browser_context.h"
  22. #if BUILDFLAG(IS_ANDROID)
  23. #include "chrome/browser/feature_guide/notifications/android/feature_notification_guide_bridge.h"
  24. #endif
  25. namespace feature_guide {
  26. namespace {
  27. // Default notification time interval in days.
  28. constexpr int kDefaultNotificationTimeIntervalDays = 7;
  29. void AddFeatureIfEnabled(std::vector<FeatureType>* enabled_features,
  30. const std::string& feature_name,
  31. FeatureType feature_type,
  32. int repeat_count) {
  33. if (!base::GetFieldTrialParamByFeatureAsBool(
  34. features::kFeatureNotificationGuide, feature_name, false)) {
  35. return;
  36. }
  37. for (int i = 0; i < repeat_count; i++) {
  38. enabled_features->emplace_back(feature_type);
  39. }
  40. }
  41. std::vector<FeatureType> GetEnabledFeaturesFromVariations() {
  42. std::vector<FeatureType> enabled_features;
  43. int repeat_count = base::GetFieldTrialParamByFeatureAsInt(
  44. features::kFeatureNotificationGuide, "feature_notification_repeat_count",
  45. 1);
  46. AddFeatureIfEnabled(&enabled_features, "enable_feature_incognito_tab",
  47. FeatureType::kIncognitoTab, repeat_count);
  48. AddFeatureIfEnabled(&enabled_features, "enable_feature_ntp_suggestion_card",
  49. FeatureType::kNTPSuggestionCard, repeat_count);
  50. AddFeatureIfEnabled(&enabled_features, "enable_feature_voice_search",
  51. FeatureType::kVoiceSearch, repeat_count);
  52. AddFeatureIfEnabled(&enabled_features, "enable_feature_default_browser",
  53. FeatureType::kDefaultBrowser, repeat_count);
  54. AddFeatureIfEnabled(&enabled_features, "enable_feature_sign_in",
  55. FeatureType::kSignIn, repeat_count);
  56. return enabled_features;
  57. }
  58. base::TimeDelta GetNotificationStartTimeDeltaFromVariations() {
  59. int notification_interval_days = base::GetFieldTrialParamByFeatureAsInt(
  60. features::kFeatureNotificationGuide, "notification_interval_days",
  61. kDefaultNotificationTimeIntervalDays);
  62. return base::Days(notification_interval_days);
  63. }
  64. } // namespace
  65. // static
  66. FeatureNotificationGuideServiceFactory*
  67. FeatureNotificationGuideServiceFactory::GetInstance() {
  68. return base::Singleton<FeatureNotificationGuideServiceFactory>::get();
  69. }
  70. // static
  71. FeatureNotificationGuideService*
  72. FeatureNotificationGuideServiceFactory::GetForProfile(Profile* profile) {
  73. return static_cast<FeatureNotificationGuideService*>(
  74. GetInstance()->GetServiceForBrowserContext(profile, true));
  75. }
  76. FeatureNotificationGuideServiceFactory::FeatureNotificationGuideServiceFactory()
  77. : BrowserContextKeyedServiceFactory(
  78. "FeatureNotificationGuideService",
  79. BrowserContextDependencyManager::GetInstance()) {
  80. DependsOn(NotificationScheduleServiceFactory::GetInstance());
  81. DependsOn(
  82. segmentation_platform::SegmentationPlatformServiceFactory::GetInstance());
  83. }
  84. KeyedService* FeatureNotificationGuideServiceFactory::BuildServiceInstanceFor(
  85. content::BrowserContext* context) const {
  86. Profile* profile = Profile::FromBrowserContext(context);
  87. auto* notification_scheduler =
  88. NotificationScheduleServiceFactory::GetForKey(profile->GetProfileKey());
  89. feature_engagement::Tracker* tracker =
  90. feature_engagement::TrackerFactory::GetForBrowserContext(profile);
  91. segmentation_platform::SegmentationPlatformService*
  92. segmentation_platform_service = segmentation_platform::
  93. SegmentationPlatformServiceFactory::GetForProfile(profile);
  94. Config config;
  95. config.enabled_features = GetEnabledFeaturesFromVariations();
  96. config.notification_deliver_time_delta =
  97. GetNotificationStartTimeDeltaFromVariations();
  98. config.feature_notification_tracking_only =
  99. base::GetFieldTrialParamByFeatureAsBool(
  100. features::kFeatureNotificationGuide,
  101. "feature_notification_tracking_only", false);
  102. std::unique_ptr<FeatureNotificationGuideService::Delegate> delegate;
  103. #if BUILDFLAG(IS_ANDROID)
  104. delegate.reset(new FeatureNotificationGuideBridge());
  105. #endif
  106. return new FeatureNotificationGuideServiceImpl(
  107. std::move(delegate), config, notification_scheduler, tracker,
  108. segmentation_platform_service, base::DefaultClock::GetInstance());
  109. }
  110. bool FeatureNotificationGuideServiceFactory::ServiceIsNULLWhileTesting() const {
  111. return true;
  112. }
  113. } // namespace feature_guide