| 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 |
| 16 // Only once trace manager can exist at a time. | 15 // Only once trace manager can exist at a time. |
| 17 TracingManager* g_tracing_manager = NULL; | 16 TracingManager* g_tracing_manager = NULL; |
| 18 // Trace IDs start at 1 and increase. | 17 // Trace IDs start at 1 and increase. |
| 19 int g_next_trace_id = 1; | 18 int g_next_trace_id = 1; |
| 20 // Name of the file to store the tracing data as. | 19 // Name of the file to store the tracing data as. |
| 21 const base::FilePath::CharType kTracingFilename[] = | 20 const base::FilePath::CharType kTracingFilename[] = |
| 22 FILE_PATH_LITERAL("tracing.json"); | 21 FILE_PATH_LITERAL("tracing.json"); |
| 23 } | 22 } |
| 24 | 23 |
| 25 TracingManager::TracingManager() | 24 TracingManager::TracingManager() |
| (...skipping 10 matching lines...) Expand all Loading... |
| 36 } | 35 } |
| 37 | 36 |
| 38 int TracingManager::RequestTrace() { | 37 int TracingManager::RequestTrace() { |
| 39 // Return the current trace if one is being collected. | 38 // Return the current trace if one is being collected. |
| 40 if (current_trace_id_) | 39 if (current_trace_id_) |
| 41 return current_trace_id_; | 40 return current_trace_id_; |
| 42 | 41 |
| 43 current_trace_id_ = g_next_trace_id; | 42 current_trace_id_ = g_next_trace_id; |
| 44 ++g_next_trace_id; | 43 ++g_next_trace_id; |
| 45 content::TracingController::GetInstance()->DisableRecording( | 44 content::TracingController::GetInstance()->DisableRecording( |
| 46 base::FilePath(), | 45 content::TracingController::CreateStringSink( |
| 47 base::Bind(&TracingManager::OnTraceDataCollected, | 46 base::Bind(&TracingManager::OnTraceDataCollected, |
| 48 weak_ptr_factory_.GetWeakPtr())); | 47 weak_ptr_factory_.GetWeakPtr()))); |
| 49 return current_trace_id_; | 48 return current_trace_id_; |
| 50 } | 49 } |
| 51 | 50 |
| 52 bool TracingManager::GetTraceData(int id, const TraceDataCallback& callback) { | 51 bool TracingManager::GetTraceData(int id, const TraceDataCallback& callback) { |
| 53 // If a trace is being collected currently, send it via callback when | 52 // If a trace is being collected currently, send it via callback when |
| 54 // complete. | 53 // complete. |
| 55 if (current_trace_id_) { | 54 if (current_trace_id_) { |
| 56 // Only allow one trace data request at a time. | 55 // Only allow one trace data request at a time. |
| 57 if (trace_callback_.is_null()) { | 56 if (trace_callback_.is_null()) { |
| 58 trace_callback_ = callback; | 57 trace_callback_ = callback; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 89 } | 88 } |
| 90 } | 89 } |
| 91 | 90 |
| 92 void TracingManager::StartTracing() { | 91 void TracingManager::StartTracing() { |
| 93 content::TracingController::GetInstance()->EnableRecording( | 92 content::TracingController::GetInstance()->EnableRecording( |
| 94 base::debug::CategoryFilter(), | 93 base::debug::CategoryFilter(), |
| 95 base::debug::TraceOptions(), | 94 base::debug::TraceOptions(), |
| 96 content::TracingController::EnableRecordingDoneCallback()); | 95 content::TracingController::EnableRecordingDoneCallback()); |
| 97 } | 96 } |
| 98 | 97 |
| 99 void TracingManager::OnTraceDataCollected(const base::FilePath& path) { | 98 void TracingManager::OnTraceDataCollected(base::RefCountedString* trace_data) { |
| 100 if (!current_trace_id_) | 99 if (!current_trace_id_) |
| 101 return; | 100 return; |
| 102 | 101 |
| 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; | 102 std::string output_val; |
| 111 feedback_util::ZipString( | 103 feedback_util::ZipString( |
| 112 base::FilePath(kTracingFilename), data, &output_val); | 104 base::FilePath(kTracingFilename), trace_data->data(), &output_val); |
| 113 | 105 |
| 114 scoped_refptr<base::RefCountedString> output( | 106 scoped_refptr<base::RefCountedString> output( |
| 115 base::RefCountedString::TakeString(&output_val)); | 107 base::RefCountedString::TakeString(&output_val)); |
| 116 | 108 |
| 117 trace_data_[current_trace_id_] = output; | 109 trace_data_[current_trace_id_] = output; |
| 118 | 110 |
| 119 if (!trace_callback_.is_null()) { | 111 if (!trace_callback_.is_null()) { |
| 120 trace_callback_.Run(output); | 112 trace_callback_.Run(output); |
| 121 trace_callback_.Reset(); | 113 trace_callback_.Reset(); |
| 122 } | 114 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 134 // static | 126 // static |
| 135 scoped_ptr<TracingManager> TracingManager::Create() { | 127 scoped_ptr<TracingManager> TracingManager::Create() { |
| 136 if (g_tracing_manager) | 128 if (g_tracing_manager) |
| 137 return scoped_ptr<TracingManager>(); | 129 return scoped_ptr<TracingManager>(); |
| 138 return scoped_ptr<TracingManager>(new TracingManager()); | 130 return scoped_ptr<TracingManager>(new TracingManager()); |
| 139 } | 131 } |
| 140 | 132 |
| 141 TracingManager* TracingManager::Get() { | 133 TracingManager* TracingManager::Get() { |
| 142 return g_tracing_manager; | 134 return g_tracing_manager; |
| 143 } | 135 } |
| OLD | NEW |