OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/protocol/tracing_handler.h" | 5 #include "content/browser/devtools/protocol/tracing_handler.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 private: | 40 private: |
41 ~DevToolsTraceSinkProxy() override {} | 41 ~DevToolsTraceSinkProxy() override {} |
42 | 42 |
43 base::WeakPtr<TracingHandler> tracing_handler_; | 43 base::WeakPtr<TracingHandler> tracing_handler_; |
44 }; | 44 }; |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 TracingHandler::TracingHandler(TracingHandler::Target target) | 48 TracingHandler::TracingHandler(TracingHandler::Target target) |
49 : target_(target), | 49 : target_(target), |
50 is_recording_(false), | |
51 weak_factory_(this) { | 50 weak_factory_(this) { |
52 } | 51 } |
53 | 52 |
54 TracingHandler::~TracingHandler() { | 53 TracingHandler::~TracingHandler() { |
55 } | 54 } |
56 | 55 |
57 void TracingHandler::SetClient(scoped_ptr<Client> client) { | 56 void TracingHandler::SetClient(scoped_ptr<Client> client) { |
58 client_.swap(client); | 57 client_.swap(client); |
59 } | 58 } |
60 | 59 |
61 void TracingHandler::Detached() { | 60 void TracingHandler::Detached() { |
62 if (is_recording_) | 61 if (IsRecording()) |
63 DisableRecording(true); | 62 DisableRecording(true); |
64 } | 63 } |
65 | 64 |
66 void TracingHandler::OnTraceDataCollected(const std::string& trace_fragment) { | 65 void TracingHandler::OnTraceDataCollected(const std::string& trace_fragment) { |
67 // Hand-craft protocol notification message so we can substitute JSON | 66 // Hand-craft protocol notification message so we can substitute JSON |
68 // that we already got as string as a bare object, not a quoted string. | 67 // that we already got as string as a bare object, not a quoted string. |
69 std::string message( | 68 std::string message( |
70 "{ \"method\": \"Tracing.dataCollected\", \"params\": { \"value\": ["); | 69 "{ \"method\": \"Tracing.dataCollected\", \"params\": { \"value\": ["); |
71 const size_t messageSuffixSize = 10; | 70 const size_t messageSuffixSize = 10; |
72 message.reserve(message.size() + trace_fragment.size() + messageSuffixSize); | 71 message.reserve(message.size() + trace_fragment.size() + messageSuffixSize); |
73 message += trace_fragment; | 72 message += trace_fragment; |
74 message += "] } }"; | 73 message += "] } }"; |
75 client_->SendRawMessage(message); | 74 client_->SendRawMessage(message); |
76 } | 75 } |
77 | 76 |
78 void TracingHandler::OnTraceComplete() { | 77 void TracingHandler::OnTraceComplete() { |
79 client_->TracingComplete(TracingCompleteParams::Create()); | 78 client_->TracingComplete(TracingCompleteParams::Create()); |
80 } | 79 } |
81 | 80 |
82 Response TracingHandler::Start(DevToolsCommandId command_id, | 81 Response TracingHandler::Start(DevToolsCommandId command_id, |
83 const std::string* categories, | 82 const std::string* categories, |
84 const std::string* options, | 83 const std::string* options, |
85 const double* buffer_usage_reporting_interval) { | 84 const double* buffer_usage_reporting_interval) { |
86 if (is_recording_) | 85 if (IsRecording()) |
87 return Response::InternalError("Tracing is already started"); | 86 return Response::InternalError("Tracing is already started"); |
88 | 87 |
89 is_recording_ = true; | |
90 base::trace_event::TraceConfig trace_config( | 88 base::trace_event::TraceConfig trace_config( |
91 categories ? *categories : std::string(), | 89 categories ? *categories : std::string(), |
92 options ? *options : std::string()); | 90 options ? *options : std::string()); |
93 if (buffer_usage_reporting_interval) | 91 if (buffer_usage_reporting_interval) |
94 SetupTimer(*buffer_usage_reporting_interval); | 92 SetupTimer(*buffer_usage_reporting_interval); |
95 | 93 |
96 // If inspected target is a render process Tracing.start will be handled by | 94 // If inspected target is a render process Tracing.start will be handled by |
97 // tracing agent in the renderer. | 95 // tracing agent in the renderer. |
98 if (target_ == Renderer) { | 96 if (target_ == Renderer) { |
99 TracingController::GetInstance()->EnableRecording( | 97 TracingController::GetInstance()->EnableRecording( |
100 trace_config, | 98 trace_config, |
101 TracingController::EnableRecordingDoneCallback()); | 99 TracingController::EnableRecordingDoneCallback()); |
102 return Response::FallThrough(); | 100 return Response::FallThrough(); |
103 } | 101 } |
104 | 102 |
105 TracingController::GetInstance()->EnableRecording( | 103 TracingController::GetInstance()->EnableRecording( |
106 trace_config, | 104 trace_config, |
107 base::Bind(&TracingHandler::OnRecordingEnabled, | 105 base::Bind(&TracingHandler::OnRecordingEnabled, |
108 weak_factory_.GetWeakPtr(), | 106 weak_factory_.GetWeakPtr(), |
109 command_id)); | 107 command_id)); |
110 return Response::OK(); | 108 return Response::OK(); |
111 } | 109 } |
112 | 110 |
113 Response TracingHandler::End(DevToolsCommandId command_id) { | 111 Response TracingHandler::End(DevToolsCommandId command_id) { |
114 if (!is_recording_) | 112 if (!IsRecording()) |
115 return Response::InternalError("Tracing is not started"); | 113 return Response::InternalError("Tracing is not started"); |
116 | 114 |
117 DisableRecording(false); | 115 DisableRecording(false); |
118 // If inspected target is a render process Tracing.end will be handled by | 116 // If inspected target is a render process Tracing.end will be handled by |
119 // tracing agent in the renderer. | 117 // tracing agent in the renderer. |
120 return target_ == Renderer ? Response::FallThrough() : Response::OK(); | 118 return target_ == Renderer ? Response::FallThrough() : Response::OK(); |
121 } | 119 } |
122 | 120 |
123 Response TracingHandler::GetCategories(DevToolsCommandId command_id) { | 121 Response TracingHandler::GetCategories(DevToolsCommandId command_id) { |
124 TracingController::GetInstance()->GetCategories( | 122 TracingController::GetInstance()->GetCategories( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 FROM_HERE, interval, | 162 FROM_HERE, interval, |
165 base::Bind(base::IgnoreResult(&TracingController::GetTraceBufferUsage), | 163 base::Bind(base::IgnoreResult(&TracingController::GetTraceBufferUsage), |
166 base::Unretained(TracingController::GetInstance()), | 164 base::Unretained(TracingController::GetInstance()), |
167 base::Bind(&TracingHandler::OnBufferUsage, | 165 base::Bind(&TracingHandler::OnBufferUsage, |
168 weak_factory_.GetWeakPtr())), | 166 weak_factory_.GetWeakPtr())), |
169 true)); | 167 true)); |
170 buffer_usage_poll_timer_->Reset(); | 168 buffer_usage_poll_timer_->Reset(); |
171 } | 169 } |
172 | 170 |
173 void TracingHandler::DisableRecording(bool abort) { | 171 void TracingHandler::DisableRecording(bool abort) { |
174 is_recording_ = false; | |
175 buffer_usage_poll_timer_.reset(); | 172 buffer_usage_poll_timer_.reset(); |
176 TracingController::GetInstance()->DisableRecording( | 173 TracingController::GetInstance()->DisableRecording( |
177 abort ? nullptr : new DevToolsTraceSinkProxy(weak_factory_.GetWeakPtr())); | 174 abort ? nullptr : new DevToolsTraceSinkProxy(weak_factory_.GetWeakPtr())); |
178 } | 175 } |
179 | 176 |
| 177 bool TracingHandler::IsRecording() const { |
| 178 return TracingController::GetInstance()->IsRecording(); |
| 179 } |
| 180 |
180 } // namespace tracing | 181 } // namespace tracing |
181 } // namespace devtools | 182 } // namespace devtools |
182 } // namespace content | 183 } // namespace content |
OLD | NEW |