Index: components/browser_watcher/postmortem_minidump_writer.h |
diff --git a/components/browser_watcher/postmortem_minidump_writer.h b/components/browser_watcher/postmortem_minidump_writer.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..632e3a2ee0b06e42beabd2bf1edffb417f95fca4 |
--- /dev/null |
+++ b/components/browser_watcher/postmortem_minidump_writer.h |
@@ -0,0 +1,80 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_BROWSER_WATCHER_POSTMORTEM_MINIDUMP_WRITER_H_ |
+#define COMPONENTS_BROWSER_WATCHER_POSTMORTEM_MINIDUMP_WRITER_H_ |
+ |
+#include <stdint.h> |
+ |
+#include "base/files/file_path.h" |
+#include "base/files/scoped_file.h" |
+#include "base/macros.h" |
+#include "base/strings/string_piece.h" |
+#include "components/browser_watcher/stability_report.pb.h" |
+ |
+namespace browser_watcher { |
+ |
+// A class with functionality for writing minimal minidump containers to wrap |
+// postmortem stability reports. |
+// TODO(manzagop): remove this class once CrashPad takes over writing postmortem |
+// minidumps. |
+class PostmortemMinidumpWriter { |
+ public: |
+ using Position = uint32_t; |
+ |
+ PostmortemMinidumpWriter(); |
+ ~PostmortemMinidumpWriter() = default; |
+ |
+ // Write to |minidump| a minimal minidump that wraps |report|. Returns true on |
+ // success, false otherwise. |
+ bool WriteDump(const base::FilePath& minidump, const StabilityReport& report); |
+ |
+ private: |
+ // The minidump header is always located at the head. |
+ static const Position kHeaderPos = 0U; |
+ |
+ // Allocate |size_bytes| within the minidump. On succress, |pos| contains the |
scottmg
2016/08/03 22:47:06
success
manzagop (departed)
2016/08/10 15:59:51
Done.
|
+ // location of the allocation. Returns true on success, false otherwise. |
+ bool Allocate(size_t size_bytes, Position* pos); |
+ |
+ // Write to pre-allocated space. |
+ template <class DataType> |
+ bool Write(Position pos, const DataType& data); |
+ bool WriteBytes(Position pos, size_t size_bytes, const void* data); |
+ |
+ // Allocate space for and write the contents of |data|. On success, |pos| |
+ // contains the location of the write. Returns true on success, false |
+ // otherwise. |
+ template <class DataType> |
+ bool Append(const DataType& data, Position* pos); |
+ bool AppendBytes(base::StringPiece data, Position* pos); |
+ |
+ // Safely increment |cursor_| by |size_bytes|. |
+ bool IncrementCursor(size_t size_bytes); |
+ |
+ // The next allocatable position. |
+ Position cursor_; |
+ |
+ // The file to write to. |
+ base::ScopedFILE minidump_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PostmortemMinidumpWriter); |
+}; |
+ |
+template <class DataType> |
+bool PostmortemMinidumpWriter::Write(Position pos, const DataType& data) { |
+ return WriteBytes(pos, sizeof(data), &data); |
+} |
+ |
+template <class DataType> |
+bool PostmortemMinidumpWriter::Append(const DataType& data, Position* pos) { |
+ DCHECK(pos); |
+ if (!Allocate(sizeof(data), pos)) |
+ return false; |
+ return Write(*pos, data); |
+} |
+ |
+} // namespace browser_watcher |
+ |
+#endif // COMPONENTS_BROWSER_WATCHER_POSTMORTEM_MINIDUMP_WRITER_H_ |