1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/safe_browsing/chrome_ping_manager_factory.h"
#include "base/command_line.h"
#include "base/no_destructor.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/chrome_safe_browsing_hats_delegate.h"
#include "chrome/browser/safe_browsing/chrome_user_population_helper.h"
#include "chrome/browser/safe_browsing/chrome_v4_protocol_config_provider.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/content/browser/web_ui/safe_browsing_ui.h"
#include "components/safe_browsing/core/browser/ping_manager.h"
#include "components/safe_browsing/core/browser/sync/safe_browsing_primary_account_token_fetcher.h"
#include "components/safe_browsing/core/browser/sync/sync_utils.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "content/public/browser/browser_thread.h"
namespace safe_browsing {
namespace {
bool kAllowPingManagerInTests = false;
} // namespace
// static
ChromePingManagerFactory* ChromePingManagerFactory::GetInstance() {
static base::NoDestructor<ChromePingManagerFactory> instance;
return instance.get();
}
// static
PingManager* ChromePingManagerFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<PingManager*>(
GetInstance()->GetServiceForBrowserContext(context, /*create=*/true));
}
ChromePingManagerFactory::ChromePingManagerFactory()
: ProfileKeyedServiceFactory(
"ChromeSafeBrowsingPingManager",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kOriginalOnly)
// Telemetry report should not be sent in guest mode.
.WithGuest(ProfileSelection::kNone)
.Build()) {
DependsOn(IdentityManagerFactory::GetInstance());
}
ChromePingManagerFactory::~ChromePingManagerFactory() = default;
std::unique_ptr<KeyedService>
ChromePingManagerFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context);
std::unique_ptr<ChromeSafeBrowsingHatsDelegate> hats_delegate = nullptr;
#if BUILDFLAG(FULL_SAFE_BROWSING)
hats_delegate = std::make_unique<ChromeSafeBrowsingHatsDelegate>(profile);
#endif
return PingManager::Create(
GetV4ProtocolConfig(),
g_browser_process->safe_browsing_service()->GetURLLoaderFactory(profile),
std::make_unique<SafeBrowsingPrimaryAccountTokenFetcher>(
IdentityManagerFactory::GetForProfile(profile)),
base::BindRepeating(
&ChromePingManagerFactory::ShouldFetchAccessTokenForReport, profile),
safe_browsing::WebUIInfoSingleton::GetInstance(),
content::GetUIThreadTaskRunner({}),
base::BindRepeating(&safe_browsing::GetUserPopulationForProfile, profile),
base::BindRepeating(&safe_browsing::GetPageLoadTokenForURL, profile),
std::move(hats_delegate), /*persister_root_path=*/profile->GetPath(),
base::BindRepeating(&ChromePingManagerFactory::ShouldSendPersistedReport,
profile));
}
// static
bool ChromePingManagerFactory::ShouldFetchAccessTokenForReport(
Profile* profile) {
PrefService* prefs = profile->GetPrefs();
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
return IsEnhancedProtectionEnabled(*prefs) && identity_manager &&
safe_browsing::SyncUtils::IsPrimaryAccountSignedIn(identity_manager);
}
// static
bool ChromePingManagerFactory::ShouldSendPersistedReport(Profile* profile) {
return !profile->IsOffTheRecord() &&
IsExtendedReportingEnabled(*profile->GetPrefs());
}
bool ChromePingManagerFactory::ServiceIsCreatedWithBrowserContext() const {
// PingManager is created at startup to send persisted reports.
return true;
}
bool ChromePingManagerFactory::ServiceIsNULLWhileTesting() const {
return !kAllowPingManagerInTests;
}
ChromePingManagerAllowerForTesting::ChromePingManagerAllowerForTesting() {
kAllowPingManagerInTests = true;
}
ChromePingManagerAllowerForTesting::~ChromePingManagerAllowerForTesting() {
kAllowPingManagerInTests = false;
}
} // namespace safe_browsing
|