1 // Copyright 2014 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 5 #ifndef CONTENT_BROWSER_DEVTOOLS_PROTOCOL_TRACING_HANDLER_H_ 6 #define CONTENT_BROWSER_DEVTOOLS_PROTOCOL_TRACING_HANDLER_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <memory> 12 #include <set> 13 #include <string> 14 #include <unordered_set> 15 #include <vector> 16 17 #include "base/gtest_prod_util.h" 18 #include "base/macros.h" 19 #include "base/memory/weak_ptr.h" 20 #include "base/trace_event/trace_event.h" 21 #include "content/browser/devtools/protocol/devtools_domain_handler.h" 22 #include "content/browser/devtools/protocol/tracing.h" 23 #include "content/common/content_export.h" 24 #include "content/public/browser/tracing_controller.h" 25 26 namespace base { 27 class RepeatingTimer; 28 } 29 30 namespace media { 31 class VideoFrame; 32 } 33 34 namespace content { 35 36 class DevToolsAgentHostImpl; 37 class DevToolsVideoConsumer; 38 class DevToolsIOContext; 39 class FrameTreeNode; 40 class NavigationRequest; 41 class RenderFrameHost; 42 class RenderProcessHost; 43 44 namespace protocol { 45 46 class TracingHandler : public DevToolsDomainHandler, public Tracing::Backend { 47 public: 48 CONTENT_EXPORT TracingHandler(FrameTreeNode* frame_tree_node, 49 DevToolsIOContext* io_context); 50 CONTENT_EXPORT ~TracingHandler() override; 51 52 static std::vector<TracingHandler*> ForAgentHost(DevToolsAgentHostImpl* host); 53 54 // DevToolsDomainHandler implementation. 55 void SetRenderer(int process_host_id, 56 RenderFrameHostImpl* frame_host) override; 57 void Wire(UberDispatcher* dispatcher) override; 58 Response Disable() override; 59 60 void OnTraceDataCollected(std::unique_ptr<std::string> trace_fragment); 61 void OnTraceComplete(); 62 void OnTraceToStreamComplete(const std::string& stream_handle); 63 64 // Protocol methods. 65 void Start(Maybe<std::string> categories, 66 Maybe<std::string> options, 67 Maybe<double> buffer_usage_reporting_interval, 68 Maybe<std::string> transfer_mode, 69 Maybe<std::string> transfer_format, 70 Maybe<std::string> transfer_compression, 71 Maybe<Tracing::TraceConfig> config, 72 std::unique_ptr<StartCallback> callback) override; 73 Response End() override; 74 void GetCategories(std::unique_ptr<GetCategoriesCallback> callback) override; 75 void RequestMemoryDump( 76 Maybe<bool> deterministic, 77 Maybe<std::string> level_of_detail, 78 std::unique_ptr<RequestMemoryDumpCallback> callback) override; 79 Response RecordClockSyncMarker(const std::string& sync_id) override; 80 did_initiate_recording()81 bool did_initiate_recording() { return did_initiate_recording_; } 82 void ReadyToCommitNavigation(NavigationRequest* navigation_request); 83 void FrameDeleted(RenderFrameHostImpl* frame_host); 84 85 private: 86 friend class TracingHandlerTest; 87 88 class TracingSession; 89 class LegacyTracingSession; 90 class PerfettoTracingSession; 91 92 struct TraceDataBufferState { 93 public: 94 std::string data; 95 size_t pos = 0; 96 int open_braces = 0; 97 bool in_string = false; 98 bool slashed = false; 99 size_t offset = 0; 100 }; 101 102 void OnRecordingEnabled(std::unique_ptr<StartCallback> callback); 103 void OnBufferUsage(float percent_full, size_t approximate_event_count); 104 void OnCategoriesReceived(std::unique_ptr<GetCategoriesCallback> callback, 105 const std::set<std::string>& category_set); 106 void OnMemoryDumpFinished(std::unique_ptr<RequestMemoryDumpCallback> callback, 107 bool success, 108 uint64_t dump_id); 109 void OnFrameFromVideoConsumer(scoped_refptr<media::VideoFrame> frame); 110 // Assuming that the input is a potentially incomplete string representation 111 // of a comma separated list of JSON objects, return the longest prefix that 112 // is a valid list and store the rest to be used in subsequent calls. 113 CONTENT_EXPORT std::string UpdateTraceDataBuffer( 114 const std::string& trace_fragment); 115 116 void SetupTimer(double usage_reporting_interval); 117 void UpdateBufferUsage(); 118 void StopTracing( 119 const scoped_refptr<TracingController::TraceDataEndpoint>& endpoint, 120 const std::string& agent_label); 121 bool IsTracing() const; 122 void EmitFrameTree(); 123 static bool IsStartupTracingActive(); 124 CONTENT_EXPORT static base::trace_event::TraceConfig 125 GetTraceConfigFromDevToolsConfig( 126 const base::DictionaryValue& devtools_config); 127 void SetupProcessFilter(base::ProcessId gpu_pid, RenderFrameHost*); 128 void StartTracingWithGpuPid(std::unique_ptr<StartCallback>, 129 base::ProcessId gpu_pid); 130 void AppendProcessId(RenderFrameHost*, 131 std::unordered_set<base::ProcessId>* process_set); 132 void OnProcessReady(RenderProcessHost*); 133 134 std::unique_ptr<base::RepeatingTimer> buffer_usage_poll_timer_; 135 136 std::unique_ptr<Tracing::Frontend> frontend_; 137 DevToolsIOContext* io_context_; 138 FrameTreeNode* frame_tree_node_; 139 bool did_initiate_recording_; 140 bool return_as_stream_; 141 bool gzip_compression_; 142 bool proto_format_; 143 double buffer_usage_reporting_interval_; 144 TraceDataBufferState trace_data_buffer_state_; 145 std::unique_ptr<DevToolsVideoConsumer> video_consumer_; 146 int number_of_screenshots_from_video_consumer_ = 0; 147 base::trace_event::TraceConfig trace_config_; 148 std::unique_ptr<TracingSession> session_; 149 base::WeakPtrFactory<TracingHandler> weak_factory_{this}; 150 151 FRIEND_TEST_ALL_PREFIXES(TracingHandlerTest, 152 GetTraceConfigFromDevToolsConfig); 153 DISALLOW_COPY_AND_ASSIGN(TracingHandler); 154 }; 155 156 } // namespace protocol 157 } // namespace content 158 159 #endif // CONTENT_BROWSER_DEVTOOLS_PROTOCOL_TRACING_HANDLER_H_ 160