Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: chrome/test/base/tracing.cc

Issue 67683003: Remove TraceController (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix problematic Unretained Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/file_util.h"
8 #include "base/files/file_path.h"
8 #include "base/memory/singleton.h" 9 #include "base/memory/singleton.h"
9 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/timer/timer.h"
10 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/trace_controller.h" 13 #include "content/public/browser/tracing_controller.h"
12 #include "content/public/browser/trace_subscriber.h"
13 #include "content/public/test/test_utils.h" 14 #include "content/public/test/test_utils.h"
14 15
15 namespace { 16 namespace {
16 17
17 using content::BrowserThread; 18 using content::BrowserThread;
18 19
19 class InProcessTraceController : public content::TraceSubscriber { 20 class InProcessTraceController {
20 public: 21 public:
21 static InProcessTraceController* GetInstance() { 22 static InProcessTraceController* GetInstance() {
22 return Singleton<InProcessTraceController>::get(); 23 return Singleton<InProcessTraceController>::get();
23 } 24 }
24 25
25 InProcessTraceController() 26 InProcessTraceController()
26 : is_waiting_on_watch_(false), 27 : is_waiting_on_watch_(false),
27 watch_notification_count_(0) {} 28 watch_notification_count_(0) {}
28 virtual ~InProcessTraceController() {} 29 virtual ~InProcessTraceController() {}
29 30
30 bool BeginTracing(const std::string& category_patterns) { 31 bool BeginTracing(const std::string& category_patterns) {
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32 return content::TraceController::GetInstance()->BeginTracing( 33 return content::TracingController::GetInstance()->EnableRecording(
33 this, category_patterns, base::debug::TraceLog::RECORD_UNTIL_FULL); 34 category_patterns, content::TracingController::DEFAULT_OPTIONS,
35 content::TracingController::EnableRecordingDoneCallback());
36 return true;
34 } 37 }
35 38
36 bool BeginTracingWithWatch(const std::string& category_patterns, 39 bool BeginTracingWithWatch(const std::string& category_patterns,
37 const std::string& category_name, 40 const std::string& category_name,
38 const std::string& event_name, 41 const std::string& event_name,
39 int num_occurrences) { 42 int num_occurrences) {
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
41 DCHECK(num_occurrences > 0); 44 DCHECK(num_occurrences > 0);
42 watch_notification_count_ = num_occurrences; 45 watch_notification_count_ = num_occurrences;
43 return BeginTracing(category_patterns) && 46 return BeginTracing(category_patterns) &&
44 content::TraceController::GetInstance()->SetWatchEvent( 47 content::TracingController::GetInstance()->SetWatchEvent(
45 this, category_name, event_name); 48 category_name, event_name,
49 base::Bind(&InProcessTraceController::OnWatchEventMatched,
50 base::Unretained(this)));
46 } 51 }
47 52
48 bool WaitForWatchEvent(base::TimeDelta timeout) { 53 bool WaitForWatchEvent(base::TimeDelta timeout) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
50 if (watch_notification_count_ == 0) 55 if (watch_notification_count_ == 0)
51 return true; 56 return true;
52 57
53 if (timeout != base::TimeDelta()) { 58 if (timeout != base::TimeDelta()) {
54 timer_.Start(FROM_HERE, timeout, this, 59 timer_.Start(FROM_HERE, timeout, this,
55 &InProcessTraceController::Timeout); 60 &InProcessTraceController::Timeout);
56 } 61 }
57 62
58 is_waiting_on_watch_ = true; 63 is_waiting_on_watch_ = true;
59 message_loop_runner_ = new content::MessageLoopRunner; 64 message_loop_runner_ = new content::MessageLoopRunner;
60 message_loop_runner_->Run(); 65 message_loop_runner_->Run();
61 is_waiting_on_watch_ = false; 66 is_waiting_on_watch_ = false;
62 67
63 return watch_notification_count_ == 0; 68 return watch_notification_count_ == 0;
64 } 69 }
65 70
66 bool EndTracing(std::string* json_trace_output) { 71 bool EndTracing(std::string* json_trace_output) {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
68 using namespace base::debug; 73 using namespace base::debug;
69 74
70 TraceResultBuffer::SimpleOutput output; 75 if (!content::TracingController::GetInstance()->DisableRecording(
71 trace_buffer_.SetOutputCallback(output.GetCallback()); 76 base::FilePath(),
77 base::Bind(&InProcessTraceController::OnTraceDataCollected,
78 base::Unretained(this),
79 base::Unretained(json_trace_output))))
80 return false;
72 81
73 trace_buffer_.Start(); 82 // Wait for OnTraceDataCollected() to quit the message loop.
74 if (!content::TraceController::GetInstance()->EndTracingAsync(this))
75 return false;
76 // Wait for OnEndTracingComplete() to quit the message loop.
77 // OnTraceDataCollected may be called multiple times while blocking here.
78 message_loop_runner_ = new content::MessageLoopRunner; 83 message_loop_runner_ = new content::MessageLoopRunner;
79 message_loop_runner_->Run(); 84 message_loop_runner_->Run();
80 trace_buffer_.Finish();
81 trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback());
82
83 *json_trace_output = output.json_output;
84 85
85 // Watch notifications can occur during this method's message loop run, but 86 // Watch notifications can occur during this method's message loop run, but
86 // not after, so clear them here. 87 // not after, so clear them here.
87 watch_notification_count_ = 0; 88 watch_notification_count_ = 0;
88 return true; 89 return true;
89 } 90 }
90 91
91 private: 92 private:
92 friend struct DefaultSingletonTraits<InProcessTraceController>; 93 friend struct DefaultSingletonTraits<InProcessTraceController>;
93 94
94 // TraceSubscriber implementation 95 void OnTraceDataCollected(std::string* json_trace_output,
95 virtual void OnEndTracingComplete() OVERRIDE { 96 const base::FilePath& path) {
96 message_loop_runner_->Quit(); 97 bool old_io_allowed = base::ThreadRestrictions::SetIOAllowed(true);
98 bool ok = base::ReadFileToString(path, json_trace_output);
99 DCHECK(ok);
100 base::DeleteFile(path, false);
101 base::ThreadRestrictions::SetIOAllowed(old_io_allowed);
jam 2013/11/18 16:57:21 nit: use base::ThreadRestrictions::ScopedAllowIO
97 } 102 }
98 103
99 // TraceSubscriber implementation 104 void OnWatchEventMatched() {
100 virtual void OnTraceDataCollected(
101 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE {
102 trace_buffer_.AddFragment(trace_fragment->data());
103 }
104
105 // TraceSubscriber implementation
106 virtual void OnEventWatchNotification() OVERRIDE {
107 if (watch_notification_count_ == 0) 105 if (watch_notification_count_ == 0)
108 return; 106 return;
109 if (--watch_notification_count_ == 0) { 107 if (--watch_notification_count_ == 0) {
110 timer_.Stop(); 108 timer_.Stop();
111 if (is_waiting_on_watch_) 109 if (is_waiting_on_watch_)
112 message_loop_runner_->Quit(); 110 message_loop_runner_->Quit();
113 } 111 }
114 } 112 }
115 113
116 void Timeout() { 114 void Timeout() {
117 DCHECK(is_waiting_on_watch_); 115 DCHECK(is_waiting_on_watch_);
118 message_loop_runner_->Quit(); 116 message_loop_runner_->Quit();
119 } 117 }
120 118
121 // For collecting trace data asynchronously.
122 base::debug::TraceResultBuffer trace_buffer_;
123
124 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; 119 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
125 120
126 base::OneShotTimer<InProcessTraceController> timer_; 121 base::OneShotTimer<InProcessTraceController> timer_;
127 122
128 bool is_waiting_on_watch_; 123 bool is_waiting_on_watch_;
129 int watch_notification_count_; 124 int watch_notification_count_;
130 125
131 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); 126 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController);
132 }; 127 };
133 128
(...skipping 17 matching lines...) Expand all
151 bool WaitForWatchEvent(base::TimeDelta timeout) { 146 bool WaitForWatchEvent(base::TimeDelta timeout) {
152 return InProcessTraceController::GetInstance()->WaitForWatchEvent(timeout); 147 return InProcessTraceController::GetInstance()->WaitForWatchEvent(timeout);
153 } 148 }
154 149
155 bool EndTracing(std::string* json_trace_output) { 150 bool EndTracing(std::string* json_trace_output) {
156 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); 151 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output);
157 } 152 }
158 153
159 } // namespace tracing 154 } // namespace tracing
160 155
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698