| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "sky/shell/tracing_controller.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_string.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/trace_event/trace_event.h" | |
| 12 #include "jni/TracingController_jni.h" | |
| 13 | |
| 14 namespace sky { | |
| 15 namespace shell { | |
| 16 namespace { | |
| 17 | |
| 18 const char kStart[] = "{\"traceEvents\":["; | |
| 19 const char kEnd[] = "]}"; | |
| 20 | |
| 21 static FILE* g_file = NULL; | |
| 22 | |
| 23 void Write(const std::string& data) { | |
| 24 ignore_result(fwrite(data.data(), data.length(), 1, g_file)); | |
| 25 } | |
| 26 | |
| 27 void HandleChunk(const scoped_refptr<base::RefCountedString>& chunk, | |
| 28 bool has_more_events) { | |
| 29 Write(chunk->data()); | |
| 30 if (has_more_events) | |
| 31 Write(","); | |
| 32 | |
| 33 if (!has_more_events) { | |
| 34 Write(kEnd); | |
| 35 base::CloseFile(g_file); | |
| 36 g_file = NULL; | |
| 37 | |
| 38 LOG(INFO) << "Trace complete"; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 static void StartTracing(JNIEnv* env, jclass clazz) { | |
| 45 LOG(INFO) << "Starting trace"; | |
| 46 | |
| 47 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
| 48 base::trace_event::CategoryFilter("*"), | |
| 49 base::trace_event::TraceLog::RECORDING_MODE, | |
| 50 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); | |
| 51 } | |
| 52 | |
| 53 static void StopTracing(JNIEnv* env, jclass clazz, jstring path) { | |
| 54 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
| 55 | |
| 56 base::FilePath file_path(base::android::ConvertJavaStringToUTF8(env, path)); | |
| 57 g_file = base::OpenFile(file_path, "w"); | |
| 58 CHECK(g_file) << "Failed to open file " << file_path.LossyDisplayName(); | |
| 59 | |
| 60 LOG(INFO) << "Saving trace to " << file_path.LossyDisplayName(); | |
| 61 Write(kStart); | |
| 62 base::trace_event::TraceLog::GetInstance()->Flush(base::Bind(&HandleChunk)); | |
| 63 } | |
| 64 | |
| 65 bool RegisterTracingController(JNIEnv* env) { | |
| 66 return RegisterNativesImpl(env); | |
| 67 } | |
| 68 | |
| 69 } // namespace shell | |
| 70 } // namespace sky | |
| OLD | NEW |