Implement crash-reporter.getUploadedReports API.

Also redefine the getLastCrashReport API implementation using
getUploadedReports API.
This commit is contained in:
Haojian Wu 2015-06-05 18:50:52 +08:00
parent 129159c895
commit c821a06e2f
5 changed files with 50 additions and 33 deletions

View file

@ -41,23 +41,23 @@ class CrashReporter
start()
getLastCrashReport: ->
if process.platform is 'darwin'
reports = binding._getUploadedReports()
return if reports.length > 0 then reports[0] else null
reports = this.getUploadedReports()
if reports.length > 0 then reports[0] else null
getUploadedReports: ->
tmpdir =
if process.platform is 'win32'
os.tmpdir()
else
'/tmp'
log = path.join tmpdir, "#{@productName} Crashes", 'uploads.log'
try
reports = String(fs.readFileSync(log)).split('\n')
return null unless reports.length > 1
[time, id] = reports[reports.length - 2].split ','
return {date: new Date(parseInt(time) * 1000), id}
catch e
return null
log =
if process.platform is 'darwin'
path.join tmpdir, "#{@productName} Crashes"
else
path.join tmpdir, "#{@productName} Crashes", 'uploads.log'
console.log log;
binding._getUploadedReports(log)
crashRepoter = new CrashReporter
module.exports = crashRepoter

View file

@ -7,6 +7,9 @@
#include "atom/browser/browser.h"
#include "atom/common/atom_version.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/strings/string_split.h"
#include "base/strings/string_number_conversions.h"
#include "content/public/common/content_switches.h"
namespace crash_reporter {
@ -40,8 +43,24 @@ void CrashReporter::SetUploadParameters(const StringMap& parameters) {
}
std::vector<CrashReporter::UploadReportResult>
CrashReporter::GetUploadedReports() {
return std::vector<CrashReporter::UploadReportResult>();
CrashReporter::GetUploadedReports(const std::string& path) {
std::string file_content;
std::vector<CrashReporter::UploadReportResult> result;
if (base::ReadFileToString(base::FilePath(path), &file_content)) {
std::vector<std::string> reports;
base::SplitString(file_content, '\n', &reports);
for (const std::string& report : reports) {
std::vector<std::string> report_item;
base::SplitString(report, ',', &report_item);
int report_time = 0;
if (report_item.size() >= 2 && base::StringToInt(report_item[0],
&report_time)) {
result.push_back(CrashReporter::UploadReportResult(report_time,
report_item[1]));
}
}
}
return result;
}
} // namespace crash_reporter

View file

@ -28,7 +28,8 @@ class CrashReporter {
bool skip_system_crash_handler,
const StringMap& extra_parameters);
virtual std::vector<CrashReporter::UploadReportResult> GetUploadedReports();
virtual std::vector<CrashReporter::UploadReportResult> GetUploadedReports(
const std::string& path);
protected:
CrashReporter();

View file

@ -16,10 +16,6 @@
template <typename T> struct DefaultSingletonTraits;
namespace crashpad {
class CrashReportDatabase;
}
namespace crash_reporter {
class CrashReporterMac : public CrashReporter {
@ -44,10 +40,10 @@ class CrashReporterMac : public CrashReporter {
void SetCrashKeyValue(const base::StringPiece& key,
const base::StringPiece& value);
std::vector<UploadReportResult> GetUploadedReports() override;
std::vector<UploadReportResult> GetUploadedReports(
const std::string& path) override;
scoped_ptr<crashpad::SimpleStringDictionary> simple_string_dictionary_;
scoped_ptr<crashpad::CrashReportDatabase> crash_report_database_;
DISALLOW_COPY_AND_ASSIGN(CrashReporterMac);
};

View file

@ -5,6 +5,7 @@
#include "atom/common/crash_reporter/crash_reporter_mac.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/mac/bundle_locations.h"
#include "base/mac/mac_util.h"
#include "base/memory/singleton.h"
@ -71,9 +72,11 @@ void CrashReporterMac::InitBreakpad(const std::string& product_name,
SetCrashKeyValue(upload_parameter.first, upload_parameter.second);
}
if (is_browser_) {
crash_report_database_ = crashpad::CrashReportDatabase::Initialize(
database_path);
SetUploadsEnabled(auto_submit);
scoped_ptr<crashpad::CrashReportDatabase> database =
crashpad::CrashReportDatabase::Initialize(database_path);
if (database) {
database->GetSettings()->SetUploadsEnabled(auto_submit);
}
}
}
@ -81,29 +84,27 @@ void CrashReporterMac::SetUploadParameters() {
upload_parameters_["platform"] = "darwin";
}
void CrashReporterMac::SetUploadsEnabled(bool enable_uploads) {
if (crash_report_database_) {
crashpad::Settings* settings = crash_report_database_->GetSettings();
settings->SetUploadsEnabled(enable_uploads);
}
}
void CrashReporterMac::SetCrashKeyValue(const base::StringPiece& key,
const base::StringPiece& value) {
simple_string_dictionary_->SetKeyValue(key.data(), value.data());
}
std::vector<CrashReporter::UploadReportResult>
CrashReporterMac::GetUploadedReports() {
CrashReporterMac::GetUploadedReports(const std::string& path) {
std::vector<CrashReporter::UploadReportResult> uploaded_reports;
if (!crash_report_database_) {
base::FilePath file_path(path);
if (!base::PathExists(file_path)) {
return uploaded_reports;
}
// Load crashpad database.
scoped_ptr<crashpad::CrashReportDatabase> database =
crashpad::CrashReportDatabase::Initialize(file_path);
DCHECK(database);
std::vector<crashpad::CrashReportDatabase::Report> completed_reports;
crashpad::CrashReportDatabase::OperationStatus status =
crash_report_database_->GetCompletedReports(&completed_reports);
database->GetCompletedReports(&completed_reports);
if (status != crashpad::CrashReportDatabase::kNoError) {
return uploaded_reports;
}