Index: components/browser_watcher/stability_report_user_stream_data_source.cc |
diff --git a/components/browser_watcher/stability_report_user_stream_data_source.cc b/components/browser_watcher/stability_report_user_stream_data_source.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ea23c7d58932e690917908a7b4e7ecb79bc612b0 |
--- /dev/null |
+++ b/components/browser_watcher/stability_report_user_stream_data_source.cc |
@@ -0,0 +1,123 @@ |
+// Copyright 2017 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. |
+ |
+#include "components/browser_watcher/stability_report_user_stream_data_source.h" |
+ |
+#include <string> |
+#include <utility> |
+ |
+#include "base/files/file.h" |
+#include "base/files/file_util.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/strings/string16.h" |
+#include "base/time/time.h" |
+#include "components/browser_watcher/minidump_user_streams.h" |
+#include "components/browser_watcher/stability_paths.h" |
+#include "components/browser_watcher/stability_report_extractor.h" |
+#include "third_party/crashpad/crashpad/minidump/minidump_user_extension_stream_data_source.h" |
+#include "third_party/crashpad/crashpad/snapshot/process_snapshot.h" |
+ |
+namespace browser_watcher { |
+ |
+namespace { |
+ |
+bool GetStabilityFileName(const base::string16& user_data_dir, |
+ crashpad::ProcessSnapshot* process_snapshot, |
+ base::FilePath* stability_file) { |
+ DCHECK(process_snapshot); |
+ DCHECK(stability_file); |
+ |
+ timeval creation_time{}; |
+ process_snapshot->ProcessStartTime(&creation_time); |
+ |
+ *stability_file = |
+ GetStabilityFileForProcess(process_snapshot->ProcessID(), creation_time, |
+ base::FilePath(user_data_dir)); |
+ return true; |
+} |
+ |
+std::unique_ptr<std::string> CollectReport(const base::FilePath& path) { |
+ StabilityReport report; |
+ CollectionStatus status = Extract(path, &report); |
+ if (status != SUCCESS) |
+ return nullptr; |
+ |
+ std::unique_ptr<std::string> serialized_report(new std::string()); |
+ if (!report.SerializeToString(serialized_report.get())) |
+ return nullptr; |
+ |
+ // Open (with delete) and then immediately close the file by going out of |
+ // scope. This should cause the stability debugging file to be deleted prior |
+ // to the next execution. |
+ // TODO(manzagop): set the persistent allocator file's state to deleted in |
+ // case the file can't be deleted. |
+ base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ | |
+ base::File::FLAG_DELETE_ON_CLOSE); |
+ |
+ return serialized_report; |
+} |
+ |
+class BufferExtensionStreamDataSource final |
+ : public crashpad::MinidumpUserExtensionStreamDataSource { |
+ public: |
+ BufferExtensionStreamDataSource(uint32_t stream_type, |
+ std::unique_ptr<std::string> data); |
Sigurður Ásgeirsson
2017/05/11 13:48:32
why not construct this with the StabilityReport pr
manzagop (departed)
2017/05/11 14:54:45
Done. Note the string might be empty if the serial
Sigurður Ásgeirsson
2017/05/11 15:33:40
Interesting distinction, is this helpful or harmfu
manzagop (departed)
2017/05/11 19:14:53
Went with early pruning. Done.
|
+ |
+ size_t StreamDataSize() override; |
+ bool ReadStreamData(Delegate* delegate) override; |
+ |
+ private: |
+ std::unique_ptr<std::string> data_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BufferExtensionStreamDataSource); |
+}; |
+ |
+BufferExtensionStreamDataSource::BufferExtensionStreamDataSource( |
+ uint32_t stream_type, |
+ std::unique_ptr<std::string> data) |
+ : crashpad::MinidumpUserExtensionStreamDataSource(stream_type), |
+ data_(std::move(data)) { |
+ DCHECK(data_); |
+} |
+ |
+size_t BufferExtensionStreamDataSource::StreamDataSize() { |
+ return data_->size(); |
+} |
+ |
+bool BufferExtensionStreamDataSource::ReadStreamData(Delegate* delegate) { |
+ return delegate->ExtensionStreamDataSourceRead( |
+ data_->size() ? data_->data() : nullptr, data_->size()); |
+} |
+ |
+} // namespace |
+ |
+StabilityReportUserStreamDataSource::StabilityReportUserStreamDataSource( |
+ const base::string16& user_data_dir) |
+ : user_data_dir_(user_data_dir) {} |
+ |
+std::unique_ptr<crashpad::MinidumpUserExtensionStreamDataSource> |
+StabilityReportUserStreamDataSource::ProduceStreamData( |
+ crashpad::ProcessSnapshot* process_snapshot) { |
+ DCHECK(process_snapshot); |
+ |
+ if (user_data_dir_.empty()) |
+ return nullptr; |
+ |
+ base::FilePath stability_file; |
+ GetStabilityFileName(user_data_dir_, process_snapshot, &stability_file); |
+ if (!PathExists(stability_file)) { |
+ // Either this is not an instrumented process, or the stability file cannot |
+ // be found. |
+ return nullptr; |
+ } |
+ |
+ std::unique_ptr<std::string> report = CollectReport(stability_file); |
Sigurður Ásgeirsson
2017/05/11 13:48:32
yups, just return the extension stream thingy from
manzagop (departed)
2017/05/11 14:54:45
Done.
|
+ if (!report) |
+ return nullptr; |
+ |
+ return base::WrapUnique(new BufferExtensionStreamDataSource( |
+ kStabilityReportStreamType, std::move(report))); |
+} |
+ |
+} // namespace browser_watcher |