2013-12-17 23:04:35 +01:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
2012-10-18 00:45:49 +02:00
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/trace_subscriber.h"
|
|
|
|
#include "include/cef_trace.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
#include "base/files/file_util.h"
|
2015-03-04 02:00:13 +01:00
|
|
|
#include "base/trace_event/trace_event.h"
|
2013-12-17 23:04:35 +01:00
|
|
|
#include "content/public/browser/tracing_controller.h"
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Create the temporary file and then execute |callback| on the thread
|
|
|
|
// represented by |message_loop_proxy|.
|
|
|
|
void CreateTemporaryFileOnFileThread(
|
|
|
|
scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
|
|
|
|
base::Callback<void(const base::FilePath&)> callback) {
|
|
|
|
CEF_REQUIRE_FILET();
|
|
|
|
base::FilePath file_path;
|
|
|
|
if (!base::CreateTemporaryFile(&file_path))
|
|
|
|
LOG(ERROR) << "Failed to create temporary file.";
|
|
|
|
message_loop_proxy->PostTask(FROM_HERE, base::Bind(callback, file_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
using content::TracingController;
|
2012-10-18 00:45:49 +02:00
|
|
|
|
|
|
|
CefTraceSubscriber::CefTraceSubscriber()
|
2013-12-17 23:04:35 +01:00
|
|
|
: collecting_trace_data_(false),
|
|
|
|
weak_factory_(this) {
|
2012-10-18 00:45:49 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefTraceSubscriber::~CefTraceSubscriber() {
|
|
|
|
CEF_REQUIRE_UIT();
|
2014-09-27 01:48:19 +02:00
|
|
|
if (collecting_trace_data_)
|
|
|
|
TracingController::GetInstance()->DisableRecording(NULL);
|
2012-10-18 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2014-02-05 21:35:45 +01:00
|
|
|
bool CefTraceSubscriber::BeginTracing(
|
|
|
|
const std::string& categories,
|
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
2012-10-18 00:45:49 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
if (collecting_trace_data_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
collecting_trace_data_ = true;
|
|
|
|
|
2014-02-05 21:35:45 +01:00
|
|
|
TracingController::EnableRecordingDoneCallback done_callback;
|
2014-09-27 01:48:19 +02:00
|
|
|
if (callback.get()) {
|
|
|
|
done_callback =
|
|
|
|
base::Bind(&CefCompletionCallback::OnComplete, callback.get());
|
|
|
|
}
|
2014-02-05 21:35:45 +01:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
TracingController::GetInstance()->EnableRecording(
|
2015-03-04 02:00:13 +01:00
|
|
|
base::trace_event::CategoryFilter(categories),
|
|
|
|
base::trace_event::TraceOptions(),
|
2014-09-04 19:53:40 +02:00
|
|
|
done_callback);
|
2013-12-17 23:04:35 +01:00
|
|
|
return true;
|
2012-10-18 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2014-02-05 21:35:45 +01:00
|
|
|
bool CefTraceSubscriber::EndTracing(
|
2013-12-17 23:04:35 +01:00
|
|
|
const base::FilePath& tracing_file,
|
|
|
|
CefRefPtr<CefEndTracingCallback> callback) {
|
2012-10-18 00:45:49 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
if (!collecting_trace_data_)
|
|
|
|
return false;
|
|
|
|
|
2014-12-02 17:42:34 +01:00
|
|
|
if (!callback.get()) {
|
|
|
|
// Discard the trace data.
|
|
|
|
collecting_trace_data_ = false;
|
|
|
|
TracingController::GetInstance()->DisableRecording(NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
if (tracing_file.empty()) {
|
|
|
|
// Create a new temporary file path on the FILE thread, then continue.
|
|
|
|
CEF_POST_TASK(CEF_FILET,
|
|
|
|
base::Bind(CreateTemporaryFileOnFileThread,
|
|
|
|
base::MessageLoop::current()->message_loop_proxy(),
|
|
|
|
base::Bind(&CefTraceSubscriber::ContinueEndTracing,
|
|
|
|
weak_factory_.GetWeakPtr(), callback)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-02 17:42:34 +01:00
|
|
|
base::Closure result_callback =
|
|
|
|
base::Bind(&CefTraceSubscriber::OnTracingFileResult,
|
|
|
|
weak_factory_.GetWeakPtr(), callback, tracing_file);
|
2012-10-18 00:45:49 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
TracingController::GetInstance()->DisableRecording(
|
2014-09-27 01:48:19 +02:00
|
|
|
TracingController::CreateFileSink(tracing_file, result_callback));
|
2013-12-17 23:04:35 +01:00
|
|
|
return true;
|
2012-10-18 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
void CefTraceSubscriber::ContinueEndTracing(
|
|
|
|
CefRefPtr<CefEndTracingCallback> callback,
|
|
|
|
const base::FilePath& tracing_file) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (!tracing_file.empty())
|
|
|
|
EndTracing(tracing_file, callback);
|
|
|
|
}
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
void CefTraceSubscriber::OnTracingFileResult(
|
|
|
|
CefRefPtr<CefEndTracingCallback> callback,
|
|
|
|
const base::FilePath& tracing_file) {
|
2012-10-18 00:45:49 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2013-12-17 23:04:35 +01:00
|
|
|
|
2012-10-18 00:45:49 +02:00
|
|
|
collecting_trace_data_ = false;
|
2013-04-16 00:16:01 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
callback->OnEndTracingComplete(tracing_file.value());
|
2013-04-16 00:16:01 +02:00
|
|
|
}
|