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"
|
2016-10-21 21:52:29 +02:00
|
|
|
#include "base/threading/thread_task_runner_handle.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(
|
2015-07-24 02:06:56 +02:00
|
|
|
scoped_refptr<base::SequencedTaskRunner> message_loop_proxy,
|
2014-09-27 01:48:19 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
// Release the wrapped callback object after completion.
|
|
|
|
class CefCompletionCallbackWrapper : public CefCompletionCallback {
|
|
|
|
public:
|
|
|
|
explicit CefCompletionCallbackWrapper(
|
|
|
|
CefRefPtr<CefCompletionCallback> callback)
|
|
|
|
: callback_(callback) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnComplete() override {
|
|
|
|
if (callback_) {
|
|
|
|
callback_->OnComplete();
|
|
|
|
callback_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefRefPtr<CefCompletionCallback> callback_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefCompletionCallbackWrapper);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefCompletionCallbackWrapper);
|
|
|
|
};
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
} // 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_)
|
2015-12-09 17:10:16 +01:00
|
|
|
TracingController::GetInstance()->StopTracing(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;
|
|
|
|
|
2015-12-09 17:10:16 +01:00
|
|
|
TracingController::StartTracingDoneCallback done_callback;
|
2014-09-27 01:48:19 +02:00
|
|
|
if (callback.get()) {
|
2016-01-06 20:20:54 +01:00
|
|
|
// Work around a bug introduced in http://crbug.com/542390#c22 that keeps a
|
|
|
|
// reference to |done_callback| after execution.
|
|
|
|
callback = new CefCompletionCallbackWrapper(callback);
|
2014-09-27 01:48:19 +02:00
|
|
|
done_callback =
|
|
|
|
base::Bind(&CefCompletionCallback::OnComplete, callback.get());
|
|
|
|
}
|
2014-02-05 21:35:45 +01:00
|
|
|
|
2015-12-09 17:10:16 +01:00
|
|
|
TracingController::GetInstance()->StartTracing(
|
2015-06-06 00:06:48 +02:00
|
|
|
base::trace_event::TraceConfig(categories, ""),
|
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;
|
2015-12-09 17:10:16 +01:00
|
|
|
TracingController::GetInstance()->StopTracing(NULL);
|
2014-12-02 17:42:34 +01:00
|
|
|
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,
|
2016-10-21 21:52:29 +02:00
|
|
|
base::ThreadTaskRunnerHandle::Get(),
|
2014-09-27 01:48:19 +02:00
|
|
|
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
|
|
|
|
2015-12-09 17:10:16 +01:00
|
|
|
TracingController::GetInstance()->StopTracing(
|
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
|
|
|
}
|