Implement crash-reporter.getUploadedReports API.
authorHaojian Wu <hokein.wu@gmail.com>
Fri, 5 Jun 2015 10:50:52 +0000 (18:50 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Fri, 5 Jun 2015 10:59:03 +0000 (18:59 +0800)
Also redefine the getLastCrashReport API implementation using
getUploadedReports API.

atom/common/api/lib/crash-reporter.coffee
atom/common/crash_reporter/crash_reporter.cc
atom/common/crash_reporter/crash_reporter.h
atom/common/crash_reporter/crash_reporter_mac.h
atom/common/crash_reporter/crash_reporter_mac.mm

index c151ff7..868cda2 100644 (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
index 25dbf60..365860b 100644 (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
index 6cfa5b3..c7d58ca 100644 (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();
index ca31237..cbdb3c6 100644 (file)
 
 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);
 };
index 4edd35a..00f37cc 100644 (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;
   }