Android Browser Compositor: Add ScheduleComposite() callback.
[chromium-blink-merge.git] / content / browser / trace_subscriber_stdio.cc
blob05ebabace25b28d7fcc3a625abb112389fcaf103
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
3 // found in the LICENSE file.
5 #include "content/browser/trace_subscriber_stdio.h"
7 #include "base/bind.h"
8 #include "base/debug/trace_event.h"
9 #include "base/logging.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "content/public/browser/browser_thread.h"
13 namespace content {
15 // All method calls on this class are done on a SequencedWorkerPool thread.
16 class TraceSubscriberStdioImpl
17 : public base::RefCountedThreadSafe<TraceSubscriberStdioImpl> {
18 public:
19 explicit TraceSubscriberStdioImpl(const FilePath& path)
20 : path_(path),
21 file_(0) {}
23 void OnStart() {
24 DCHECK(!file_);
25 trace_buffer_.SetOutputCallback(
26 base::Bind(&TraceSubscriberStdioImpl::Write, this));
27 file_ = file_util::OpenFile(path_, "w+");
28 if (IsValid()) {
29 LOG(INFO) << "Logging performance trace to file: " << path_.value();
30 trace_buffer_.Start();
31 } else {
32 LOG(ERROR) << "Failed to open performance trace file: " << path_.value();
36 void OnData(const scoped_refptr<base::RefCountedString>& data_ptr) {
37 trace_buffer_.AddFragment(data_ptr->data());
40 void OnEnd() {
41 trace_buffer_.Finish();
42 CloseFile();
45 private:
46 friend class base::RefCountedThreadSafe<TraceSubscriberStdioImpl>;
48 ~TraceSubscriberStdioImpl() {
49 CloseFile();
52 bool IsValid() const {
53 return file_ && (0 == ferror(file_));
56 void CloseFile() {
57 if (file_) {
58 fclose(file_);
59 file_ = 0;
61 // This is important, as it breaks a reference cycle.
62 trace_buffer_.SetOutputCallback(
63 base::debug::TraceResultBuffer::OutputCallback());
66 void Write(const std::string& output_str) {
67 if (IsValid()) {
68 size_t written = fwrite(output_str.data(), 1, output_str.size(), file_);
69 if (written != output_str.size()) {
70 LOG(ERROR) << "Error " << ferror(file_) << " in fwrite() to trace file";
71 CloseFile();
76 FilePath path_;
77 FILE* file_;
78 base::debug::TraceResultBuffer trace_buffer_;
81 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path)
82 : impl_(new TraceSubscriberStdioImpl(path)) {
83 BrowserThread::PostBlockingPoolSequencedTask(
84 __FILE__, FROM_HERE,
85 base::Bind(&TraceSubscriberStdioImpl::OnStart, impl_));
88 TraceSubscriberStdio::~TraceSubscriberStdio() {
91 void TraceSubscriberStdio::OnEndTracingComplete() {
92 BrowserThread::PostBlockingPoolSequencedTask(
93 __FILE__, FROM_HERE,
94 base::Bind(&TraceSubscriberStdioImpl::OnEnd, impl_));
97 void TraceSubscriberStdio::OnTraceDataCollected(
98 const scoped_refptr<base::RefCountedString>& data_ptr) {
99 BrowserThread::PostBlockingPoolSequencedTask(
100 __FILE__, FROM_HERE,
101 base::Bind(&TraceSubscriberStdioImpl::OnData, impl_, data_ptr));
104 } // namespace content