Implement crash-reporter.getLastLastCrashReport API on OS X.

This commit is contained in:
Haojian Wu 2015-06-03 09:47:42 +08:00
parent 2396b51cb6
commit 4457edb1d3
6 changed files with 72 additions and 0 deletions

View file

@ -31,10 +31,34 @@ struct Converter<std::map<std::string, std::string> > {
}
};
template<>
struct Converter<std::vector<crash_reporter::CrashReporter::UploadReportResult> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::vector<
crash_reporter::CrashReporter::UploadReportResult>& reports) {
v8::Local<v8::Array> result(v8::Array::New(isolate, reports.size()));
for (size_t i = 0; i < reports.size(); ++i) {
mate::Dictionary dict(isolate, v8::Object::New(isolate));
dict.Set("date", reports[i].first);
dict.Set("id", reports[i].second);
v8::TryCatch try_catch;
result->Set(static_cast<uint32>(i), dict.GetHandle());
if (try_catch.HasCaught())
LOG(ERROR) << "Setter for index " << i << " threw an exception.";
}
return result;
}
};
} // namespace mate
namespace {
std::vector<crash_reporter::CrashReporter::UploadReportResult>
GetUploadedReports() {
return (crash_reporter::CrashReporter::GetInstance())->GetUploadedReports();
}
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
using crash_reporter::CrashReporter;
@ -42,6 +66,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
dict.SetMethod("start",
base::Bind(&CrashReporter::Start,
base::Unretained(CrashReporter::GetInstance())));
dict.SetMethod("_getUploadedReports", &GetUploadedReports);
}
} // namespace

View file

@ -41,6 +41,10 @@ class CrashReporter
start()
getLastCrashReport: ->
if process.platform is 'darwin'
reports = binding._getUploadedReports()
return if reports.length > 0 then reports[0] else null
tmpdir =
if process.platform is 'win32'
os.tmpdir()

View file

@ -39,4 +39,8 @@ void CrashReporter::SetUploadParameters(const StringMap& parameters) {
SetUploadParameters();
}
std::vector<CrashReporter::UploadReportResult> CrashReporter::GetUploadedReports() {
return std::vector<CrashReporter::UploadReportResult>();
}
} // namespace crash_reporter

View file

@ -7,6 +7,7 @@
#include <map>
#include <string>
#include <vector>
#include "base/basictypes.h"
@ -15,6 +16,7 @@ namespace crash_reporter {
class CrashReporter {
public:
typedef std::map<std::string, std::string> StringMap;
typedef std::pair<int, std::string> UploadReportResult; // upload-date, id
static CrashReporter* GetInstance();
@ -25,6 +27,8 @@ class CrashReporter {
bool skip_system_crash_handler,
const StringMap& extra_parameters);
virtual std::vector<CrashReporter::UploadReportResult> GetUploadedReports();
protected:
CrashReporter();
virtual ~CrashReporter();

View file

@ -43,6 +43,8 @@ class CrashReporterMac : public CrashReporter {
void SetCrashKeyValue(const base::StringPiece& key,
const base::StringPiece& value);
std::vector<UploadReportResult> GetUploadedReports() override;
scoped_ptr<crashpad::SimpleStringDictionary> simple_string_dictionary_;
scoped_ptr<crashpad::CrashReportDatabase> crash_report_database_;

View file

@ -96,6 +96,39 @@ void CrashReporterMac::SetCrashKeyValue(const base::StringPiece& key,
simple_string_dictionary_->SetKeyValue(key.data(), value.data());
}
std::vector<CrashReporter::UploadReportResult>
CrashReporterMac::GetUploadedReports() {
std::vector<CrashReporter::UploadReportResult> uploaded_reports;
if (!crash_report_database_) {
return uploaded_reports;
}
std::vector<crashpad::CrashReportDatabase::Report> completed_reports;
crashpad::CrashReportDatabase::OperationStatus status =
crash_report_database_->GetCompletedReports(&completed_reports);
if (status != crashpad::CrashReportDatabase::kNoError) {
return uploaded_reports;
}
for (const crashpad::CrashReportDatabase::Report& completed_report :
completed_reports) {
if (completed_report.uploaded) {
uploaded_reports.push_back(
UploadReportResult(static_cast<int>(completed_report.creation_time),
completed_report.id));
}
}
struct {
bool operator()(const UploadReportResult& a, const UploadReportResult& b) {
return a.first >= b.first;
}
} sort_by_time;
std::sort(uploaded_reports.begin(), uploaded_reports.end(), sort_by_time);
return uploaded_reports;
}
// static
CrashReporterMac* CrashReporterMac::GetInstance() {
return Singleton<CrashReporterMac>::get();