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.
|
|
|
|
|
2017-05-19 11:06:00 +02:00
|
|
|
#include "libcef/browser/trace_subscriber.h"
|
2012-10-18 00:45:49 +02:00
|
|
|
#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
|
2017-05-17 11:29:28 +02:00
|
|
|
// represented by |message_loop_proxy|.
|
2018-03-20 21:15:08 +01:00
|
|
|
void CreateTemporaryFileOnBackgroundThread(
|
2015-07-24 02:06:56 +02:00
|
|
|
scoped_refptr<base::SequencedTaskRunner> message_loop_proxy,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::OnceCallback<void(const base::FilePath&)> callback) {
|
2018-03-20 21:15:08 +01:00
|
|
|
CEF_REQUIRE_BLOCKING();
|
2014-09-27 01:48:19 +02:00
|
|
|
base::FilePath file_path;
|
|
|
|
if (!base::CreateTemporaryFile(&file_path))
|
|
|
|
LOG(ERROR) << "Failed to create temporary file.";
|
2021-06-04 03:34:56 +02:00
|
|
|
message_loop_proxy->PostTask(FROM_HERE,
|
|
|
|
base::BindOnce(std::move(callback), file_path));
|
2014-09-27 01:48:19 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2017-05-17 11:29:28 +02:00
|
|
|
: callback_(callback) {}
|
2016-01-06 20:20:54 +01:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefCompletionCallbackWrapper(const CefCompletionCallbackWrapper&) = delete;
|
|
|
|
CefCompletionCallbackWrapper& operator=(const CefCompletionCallbackWrapper&) =
|
|
|
|
delete;
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
void OnComplete() override {
|
|
|
|
if (callback_) {
|
|
|
|
callback_->OnComplete();
|
|
|
|
callback_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefRefPtr<CefCompletionCallback> callback_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(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()
|
2017-05-17 11:29:28 +02: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_)
|
2020-01-15 14:36:24 +01:00
|
|
|
TracingController::GetInstance()->StopTracing(nullptr);
|
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 =
|
2018-10-02 14:14:11 +02:00
|
|
|
base::BindOnce(&CefCompletionCallback::OnComplete, callback.get());
|
2014-09-27 01:48:19 +02:00
|
|
|
}
|
2014-02-05 21:35:45 +01:00
|
|
|
|
2015-12-09 17:10:16 +01:00
|
|
|
TracingController::GetInstance()->StartTracing(
|
2018-10-02 14:14:11 +02:00
|
|
|
base::trace_event::TraceConfig(categories, ""), std::move(done_callback));
|
2013-12-17 23:04:35 +01:00
|
|
|
return true;
|
2012-10-18 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool CefTraceSubscriber::EndTracing(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;
|
2020-01-15 14:36:24 +01:00
|
|
|
TracingController::GetInstance()->StopTracing(nullptr);
|
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.
|
2018-03-20 21:15:08 +01:00
|
|
|
CEF_POST_USER_VISIBLE_TASK(
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(CreateTemporaryFileOnBackgroundThread,
|
|
|
|
base::ThreadTaskRunnerHandle::Get(),
|
|
|
|
base::BindOnce(&CefTraceSubscriber::ContinueEndTracing,
|
|
|
|
weak_factory_.GetWeakPtr(), callback)));
|
2014-09-27 01:48:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-04 03:34:56 +02:00
|
|
|
auto result_callback =
|
|
|
|
base::BindOnce(&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(
|
2021-06-04 03:34:56 +02:00
|
|
|
TracingController::CreateFileEndpoint(tracing_file,
|
2022-03-23 22:30:56 +01:00
|
|
|
std::move(result_callback),
|
|
|
|
base::TaskPriority::USER_VISIBLE));
|
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
|
|
|
}
|