#include "content/browser/tracing/background_tracing_manager_impl.h"
#include <optional>
#include <utility>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/location.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/sequence_bound.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/variations/hashing.h"
#include "content/browser/tracing/background_tracing_active_scenario.h"
#include "content/browser/tracing/background_tracing_agent_client_impl.h"
#include "content/browser/tracing/background_tracing_rule.h"
#include "content/browser/tracing/trace_report/trace_report_database.h"
#include "content/browser/tracing/trace_report/trace_upload_list.h"
#include "content/browser/tracing/tracing_controller_impl.h"
#include "content/common/child_process.mojom.h"
#include "content/public/browser/browser_child_process_host.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/browser/child_process_host.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/tracing_delegate.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "net/base/network_change_notifier.h"
#include "services/tracing/public/cpp/perfetto/trace_event_data_source.h"
#include "services/tracing/public/cpp/trace_event_agent.h"
#include "services/tracing/public/cpp/trace_startup_config.h"
#include "services/tracing/public/cpp/tracing_features.h"
#include "third_party/zlib/google/compression_utils.h"
namespace content {
namespace {
const base::TimeDelta kTraceReportTimeToLive = …;
const size_t kMaxTraceContent = …;
const base::TimeDelta kMinTimeUntilNextUpload = …;
const size_t kMaxTracesPerScenario = …;
const base::TimeDelta kMaxTracesPerScenarioDuration = …;
const char kBackgroundTracingConfig[] = …;
BackgroundTracingManager* g_background_tracing_manager = …;
BackgroundTracingManagerImpl* g_background_tracing_manager_impl = …;
void OpenDatabaseOnDatabaseTaskRunner(
TraceReportDatabase* database,
std::optional<base::FilePath> database_dir,
base::OnceCallback<void(BackgroundTracingManagerImpl::ScenarioCountMap,
std::optional<BaseTraceReport>,
bool)> on_database_created) { … }
void AddTraceOnDatabaseTaskRunner(
TraceReportDatabase* database,
std::string&& serialized_trace,
std::string&& serialized_system_profile,
BaseTraceReport base_report,
bool should_save_trace,
bool is_crash_scenario,
base::OnceCallback<void(std::optional<NewTraceReport>, bool)>
on_trace_saved) { … }
void GetProtoValueOnDatabaseTaskRunner(
TraceReportDatabase* database,
base::Token uuid,
base::OnceCallback<void(std::optional<std::string>,
std::optional<std::string>)> receive_callback,
base::OnceCallback<void(std::optional<BaseTraceReport>, bool)>
on_finalize_complete) { … }
class PreferenceManagerImpl
: public BackgroundTracingManagerImpl::PreferenceManager { … };
}
BASE_FEATURE(…);
const char BackgroundTracingManager::kContentTriggerConfig[] = …;
std::unique_ptr<BackgroundTracingManager>
BackgroundTracingManager::CreateInstance() { … }
BackgroundTracingManager& BackgroundTracingManager::GetInstance() { … }
void BackgroundTracingManager::SetInstance(
BackgroundTracingManager* tracing_manager) { … }
void BackgroundTracingManagerImpl::RecordMetric(Metrics metric) { … }
BackgroundTracingManagerImpl& BackgroundTracingManagerImpl::GetInstance() { … }
void BackgroundTracingManagerImpl::ActivateForProcess(
int child_process_id,
mojom::ChildProcess* child_process) { … }
BackgroundTracingManagerImpl::BackgroundTracingManagerImpl()
: … { … }
BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() { … }
void BackgroundTracingManagerImpl::OpenDatabaseIfExists() { … }
void BackgroundTracingManagerImpl::GetAllTraceReports(
GetReportsCallback callback) { … }
void BackgroundTracingManagerImpl::DeleteSingleTrace(
const base::Token& trace_uuid,
FinishedProcessingCallback callback) { … }
void BackgroundTracingManagerImpl::DeleteAllTraces(
TraceUploadList::FinishedProcessingCallback callback) { … }
void BackgroundTracingManagerImpl::UserUploadSingleTrace(
const base::Token& trace_uuid,
TraceUploadList::FinishedProcessingCallback callback) { … }
void BackgroundTracingManagerImpl::DownloadTrace(const base::Token& trace_uuid,
GetProtoCallback callback) { … }
void BackgroundTracingManagerImpl::OnTraceDatabaseCreated(
ScenarioCountMap scenario_saved_counts,
std::optional<BaseTraceReport> trace_to_upload,
bool creation_result) { … }
void BackgroundTracingManagerImpl::OnTraceDatabaseUpdated(
ScenarioCountMap scenario_saved_counts) { … }
void BackgroundTracingManagerImpl::OnTraceSaved(
const std::string& scenario_name,
std::optional<NewTraceReport> trace_to_upload,
bool success) { … }
void BackgroundTracingManagerImpl::AddMetadataGeneratorFunction() { … }
bool BackgroundTracingManagerImpl::RequestActivateScenario() { … }
void BackgroundTracingManagerImpl::SetReceiveCallback(
ReceiveCallback receive_callback) { … }
bool BackgroundTracingManagerImpl::InitializePerfettoTriggerRules(
const perfetto::protos::gen::TracingTriggerRulesConfig& config) { … }
bool BackgroundTracingManagerImpl::InitializeFieldScenarios(
const perfetto::protos::gen::ChromeFieldTracingConfig& config,
DataFiltering data_filtering) { … }
std::vector<std::string> BackgroundTracingManagerImpl::AddPresetScenarios(
const perfetto::protos::gen::ChromeFieldTracingConfig& config,
DataFiltering data_filtering) { … }
std::vector<trace_report::mojom::ScenarioPtr>
BackgroundTracingManagerImpl::GetAllFieldScenarios() const { … }
std::vector<trace_report::mojom::ScenarioPtr>
BackgroundTracingManagerImpl::GetAllPresetScenarios() const { … }
bool BackgroundTracingManagerImpl::SetEnabledScenarios(
std::vector<std::string> enabled_scenarios) { … }
std::vector<std::string> BackgroundTracingManagerImpl::GetEnabledScenarios()
const { … }
bool BackgroundTracingManagerImpl::SetActiveScenario(
std::unique_ptr<BackgroundTracingConfig> config,
DataFiltering data_filtering) { … }
void BackgroundTracingManagerImpl::InitializeTraceReportDatabase(
bool open_in_memory) { … }
bool BackgroundTracingManagerImpl::OnScenarioActive(
TracingScenario* active_scenario) { … }
bool BackgroundTracingManagerImpl::OnScenarioIdle(
TracingScenario* idle_scenario) { … }
void BackgroundTracingManagerImpl::OnScenarioRecording(
TracingScenario* scenario) { … }
void BackgroundTracingManagerImpl::SaveTrace(
TracingScenario* scenario,
base::Token trace_uuid,
const BackgroundTracingRule* triggered_rule,
std::string&& trace_data) { … }
bool BackgroundTracingManagerImpl::HasActiveScenario() { … }
bool BackgroundTracingManagerImpl::HasTraceToUpload() { … }
void BackgroundTracingManagerImpl::GetTraceToUpload(
base::OnceCallback<void(std::optional<std::string>,
std::optional<std::string>)> receive_callback) { … }
void BackgroundTracingManagerImpl::OnFinalizeComplete(
std::optional<BaseTraceReport> trace_to_upload,
bool success) { … }
void BackgroundTracingManagerImpl::AddEnabledStateObserverForTesting(
BackgroundTracingManager::EnabledStateTestObserver* observer) { … }
void BackgroundTracingManagerImpl::RemoveEnabledStateObserverForTesting(
BackgroundTracingManager::EnabledStateTestObserver* observer) { … }
void BackgroundTracingManagerImpl::AddAgent(
tracing::mojom::BackgroundTracingAgent* agent) { … }
void BackgroundTracingManagerImpl::RemoveAgent(
tracing::mojom::BackgroundTracingAgent* agent) { … }
void BackgroundTracingManagerImpl::AddAgentObserver(AgentObserver* observer) { … }
void BackgroundTracingManagerImpl::RemoveAgentObserver(
AgentObserver* observer) { … }
BackgroundTracingActiveScenario*
BackgroundTracingManagerImpl::GetActiveScenarioForTesting() { … }
bool BackgroundTracingManagerImpl::IsTracingForTesting() { … }
void BackgroundTracingManagerImpl::SaveTraceForTesting(
std::string&& serialized_trace,
const std::string& scenario_name,
const std::string& rule_name,
const base::Token& uuid) { … }
void BackgroundTracingManagerImpl::SetPreferenceManagerForTesting(
std::unique_ptr<PreferenceManager> preferences) { … }
size_t BackgroundTracingManagerImpl::GetScenarioSavedCount(
const std::string& scenario_name) { … }
void BackgroundTracingManagerImpl::OnProtoDataComplete(
std::string&& serialized_trace,
const std::string& scenario_name,
const std::string& rule_name,
bool privacy_filter_enabled,
bool is_crash_scenario,
const base::Token& uuid) { … }
std::unique_ptr<content::BackgroundTracingConfig>
BackgroundTracingManagerImpl::GetBackgroundTracingConfig(
const std::string& trial_name) { … }
void BackgroundTracingManagerImpl::SetSystemProfileRecorder(
base::RepeatingCallback<std::string()> recorder) { … }
void BackgroundTracingManagerImpl::AddNamedTriggerObserver(
const std::string& trigger_name,
BackgroundTracingRule* observer) { … }
void BackgroundTracingManagerImpl::RemoveNamedTriggerObserver(
const std::string& trigger_name,
BackgroundTracingRule* observer) { … }
bool BackgroundTracingManagerImpl::DoEmitNamedTrigger(
const std::string& trigger_name,
std::optional<int32_t> value) { … }
void BackgroundTracingManagerImpl::InvalidateTriggersCallbackForTesting() { … }
void BackgroundTracingManagerImpl::OnStartTracingDone() { … }
void BackgroundTracingManagerImpl::GenerateMetadataProto(
perfetto::protos::pbzero::ChromeMetadataPacket* metadata,
bool privacy_filtering_enabled) { … }
void BackgroundTracingManagerImpl::AbortScenarioForTesting() { … }
void BackgroundTracingManagerImpl::OnScenarioAborted() { … }
void BackgroundTracingManagerImpl::CleanDatabase() { … }
void BackgroundTracingManagerImpl::DeleteTracesInDateRange(base::Time start,
base::Time end) { … }
void BackgroundTracingManagerImpl::AddPendingAgent(
int child_process_id,
mojo::PendingRemote<tracing::mojom::BackgroundTracingAgentProvider>
pending_provider) { … }
void BackgroundTracingManagerImpl::ClearPendingAgent(int child_process_id) { … }
void BackgroundTracingManagerImpl::MaybeConstructPendingAgents() { … }
size_t BackgroundTracingManagerImpl::GetTraceUploadLimitKb() const { … }
}