OLD | NEW |
(Empty) | |
| 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/browser/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/trace_event_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: |
| 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 } |
| 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. |
| 38 const char* name() { return name_; } |
| 39 const char* arg_name() { return arg_ ? "arg" : NULL; } |
| 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 |
| 54 static jboolean TraceEnabled(JNIEnv* env, jclass clazz) { |
| 55 return base::debug::TraceLog::GetInstance()->IsEnabled(); |
| 56 } |
| 57 |
| 58 static void Instant(JNIEnv* env, jclass clazz, |
| 59 jstring jname, jstring jarg) { |
| 60 TraceEventDataConverter converter(env, jname, jarg); |
| 61 if (converter.arg()) { |
| 62 TRACE_EVENT_COPY_INSTANT1(kJavaCategory, converter.name(), |
| 63 converter.arg_name(), converter.arg()); |
| 64 } else { |
| 65 TRACE_EVENT_COPY_INSTANT0(kJavaCategory, converter.name()); |
| 66 } |
| 67 } |
| 68 |
| 69 static void Begin(JNIEnv* env, jclass clazz, |
| 70 jstring jname, jstring jarg) { |
| 71 TraceEventDataConverter converter(env, jname, jarg); |
| 72 if (converter.arg()) { |
| 73 TRACE_EVENT_COPY_BEGIN1(kJavaCategory, converter.name(), |
| 74 converter.arg_name(), converter.arg()); |
| 75 } else { |
| 76 TRACE_EVENT_COPY_BEGIN0(kJavaCategory, converter.name()); |
| 77 } |
| 78 } |
| 79 |
| 80 static void End(JNIEnv* env, jclass clazz, |
| 81 jstring jname, jstring jarg) { |
| 82 TraceEventDataConverter converter(env, jname, jarg); |
| 83 if (converter.arg()) { |
| 84 TRACE_EVENT_COPY_END1(kJavaCategory, converter.name(), |
| 85 converter.arg_name(), converter.arg()); |
| 86 } else { |
| 87 TRACE_EVENT_COPY_END0(kJavaCategory, converter.name()); |
| 88 } |
| 89 } |
| 90 |
| 91 bool RegisterTraceEvent(JNIEnv* env) { |
| 92 return RegisterNativesImpl(env); |
| 93 } |
OLD | NEW |