Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / browser_shutdown_profile_dumper.cc
blob4330c6141ca755f1005e74d9c55b9fef7aa03e76
1 // Copyright 2013 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/browser_shutdown_profile_dumper.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/thread.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "base/trace_event/trace_event.h"
17 #include "base/trace_event/trace_event_impl.h"
18 #include "components/tracing/tracing_switches.h"
20 namespace content {
22 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper(
23 const base::FilePath& dump_file_name)
24 : dump_file_name_(dump_file_name),
25 blocks_(0),
26 dump_file_(NULL) {
29 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() {
30 WriteTracesToDisc();
33 static float GetTraceBufferPercentFull() {
34 base::trace_event::TraceLogStatus status =
35 base::trace_event::TraceLog::GetInstance()->GetStatus();
36 return 100 * static_cast<float>(static_cast<double>(status.event_count) /
37 status.event_capacity);
40 void BrowserShutdownProfileDumper::WriteTracesToDisc() {
41 // Note: I have seen a usage of 0.000xx% when dumping - which fits easily.
42 // Since the tracer stops when the trace buffer is filled, we'd rather save
43 // what we have than nothing since we might see from the amount of events
44 // that caused the problem.
45 DVLOG(1) << "Flushing shutdown traces to disc. The buffer is "
46 << GetTraceBufferPercentFull() << "% full.";
47 DCHECK(!dump_file_);
48 dump_file_ = base::OpenFile(dump_file_name_, "w+");
49 if (!IsFileValid()) {
50 LOG(ERROR) << "Failed to open performance trace file: "
51 << dump_file_name_.value();
52 return;
54 WriteString("{\"traceEvents\":");
55 WriteString("[");
57 // TraceLog::Flush() requires the calling thread to have a message loop.
58 // As the message loop of the current thread may have quit, start another
59 // thread for flushing the trace.
60 base::WaitableEvent flush_complete_event(false, false);
61 base::Thread flush_thread("browser_shutdown_trace_event_flush");
62 flush_thread.Start();
63 flush_thread.task_runner()->PostTask(
64 FROM_HERE, base::Bind(&BrowserShutdownProfileDumper::EndTraceAndFlush,
65 base::Unretained(this),
66 base::Unretained(&flush_complete_event)));
68 bool original_wait_allowed = base::ThreadRestrictions::SetWaitAllowed(true);
69 flush_complete_event.Wait();
70 base::ThreadRestrictions::SetWaitAllowed(original_wait_allowed);
73 void BrowserShutdownProfileDumper::EndTraceAndFlush(
74 base::WaitableEvent* flush_complete_event) {
75 base::trace_event::TraceLog::GetInstance()->SetDisabled();
76 base::trace_event::TraceLog::GetInstance()->Flush(
77 base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected,
78 base::Unretained(this),
79 base::Unretained(flush_complete_event)));
82 // static
83 base::FilePath BrowserShutdownProfileDumper::GetShutdownProfileFileName() {
84 const base::CommandLine& command_line =
85 *base::CommandLine::ForCurrentProcess();
86 base::FilePath trace_file =
87 command_line.GetSwitchValuePath(switches::kTraceShutdownFile);
89 if (!trace_file.empty())
90 return trace_file;
92 // Default to saving the startup trace into the current dir.
93 return base::FilePath().AppendASCII("chrometrace.log");
96 void BrowserShutdownProfileDumper::WriteTraceDataCollected(
97 base::WaitableEvent* flush_complete_event,
98 const scoped_refptr<base::RefCountedString>& events_str,
99 bool has_more_events) {
100 if (!IsFileValid()) {
101 flush_complete_event->Signal();
102 return;
104 if (blocks_) {
105 // Blocks are not comma separated. Beginning with the second block we
106 // start therefore to add one in front of the previous block.
107 WriteString(",");
109 ++blocks_;
110 WriteString(events_str->data());
112 if (!has_more_events) {
113 WriteString("]");
114 WriteString("}");
115 CloseFile();
116 flush_complete_event->Signal();
120 bool BrowserShutdownProfileDumper::IsFileValid() {
121 return dump_file_ && (ferror(dump_file_) == 0);
124 void BrowserShutdownProfileDumper::WriteString(const std::string& string) {
125 WriteChars(string.data(), string.size());
128 void BrowserShutdownProfileDumper::WriteChars(const char* chars, size_t size) {
129 if (!IsFileValid())
130 return;
132 size_t written = fwrite(chars, 1, size, dump_file_);
133 if (written != size) {
134 LOG(ERROR) << "Error " << ferror(dump_file_)
135 << " in fwrite() to trace file '" << dump_file_name_.value()
136 << "'";
137 CloseFile();
141 void BrowserShutdownProfileDumper::CloseFile() {
142 if (!dump_file_)
143 return;
144 base::CloseFile(dump_file_);
145 dump_file_ = NULL;
148 } // namespace content