1 // Copyright 2013 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 "content/browser/android/tracing_controller_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/debug/trace_event.h"
10 #include "base/json/json_writer.h"
11 #include "base/logging.h"
12 #include "content/public/browser/tracing_controller.h"
13 #include "jni/TracingControllerAndroid_jni.h"
17 static jlong
Init(JNIEnv
* env
, jobject obj
) {
18 TracingControllerAndroid
* profiler
= new TracingControllerAndroid(env
, obj
);
19 return reinterpret_cast<intptr_t>(profiler
);
22 TracingControllerAndroid::TracingControllerAndroid(JNIEnv
* env
, jobject obj
)
23 : weak_java_object_(env
, obj
),
24 weak_factory_(this) {}
26 TracingControllerAndroid::~TracingControllerAndroid() {}
28 void TracingControllerAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
32 bool TracingControllerAndroid::StartTracing(JNIEnv
* env
,
35 jstring jtraceoptions
) {
36 std::string categories
=
37 base::android::ConvertJavaStringToUTF8(env
, jcategories
);
38 base::debug::TraceOptions trace_options
;
39 trace_options
.SetFromString(
40 base::android::ConvertJavaStringToUTF8(env
, jtraceoptions
));
42 // This log is required by adb_profile_chrome.py.
43 LOG(WARNING
) << "Logging performance trace to file";
45 return TracingController::GetInstance()->EnableRecording(
46 base::debug::CategoryFilter(categories
),
48 TracingController::EnableRecordingDoneCallback());
51 void TracingControllerAndroid::StopTracing(JNIEnv
* env
,
54 base::FilePath
file_path(
55 base::android::ConvertJavaStringToUTF8(env
, jfilepath
));
56 if (!TracingController::GetInstance()->DisableRecording(
58 base::Bind(&TracingControllerAndroid::OnTracingStopped
,
59 weak_factory_
.GetWeakPtr()))) {
60 LOG(ERROR
) << "EndTracingAsync failed, forcing an immediate stop";
61 OnTracingStopped(file_path
);
65 void TracingControllerAndroid::GenerateTracingFilePath(
66 base::FilePath
* file_path
) {
67 JNIEnv
* env
= base::android::AttachCurrentThread();
68 ScopedJavaLocalRef
<jstring
> jfilename
=
69 Java_TracingControllerAndroid_generateTracingFilePath(env
);
70 *file_path
= base::FilePath(
71 base::android::ConvertJavaStringToUTF8(env
, jfilename
.obj()));
74 void TracingControllerAndroid::OnTracingStopped(
75 const base::FilePath
& file_path
) {
76 JNIEnv
* env
= base::android::AttachCurrentThread();
77 base::android::ScopedJavaLocalRef
<jobject
> obj
= weak_java_object_
.get(env
);
79 Java_TracingControllerAndroid_onTracingStopped(env
, obj
.obj());
82 bool TracingControllerAndroid::GetKnownCategoryGroupsAsync(JNIEnv
* env
,
84 if (!TracingController::GetInstance()->GetCategories(
85 base::Bind(&TracingControllerAndroid::OnKnownCategoriesReceived
,
86 weak_factory_
.GetWeakPtr()))) {
92 void TracingControllerAndroid::OnKnownCategoriesReceived(
93 const std::set
<std::string
>& categories_received
) {
94 scoped_ptr
<base::ListValue
> category_list(new base::ListValue());
95 for (std::set
<std::string
>::const_iterator it
= categories_received
.begin();
96 it
!= categories_received
.end();
98 category_list
->AppendString(*it
);
100 std::string received_category_list
;
101 base::JSONWriter::Write(category_list
.get(), &received_category_list
);
103 // This log is required by adb_profile_chrome.py.
104 LOG(WARNING
) << "{\"traceCategoriesList\": " << received_category_list
<< "}";
107 static jstring
GetDefaultCategories(JNIEnv
* env
, jobject obj
) {
108 return base::android::ConvertUTF8ToJavaString(env
,
109 base::debug::CategoryFilter::kDefaultCategoryFilterString
).Release();
112 bool RegisterTracingControllerAndroid(JNIEnv
* env
) {
113 return RegisterNativesImpl(env
);
116 } // namespace content