weblayer_ping_manager_factory.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2022 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/safe_browsing/weblayer_ping_manager_factory.h"
  5. #include "base/no_destructor.h"
  6. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  7. #include "components/safe_browsing/content/browser/web_ui/safe_browsing_ui.h"
  8. #include "components/safe_browsing/core/browser/ping_manager.h"
  9. #include "components/safe_browsing/core/common/features.h"
  10. #include "content/public/browser/browser_task_traits.h"
  11. #include "weblayer/browser/browser_context_impl.h"
  12. #include "weblayer/browser/browser_process.h"
  13. #include "weblayer/browser/profile_impl.h"
  14. #include "weblayer/browser/safe_browsing/safe_browsing_service.h"
  15. #include "weblayer/browser/safe_browsing/safe_browsing_token_fetcher_impl.h"
  16. #include "weblayer/browser/safe_browsing/weblayer_user_population_helper.h"
  17. namespace weblayer {
  18. // static
  19. WebLayerPingManagerFactory* WebLayerPingManagerFactory::GetInstance() {
  20. static base::NoDestructor<WebLayerPingManagerFactory> instance;
  21. return instance.get();
  22. }
  23. // static
  24. safe_browsing::PingManager* WebLayerPingManagerFactory::GetForBrowserContext(
  25. content::BrowserContext* context) {
  26. return static_cast<safe_browsing::PingManager*>(
  27. GetInstance()->GetServiceForBrowserContext(context, /*create=*/true));
  28. }
  29. WebLayerPingManagerFactory::WebLayerPingManagerFactory()
  30. : BrowserContextKeyedServiceFactory(
  31. "WeblayerSafeBrowsingPingManager",
  32. BrowserContextDependencyManager::GetInstance()) {}
  33. WebLayerPingManagerFactory::~WebLayerPingManagerFactory() = default;
  34. KeyedService* WebLayerPingManagerFactory::BuildServiceInstanceFor(
  35. content::BrowserContext* context) const {
  36. return safe_browsing::PingManager::Create(
  37. safe_browsing::GetV4ProtocolConfig(GetProtocolConfigClientName(),
  38. /*disable_auto_update=*/false),
  39. // TODO(crbug.com/1233532): Should WebLayer support the
  40. // kSafeBrowsingSeparateNetworkContexts feature?
  41. BrowserProcess::GetInstance()
  42. ->GetSafeBrowsingService()
  43. ->GetURLLoaderFactory(),
  44. std::make_unique<SafeBrowsingTokenFetcherImpl>(base::BindRepeating(
  45. &ProfileImpl::access_token_fetch_delegate,
  46. base::Unretained(ProfileImpl::FromBrowserContext(context)))),
  47. base::BindRepeating(
  48. &WebLayerPingManagerFactory::ShouldFetchAccessTokenForReport,
  49. base::Unretained(this), context),
  50. safe_browsing::WebUIInfoSingleton::GetInstance(),
  51. content::GetUIThreadTaskRunner({}),
  52. base::BindRepeating(&GetUserPopulationForBrowserContext, context));
  53. }
  54. bool WebLayerPingManagerFactory::ShouldFetchAccessTokenForReport(
  55. content::BrowserContext* context) const {
  56. PrefService* pref_service =
  57. static_cast<BrowserContextImpl*>(context)->pref_service();
  58. return base::FeatureList::IsEnabled(
  59. safe_browsing::kSafeBrowsingCsbrrWithToken) &&
  60. safe_browsing::IsEnhancedProtectionEnabled(*pref_service) &&
  61. // TODO(crbug.com/1171215): Change this to production mechanism for
  62. // enabling Gaia-keyed client reports once that mechanism is
  63. // determined.
  64. is_account_signed_in_for_testing_;
  65. }
  66. std::string WebLayerPingManagerFactory::GetProtocolConfigClientName() const {
  67. // Return a weblayer specific client name.
  68. return "weblayer";
  69. }
  70. // TODO(crbug.com/1171215): Remove this once browsertests can enable this
  71. // functionality via the production mechanism for doing so.
  72. void WebLayerPingManagerFactory::SignInAccountForTesting() {
  73. is_account_signed_in_for_testing_ = true;
  74. }
  75. } // namespace weblayer