- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / android / tracing_controller_android.cc
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/command_line.h"
10 #include "base/debug/trace_event.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "content/browser/tracing/trace_subscriber_stdio.h"
14 #include "content/public/browser/trace_controller.h"
15 #include "jni/TracingControllerAndroid_jni.h"
16
17 namespace content {
18
19 static jint Init(JNIEnv* env, jobject obj) {
20   TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj);
21   return reinterpret_cast<jint>(profiler);
22 }
23
24 class TracingControllerAndroid::Subscriber
25     : public content::TraceSubscriberStdio {
26  public:
27   Subscriber(TracingControllerAndroid* profiler, const base::FilePath& path)
28       : TraceSubscriberStdio(path, FILE_TYPE_ARRAY, false),
29         profiler_(profiler) {}
30
31   void set_profiler(TracingControllerAndroid* profiler) {
32     CHECK(!profiler_);
33     profiler_ = profiler;
34   }
35
36   virtual void OnEndTracingComplete() OVERRIDE {
37     TraceSubscriberStdio::OnEndTracingComplete();
38     profiler_->OnTracingStopped();
39   }
40
41  private:
42   TracingControllerAndroid* profiler_;
43 };
44
45 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj)
46     : weak_java_object_(env, obj) {}
47
48 TracingControllerAndroid::~TracingControllerAndroid() {}
49
50 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) {
51   delete this;
52 }
53
54 bool TracingControllerAndroid::StartTracing(JNIEnv* env,
55                                             jobject obj,
56                                             jstring jfilename,
57                                             jstring jcategories,
58                                             jboolean record_continuously) {
59   if (subscriber_.get()) {
60     return false;
61   }
62   std::string filename = base::android::ConvertJavaStringToUTF8(env, jfilename);
63   std::string categories =
64       base::android::ConvertJavaStringToUTF8(env, jcategories);
65   subscriber_.reset(new Subscriber(this, base::FilePath(filename)));
66   return TraceController::GetInstance()->BeginTracing(
67       subscriber_.get(),
68       categories,
69       record_continuously ? base::debug::TraceLog::RECORD_CONTINUOUSLY
70                           : base::debug::TraceLog::RECORD_UNTIL_FULL);
71 }
72
73 void TracingControllerAndroid::StopTracing(JNIEnv* env, jobject obj) {
74   if (!subscriber_.get()) {
75     return;
76   }
77   TraceController* controller = TraceController::GetInstance();
78   if (!controller->EndTracingAsync(subscriber_.get())) {
79     LOG(ERROR) << "EndTracingAsync failed, forcing an immediate stop";
80     controller->CancelSubscriber(subscriber_.get());
81     OnTracingStopped();
82   }
83 }
84
85 void TracingControllerAndroid::OnTracingStopped() {
86   JNIEnv* env = base::android::AttachCurrentThread();
87   base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env);
88   if (obj.obj()) {
89     Java_TracingControllerAndroid_onTracingStopped(env, obj.obj());
90   }
91   subscriber_.reset();
92 }
93
94 static jstring GetDefaultCategories(JNIEnv* env, jobject obj) {
95   return base::android::ConvertUTF8ToJavaString(env,
96       base::debug::CategoryFilter::kDefaultCategoryFilterString).Release();
97 }
98
99 bool RegisterTracingControllerAndroid(JNIEnv* env) {
100   return RegisterNativesImpl(env);
101 }
102
103 }  // namespace content