#include "chrome/browser/enterprise/connectors/reporting/realtime_reporting_client.h"
#include <optional>
#include <utility>
#include <vector>
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/i18n/time_formatting.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/connectors/common.h"
#include "chrome/browser/enterprise/connectors/connectors_service.h"
#include "chrome/browser/enterprise/connectors/reporting/metrics_utils.h"
#include "chrome/browser/enterprise/identifiers/profile_id_service_factory.h"
#include "chrome/browser/extensions/api/safe_browsing_private/safe_browsing_private_event_router.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/reporting_util.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/enterprise/browser/identifiers/profile_id_service.h"
#include "components/enterprise/connectors/core/reporting_service_settings.h"
#include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "components/policy/core/common/cloud/cloud_policy_util.h"
#include "components/policy/core/common/cloud/device_management_service.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_manager.h"
#include "components/policy/core/common/cloud/realtime_reporting_job_configuration.h"
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
#include "components/safe_browsing/content/browser/web_ui/safe_browsing_ui.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "content/public/browser/browser_context.h"
#include "extensions/browser/event_router.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/components/mgs/managed_guest_session_utils.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/policy/core/user_cloud_policy_manager_ash.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/browser_process_platform_part_chromeos.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#else
#include "components/enterprise/browser/controller/browser_dm_token_storage.h"
#include "components/enterprise/browser/controller/chrome_browser_cloud_management_controller.h"
#include "components/policy/core/common/cloud/reporting_job_configuration_base.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/strings/utf_string_conversions.h"
#endif
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
#include "chrome/browser/enterprise/signals/signals_aggregator_factory.h"
#include "chrome/browser/enterprise/signin/enterprise_signin_prefs.h"
#include "components/device_signals/core/browser/signals_aggregator.h"
#include "components/device_signals/core/common/signals_constants.h"
#include "components/policy/core/common/features.h"
#endif
namespace enterprise_connectors {
namespace {
#if BUILDFLAG(IS_CHROMEOS_ASH)
const char kPolicyClientDescription[] = "any";
#else
const char kChromeBrowserCloudManagementClientDescription[] = …;
#endif
const char kProfilePolicyClientDescription[] = …;
bool IsClientValid(const std::string& dm_token,
policy::CloudPolicyClient* client) { … }
void UploadCallback(base::Value::Dict event_wrapper,
bool per_profile,
policy::CloudPolicyClient* client,
EnterpriseReportingEventType eventType,
policy::CloudPolicyClient::Result upload_result) { … }
void UploadSecurityEventReport(base::Value::Dict event,
policy::CloudPolicyClient* client,
std::string name,
const ReportingSettings& settings,
content::BrowserContext* context,
base::Time time) { … }
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
void PopulateSignals(base::Value::Dict event,
policy::CloudPolicyClient* client,
std::string name,
ReportingSettings settings,
content::BrowserContext* context,
base::Time time,
device_signals::SignalsAggregationResponse response) { … }
#endif
bool IsManagedGuestSession() { … }
}
const char RealtimeReportingClient::kKeyProfileIdentifier[] = …;
const char RealtimeReportingClient::kKeyProfileUserName[] = …;
RealtimeReportingClient::RealtimeReportingClient(
content::BrowserContext* context)
: … { … }
RealtimeReportingClient::~RealtimeReportingClient() { … }
bool RealtimeReportingClient::ShouldInitRealtimeReportingClient() { … }
void RealtimeReportingClient::SetBrowserCloudPolicyClientForTesting(
policy::CloudPolicyClient* client) { … }
void RealtimeReportingClient::SetProfileCloudPolicyClientForTesting(
policy::CloudPolicyClient* client) { … }
void RealtimeReportingClient::SetIdentityManagerForTesting(
signin::IdentityManager* identity_manager) { … }
void RealtimeReportingClient::InitRealtimeReportingClient(
const ReportingSettings& settings) { … }
std::pair<std::string, policy::CloudPolicyClient*>
RealtimeReportingClient::InitBrowserReportingClient(
const std::string& dm_token) { … }
#if !BUILDFLAG(IS_CHROMEOS_ASH)
std::pair<std::string, policy::CloudPolicyClient*>
RealtimeReportingClient::InitProfileReportingClient(
const std::string& dm_token) { … }
#endif
void RealtimeReportingClient::OnCloudPolicyClientAvailable(
const std::string& policy_client_desc,
policy::CloudPolicyClient* client) { … }
std::optional<ReportingSettings>
RealtimeReportingClient::GetReportingSettings() { … }
void RealtimeReportingClient::ReportRealtimeEvent(
const std::string& name,
const ReportingSettings& settings,
base::Value::Dict event) { … }
void RealtimeReportingClient::ReportPastEvent(const std::string& name,
const ReportingSettings& settings,
base::Value::Dict event,
const base::Time& time) { … }
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
void AddCrowdstrikeSignalsToEvent(
base::Value::Dict& event,
const device_signals::SignalsAggregationResponse& response) { … }
#endif
void RealtimeReportingClient::ReportEventWithTimestamp(
const std::string& name,
const ReportingSettings& settings,
base::Value::Dict event,
const base::Time& time,
bool include_profile_user_name) { … }
std::string RealtimeReportingClient::GetProfileUserName() const { … }
std::string RealtimeReportingClient::GetProfileIdentifier() const { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
const user_manager::User* RealtimeReportingClient::GetChromeOSUser() {
return user_manager::UserManager::IsInitialized()
? user_manager::UserManager::Get()->GetPrimaryUser()
: nullptr;
}
#endif
void RealtimeReportingClient::RemoveDmTokenFromRejectedSet(
const std::string& dm_token) { … }
void RealtimeReportingClient::OnClientError(policy::CloudPolicyClient* client) { … }
base::WeakPtr<RealtimeReportingClient> RealtimeReportingClient::GetWeakPtr() { … }
}