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/json/json_writer.h"
10 #include "base/logging.h"
11 #include "base/trace_event/trace_event.h"
12 #include "content/public/browser/tracing_controller.h"
13 #include "jni/TracingControllerAndroid_jni.h"
17 static jlong
Init(JNIEnv
* env
, const JavaParamRef
<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
);
39 base::android::ConvertJavaStringToUTF8(env
, jtraceoptions
);
41 // This log is required by adb_profile_chrome.py.
42 LOG(WARNING
) << "Logging performance trace to file";
44 return TracingController::GetInstance()->EnableRecording(
45 base::trace_event::TraceConfig(categories
, options
),
46 TracingController::EnableRecordingDoneCallback());
49 void TracingControllerAndroid::StopTracing(JNIEnv
* env
,
52 base::FilePath
file_path(
53 base::android::ConvertJavaStringToUTF8(env
, jfilepath
));
54 if (!TracingController::GetInstance()->DisableRecording(
55 TracingController::CreateFileSink(
57 base::Bind(&TracingControllerAndroid::OnTracingStopped
,
58 weak_factory_
.GetWeakPtr())))) {
59 LOG(ERROR
) << "EndTracingAsync failed, forcing an immediate stop";
64 void TracingControllerAndroid::GenerateTracingFilePath(
65 base::FilePath
* file_path
) {
66 JNIEnv
* env
= base::android::AttachCurrentThread();
67 ScopedJavaLocalRef
<jstring
> jfilename
=
68 Java_TracingControllerAndroid_generateTracingFilePath(env
);
69 *file_path
= base::FilePath(
70 base::android::ConvertJavaStringToUTF8(env
, jfilename
.obj()));
73 void TracingControllerAndroid::OnTracingStopped() {
74 JNIEnv
* env
= base::android::AttachCurrentThread();
75 base::android::ScopedJavaLocalRef
<jobject
> obj
= weak_java_object_
.get(env
);
77 Java_TracingControllerAndroid_onTracingStopped(env
, obj
.obj());
80 bool TracingControllerAndroid::GetKnownCategoryGroupsAsync(JNIEnv
* env
,
82 if (!TracingController::GetInstance()->GetCategories(
83 base::Bind(&TracingControllerAndroid::OnKnownCategoriesReceived
,
84 weak_factory_
.GetWeakPtr()))) {
90 void TracingControllerAndroid::OnKnownCategoriesReceived(
91 const std::set
<std::string
>& categories_received
) {
92 base::ListValue category_list
;
93 for (std::set
<std::string
>::const_iterator it
= categories_received
.begin();
94 it
!= categories_received
.end();
96 category_list
.AppendString(*it
);
98 std::string received_category_list
;
99 base::JSONWriter::Write(category_list
, &received_category_list
);
101 // This log is required by adb_profile_chrome.py.
102 LOG(WARNING
) << "{\"traceCategoriesList\": " << received_category_list
<< "}";
105 static ScopedJavaLocalRef
<jstring
> GetDefaultCategories(
107 const JavaParamRef
<jobject
>& obj
) {
108 base::trace_event::TraceConfig trace_config
;
109 return base::android::ConvertUTF8ToJavaString(
110 env
, trace_config
.ToCategoryFilterString());
113 bool RegisterTracingControllerAndroid(JNIEnv
* env
) {
114 return RegisterNativesImpl(env
);
117 } // namespace content