forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace_event_binding.cc
143 lines (119 loc) · 4.91 KB
/
trace_event_binding.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <jni.h>
#include <set>
#include "base/android/jni_string.h"
#include "base/macros.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_event_impl.h"
#include "jni/TraceEvent_jni.h"
namespace base {
namespace android {
namespace {
constexpr const char kJavaCategory[] = "Java";
constexpr const char kToplevelCategory[] = "toplevel";
constexpr const char kLooperDispatchMessage[] = "Looper.dispatchMessage";
// Boilerplate for safely converting Java data to TRACE_EVENT data.
class TraceEventDataConverter {
public:
TraceEventDataConverter(JNIEnv* env, jstring jname, jstring jarg)
: name_(ConvertJavaStringToUTF8(env, jname)),
has_arg_(jarg != nullptr),
arg_(jarg ? ConvertJavaStringToUTF8(env, jarg) : "") {}
~TraceEventDataConverter() = default;
// Return saves values to pass to TRACE_EVENT macros.
const char* name() { return name_.c_str(); }
const char* arg_name() { return has_arg_ ? "arg" : nullptr; }
const char* arg() { return has_arg_ ? arg_.c_str() : nullptr; }
private:
std::string name_;
bool has_arg_;
std::string arg_;
DISALLOW_COPY_AND_ASSIGN(TraceEventDataConverter);
};
class TraceEnabledObserver
: public trace_event::TraceLog::EnabledStateObserver {
public:
~TraceEnabledObserver() override = default;
// trace_event::TraceLog::EnabledStateObserver:
void OnTraceLogEnabled() override {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::Java_TraceEvent_setEnabled(env, true);
}
void OnTraceLogDisabled() override {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::Java_TraceEvent_setEnabled(env, false);
}
};
} // namespace
static void JNI_TraceEvent_RegisterEnabledObserver(JNIEnv* env) {
bool enabled = trace_event::TraceLog::GetInstance()->IsEnabled();
base::android::Java_TraceEvent_setEnabled(env, enabled);
trace_event::TraceLog::GetInstance()->AddOwnedEnabledStateObserver(
std::make_unique<TraceEnabledObserver>());
}
static void JNI_TraceEvent_StartATrace(JNIEnv* env) {
base::trace_event::TraceLog::GetInstance()->StartATrace();
}
static void JNI_TraceEvent_StopATrace(JNIEnv* env) {
base::trace_event::TraceLog::GetInstance()->StopATrace();
}
static void JNI_TraceEvent_Instant(JNIEnv* env,
const JavaParamRef<jstring>& jname,
const JavaParamRef<jstring>& jarg) {
TraceEventDataConverter converter(env, jname, jarg);
if (converter.arg()) {
TRACE_EVENT_COPY_INSTANT1(kJavaCategory, converter.name(),
TRACE_EVENT_SCOPE_THREAD,
converter.arg_name(), converter.arg());
} else {
TRACE_EVENT_COPY_INSTANT0(kJavaCategory, converter.name(),
TRACE_EVENT_SCOPE_THREAD);
}
}
static void JNI_TraceEvent_Begin(JNIEnv* env,
const JavaParamRef<jstring>& jname,
const JavaParamRef<jstring>& jarg) {
TraceEventDataConverter converter(env, jname, jarg);
if (converter.arg()) {
TRACE_EVENT_COPY_BEGIN1(kJavaCategory, converter.name(),
converter.arg_name(), converter.arg());
} else {
TRACE_EVENT_COPY_BEGIN0(kJavaCategory, converter.name());
}
}
static void JNI_TraceEvent_End(JNIEnv* env,
const JavaParamRef<jstring>& jname,
const JavaParamRef<jstring>& jarg) {
TraceEventDataConverter converter(env, jname, jarg);
if (converter.arg()) {
TRACE_EVENT_COPY_END1(kJavaCategory, converter.name(),
converter.arg_name(), converter.arg());
} else {
TRACE_EVENT_COPY_END0(kJavaCategory, converter.name());
}
}
static void JNI_TraceEvent_BeginToplevel(JNIEnv* env,
const JavaParamRef<jstring>& jtarget) {
std::string target = ConvertJavaStringToUTF8(env, jtarget);
TRACE_EVENT_BEGIN1(kToplevelCategory, kLooperDispatchMessage, "target",
target);
}
static void JNI_TraceEvent_EndToplevel(JNIEnv* env) {
TRACE_EVENT_END0(kToplevelCategory, kLooperDispatchMessage);
}
static void JNI_TraceEvent_StartAsync(JNIEnv* env,
const JavaParamRef<jstring>& jname,
jlong jid) {
TraceEventDataConverter converter(env, jname, nullptr);
TRACE_EVENT_COPY_ASYNC_BEGIN0(kJavaCategory, converter.name(), jid);
}
static void JNI_TraceEvent_FinishAsync(JNIEnv* env,
const JavaParamRef<jstring>& jname,
jlong jid) {
TraceEventDataConverter converter(env, jname, nullptr);
TRACE_EVENT_COPY_ASYNC_END0(kJavaCategory, converter.name(), jid);
}
} // namespace android
} // namespace base