2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-07-31 07:08:54 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <set>
|
2014-10-11 11:11:34 +00:00
|
|
|
#include <string>
|
2014-07-31 07:08:54 +00:00
|
|
|
|
2015-08-07 10:10:19 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2014-07-31 07:08:54 +00:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
|
|
|
#include "base/bind.h"
|
2015-08-04 20:12:57 +00:00
|
|
|
#include "base/files/file_util.h"
|
2014-07-31 07:08:54 +00:00
|
|
|
#include "content/public/browser/tracing_controller.h"
|
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
|
2016-09-06 08:24:37 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
2014-07-31 07:08:54 +00:00
|
|
|
using content::TracingController;
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
template<>
|
2015-09-02 07:16:49 +00:00
|
|
|
struct Converter<base::trace_event::TraceConfig> {
|
2014-07-31 07:08:54 +00:00
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Value> val,
|
2015-09-02 07:16:49 +00:00
|
|
|
base::trace_event::TraceConfig* out) {
|
|
|
|
Dictionary options;
|
2014-10-11 11:11:34 +00:00
|
|
|
if (!ConvertFromV8(isolate, val, &options))
|
|
|
|
return false;
|
2015-09-02 07:16:49 +00:00
|
|
|
std::string category_filter, trace_options;
|
|
|
|
if (!options.Get("categoryFilter", &category_filter) ||
|
|
|
|
!options.Get("traceOptions", &trace_options))
|
|
|
|
return false;
|
|
|
|
*out = base::trace_event::TraceConfig(category_filter, trace_options);
|
|
|
|
return true;
|
2014-10-11 11:11:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-31 07:08:54 +00:00
|
|
|
} // namespace mate
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-08-04 20:12:57 +00:00
|
|
|
using CompletionCallback = base::Callback<void(const base::FilePath&)>;
|
|
|
|
|
2017-12-18 00:08:08 +00:00
|
|
|
scoped_refptr<TracingController::TraceDataEndpoint> GetTraceDataEndpoint(
|
2015-08-04 20:12:57 +00:00
|
|
|
const base::FilePath& path, const CompletionCallback& callback) {
|
|
|
|
base::FilePath result_file_path = path;
|
|
|
|
if (result_file_path.empty() && !base::CreateTemporaryFile(&result_file_path))
|
|
|
|
LOG(ERROR) << "Creating temporary file failed";
|
|
|
|
|
2017-12-18 00:08:08 +00:00
|
|
|
return TracingController::CreateFileEndpoint(result_file_path,
|
2015-08-04 20:12:57 +00:00
|
|
|
base::Bind(callback,
|
|
|
|
result_file_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopRecording(const base::FilePath& path,
|
|
|
|
const CompletionCallback& callback) {
|
2016-03-08 14:28:53 +00:00
|
|
|
TracingController::GetInstance()->StopTracing(
|
2017-12-18 00:08:08 +00:00
|
|
|
GetTraceDataEndpoint(path, callback));
|
2015-08-04 20:12:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2015-03-10 23:02:22 +00:00
|
|
|
auto controller = base::Unretained(TracingController::GetInstance());
|
2014-07-31 07:08:54 +00:00
|
|
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
|
|
|
dict.SetMethod("getCategories", base::Bind(
|
2015-03-10 23:02:22 +00:00
|
|
|
&TracingController::GetCategories, controller));
|
2014-07-31 07:08:54 +00:00
|
|
|
dict.SetMethod("startRecording", base::Bind(
|
2016-03-08 14:28:53 +00:00
|
|
|
&TracingController::StartTracing, controller));
|
2015-08-04 20:12:57 +00:00
|
|
|
dict.SetMethod("stopRecording", &StopRecording);
|
2015-03-10 23:02:22 +00:00
|
|
|
dict.SetMethod("getTraceBufferUsage", base::Bind(
|
|
|
|
&TracingController::GetTraceBufferUsage, controller));
|
2014-07-31 07:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_content_tracing, Initialize)
|