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.
4
5#ifndef CONTENT_BROWSER_BROWSER_SHUTDOWN_PROFILE_DUMPER_H_
6#define CONTENT_BROWSER_BROWSER_SHUTDOWN_PROFILE_DUMPER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/files/file_path.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/ref_counted_memory.h"
14#include "content/common/content_export.h"
15
16namespace base {
17class FilePath;
18class WaitableEvent;
19}
20
21namespace content {
22
23// This class is intended to dump the tracing results of the shutdown process
24// to a file before the browser process exits.
25// It will save the file either into the command line passed
26// "--trace-shutdown-file=<name>" parameter - or - to "chrometrace.log" in the
27// current directory.
28// Use the class with a scoped_ptr to get files written in the destructor.
29// Note that we cannot use the asynchronous file writer since the
30// |SequencedWorkerPool| will get killed in the shutdown process.
31class BrowserShutdownProfileDumper {
32 public:
33  explicit BrowserShutdownProfileDumper(const base::FilePath& dump_file_name);
34
35  ~BrowserShutdownProfileDumper();
36
37  // Returns the file name where we should save the shutdown trace dump to.
38  static base::FilePath GetShutdownProfileFileName();
39
40 private:
41  // Writes all traces which happened to disk.
42  void WriteTracesToDisc();
43
44  void EndTraceAndFlush(base::WaitableEvent* flush_complete_event);
45
46  // The callback for the |TraceLog::Flush| function. It saves all traces to
47  // disc.
48  void WriteTraceDataCollected(
49      base::WaitableEvent* flush_complete_event,
50      const scoped_refptr<base::RefCountedString>& events_str,
51      bool has_more_events);
52
53  // Returns true if the dump file is valid.
54  bool IsFileValid();
55
56  // Writes a string to the dump file.
57  void WriteString(const std::string& string);
58
59  // Write a buffer to the dump file.
60  void WriteChars(const char* chars, size_t size);
61
62  // Closes the dump file.
63  void CloseFile();
64
65  // The name of the dump file.
66  const base::FilePath dump_file_name_;
67
68  // The number of blocks we have already written.
69  int blocks_;
70  // For dumping the content to disc.
71  FILE* dump_file_;
72
73  DISALLOW_COPY_AND_ASSIGN(BrowserShutdownProfileDumper);
74};
75
76}  // namespace content
77
78#endif  // CONTENT_BROWSER_BROWSER_SHUTDOWN_PROFILE_DUMPER_H_
79