atom_api_content_tracing.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include <set>
  5. #include <string>
  6. #include "atom/common/native_mate_converters/callback.h"
  7. #include "atom/common/native_mate_converters/file_path_converter.h"
  8. #include "base/bind.h"
  9. #include "base/files/file_util.h"
  10. #include "content/public/browser/tracing_controller.h"
  11. #include "native_mate/dictionary.h"
  12. #include "atom/common/node_includes.h"
  13. using content::TracingController;
  14. namespace mate {
  15. template <>
  16. struct Converter<base::trace_event::TraceConfig> {
  17. static bool FromV8(v8::Isolate* isolate,
  18. v8::Local<v8::Value> val,
  19. base::trace_event::TraceConfig* out) {
  20. Dictionary options;
  21. if (!ConvertFromV8(isolate, val, &options))
  22. return false;
  23. std::string category_filter, trace_options;
  24. if (!options.Get("categoryFilter", &category_filter) ||
  25. !options.Get("traceOptions", &trace_options))
  26. return false;
  27. *out = base::trace_event::TraceConfig(category_filter, trace_options);
  28. return true;
  29. }
  30. };
  31. } // namespace mate
  32. namespace {
  33. using CompletionCallback = base::Callback<void(const base::FilePath&)>;
  34. scoped_refptr<TracingController::TraceDataEndpoint> GetTraceDataEndpoint(
  35. const base::FilePath& path,
  36. const CompletionCallback& callback) {
  37. base::FilePath result_file_path = path;
  38. if (result_file_path.empty() && !base::CreateTemporaryFile(&result_file_path))
  39. LOG(ERROR) << "Creating temporary file failed";
  40. return TracingController::CreateFileEndpoint(
  41. result_file_path, base::Bind(callback, result_file_path));
  42. }
  43. void StopRecording(const base::FilePath& path,
  44. const CompletionCallback& callback) {
  45. TracingController::GetInstance()->StopTracing(
  46. GetTraceDataEndpoint(path, callback));
  47. }
  48. void Initialize(v8::Local<v8::Object> exports,
  49. v8::Local<v8::Value> unused,
  50. v8::Local<v8::Context> context,
  51. void* priv) {
  52. auto controller = base::Unretained(TracingController::GetInstance());
  53. mate::Dictionary dict(context->GetIsolate(), exports);
  54. dict.SetMethod("getCategories",
  55. base::Bind(&TracingController::GetCategories, controller));
  56. dict.SetMethod("startRecording",
  57. base::Bind(&TracingController::StartTracing, controller));
  58. dict.SetMethod("stopRecording", &StopRecording);
  59. dict.SetMethod(
  60. "getTraceBufferUsage",
  61. base::Bind(&TracingController::GetTraceBufferUsage, controller));
  62. }
  63. } // namespace
  64. NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_content_tracing, Initialize)