blob: f210652a5aaba2190ae6c3c60322b543bacc5bcc [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/base/tracing.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/macros.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 "base/trace_event/trace_event.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/tracing_controller.h"
#include "content/public/test/test_utils.h"
namespace {
using content::BrowserThread;
class StringTraceSink : public content::TracingController::TraceDataSink {
public:
StringTraceSink(std::string* result, const base::Closure& callback)
: result_(result), completion_callback_(callback) {}
void AddTraceChunk(const std::string& chunk) override {
*result_ += result_->empty() ? "[" : ",";
*result_ += chunk;
}
void Close() override {
if (!result_->empty())
*result_ += "]";
completion_callback_.Run();
}
private:
~StringTraceSink() override {}
std::string* result_;
base::Closure completion_callback_;
DISALLOW_COPY_AND_ASSIGN(StringTraceSink);
};
class InProcessTraceController {
public:
static InProcessTraceController* GetInstance() {
return base::Singleton<InProcessTraceController>::get();
}
InProcessTraceController() {}
virtual ~InProcessTraceController() {}
bool BeginTracing(const base::trace_event::TraceConfig& trace_config) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return content::TracingController::GetInstance()->StartTracing(
trace_config, content::TracingController::StartTracingDoneCallback());
}
bool EndTracing(std::string* json_trace_output) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
using namespace base::debug;
if (!content::TracingController::GetInstance()->StopTracing(
new StringTraceSink(
json_trace_output,
base::Bind(&InProcessTraceController::OnTracingComplete,
base::Unretained(this))))) {
return false;
}
// Wait for OnEndTracingComplete() to quit the message loop.
message_loop_runner_ = new content::MessageLoopRunner;
message_loop_runner_->Run();
return true;
}
private:
friend struct base::DefaultSingletonTraits<InProcessTraceController>;
void OnEnableTracingComplete() {
message_loop_runner_->Quit();
}
void OnTracingComplete() { message_loop_runner_->Quit(); }
scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
base::OneShotTimer timer_;
DISALLOW_COPY_AND_ASSIGN(InProcessTraceController);
};
} // namespace
namespace tracing {
bool BeginTracing(const std::string& category_patterns) {
return InProcessTraceController::GetInstance()->BeginTracing(
base::trace_event::TraceConfig(category_patterns, ""));
}
bool BeginTracingWithTraceConfig(
const base::trace_event::TraceConfig& trace_config) {
return InProcessTraceController::GetInstance()->BeginTracing(trace_config);
}
bool EndTracing(std::string* json_trace_output) {
return InProcessTraceController::GetInstance()->EndTracing(json_trace_output);
}
} // namespace tracing