Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / browser_shutdown_profile_dumper.cc
blob6c4d8c8c14a76418be1cf27fdf22bfea293e1d44
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/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/thread.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "base/trace_event/trace_event.h"
16 #include "base/trace_event/trace_event_impl.h"
17 #include "content/public/common/content_switches.h"
19 namespace content {
21 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper(
22 const base::FilePath& dump_file_name)
23 : dump_file_name_(dump_file_name),
24 blocks_(0),
25 dump_file_(NULL) {
28 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() {
29 WriteTracesToDisc();
32 static float GetTraceBufferPercentFull() {
33 base::trace_event::TraceLogStatus status =
34 base::trace_event::TraceLog::GetInstance()->GetStatus();
35 return 100 * static_cast<float>(static_cast<double>(status.event_count) /
36 status.event_capacity);
39 void BrowserShutdownProfileDumper::WriteTracesToDisc() {
40 // Note: I have seen a usage of 0.000xx% when dumping - which fits easily.
41 // Since the tracer stops when the trace buffer is filled, we'd rather save
42 // what we have than nothing since we might see from the amount of events
43 // that caused the problem.
44 DVLOG(1) << "Flushing shutdown traces to disc. The buffer is "
45 << GetTraceBufferPercentFull() << "% full.";
46 DCHECK(!dump_file_);
47 dump_file_ = base::OpenFile(dump_file_name_, "w+");
48 if (!IsFileValid()) {
49 LOG(ERROR) << "Failed to open performance trace file: "
50 << dump_file_name_.value();
51 return;
53 WriteString("{\"traceEvents\":");
54 WriteString("[");
56 // TraceLog::Flush() requires the calling thread to have a message loop.
57 // As the message loop of the current thread may have quit, start another
58 // thread for flushing the trace.
59 base::WaitableEvent flush_complete_event(false, false);
60 base::Thread flush_thread("browser_shutdown_trace_event_flush");
61 flush_thread.Start();
62 flush_thread.message_loop()->PostTask(
63 FROM_HERE,
64 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