toolkit/crashreporter/content/crashes.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/content/crashes.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,183 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const { classes: Cc, utils: Cu, interfaces: Ci } = Components;
     1.9 +
    1.10 +var reportURL;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/CrashReports.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +Cu.import("resource://gre/modules/Task.jsm");
    1.16 +Cu.import("resource://gre/modules/osfile.jsm");
    1.17 +
    1.18 +XPCOMUtils.defineLazyModuleGetter(this, "CrashSubmit",
    1.19 +  "resource://gre/modules/CrashSubmit.jsm");
    1.20 +
    1.21 +const buildID = Services.appinfo.appBuildID;
    1.22 +
    1.23 +function submitSuccess(dumpid, ret) {
    1.24 +  let link = document.getElementById(dumpid);
    1.25 +  if (link) {
    1.26 +    link.className = "";
    1.27 +    // reset the link to point at our new crash report. this way, if the
    1.28 +    // user clicks "Back", the link will be correct.
    1.29 +    let CrashID = ret.CrashID;
    1.30 +    link.firstChild.textContent = CrashID;
    1.31 +    link.setAttribute("id", CrashID);
    1.32 +    link.removeEventListener("click", submitPendingReport, true);
    1.33 +
    1.34 +    if (reportURL) {
    1.35 +      link.setAttribute("href", reportURL + CrashID);
    1.36 +      // redirect the user to their brand new crash report
    1.37 +      window.location.href = reportURL + CrashID;
    1.38 +    }
    1.39 +  }
    1.40 +}
    1.41 +
    1.42 +function submitError(dumpid) {
    1.43 +  //XXX: do something more useful here
    1.44 +  let link = document.getElementById(dumpid);
    1.45 +  if (link)
    1.46 +    link.className = "";
    1.47 +  // dispatch an event, useful for testing
    1.48 +  let event = document.createEvent("Events");
    1.49 +  event.initEvent("CrashSubmitFailed", true, false);
    1.50 +  document.dispatchEvent(event);
    1.51 +}
    1.52 +
    1.53 +function submitPendingReport(event) {
    1.54 +  var link = event.target;
    1.55 +  var id = link.firstChild.textContent;
    1.56 +  if (CrashSubmit.submit(id, { submitSuccess: submitSuccess,
    1.57 +                               submitError: submitError,
    1.58 +                               noThrottle: true })) {
    1.59 +    link.className = "submitting";
    1.60 +  }
    1.61 +  event.preventDefault();
    1.62 +  return false;
    1.63 +}
    1.64 +
    1.65 +function populateReportList() {
    1.66 +  var prefService = Cc["@mozilla.org/preferences-service;1"].
    1.67 +                    getService(Ci.nsIPrefBranch);
    1.68 +
    1.69 +  try {
    1.70 +    reportURL = prefService.getCharPref("breakpad.reportURL");
    1.71 +    // Ignore any non http/https urls
    1.72 +    if (!/^https?:/i.test(reportURL))
    1.73 +      reportURL = null;
    1.74 +  }
    1.75 +  catch (e) { }
    1.76 +  if (!reportURL) {
    1.77 +    document.getElementById("clear-reports").style.display = "none";
    1.78 +    document.getElementById("reportList").style.display = "none";
    1.79 +    document.getElementById("noConfig").style.display = "block";
    1.80 +    return;
    1.81 +  }
    1.82 +  let reports = CrashReports.getReports();
    1.83 +
    1.84 +  if (reports.length == 0) {
    1.85 +    document.getElementById("clear-reports").style.display = "none";
    1.86 +    document.getElementById("reportList").style.display = "none";
    1.87 +    document.getElementById("noReports").style.display = "block";
    1.88 +    return;
    1.89 +  }
    1.90 +
    1.91 +  var formatter = Cc["@mozilla.org/intl/scriptabledateformat;1"].
    1.92 +                  createInstance(Ci.nsIScriptableDateFormat);
    1.93 +  var body = document.getElementById("tbody");
    1.94 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.95 +            getService(Ci.nsIIOService);
    1.96 +  var reportURI = ios.newURI(reportURL, null, null);
    1.97 +  // resolving this URI relative to /report/index
    1.98 +  var aboutThrottling = ios.newURI("../../about/throttling", null, reportURI);
    1.99 +
   1.100 +  for (var i = 0; i < reports.length; i++) {
   1.101 +    var row = document.createElement("tr");
   1.102 +    var cell = document.createElement("td");
   1.103 +    row.appendChild(cell);
   1.104 +    var link = document.createElement("a");
   1.105 +    if (reports[i].pending) {
   1.106 +      link.setAttribute("href", aboutThrottling.spec);
   1.107 +      link.addEventListener("click", submitPendingReport, true);
   1.108 +    }
   1.109 +    else {
   1.110 +      link.setAttribute("href", reportURL + reports[i].id);
   1.111 +    }
   1.112 +    link.setAttribute("id", reports[i].id);
   1.113 +    link.appendChild(document.createTextNode(reports[i].id));
   1.114 +    cell.appendChild(link);
   1.115 +
   1.116 +    var date = new Date(reports[i].date);
   1.117 +    cell = document.createElement("td");
   1.118 +    var datestr = formatter.FormatDate("",
   1.119 +                                       Ci.nsIScriptableDateFormat.dateFormatShort,
   1.120 +                                       date.getFullYear(),
   1.121 +                                       date.getMonth() + 1,
   1.122 +                                       date.getDate());
   1.123 +    cell.appendChild(document.createTextNode(datestr));
   1.124 +    row.appendChild(cell);
   1.125 +    cell = document.createElement("td");
   1.126 +    var timestr = formatter.FormatTime("",
   1.127 +                                       Ci.nsIScriptableDateFormat.timeFormatNoSeconds,
   1.128 +                                       date.getHours(),
   1.129 +                                       date.getMinutes(),
   1.130 +                                       date.getSeconds());
   1.131 +    cell.appendChild(document.createTextNode(timestr));
   1.132 +    row.appendChild(cell);
   1.133 +    body.appendChild(row);
   1.134 +  }
   1.135 +}
   1.136 +
   1.137 +let clearReports = Task.async(function*() {
   1.138 +  let bundle = Services.strings.createBundle("chrome://global/locale/crashes.properties");
   1.139 +
   1.140 +  if (!Services.
   1.141 +         prompt.confirm(window,
   1.142 +                        bundle.GetStringFromName("deleteconfirm.title"),
   1.143 +                        bundle.GetStringFromName("deleteconfirm.description"))) {
   1.144 +    return;
   1.145 +  }
   1.146 +
   1.147 +  let cleanupFolder = Task.async(function*(path, filter) {
   1.148 +    let iterator = new OS.File.DirectoryIterator(path);
   1.149 +    try {
   1.150 +      yield iterator.forEach(Task.async(function*(aEntry) {
   1.151 +        if (!filter || (yield filter(aEntry))) {
   1.152 +          yield OS.File.remove(aEntry.path);
   1.153 +        }
   1.154 +      }));
   1.155 +    } catch (e if e instanceof OS.File.Error && e.becauseNoSuchFile) {
   1.156 +    } finally {
   1.157 +      iterator.close();
   1.158 +    }
   1.159 +  });
   1.160 +
   1.161 +  yield cleanupFolder(CrashReports.submittedDir.path, function*(aEntry) {
   1.162 +    return aEntry.name.startsWith("bp-") && aEntry.name.endsWith(".txt");
   1.163 +  });
   1.164 +
   1.165 +  let oneYearAgo = Date.now() - 31586000000;
   1.166 +  yield cleanupFolder(CrashReports.reportsDir.path, function*(aEntry) {
   1.167 +    if (!aEntry.name.startsWith("InstallTime") ||
   1.168 +        aEntry.name == "InstallTime" + buildID) {
   1.169 +      return false;
   1.170 +    }
   1.171 +
   1.172 +    let date = aEntry.winLastWriteDate;
   1.173 +    if (!date) {
   1.174 +      let stat = yield OS.File.stat(aEntry.path);
   1.175 +      date = stat.lastModificationDate;
   1.176 +    }
   1.177 +
   1.178 +    return (date < oneYearAgo);
   1.179 +  });
   1.180 +
   1.181 +  yield cleanupFolder(CrashReports.pendingDir.path);
   1.182 +
   1.183 +  document.getElementById("clear-reports").style.display = "none";
   1.184 +  document.getElementById("reportList").style.display = "none";
   1.185 +  document.getElementById("noReports").style.display = "block";
   1.186 +});

mercurial