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 "content/public/browser/browser_thread.h" | |
10 #include "content/public/browser/trace_controller.h" | 11 #include "content/public/browser/trace_controller.h" |
11 #include "content/public/browser/trace_subscriber.h" | 12 #include "content/public/browser/trace_subscriber.h" |
12 #include "content/public/test/test_utils.h" | 13 #include "content/public/test/test_utils.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
17 using content::BrowserThread; | |
18 | |
16 class InProcessTraceController : public content::TraceSubscriber { | 19 class InProcessTraceController : public content::TraceSubscriber { |
17 public: | 20 public: |
18 static InProcessTraceController* GetInstance() { | 21 static InProcessTraceController* GetInstance() { |
19 return Singleton<InProcessTraceController>::get(); | 22 return Singleton<InProcessTraceController>::get(); |
20 } | 23 } |
21 | 24 |
22 InProcessTraceController() {} | 25 InProcessTraceController() |
26 : is_waiting_on_watch_(false) | |
27 , watch_notification_count_(0) {} | |
sky
2012/08/29 16:05:09
, on previous line.
jbates
2012/08/29 21:14:12
Done.
| |
23 virtual ~InProcessTraceController() {} | 28 virtual ~InProcessTraceController() {} |
24 | 29 |
25 bool BeginTracing(const std::string& categories) { | 30 bool BeginTracing(const std::string& categories) { |
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
26 return content::TraceController::GetInstance()->BeginTracing( | 32 return content::TraceController::GetInstance()->BeginTracing( |
27 this, categories); | 33 this, categories); |
28 } | 34 } |
29 | 35 |
36 bool BeginTracingWithWatch(const std::string& categories, | |
37 const char* category_name, | |
38 const char* event_name, | |
39 int num_occurrences) { | |
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
41 DCHECK(num_occurrences > 0); | |
42 watch_notification_count_ = num_occurrences; | |
43 return BeginTracing(categories) && | |
44 content::TraceController::GetInstance()->SetWatchEvent( | |
45 this, category_name, event_name); | |
46 } | |
47 | |
48 bool WaitForWatchEvent(base::TimeDelta timeout) { | |
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
50 if (watch_notification_count_ == 0) | |
51 return true; | |
52 | |
53 if (timeout != base::TimeDelta()) { | |
54 timer_.Start(FROM_HERE, timeout, this, | |
55 &InProcessTraceController::Timeout); | |
56 } | |
57 | |
58 is_waiting_on_watch_ = true; | |
59 message_loop_runner_ = new content::MessageLoopRunner; | |
60 message_loop_runner_->Run(); | |
61 is_waiting_on_watch_ = false; | |
62 | |
63 return watch_notification_count_ == 0; | |
64 } | |
65 | |
30 bool EndTracing(std::string* json_trace_output) { | 66 bool EndTracing(std::string* json_trace_output) { |
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
31 using namespace base::debug; | 68 using namespace base::debug; |
32 | 69 |
33 TraceResultBuffer::SimpleOutput output; | 70 TraceResultBuffer::SimpleOutput output; |
34 trace_buffer_.SetOutputCallback(output.GetCallback()); | 71 trace_buffer_.SetOutputCallback(output.GetCallback()); |
35 | 72 |
36 trace_buffer_.Start(); | 73 trace_buffer_.Start(); |
37 if (!content::TraceController::GetInstance()->EndTracingAsync(this)) | 74 if (!content::TraceController::GetInstance()->EndTracingAsync(this)) |
38 return false; | 75 return false; |
39 // Wait for OnEndTracingComplete() to quit the message loop. | 76 // Wait for OnEndTracingComplete() to quit the message loop. |
40 // OnTraceDataCollected may be called multiple times while blocking here. | 77 // OnTraceDataCollected may be called multiple times while blocking here. |
41 message_loop_runner_ = new content::MessageLoopRunner; | 78 message_loop_runner_ = new content::MessageLoopRunner; |
42 message_loop_runner_->Run(); | 79 message_loop_runner_->Run(); |
43 trace_buffer_.Finish(); | 80 trace_buffer_.Finish(); |
44 trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback()); | 81 trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback()); |
45 | 82 |
46 *json_trace_output = output.json_output; | 83 *json_trace_output = output.json_output; |
84 | |
85 // Watch notifications can occur during this method's message loop run, but | |
86 // not after, so clear them here. | |
87 watch_notification_count_ = 0; | |
47 return true; | 88 return true; |
48 } | 89 } |
49 | 90 |
50 private: | 91 private: |
51 friend struct DefaultSingletonTraits<InProcessTraceController>; | 92 friend struct DefaultSingletonTraits<InProcessTraceController>; |
52 | 93 |
53 // TraceSubscriber | 94 // TraceSubscriber implementation |
54 virtual void OnEndTracingComplete() OVERRIDE { | 95 virtual void OnEndTracingComplete() OVERRIDE { |
55 message_loop_runner_->Quit(); | 96 message_loop_runner_->Quit(); |
56 } | 97 } |
57 | 98 |
58 // TraceSubscriber | 99 // TraceSubscriber implementation |
59 virtual void OnTraceDataCollected( | 100 virtual void OnTraceDataCollected( |
60 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE { | 101 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE { |
61 trace_buffer_.AddFragment(trace_fragment->data()); | 102 trace_buffer_.AddFragment(trace_fragment->data()); |
62 } | 103 } |
63 | 104 |
105 // TraceSubscriber implementation | |
106 virtual void OnEventWatchNotification() { | |
sky
2012/08/29 16:05:09
OVERRIDE
jbates
2012/08/29 21:14:12
Done.
| |
107 if (watch_notification_count_ == 0) | |
108 return; | |
109 if (--watch_notification_count_ == 0) { | |
110 timer_.Stop(); | |
111 if (is_waiting_on_watch_) | |
112 message_loop_runner_->Quit(); | |
sky
2012/08/29 16:05:09
Is it possible the timer is still running?
jbates
2012/08/29 21:14:12
It's stopped on line 110.
| |
113 } | |
114 } | |
115 | |
116 void Timeout() { | |
117 DCHECK(is_waiting_on_watch_); | |
118 message_loop_runner_->Quit(); | |
119 } | |
120 | |
64 // For collecting trace data asynchronously. | 121 // For collecting trace data asynchronously. |
65 base::debug::TraceResultBuffer trace_buffer_; | 122 base::debug::TraceResultBuffer trace_buffer_; |
66 | 123 |
67 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 124 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
68 | 125 |
126 base::OneShotTimer<InProcessTraceController> timer_; | |
127 | |
128 bool is_waiting_on_watch_; | |
129 int watch_notification_count_; | |
130 | |
69 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); | 131 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); |
70 }; | 132 }; |
71 | 133 |
72 } // namespace | 134 } // namespace |
73 | 135 |
74 namespace tracing { | 136 namespace tracing { |
75 | 137 |
76 bool BeginTracing(const std::string& categories) { | 138 bool BeginTracing(const std::string& categories) { |
77 return InProcessTraceController::GetInstance()->BeginTracing(categories); | 139 return InProcessTraceController::GetInstance()->BeginTracing(categories); |
78 } | 140 } |
79 | 141 |
142 bool BeginTracingWithWatch(const std::string& categories, | |
143 const char* category_name, | |
144 const char* event_name, | |
145 int num_occurrences) { | |
146 return InProcessTraceController::GetInstance()->BeginTracingWithWatch( | |
147 categories, category_name, event_name, num_occurrences); | |
148 } | |
149 | |
150 bool WaitForWatchEvent(base::TimeDelta timeout) { | |
151 return InProcessTraceController::GetInstance()->WaitForWatchEvent(timeout); | |
152 } | |
153 | |
80 bool EndTracing(std::string* json_trace_output) { | 154 bool EndTracing(std::string* json_trace_output) { |
81 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); | 155 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); |
82 } | 156 } |
83 | 157 |
84 } // namespace tracing | 158 } // namespace tracing |
85 | 159 |
OLD | NEW |