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"
|
2018-12-20 12:11:17 +00:00
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
2019-03-12 00:13:43 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2019-01-30 20:39:55 +00:00
|
|
|
#include "atom/common/promise_util.h"
|
2014-07-31 07:08:54 +00:00
|
|
|
#include "base/bind.h"
|
2015-08-04 20:12:57 +00:00
|
|
|
#include "base/files/file_util.h"
|
2019-04-05 18:19:06 +00:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2014-07-31 07:08:54 +00:00
|
|
|
#include "content/public/browser/tracing_controller.h"
|
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
|
|
|
|
using content::TracingController;
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
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) {
|
2018-12-20 12:11:17 +00:00
|
|
|
// (alexeykuzmin): A combination of "categoryFilter" and "traceOptions"
|
|
|
|
// has to be checked first because none of the fields
|
|
|
|
// in the `memory_dump_config` dict below are mandatory
|
|
|
|
// and we cannot check the config format.
|
2015-09-02 07:16:49 +00:00
|
|
|
Dictionary options;
|
2018-12-20 12:11:17 +00:00
|
|
|
if (ConvertFromV8(isolate, val, &options)) {
|
|
|
|
std::string category_filter, trace_options;
|
|
|
|
if (options.Get("categoryFilter", &category_filter) &&
|
|
|
|
options.Get("traceOptions", &trace_options)) {
|
|
|
|
*out = base::trace_event::TraceConfig(category_filter, trace_options);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base::DictionaryValue memory_dump_config;
|
|
|
|
if (ConvertFromV8(isolate, val, &memory_dump_config)) {
|
|
|
|
*out = base::trace_event::TraceConfig(memory_dump_config);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-10-11 11:11:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-31 07:08:54 +00:00
|
|
|
} // namespace mate
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-04-28 01:03:06 +00:00
|
|
|
using CompletionCallback = base::RepeatingCallback<void(const base::FilePath&)>;
|
2015-08-04 20:12:57 +00:00
|
|
|
|
2017-12-18 00:08:08 +00:00
|
|
|
scoped_refptr<TracingController::TraceDataEndpoint> GetTraceDataEndpoint(
|
2018-04-18 01:55:30 +00:00
|
|
|
const base::FilePath& path,
|
|
|
|
const CompletionCallback& callback) {
|
2015-08-04 20:12:57 +00:00
|
|
|
base::FilePath result_file_path = path;
|
2019-04-05 18:19:06 +00:00
|
|
|
|
|
|
|
// base::CreateTemporaryFile prevents blocking so we need to allow it
|
|
|
|
// for now since offloading this to a different sequence would require
|
|
|
|
// changing the api shape
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
2015-08-04 20:12:57 +00:00
|
|
|
if (result_file_path.empty() && !base::CreateTemporaryFile(&result_file_path))
|
|
|
|
LOG(ERROR) << "Creating temporary file failed";
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
return TracingController::CreateFileEndpoint(
|
2019-04-28 01:03:06 +00:00
|
|
|
result_file_path, base::BindRepeating(callback, result_file_path));
|
2015-08-04 20:12:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 02:53:55 +00:00
|
|
|
v8::Local<v8::Promise> StopRecording(v8::Isolate* isolate,
|
|
|
|
const base::FilePath& path) {
|
2019-02-21 12:32:44 +00:00
|
|
|
atom::util::Promise promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
// TODO(zcbenz): Remove the use of CopyablePromise when the
|
|
|
|
// CreateFileEndpoint API accepts OnceCallback.
|
|
|
|
TracingController::GetInstance()->StopTracing(GetTraceDataEndpoint(
|
2019-04-28 01:03:06 +00:00
|
|
|
path,
|
|
|
|
base::BindRepeating(atom::util::CopyablePromise::ResolveCopyablePromise<
|
|
|
|
const base::FilePath&>,
|
|
|
|
atom::util::CopyablePromise(promise))));
|
2019-02-21 12:32:44 +00:00
|
|
|
return handle;
|
2015-08-04 20:12:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:39:55 +00:00
|
|
|
v8::Local<v8::Promise> GetCategories(v8::Isolate* isolate) {
|
2019-02-21 12:32:44 +00:00
|
|
|
atom::util::Promise promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
2019-01-30 20:39:55 +00:00
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
// Note: This method always succeeds.
|
2019-03-13 21:30:21 +00:00
|
|
|
TracingController::GetInstance()->GetCategories(base::BindOnce(
|
|
|
|
atom::util::Promise::ResolvePromise<const std::set<std::string>&>,
|
|
|
|
std::move(promise)));
|
2019-01-12 01:00:43 +00:00
|
|
|
|
2019-03-13 21:30:21 +00:00
|
|
|
return handle;
|
2019-01-31 02:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Promise> StartTracing(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const base::trace_event::TraceConfig& trace_config) {
|
2019-02-21 12:32:44 +00:00
|
|
|
atom::util::Promise promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
2019-01-31 02:53:55 +00:00
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
// Note: This method always succeeds.
|
|
|
|
TracingController::GetInstance()->StartTracing(
|
2019-03-13 21:30:21 +00:00
|
|
|
trace_config, base::BindOnce(atom::util::Promise::ResolveEmptyPromise,
|
|
|
|
std::move(promise)));
|
2019-02-21 12:32:44 +00:00
|
|
|
return handle;
|
2019-01-12 01:00:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
void OnTraceBufferUsageAvailable(atom::util::Promise promise,
|
2019-02-13 21:24:57 +00:00
|
|
|
float percent_full,
|
|
|
|
size_t approximate_count) {
|
2019-02-21 12:32:44 +00:00
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate());
|
2019-02-13 21:24:57 +00:00
|
|
|
dict.Set("percentage", percent_full);
|
|
|
|
dict.Set("value", approximate_count);
|
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
promise.Resolve(dict.GetHandle());
|
2019-02-13 21:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Promise> GetTraceBufferUsage(v8::Isolate* isolate) {
|
2019-02-21 12:32:44 +00:00
|
|
|
atom::util::Promise promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
2019-02-13 21:24:57 +00:00
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
// Note: This method always succeeds.
|
|
|
|
TracingController::GetInstance()->GetTraceBufferUsage(
|
|
|
|
base::BindOnce(&OnTraceBufferUsageAvailable, std::move(promise)));
|
|
|
|
return handle;
|
2019-01-12 01:00:43 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2014-07-31 07:08:54 +00:00
|
|
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
2019-01-12 01:00:43 +00:00
|
|
|
dict.SetMethod("getCategories", &GetCategories);
|
|
|
|
dict.SetMethod("startRecording", &StartTracing);
|
2015-08-04 20:12:57 +00:00
|
|
|
dict.SetMethod("stopRecording", &StopRecording);
|
2019-01-12 01:00:43 +00:00
|
|
|
dict.SetMethod("getTraceBufferUsage", &GetTraceBufferUsage);
|
2014-07-31 07:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-03-08 18:29:52 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_content_tracing, Initialize)
|