Cleanup setting of 'sysroot' in common.gypi
[chromium-blink-merge.git] / base / android / trace_event_binding.cc
blobf761a646027d66e005f16891bcd4dc5cc5d07459
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/android/jni_string.h"
12 #include "base/lazy_instance.h"
13 #include "base/trace_event/trace_event.h"
14 #include "base/trace_event/trace_event_impl.h"
15 #include "jni/TraceEvent_jni.h"
17 namespace base {
18 namespace android {
20 namespace {
22 const char kJavaCategory[] = "Java";
23 const char kToplevelCategory[] = "toplevel";
24 const char kLooperDispatchMessage[] = "Looper.dispatchMessage";
26 // Boilerplate for safely converting Java data to TRACE_EVENT data.
27 class TraceEventDataConverter {
28 public:
29 TraceEventDataConverter(JNIEnv* env, jstring jname, jstring jarg)
30 : env_(env),
31 jname_(jname),
32 jarg_(jarg),
33 name_(ConvertJavaStringToUTF8(env, jname)),
34 has_arg_(jarg != nullptr),
35 arg_(jarg ? ConvertJavaStringToUTF8(env, jarg) : "") {}
36 ~TraceEventDataConverter() {
39 // Return saves values to pass to TRACE_EVENT macros.
40 const char* name() { return name_.c_str(); }
41 const char* arg_name() { return has_arg_ ? "arg" : nullptr; }
42 const char* arg() { return has_arg_ ? arg_.c_str() : nullptr; }
44 private:
45 JNIEnv* env_;
46 jstring jname_;
47 jstring jarg_;
48 std::string name_;
49 bool has_arg_;
50 std::string arg_;
52 DISALLOW_COPY_AND_ASSIGN(TraceEventDataConverter);
55 class TraceEnabledObserver
56 : public trace_event::TraceLog::EnabledStateObserver {
57 public:
58 void OnTraceLogEnabled() override {
59 JNIEnv* env = base::android::AttachCurrentThread();
60 base::android::Java_TraceEvent_setEnabled(env, true);
62 void OnTraceLogDisabled() override {
63 JNIEnv* env = base::android::AttachCurrentThread();
64 base::android::Java_TraceEvent_setEnabled(env, false);
68 base::LazyInstance<TraceEnabledObserver>::Leaky g_trace_enabled_state_observer_;
70 } // namespace
72 static void RegisterEnabledObserver(JNIEnv* env,
73 const JavaParamRef<jclass>& clazz) {
74 bool enabled = trace_event::TraceLog::GetInstance()->IsEnabled();
75 base::android::Java_TraceEvent_setEnabled(env, enabled);
76 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(
77 g_trace_enabled_state_observer_.Pointer());
80 static void StartATrace(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
81 base::trace_event::TraceLog::GetInstance()->StartATrace();
84 static void StopATrace(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
85 base::trace_event::TraceLog::GetInstance()->StopATrace();
88 static void Instant(JNIEnv* env,
89 const JavaParamRef<jclass>& clazz,
90 const JavaParamRef<jstring>& jname,
91 const JavaParamRef<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,
104 const JavaParamRef<jclass>& clazz,
105 const JavaParamRef<jstring>& jname,
106 const JavaParamRef<jstring>& jarg) {
107 TraceEventDataConverter converter(env, jname, jarg);
108 if (converter.arg()) {
109 TRACE_EVENT_COPY_BEGIN1(kJavaCategory, converter.name(),
110 converter.arg_name(), converter.arg());
111 } else {
112 TRACE_EVENT_COPY_BEGIN0(kJavaCategory, converter.name());
116 static void End(JNIEnv* env,
117 const JavaParamRef<jclass>& clazz,
118 const JavaParamRef<jstring>& jname,
119 const JavaParamRef<jstring>& jarg) {
120 TraceEventDataConverter converter(env, jname, jarg);
121 if (converter.arg()) {
122 TRACE_EVENT_COPY_END1(kJavaCategory, converter.name(),
123 converter.arg_name(), converter.arg());
124 } else {
125 TRACE_EVENT_COPY_END0(kJavaCategory, converter.name());
129 static void BeginToplevel(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
130 TRACE_EVENT_BEGIN0(kToplevelCategory, kLooperDispatchMessage);
133 static void EndToplevel(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
134 TRACE_EVENT_END0(kToplevelCategory, kLooperDispatchMessage);
137 static void StartAsync(JNIEnv* env,
138 const JavaParamRef<jclass>& clazz,
139 const JavaParamRef<jstring>& jname,
140 jlong jid) {
141 TraceEventDataConverter converter(env, jname, nullptr);
142 TRACE_EVENT_COPY_ASYNC_BEGIN0(kJavaCategory, converter.name(), jid);
145 static void FinishAsync(JNIEnv* env,
146 const JavaParamRef<jclass>& clazz,
147 const JavaParamRef<jstring>& jname,
148 jlong jid) {
149 TraceEventDataConverter converter(env, jname, nullptr);
150 TRACE_EVENT_COPY_ASYNC_END0(kJavaCategory, converter.name(), jid);
153 bool RegisterTraceEvent(JNIEnv* env) {
154 return RegisterNativesImpl(env);
157 } // namespace android
158 } // namespace base