feature_list_creator.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2019 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 "weblayer/browser/feature_list_creator.h"
  5. #include "base/base_switches.h"
  6. #include "base/command_line.h"
  7. #include "build/build_config.h"
  8. #include "components/metrics/metrics_state_manager.h"
  9. #include "components/prefs/pref_service.h"
  10. #include "components/variations/service/variations_service.h"
  11. #include "components/variations/variations_crash_keys.h"
  12. #include "components/variations/variations_switches.h"
  13. #include "content/public/browser/network_service_instance.h"
  14. #include "content/public/common/content_switch_dependent_feature_overrides.h"
  15. #include "services/network/public/cpp/shared_url_loader_factory.h"
  16. #include "weblayer/browser/system_network_context_manager.h"
  17. #include "weblayer/browser/weblayer_variations_service_client.h"
  18. #if BUILDFLAG(IS_ANDROID)
  19. #include "weblayer/browser/android/metrics/weblayer_metrics_service_client.h"
  20. #endif
  21. #if BUILDFLAG(IS_ANDROID)
  22. namespace switches {
  23. const char kDisableBackgroundNetworking[] = "disable-background-networking";
  24. } // namespace switches
  25. #endif
  26. namespace weblayer {
  27. namespace {
  28. FeatureListCreator* feature_list_creator_instance = nullptr;
  29. } // namespace
  30. FeatureListCreator::FeatureListCreator(PrefService* local_state)
  31. : local_state_(local_state) {
  32. DCHECK(local_state_);
  33. DCHECK(!feature_list_creator_instance);
  34. feature_list_creator_instance = this;
  35. }
  36. FeatureListCreator::~FeatureListCreator() {
  37. feature_list_creator_instance = nullptr;
  38. }
  39. // static
  40. FeatureListCreator* FeatureListCreator::GetInstance() {
  41. DCHECK(feature_list_creator_instance);
  42. return feature_list_creator_instance;
  43. }
  44. void FeatureListCreator::SetSystemNetworkContextManager(
  45. SystemNetworkContextManager* system_network_context_manager) {
  46. system_network_context_manager_ = system_network_context_manager;
  47. }
  48. void FeatureListCreator::CreateFeatureListAndFieldTrials() {
  49. #if BUILDFLAG(IS_ANDROID)
  50. WebLayerMetricsServiceClient::GetInstance()->Initialize(local_state_);
  51. #endif
  52. SetUpFieldTrials();
  53. }
  54. void FeatureListCreator::PerformPreMainMessageLoopStartup() {
  55. #if BUILDFLAG(IS_ANDROID)
  56. // It is expected this is called after SetUpFieldTrials().
  57. DCHECK(variations_service_);
  58. variations_service_->PerformPreMainMessageLoopStartup();
  59. #endif
  60. }
  61. void FeatureListCreator::OnBrowserFragmentStarted() {
  62. if (has_browser_fragment_started_)
  63. return;
  64. has_browser_fragment_started_ = true;
  65. // It is expected this is called after SetUpFieldTrials().
  66. DCHECK(variations_service_);
  67. // This function is called any time a BrowserFragment is started.
  68. // OnAppEnterForeground() really need only be called once, and because our
  69. // notion of a fragment doesn't really map to the Application as a whole,
  70. // call this function once.
  71. variations_service_->OnAppEnterForeground();
  72. }
  73. void FeatureListCreator::SetUpFieldTrials() {
  74. #if BUILDFLAG(IS_ANDROID)
  75. // The FieldTrialList should have been instantiated in
  76. // AndroidMetricsServiceClient::Initialize().
  77. DCHECK(base::FieldTrialList::GetInstance());
  78. DCHECK(system_network_context_manager_);
  79. auto* metrics_client = WebLayerMetricsServiceClient::GetInstance();
  80. variations_service_ = variations::VariationsService::Create(
  81. std::make_unique<WebLayerVariationsServiceClient>(
  82. system_network_context_manager_),
  83. local_state_, metrics_client->metrics_state_manager(),
  84. switches::kDisableBackgroundNetworking, variations::UIStringOverrider(),
  85. base::BindOnce(&content::GetNetworkConnectionTracker));
  86. variations_service_->OverridePlatform(
  87. variations::Study::PLATFORM_ANDROID_WEBLAYER, "android_weblayer");
  88. std::vector<std::string> variation_ids;
  89. auto feature_list = std::make_unique<base::FeatureList>();
  90. const base::CommandLine* command_line =
  91. base::CommandLine::ForCurrentProcess();
  92. variations_service_->SetUpFieldTrials(
  93. variation_ids,
  94. command_line->GetSwitchValueASCII(
  95. variations::switches::kForceVariationIds),
  96. content::GetSwitchDependentFeatureOverrides(*command_line),
  97. std::move(feature_list), &weblayer_field_trials_);
  98. variations::InitCrashKeys();
  99. #else
  100. // TODO(weblayer-dev): Support variations on desktop.
  101. #endif
  102. }
  103. } // namespace weblayer