Index: chrome/test/base/tracing.cc |
diff --git a/chrome/test/base/tracing.cc b/chrome/test/base/tracing.cc |
index 7060b9b6c958a9c6db44b890b2ac7d37739c2792..5aa07e7e4d6f287e8de7564f6a258e09dd087e63 100644 |
--- a/chrome/test/base/tracing.cc |
+++ b/chrome/test/base/tracing.cc |
@@ -4,19 +4,21 @@ |
#include "chrome/test/base/tracing.h" |
-#include "base/debug/trace_event.h" |
+#include "base/file_util.h" |
+#include "base/files/file_path.h" |
#include "base/memory/singleton.h" |
#include "base/message_loop/message_loop.h" |
+#include "base/strings/string_util.h" |
+#include "base/timer/timer.h" |
#include "content/public/browser/browser_thread.h" |
-#include "content/public/browser/trace_controller.h" |
-#include "content/public/browser/trace_subscriber.h" |
+#include "content/public/browser/tracing_controller.h" |
#include "content/public/test/test_utils.h" |
namespace { |
using content::BrowserThread; |
-class InProcessTraceController : public content::TraceSubscriber { |
+class InProcessTraceController { |
public: |
static InProcessTraceController* GetInstance() { |
return Singleton<InProcessTraceController>::get(); |
@@ -29,8 +31,10 @@ class InProcessTraceController : public content::TraceSubscriber { |
bool BeginTracing(const std::string& category_patterns) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- return content::TraceController::GetInstance()->BeginTracing( |
- this, category_patterns, base::debug::TraceLog::RECORD_UNTIL_FULL); |
+ return content::TracingController::GetInstance()->EnableRecording( |
+ category_patterns, content::TracingController::DEFAULT_OPTIONS, |
+ content::TracingController::EnableRecordingDoneCallback()); |
+ return true; |
} |
bool BeginTracingWithWatch(const std::string& category_patterns, |
@@ -40,9 +44,11 @@ class InProcessTraceController : public content::TraceSubscriber { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
DCHECK(num_occurrences > 0); |
watch_notification_count_ = num_occurrences; |
- return BeginTracing(category_patterns) && |
- content::TraceController::GetInstance()->SetWatchEvent( |
- this, category_name, event_name); |
+ return content::TracingController::GetInstance()->SetWatchEvent( |
+ category_name, event_name, |
+ base::Bind(&InProcessTraceController::OnWatchEventMatched, |
+ base::Unretained(this))) && |
+ BeginTracing(category_patterns); |
} |
bool WaitForWatchEvent(base::TimeDelta timeout) { |
@@ -67,20 +73,16 @@ class InProcessTraceController : public content::TraceSubscriber { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
using namespace base::debug; |
- TraceResultBuffer::SimpleOutput output; |
- trace_buffer_.SetOutputCallback(output.GetCallback()); |
- |
- trace_buffer_.Start(); |
- if (!content::TraceController::GetInstance()->EndTracingAsync(this)) |
+ if (!content::TracingController::GetInstance()->DisableRecording( |
+ base::FilePath(), |
+ base::Bind(&InProcessTraceController::OnTraceDataCollected, |
+ base::Unretained(this), |
+ base::Unretained(json_trace_output)))) |
return false; |
+ |
// Wait for OnEndTracingComplete() to quit the message loop. |
- // OnTraceDataCollected may be called multiple times while blocking here. |
message_loop_runner_ = new content::MessageLoopRunner; |
message_loop_runner_->Run(); |
- trace_buffer_.Finish(); |
- trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback()); |
- |
- *json_trace_output = output.json_output; |
// Watch notifications can occur during this method's message loop run, but |
// not after, so clear them here. |
@@ -91,19 +93,40 @@ class InProcessTraceController : public content::TraceSubscriber { |
private: |
friend struct DefaultSingletonTraits<InProcessTraceController>; |
- // TraceSubscriber implementation |
- virtual void OnEndTracingComplete() OVERRIDE { |
+ void OnEndTracingComplete() { |
message_loop_runner_->Quit(); |
} |
- // TraceSubscriber implementation |
- virtual void OnTraceDataCollected( |
- const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE { |
- trace_buffer_.AddFragment(trace_fragment->data()); |
+ void OnTraceDataCollected(std::string* json_trace_output, |
+ const base::FilePath& path) { |
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&InProcessTraceController::ReadTraceData, |
+ base::Unretained(this), |
+ base::Unretained(json_trace_output), |
+ path)); |
} |
- // TraceSubscriber implementation |
- virtual void OnEventWatchNotification() OVERRIDE { |
+ void ReadTraceData(std::string* json_trace_output, |
+ const base::FilePath& path) { |
+ json_trace_output->clear(); |
+ bool ok = base::ReadFileToString(path, json_trace_output); |
+ DCHECK(ok); |
+ base::DeleteFile(path, false); |
+ |
+ // The callers expect an array of trace events. |
+ const char* preamble = "{\"traceEvents\": "; |
+ const char* trailout = "}"; |
+ DCHECK(StartsWithASCII(*json_trace_output, preamble, true)); |
+ DCHECK(EndsWith(*json_trace_output, trailout, true)); |
+ json_trace_output->erase(0, strlen(preamble)); |
+ json_trace_output->erase(json_trace_output->end() - 1); |
+ |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
+ base::Bind(&InProcessTraceController::OnEndTracingComplete, |
+ base::Unretained(this))); |
+ } |
+ |
+ void OnWatchEventMatched() { |
if (watch_notification_count_ == 0) |
return; |
if (--watch_notification_count_ == 0) { |
@@ -118,9 +141,6 @@ class InProcessTraceController : public content::TraceSubscriber { |
message_loop_runner_->Quit(); |
} |
- // For collecting trace data asynchronously. |
- base::debug::TraceResultBuffer trace_buffer_; |
- |
scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
base::OneShotTimer<InProcessTraceController> timer_; |