Chromium Code Reviews| 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 "components/feedback/tracing_manager.h" | 5 #include "components/feedback/tracing_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 12 #include "components/feedback/feedback_util.h" | 10 #include "components/feedback/feedback_util.h" |
| 13 #include "content/public/browser/tracing_controller.h" | 11 #include "content/public/browser/tracing_controller.h" |
| 14 | 12 |
| 15 namespace { | 13 namespace { |
| 14 | |
| 15 class StringTraceDataSink : public content::TracingController::TraceDataSink { | |
|
dsinclair
2014/09/05 19:27:42
Is this the same as the StringTraceDataSink in Tra
| |
| 16 public: | |
| 17 typedef base::Callback<void(const std::string&)> CompletionCallback; | |
| 18 | |
| 19 explicit StringTraceDataSink(CompletionCallback callback) | |
| 20 : completion_callback_(callback) { | |
| 21 } | |
| 22 | |
| 23 // TracingController::TraceDataSink implementation | |
| 24 virtual void AddTraceChunk(const std::string& chunk) OVERRIDE { | |
| 25 if (!trace_.empty()) | |
| 26 trace_ += ","; | |
| 27 trace_ += chunk; | |
| 28 } | |
| 29 virtual void SetSystemTrace(const std::string& data) OVERRIDE { | |
| 30 system_trace_ = data; | |
| 31 } | |
| 32 virtual void Close() OVERRIDE { | |
| 33 std::string result = "{\"traceEvents\":[" + trace_ + "]"; | |
| 34 if (!system_trace_.empty()) | |
| 35 result += ",\"systemTraceEvents\":[" + system_trace_ + "]"; | |
| 36 result += "}"; | |
| 37 | |
| 38 completion_callback_.Run(result); | |
| 39 } | |
| 40 | |
| 41 private: | |
| 42 virtual ~StringTraceDataSink() {} | |
| 43 | |
| 44 std::string trace_; | |
| 45 std::string system_trace_; | |
| 46 CompletionCallback completion_callback_; | |
| 47 }; | |
| 48 | |
| 49 | |
| 16 // Only once trace manager can exist at a time. | 50 // Only once trace manager can exist at a time. |
| 17 TracingManager* g_tracing_manager = NULL; | 51 TracingManager* g_tracing_manager = NULL; |
| 18 // Trace IDs start at 1 and increase. | 52 // Trace IDs start at 1 and increase. |
| 19 int g_next_trace_id = 1; | 53 int g_next_trace_id = 1; |
| 20 // Name of the file to store the tracing data as. | 54 // Name of the file to store the tracing data as. |
| 21 const base::FilePath::CharType kTracingFilename[] = | 55 const base::FilePath::CharType kTracingFilename[] = |
| 22 FILE_PATH_LITERAL("tracing.json"); | 56 FILE_PATH_LITERAL("tracing.json"); |
| 23 } | 57 } |
| 24 | 58 |
| 25 TracingManager::TracingManager() | 59 TracingManager::TracingManager() |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 36 } | 70 } |
| 37 | 71 |
| 38 int TracingManager::RequestTrace() { | 72 int TracingManager::RequestTrace() { |
| 39 // Return the current trace if one is being collected. | 73 // Return the current trace if one is being collected. |
| 40 if (current_trace_id_) | 74 if (current_trace_id_) |
| 41 return current_trace_id_; | 75 return current_trace_id_; |
| 42 | 76 |
| 43 current_trace_id_ = g_next_trace_id; | 77 current_trace_id_ = g_next_trace_id; |
| 44 ++g_next_trace_id; | 78 ++g_next_trace_id; |
| 45 content::TracingController::GetInstance()->DisableRecording( | 79 content::TracingController::GetInstance()->DisableRecording( |
| 46 base::FilePath(), | 80 new StringTraceDataSink(base::Bind(&TracingManager::OnTraceDataCollected, |
| 47 base::Bind(&TracingManager::OnTraceDataCollected, | 81 weak_ptr_factory_.GetWeakPtr()))); |
| 48 weak_ptr_factory_.GetWeakPtr())); | |
| 49 return current_trace_id_; | 82 return current_trace_id_; |
| 50 } | 83 } |
| 51 | 84 |
| 52 bool TracingManager::GetTraceData(int id, const TraceDataCallback& callback) { | 85 bool TracingManager::GetTraceData(int id, const TraceDataCallback& callback) { |
| 53 // If a trace is being collected currently, send it via callback when | 86 // If a trace is being collected currently, send it via callback when |
| 54 // complete. | 87 // complete. |
| 55 if (current_trace_id_) { | 88 if (current_trace_id_) { |
| 56 // Only allow one trace data request at a time. | 89 // Only allow one trace data request at a time. |
| 57 if (trace_callback_.is_null()) { | 90 if (trace_callback_.is_null()) { |
| 58 trace_callback_ = callback; | 91 trace_callback_ = callback; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 89 } | 122 } |
| 90 } | 123 } |
| 91 | 124 |
| 92 void TracingManager::StartTracing() { | 125 void TracingManager::StartTracing() { |
| 93 content::TracingController::GetInstance()->EnableRecording( | 126 content::TracingController::GetInstance()->EnableRecording( |
| 94 base::debug::CategoryFilter(), | 127 base::debug::CategoryFilter(), |
| 95 base::debug::TraceOptions(), | 128 base::debug::TraceOptions(), |
| 96 content::TracingController::EnableRecordingDoneCallback()); | 129 content::TracingController::EnableRecordingDoneCallback()); |
| 97 } | 130 } |
| 98 | 131 |
| 99 void TracingManager::OnTraceDataCollected(const base::FilePath& path) { | 132 void TracingManager::OnTraceDataCollected(const std::string& trace_data) { |
| 100 if (!current_trace_id_) | 133 if (!current_trace_id_) |
| 101 return; | 134 return; |
| 102 | 135 |
| 103 std::string data; | |
| 104 if (!base::ReadFileToString(path, &data)) { | |
| 105 LOG(ERROR) << "Failed to read trace data from: " << path.value(); | |
| 106 return; | |
| 107 } | |
| 108 base::DeleteFile(path, false); | |
| 109 | |
| 110 std::string output_val; | 136 std::string output_val; |
| 111 feedback_util::ZipString( | 137 feedback_util::ZipString( |
| 112 base::FilePath(kTracingFilename), data, &output_val); | 138 base::FilePath(kTracingFilename), trace_data, &output_val); |
| 113 | 139 |
| 114 scoped_refptr<base::RefCountedString> output( | 140 scoped_refptr<base::RefCountedString> output( |
| 115 base::RefCountedString::TakeString(&output_val)); | 141 base::RefCountedString::TakeString(&output_val)); |
| 116 | 142 |
| 117 trace_data_[current_trace_id_] = output; | 143 trace_data_[current_trace_id_] = output; |
| 118 | 144 |
| 119 if (!trace_callback_.is_null()) { | 145 if (!trace_callback_.is_null()) { |
| 120 trace_callback_.Run(output); | 146 trace_callback_.Run(output); |
| 121 trace_callback_.Reset(); | 147 trace_callback_.Reset(); |
| 122 } | 148 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 134 // static | 160 // static |
| 135 scoped_ptr<TracingManager> TracingManager::Create() { | 161 scoped_ptr<TracingManager> TracingManager::Create() { |
| 136 if (g_tracing_manager) | 162 if (g_tracing_manager) |
| 137 return scoped_ptr<TracingManager>(); | 163 return scoped_ptr<TracingManager>(); |
| 138 return scoped_ptr<TracingManager>(new TracingManager()); | 164 return scoped_ptr<TracingManager>(new TracingManager()); |
| 139 } | 165 } |
| 140 | 166 |
| 141 TracingManager* TracingManager::Get() { | 167 TracingManager* TracingManager::Get() { |
| 142 return g_tracing_manager; | 168 return g_tracing_manager; |
| 143 } | 169 } |
| OLD | NEW |