Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / android / tracing_controller_android.cc
blob6953a0de5f85015cb0334cb5d0287a3ba9031130
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"
15 namespace content {
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) {
29 delete this;
32 bool TracingControllerAndroid::StartTracing(JNIEnv* env,
33 jobject obj,
34 jstring jcategories,
35 jstring jtraceoptions) {
36 std::string categories =
37 base::android::ConvertJavaStringToUTF8(env, jcategories);
38 base::trace_event::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::trace_event::CategoryFilter(categories),
47 trace_options,
48 TracingController::EnableRecordingDoneCallback());
51 void TracingControllerAndroid::StopTracing(JNIEnv* env,
52 jobject obj,
53 jstring jfilepath) {
54 base::FilePath file_path(
55 base::android::ConvertJavaStringToUTF8(env, jfilepath));
56 if (!TracingController::GetInstance()->DisableRecording(
57 TracingController::CreateFileSink(
58 file_path,
59 base::Bind(&TracingControllerAndroid::OnTracingStopped,
60 weak_factory_.GetWeakPtr())))) {
61 LOG(ERROR) << "EndTracingAsync failed, forcing an immediate stop";
62 OnTracingStopped();
66 void TracingControllerAndroid::GenerateTracingFilePath(
67 base::FilePath* file_path) {
68 JNIEnv* env = base::android::AttachCurrentThread();
69 ScopedJavaLocalRef<jstring> jfilename =
70 Java_TracingControllerAndroid_generateTracingFilePath(env);
71 *file_path = base::FilePath(
72 base::android::ConvertJavaStringToUTF8(env, jfilename.obj()));
75 void TracingControllerAndroid::OnTracingStopped() {
76 JNIEnv* env = base::android::AttachCurrentThread();
77 base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env);
78 if (obj.obj())
79 Java_TracingControllerAndroid_onTracingStopped(env, obj.obj());
82 bool TracingControllerAndroid::GetKnownCategoryGroupsAsync(JNIEnv* env,
83 jobject obj) {
84 if (!TracingController::GetInstance()->GetCategories(
85 base::Bind(&TracingControllerAndroid::OnKnownCategoriesReceived,
86 weak_factory_.GetWeakPtr()))) {
87 return false;
89 return true;
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();
97 ++it) {
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(
109 env,
110 base::trace_event::CategoryFilter::kDefaultCategoryFilterString)
111 .Release();
114 bool RegisterTracingControllerAndroid(JNIEnv* env) {
115 return RegisterNativesImpl(env);
118 } // namespace content