1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_TRACING_TRACING_CONSUMER_WIN_H_
6#define CONTENT_BROWSER_TRACING_TRACING_CONSUMER_WIN_H_
7
8#include "base/bind.h"
9#include "base/memory/ref_counted_memory.h"
10#include "base/threading/thread.h"
11#include "base/values.h"
12#include "base/win/event_trace_consumer.h"
13#include "base/win/event_trace_controller.h"
14
15template <typename Type>
16struct DefaultSingletonTraits;
17
18namespace content {
19
20class EtwSystemEventConsumer :
21    public base::win::EtwTraceConsumerBase<EtwSystemEventConsumer> {
22 public:
23  typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&)>
24      OutputCallback;
25
26  bool StartSystemTracing();
27  void StopSystemTracing(const OutputCallback& callback);
28
29  // Retrieve the ETW consumer instance.
30  static EtwSystemEventConsumer* GetInstance();
31
32 private:
33  // This allows constructor and destructor to be private and usable only
34  // by the Singleton class.
35  friend struct DefaultSingletonTraits<EtwSystemEventConsumer>;
36
37  // Constructor.
38  EtwSystemEventConsumer();
39  virtual ~EtwSystemEventConsumer();
40
41  void AddSyncEventToBuffer();
42  void AppendEventToBuffer(EVENT_TRACE* event);
43
44  // Static override of EtwTraceConsumerBase::ProcessEvent.
45  // @param event the raw ETW event to process.
46  friend class base::win::EtwTraceConsumerBase<EtwSystemEventConsumer>;
47  static void ProcessEvent(EVENT_TRACE* event);
48
49  // Request the ETW trace controller to activate the kernel tracing.
50  // returns true on success, false if the kernel tracing isn't activated.
51  bool StartKernelSessionTracing();
52
53  // Request the ETW trace controller to deactivate the kernel tracing.
54  // @param callback the callback to call with the consumed events.
55  // @returns true on success, false if an error occurred.
56  bool StopKernelSessionTracing();
57
58  void OnStopSystemTracingDone(
59    const OutputCallback& callback,
60    const scoped_refptr<base::RefCountedString>& result);
61
62  void TraceAndConsumeOnThread();
63  void FlushOnThread(const OutputCallback& callback);
64
65  scoped_ptr<base::ListValue> events_;
66  base::Thread thread_;
67  TRACEHANDLE session_handle_;
68  base::win::EtwTraceProperties properties_;
69
70  DISALLOW_COPY_AND_ASSIGN(EtwSystemEventConsumer);
71};
72
73}  // namespace content
74
75#endif  // CONTENT_BROWSER_TRACING_TRACING_CONSUMER_WIN_H_
76