// Copyright (c) 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_ #define CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_ #include #include #include #include #include "base/callback_forward.h" #include "base/memory/ref_counted.h" #include "base/task/task_traits.h" #include "base/timer/timer.h" #include "content/common/content_export.h" #include "content/public/browser/tracing_controller.h" #include "mojo/public/cpp/bindings/receiver.h" #include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/system/data_pipe_drainer.h" #include "services/tracing/public/mojom/perfetto_service.mojom.h" namespace perfetto { namespace protos { namespace pbzero { class TracePacket; } // namespace pbzero } // namespace protos } // namespace perfetto namespace base { namespace trace_event { class TraceConfig; } // namespace trace_event class DictionaryValue; } // namespace base namespace tracing { class BaseAgent; } // namespace tracing namespace content { class PerfettoFileTracer; class TracingDelegate; class TracingControllerImpl : public TracingController, public mojo::DataPipeDrainer::Client, public tracing::mojom::TracingSessionClient { public: // Create an endpoint for dumping the trace data to a callback. CONTENT_EXPORT static scoped_refptr CreateCallbackEndpoint( CompletionCallback callback); CONTENT_EXPORT static scoped_refptr CreateCompressedStringEndpoint(scoped_refptr endpoint, bool compress_with_background_priority); CONTENT_EXPORT static TracingControllerImpl* GetInstance(); // Should be called on the UI thread. CONTENT_EXPORT TracingControllerImpl(); // TracingController implementation. bool GetCategories(GetCategoriesDoneCallback callback) override; bool StartTracing(const base::trace_event::TraceConfig& trace_config, StartTracingDoneCallback callback) override; bool StopTracing(const scoped_refptr& endpoint) override; bool StopTracing(const scoped_refptr& endpoint, const std::string& agent_label, bool privacy_filtering_enabled = false) override; bool GetTraceBufferUsage(GetTraceBufferUsageCallback callback) override; bool IsTracing() override; // tracing::mojom::TracingSessionClient implementation: void OnTracingEnabled() override; void OnTracingDisabled(bool tracing_succeeded) override; void OnTracingFailed(); // For unittests. CONTENT_EXPORT void SetTracingDelegateForTesting( std::unique_ptr delegate); // If command line flags specify startup tracing options, adopts the startup // tracing session and relays it to all tracing agents. Note that the local // TraceLog has already been enabled at this point by // tracing::EnableStartupTracingIfNeeded(), before threads were available. // Requires browser threads to have started and a started main message loop. void StartStartupTracingIfNeeded(); // Should be called before browser main loop shutdown. If startup tracing is // tracing to a file and is still active, this stops the duration timer if it // exists. void FinalizeStartupTracingIfNeeded(); PerfettoFileTracer* perfetto_file_tracer_for_testing() const { return perfetto_file_tracer_.get(); } private: friend std::default_delete; ~TracingControllerImpl() override; void AddAgents(); void ConnectToServiceIfNeeded(); std::unique_ptr GenerateMetadataDict(); void GenerateMetadataPacket(perfetto::protos::pbzero::TracePacket* packet, bool privacy_filtering_enabled); // mojo::DataPipeDrainer::Client void OnDataAvailable(const void* data, size_t num_bytes) override; void OnDataComplete() override; void OnReadBuffersComplete(); void CompleteFlush(); void InitStartupTracingForDuration(); void EndStartupTracing(); #if defined(OS_CHROMEOS) void OnMachineStatisticsLoaded(); #endif base::FilePath GetStartupTraceFileName() const; std::unique_ptr perfetto_file_tracer_; mojo::Remote consumer_host_; mojo::Remote tracing_session_host_; mojo::Receiver receiver_{this}; StartTracingDoneCallback start_tracing_callback_; std::vector> agents_; std::unique_ptr delegate_; std::unique_ptr trace_config_; std::unique_ptr drainer_; scoped_refptr trace_data_endpoint_; bool is_data_complete_ = false; bool read_buffers_complete_ = false; base::FilePath startup_trace_file_; // This timer initiates trace file saving. base::OneShotTimer startup_trace_timer_; #if defined(OS_CHROMEOS) bool are_statistics_loaded_ = false; std::string hardware_class_; base::WeakPtrFactory weak_ptr_factory_{this}; #endif DISALLOW_COPY_AND_ASSIGN(TracingControllerImpl); }; } // namespace content #endif // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_