Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / content / browser / browser_shutdown_profile_dumper.cc
blob3e74e063b0c0b60aadd9d47783d0c248c2d12687
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/base_switches.h"
8 #include "base/command_line.h"
9 #include "base/debug/trace_event.h"
10 #include "base/debug/trace_event_impl.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "content/public/common/content_switches.h"
19 namespace content {
21 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper()
22 : blocks_(0),
23 dump_file_(NULL) {
26 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() {
27 WriteTracesToDisc(GetFileName());
30 void BrowserShutdownProfileDumper::WriteTracesToDisc(
31 const base::FilePath& file_name) {
32 // Note: I have seen a usage of 0.000xx% when dumping - which fits easily.
33 // Since the tracer stops when the trace buffer is filled, we'd rather save
34 // what we have than nothing since we might see from the amount of events
35 // that caused the problem.
36 DVLOG(1) << "Flushing shutdown traces to disc. The buffer is %" <<
37 base::debug::TraceLog::GetInstance()->GetBufferPercentFull() <<
38 " full.";
39 DCHECK(!dump_file_);
40 dump_file_ = base::OpenFile(file_name, "w+");
41 if (!IsFileValid()) {
42 LOG(ERROR) << "Failed to open performance trace file: " <<
43 file_name.value();
44 return;
46 WriteString("{\"traceEvents\":");
47 WriteString("[");
49 // TraceLog::Flush() requires the calling thread to have a message loop.
50 // As the message loop of the current thread may have quit, start another
51 // thread for flushing the trace.
52 base::WaitableEvent flush_complete_event(false, false);
53 base::Thread flush_thread("browser_shutdown_trace_event_flush");
54 flush_thread.Start();
55 flush_thread.message_loop()->PostTask(
56 FROM_HERE,
57 base::Bind(&BrowserShutdownProfileDumper::EndTraceAndFlush,
58 base::Unretained(this),
59 base::Unretained(&flush_complete_event)));
61 bool original_wait_allowed = base::ThreadRestrictions::SetWaitAllowed(true);
62 flush_complete_event.Wait();
63 base::ThreadRestrictions::SetWaitAllowed(original_wait_allowed);
66 void BrowserShutdownProfileDumper::EndTraceAndFlush(
67 base::WaitableEvent* flush_complete_event) {
68 base::debug::TraceLog::GetInstance()->SetDisabled();
69 base::debug::TraceLog::GetInstance()->Flush(
70 base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected,
71 base::Unretained(this),
72 base::Unretained(flush_complete_event)));
75 base::FilePath BrowserShutdownProfileDumper::GetFileName() {
76 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
77 base::FilePath trace_file =
78 command_line.GetSwitchValuePath(switches::kTraceShutdownFile);
80 if (!trace_file.empty())
81 return trace_file;
83 // Default to saving the startup trace into the current dir.
84 return base::FilePath().AppendASCII("chrometrace.log");
87 void BrowserShutdownProfileDumper::WriteTraceDataCollected(
88 base::WaitableEvent* flush_complete_event,
89 const scoped_refptr<base::RefCountedString>& events_str,
90 bool has_more_events) {
91 if (!IsFileValid()) {
92 flush_complete_event->Signal();
93 return;
95 if (blocks_) {
96 // Blocks are not comma separated. Beginning with the second block we
97 // start therefore to add one in front of the previous block.
98 WriteString(",");
100 ++blocks_;
101 WriteString(events_str->data());
103 if (!has_more_events) {
104 WriteString("]");
105 WriteString("}");
106 CloseFile();
107 flush_complete_event->Signal();
111 bool BrowserShutdownProfileDumper::IsFileValid() {
112 return dump_file_ && (ferror(dump_file_) == 0);
115 void BrowserShutdownProfileDumper::WriteString(const std::string& string) {
116 WriteChars(string.data(), string.size());
119 void BrowserShutdownProfileDumper::WriteChars(const char* chars, size_t size) {
120 if (!IsFileValid())
121 return;
123 size_t written = fwrite(chars, 1, size, dump_file_);
124 if (written != size) {
125 LOG(ERROR) << "Error " << ferror(dump_file_) <<
126 " in fwrite() to trace file";
127 CloseFile();
131 void BrowserShutdownProfileDumper::CloseFile() {
132 if (!dump_file_)
133 return;
134 base::CloseFile(dump_file_);
135 dump_file_ = NULL;
138 } // namespace content