1 // Copyright (c) 2012 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 #include "content/common/android/trace_event_binding.h"
6
7 #include <jni.h>
8
9 #include <set>
10
11 #include "base/debug/trace_event.h"
12 #include "base/lazy_instance.h"
13 #include "jni/TraceEvent_jni.h"
14
15 namespace {
16
17 const char kJavaCategory[] = "Java";
18
19 // Boilerplate for safely converting Java data to TRACE_EVENT data.
20 class TraceEventDataConverter {
21 public:
TraceEventDataConverter(JNIEnv * env,jstring jname,jstring jarg)22 TraceEventDataConverter(JNIEnv* env,
23 jstring jname,
24 jstring jarg)
25 : env_(env),
26 jname_(jname),
27 jarg_(jarg),
28 name_(env->GetStringUTFChars(jname, NULL)),
29 arg_(jarg ? env->GetStringUTFChars(jarg, NULL) : NULL) {
30 }
~TraceEventDataConverter()31 ~TraceEventDataConverter() {
32 env_->ReleaseStringUTFChars(jname_, name_);
33 if (jarg_)
34 env_->ReleaseStringUTFChars(jarg_, arg_);
35 }
36
37 // Return saves values to pass to TRACE_EVENT macros.
name()38 const char* name() { return name_; }
arg_name()39 const char* arg_name() { return arg_ ? "arg" : NULL; }
arg()40 const char* arg() { return arg_; }
41
42 private:
43 JNIEnv* env_;
44 jstring jname_;
45 jstring jarg_;
46 const char* name_;
47 const char* arg_;
48
49 DISALLOW_COPY_AND_ASSIGN(TraceEventDataConverter);
50 };
51
52 } // namespace
53
TraceEnabled(JNIEnv * env,jclass clazz)54 static jboolean TraceEnabled(JNIEnv* env, jclass clazz) {
55 return base::debug::TraceLog::GetInstance()->IsEnabled();
56 }
57
StartATrace(JNIEnv * env,jclass clazz)58 static void StartATrace(JNIEnv* env, jclass clazz) {
59 base::debug::TraceLog::GetInstance()->StartATrace();
60 }
61
StopATrace(JNIEnv * env,jclass clazz)62 static void StopATrace(JNIEnv* env, jclass clazz) {
63 base::debug::TraceLog::GetInstance()->StopATrace();
64 }
65
Instant(JNIEnv * env,jclass clazz,jstring jname,jstring jarg)66 static void Instant(JNIEnv* env, jclass clazz,
67 jstring jname, jstring jarg) {
68 TraceEventDataConverter converter(env, jname, jarg);
69 if (converter.arg()) {
70 TRACE_EVENT_COPY_INSTANT1(kJavaCategory, converter.name(),
71 TRACE_EVENT_SCOPE_THREAD,
72 converter.arg_name(), converter.arg());
73 } else {
74 TRACE_EVENT_COPY_INSTANT0(kJavaCategory, converter.name(),
75 TRACE_EVENT_SCOPE_THREAD);
76 }
77 }
78
Begin(JNIEnv * env,jclass clazz,jstring jname,jstring jarg)79 static void Begin(JNIEnv* env, jclass clazz,
80 jstring jname, jstring jarg) {
81 TraceEventDataConverter converter(env, jname, jarg);
82 if (converter.arg()) {
83 TRACE_EVENT_COPY_BEGIN1(kJavaCategory, converter.name(),
84 converter.arg_name(), converter.arg());
85 } else {
86 TRACE_EVENT_COPY_BEGIN0(kJavaCategory, converter.name());
87 }
88 }
89
End(JNIEnv * env,jclass clazz,jstring jname,jstring jarg)90 static void End(JNIEnv* env, jclass clazz,
91 jstring jname, jstring jarg) {
92 TraceEventDataConverter converter(env, jname, jarg);
93 if (converter.arg()) {
94 TRACE_EVENT_COPY_END1(kJavaCategory, converter.name(),
95 converter.arg_name(), converter.arg());
96 } else {
97 TRACE_EVENT_COPY_END0(kJavaCategory, converter.name());
98 }
99 }
100
StartAsync(JNIEnv * env,jclass clazz,jstring jname,jlong jid,jstring jarg)101 static void StartAsync(JNIEnv* env, jclass clazz,
102 jstring jname, jlong jid, jstring jarg) {
103 TraceEventDataConverter converter(env, jname, jarg);
104 if (converter.arg()) {
105 TRACE_EVENT_COPY_ASYNC_BEGIN1(kJavaCategory,
106 converter.name(),
107 jid,
108 converter.arg_name(),
109 converter.arg());
110 } else {
111 TRACE_EVENT_COPY_ASYNC_BEGIN0(kJavaCategory,
112 converter.name(),
113 jid);
114 }
115 }
116
FinishAsync(JNIEnv * env,jclass clazz,jstring jname,jlong jid,jstring jarg)117 static void FinishAsync(JNIEnv* env, jclass clazz,
118 jstring jname, jlong jid, jstring jarg) {
119 TraceEventDataConverter converter(env, jname, jarg);
120 if (converter.arg()) {
121 TRACE_EVENT_COPY_ASYNC_END1(kJavaCategory,
122 converter.name(),
123 jid,
124 converter.arg_name(),
125 converter.arg());
126 } else {
127 TRACE_EVENT_COPY_ASYNC_END0(kJavaCategory,
128 converter.name(),
129 jid);
130 }
131 }
132
RegisterTraceEvent(JNIEnv * env)133 bool RegisterTraceEvent(JNIEnv* env) {
134 return RegisterNativesImpl(env);
135 }
136