Revert of Use Surfaces on Windows, Linux, and Mac OS (try #4) (patchset #6 id:100001...
[chromium-blink-merge.git] / base / android / trace_event_binding.cc
blob791b67fcf23e18b41a0927c817285c9e3a52afcd
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.
5 #include "base/android/trace_event_binding.h"
7 #include <jni.h>
9 #include <set>
11 #include "base/lazy_instance.h"
12 #include "base/trace_event/trace_event.h"
13 #include "base/trace_event/trace_event_impl.h"
14 #include "jni/TraceEvent_jni.h"
16 namespace base {
17 namespace android {
19 namespace {
21 const char kJavaCategory[] = "Java";
22 const char kToplevelCategory[] = "toplevel";
23 const char kLooperDispatchMessage[] = "Looper.dispatchMessage";
25 // Boilerplate for safely converting Java data to TRACE_EVENT data.
26 class TraceEventDataConverter {
27 public:
28 TraceEventDataConverter(JNIEnv* env,
29 jstring jname,
30 jstring jarg)
31 : env_(env),
32 jname_(jname),
33 jarg_(jarg),
34 name_(env->GetStringUTFChars(jname, NULL)),
35 arg_(jarg ? env->GetStringUTFChars(jarg, NULL) : NULL) {
37 ~TraceEventDataConverter() {
38 env_->ReleaseStringUTFChars(jname_, name_);
39 if (jarg_)
40 env_->ReleaseStringUTFChars(jarg_, arg_);
43 // Return saves values to pass to TRACE_EVENT macros.
44 const char* name() { return name_; }
45 const char* arg_name() { return arg_ ? "arg" : NULL; }
46 const char* arg() { return arg_; }
48 private:
49 JNIEnv* env_;
50 jstring jname_;
51 jstring jarg_;
52 const char* name_;
53 const char* arg_;
55 DISALLOW_COPY_AND_ASSIGN(TraceEventDataConverter);
58 class TraceEnabledObserver
59 : public trace_event::TraceLog::EnabledStateObserver {
60 public:
61 void OnTraceLogEnabled() override {
62 JNIEnv* env = base::android::AttachCurrentThread();
63 base::android::Java_TraceEvent_setEnabled(env, true);
65 void OnTraceLogDisabled() override {
66 JNIEnv* env = base::android::AttachCurrentThread();
67 base::android::Java_TraceEvent_setEnabled(env, false);
71 base::LazyInstance<TraceEnabledObserver>::Leaky g_trace_enabled_state_observer_;
73 } // namespace
75 static void RegisterEnabledObserver(JNIEnv* env, jclass clazz) {
76 bool enabled = trace_event::TraceLog::GetInstance()->IsEnabled();
77 base::android::Java_TraceEvent_setEnabled(env, enabled);
78 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(
79 g_trace_enabled_state_observer_.Pointer());
82 static void StartATrace(JNIEnv* env, jclass clazz) {
83 base::trace_event::TraceLog::GetInstance()->StartATrace();
86 static void StopATrace(JNIEnv* env, jclass clazz) {
87 base::trace_event::TraceLog::GetInstance()->StopATrace();
90 static void Instant(JNIEnv* env, jclass clazz,
91 jstring jname, jstring jarg) {
92 TraceEventDataConverter converter(env, jname, jarg);
93 if (converter.arg()) {
94 TRACE_EVENT_COPY_INSTANT1(kJavaCategory, converter.name(),
95 TRACE_EVENT_SCOPE_THREAD,
96 converter.arg_name(), converter.arg());
97 } else {
98 TRACE_EVENT_COPY_INSTANT0(kJavaCategory, converter.name(),
99 TRACE_EVENT_SCOPE_THREAD);
103 static void Begin(JNIEnv* env, jclass clazz,
104 jstring jname, jstring jarg) {
105 TraceEventDataConverter converter(env, jname, jarg);
106 if (converter.arg()) {
107 TRACE_EVENT_COPY_BEGIN1(kJavaCategory, converter.name(),
108 converter.arg_name(), converter.arg());
109 } else {
110 TRACE_EVENT_COPY_BEGIN0(kJavaCategory, converter.name());
114 static void End(JNIEnv* env, jclass clazz,
115 jstring jname, jstring jarg) {
116 TraceEventDataConverter converter(env, jname, jarg);
117 if (converter.arg()) {
118 TRACE_EVENT_COPY_END1(kJavaCategory, converter.name(),
119 converter.arg_name(), converter.arg());
120 } else {
121 TRACE_EVENT_COPY_END0(kJavaCategory, converter.name());
125 static void BeginToplevel(JNIEnv* env, jclass clazz) {
126 TRACE_EVENT_BEGIN0(kToplevelCategory, kLooperDispatchMessage);
129 static void EndToplevel(JNIEnv* env, jclass clazz) {
130 TRACE_EVENT_END0(kToplevelCategory, kLooperDispatchMessage);
133 static void StartAsync(JNIEnv* env, jclass clazz, jstring jname, jlong jid) {
134 TraceEventDataConverter converter(env, jname, nullptr);
135 TRACE_EVENT_COPY_ASYNC_BEGIN0(kJavaCategory, converter.name(), jid);
138 static void FinishAsync(JNIEnv* env, jclass clazz, jstring jname, jlong jid) {
139 TraceEventDataConverter converter(env, jname, nullptr);
140 TRACE_EVENT_COPY_ASYNC_END0(kJavaCategory, converter.name(), jid);
143 bool RegisterTraceEvent(JNIEnv* env) {
144 return RegisterNativesImpl(env);
147 } // namespace android
148 } // namespace base