SupervisedUser SafeSites: Switch to the new SafeSearch API
[chromium-blink-merge.git] / base / android / trace_event_binding.cc
blob3c5ee17c7abf0c18de1ab17997bd5654077bbb00
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, jclass clazz) {
73 bool enabled = trace_event::TraceLog::GetInstance()->IsEnabled();
74 base::android::Java_TraceEvent_setEnabled(env, enabled);
75 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(
76 g_trace_enabled_state_observer_.Pointer());
79 static void StartATrace(JNIEnv* env, jclass clazz) {
80 base::trace_event::TraceLog::GetInstance()->StartATrace();
83 static void StopATrace(JNIEnv* env, jclass clazz) {
84 base::trace_event::TraceLog::GetInstance()->StopATrace();
87 static void Instant(JNIEnv* env, jclass clazz,
88 jstring jname, jstring jarg) {
89 TraceEventDataConverter converter(env, jname, jarg);
90 if (converter.arg()) {
91 TRACE_EVENT_COPY_INSTANT1(kJavaCategory, converter.name(),
92 TRACE_EVENT_SCOPE_THREAD,
93 converter.arg_name(), converter.arg());
94 } else {
95 TRACE_EVENT_COPY_INSTANT0(kJavaCategory, converter.name(),
96 TRACE_EVENT_SCOPE_THREAD);
100 static void Begin(JNIEnv* env, jclass clazz,
101 jstring jname, jstring jarg) {
102 TraceEventDataConverter converter(env, jname, jarg);
103 if (converter.arg()) {
104 TRACE_EVENT_COPY_BEGIN1(kJavaCategory, converter.name(),
105 converter.arg_name(), converter.arg());
106 } else {
107 TRACE_EVENT_COPY_BEGIN0(kJavaCategory, converter.name());
111 static void End(JNIEnv* env, jclass clazz,
112 jstring jname, jstring jarg) {
113 TraceEventDataConverter converter(env, jname, jarg);
114 if (converter.arg()) {
115 TRACE_EVENT_COPY_END1(kJavaCategory, converter.name(),
116 converter.arg_name(), converter.arg());
117 } else {
118 TRACE_EVENT_COPY_END0(kJavaCategory, converter.name());
122 static void BeginToplevel(JNIEnv* env, jclass clazz) {
123 TRACE_EVENT_BEGIN0(kToplevelCategory, kLooperDispatchMessage);
126 static void EndToplevel(JNIEnv* env, jclass clazz) {
127 TRACE_EVENT_END0(kToplevelCategory, kLooperDispatchMessage);
130 static void StartAsync(JNIEnv* env, jclass clazz, jstring jname, jlong jid) {
131 TraceEventDataConverter converter(env, jname, nullptr);
132 TRACE_EVENT_COPY_ASYNC_BEGIN0(kJavaCategory, converter.name(), jid);
135 static void FinishAsync(JNIEnv* env, jclass clazz, jstring jname, jlong jid) {
136 TraceEventDataConverter converter(env, jname, nullptr);
137 TRACE_EVENT_COPY_ASYNC_END0(kJavaCategory, converter.name(), jid);
140 bool RegisterTraceEvent(JNIEnv* env) {
141 return RegisterNativesImpl(env);
144 } // namespace android
145 } // namespace base