| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/test/base/tracing.h" | 5 #include "chrome/test/base/tracing.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/test/base/ui_test_utils.h" | |
| 11 #include "content/public/browser/trace_controller.h" | 10 #include "content/public/browser/trace_controller.h" |
| 12 #include "content/public/browser/trace_subscriber.h" | 11 #include "content/public/browser/trace_subscriber.h" |
| 12 #include "content/public/test/test_utils.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class InProcessTraceController : public content::TraceSubscriber { | 16 class InProcessTraceController : public content::TraceSubscriber { |
| 17 public: | 17 public: |
| 18 static InProcessTraceController* GetInstance() { | 18 static InProcessTraceController* GetInstance() { |
| 19 return Singleton<InProcessTraceController>::get(); | 19 return Singleton<InProcessTraceController>::get(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 InProcessTraceController() {} | 22 InProcessTraceController() {} |
| 23 virtual ~InProcessTraceController() {} | 23 virtual ~InProcessTraceController() {} |
| 24 | 24 |
| 25 bool BeginTracing(const std::string& categories) { | 25 bool BeginTracing(const std::string& categories) { |
| 26 return content::TraceController::GetInstance()->BeginTracing( | 26 return content::TraceController::GetInstance()->BeginTracing( |
| 27 this, categories); | 27 this, categories); |
| 28 } | 28 } |
| 29 | 29 |
| 30 bool EndTracing(std::string* json_trace_output) { | 30 bool EndTracing(std::string* json_trace_output) { |
| 31 using namespace base::debug; | 31 using namespace base::debug; |
| 32 | 32 |
| 33 TraceResultBuffer::SimpleOutput output; | 33 TraceResultBuffer::SimpleOutput output; |
| 34 trace_buffer_.SetOutputCallback(output.GetCallback()); | 34 trace_buffer_.SetOutputCallback(output.GetCallback()); |
| 35 | 35 |
| 36 trace_buffer_.Start(); | 36 trace_buffer_.Start(); |
| 37 if (!content::TraceController::GetInstance()->EndTracingAsync(this)) | 37 if (!content::TraceController::GetInstance()->EndTracingAsync(this)) |
| 38 return false; | 38 return false; |
| 39 // Wait for OnEndTracingComplete() to quit the message loop. | 39 // Wait for OnEndTracingComplete() to quit the message loop. |
| 40 // OnTraceDataCollected may be called multiple times while blocking here. | 40 // OnTraceDataCollected may be called multiple times while blocking here. |
| 41 message_loop_runner_ = new ui_test_utils::MessageLoopRunner; | 41 message_loop_runner_ = new content::MessageLoopRunner; |
| 42 message_loop_runner_->Run(); | 42 message_loop_runner_->Run(); |
| 43 trace_buffer_.Finish(); | 43 trace_buffer_.Finish(); |
| 44 trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback()); | 44 trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback()); |
| 45 | 45 |
| 46 *json_trace_output = output.json_output; | 46 *json_trace_output = output.json_output; |
| 47 return true; | 47 return true; |
| 48 } | 48 } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 friend struct DefaultSingletonTraits<InProcessTraceController>; | 51 friend struct DefaultSingletonTraits<InProcessTraceController>; |
| 52 | 52 |
| 53 // TraceSubscriber | 53 // TraceSubscriber |
| 54 virtual void OnEndTracingComplete() OVERRIDE { | 54 virtual void OnEndTracingComplete() OVERRIDE { |
| 55 message_loop_runner_->Quit(); | 55 message_loop_runner_->Quit(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // TraceSubscriber | 58 // TraceSubscriber |
| 59 virtual void OnTraceDataCollected( | 59 virtual void OnTraceDataCollected( |
| 60 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE { | 60 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE { |
| 61 trace_buffer_.AddFragment(trace_fragment->data()); | 61 trace_buffer_.AddFragment(trace_fragment->data()); |
| 62 } | 62 } |
| 63 | 63 |
| 64 // For collecting trace data asynchronously. | 64 // For collecting trace data asynchronously. |
| 65 base::debug::TraceResultBuffer trace_buffer_; | 65 base::debug::TraceResultBuffer trace_buffer_; |
| 66 | 66 |
| 67 scoped_refptr<ui_test_utils::MessageLoopRunner> message_loop_runner_; | 67 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 68 | 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); | 69 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 } // namespace | 72 } // namespace |
| 73 | 73 |
| 74 namespace tracing { | 74 namespace tracing { |
| 75 | 75 |
| 76 bool BeginTracing(const std::string& categories) { | 76 bool BeginTracing(const std::string& categories) { |
| 77 return InProcessTraceController::GetInstance()->BeginTracing(categories); | 77 return InProcessTraceController::GetInstance()->BeginTracing(categories); |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool EndTracing(std::string* json_trace_output) { | 80 bool EndTracing(std::string* json_trace_output) { |
| 81 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); | 81 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); |
| 82 } | 82 } |
| 83 | 83 |
| 84 } // namespace tracing | 84 } // namespace tracing |
| 85 | 85 |
| OLD | NEW |