Home | History | Annotate | Download | only in android
      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.
      4 
      5 #include "content/browser/android/tracing_controller_android.h"
      6 
      7 #include "base/android/jni_android.h"
      8 #include "base/android/jni_string.h"
      9 #include "base/debug/trace_event.h"
     10 #include "base/logging.h"
     11 #include "content/public/browser/tracing_controller.h"
     12 #include "jni/TracingControllerAndroid_jni.h"
     13 
     14 namespace content {
     15 
     16 static jlong Init(JNIEnv* env, jobject obj) {
     17   TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj);
     18   return reinterpret_cast<intptr_t>(profiler);
     19 }
     20 
     21 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj)
     22     : weak_java_object_(env, obj),
     23       weak_factory_(this) {}
     24 
     25 TracingControllerAndroid::~TracingControllerAndroid() {}
     26 
     27 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) {
     28   delete this;
     29 }
     30 
     31 bool TracingControllerAndroid::StartTracing(JNIEnv* env,
     32                                             jobject obj,
     33                                             jstring jfilename,
     34                                             jstring jcategories,
     35                                             jboolean record_continuously) {
     36   file_path_ = base::FilePath(
     37       base::android::ConvertJavaStringToUTF8(env, jfilename));
     38   std::string categories =
     39       base::android::ConvertJavaStringToUTF8(env, jcategories);
     40 
     41   // This log is required by adb_profile_chrome.py.
     42   LOG(WARNING) << "Logging performance trace to file: " << file_path_.value();
     43 
     44   return TracingController::GetInstance()->EnableRecording(
     45       categories,
     46       record_continuously ? TracingController::RECORD_CONTINUOUSLY
     47                           : TracingController::DEFAULT_OPTIONS,
     48       TracingController::EnableRecordingDoneCallback());
     49 }
     50 
     51 void TracingControllerAndroid::StopTracing(JNIEnv* env, jobject obj) {
     52   if (!TracingController::GetInstance()->DisableRecording(
     53       file_path_,
     54       base::Bind(&TracingControllerAndroid::OnTracingStopped,
     55                  weak_factory_.GetWeakPtr()))) {
     56     LOG(ERROR) << "EndTracingAsync failed, forcing an immediate stop";
     57     OnTracingStopped(file_path_);
     58   }
     59 }
     60 
     61 void TracingControllerAndroid::OnTracingStopped(
     62     const base::FilePath& file_path) {
     63   JNIEnv* env = base::android::AttachCurrentThread();
     64   base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env);
     65   if (obj.obj())
     66     Java_TracingControllerAndroid_onTracingStopped(env, obj.obj());
     67 }
     68 
     69 static jstring GetDefaultCategories(JNIEnv* env, jobject obj) {
     70   return base::android::ConvertUTF8ToJavaString(env,
     71       base::debug::CategoryFilter::kDefaultCategoryFilterString).Release();
     72 }
     73 
     74 bool RegisterTracingControllerAndroid(JNIEnv* env) {
     75   return RegisterNativesImpl(env);
     76 }
     77 
     78 }  // namespace content
     79