OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/android/tracing_controller_android.h" | 5 #include "content/browser/android/tracing_controller_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/command_line.h" |
9 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
| 11 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "content/public/browser/tracing_controller.h" | 13 #include "content/browser/tracing/trace_subscriber_stdio.h" |
| 14 #include "content/public/browser/trace_controller.h" |
12 #include "jni/TracingControllerAndroid_jni.h" | 15 #include "jni/TracingControllerAndroid_jni.h" |
13 | 16 |
14 namespace content { | 17 namespace content { |
15 | 18 |
16 static jlong Init(JNIEnv* env, jobject obj) { | 19 static jlong Init(JNIEnv* env, jobject obj) { |
17 TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj); | 20 TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj); |
18 return reinterpret_cast<intptr_t>(profiler); | 21 return reinterpret_cast<intptr_t>(profiler); |
19 } | 22 } |
20 | 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 |
21 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj) | 45 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj) |
22 : weak_java_object_(env, obj), | 46 : weak_java_object_(env, obj) {} |
23 weak_factory_(this) {} | |
24 | 47 |
25 TracingControllerAndroid::~TracingControllerAndroid() {} | 48 TracingControllerAndroid::~TracingControllerAndroid() {} |
26 | 49 |
27 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) { | 50 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) { |
28 delete this; | 51 delete this; |
29 } | 52 } |
30 | 53 |
31 bool TracingControllerAndroid::StartTracing(JNIEnv* env, | 54 bool TracingControllerAndroid::StartTracing(JNIEnv* env, |
32 jobject obj, | 55 jobject obj, |
33 jstring jfilename, | 56 jstring jfilename, |
34 jstring jcategories, | 57 jstring jcategories, |
35 jboolean record_continuously) { | 58 jboolean record_continuously) { |
36 file_path_ = base::FilePath( | 59 if (subscriber_.get()) { |
37 base::android::ConvertJavaStringToUTF8(env, jfilename)); | 60 return false; |
| 61 } |
| 62 std::string filename = base::android::ConvertJavaStringToUTF8(env, jfilename); |
38 std::string categories = | 63 std::string categories = |
39 base::android::ConvertJavaStringToUTF8(env, jcategories); | 64 base::android::ConvertJavaStringToUTF8(env, jcategories); |
40 | 65 subscriber_.reset(new Subscriber(this, base::FilePath(filename))); |
41 // This log is required by adb_profile_chrome.py. | 66 return TraceController::GetInstance()->BeginTracing( |
42 LOG(WARNING) << "Logging performance trace to file: " << file_path_.value(); | 67 subscriber_.get(), |
43 | |
44 return TracingController::GetInstance()->EnableRecording( | |
45 categories, | 68 categories, |
46 record_continuously ? TracingController::RECORD_CONTINUOUSLY | 69 record_continuously ? base::debug::TraceLog::RECORD_CONTINUOUSLY |
47 : TracingController::DEFAULT_OPTIONS, | 70 : base::debug::TraceLog::RECORD_UNTIL_FULL); |
48 TracingController::EnableRecordingDoneCallback()); | |
49 } | 71 } |
50 | 72 |
51 void TracingControllerAndroid::StopTracing(JNIEnv* env, jobject obj) { | 73 void TracingControllerAndroid::StopTracing(JNIEnv* env, jobject obj) { |
52 if (!TracingController::GetInstance()->DisableRecording( | 74 if (!subscriber_.get()) { |
53 file_path_, | 75 return; |
54 base::Bind(&TracingControllerAndroid::OnTracingStopped, | 76 } |
55 weak_factory_.GetWeakPtr()))) { | 77 TraceController* controller = TraceController::GetInstance(); |
| 78 if (!controller->EndTracingAsync(subscriber_.get())) { |
56 LOG(ERROR) << "EndTracingAsync failed, forcing an immediate stop"; | 79 LOG(ERROR) << "EndTracingAsync failed, forcing an immediate stop"; |
57 OnTracingStopped(file_path_); | 80 controller->CancelSubscriber(subscriber_.get()); |
| 81 OnTracingStopped(); |
58 } | 82 } |
59 } | 83 } |
60 | 84 |
61 void TracingControllerAndroid::OnTracingStopped( | 85 void TracingControllerAndroid::OnTracingStopped() { |
62 const base::FilePath& file_path) { | |
63 JNIEnv* env = base::android::AttachCurrentThread(); | 86 JNIEnv* env = base::android::AttachCurrentThread(); |
64 base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env); | 87 base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env); |
65 if (obj.obj()) | 88 if (obj.obj()) { |
66 Java_TracingControllerAndroid_onTracingStopped(env, obj.obj()); | 89 Java_TracingControllerAndroid_onTracingStopped(env, obj.obj()); |
| 90 } |
| 91 subscriber_.reset(); |
67 } | 92 } |
68 | 93 |
69 static jstring GetDefaultCategories(JNIEnv* env, jobject obj) { | 94 static jstring GetDefaultCategories(JNIEnv* env, jobject obj) { |
70 return base::android::ConvertUTF8ToJavaString(env, | 95 return base::android::ConvertUTF8ToJavaString(env, |
71 base::debug::CategoryFilter::kDefaultCategoryFilterString).Release(); | 96 base::debug::CategoryFilter::kDefaultCategoryFilterString).Release(); |
72 } | 97 } |
73 | 98 |
74 bool RegisterTracingControllerAndroid(JNIEnv* env) { | 99 bool RegisterTracingControllerAndroid(JNIEnv* env) { |
75 return RegisterNativesImpl(env); | 100 return RegisterNativesImpl(env); |
76 } | 101 } |
77 | 102 |
78 } // namespace content | 103 } // namespace content |
OLD | NEW |